activerecord 3.2.6 → 6.0.0

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.

Files changed (371) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +611 -6417
  3. data/MIT-LICENSE +4 -2
  4. data/README.rdoc +44 -47
  5. data/examples/performance.rb +79 -71
  6. data/examples/simple.rb +6 -5
  7. data/lib/active_record/aggregations.rb +268 -238
  8. data/lib/active_record/association_relation.rb +40 -0
  9. data/lib/active_record/associations/alias_tracker.rb +47 -42
  10. data/lib/active_record/associations/association.rb +173 -81
  11. data/lib/active_record/associations/association_scope.rb +124 -92
  12. data/lib/active_record/associations/belongs_to_association.rb +83 -38
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +11 -9
  14. data/lib/active_record/associations/builder/association.rb +113 -32
  15. data/lib/active_record/associations/builder/belongs_to.rb +105 -60
  16. data/lib/active_record/associations/builder/collection_association.rb +53 -56
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +98 -41
  18. data/lib/active_record/associations/builder/has_many.rb +11 -63
  19. data/lib/active_record/associations/builder/has_one.rb +47 -45
  20. data/lib/active_record/associations/builder/singular_association.rb +30 -18
  21. data/lib/active_record/associations/collection_association.rb +217 -295
  22. data/lib/active_record/associations/collection_proxy.rb +1074 -77
  23. data/lib/active_record/associations/foreign_association.rb +20 -0
  24. data/lib/active_record/associations/has_many_association.rb +78 -50
  25. data/lib/active_record/associations/has_many_through_association.rb +99 -61
  26. data/lib/active_record/associations/has_one_association.rb +75 -30
  27. data/lib/active_record/associations/has_one_through_association.rb +20 -11
  28. data/lib/active_record/associations/join_dependency/join_association.rb +45 -119
  29. data/lib/active_record/associations/join_dependency/join_base.rb +11 -12
  30. data/lib/active_record/associations/join_dependency/join_part.rb +35 -42
  31. data/lib/active_record/associations/join_dependency.rb +208 -164
  32. data/lib/active_record/associations/preloader/association.rb +93 -87
  33. data/lib/active_record/associations/preloader/through_association.rb +87 -38
  34. data/lib/active_record/associations/preloader.rb +134 -110
  35. data/lib/active_record/associations/singular_association.rb +19 -24
  36. data/lib/active_record/associations/through_association.rb +61 -27
  37. data/lib/active_record/associations.rb +1766 -1505
  38. data/lib/active_record/attribute_assignment.rb +57 -193
  39. data/lib/active_record/attribute_decorators.rb +90 -0
  40. data/lib/active_record/attribute_methods/before_type_cast.rb +58 -8
  41. data/lib/active_record/attribute_methods/dirty.rb +187 -67
  42. data/lib/active_record/attribute_methods/primary_key.rb +100 -78
  43. data/lib/active_record/attribute_methods/query.rb +10 -8
  44. data/lib/active_record/attribute_methods/read.rb +29 -118
  45. data/lib/active_record/attribute_methods/serialization.rb +60 -72
  46. data/lib/active_record/attribute_methods/time_zone_conversion.rb +69 -42
  47. data/lib/active_record/attribute_methods/write.rb +36 -44
  48. data/lib/active_record/attribute_methods.rb +306 -161
  49. data/lib/active_record/attributes.rb +279 -0
  50. data/lib/active_record/autosave_association.rb +324 -238
  51. data/lib/active_record/base.rb +114 -507
  52. data/lib/active_record/callbacks.rb +147 -83
  53. data/lib/active_record/coders/json.rb +15 -0
  54. data/lib/active_record/coders/yaml_column.rb +32 -23
  55. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +962 -279
  56. data/lib/active_record/connection_adapters/abstract/database_limits.rb +32 -5
  57. data/lib/active_record/connection_adapters/abstract/database_statements.rb +331 -209
  58. data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -23
  59. data/lib/active_record/connection_adapters/abstract/quoting.rb +201 -65
  60. data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
  61. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +510 -289
  63. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +93 -0
  64. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1182 -313
  65. data/lib/active_record/connection_adapters/abstract/transaction.rb +323 -0
  66. data/lib/active_record/connection_adapters/abstract_adapter.rb +585 -120
  67. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +610 -463
  68. data/lib/active_record/connection_adapters/column.rb +58 -233
  69. data/lib/active_record/connection_adapters/connection_specification.rb +297 -0
  70. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +29 -0
  71. data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
  72. data/lib/active_record/connection_adapters/mysql/database_statements.rb +200 -0
  73. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
  74. data/lib/active_record/connection_adapters/mysql/quoting.rb +81 -0
  75. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +72 -0
  76. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +95 -0
  77. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +88 -0
  78. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
  79. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +31 -0
  80. data/lib/active_record/connection_adapters/mysql2_adapter.rb +75 -207
  81. data/lib/active_record/connection_adapters/postgresql/column.rb +30 -0
  82. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +182 -0
  83. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
  84. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +53 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +113 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +26 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
  107. data/lib/active_record/connection_adapters/postgresql/quoting.rb +205 -0
  108. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
  109. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +222 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +776 -0
  113. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +36 -0
  114. data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
  115. data/lib/active_record/connection_adapters/postgresql_adapter.rb +695 -1052
  116. data/lib/active_record/connection_adapters/schema_cache.rb +115 -24
  117. data/lib/active_record/connection_adapters/sql_type_metadata.rb +37 -0
  118. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +118 -0
  119. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
  120. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +103 -0
  121. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
  122. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
  124. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +137 -0
  125. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +528 -26
  126. data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
  127. data/lib/active_record/connection_handling.rb +267 -0
  128. data/lib/active_record/core.rb +599 -0
  129. data/lib/active_record/counter_cache.rb +177 -103
  130. data/lib/active_record/database_configurations/database_config.rb +37 -0
  131. data/lib/active_record/database_configurations/hash_config.rb +50 -0
  132. data/lib/active_record/database_configurations/url_config.rb +79 -0
  133. data/lib/active_record/database_configurations.rb +233 -0
  134. data/lib/active_record/define_callbacks.rb +22 -0
  135. data/lib/active_record/dynamic_matchers.rb +107 -64
  136. data/lib/active_record/enum.rb +274 -0
  137. data/lib/active_record/errors.rb +254 -61
  138. data/lib/active_record/explain.rb +35 -70
  139. data/lib/active_record/explain_registry.rb +32 -0
  140. data/lib/active_record/explain_subscriber.rb +18 -8
  141. data/lib/active_record/fixture_set/file.rb +82 -0
  142. data/lib/active_record/fixture_set/model_metadata.rb +33 -0
  143. data/lib/active_record/fixture_set/render_context.rb +17 -0
  144. data/lib/active_record/fixture_set/table_row.rb +153 -0
  145. data/lib/active_record/fixture_set/table_rows.rb +47 -0
  146. data/lib/active_record/fixtures.rb +291 -475
  147. data/lib/active_record/gem_version.rb +17 -0
  148. data/lib/active_record/inheritance.rb +219 -100
  149. data/lib/active_record/insert_all.rb +179 -0
  150. data/lib/active_record/integration.rb +175 -17
  151. data/lib/active_record/internal_metadata.rb +53 -0
  152. data/lib/active_record/legacy_yaml_adapter.rb +48 -0
  153. data/lib/active_record/locale/en.yml +9 -1
  154. data/lib/active_record/locking/optimistic.rb +106 -92
  155. data/lib/active_record/locking/pessimistic.rb +23 -11
  156. data/lib/active_record/log_subscriber.rb +80 -30
  157. data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
  158. data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
  159. data/lib/active_record/middleware/database_selector.rb +75 -0
  160. data/lib/active_record/migration/command_recorder.rb +235 -56
  161. data/lib/active_record/migration/compatibility.rb +244 -0
  162. data/lib/active_record/migration/join_table.rb +17 -0
  163. data/lib/active_record/migration.rb +917 -301
  164. data/lib/active_record/model_schema.rb +351 -175
  165. data/lib/active_record/nested_attributes.rb +366 -235
  166. data/lib/active_record/no_touching.rb +65 -0
  167. data/lib/active_record/null_relation.rb +68 -0
  168. data/lib/active_record/persistence.rb +761 -166
  169. data/lib/active_record/query_cache.rb +22 -44
  170. data/lib/active_record/querying.rb +55 -31
  171. data/lib/active_record/railtie.rb +185 -47
  172. data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
  173. data/lib/active_record/railties/console_sandbox.rb +5 -4
  174. data/lib/active_record/railties/controller_runtime.rb +35 -33
  175. data/lib/active_record/railties/databases.rake +366 -463
  176. data/lib/active_record/readonly_attributes.rb +4 -6
  177. data/lib/active_record/reflection.rb +736 -228
  178. data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
  179. data/lib/active_record/relation/batches.rb +252 -52
  180. data/lib/active_record/relation/calculations.rb +340 -270
  181. data/lib/active_record/relation/delegation.rb +117 -36
  182. data/lib/active_record/relation/finder_methods.rb +439 -286
  183. data/lib/active_record/relation/from_clause.rb +26 -0
  184. data/lib/active_record/relation/merger.rb +184 -0
  185. data/lib/active_record/relation/predicate_builder/array_handler.rb +49 -0
  186. data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
  187. data/lib/active_record/relation/predicate_builder/base_handler.rb +18 -0
  188. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +19 -0
  189. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
  190. data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
  191. data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
  192. data/lib/active_record/relation/predicate_builder.rb +131 -39
  193. data/lib/active_record/relation/query_attribute.rb +50 -0
  194. data/lib/active_record/relation/query_methods.rb +1163 -221
  195. data/lib/active_record/relation/record_fetch_warning.rb +51 -0
  196. data/lib/active_record/relation/spawn_methods.rb +49 -120
  197. data/lib/active_record/relation/where_clause.rb +190 -0
  198. data/lib/active_record/relation/where_clause_factory.rb +33 -0
  199. data/lib/active_record/relation.rb +671 -349
  200. data/lib/active_record/result.rb +149 -15
  201. data/lib/active_record/runtime_registry.rb +24 -0
  202. data/lib/active_record/sanitization.rb +153 -133
  203. data/lib/active_record/schema.rb +22 -19
  204. data/lib/active_record/schema_dumper.rb +178 -112
  205. data/lib/active_record/schema_migration.rb +60 -0
  206. data/lib/active_record/scoping/default.rb +107 -98
  207. data/lib/active_record/scoping/named.rb +130 -115
  208. data/lib/active_record/scoping.rb +77 -123
  209. data/lib/active_record/secure_token.rb +40 -0
  210. data/lib/active_record/serialization.rb +10 -6
  211. data/lib/active_record/statement_cache.rb +148 -0
  212. data/lib/active_record/store.rb +256 -16
  213. data/lib/active_record/suppressor.rb +61 -0
  214. data/lib/active_record/table_metadata.rb +75 -0
  215. data/lib/active_record/tasks/database_tasks.rb +506 -0
  216. data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
  217. data/lib/active_record/tasks/postgresql_database_tasks.rb +141 -0
  218. data/lib/active_record/tasks/sqlite_database_tasks.rb +77 -0
  219. data/lib/active_record/test_databases.rb +23 -0
  220. data/lib/active_record/test_fixtures.rb +224 -0
  221. data/lib/active_record/timestamp.rb +93 -39
  222. data/lib/active_record/touch_later.rb +66 -0
  223. data/lib/active_record/transactions.rb +260 -129
  224. data/lib/active_record/translation.rb +3 -1
  225. data/lib/active_record/type/adapter_specific_registry.rb +129 -0
  226. data/lib/active_record/type/date.rb +9 -0
  227. data/lib/active_record/type/date_time.rb +9 -0
  228. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  229. data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
  230. data/lib/active_record/type/internal/timezone.rb +17 -0
  231. data/lib/active_record/type/json.rb +30 -0
  232. data/lib/active_record/type/serialized.rb +71 -0
  233. data/lib/active_record/type/text.rb +11 -0
  234. data/lib/active_record/type/time.rb +21 -0
  235. data/lib/active_record/type/type_map.rb +62 -0
  236. data/lib/active_record/type/unsigned_integer.rb +17 -0
  237. data/lib/active_record/type.rb +78 -0
  238. data/lib/active_record/type_caster/connection.rb +34 -0
  239. data/lib/active_record/type_caster/map.rb +20 -0
  240. data/lib/active_record/type_caster.rb +9 -0
  241. data/lib/active_record/validations/absence.rb +25 -0
  242. data/lib/active_record/validations/associated.rb +35 -18
  243. data/lib/active_record/validations/length.rb +26 -0
  244. data/lib/active_record/validations/presence.rb +68 -0
  245. data/lib/active_record/validations/uniqueness.rb +123 -77
  246. data/lib/active_record/validations.rb +54 -43
  247. data/lib/active_record/version.rb +7 -7
  248. data/lib/active_record.rb +97 -49
  249. data/lib/arel/alias_predication.rb +9 -0
  250. data/lib/arel/attributes/attribute.rb +37 -0
  251. data/lib/arel/attributes.rb +22 -0
  252. data/lib/arel/collectors/bind.rb +24 -0
  253. data/lib/arel/collectors/composite.rb +31 -0
  254. data/lib/arel/collectors/plain_string.rb +20 -0
  255. data/lib/arel/collectors/sql_string.rb +20 -0
  256. data/lib/arel/collectors/substitute_binds.rb +28 -0
  257. data/lib/arel/crud.rb +42 -0
  258. data/lib/arel/delete_manager.rb +18 -0
  259. data/lib/arel/errors.rb +9 -0
  260. data/lib/arel/expressions.rb +29 -0
  261. data/lib/arel/factory_methods.rb +49 -0
  262. data/lib/arel/insert_manager.rb +49 -0
  263. data/lib/arel/math.rb +45 -0
  264. data/lib/arel/nodes/and.rb +32 -0
  265. data/lib/arel/nodes/ascending.rb +23 -0
  266. data/lib/arel/nodes/binary.rb +52 -0
  267. data/lib/arel/nodes/bind_param.rb +36 -0
  268. data/lib/arel/nodes/case.rb +55 -0
  269. data/lib/arel/nodes/casted.rb +50 -0
  270. data/lib/arel/nodes/comment.rb +29 -0
  271. data/lib/arel/nodes/count.rb +12 -0
  272. data/lib/arel/nodes/delete_statement.rb +45 -0
  273. data/lib/arel/nodes/descending.rb +23 -0
  274. data/lib/arel/nodes/equality.rb +18 -0
  275. data/lib/arel/nodes/extract.rb +24 -0
  276. data/lib/arel/nodes/false.rb +16 -0
  277. data/lib/arel/nodes/full_outer_join.rb +8 -0
  278. data/lib/arel/nodes/function.rb +44 -0
  279. data/lib/arel/nodes/grouping.rb +8 -0
  280. data/lib/arel/nodes/in.rb +8 -0
  281. data/lib/arel/nodes/infix_operation.rb +80 -0
  282. data/lib/arel/nodes/inner_join.rb +8 -0
  283. data/lib/arel/nodes/insert_statement.rb +37 -0
  284. data/lib/arel/nodes/join_source.rb +20 -0
  285. data/lib/arel/nodes/matches.rb +18 -0
  286. data/lib/arel/nodes/named_function.rb +23 -0
  287. data/lib/arel/nodes/node.rb +50 -0
  288. data/lib/arel/nodes/node_expression.rb +13 -0
  289. data/lib/arel/nodes/outer_join.rb +8 -0
  290. data/lib/arel/nodes/over.rb +15 -0
  291. data/lib/arel/nodes/regexp.rb +16 -0
  292. data/lib/arel/nodes/right_outer_join.rb +8 -0
  293. data/lib/arel/nodes/select_core.rb +67 -0
  294. data/lib/arel/nodes/select_statement.rb +41 -0
  295. data/lib/arel/nodes/sql_literal.rb +16 -0
  296. data/lib/arel/nodes/string_join.rb +11 -0
  297. data/lib/arel/nodes/table_alias.rb +27 -0
  298. data/lib/arel/nodes/terminal.rb +16 -0
  299. data/lib/arel/nodes/true.rb +16 -0
  300. data/lib/arel/nodes/unary.rb +45 -0
  301. data/lib/arel/nodes/unary_operation.rb +20 -0
  302. data/lib/arel/nodes/unqualified_column.rb +22 -0
  303. data/lib/arel/nodes/update_statement.rb +41 -0
  304. data/lib/arel/nodes/values_list.rb +9 -0
  305. data/lib/arel/nodes/window.rb +126 -0
  306. data/lib/arel/nodes/with.rb +11 -0
  307. data/lib/arel/nodes.rb +68 -0
  308. data/lib/arel/order_predications.rb +13 -0
  309. data/lib/arel/predications.rb +257 -0
  310. data/lib/arel/select_manager.rb +271 -0
  311. data/lib/arel/table.rb +110 -0
  312. data/lib/arel/tree_manager.rb +72 -0
  313. data/lib/arel/update_manager.rb +34 -0
  314. data/lib/arel/visitors/depth_first.rb +204 -0
  315. data/lib/arel/visitors/dot.rb +297 -0
  316. data/lib/arel/visitors/ibm_db.rb +34 -0
  317. data/lib/arel/visitors/informix.rb +62 -0
  318. data/lib/arel/visitors/mssql.rb +157 -0
  319. data/lib/arel/visitors/mysql.rb +83 -0
  320. data/lib/arel/visitors/oracle.rb +159 -0
  321. data/lib/arel/visitors/oracle12.rb +66 -0
  322. data/lib/arel/visitors/postgresql.rb +110 -0
  323. data/lib/arel/visitors/sqlite.rb +39 -0
  324. data/lib/arel/visitors/to_sql.rb +889 -0
  325. data/lib/arel/visitors/visitor.rb +46 -0
  326. data/lib/arel/visitors/where_sql.rb +23 -0
  327. data/lib/arel/visitors.rb +20 -0
  328. data/lib/arel/window_predications.rb +9 -0
  329. data/lib/arel.rb +51 -0
  330. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  331. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
  332. data/lib/rails/generators/active_record/migration/migration_generator.rb +59 -9
  333. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
  334. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +48 -0
  335. data/lib/rails/generators/active_record/migration.rb +41 -8
  336. data/lib/rails/generators/active_record/model/model_generator.rb +24 -22
  337. data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
  338. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +1 -1
  339. data/lib/rails/generators/active_record.rb +10 -16
  340. metadata +285 -149
  341. data/examples/associations.png +0 -0
  342. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
  343. data/lib/active_record/associations/join_helper.rb +0 -55
  344. data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
  345. data/lib/active_record/associations/preloader/collection_association.rb +0 -24
  346. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
  347. data/lib/active_record/associations/preloader/has_many.rb +0 -17
  348. data/lib/active_record/associations/preloader/has_many_through.rb +0 -15
  349. data/lib/active_record/associations/preloader/has_one.rb +0 -23
  350. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  351. data/lib/active_record/associations/preloader/singular_association.rb +0 -21
  352. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
  353. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -188
  354. data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -426
  355. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -579
  356. data/lib/active_record/dynamic_finder_match.rb +0 -68
  357. data/lib/active_record/dynamic_scope_match.rb +0 -23
  358. data/lib/active_record/fixtures/file.rb +0 -65
  359. data/lib/active_record/identity_map.rb +0 -162
  360. data/lib/active_record/observer.rb +0 -121
  361. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  362. data/lib/active_record/serializers/xml_serializer.rb +0 -203
  363. data/lib/active_record/session_store.rb +0 -358
  364. data/lib/active_record/test_case.rb +0 -73
  365. data/lib/rails/generators/active_record/migration/templates/migration.rb +0 -34
  366. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
  367. data/lib/rails/generators/active_record/model/templates/model.rb +0 -12
  368. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  369. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
  370. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
  371. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -1,34 +1,28 @@
