activerecord 5.1.5 → 5.2.8.1

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 (261) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +655 -608
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +5 -5
  5. data/examples/performance.rb +2 -0
  6. data/examples/simple.rb +2 -0
  7. data/lib/active_record/aggregations.rb +6 -5
  8. data/lib/active_record/association_relation.rb +7 -5
  9. data/lib/active_record/associations/alias_tracker.rb +19 -27
  10. data/lib/active_record/associations/association.rb +41 -37
  11. data/lib/active_record/associations/association_scope.rb +38 -50
  12. data/lib/active_record/associations/belongs_to_association.rb +28 -9
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -8
  14. data/lib/active_record/associations/builder/association.rb +4 -7
  15. data/lib/active_record/associations/builder/belongs_to.rb +14 -5
  16. data/lib/active_record/associations/builder/collection_association.rb +3 -3
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
  18. data/lib/active_record/associations/builder/has_many.rb +2 -0
  19. data/lib/active_record/associations/builder/has_one.rb +2 -0
  20. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  21. data/lib/active_record/associations/collection_association.rb +59 -47
  22. data/lib/active_record/associations/collection_proxy.rb +20 -49
  23. data/lib/active_record/associations/foreign_association.rb +2 -0
  24. data/lib/active_record/associations/has_many_association.rb +12 -1
  25. data/lib/active_record/associations/has_many_through_association.rb +36 -30
  26. data/lib/active_record/associations/has_one_association.rb +12 -1
  27. data/lib/active_record/associations/has_one_through_association.rb +13 -8
  28. data/lib/active_record/associations/join_dependency/join_association.rb +39 -63
  29. data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
  30. data/lib/active_record/associations/join_dependency/join_part.rb +9 -9
  31. data/lib/active_record/associations/join_dependency.rb +48 -93
  32. data/lib/active_record/associations/preloader/association.rb +45 -61
  33. data/lib/active_record/associations/preloader/through_association.rb +71 -79
  34. data/lib/active_record/associations/preloader.rb +18 -38
  35. data/lib/active_record/associations/singular_association.rb +14 -16
  36. data/lib/active_record/associations/through_association.rb +26 -11
  37. data/lib/active_record/associations.rb +40 -63
  38. data/lib/active_record/attribute_assignment.rb +2 -5
  39. data/lib/active_record/attribute_decorators.rb +3 -2
  40. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
  41. data/lib/active_record/attribute_methods/dirty.rb +32 -216
  42. data/lib/active_record/attribute_methods/primary_key.rb +7 -6
  43. data/lib/active_record/attribute_methods/query.rb +2 -0
  44. data/lib/active_record/attribute_methods/read.rb +9 -3
  45. data/lib/active_record/attribute_methods/serialization.rb +23 -0
  46. data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
  47. data/lib/active_record/attribute_methods/write.rb +21 -9
  48. data/lib/active_record/attribute_methods.rb +65 -24
  49. data/lib/active_record/attributes.rb +7 -6
  50. data/lib/active_record/autosave_association.rb +35 -19
  51. data/lib/active_record/base.rb +2 -0
  52. data/lib/active_record/callbacks.rb +12 -6
  53. data/lib/active_record/coders/json.rb +2 -0
  54. data/lib/active_record/coders/yaml_column.rb +15 -1
  55. data/lib/active_record/collection_cache_key.rb +12 -8
  56. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +142 -42
  57. data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -0
  58. data/lib/active_record/connection_adapters/abstract/database_statements.rb +174 -33
  59. data/lib/active_record/connection_adapters/abstract/query_cache.rb +15 -5
  60. data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -32
  61. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +14 -5
  63. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +64 -6
  64. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
  65. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +152 -81
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +66 -21
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +84 -97
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +110 -173
  69. data/lib/active_record/connection_adapters/column.rb +3 -1
  70. data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
  71. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +13 -2
  72. data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
  73. data/lib/active_record/connection_adapters/mysql/database_statements.rb +47 -2
  74. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
  75. data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
  76. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
  77. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -10
  78. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -30
  79. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +106 -1
  80. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
  81. data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +2 -0
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -0
  84. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +13 -1
  86. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
  93. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -1
  97. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +5 -3
  99. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +8 -2
  102. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
  104. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
  105. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid.rb +3 -1
  108. data/lib/active_record/connection_adapters/postgresql/quoting.rb +18 -0
  109. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  110. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +50 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -11
  112. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
  113. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +234 -112
  114. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
  115. data/lib/active_record/connection_adapters/postgresql/utils.rb +3 -1
  116. data/lib/active_record/connection_adapters/postgresql_adapter.rb +66 -74
  117. data/lib/active_record/connection_adapters/schema_cache.rb +4 -2
  118. data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
  119. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
  120. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +24 -1
  121. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
  122. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
  123. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
  124. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +75 -1
  125. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +82 -95
  126. data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
  127. data/lib/active_record/connection_handling.rb +4 -2
  128. data/lib/active_record/core.rb +51 -61
  129. data/lib/active_record/counter_cache.rb +20 -15
  130. data/lib/active_record/define_callbacks.rb +5 -3
  131. data/lib/active_record/dynamic_matchers.rb +9 -9
  132. data/lib/active_record/enum.rb +18 -13
  133. data/lib/active_record/errors.rb +60 -15
  134. data/lib/active_record/explain.rb +3 -1
  135. data/lib/active_record/explain_registry.rb +2 -0
  136. data/lib/active_record/explain_subscriber.rb +2 -0
  137. data/lib/active_record/fixture_set/file.rb +2 -0
  138. data/lib/active_record/fixtures.rb +67 -60
  139. data/lib/active_record/gem_version.rb +5 -3
  140. data/lib/active_record/inheritance.rb +49 -19
  141. data/lib/active_record/integration.rb +58 -19
  142. data/lib/active_record/internal_metadata.rb +2 -0
  143. data/lib/active_record/legacy_yaml_adapter.rb +3 -1
  144. data/lib/active_record/locking/optimistic.rb +30 -42
  145. data/lib/active_record/locking/pessimistic.rb +9 -6
  146. data/lib/active_record/log_subscriber.rb +43 -0
  147. data/lib/active_record/migration/command_recorder.rb +11 -9
  148. data/lib/active_record/migration/compatibility.rb +47 -9
  149. data/lib/active_record/migration/join_table.rb +2 -0
  150. data/lib/active_record/migration.rb +189 -139
  151. data/lib/active_record/model_schema.rb +19 -24
  152. data/lib/active_record/nested_attributes.rb +18 -6
  153. data/lib/active_record/no_touching.rb +3 -1
  154. data/lib/active_record/null_relation.rb +2 -0
  155. data/lib/active_record/persistence.rb +198 -49
  156. data/lib/active_record/query_cache.rb +12 -14
  157. data/lib/active_record/querying.rb +4 -2
  158. data/lib/active_record/railtie.rb +80 -6
  159. data/lib/active_record/railties/console_sandbox.rb +2 -0
  160. data/lib/active_record/railties/controller_runtime.rb +2 -0
  161. data/lib/active_record/railties/databases.rake +46 -36
  162. data/lib/active_record/readonly_attributes.rb +3 -2
  163. data/lib/active_record/reflection.rb +108 -194
  164. data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
  165. data/lib/active_record/relation/batches.rb +20 -5
  166. data/lib/active_record/relation/calculations.rb +46 -20
  167. data/lib/active_record/relation/delegation.rb +45 -27
  168. data/lib/active_record/relation/finder_methods.rb +77 -78
  169. data/lib/active_record/relation/from_clause.rb +2 -8
  170. data/lib/active_record/relation/merger.rb +53 -23
  171. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
  172. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  173. data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
  174. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
  175. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  176. data/lib/active_record/relation/predicate_builder/range_handler.rb +26 -9
  177. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  178. data/lib/active_record/relation/predicate_builder.rb +60 -79
  179. data/lib/active_record/relation/query_attribute.rb +28 -2
  180. data/lib/active_record/relation/query_methods.rb +129 -100
  181. data/lib/active_record/relation/record_fetch_warning.rb +2 -0
  182. data/lib/active_record/relation/spawn_methods.rb +4 -2
  183. data/lib/active_record/relation/where_clause.rb +65 -68
  184. data/lib/active_record/relation/where_clause_factory.rb +5 -48
  185. data/lib/active_record/relation.rb +120 -214
  186. data/lib/active_record/result.rb +2 -0
  187. data/lib/active_record/runtime_registry.rb +2 -0
  188. data/lib/active_record/sanitization.rb +129 -121
  189. data/lib/active_record/schema.rb +4 -2
  190. data/lib/active_record/schema_dumper.rb +36 -26
  191. data/lib/active_record/schema_migration.rb +2 -0
  192. data/lib/active_record/scoping/default.rb +8 -9
  193. data/lib/active_record/scoping/named.rb +23 -7
  194. data/lib/active_record/scoping.rb +9 -8
  195. data/lib/active_record/secure_token.rb +2 -0
  196. data/lib/active_record/serialization.rb +2 -0
  197. data/lib/active_record/statement_cache.rb +23 -13
  198. data/lib/active_record/store.rb +3 -1
  199. data/lib/active_record/suppressor.rb +2 -0
  200. data/lib/active_record/table_metadata.rb +12 -3
  201. data/lib/active_record/tasks/database_tasks.rb +26 -15
  202. data/lib/active_record/tasks/mysql_database_tasks.rb +9 -48
  203. data/lib/active_record/tasks/postgresql_database_tasks.rb +10 -2
  204. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
  205. data/lib/active_record/timestamp.rb +13 -6
  206. data/lib/active_record/touch_later.rb +2 -0
  207. data/lib/active_record/transactions.rb +33 -28
  208. data/lib/active_record/translation.rb +2 -0
  209. data/lib/active_record/type/adapter_specific_registry.rb +2 -0
  210. data/lib/active_record/type/date.rb +2 -0
  211. data/lib/active_record/type/date_time.rb +2 -0
  212. data/lib/active_record/type/decimal_without_scale.rb +2 -0
  213. data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
  214. data/lib/active_record/type/internal/timezone.rb +2 -0
  215. data/lib/active_record/type/json.rb +30 -0
  216. data/lib/active_record/type/serialized.rb +6 -0
  217. data/lib/active_record/type/text.rb +2 -0
  218. data/lib/active_record/type/time.rb +2 -0
  219. data/lib/active_record/type/type_map.rb +2 -0
  220. data/lib/active_record/type/unsigned_integer.rb +2 -0
  221. data/lib/active_record/type.rb +4 -1
  222. data/lib/active_record/type_caster/connection.rb +2 -0
  223. data/lib/active_record/type_caster/map.rb +3 -1
  224. data/lib/active_record/type_caster.rb +2 -0
  225. data/lib/active_record/validations/absence.rb +2 -0
  226. data/lib/active_record/validations/associated.rb +2 -0
  227. data/lib/active_record/validations/length.rb +2 -0
  228. data/lib/active_record/validations/presence.rb +2 -0
  229. data/lib/active_record/validations/uniqueness.rb +36 -6
  230. data/lib/active_record/validations.rb +2 -0
  231. data/lib/active_record/version.rb +2 -0
  232. data/lib/active_record.rb +11 -4
  233. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  234. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  235. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
  236. data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
  237. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
  238. data/lib/rails/generators/active_record/migration.rb +2 -0
  239. data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
  240. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
  241. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  242. data/lib/rails/generators/active_record.rb +3 -1
  243. metadata +26 -40
  244. data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
  245. data/lib/active_record/associations/preloader/collection_association.rb +0 -17
  246. data/lib/active_record/associations/preloader/has_many.rb +0 -15
  247. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  248. data/lib/active_record/associations/preloader/has_one.rb +0 -15
  249. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  250. data/lib/active_record/associations/preloader/singular_association.rb +0 -18
  251. data/lib/active_record/attribute/user_provided_default.rb +0 -30
  252. data/lib/active_record/attribute.rb +0 -240
  253. data/lib/active_record/attribute_mutation_tracker.rb +0 -114
  254. data/lib/active_record/attribute_set/builder.rb +0 -124
  255. data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
  256. data/lib/active_record/attribute_set.rb +0 -113
  257. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
  258. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  259. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
  260. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
  261. data/lib/active_record/type/internal/abstract_json.rb +0 -37
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Type
5
+ class Json < ActiveModel::Type::Value
6
+ include ActiveModel::Type::Helpers::Mutable
7
+
8
+ def type
9
+ :json
10
+ end
11
+
12
+ def deserialize(value)
13
+ return value unless value.is_a?(::String)
14
+ ActiveSupport::JSON.decode(value) rescue nil
15
+ end
16
+
17
+ def serialize(value)
18
+ ActiveSupport::JSON.encode(value) unless value.nil?
19
+ end
20
+
21
+ def changed_in_place?(raw_old_value, new_value)
22
+ deserialize(raw_old_value) != new_value
23
+ end
24
+
25
+ def accessor
26
+ ActiveRecord::Store::StringKeyedHashAccessor
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module Type
3
5
  class Serialized < DelegateClass(ActiveModel::Type::Value) # :nodoc:
