activerecord 2.3.18 → 3.2.22
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1014 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +222 -0
- data/examples/performance.rb +100 -126
- data/examples/simple.rb +14 -0
- data/lib/active_record/aggregations.rb +93 -99
- data/lib/active_record/associations/alias_tracker.rb +76 -0
- data/lib/active_record/associations/association.rb +247 -0
- data/lib/active_record/associations/association_scope.rb +134 -0
- data/lib/active_record/associations/belongs_to_association.rb +54 -61
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +17 -59
- data/lib/active_record/associations/builder/association.rb +55 -0
- data/lib/active_record/associations/builder/belongs_to.rb +88 -0
- data/lib/active_record/associations/builder/collection_association.rb +75 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +57 -0
- data/lib/active_record/associations/builder/has_many.rb +71 -0
- data/lib/active_record/associations/builder/has_one.rb +62 -0
- data/lib/active_record/associations/builder/singular_association.rb +32 -0
- data/lib/active_record/associations/collection_association.rb +580 -0
- data/lib/active_record/associations/collection_proxy.rb +133 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +39 -119
- data/lib/active_record/associations/has_many_association.rb +60 -79
- data/lib/active_record/associations/has_many_through_association.rb +127 -206
- data/lib/active_record/associations/has_one_association.rb +55 -114
- data/lib/active_record/associations/has_one_through_association.rb +25 -26
- data/lib/active_record/associations/join_dependency/join_association.rb +159 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
- data/lib/active_record/associations/join_dependency.rb +214 -0
- data/lib/active_record/associations/join_helper.rb +55 -0
- data/lib/active_record/associations/preloader/association.rb +125 -0
- data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
- data/lib/active_record/associations/preloader/collection_association.rb +24 -0
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
- data/lib/active_record/associations/preloader/has_many.rb +17 -0
- data/lib/active_record/associations/preloader/has_many_through.rb +15 -0
- data/lib/active_record/associations/preloader/has_one.rb +23 -0
- data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
- data/lib/active_record/associations/preloader/singular_association.rb +21 -0
- data/lib/active_record/associations/preloader/through_association.rb +67 -0
- data/lib/active_record/associations/preloader.rb +181 -0
- data/lib/active_record/associations/singular_association.rb +64 -0
- data/lib/active_record/associations/through_association.rb +87 -0
- data/lib/active_record/associations.rb +693 -1337
- data/lib/active_record/attribute_assignment.rb +221 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +31 -0
- data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +32 -0
- data/lib/active_record/attribute_methods/dirty.rb +111 -0
- data/lib/active_record/attribute_methods/primary_key.rb +114 -0
- data/lib/active_record/attribute_methods/query.rb +39 -0
- data/lib/active_record/attribute_methods/read.rb +136 -0
- data/lib/active_record/attribute_methods/serialization.rb +120 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +65 -0
- data/lib/active_record/attribute_methods/write.rb +70 -0
- data/lib/active_record/attribute_methods.rb +211 -339
- data/lib/active_record/autosave_association.rb +179 -149
- data/lib/active_record/base.rb +401 -2907
- data/lib/active_record/callbacks.rb +91 -176
- data/lib/active_record/coders/yaml_column.rb +41 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +236 -119
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +110 -58
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +12 -11
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +175 -74
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +31 -35
- data/lib/active_record/connection_adapters/abstract/quoting.rb +71 -21
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +81 -311
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +194 -78
- data/lib/active_record/connection_adapters/abstract_adapter.rb +130 -83
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +676 -0
- data/lib/active_record/connection_adapters/column.rb +296 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +280 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +272 -493
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +650 -405
- data/lib/active_record/connection_adapters/schema_cache.rb +69 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +30 -9
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +276 -147
- data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
- data/lib/active_record/counter_cache.rb +123 -0
- data/lib/active_record/dynamic_finder_match.rb +41 -14
- data/lib/active_record/dynamic_matchers.rb +84 -0
- data/lib/active_record/dynamic_scope_match.rb +13 -15
- data/lib/active_record/errors.rb +195 -0
- data/lib/active_record/explain.rb +86 -0
- data/lib/active_record/explain_subscriber.rb +25 -0
- data/lib/active_record/fixtures/file.rb +65 -0
- data/lib/active_record/fixtures.rb +695 -770
- data/lib/active_record/identity_map.rb +162 -0
- data/lib/active_record/inheritance.rb +174 -0
- data/lib/active_record/integration.rb +60 -0
- data/lib/active_record/locale/en.yml +9 -27
- data/lib/active_record/locking/optimistic.rb +76 -73
- data/lib/active_record/locking/pessimistic.rb +32 -10
- data/lib/active_record/log_subscriber.rb +72 -0
- data/lib/active_record/migration/command_recorder.rb +105 -0
- data/lib/active_record/migration.rb +415 -205
- data/lib/active_record/model_schema.rb +368 -0
- data/lib/active_record/nested_attributes.rb +153 -63
- data/lib/active_record/observer.rb +27 -103
- data/lib/active_record/persistence.rb +376 -0
- data/lib/active_record/query_cache.rb +49 -8
- data/lib/active_record/querying.rb +58 -0
- data/lib/active_record/railtie.rb +131 -0
- data/lib/active_record/railties/console_sandbox.rb +6 -0
- data/lib/active_record/railties/controller_runtime.rb +49 -0
- data/lib/active_record/railties/databases.rake +659 -0
- data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
- data/lib/active_record/readonly_attributes.rb +26 -0
- data/lib/active_record/reflection.rb +269 -120
- data/lib/active_record/relation/batches.rb +90 -0
- data/lib/active_record/relation/calculations.rb +372 -0
- data/lib/active_record/relation/delegation.rb +49 -0
- data/lib/active_record/relation/finder_methods.rb +402 -0
- data/lib/active_record/relation/predicate_builder.rb +63 -0
- data/lib/active_record/relation/query_methods.rb +417 -0
- data/lib/active_record/relation/spawn_methods.rb +180 -0
- data/lib/active_record/relation.rb +537 -0
- data/lib/active_record/result.rb +40 -0
- data/lib/active_record/sanitization.rb +194 -0
- data/lib/active_record/schema.rb +9 -6
- data/lib/active_record/schema_dumper.rb +55 -32
- data/lib/active_record/scoping/default.rb +142 -0
- data/lib/active_record/scoping/named.rb +200 -0
- data/lib/active_record/scoping.rb +152 -0
- data/lib/active_record/serialization.rb +8 -91
- data/lib/active_record/serializers/xml_serializer.rb +43 -197
- data/lib/active_record/session_store.rb +129 -103
- data/lib/active_record/store.rb +52 -0
- data/lib/active_record/test_case.rb +30 -23
- data/lib/active_record/timestamp.rb +95 -52
- data/lib/active_record/transactions.rb +212 -66
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/validations/associated.rb +43 -0
- data/lib/active_record/validations/uniqueness.rb +180 -0
- data/lib/active_record/validations.rb +43 -1106
- data/lib/active_record/version.rb +5 -4
- data/lib/active_record.rb +121 -48
- data/lib/rails/generators/active_record/migration/migration_generator.rb +25 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +34 -0
- data/lib/rails/generators/active_record/migration.rb +15 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +47 -0
- data/lib/rails/generators/active_record/model/templates/migration.rb +15 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +12 -0
- data/lib/rails/generators/active_record/model/templates/module.rb +7 -0
- data/lib/rails/generators/active_record/observer/observer_generator.rb +15 -0
- data/lib/rails/generators/active_record/observer/templates/observer.rb +4 -0
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +25 -0
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +12 -0
- data/lib/rails/generators/active_record.rb +25 -0
- metadata +187 -363
- data/CHANGELOG +0 -5904
- data/README +0 -351
- data/RUNNING_UNIT_TESTS +0 -36
- data/Rakefile +0 -268
- data/install.rb +0 -30
- data/lib/active_record/association_preload.rb +0 -406
- data/lib/active_record/associations/association_collection.rb +0 -533
- data/lib/active_record/associations/association_proxy.rb +0 -288
- data/lib/active_record/batches.rb +0 -85
- data/lib/active_record/calculations.rb +0 -321
- data/lib/active_record/dirty.rb +0 -183
- data/lib/active_record/named_scope.rb +0 -197
- data/lib/active_record/serializers/json_serializer.rb +0 -91
- data/lib/activerecord.rb +0 -2
- data/test/assets/example.log +0 -1
- data/test/assets/flowers.jpg +0 -0
- data/test/cases/aaa_create_tables_test.rb +0 -24
- data/test/cases/active_schema_test_mysql.rb +0 -122
- data/test/cases/active_schema_test_postgresql.rb +0 -24
- data/test/cases/adapter_test.rb +0 -144
- data/test/cases/aggregations_test.rb +0 -167
- data/test/cases/ar_schema_test.rb +0 -32
- data/test/cases/associations/belongs_to_associations_test.rb +0 -438
- data/test/cases/associations/callbacks_test.rb +0 -161
- data/test/cases/associations/cascaded_eager_loading_test.rb +0 -131
- data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +0 -36
- data/test/cases/associations/eager_load_nested_include_test.rb +0 -131
- data/test/cases/associations/eager_load_nested_polymorphic_include.rb +0 -19
- data/test/cases/associations/eager_singularization_test.rb +0 -145
- data/test/cases/associations/eager_test.rb +0 -852
- data/test/cases/associations/extension_test.rb +0 -62
- data/test/cases/associations/habtm_join_table_test.rb +0 -56
- data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +0 -827
- data/test/cases/associations/has_many_associations_test.rb +0 -1273
- data/test/cases/associations/has_many_through_associations_test.rb +0 -360
- data/test/cases/associations/has_one_associations_test.rb +0 -330
- data/test/cases/associations/has_one_through_associations_test.rb +0 -209
- data/test/cases/associations/inner_join_association_test.rb +0 -93
- data/test/cases/associations/inverse_associations_test.rb +0 -566
- data/test/cases/associations/join_model_test.rb +0 -712
- data/test/cases/associations_test.rb +0 -282
- data/test/cases/attribute_methods_test.rb +0 -305
- data/test/cases/autosave_association_test.rb +0 -1218
- data/test/cases/base_test.rb +0 -2166
- data/test/cases/batches_test.rb +0 -81
- data/test/cases/binary_test.rb +0 -30
- data/test/cases/calculations_test.rb +0 -360
- data/test/cases/callbacks_observers_test.rb +0 -38
- data/test/cases/callbacks_test.rb +0 -438
- data/test/cases/class_inheritable_attributes_test.rb +0 -32
- data/test/cases/column_alias_test.rb +0 -17
- data/test/cases/column_definition_test.rb +0 -70
- data/test/cases/connection_pool_test.rb +0 -25
- data/test/cases/connection_test_firebird.rb +0 -8
- data/test/cases/connection_test_mysql.rb +0 -65
- data/test/cases/copy_table_test_sqlite.rb +0 -80
- data/test/cases/counter_cache_test.rb +0 -84
- data/test/cases/database_statements_test.rb +0 -12
- data/test/cases/datatype_test_postgresql.rb +0 -204
- data/test/cases/date_time_test.rb +0 -37
- data/test/cases/default_test_firebird.rb +0 -16
- data/test/cases/defaults_test.rb +0 -111
- data/test/cases/deprecated_finder_test.rb +0 -30
- data/test/cases/dirty_test.rb +0 -316
- data/test/cases/finder_respond_to_test.rb +0 -76
- data/test/cases/finder_test.rb +0 -1098
- data/test/cases/fixtures_test.rb +0 -661
- data/test/cases/helper.rb +0 -68
- data/test/cases/i18n_test.rb +0 -46
- data/test/cases/inheritance_test.rb +0 -262
- data/test/cases/invalid_date_test.rb +0 -24
- data/test/cases/json_serialization_test.rb +0 -219
- data/test/cases/lifecycle_test.rb +0 -193
- data/test/cases/locking_test.rb +0 -350
- data/test/cases/method_scoping_test.rb +0 -704
- data/test/cases/migration_test.rb +0 -1649
- data/test/cases/migration_test_firebird.rb +0 -124
- data/test/cases/mixin_test.rb +0 -96
- data/test/cases/modules_test.rb +0 -109
- data/test/cases/multiple_db_test.rb +0 -85
- data/test/cases/named_scope_test.rb +0 -372
- data/test/cases/nested_attributes_test.rb +0 -840
- data/test/cases/pk_test.rb +0 -119
- data/test/cases/pooled_connections_test.rb +0 -103
- data/test/cases/query_cache_test.rb +0 -129
- data/test/cases/readonly_test.rb +0 -107
- data/test/cases/reflection_test.rb +0 -234
- data/test/cases/reload_models_test.rb +0 -22
- data/test/cases/repair_helper.rb +0 -50
- data/test/cases/reserved_word_test_mysql.rb +0 -176
- data/test/cases/sanitize_test.rb +0 -25
- data/test/cases/schema_authorization_test_postgresql.rb +0 -75
- data/test/cases/schema_dumper_test.rb +0 -211
- data/test/cases/schema_test_postgresql.rb +0 -178
- data/test/cases/serialization_test.rb +0 -47
- data/test/cases/sp_test_mysql.rb +0 -16
- data/test/cases/synonym_test_oracle.rb +0 -17
- data/test/cases/timestamp_test.rb +0 -75
- data/test/cases/transactions_test.rb +0 -543
- data/test/cases/unconnected_test.rb +0 -32
- data/test/cases/validations_i18n_test.rb +0 -925
- data/test/cases/validations_test.rb +0 -1684
- data/test/cases/xml_serialization_test.rb +0 -240
- data/test/cases/yaml_serialization_test.rb +0 -11
- data/test/config.rb +0 -5
- data/test/connections/jdbc_jdbcderby/connection.rb +0 -18
- data/test/connections/jdbc_jdbch2/connection.rb +0 -18
- data/test/connections/jdbc_jdbchsqldb/connection.rb +0 -18
- data/test/connections/jdbc_jdbcmysql/connection.rb +0 -26
- data/test/connections/jdbc_jdbcpostgresql/connection.rb +0 -26
- data/test/connections/jdbc_jdbcsqlite3/connection.rb +0 -25
- data/test/connections/native_db2/connection.rb +0 -25
- data/test/connections/native_firebird/connection.rb +0 -26
- data/test/connections/native_frontbase/connection.rb +0 -27
- data/test/connections/native_mysql/connection.rb +0 -25
- data/test/connections/native_openbase/connection.rb +0 -21
- data/test/connections/native_oracle/connection.rb +0 -27
- data/test/connections/native_postgresql/connection.rb +0 -21
- data/test/connections/native_sqlite/connection.rb +0 -25
- data/test/connections/native_sqlite3/connection.rb +0 -25
- data/test/connections/native_sqlite3/in_memory_connection.rb +0 -18
- data/test/connections/native_sybase/connection.rb +0 -23
- data/test/fixtures/accounts.yml +0 -29
- data/test/fixtures/all/developers.yml +0 -0
- data/test/fixtures/all/people.csv +0 -0
- data/test/fixtures/all/tasks.yml +0 -0
- data/test/fixtures/author_addresses.yml +0 -5
- data/test/fixtures/author_favorites.yml +0 -4
- data/test/fixtures/authors.yml +0 -9
- data/test/fixtures/binaries.yml +0 -132
- data/test/fixtures/books.yml +0 -7
- data/test/fixtures/categories/special_categories.yml +0 -9
- data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +0 -4
- data/test/fixtures/categories.yml +0 -14
- data/test/fixtures/categories_ordered.yml +0 -7
- data/test/fixtures/categories_posts.yml +0 -23
- data/test/fixtures/categorizations.yml +0 -17
- data/test/fixtures/clubs.yml +0 -6
- data/test/fixtures/comments.yml +0 -59
- data/test/fixtures/companies.yml +0 -56
- data/test/fixtures/computers.yml +0 -4
- data/test/fixtures/courses.yml +0 -7
- data/test/fixtures/customers.yml +0 -26
- data/test/fixtures/developers.yml +0 -21
- data/test/fixtures/developers_projects.yml +0 -17
- data/test/fixtures/edges.yml +0 -6
- data/test/fixtures/entrants.yml +0 -14
- data/test/fixtures/faces.yml +0 -11
- data/test/fixtures/fk_test_has_fk.yml +0 -3
- data/test/fixtures/fk_test_has_pk.yml +0 -2
- data/test/fixtures/funny_jokes.yml +0 -10
- data/test/fixtures/interests.yml +0 -33
- data/test/fixtures/items.yml +0 -4
- data/test/fixtures/jobs.yml +0 -7
- data/test/fixtures/legacy_things.yml +0 -3
- data/test/fixtures/mateys.yml +0 -4
- data/test/fixtures/member_types.yml +0 -6
- data/test/fixtures/members.yml +0 -6
- data/test/fixtures/memberships.yml +0 -20
- data/test/fixtures/men.yml +0 -5
- data/test/fixtures/minimalistics.yml +0 -2
- data/test/fixtures/mixed_case_monkeys.yml +0 -6
- data/test/fixtures/mixins.yml +0 -29
- data/test/fixtures/movies.yml +0 -7
- data/test/fixtures/naked/csv/accounts.csv +0 -1
- data/test/fixtures/naked/yml/accounts.yml +0 -1
- data/test/fixtures/naked/yml/companies.yml +0 -1
- data/test/fixtures/naked/yml/courses.yml +0 -1
- data/test/fixtures/organizations.yml +0 -5
- data/test/fixtures/owners.yml +0 -7
- data/test/fixtures/parrots.yml +0 -27
- data/test/fixtures/parrots_pirates.yml +0 -7
- data/test/fixtures/people.yml +0 -15
- data/test/fixtures/pets.yml +0 -14
- data/test/fixtures/pirates.yml +0 -9
- data/test/fixtures/polymorphic_designs.yml +0 -19
- data/test/fixtures/polymorphic_prices.yml +0 -19
- data/test/fixtures/posts.yml +0 -52
- data/test/fixtures/price_estimates.yml +0 -7
- data/test/fixtures/projects.yml +0 -7
- data/test/fixtures/readers.yml +0 -9
- data/test/fixtures/references.yml +0 -17
- data/test/fixtures/reserved_words/distinct.yml +0 -5
- data/test/fixtures/reserved_words/distincts_selects.yml +0 -11
- data/test/fixtures/reserved_words/group.yml +0 -14
- data/test/fixtures/reserved_words/select.yml +0 -8
- data/test/fixtures/reserved_words/values.yml +0 -7
- data/test/fixtures/ships.yml +0 -5
- data/test/fixtures/sponsors.yml +0 -9
- data/test/fixtures/subscribers.yml +0 -7
- data/test/fixtures/subscriptions.yml +0 -12
- data/test/fixtures/taggings.yml +0 -28
- data/test/fixtures/tags.yml +0 -7
- data/test/fixtures/tasks.yml +0 -7
- data/test/fixtures/tees.yml +0 -4
- data/test/fixtures/ties.yml +0 -4
- data/test/fixtures/topics.yml +0 -42
- data/test/fixtures/toys.yml +0 -4
- data/test/fixtures/treasures.yml +0 -10
- data/test/fixtures/vertices.yml +0 -4
- data/test/fixtures/warehouse-things.yml +0 -3
- data/test/fixtures/zines.yml +0 -5
- data/test/migrations/broken/100_migration_that_raises_exception.rb +0 -10
- data/test/migrations/decimal/1_give_me_big_numbers.rb +0 -15
- data/test/migrations/duplicate/1_people_have_last_names.rb +0 -9
- data/test/migrations/duplicate/2_we_need_reminders.rb +0 -12
- data/test/migrations/duplicate/3_foo.rb +0 -7
- data/test/migrations/duplicate/3_innocent_jointable.rb +0 -12
- data/test/migrations/duplicate_names/20080507052938_chunky.rb +0 -7
- data/test/migrations/duplicate_names/20080507053028_chunky.rb +0 -7
- data/test/migrations/interleaved/pass_1/3_innocent_jointable.rb +0 -12
- data/test/migrations/interleaved/pass_2/1_people_have_last_names.rb +0 -9
- data/test/migrations/interleaved/pass_2/3_innocent_jointable.rb +0 -12
- data/test/migrations/interleaved/pass_3/1_people_have_last_names.rb +0 -9
- data/test/migrations/interleaved/pass_3/2_i_raise_on_down.rb +0 -8
- data/test/migrations/interleaved/pass_3/3_innocent_jointable.rb +0 -12
- data/test/migrations/missing/1000_people_have_middle_names.rb +0 -9
- data/test/migrations/missing/1_people_have_last_names.rb +0 -9
- data/test/migrations/missing/3_we_need_reminders.rb +0 -12
- data/test/migrations/missing/4_innocent_jointable.rb +0 -12
- data/test/migrations/valid/1_people_have_last_names.rb +0 -9
- data/test/migrations/valid/2_we_need_reminders.rb +0 -12
- data/test/migrations/valid/3_innocent_jointable.rb +0 -12
- data/test/models/author.rb +0 -151
- data/test/models/auto_id.rb +0 -4
- data/test/models/binary.rb +0 -2
- data/test/models/bird.rb +0 -9
- data/test/models/book.rb +0 -4
- data/test/models/categorization.rb +0 -5
- data/test/models/category.rb +0 -34
- data/test/models/citation.rb +0 -6
- data/test/models/club.rb +0 -13
- data/test/models/column_name.rb +0 -3
- data/test/models/comment.rb +0 -29
- data/test/models/company.rb +0 -173
- data/test/models/company_in_module.rb +0 -78
- data/test/models/computer.rb +0 -3
- data/test/models/contact.rb +0 -16
- data/test/models/contract.rb +0 -5
- data/test/models/course.rb +0 -3
- data/test/models/customer.rb +0 -73
- data/test/models/default.rb +0 -2
- data/test/models/developer.rb +0 -101
- data/test/models/edge.rb +0 -5
- data/test/models/entrant.rb +0 -3
- data/test/models/essay.rb +0 -3
- data/test/models/event.rb +0 -3
- data/test/models/event_author.rb +0 -8
- data/test/models/face.rb +0 -7
- data/test/models/guid.rb +0 -2
- data/test/models/interest.rb +0 -5
- data/test/models/invoice.rb +0 -4
- data/test/models/item.rb +0 -7
- data/test/models/job.rb +0 -5
- data/test/models/joke.rb +0 -3
- data/test/models/keyboard.rb +0 -3
- data/test/models/legacy_thing.rb +0 -3
- data/test/models/line_item.rb +0 -3
- data/test/models/man.rb +0 -9
- data/test/models/matey.rb +0 -4
- data/test/models/member.rb +0 -12
- data/test/models/member_detail.rb +0 -5
- data/test/models/member_type.rb +0 -3
- data/test/models/membership.rb +0 -9
- data/test/models/minimalistic.rb +0 -2
- data/test/models/mixed_case_monkey.rb +0 -3
- data/test/models/movie.rb +0 -5
- data/test/models/order.rb +0 -4
- data/test/models/organization.rb +0 -6
- data/test/models/owner.rb +0 -5
- data/test/models/parrot.rb +0 -22
- data/test/models/person.rb +0 -16
- data/test/models/pet.rb +0 -5
- data/test/models/pirate.rb +0 -80
- data/test/models/polymorphic_design.rb +0 -3
- data/test/models/polymorphic_price.rb +0 -3
- data/test/models/post.rb +0 -102
- data/test/models/price_estimate.rb +0 -3
- data/test/models/project.rb +0 -30
- data/test/models/reader.rb +0 -4
- data/test/models/reference.rb +0 -4
- data/test/models/reply.rb +0 -46
- data/test/models/ship.rb +0 -19
- data/test/models/ship_part.rb +0 -7
- data/test/models/sponsor.rb +0 -4
- data/test/models/subject.rb +0 -4
- data/test/models/subscriber.rb +0 -8
- data/test/models/subscription.rb +0 -4
- data/test/models/tag.rb +0 -7
- data/test/models/tagging.rb +0 -10
- data/test/models/task.rb +0 -3
- data/test/models/tee.rb +0 -4
- data/test/models/tie.rb +0 -4
- data/test/models/topic.rb +0 -80
- data/test/models/toy.rb +0 -6
- data/test/models/treasure.rb +0 -8
- data/test/models/vertex.rb +0 -9
- data/test/models/warehouse_thing.rb +0 -5
- data/test/models/zine.rb +0 -3
- data/test/schema/mysql_specific_schema.rb +0 -31
- data/test/schema/postgresql_specific_schema.rb +0 -114
- data/test/schema/schema.rb +0 -550
- data/test/schema/schema2.rb +0 -6
- data/test/schema/sqlite_specific_schema.rb +0 -25
@@ -0,0 +1,376 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
# = Active Record Persistence
|
5
|
+
module Persistence
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# Creates an object (or multiple objects) and saves it to the database, if validations pass.
|
10
|
+
# The resulting object is returned whether the object was saved successfully to the database or not.
|
11
|
+
#
|
12
|
+
# The +attributes+ parameter can be either be a Hash or an Array of Hashes. These Hashes describe the
|
13
|
+
# attributes on the objects that are to be created.
|
14
|
+
#
|
15
|
+
# +create+ respects mass-assignment security and accepts either +:as+ or +:without_protection+ options
|
16
|
+
# in the +options+ parameter.
|
17
|
+
#
|
18
|
+
# ==== Examples
|
19
|
+
# # Create a single new object
|
20
|
+
# User.create(:first_name => 'Jamie')
|
21
|
+
#
|
22
|
+
# # Create a single new object using the :admin mass-assignment security role
|
23
|
+
# User.create({ :first_name => 'Jamie', :is_admin => true }, :as => :admin)
|
24
|
+
#
|
25
|
+
# # Create a single new object bypassing mass-assignment security
|
26
|
+
# User.create({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true)
|
27
|
+
#
|
28
|
+
# # Create an Array of new objects
|
29
|
+
# User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }])
|
30
|
+
#
|
31
|
+
# # Create a single object and pass it into a block to set other attributes.
|
32
|
+
# User.create(:first_name => 'Jamie') do |u|
|
33
|
+
# u.is_admin = false
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# # Creating an Array of new objects using a block, where the block is executed for each object:
|
37
|
+
# User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }]) do |u|
|
38
|
+
# u.is_admin = false
|
39
|
+
# end
|
40
|
+
def create(attributes = nil, options = {}, &block)
|
41
|
+
if attributes.is_a?(Array)
|
42
|
+
attributes.collect { |attr| create(attr, options, &block) }
|
43
|
+
else
|
44
|
+
object = new(attributes, options, &block)
|
45
|
+
object.save
|
46
|
+
object
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns true if this object hasn't been saved yet -- that is, a record
|
52
|
+
# for the object doesn't exist in the data store yet; otherwise, returns false.
|
53
|
+
def new_record?
|
54
|
+
@new_record
|
55
|
+
end
|
56
|
+
|
57
|
+
# Returns true if this object has been destroyed, otherwise returns false.
|
58
|
+
def destroyed?
|
59
|
+
@destroyed
|
60
|
+
end
|
61
|
+
|
62
|
+
# Returns if the record is persisted, i.e. it's not a new record and it was
|
63
|
+
# not destroyed.
|
64
|
+
def persisted?
|
65
|
+
!(new_record? || destroyed?)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Saves the model.
|
69
|
+
#
|
70
|
+
# If the model is new a record gets created in the database, otherwise
|
71
|
+
# the existing record gets updated.
|
72
|
+
#
|
73
|
+
# By default, save always run validations. If any of them fail the action
|
74
|
+
# is cancelled and +save+ returns +false+. However, if you supply
|
75
|
+
# :validate => false, validations are bypassed altogether. See
|
76
|
+
# ActiveRecord::Validations for more information.
|
77
|
+
#
|
78
|
+
# There's a series of callbacks associated with +save+. If any of the
|
79
|
+
# <tt>before_*</tt> callbacks return +false+ the action is cancelled and
|
80
|
+
# +save+ returns +false+. See ActiveRecord::Callbacks for further
|
81
|
+
# details.
|
82
|
+
def save(*)
|
83
|
+
begin
|
84
|
+
create_or_update
|
85
|
+
rescue ActiveRecord::RecordInvalid
|
86
|
+
false
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# Saves the model.
|
91
|
+
#
|
92
|
+
# If the model is new a record gets created in the database, otherwise
|
93
|
+
# the existing record gets updated.
|
94
|
+
#
|
95
|
+
# With <tt>save!</tt> validations always run. If any of them fail
|
96
|
+
# ActiveRecord::RecordInvalid gets raised. See ActiveRecord::Validations
|
97
|
+
# for more information.
|
98
|
+
#
|
99
|
+
# There's a series of callbacks associated with <tt>save!</tt>. If any of
|
100
|
+
# the <tt>before_*</tt> callbacks return +false+ the action is cancelled
|
101
|
+
# and <tt>save!</tt> raises ActiveRecord::RecordNotSaved. See
|
102
|
+
# ActiveRecord::Callbacks for further details.
|
103
|
+
def save!(*)
|
104
|
+
create_or_update || raise(RecordNotSaved)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Deletes the record in the database and freezes this instance to
|
108
|
+
# reflect that no changes should be made (since they can't be
|
109
|
+
# persisted). Returns the frozen instance.
|
110
|
+
#
|
111
|
+
# The row is simply removed with an SQL +DELETE+ statement on the
|
112
|
+
# record's primary key, and no callbacks are executed.
|
113
|
+
#
|
114
|
+
# To enforce the object's +before_destroy+ and +after_destroy+
|
115
|
+
# callbacks, Observer methods, or any <tt>:dependent</tt> association
|
116
|
+
# options, use <tt>#destroy</tt>.
|
117
|
+
def delete
|
118
|
+
if persisted?
|
119
|
+
self.class.delete(id)
|
120
|
+
IdentityMap.remove(self) if IdentityMap.enabled?
|
121
|
+
end
|
122
|
+
@destroyed = true
|
123
|
+
freeze
|
124
|
+
end
|
125
|
+
|
126
|
+
# Deletes the record in the database and freezes this instance to reflect
|
127
|
+
# that no changes should be made (since they can't be persisted).
|
128
|
+
def destroy
|
129
|
+
destroy_associations
|
130
|
+
|
131
|
+
if persisted?
|
132
|
+
IdentityMap.remove(self) if IdentityMap.enabled?
|
133
|
+
pk = self.class.primary_key
|
134
|
+
column = self.class.columns_hash[pk]
|
135
|
+
substitute = connection.substitute_at(column, 0)
|
136
|
+
|
137
|
+
relation = self.class.unscoped.where(
|
138
|
+
self.class.arel_table[pk].eq(substitute))
|
139
|
+
|
140
|
+
relation.bind_values = [[column, id]]
|
141
|
+
relation.delete_all
|
142
|
+
end
|
143
|
+
|
144
|
+
@destroyed = true
|
145
|
+
freeze
|
146
|
+
end
|
147
|
+
|
148
|
+
# Returns an instance of the specified +klass+ with the attributes of the
|
149
|
+
# current record. This is mostly useful in relation to single-table
|
150
|
+
# inheritance structures where you want a subclass to appear as the
|
151
|
+
# superclass. This can be used along with record identification in
|
152
|
+
# Action Pack to allow, say, <tt>Client < Company</tt> to do something
|
153
|
+
# like render <tt>:partial => @client.becomes(Company)</tt> to render that
|
154
|
+
# instance using the companies/company partial instead of clients/client.
|
155
|
+
#
|
156
|
+
# Note: The new instance will share a link to the same attributes as the original class.
|
157
|
+
# So any change to the attributes in either instance will affect the other.
|
158
|
+
def becomes(klass)
|
159
|
+
became = klass.new
|
160
|
+
became.instance_variable_set("@attributes", @attributes)
|
161
|
+
became.instance_variable_set("@attributes_cache", @attributes_cache)
|
162
|
+
became.instance_variable_set("@new_record", new_record?)
|
163
|
+
became.instance_variable_set("@destroyed", destroyed?)
|
164
|
+
became.instance_variable_set("@errors", errors)
|
165
|
+
became.send("#{klass.inheritance_column}=", klass.name) unless self.class.descends_from_active_record?
|
166
|
+
became
|
167
|
+
end
|
168
|
+
|
169
|
+
# Updates a single attribute and saves the record.
|
170
|
+
# This is especially useful for boolean flags on existing records. Also note that
|
171
|
+
#
|
172
|
+
# * Validation is skipped.
|
173
|
+
# * Callbacks are invoked.
|
174
|
+
# * updated_at/updated_on column is updated if that column is available.
|
175
|
+
# * Updates all the attributes that are dirty in this object.
|
176
|
+
#
|
177
|
+
def update_attribute(name, value)
|
178
|
+
name = name.to_s
|
179
|
+
raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
|
180
|
+
send("#{name}=", value)
|
181
|
+
save(:validate => false)
|
182
|
+
end
|
183
|
+
|
184
|
+
# Updates a single attribute of an object, without calling save.
|
185
|
+
#
|
186
|
+
# * Validation is skipped.
|
187
|
+
# * Callbacks are skipped.
|
188
|
+
# * updated_at/updated_on column is not updated if that column is available.
|
189
|
+
#
|
190
|
+
# Raises an +ActiveRecordError+ when called on new objects, or when the +name+
|
191
|
+
# attribute is marked as readonly.
|
192
|
+
def update_column(name, value)
|
193
|
+
name = name.to_s
|
194
|
+
raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
|
195
|
+
raise ActiveRecordError, "can not update on a new record object" unless persisted?
|
196
|
+
|
197
|
+
updated_count = self.class.unscoped.update_all({ name => value }, self.class.primary_key => id)
|
198
|
+
|
199
|
+
raw_write_attribute(name, value)
|
200
|
+
|
201
|
+
updated_count == 1
|
202
|
+
end
|
203
|
+
|
204
|
+
# Updates the attributes of the model from the passed-in hash and saves the
|
205
|
+
# record, all wrapped in a transaction. If the object is invalid, the saving
|
206
|
+
# will fail and false will be returned.
|
207
|
+
#
|
208
|
+
# When updating model attributes, mass-assignment security protection is respected.
|
209
|
+
# If no +:as+ option is supplied then the +:default+ role will be used.
|
210
|
+
# If you want to bypass the protection given by +attr_protected+ and
|
211
|
+
# +attr_accessible+ then you can do so using the +:without_protection+ option.
|
212
|
+
def update_attributes(attributes, options = {})
|
213
|
+
# The following transaction covers any possible database side-effects of the
|
214
|
+
# attributes assignment. For example, setting the IDs of a child collection.
|
215
|
+
with_transaction_returning_status do
|
216
|
+
self.assign_attributes(attributes, options)
|
217
|
+
save
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
# Updates its receiver just like +update_attributes+ but calls <tt>save!</tt> instead
|
222
|
+
# of +save+, so an exception is raised if the record is invalid.
|
223
|
+
def update_attributes!(attributes, options = {})
|
224
|
+
# The following transaction covers any possible database side-effects of the
|
225
|
+
# attributes assignment. For example, setting the IDs of a child collection.
|
226
|
+
with_transaction_returning_status do
|
227
|
+
self.assign_attributes(attributes, options)
|
228
|
+
save!
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
# Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1).
|
233
|
+
# The increment is performed directly on the underlying attribute, no setter is invoked.
|
234
|
+
# Only makes sense for number-based attributes. Returns +self+.
|
235
|
+
def increment(attribute, by = 1)
|
236
|
+
self[attribute] ||= 0
|
237
|
+
self[attribute] += by
|
238
|
+
self
|
239
|
+
end
|
240
|
+
|
241
|
+
# Wrapper around +increment+ that saves the record. This method differs from
|
242
|
+
# its non-bang version in that it passes through the attribute setter.
|
243
|
+
# Saving is not subjected to validation checks. Returns +true+ if the
|
244
|
+
# record could be saved.
|
245
|
+
def increment!(attribute, by = 1)
|
246
|
+
increment(attribute, by).update_attribute(attribute, self[attribute])
|
247
|
+
end
|
248
|
+
|
249
|
+
# Initializes +attribute+ to zero if +nil+ and subtracts the value passed as +by+ (default is 1).
|
250
|
+
# The decrement is performed directly on the underlying attribute, no setter is invoked.
|
251
|
+
# Only makes sense for number-based attributes. Returns +self+.
|
252
|
+
def decrement(attribute, by = 1)
|
253
|
+
self[attribute] ||= 0
|
254
|
+
self[attribute] -= by
|
255
|
+
self
|
256
|
+
end
|
257
|
+
|
258
|
+
# Wrapper around +decrement+ that saves the record. This method differs from
|
259
|
+
# its non-bang version in that it passes through the attribute setter.
|
260
|
+
# Saving is not subjected to validation checks. Returns +true+ if the
|
261
|
+
# record could be saved.
|
262
|
+
def decrement!(attribute, by = 1)
|
263
|
+
decrement(attribute, by).update_attribute(attribute, self[attribute])
|
264
|
+
end
|
265
|
+
|
266
|
+
# Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
|
267
|
+
# if the predicate returns +true+ the attribute will become +false+. This
|
268
|
+
# method toggles directly the underlying value without calling any setter.
|
269
|
+
# Returns +self+.
|
270
|
+
def toggle(attribute)
|
271
|
+
self[attribute] = !send("#{attribute}?")
|
272
|
+
self
|
273
|
+
end
|
274
|
+
|
275
|
+
# Wrapper around +toggle+ that saves the record. This method differs from
|
276
|
+
# its non-bang version in that it passes through the attribute setter.
|
277
|
+
# Saving is not subjected to validation checks. Returns +true+ if the
|
278
|
+
# record could be saved.
|
279
|
+
def toggle!(attribute)
|
280
|
+
toggle(attribute).update_attribute(attribute, self[attribute])
|
281
|
+
end
|
282
|
+
|
283
|
+
# Reloads the attributes of this object from the database.
|
284
|
+
# The optional options argument is passed to find when reloading so you
|
285
|
+
# may do e.g. record.reload(:lock => true) to reload the same record with
|
286
|
+
# an exclusive row lock.
|
287
|
+
def reload(options = nil)
|
288
|
+
clear_aggregation_cache
|
289
|
+
clear_association_cache
|
290
|
+
|
291
|
+
IdentityMap.without do
|
292
|
+
fresh_object = self.class.unscoped { self.class.find(self.id, options) }
|
293
|
+
@attributes.update(fresh_object.instance_variable_get('@attributes'))
|
294
|
+
end
|
295
|
+
|
296
|
+
@attributes_cache = {}
|
297
|
+
self
|
298
|
+
end
|
299
|
+
|
300
|
+
# Saves the record with the updated_at/on attributes set to the current time.
|
301
|
+
# Please note that no validation is performed and no callbacks are executed.
|
302
|
+
# If an attribute name is passed, that attribute is updated along with
|
303
|
+
# updated_at/on attributes.
|
304
|
+
#
|
305
|
+
# product.touch # updates updated_at/on
|
306
|
+
# product.touch(:designed_at) # updates the designed_at attribute and updated_at/on
|
307
|
+
#
|
308
|
+
# If used along with +belongs_to+ then +touch+ will invoke +touch+ method on associated object.
|
309
|
+
#
|
310
|
+
# class Brake < ActiveRecord::Base
|
311
|
+
# belongs_to :car, :touch => true
|
312
|
+
# end
|
313
|
+
#
|
314
|
+
# class Car < ActiveRecord::Base
|
315
|
+
# belongs_to :corporation, :touch => true
|
316
|
+
# end
|
317
|
+
#
|
318
|
+
# # triggers @brake.car.touch and @brake.car.corporation.touch
|
319
|
+
# @brake.touch
|
320
|
+
def touch(name = nil)
|
321
|
+
attributes = timestamp_attributes_for_update_in_model
|
322
|
+
attributes << name if name
|
323
|
+
|
324
|
+
unless attributes.empty?
|
325
|
+
current_time = current_time_from_proper_timezone
|
326
|
+
changes = {}
|
327
|
+
|
328
|
+
attributes.each do |column|
|
329
|
+
changes[column.to_s] = write_attribute(column.to_s, current_time)
|
330
|
+
end
|
331
|
+
|
332
|
+
changes[self.class.locking_column] = increment_lock if locking_enabled?
|
333
|
+
|
334
|
+
@changed_attributes.except!(*changes.keys)
|
335
|
+
primary_key = self.class.primary_key
|
336
|
+
self.class.unscoped.update_all(changes, { primary_key => self[primary_key] }) == 1
|
337
|
+
end
|
338
|
+
end
|
339
|
+
|
340
|
+
private
|
341
|
+
|
342
|
+
# A hook to be overridden by association modules.
|
343
|
+
def destroy_associations
|
344
|
+
end
|
345
|
+
|
346
|
+
def create_or_update
|
347
|
+
raise ReadOnlyRecord if readonly?
|
348
|
+
result = new_record? ? create : update
|
349
|
+
result != false
|
350
|
+
end
|
351
|
+
|
352
|
+
# Updates the associated record with values matching those of the instance attributes.
|
353
|
+
# Returns the number of affected rows.
|
354
|
+
def update(attribute_names = @attributes.keys)
|
355
|
+
attributes_with_values = arel_attributes_values(false, false, attribute_names)
|
356
|
+
return 0 if attributes_with_values.empty?
|
357
|
+
klass = self.class
|
358
|
+
stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id)).arel.compile_update(attributes_with_values)
|
359
|
+
klass.connection.update stmt
|
360
|
+
end
|
361
|
+
|
362
|
+
# Creates a record with values matching those of the instance attributes
|
363
|
+
# and returns its id.
|
364
|
+
def create
|
365
|
+
attributes_values = arel_attributes_values(!id.nil?)
|
366
|
+
|
367
|
+
new_id = self.class.unscoped.insert attributes_values
|
368
|
+
|
369
|
+
self.id ||= new_id if self.class.primary_key
|
370
|
+
|
371
|
+
IdentityMap.add(self) if IdentityMap.enabled?
|
372
|
+
@new_record = false
|
373
|
+
id
|
374
|
+
end
|
375
|
+
end
|
376
|
+
end
|
@@ -1,21 +1,24 @@
|
|
1
|
+
require 'active_support/core_ext/object/blank'
|
2
|
+
|
1
3
|
module ActiveRecord
|
4
|
+
# = Active Record Query Cache
|
2
5
|
class QueryCache
|
3
6
|
module ClassMethods
|
4
7
|
# Enable the query cache within the block if Active Record is configured.
|
5
8
|
def cache(&block)
|
6
|
-
if ActiveRecord::Base.
|
7
|
-
yield
|
8
|
-
else
|
9
|
+
if ActiveRecord::Base.connected?
|
9
10
|
connection.cache(&block)
|
11
|
+
else
|
12
|
+
yield
|
10
13
|
end
|
11
14
|
end
|
12
15
|
|
13
16
|
# Disable the query cache within the block if Active Record is configured.
|
14
17
|
def uncached(&block)
|
15
|
-
if ActiveRecord::Base.
|
16
|
-
yield
|
17
|
-
else
|
18
|
+
if ActiveRecord::Base.connected?
|
18
19
|
connection.uncached(&block)
|
20
|
+
else
|
21
|
+
yield
|
19
22
|
end
|
20
23
|
end
|
21
24
|
end
|
@@ -24,10 +27,48 @@ module ActiveRecord
|
|
24
27
|
@app = app
|
25
28
|
end
|
26
29
|
|
30
|
+
class BodyProxy # :nodoc:
|
31
|
+
def initialize(original_cache_value, target, connection_id)
|
32
|
+
@original_cache_value = original_cache_value
|
33
|
+
@target = target
|
34
|
+
@connection_id = connection_id
|
35
|
+
end
|
36
|
+
|
37
|
+
def method_missing(method_sym, *arguments, &block)
|
38
|
+
@target.send(method_sym, *arguments, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
def respond_to?(method_sym, include_private = false)
|
42
|
+
super || @target.respond_to?(method_sym)
|
43
|
+
end
|
44
|
+
|
45
|
+
def each(&block)
|
46
|
+
@target.each(&block)
|
47
|
+
end
|
48
|
+
|
49
|
+
def close
|
50
|
+
@target.close if @target.respond_to?(:close)
|
51
|
+
ensure
|
52
|
+
ActiveRecord::Base.connection_id = @connection_id
|
53
|
+
ActiveRecord::Base.connection.clear_query_cache
|
54
|
+
unless @original_cache_value
|
55
|
+
ActiveRecord::Base.connection.disable_query_cache!
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
27
60
|
def call(env)
|
28
|
-
ActiveRecord::Base.
|
29
|
-
|
61
|
+
old = ActiveRecord::Base.connection.query_cache_enabled
|
62
|
+
ActiveRecord::Base.connection.enable_query_cache!
|
63
|
+
|
64
|
+
status, headers, body = @app.call(env)
|
65
|
+
[status, headers, BodyProxy.new(old, body, ActiveRecord::Base.connection_id)]
|
66
|
+
rescue Exception => e
|
67
|
+
ActiveRecord::Base.connection.clear_query_cache
|
68
|
+
unless old
|
69
|
+
ActiveRecord::Base.connection.disable_query_cache!
|
30
70
|
end
|
71
|
+
raise e
|
31
72
|
end
|
32
73
|
end
|
33
74
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'active_support/core_ext/module/delegation'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Querying
|
5
|
+
delegate :find, :first, :first!, :last, :last!, :all, :exists?, :any?, :many?, :to => :scoped
|
6
|
+
delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :scoped
|
7
|
+
delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :scoped
|
8
|
+
delegate :find_each, :find_in_batches, :to => :scoped
|
9
|
+
delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins,
|
10
|
+
:where, :preload, :eager_load, :includes, :from, :lock, :readonly,
|
11
|
+
:having, :create_with, :uniq, :to => :scoped
|
12
|
+
delegate :count, :average, :minimum, :maximum, :sum, :calculate, :pluck, :to => :scoped
|
13
|
+
|
14
|
+
# Executes a custom SQL query against your database and returns all the results. The results will
|
15
|
+
# be returned as an array with columns requested encapsulated as attributes of the model you call
|
16
|
+
# this method from. If you call <tt>Product.find_by_sql</tt> then the results will be returned in
|
17
|
+
# a Product object with the attributes you specified in the SQL query.
|
18
|
+
#
|
19
|
+
# If you call a complicated SQL query which spans multiple tables the columns specified by the
|
20
|
+
# SELECT will be attributes of the model, whether or not they are columns of the corresponding
|
21
|
+
# table.
|
22
|
+
#
|
23
|
+
# The +sql+ parameter is a full SQL query as a string. It will be called as is, there will be
|
24
|
+
# no database agnostic conversions performed. This should be a last resort because using, for example,
|
25
|
+
# MySQL specific terms will lock you to using that particular database engine or require you to
|
26
|
+
# change your call if you switch engines.
|
27
|
+
#
|
28
|
+
# ==== Examples
|
29
|
+
# # A simple SQL query spanning multiple tables
|
30
|
+
# Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id"
|
31
|
+
# > [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...]
|
32
|
+
#
|
33
|
+
# # You can use the same string replacement techniques as you can with ActiveRecord#find
|
34
|
+
# Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]
|
35
|
+
# > [#<Post:0x36bff9c @attributes={"title"=>"The Cheap Man Buys Twice"}>, ...]
|
36
|
+
def find_by_sql(sql, binds = [])
|
37
|
+
logging_query_plan do
|
38
|
+
connection.select_all(sanitize_sql(sql), "#{name} Load", binds).collect! { |record| instantiate(record) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns the result of an SQL statement that should only include a COUNT(*) in the SELECT part.
|
43
|
+
# The use of this method should be restricted to complicated SQL queries that can't be executed
|
44
|
+
# using the ActiveRecord::Calculations class methods. Look into those before using this.
|
45
|
+
#
|
46
|
+
# ==== Parameters
|
47
|
+
#
|
48
|
+
# * +sql+ - An SQL statement which should return a count query from the database, see the example below.
|
49
|
+
#
|
50
|
+
# ==== Examples
|
51
|
+
#
|
52
|
+
# Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id"
|
53
|
+
def count_by_sql(sql)
|
54
|
+
sql = sanitize_conditions(sql)
|
55
|
+
connection.select_value(sql, "#{name} Count").to_i
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "rails"
|
3
|
+
require "active_model/railtie"
|
4
|
+
|
5
|
+
# For now, action_controller must always be present with
|
6
|
+
# rails, so let's make sure that it gets required before
|
7
|
+
# here. This is needed for correctly setting up the middleware.
|
8
|
+
# In the future, this might become an optional require.
|
9
|
+
require "action_controller/railtie"
|
10
|
+
|
11
|
+
module ActiveRecord
|
12
|
+
# = Active Record Railtie
|
13
|
+
class Railtie < Rails::Railtie
|
14
|
+
config.active_record = ActiveSupport::OrderedOptions.new
|
15
|
+
|
16
|
+
config.app_generators.orm :active_record, :migration => true,
|
17
|
+
:timestamps => true
|
18
|
+
|
19
|
+
config.app_middleware.insert_after "::ActionDispatch::Callbacks",
|
20
|
+
"ActiveRecord::QueryCache"
|
21
|
+
|
22
|
+
config.app_middleware.insert_after "::ActionDispatch::Callbacks",
|
23
|
+
"ActiveRecord::ConnectionAdapters::ConnectionManagement"
|
24
|
+
|
25
|
+
config.action_dispatch.rescue_responses.merge!(
|
26
|
+
'ActiveRecord::RecordNotFound' => :not_found,
|
27
|
+
'ActiveRecord::StaleObjectError' => :conflict,
|
28
|
+
'ActiveRecord::RecordInvalid' => :unprocessable_entity,
|
29
|
+
'ActiveRecord::RecordNotSaved' => :unprocessable_entity
|
30
|
+
)
|
31
|
+
|
32
|
+
rake_tasks do
|
33
|
+
require "active_record/base"
|
34
|
+
load "active_record/railties/databases.rake"
|
35
|
+
end
|
36
|
+
|
37
|
+
# When loading console, force ActiveRecord::Base to be loaded
|
38
|
+
# to avoid cross references when loading a constant for the
|
39
|
+
# first time. Also, make it output to STDERR.
|
40
|
+
console do |app|
|
41
|
+
require "active_record/railties/console_sandbox" if app.sandbox?
|
42
|
+
require "active_record/base"
|
43
|
+
ActiveRecord::Base.logger = Logger.new(STDERR)
|
44
|
+
end
|
45
|
+
|
46
|
+
runner do |app|
|
47
|
+
require "active_record/base"
|
48
|
+
end
|
49
|
+
|
50
|
+
initializer "active_record.initialize_timezone" do
|
51
|
+
ActiveSupport.on_load(:active_record) do
|
52
|
+
self.time_zone_aware_attributes = true
|
53
|
+
self.default_timezone = :utc
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
initializer "active_record.logger" do
|
58
|
+
ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
|
59
|
+
end
|
60
|
+
|
61
|
+
initializer "active_record.identity_map" do |app|
|
62
|
+
config.app_middleware.insert_after "::ActionDispatch::Callbacks",
|
63
|
+
"ActiveRecord::IdentityMap::Middleware" if config.active_record.delete(:identity_map)
|
64
|
+
end
|
65
|
+
|
66
|
+
initializer "active_record.set_configs" do |app|
|
67
|
+
ActiveSupport.on_load(:active_record) do
|
68
|
+
if app.config.active_record.delete(:whitelist_attributes)
|
69
|
+
attr_accessible(nil)
|
70
|
+
end
|
71
|
+
app.config.active_record.each do |k,v|
|
72
|
+
send "#{k}=", v
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# This sets the database configuration from Configuration#database_configuration
|
78
|
+
# and then establishes the connection.
|
79
|
+
initializer "active_record.initialize_database" do |app|
|
80
|
+
ActiveSupport.on_load(:active_record) do
|
81
|
+
db_connection_type = "DATABASE_URL"
|
82
|
+
unless ENV['DATABASE_URL']
|
83
|
+
db_connection_type = "database.yml"
|
84
|
+
self.configurations = app.config.database_configuration
|
85
|
+
end
|
86
|
+
Rails.logger.info "Connecting to database specified by #{db_connection_type}"
|
87
|
+
|
88
|
+
establish_connection
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Expose database runtime to controller for logging.
|
93
|
+
initializer "active_record.log_runtime" do |app|
|
94
|
+
require "active_record/railties/controller_runtime"
|
95
|
+
ActiveSupport.on_load(:action_controller) do
|
96
|
+
include ActiveRecord::Railties::ControllerRuntime
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
initializer "active_record.set_reloader_hooks" do |app|
|
101
|
+
hook = lambda do
|
102
|
+
ActiveRecord::Base.clear_reloadable_connections!
|
103
|
+
ActiveRecord::Base.clear_cache!
|
104
|
+
end
|
105
|
+
|
106
|
+
if app.config.reload_classes_only_on_change
|
107
|
+
ActiveSupport.on_load(:active_record) do
|
108
|
+
ActionDispatch::Reloader.to_prepare(&hook)
|
109
|
+
end
|
110
|
+
else
|
111
|
+
ActiveSupport.on_load(:active_record) do
|
112
|
+
ActionDispatch::Reloader.to_cleanup(&hook)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
initializer "active_record.add_watchable_files" do |app|
|
118
|
+
config.watchable_files.concat ["#{app.root}/db/schema.rb", "#{app.root}/db/structure.sql"]
|
119
|
+
end
|
120
|
+
|
121
|
+
config.after_initialize do
|
122
|
+
ActiveSupport.on_load(:active_record) do
|
123
|
+
instantiate_observers
|
124
|
+
|
125
|
+
ActionDispatch::Reloader.to_prepare do
|
126
|
+
ActiveRecord::Base.instantiate_observers
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|