1
- begin
2
- require 'psych'
3
- rescue LoadError
4
- end
5
-
6
- require 'yaml'
7
- require 'set'
8
- require 'thread'
9
- require 'active_support/benchmarkable'
10
- require 'active_support/dependencies'
11
- require 'active_support/descendants_tracker'
12
- require 'active_support/time'
13
- require 'active_support/core_ext/class/attribute'
14
- require 'active_support/core_ext/class/attribute_accessors'
15
- require 'active_support/core_ext/class/delegating_attributes'
16
- require 'active_support/core_ext/class/attribute'
17
- require 'active_support/core_ext/array/extract_options'
18
- require 'active_support/core_ext/hash/deep_merge'
19
- require 'active_support/core_ext/hash/indifferent_access'
20
- require 'active_support/core_ext/hash/slice'
21
- require 'active_support/core_ext/string/behavior'
22
- require 'active_support/core_ext/kernel/singleton_class'
23
- require 'active_support/core_ext/module/delegation'
24
- require 'active_support/core_ext/module/introspection'
25
- require 'active_support/core_ext/object/duplicable'
26
- require 'active_support/core_ext/object/blank'
27
- require 'active_support/deprecation'
28
- require 'arel'
29
- require 'active_record/errors'
30
- require 'active_record/log_subscriber'
31
- require 'active_record/explain_subscriber'
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "active_support/benchmarkable"
5
+ require "active_support/dependencies"
6
+ require "active_support/descendants_tracker"
7
+ require "active_support/time"
8
+ require "active_support/core_ext/module/attribute_accessors"
9
+ require "active_support/core_ext/array/extract_options"
10
+ require "active_support/core_ext/hash/deep_merge"
11
+ require "active_support/core_ext/hash/slice"
12
+ require "active_support/core_ext/string/behavior"
13
+ require "active_support/core_ext/kernel/singleton_class"
14
+ require "active_support/core_ext/module/introspection"
15
+ require "active_support/core_ext/object/duplicable"
16
+ require "active_support/core_ext/class/subclasses"
17
+ require "active_record/attribute_decorators"
18
+ require "active_record/define_callbacks"
19
+ require "active_record/errors"
20
+ require "active_record/log_subscriber"
21
+ require "active_record/explain_subscriber"
22
+ require "active_record/relation/delegation"
23
+ require "active_record/attributes"
24
+ require "active_record/type_caster"
25
+ require "active_record/database_configurations"
32
26
 