@@ -49,6 +51,10 @@ module ActiveRecord
49
51
  end
50
52
  end
51
53
 
54
+ def force_equality?(value)
55
+ coder.respond_to?(:object_class) && value.is_a?(coder.object_class)
56
+ end
57
+
52
58
  private
53
59
 
54
60
  def default_value?(value)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module Type
3
5
  class Text < ActiveModel::Type::String # :nodoc:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module Type
3
5
  class Time < ActiveModel::Type::Time
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "concurrent/map"
2
4
 
3
5
  module ActiveRecord
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module Type
3
5
  class UnsignedInteger < ActiveModel::Type::Integer # :nodoc:
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_model/type"
2
4
 
3
- require "active_record/type/internal/abstract_json"
4
5
  require "active_record/type/internal/timezone"
5
6
 
6
7
  require "active_record/type/date"
7
8
  require "active_record/type/date_time"
8
9
  require "active_record/type/decimal_without_scale"
10
+ require "active_record/type/json"
9
11
  require "active_record/type/time"
10
12
  require "active_record/type/text"
11
13
  require "active_record/type/unsigned_integer"
@@ -69,6 +71,7 @@ module ActiveRecord
69
71
  register(:decimal, Type::Decimal, override: false)
70
72
  register(:float, Type::Float, override: false)
71
73
  register(:integer, Type::Integer, override: false)
74
+ register(:json, Type::Json, override: false)
72
75
  register(:string, Type::String, override: false)
73
76
  register(:text, Type::Text, override: false)
74
77
  register(:time, Type::Time, override: false)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module TypeCaster
3
5
  class Connection # :nodoc:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module TypeCaster
3
5
  class Map # :nodoc:
@@ -7,7 +9,7 @@ module ActiveRecord
7
9
 
8
10
  def type_cast_for_database(attr_name, value)
9
11
  return value if value.is_a?(Arel::Nodes::BindParam)
10
- type = types.type_for_attribute(attr_name.to_s)
12
+ type = types.type_for_attribute(attr_name)
11
13
  type.serialize(value)
12
14
  end
13
15
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_record/type_caster/map"
2
4
  require "active_record/type_caster/connection"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module Validations
3
5
  class AbsenceValidator < ActiveModel::Validations::AbsenceValidator # :nodoc:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module Validations
3
5
  class AssociatedValidator < ActiveModel::EachValidator #:nodoc:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module Validations
3
5
  class LengthValidator < ActiveModel::Validations::LengthValidator # :nodoc:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module Validations
3
5
  class PresenceValidator < ActiveModel::Validations::PresenceValidator # :nodoc:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module Validations