33
27
  module ActiveRecord #:nodoc:
34
28
  # = Active Record
@@ -47,7 +41,7 @@ module ActiveRecord #:nodoc:
47
41
  # method is especially useful when you're receiving the data from somewhere else, like an
48
42
  # HTTP request. It works like this:
49
43
  #
50
- # user = User.new(:name => "David", :occupation => "Code Artist")
44
+ # user = User.new(name: "David", occupation: "Code Artist")
51
45
  # user.name # => "David"
52
46
  #
53
47
  # You can also use block initialization:
@@ -80,7 +74,7 @@ module ActiveRecord #:nodoc:
80
74
  # end
81
75
  #
82
76
  # def self.authenticate_safely_simply(user_name, password)
83
- # where(:user_name => user_name, :password => password).first
77
+ # where(user_name: user_name, password: password).first
84
78
  # end
85
79
  # end
86
80
  #
@@ -98,27 +92,27 @@ module ActiveRecord #:nodoc:
98
92
  #
99
93
  # Company.where(
100
94
  # "id = :id AND name = :name AND division = :division AND created_at > :accounting_date",
101
- # { :id => 3, :name => "37signals", :division => "First", :accounting_date => '2005-01-01' }
95
+ # { id: 3, name: "37signals", division: "First", accounting_date: '2005-01-01' }
102
96
  # ).first