3
5
  class UniquenessValidator < ActiveModel::EachValidator # :nodoc:
@@ -6,6 +8,10 @@ module ActiveRecord
6
8
  raise ArgumentError, "#{options[:conditions]} was passed as :conditions but is not callable. " \
7
9
  "Pass a callable instead: `conditions: -> { where(approved: true) }`"
8
10
  end
11
+ unless Array(options[:scope]).all? { |scope| scope.respond_to?(:to_sym) }
12
+ raise ArgumentError, "#{options[:scope]} is not supported format for :scope option. " \
13
+ "Pass a symbol or an array of symbols instead: `scope: :user_id`"
14
+ end
9
15
  super({ case_sensitive: true }.merge!(options))
10
16
  @klass = options[:class]
11
17
  end
@@ -17,7 +23,7 @@ module ActiveRecord
17
23
  relation = build_relation(finder_class, attribute, value)
18
24
  if record.persisted?
19
25
  if finder_class.primary_key
20
- relation = relation.where.not(finder_class.primary_key => record.id_in_database || record.id)
26
+ relation = relation.where.not(finder_class.primary_key => record.id_in_database)
21
27
  else
22
28
  raise UnknownPrimaryKey.new(finder_class, "Can not validate uniqueness for persisted record without primary key.")
23
29
  end