103
97
  #
104
98
  # Similarly, a simple hash without a statement will generate conditions based on equality with the SQL AND
105
99
  # operator. For instance:
106
100
  #
107
- # Student.where(:first_name => "Harvey", :status => 1)
101
+ # Student.where(first_name: "Harvey", status: 1)
108
102
  # Student.where(params[:student])
109
103
  #
110
104
  # A range may be used in the hash to use the SQL BETWEEN operator:
111
105
  #
112
- # Student.where(:grade => 9..12)
106
+ # Student.where(grade: 9..12)
113
107
  #
114
108
  # An array may be used in the hash to use the SQL IN operator:
115
109
  #
116
- # Student.where(:grade => [9,11,12])
110
+ # Student.where(grade: [9,11,12])
117
111
  #
118
112
  # When joining tables, nested hashes or keys written in the form 'table_name.column_name'
119
113
  # can be used to qualify the table name of a particular condition. For instance:
120
114
  #
121
- # Student.joins(:schools).where(:schools => { :category => 'public' })
115
+ # Student.joins(:schools).where(schools: { category: 'public' })
122
116
  # Student.joins(:schools).where('schools.category' => 'public' )
123
117
  #
124
118
  # == Overwriting default accessors
@@ -126,36 +120,33 @@ module ActiveRecord #:nodoc:
126
120
  # All column values are automatically available through basic accessors on the Active Record
127
121
  # object, but sometimes you want to specialize this behavior. This can be done by overwriting
128
122
  # the default accessors (using the same name as the attribute) and calling
129
- # <tt>read_attribute(attr_name)</tt> and <tt>write_attribute(attr_name, value)</tt> to actually
130
- # change things.
123
+ # +super+ to actually change things.
131
124
  #
132
125
  # class Song < ActiveRecord::Base
133
126
  # # Uses an integer of seconds to hold the length of the song
134
127
  #
135
128
  # def length=(minutes)
136
- # write_attribute(:length, minutes.to_i * 60)
129
+ # super(minutes.to_i * 60)
137
130
  # end
138
131
  #
139
132
  # def length
140
- # read_attribute(:length) / 60
133
+ # super / 60
141
134
  # end
142
135
  # end
143
136
  #
144
- # You can alternatively use <tt>self[:attribute]=(value)</tt> and <tt>self[:attribute]</tt>
145
- # instead of <tt>write_attribute(:attribute, value)</tt> and <tt>read_attribute(:attribute)</tt>.
146
- #
147
137
  # == Attribute query methods
148
138
  #
149
139
  # In addition to the basic accessors, query methods are also automatically available on the Active Record object.