@@ -50,7 +56,33 @@ module ActiveRecord
50
56
  end
51
57
 
52
58
  def build_relation(klass, attribute, value)
53
- klass.unscoped.where!({ attribute => value }, options)
59
+ if reflection = klass._reflect_on_association(attribute)
60
+ attribute = reflection.foreign_key
61
+ value = value.attributes[reflection.klass.primary_key] unless value.nil?
62
+ end
63
+
64
+ if value.nil?
65
+ return klass.unscoped.where!(attribute => value)
66
+ end
67
+
68
+ # the attribute may be an aliased attribute
69
+ if klass.attribute_alias?(attribute)
70
+ attribute = klass.attribute_alias(attribute)
71
+ end
72
+
73
+ attribute_name = attribute.to_s
74
+ value = klass.predicate_builder.build_bind_attribute(attribute_name, value)
75
+
76
+ table = klass.arel_table
77
+ column = klass.columns_hash[attribute_name]
78
+
79
+ comparison = if !options[:case_sensitive]
80
+ # will use SQL LOWER function before comparison, unless it detects a case insensitive collation
81
+ klass.connection.case_insensitive_comparison(table, attribute, column, value)
82
+ else
83
+ klass.connection.case_sensitive_comparison(table, attribute, column, value)
84
+ end
85
+ klass.unscoped.where!(comparison)
54
86
  end
55
87
 
56
88
  def scope_relation(record, relation)
@@ -173,9 +205,7 @@ module ActiveRecord
173
205
  # | # Boom! We now have a duplicate
174
206
  # | # title!
175
207
  #
176
- # This could even happen if you use transactions with the 'serializable'
177
- # isolation level. The best way to work around this problem is to add a unique
178
- # index to the database table using
208
+ # The best way to work around this problem is to add a unique index to the database table using
179
209
  # {connection.add_index}[rdoc-ref:ConnectionAdapters::SchemaStatements#add_index].
180
210
  # In the rare case that a race condition occurs, the database will guarantee
181
211
  # the field's uniqueness.
@@ -187,7 +217,7 @@ module ActiveRecord
187
217
  # can catch it and restart the transaction (e.g. by telling the user
188
218
  # that the title already exists, and asking them to re-enter the title).
189
219
  # This technique is also known as
190
- # {optimistic concurrency control}[http://en.wikipedia.org/wiki/Optimistic_concurrency_control].
220
+ # {optimistic concurrency control}[https://en.wikipedia.org/wiki/Optimistic_concurrency_control].
191
221
  #
192
222
  # The bundled ActiveRecord::ConnectionAdapters distinguish unique index
193
223
  # constraint errors from other types of database errors by throwing an
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  # = Active Record \RecordInvalid
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "gem_version"
2
4
 
3
5
  module ActiveRecord
data/lib/active_record.rb CHANGED
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
- # Copyright (c) 2004-2017 David Heinemeier Hansson
4
+ # Copyright (c) 2004-2018 David Heinemeier Hansson
3
5
  #
4
6
  # Permission is hereby granted, free of charge, to any person obtaining
5
7
  # a copy of this software and associated documentation files (the
@@ -25,14 +27,14 @@ require "active_support"
25
27
  require "active_support/rails"
26
28
  require "active_model"
27
29
  require "arel"
30
+ require "yaml"
28
31
 
29
32
  require "active_record/version"
30
- require "active_record/attribute_set"
33
+ require "active_model/attribute_set"
31
34
 
32
35
  module ActiveRecord
33
36
  extend ActiveSupport::Autoload
34
37
 
35
- autoload :Attribute
36
38
  autoload :Base
37
39
  autoload :Callbacks
38
40
  autoload :Core
@@ -102,6 +104,7 @@ module ActiveRecord
102
104
 
103
105
  autoload :Result
104
106
  autoload :TableMetadata
107
+ autoload :Type
105
108
  end
106
109
 
107
110
  module Coders
@@ -177,5 +180,9 @@ ActiveSupport.on_load(:active_record) do
177
180
  end
178
181
 
179
182
  ActiveSupport.on_load(:i18n) do