150
140
  # Query methods allow you to test whether an attribute value is present.
141
+ # Additionally, when dealing with numeric values, a query method will return false if the value is zero.
151
142
  #
152
143
  # For example, an Active Record User with the <tt>name</tt> attribute has a <tt>name?</tt> method that you can call
153
144
  # to determine whether the user has a name:
154
145
  #
155
- # user = User.new(:name => "David")
146
+ # user = User.new(name: "David")
156
147
  # user.name? # => true
157
148
  #
158
- # anonymous = User.new(:name => "")
149
+ # anonymous = User.new(name: "")
159
150
  # anonymous.name? # => false
160
151
  #
161
152
  # == Accessing attributes before they have been typecasted
@@ -171,71 +162,31 @@ module ActiveRecord #:nodoc:
171
162
  #
172
163
  # == Dynamic attribute-based finders
173
164
  #
174
- # Dynamic attribute-based finders are a cleaner way of getting (and/or creating) objects
165
+ # Dynamic attribute-based finders are a mildly deprecated way of getting (and/or creating) objects
175
166
  # by simple queries without turning to SQL. They work by appending the name of an attribute
176
- # to <tt>find_by_</tt>, <tt>find_last_by_</tt>, or <tt>find_all_by_</tt> and thus produces finders
177
- # like <tt>Person.find_by_user_name</tt>, <tt>Person.find_all_by_last_name</tt>, and
178
- # <tt>Payment.find_by_transaction_id</tt>. Instead of writing
179
- # <tt>Person.where(:user_name => user_name).first</tt>, you just do <tt>Person.find_by_user_name(user_name)</tt>.
180
- # And instead of writing <tt>Person.where(:last_name => last_name).all</tt>, you just do
181
- # <tt>Person.find_all_by_last_name(last_name)</tt>.
167
+ # to <tt>find_by_</tt> like <tt>Person.find_by_user_name</tt>.
168
+ # Instead of writing <tt>Person.find_by(user_name: user_name)</tt>, you can use
169
+ # <tt>Person.find_by_user_name(user_name)</tt>.
182
170
  #
183
171
  # It's possible to add an exclamation point (!) on the end of the dynamic finders to get them to raise an
184
- # <tt>ActiveRecord::RecordNotFound</tt> error if they do not return any records,
172
+ # ActiveRecord::RecordNotFound error if they do not return any records,
185
173
  # like <tt>Person.find_by_last_name!</tt>.
186
174
  #
187
- # It's also possible to use multiple attributes in the same find by separating them with "_and_".
175
+ # It's also possible to use multiple attributes in the same <tt>find_by_</tt> by separating them with
176
+ # "_and_".
188
177
  #
189
- # Person.where(:user_name => user_name, :password => password).first
178
+ # Person.find_by(user_name: user_name, password: password)
190
179
  # Person.find_by_user_name_and_password(user_name, password) # with dynamic finder
191
180
  #
192
181
  # It's even possible to call these dynamic finder methods on relations and named scopes.
193
182
  #
194
- # Payment.order("created_on").find_all_by_amount(50)
195
- # Payment.pending.find_last_by_amount(100)
196
- #
197
- # The same dynamic finder style can be used to create the object if it doesn't already exist.
198
- # This dynamic finder is called with <tt>find_or_create_by_</tt> and will return the object if
199
- # it already exists and otherwise creates it, then returns it. Protected attributes won't be set
200
- # unless they are given in a block.
201
- #
202
- # # No 'Summer' tag exists
203
- # Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer")
204
- #
205
- # # Now the 'Summer' tag does exist
206
- # Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer")
207
- #
208
- # # Now 'Bob' exist and is an 'admin'
209
- # User.find_or_create_by_name('Bob', :age => 40) { |u| u.admin = true }
210
- #
211
- # Adding an exclamation point (!) on to the end of <tt>find_or_create_by_</tt> will
212
- # raise an <tt>ActiveRecord::RecordInvalid</tt> error if the new record is invalid.
213
- #
214
- # Use the <tt>find_or_initialize_by_</tt> finder if you want to return a new record without
215
- # saving it first. Protected attributes won't be set unless they are given in a block.
216
- #
217
- # # No 'Winter' tag exists
218
- # winter = Tag.find_or_initialize_by_name("Winter")
219
- # winter.persisted? # false
220
- #
221
- # To find by a subset of the attributes to be used for instantiating a new object, pass a hash instead of
222
- # a list of parameters.
223
- #
224
- # Tag.find_or_create_by_name(:name => "rails", :creator => current_user)
225
- #
226
- # That will either find an existing tag named "rails", or create a new one while setting the
227
- # user that created it.
228
- #
229
- # Just like <tt>find_by_*</tt>, you can also use <tt>scoped_by_*</tt> to retrieve data. The good thing about
230
- # using this feature is that the very first time result is returned using <tt>method_missing</tt> technique
231
- # but after that the method is declared on the class. Henceforth <tt>method_missing</tt> will not be hit.
232
- #
233
- # User.scoped_by_user_name('David')
183
+ # Payment.order("created_on").find_by_amount(50)
234
184
  #
235
185
  # == Saving arrays, hashes, and other non-mappable objects in text columns
236
186
  #
237
187
  # Active Record can serialize any object in text columns using YAML. To do so, you must
238
- # specify this with a call to the class method +serialize+.
188
+ # specify this with a call to the class method
189
+ # {serialize}[rdoc-ref:AttributeMethods::Serialization::ClassMethods#serialize].
239
190
  # This makes it possible to store arrays, hashes, and other non-mappable objects without doing
240
191
  # any additional work.
241
192
  #
@@ -243,7 +194,7 @@ module ActiveRecord #:nodoc:
243
194
  # serialize :preferences
244
195
  # end
245
196
  #
246
- # user = User.create(:preferences => { "background" => "black", "display" => large })
197
+ # user = User.create(preferences: { "background" => "black", "display" => large })
247
198
  # User.find(user.id).preferences # => { "background" => "black", "display" => large }
248
199
  #
249
200
  # You can also specify a class option as the second parameter that'll raise an exception
@@ -253,7 +204,7 @@ module ActiveRecord #:nodoc:
253
204
  # serialize :preferences, Hash
254
205
  # end
255
206
  #
256
- # user = User.create(:preferences => %w( one two three ))
207
+ # user = User.create(preferences: %w( one two three ))
257
208
  # User.find(user.id).preferences # raises SerializationTypeMismatch
258
209
  #
259
210
  # When you specify a class option, the default value for that attribute will be a new
@@ -269,453 +220,109 @@ module ActiveRecord #:nodoc:
269
220
  #
270
221
  # == Single table inheritance
271
222
  #
272
- # Active Record allows inheritance by storing the name of the class in a column that by
273
- # default is named "type" (can be changed by overwriting <tt>Base.inheritance_column</tt>).
274
- # This means that an inheritance looking like this:
275
- #
276
- # class Company < ActiveRecord::Base; end
277
- # class Firm < Company; end
278
- # class Client < Company; end
279
- # class PriorityClient < Client; end
280
- #
281
- # When you do <tt>Firm.create(:name => "37signals")</tt>, this record will be saved in
282
- # the companies table with type = "Firm". You can then fetch this row again using
283
- # <tt>Company.where(:name => '37signals').first</tt> and it will return a Firm object.
284
- #
285
- # If you don't have a type column defined in your table, single-table inheritance won't
286
- # be triggered. In that case, it'll work just like normal subclasses with no special magic
287
- # for differentiating between them or reloading the right type with find.
288
- #
289
- # Note, all the attributes for all the cases are kept in the same table. Read more:
290
- # http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html
223
+ # Active Record allows inheritance by storing the name of the class in a
224
+ # column that is named "type" by default. See ActiveRecord::Inheritance for
225
+ # more details.
291
226
  #
292
227
  # == Connection to multiple databases in different models
293
228
  #
294
- # Connections are usually created through ActiveRecord::Base.establish_connection and retrieved
229
+ # Connections are usually created through
230
+ # {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection] and retrieved
295
231
  # by ActiveRecord::Base.connection. All classes inheriting from ActiveRecord::Base will use this
296
232
  # connection. But you can also set a class-specific connection. For example, if Course is an
297
233
  # ActiveRecord::Base, but resides in a different database, you can just say <tt>Course.establish_connection</tt>
298
234
  # and Course and all of its subclasses will use this connection instead.
299
235
  #
300
236
  # This feature is implemented by keeping a connection pool in ActiveRecord::Base that is
301
- # a Hash indexed by the class. If a connection is requested, the retrieve_connection method
237
+ # a hash indexed by the class. If a connection is requested, the
238
+ # {ActiveRecord::Base.retrieve_connection}[rdoc-ref:ConnectionHandling#retrieve_connection] method
302
239
  # will go up the class-hierarchy until a connection is found in the connection pool.
303
240
  #
304
241
  # == Exceptions
305
242
  #
306
243
  # * ActiveRecordError - Generic error class and superclass of all other errors raised by Active Record.
307
- # * AdapterNotSpecified - The configuration hash used in <tt>establish_connection</tt> didn't include an
308
- # <tt>:adapter</tt> key.
309
- # * AdapterNotFound - The <tt>:adapter</tt> key used in <tt>establish_connection</tt> specified a
310
- # non-existent adapter
244
+ # * AdapterNotSpecified - The configuration hash used in
245
+ # {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection]
246
+ # didn't include an <tt>:adapter</tt> key.
247
+ # * AdapterNotFound - The <tt>:adapter</tt> key used in
248
+ # {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection]
249
+ # specified a non-existent adapter
311
250
  # (or a bad spelling of an existing one).
312
251
  # * AssociationTypeMismatch - The object assigned to the association wasn't of the type
313
252
  # specified in the association definition.
314
- # * SerializationTypeMismatch - The serialized object wasn't of the class specified as the second parameter.
315
- # * ConnectionNotEstablished+ - No connection has been established. Use <tt>establish_connection</tt>
316
- # before querying.
317
- # * RecordNotFound - No record responded to the +find+ method. Either the row with the given ID doesn't exist
318
- # or the row didn't meet the additional restrictions. Some +find+ calls do not raise this exception to signal
319
- # nothing was found, please check its documentation for further details.
320
- # * StatementInvalid - The database server rejected the SQL statement. The precise error is added in the message.
321
- # * MultiparameterAssignmentErrors - Collection of errors that occurred during a mass assignment using the
322
- # <tt>attributes=</tt> method. The +errors+ property of this exception contains an array of
323
- # AttributeAssignmentError
324
- # objects that should be inspected to determine which attributes triggered the errors.
325
253
  # * AttributeAssignmentError - An error occurred while doing a mass assignment through the
326
- # <tt>attributes=</tt> method.
254
+ # {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=] method.
327
255
  # You can inspect the +attribute+ property of the exception object to determine which attribute
328
256
  # triggered the error.
257
+ # * ConnectionNotEstablished - No connection has been established.
258
+ # Use {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection] before querying.
259
+ # * MultiparameterAssignmentErrors - Collection of errors that occurred during a mass assignment using the
260
+ # {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=] method.
261
+ # The +errors+ property of this exception contains an array of
262
+ # AttributeAssignmentError
263
+ # objects that should be inspected to determine which attributes triggered the errors.
264
+ # * RecordInvalid - raised by {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] and
265
+ # {ActiveRecord::Base.create!}[rdoc-ref:Persistence::ClassMethods#create!]
266
+ # when the record is invalid.
267
+ # * RecordNotFound - No record responded to the {ActiveRecord::Base.find}[rdoc-ref:FinderMethods#find] method.
268
+ # Either the row with the given ID doesn't exist or the row didn't meet the additional restrictions.
269
+ # Some {ActiveRecord::Base.find}[rdoc-ref:FinderMethods#find] calls do not raise this exception to signal
270
+ # nothing was found, please check its documentation for further details.
271
+ # * SerializationTypeMismatch - The serialized object wasn't of the class specified as the second parameter.
272
+ # * StatementInvalid - The database server rejected the SQL statement. The precise error is added in the message.
329
273
  #
330
274
  # *Note*: The attributes listed are class-level attributes (accessible from both the class and instance level).
331
275
  # So it's possible to assign a logger to the class through <tt>Base.logger=</tt> which will then be used by all
332
276
  # instances in the current object space.
333
277
  class Base