180
- I18n.load_path << File.dirname(__FILE__) + "/active_record/locale/en.yml"
183
+ I18n.load_path << File.expand_path("active_record/locale/en.yml", __dir__)
181
184
  end
185
+
186
+ YAML.load_tags["!ruby/object:ActiveRecord::AttributeSet"] = "ActiveModel::AttributeSet"
187
+ YAML.load_tags["!ruby/object:ActiveRecord::Attribute::FromDatabase"] = "ActiveModel::Attribute::FromDatabase"
188
+ YAML.load_tags["!ruby/object:ActiveRecord::LazyAttributeHash"] = "ActiveModel::LazyAttributeHash"
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/active_record"
4
+
5
+ module ActiveRecord
6
+ module Generators # :nodoc:
7
+ class ApplicationRecordGenerator < ::Rails::Generators::Base # :nodoc:
8
+ source_root File.expand_path("templates", __dir__)
9
+
10
+ # FIXME: Change this file to a symlink once RubyGems 2.5.0 is required.
11
+ def create_application_record
12
+ template "application_record.rb", application_record_file_name
13
+ end
14
+
15
+ private
16
+
17
+ def application_record_file_name
18
+ @application_record_file_name ||=
19
+ if namespaced?
20
+ "app/models/#{namespaced_path}/application_record.rb"
21
+ else
22
+ "app/models/application_record.rb"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails/generators/active_record"
2
4
 
3
5
  module ActiveRecord
@@ -26,7 +28,7 @@ module ActiveRecord
26
28
  def set_local_assigns!
27
29
  @migration_template = "migration.rb"
28
30
  case file_name
29
- when /^(add|remove)_.*_(?:to|from)_(.*)/
31
+ when /^(add)_.*_to_(.*)/, /^(remove)_.*?_from_(.*)/
30
32
  @migration_action = $1
31
33
  @table_name = normalize_table_name($2)
32
34
  when /join_table/
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails/generators/migration"
2
4
 
3
5
  module ActiveRecord
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails/generators/active_record"
2
4
 
3
5
  module ActiveRecord
@@ -21,13 +23,11 @@ module ActiveRecord
21
23
  end
22
24
 
23
25
  def create_model_file
24
- generate_application_record
25
26
  template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
26
27
  end
27
28
 
28
29
  def create_module_file
29
30
  return if regular_class_path.empty?
30
- generate_application_record
31
31
  template "module.rb", File.join("app/models", "#{class_path.join('/')}.rb") if behavior == :invoke
32
32
  end
33
33
 
@@ -39,31 +39,10 @@ module ActiveRecord
39
39
  attributes.select { |a| !a.reference? && a.has_index? }
40
40
  end
41
41
 
42
- # FIXME: Change this file to a symlink once RubyGems 2.5.0 is required.
43
- def generate_application_record
44
- if behavior == :invoke && !application_record_exist?
45
- template "application_record.rb", application_record_file_name
46
- end
47
- end
48
-
49
42
  # Used by the migration template to determine the parent name of the model
50
43
  def parent_class_name
51
44
  options[:parent] || "ApplicationRecord"
52
45
  end
53
-
54
- def application_record_exist?
55
- file_exist = nil
56
- in_root { file_exist = File.exist?(application_record_file_name) }
57
- file_exist
58
- end
59
-
60
- def application_record_file_name
61
- @application_record_file_name ||= if mountable_engine?
62
- "app/models/#{namespaced_path}/application_record.rb"
63
- else
64
- "app/models/application_record.rb"
65
- end
66
- end
67
46
  end
68
47
  end
69
48
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails/generators/named_base"
2
4
  require "rails/generators/active_model"
3
5
  require "rails/generators/active_record/migration"
@@ -10,7 +12,7 @@ module ActiveRecord
10
12
 
11
13
  # Set the current directory as base for the inherited generators.
12
14
  def self.base_root
13
- File.dirname(__FILE__)
15
+ __dir__
14
16
  end
15
17
  end
16
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.5
4
+ version: 5.2.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-14 00:00:00.000000000 Z
11
+ date: 2022-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,42 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 5.1.5
19
+ version: 5.2.8.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 5.1.5
26
+ version: 5.2.8.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 5.1.5
33
+ version: 5.2.8.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 5.1.5
40
+ version: 5.2.8.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: arel
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '8.0'
47
+ version: '9.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '8.0'
54
+ version: '9.0'
55
55
  description: Databases on Rails. Build a persistent domain model by mapping database