334
- ##
335
- # :singleton-method:
336
- # Accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class,
337
- # which is then passed on to any new database connections made and which can be retrieved on both
338
- # a class and instance level by calling +logger+.
339
- cattr_accessor :logger, :instance_writer => false
340
-
341
- ##
342
- # :singleton-method:
343
- # Contains the database configuration - as is typically stored in config/database.yml -
344
- # as a Hash.
345
- #
346
- # For example, the following database.yml...
347
- #
348
- # development:
349
- # adapter: sqlite3
350
- # database: db/development.sqlite3
351
- #
352
- # production:
353
- # adapter: sqlite3
354
- # database: db/production.sqlite3
355
- #
356
- # ...would result in ActiveRecord::Base.configurations to look like this:
357
- #
358
- # {
359
- # 'development' => {
360
- # 'adapter' => 'sqlite3',
361
- # 'database' => 'db/development.sqlite3'
362
- # },
363
- # 'production' => {
364
- # 'adapter' => 'sqlite3',
365
- # 'database' => 'db/production.sqlite3'
366
- # }
367
- # }
368
- cattr_accessor :configurations, :instance_writer => false
369
- @@configurations = {}
370
-
371
- ##
372
- # :singleton-method:
373
- # Determines whether to use Time.local (using :local) or Time.utc (using :utc) when pulling
374
- # dates and times from the database. This is set to :local by default.
375
- cattr_accessor :default_timezone, :instance_writer => false
376
- @@default_timezone = :local
377
-
378
- ##
379
- # :singleton-method:
380
- # Specifies the format to use when dumping the database schema with Rails'
381
- # Rakefile. If :sql, the schema is dumped as (potentially database-
382
- # specific) SQL statements. If :ruby, the schema is dumped as an
383
- # ActiveRecord::Schema file which can be loaded into any database that
384
- # supports migrations. Use :ruby if you want to have different database
385
- # adapters for, e.g., your development and test environments.
386
- cattr_accessor :schema_format , :instance_writer => false
387
- @@schema_format = :ruby
388
-
389
- ##
390
- # :singleton-method:
391
- # Specify whether or not to use timestamps for migration versions
392
- cattr_accessor :timestamped_migrations , :instance_writer => false
393
- @@timestamped_migrations = true
394
-
395
- class << self # Class methods
396
- def inherited(child_class) #:nodoc:
397
- child_class.initialize_generated_modules
398
- super
399
- end
400
-
401
- def initialize_generated_modules #:nodoc:
402
- @attribute_methods_mutex = Mutex.new
403
-
404
- # force attribute methods to be higher in inheritance hierarchy than other generated methods
405
- generated_attribute_methods
406
- generated_feature_methods
407
- end
408
-
409
- def generated_feature_methods
410
- @generated_feature_methods ||= begin
411
- mod = const_set(:GeneratedFeatureMethods, Module.new)
412
- include mod
413
- mod
414
- end
415
- end
416
-
417
- # Returns a string like 'Post(id:integer, title:string, body:text)'
418
- def inspect
419
- if self == Base
420
- super
421
- elsif abstract_class?
422
- "#{super}(abstract)"
423
- elsif table_exists?
424
- attr_list = columns.map { |c| "#{c.name}: #{c.type}" } * ', '
425
- "#{super}(#{attr_list})"
426
- else
427
- "#{super}(Table doesn't exist)"
428
- end
429
- end
430
-
431
- # Overwrite the default class equality method to provide support for association proxies.
432
- def ===(object)
433
- object.is_a?(self)
434
- end
435
-
436
- def arel_table
437
- @arel_table ||= Arel::Table.new(table_name, arel_engine)
438
- end
439
-
440
- def arel_engine
441
- @arel_engine ||= begin
442
- if self == ActiveRecord::Base
443
- ActiveRecord::Base
444
- else
445
- connection_handler.retrieve_connection_pool(self) ? self : superclass.arel_engine
446
- end
447
- end
448
- end
449
-
450
- private
451
-
452
- def relation #:nodoc:
453
- @relation ||= Relation.new(self, arel_table)
454
-
455
- if finder_needs_type_condition?
456
- @relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name)
457
- else
458
- @relation
459
- end
460
- end
461
- end
462
-
463
- public
464
- # New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
465
- # attributes but not yet saved (pass a hash with key names matching the associated table column names).
466
- # In both instances, valid attribute keys are determined by the column names of the associated table --
467
- # hence you can't have attributes that aren't part of the table columns.
468
- #
469
- # +initialize+ respects mass-assignment security and accepts either +:as+ or +:without_protection+ options
470
- # in the +options+ parameter.
471
- #
472
- # ==== Examples
473
- # # Instantiates a single new object
474
- # User.new(:first_name => 'Jamie')
475
- #
476
- # # Instantiates a single new object using the :admin mass-assignment security role
477
- # User.new({ :first_name => 'Jamie', :is_admin => true }, :as => :admin)
478
- #
479
- # # Instantiates a single new object bypassing mass-assignment security
480
- # User.new({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true)
481
- def initialize(attributes = nil, options = {})
482
- @attributes = self.class.initialize_attributes(self.class.column_defaults.dup)
483
- @association_cache = {}
484
- @aggregation_cache = {}
485
- @attributes_cache = {}
486
- @new_record = true
487
- @readonly = false
488
- @destroyed = false
489
- @marked_for_destruction = false
490
- @previously_changed = {}
491
- @changed_attributes = {}
492
- @relation = nil
493
-
494
- ensure_proper_type
495
-
496
- populate_with_current_scope_attributes
497
-
498
- assign_attributes(attributes, options) if attributes
499
-
500
- yield self if block_given?
501
- run_callbacks :initialize
502
- end
503
-
504
- # Initialize an empty model object from +coder+. +coder+ must contain
505
- # the attributes necessary for initializing an empty model object. For
506
- # example:
507
- #
508
- # class Post < ActiveRecord::Base
509
- # end
510
- #
511
- # post = Post.allocate
512
- # post.init_with('attributes' => { 'title' => 'hello world' })
513
- # post.title # => 'hello world'
514
- def init_with(coder)
515
- @attributes = self.class.initialize_attributes(coder['attributes'])
516
- @relation = nil
517
-
518
- @attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
519
- @association_cache = {}
520
- @aggregation_cache = {}
521
- @readonly = @destroyed = @marked_for_destruction = false
522
- @new_record = false
523
- run_callbacks :find
524
- run_callbacks :initialize
525
-
526
- self
527
- end
528
-
529
- # Duped objects have no id assigned and are treated as new records. Note
530
- # that this is a "shallow" copy as it copies the object's attributes
531
- # only, not its associations. The extent of a "deep" copy is application
532
- # specific and is therefore left to the application to implement according
533
- # to its need.
534
- # The dup method does not preserve the timestamps (created|updated)_(at|on).
535
- def initialize_dup(other)
536
- cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
537
- self.class.initialize_attributes(cloned_attributes, :serialized => false)
538
-
539
- cloned_attributes.delete(self.class.primary_key)
540
-
541
- @attributes = cloned_attributes
542
-
543
- _run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
544
-
545
- @changed_attributes = {}
546
- self.class.column_defaults.each do |attr, orig_value|
547
- @changed_attributes[attr] = orig_value if field_changed?(attr, orig_value, @attributes[attr])
548
- end
549
-
550
- @aggregation_cache = {}
551
- @association_cache = {}
552
- @attributes_cache = {}
553
- @new_record = true
554
-
555
- ensure_proper_type
556
- populate_with_current_scope_attributes
557
- super
558
- end
559
-
560
- # Backport dup from 1.9 so that initialize_dup() gets called
561
- unless Object.respond_to?(:initialize_dup)
562
- def dup # :nodoc:
563
- copy = super
564
- copy.initialize_dup(self)
565
- copy
566
- end
567
- end
568
-
569
- # Populate +coder+ with attributes about this record that should be
570
- # serialized. The structure of +coder+ defined in this method is
571
- # guaranteed to match the structure of +coder+ passed to the +init_with+
572
- # method.
573
- #
574
- # Example:
575
- #
576
- # class Post < ActiveRecord::Base
577
- # end
578
- # coder = {}
579
- # Post.new.encode_with(coder)
580
- # coder # => { 'id' => nil, ... }
581
- def encode_with(coder)
582
- coder['attributes'] = attributes
583
- end
584
-
585
- # Returns true if +comparison_object+ is the same exact object, or +comparison_object+
586
- # is of the same type and +self+ has an ID and it is equal to +comparison_object.id+.
587
- #
588
- # Note that new records are different from any other record by definition, unless the
589
- # other record is the receiver itself. Besides, if you fetch existing records with
590
- # +select+ and leave the ID out, you're on your own, this predicate will return false.
591
- #
592
- # Note also that destroying a record preserves its ID in the model instance, so deleted
593
- # models are still comparable.
594
- def ==(comparison_object)
595
- super ||
596
- comparison_object.instance_of?(self.class) &&
597
- id.present? &&
598
- comparison_object.id == id
599
- end
600
- alias :eql? :==
601
-
602
- # Delegates to id in order to allow two records of the same type and id to work with something like:
603
- # [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
604
- def hash
605
- id.hash
606
- end
607
-
608
- # Freeze the attributes hash such that associations are still accessible, even on destroyed records.
609
- def freeze
610
- @attributes.freeze; self
611
- end
612
-
613
- # Returns +true+ if the attributes hash has been frozen.
614
- def frozen?
615
- @attributes.frozen?
616
- end
617
-
618
- # Allows sort on objects
619
- def <=>(other_object)
620
- if other_object.is_a?(self.class)
621
- self.to_key <=> other_object.to_key
622
- else
623
- nil
624
- end
625
- end
626
-
627
- # Returns +true+ if the record is read only. Records loaded through joins with piggy-back
628
- # attributes will be marked as read only since they cannot be saved.
629
- def readonly?
630
- @readonly
631
- end
632
-
633
- # Marks this record as read only.
634
- def readonly!
635
- @readonly = true
636
- end
637
-
638
- # Returns the contents of the record as a nicely formatted string.
639
- def inspect
640
- inspection = if @attributes
641
- self.class.column_names.collect { |name|
642
- if has_attribute?(name)
643
- "#{name}: #{attribute_for_inspect(name)}"
644
- end
645
- }.compact.join(", ")
646
- else
647
- "not initialized"
648
- end
649
- "#<#{self.class} #{inspection}>"
650
- end
651
-
652
- # Hackery to accomodate Syck. Remove for 4.0.
653
- def to_yaml(opts = {}) #:nodoc:
654
- if YAML.const_defined?(:ENGINE) && !YAML::ENGINE.syck?
655
- super
656
- else
657
- coder = {}
658
- encode_with(coder)
659
- YAML.quick_emit(self, opts) do |out|
660
- out.map(taguri, to_yaml_style) do |map|
661
- coder.each { |k, v| map.add(k, v) }
662
- end
663
- end
664
- end
665
- end
666
-
667
- # Hackery to accomodate Syck. Remove for 4.0.
668
- def yaml_initialize(tag, coder) #:nodoc:
669
- init_with(coder)
670
- end
671
-
672
- private
673
-
674
- # Under Ruby 1.9, Array#flatten will call #to_ary (recursively) on each of the elements
675
- # of the array, and then rescues from the possible NoMethodError. If those elements are
676
- # ActiveRecord::Base's, then this triggers the various method_missing's that we have,
677
- # which significantly impacts upon performance.
678
- #
679
- # So we can avoid the method_missing hit by explicitly defining #to_ary as nil here.
680
- #
681
- # See also http://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary/
682
- def to_ary # :nodoc:
683
- nil
684
- end
685
-
686
- include ActiveRecord::Persistence
687
278
  extend ActiveModel::Naming
688
- extend QueryCache::ClassMethods
279
+
689
280
  extend ActiveSupport::Benchmarkable
690
281
  extend ActiveSupport::DescendantsTracker
691
282
 
283
+ extend ConnectionHandling
284
+ extend QueryCache::ClassMethods
692
285
  extend Querying
286
+ extend Translation
287
+ extend DynamicMatchers
288
+ extend Explain
289
+ extend Enum
290
+ extend Delegation::DelegateCache
291
+ extend Aggregations::ClassMethods
292
+
293
+ include Core
294
+ include Persistence
693
295
  include ReadonlyAttributes
694
296
  include ModelSchema
695
- extend Translation
696
297
  include Inheritance
697
298
  include Scoping
698
- extend DynamicMatchers
699
299
  include Sanitization
700
- include Integration
701
300
  include AttributeAssignment
702
301
  include ActiveModel::Conversion
302
+ include Integration
703
303
  include Validations
704
- extend CounterCache
705
- include Locking::Optimistic, Locking::Pessimistic
304
+ include CounterCache
305
+ include Attributes
306
+ include AttributeDecorators
307
+ include Locking::Optimistic
308
+ include Locking::Pessimistic
309
+ include DefineCallbacks
706
310
  include AttributeMethods
707
- include Callbacks, ActiveModel::Observing, Timestamp
311
+ include Callbacks
312
+ include Timestamp
708
313
  include Associations
709
- include IdentityMap
710
314
  include ActiveModel::SecurePassword
711
- extend Explain
712
-
713
- # AutosaveAssociation needs to be included before Transactions, because we want
714
- # #save_with_autosave_associations to be wrapped inside a transaction.
715
- include AutosaveAssociation, NestedAttributes
716
- include Aggregations, Transactions, Reflection, Serialization, Store
315
+ include AutosaveAssociation
316
+ include NestedAttributes
317
+ include Transactions
318
+ include TouchLater
319
+ include NoTouching
320
+ include Reflection
321
+ include Serialization
322
+ include Store
323
+ include SecureToken
324
+ include Suppressor
717
325
  end
718
- end
719
326
 
720
- require 'active_record/connection_adapters/abstract/connection_specification'
721
- ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
327
+ ActiveSupport.run_load_hooks(:active_record, Base)
328
+ end