56
56
  tables to Ruby classes. Strong conventions for associations, validations, aggregations,
57
57
  migrations, and testing come baked-in.
@@ -95,18 +95,9 @@ files:
95
95
  - lib/active_record/associations/join_dependency/join_part.rb
96
96
  - lib/active_record/associations/preloader.rb
97
97
  - lib/active_record/associations/preloader/association.rb
98
- - lib/active_record/associations/preloader/belongs_to.rb
99
- - lib/active_record/associations/preloader/collection_association.rb
100
- - lib/active_record/associations/preloader/has_many.rb
101
- - lib/active_record/associations/preloader/has_many_through.rb
102
- - lib/active_record/associations/preloader/has_one.rb
103
- - lib/active_record/associations/preloader/has_one_through.rb
104
- - lib/active_record/associations/preloader/singular_association.rb
105
98
  - lib/active_record/associations/preloader/through_association.rb
106
99
  - lib/active_record/associations/singular_association.rb
107
100
  - lib/active_record/associations/through_association.rb
108
- - lib/active_record/attribute.rb
109
- - lib/active_record/attribute/user_provided_default.rb
110
101
  - lib/active_record/attribute_assignment.rb
111
102
  - lib/active_record/attribute_decorators.rb
112
103
  - lib/active_record/attribute_methods.rb
@@ -118,10 +109,6 @@ files:
118
109
  - lib/active_record/attribute_methods/serialization.rb
119
110
  - lib/active_record/attribute_methods/time_zone_conversion.rb
120
111
  - lib/active_record/attribute_methods/write.rb
121
- - lib/active_record/attribute_mutation_tracker.rb
122
- - lib/active_record/attribute_set.rb
123
- - lib/active_record/attribute_set/builder.rb
124
- - lib/active_record/attribute_set/yaml_encoder.rb
125
112
  - lib/active_record/attributes.rb
126
113
  - lib/active_record/autosave_association.rb
127
114
  - lib/active_record/base.rb
@@ -164,12 +151,12 @@ files:
164
151
  - lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb
165
152
  - lib/active_record/connection_adapters/postgresql/oid/bytea.rb
166
153
  - lib/active_record/connection_adapters/postgresql/oid/cidr.rb
154
+ - lib/active_record/connection_adapters/postgresql/oid/date.rb
167
155
  - lib/active_record/connection_adapters/postgresql/oid/date_time.rb
168
156
  - lib/active_record/connection_adapters/postgresql/oid/decimal.rb
169
157
  - lib/active_record/connection_adapters/postgresql/oid/enum.rb
170
158
  - lib/active_record/connection_adapters/postgresql/oid/hstore.rb
171
159
  - lib/active_record/connection_adapters/postgresql/oid/inet.rb
172
- - lib/active_record/connection_adapters/postgresql/oid/json.rb
173
160
  - lib/active_record/connection_adapters/postgresql/oid/jsonb.rb
174
161
  - lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb
175
162
  - lib/active_record/connection_adapters/postgresql/oid/money.rb
@@ -236,7 +223,6 @@ files:
236
223
  - lib/active_record/railties/console_sandbox.rb
237
224
  - lib/active_record/railties/controller_runtime.rb
238
225
  - lib/active_record/railties/databases.rake
239
- - lib/active_record/railties/jdbcmysql_error.rb
240
226
  - lib/active_record/readonly_attributes.rb
241
227
  - lib/active_record/reflection.rb
242
228
  - lib/active_record/relation.rb
@@ -249,10 +235,10 @@ files:
249
235
  - lib/active_record/relation/merger.rb
250
236
  - lib/active_record/relation/predicate_builder.rb
251
237
  - lib/active_record/relation/predicate_builder/array_handler.rb
252
- - lib/active_record/relation/predicate_builder/association_query_handler.rb
238
+ - lib/active_record/relation/predicate_builder/association_query_value.rb
253
239
  - lib/active_record/relation/predicate_builder/base_handler.rb
254
240
  - lib/active_record/relation/predicate_builder/basic_object_handler.rb
255
- - lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb
241
+ - lib/active_record/relation/predicate_builder/polymorphic_array_value.rb
256
242
  - lib/active_record/relation/predicate_builder/range_handler.rb
257
243
  - lib/active_record/relation/predicate_builder/relation_handler.rb
258
244
  - lib/active_record/relation/query_attribute.rb
@@ -290,8 +276,8 @@ files:
290
276
  - lib/active_record/type/date_time.rb
291
277
  - lib/active_record/type/decimal_without_scale.rb
292
278
  - lib/active_record/type/hash_lookup_type_map.rb
293
- - lib/active_record/type/internal/abstract_json.rb
294
279
  - lib/active_record/type/internal/timezone.rb
280
+ - lib/active_record/type/json.rb
295
281
  - lib/active_record/type/serialized.rb
296
282
  - lib/active_record/type/text.rb
297
283
  - lib/active_record/type/time.rb
@@ -308,21 +294,22 @@ files:
308
294
  - lib/active_record/validations/uniqueness.rb
309
295
  - lib/active_record/version.rb
310
296
  - lib/rails/generators/active_record.rb
297
+ - lib/rails/generators/active_record/application_record/application_record_generator.rb
298
+ - lib/rails/generators/active_record/application_record/templates/application_record.rb.tt
311
299
  - lib/rails/generators/active_record/migration.rb
312
300
  - lib/rails/generators/active_record/migration/migration_generator.rb
313
- - lib/rails/generators/active_record/migration/templates/create_table_migration.rb
314
- - lib/rails/generators/active_record/migration/templates/migration.rb
301
+ - lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt
302
+ - lib/rails/generators/active_record/migration/templates/migration.rb.tt
315
303
  - lib/rails/generators/active_record/model/model_generator.rb
316
- - lib/rails/generators/active_record/model/templates/application_record.rb
317
- - lib/rails/generators/active_record/model/templates/model.rb
318
- - lib/rails/generators/active_record/model/templates/module.rb
304
+ - lib/rails/generators/active_record/model/templates/model.rb.tt
305
+ - lib/rails/generators/active_record/model/templates/module.rb.tt
319
306
  homepage: http://rubyonrails.org
320
307
  licenses:
321
308
  - MIT
322
309
  metadata:
323
- source_code_uri: https://github.com/rails/rails/tree/v5.1.5/activerecord
324
- changelog_uri: https://github.com/rails/rails/blob/v5.1.5/activerecord/CHANGELOG.md
325
- post_install_message:
310
+ source_code_uri: https://github.com/rails/rails/tree/v5.2.8.1/activerecord
311
+ changelog_uri: https://github.com/rails/rails/blob/v5.2.8.1/activerecord/CHANGELOG.md
312
+ post_install_message:
326
313
  rdoc_options:
327
314
  - "--main"
328
315
  - README.rdoc
@@ -339,9 +326,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
339
326
  - !ruby/object:Gem::Version
340
327
  version: '0'
341
328
  requirements: []
342
- rubyforge_project:
343
- rubygems_version: 2.7.3
344
- signing_key:
329
+ rubygems_version: 3.3.3
330
+ signing_key:
345
331
  specification_version: 4
346
332
  summary: Object-relational mapper framework (part of Rails).
347
333
  test_files: []
@@ -1,15 +0,0 @@
1
- module ActiveRecord
2
- module Associations
3
- class Preloader
4
- class BelongsTo < SingularAssociation #:nodoc:
5
- def association_key_name
6
- reflection.options[:primary_key] || klass && klass.primary_key
7
- end
8
-
9
- def owner_key_name
10
- reflection.foreign_key
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,17 +0,0 @@
1
- module ActiveRecord
2
- module Associations
3
- class Preloader
4
- class CollectionAssociation < Association #:nodoc:
5
- private
6
-
7
- def preload(preloader)
8
- associated_records_by_owner(preloader).each do |owner, records|
9
- association = owner.association(reflection.name)
10
- association.loaded!
11
- association.target.concat(records)
12
- end
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,15 +0,0 @@
1
- module ActiveRecord
2
- module Associations
3
- class Preloader
4
- class HasMany < CollectionAssociation #:nodoc:
5
- def association_key_name
6
- reflection.foreign_key
7
- end
8
-
9
- def owner_key_name
10
- reflection.active_record_primary_key
11
- end
12
- end
13
- end
14
- end
15
- end