activerecord 5.1.7 → 5.2.0.beta1

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 (259) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +221 -900
  3. data/README.rdoc +3 -3
  4. data/examples/performance.rb +2 -0
  5. data/examples/simple.rb +2 -0
  6. data/lib/active_record.rb +10 -3
  7. data/lib/active_record/aggregations.rb +2 -0
  8. data/lib/active_record/association_relation.rb +2 -0
  9. data/lib/active_record/associations.rb +13 -42
  10. data/lib/active_record/associations/alias_tracker.rb +17 -17
  11. data/lib/active_record/associations/association.rb +11 -22
  12. data/lib/active_record/associations/association_scope.rb +32 -44
  13. data/lib/active_record/associations/belongs_to_association.rb +6 -4
  14. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +3 -1
  15. data/lib/active_record/associations/builder/association.rb +2 -5
  16. data/lib/active_record/associations/builder/belongs_to.rb +7 -12
  17. data/lib/active_record/associations/builder/collection_association.rb +1 -1
  18. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
  19. data/lib/active_record/associations/builder/has_many.rb +2 -0
  20. data/lib/active_record/associations/builder/has_one.rb +2 -0
  21. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  22. data/lib/active_record/associations/collection_association.rb +41 -33
  23. data/lib/active_record/associations/collection_proxy.rb +11 -14
  24. data/lib/active_record/associations/foreign_association.rb +2 -0
  25. data/lib/active_record/associations/has_many_association.rb +4 -2
  26. data/lib/active_record/associations/has_many_through_association.rb +4 -2
  27. data/lib/active_record/associations/has_one_association.rb +3 -1
  28. data/lib/active_record/associations/has_one_through_association.rb +3 -1
  29. data/lib/active_record/associations/join_dependency.rb +22 -40
  30. data/lib/active_record/associations/join_dependency/join_association.rb +17 -56
  31. data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
  32. data/lib/active_record/associations/join_dependency/join_part.rb +2 -9
  33. data/lib/active_record/associations/preloader.rb +17 -37
  34. data/lib/active_record/associations/preloader/association.rb +42 -58
  35. data/lib/active_record/associations/preloader/through_association.rb +71 -79
  36. data/lib/active_record/associations/singular_association.rb +14 -10
  37. data/lib/active_record/associations/through_association.rb +3 -1
  38. data/lib/active_record/attribute_assignment.rb +2 -0
  39. data/lib/active_record/attribute_decorators.rb +3 -2
  40. data/lib/active_record/attribute_methods.rb +47 -7
  41. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
  42. data/lib/active_record/attribute_methods/dirty.rb +25 -214
  43. data/lib/active_record/attribute_methods/primary_key.rb +7 -6
  44. data/lib/active_record/attribute_methods/query.rb +2 -0
  45. data/lib/active_record/attribute_methods/read.rb +8 -2
  46. data/lib/active_record/attribute_methods/serialization.rb +23 -0
  47. data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
  48. data/lib/active_record/attribute_methods/write.rb +21 -9
  49. data/lib/active_record/attributes.rb +7 -6
  50. data/lib/active_record/autosave_association.rb +5 -11
  51. data/lib/active_record/base.rb +2 -0
  52. data/lib/active_record/callbacks.rb +6 -8
  53. data/lib/active_record/coders/json.rb +2 -0
  54. data/lib/active_record/coders/yaml_column.rb +2 -0
  55. data/lib/active_record/collection_cache_key.rb +10 -5
  56. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +110 -35
  57. data/lib/active_record/connection_adapters/abstract/database_limits.rb +2 -0
  58. data/lib/active_record/connection_adapters/abstract/database_statements.rb +120 -28
  59. data/lib/active_record/connection_adapters/abstract/query_cache.rb +7 -2
  60. data/lib/active_record/connection_adapters/abstract/quoting.rb +14 -33
  61. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +13 -5
  63. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +40 -2
  64. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
  65. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +103 -63
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +45 -9
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +62 -90
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +75 -138
  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 +2 -0
  72. data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
  73. data/lib/active_record/connection_adapters/mysql/database_statements.rb +3 -1
  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 -6
  78. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -30
  79. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +91 -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.rb +2 -1
  86. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +3 -11
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -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 +3 -1
  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 +3 -5
  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/quoting.rb +10 -0
  108. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  109. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +2 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +11 -7
  111. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
  112. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +79 -65
  113. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
  114. data/lib/active_record/connection_adapters/postgresql/utils.rb +2 -0
  115. data/lib/active_record/connection_adapters/postgresql_adapter.rb +47 -82
  116. data/lib/active_record/connection_adapters/schema_cache.rb +2 -0
  117. data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
  118. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
  119. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +19 -2
  120. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
  121. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
  122. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
  123. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +71 -1
  124. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +34 -89
  125. data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
  126. data/lib/active_record/connection_handling.rb +4 -2
  127. data/lib/active_record/core.rb +27 -57
  128. data/lib/active_record/counter_cache.rb +15 -12
  129. data/lib/active_record/define_callbacks.rb +5 -3
  130. data/lib/active_record/dynamic_matchers.rb +9 -9
  131. data/lib/active_record/enum.rb +15 -13
  132. data/lib/active_record/errors.rb +54 -21
  133. data/lib/active_record/explain.rb +3 -1
  134. data/lib/active_record/explain_registry.rb +2 -0
  135. data/lib/active_record/explain_subscriber.rb +2 -0
  136. data/lib/active_record/fixture_set/file.rb +2 -0
  137. data/lib/active_record/fixtures.rb +40 -24
  138. data/lib/active_record/gem_version.rb +5 -3
  139. data/lib/active_record/inheritance.rb +6 -5
  140. data/lib/active_record/integration.rb +58 -19
  141. data/lib/active_record/internal_metadata.rb +2 -0
  142. data/lib/active_record/legacy_yaml_adapter.rb +3 -1
  143. data/lib/active_record/locking/optimistic.rb +31 -20
  144. data/lib/active_record/locking/pessimistic.rb +10 -7
  145. data/lib/active_record/log_subscriber.rb +2 -0
  146. data/lib/active_record/migration.rb +47 -21
  147. data/lib/active_record/migration/command_recorder.rb +11 -9
  148. data/lib/active_record/migration/compatibility.rb +20 -2
  149. data/lib/active_record/migration/join_table.rb +2 -0
  150. data/lib/active_record/model_schema.rb +29 -38
  151. data/lib/active_record/nested_attributes.rb +18 -6
  152. data/lib/active_record/no_touching.rb +3 -1
  153. data/lib/active_record/null_relation.rb +2 -0
  154. data/lib/active_record/persistence.rb +184 -40
  155. data/lib/active_record/query_cache.rb +17 -12
  156. data/lib/active_record/querying.rb +3 -1
  157. data/lib/active_record/railtie.rb +54 -1
  158. data/lib/active_record/railties/console_sandbox.rb +2 -0
  159. data/lib/active_record/railties/controller_runtime.rb +2 -0
  160. data/lib/active_record/railties/databases.rake +41 -28
  161. data/lib/active_record/readonly_attributes.rb +3 -2
  162. data/lib/active_record/reflection.rb +100 -182
  163. data/lib/active_record/relation.rb +61 -193
  164. data/lib/active_record/relation/batches.rb +20 -5
  165. data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
  166. data/lib/active_record/relation/calculations.rb +40 -23
  167. data/lib/active_record/relation/delegation.rb +10 -27
  168. data/lib/active_record/relation/finder_methods.rb +53 -49
  169. data/lib/active_record/relation/from_clause.rb +2 -8
  170. data/lib/active_record/relation/merger.rb +22 -19
  171. data/lib/active_record/relation/predicate_builder.rb +42 -79
  172. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
  173. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  174. data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
  175. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
  176. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +54 -0
  177. data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -6
  178. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  179. data/lib/active_record/relation/query_attribute.rb +9 -2
  180. data/lib/active_record/relation/query_methods.rb +80 -69
  181. data/lib/active_record/relation/record_fetch_warning.rb +2 -0
  182. data/lib/active_record/relation/spawn_methods.rb +2 -0
  183. data/lib/active_record/relation/where_clause.rb +50 -67
  184. data/lib/active_record/relation/where_clause_factory.rb +4 -46
  185. data/lib/active_record/result.rb +2 -0
  186. data/lib/active_record/runtime_registry.rb +2 -0
  187. data/lib/active_record/sanitization.rb +15 -9
  188. data/lib/active_record/schema.rb +3 -1
  189. data/lib/active_record/schema_dumper.rb +24 -23
  190. data/lib/active_record/schema_migration.rb +2 -0
  191. data/lib/active_record/scoping.rb +9 -8
  192. data/lib/active_record/scoping/default.rb +6 -7
  193. data/lib/active_record/scoping/named.rb +15 -7
  194. data/lib/active_record/secure_token.rb +2 -0
  195. data/lib/active_record/serialization.rb +2 -0
  196. data/lib/active_record/statement_cache.rb +22 -12
  197. data/lib/active_record/store.rb +2 -0
  198. data/lib/active_record/suppressor.rb +2 -0
  199. data/lib/active_record/table_metadata.rb +3 -1
  200. data/lib/active_record/tasks/database_tasks.rb +23 -12
  201. data/lib/active_record/tasks/mysql_database_tasks.rb +9 -48
  202. data/lib/active_record/tasks/postgresql_database_tasks.rb +10 -2
  203. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
  204. data/lib/active_record/timestamp.rb +5 -12
  205. data/lib/active_record/touch_later.rb +2 -0
  206. data/lib/active_record/transactions.rb +9 -7
  207. data/lib/active_record/translation.rb +2 -0
  208. data/lib/active_record/type.rb +4 -1
  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 +2 -4
  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_caster.rb +2 -0
  222. data/lib/active_record/type_caster/connection.rb +2 -0
  223. data/lib/active_record/type_caster/map.rb +2 -0
  224. data/lib/active_record/validations.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/version.rb +2 -0
  231. data/lib/rails/generators/active_record.rb +3 -1
  232. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  233. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  234. data/lib/rails/generators/active_record/migration.rb +2 -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/model/model_generator.rb +2 -23
  239. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
  240. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  241. metadata +25 -38
  242. data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
  243. data/lib/active_record/associations/preloader/collection_association.rb +0 -17
  244. data/lib/active_record/associations/preloader/has_many.rb +0 -15
  245. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  246. data/lib/active_record/associations/preloader/has_one.rb +0 -15
  247. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  248. data/lib/active_record/associations/preloader/singular_association.rb +0 -18
  249. data/lib/active_record/attribute.rb +0 -240
  250. data/lib/active_record/attribute/user_provided_default.rb +0 -30
  251. data/lib/active_record/attribute_mutation_tracker.rb +0 -122
  252. data/lib/active_record/attribute_set.rb +0 -113
  253. data/lib/active_record/attribute_set/builder.rb +0 -126
  254. data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
  255. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
  256. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  257. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
  258. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
  259. data/lib/active_record/type/internal/abstract_json.rb +0 -37
@@ -1,30 +0,0 @@
1
- require "active_record/attribute"
2
-
3
- module ActiveRecord
4
- class Attribute # :nodoc:
5
- class UserProvidedDefault < FromUser # :nodoc:
6
- def initialize(name, value, type, database_default)
7
- @user_provided_value = value
8
- super(name, value, type, database_default)
9
- end
10
-
11
- def value_before_type_cast
12
- if user_provided_value.is_a?(Proc)
13
- @memoized_value_before_type_cast ||= user_provided_value.call
14
- else
15
- @user_provided_value
16
- end
17
- end
18
-
19
- def with_type(type)
20
- self.class.new(name, user_provided_value, type, original_attribute)
21
- end
22
-
23
- # TODO Change this to private once we've dropped Ruby 2.2 support.
24
- # Workaround for Ruby 2.2 "private attribute?" warning.
25
- protected
26
-
27
- attr_reader :user_provided_value
28
- end
29
- end
30
- end
@@ -1,122 +0,0 @@
1
- module ActiveRecord
2
- class AttributeMutationTracker # :nodoc:
3
- OPTION_NOT_GIVEN = Object.new
4
-
5
- def initialize(attributes)
6
- @attributes = attributes
7
- @forced_changes = Set.new
8
- @deprecated_forced_changes = Set.new
9
- end
10
-
11
- def changed_attribute_names
12
- attr_names.select { |attr_name| changed?(attr_name) }
13
- end
14
-
15
- def changed_values
16
- attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
17
- if changed?(attr_name)
18
- result[attr_name] = attributes[attr_name].original_value
19
- end
20
- end
21
- end
22
-
23
- def changes
24
- attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
25
- change = change_to_attribute(attr_name)
26
- if change
27
- result.merge!(attr_name => change)
28
- end
29
- end
30
- end
31
-
32
- def change_to_attribute(attr_name)
33
- attr_name = attr_name.to_s
34
- if changed?(attr_name)
35
- [attributes[attr_name].original_value, attributes.fetch_value(attr_name)]
36
- end
37
- end
38
-
39
- def any_changes?
40
- attr_names.any? { |attr| changed?(attr) } || deprecated_forced_changes.any?
41
- end
42
-
43
- def changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
44
- attr_name = attr_name.to_s
45
- forced_changes.include?(attr_name) ||
46
- attributes[attr_name].changed? &&
47
- (OPTION_NOT_GIVEN == from || attributes[attr_name].original_value == from) &&
48
- (OPTION_NOT_GIVEN == to || attributes[attr_name].value == to)
49
- end
50
-
51
- def changed_in_place?(attr_name)
52
- attributes[attr_name.to_s].changed_in_place?
53
- end
54
-
55
- def forget_change(attr_name)
56
- attr_name = attr_name.to_s
57
- attributes[attr_name] = attributes[attr_name].forgetting_assignment
58
- forced_changes.delete(attr_name)
59
- end
60
-
61
- def original_value(attr_name)
62
- attributes[attr_name.to_s].original_value
63
- end
64
-
65
- def force_change(attr_name)
66
- forced_changes << attr_name.to_s
67
- end
68
-
69
- def deprecated_force_change(attr_name)
70
- deprecated_forced_changes << attr_name.to_s
71
- end
72
-
73
- # TODO Change this to private once we've dropped Ruby 2.2 support.
74
- # Workaround for Ruby 2.2 "private attribute?" warning.
75
- protected
76
-
77
- attr_reader :attributes, :forced_changes, :deprecated_forced_changes
78
-
79
- private
80
-
81
- def attr_names
82
- attributes.keys
83
- end
84
- end
85
-
86
- class NullMutationTracker # :nodoc:
87
- include Singleton
88
-
89
- def changed_attribute_names(*)
90
- []
91
- end
92
-
93
- def changed_values(*)
94
- {}
95
- end
96
-
97
- def changes(*)
98
- {}
99
- end
100
-
101
- def change_to_attribute(attr_name)
102
- end
103
-
104
- def any_changes?(*)
105
- false
106
- end
107
-
108
- def changed?(*)
109
- false
110
- end
111
-
112
- def changed_in_place?(*)
113
- false
114
- end
115
-
116
- def forget_change(*)
117
- end
118
-
119
- def original_value(*)
120
- end
121
- end
122
- end
@@ -1,113 +0,0 @@
1
- require "active_record/attribute_set/builder"
2
- require "active_record/attribute_set/yaml_encoder"
3
-
4
- module ActiveRecord
5
- class AttributeSet # :nodoc:
6
- delegate :each_value, :fetch, :except, to: :attributes
7
-
8
- def initialize(attributes)
9
- @attributes = attributes
10
- end
11
-
12
- def [](name)
13
- attributes[name] || Attribute.null(name)
14
- end
15
-
16
- def []=(name, value)
17
- attributes[name] = value
18
- end
19
-
20
- def values_before_type_cast
21
- attributes.transform_values(&:value_before_type_cast)
22
- end
23
-
24
- def to_hash
25
- initialized_attributes.transform_values(&:value)
26
- end
27
- alias_method :to_h, :to_hash
28
-
29
- def key?(name)
30
- attributes.key?(name) && self[name].initialized?
31
- end
32
-
33
- def keys
34
- attributes.each_key.select { |name| self[name].initialized? }
35
- end
36
-
37
- if defined?(JRUBY_VERSION)
38
- # This form is significantly faster on JRuby, and this is one of our biggest hotspots.
39
- # https://github.com/jruby/jruby/pull/2562
40
- def fetch_value(name, &block)
41
- self[name].value(&block)
42
- end
43
- else
44
- def fetch_value(name)
45
- self[name].value { |n| yield n if block_given? }
46
- end
47
- end
48
-
49
- def write_from_database(name, value)
50
- attributes[name] = self[name].with_value_from_database(value)
51
- end
52
-
53
- def write_from_user(name, value)
54
- attributes[name] = self[name].with_value_from_user(value)
55
- end
56
-
57
- def write_cast_value(name, value)
58
- attributes[name] = self[name].with_cast_value(value)
59
- end
60
-
61
- def freeze
62
- @attributes.freeze
63
- super
64
- end
65
-
66
- def deep_dup
67
- dup.tap do |copy|
68
- copy.instance_variable_set(:@attributes, attributes.deep_dup)
69
- end
70
- end
71
-
72
- def initialize_dup(_)
73
- @attributes = attributes.dup
74
- super
75
- end
76
-
77
- def initialize_clone(_)
78
- @attributes = attributes.clone
79
- super
80
- end
81
-
82
- def reset(key)
83
- if key?(key)
84
- write_from_database(key, nil)
85
- end
86
- end
87
-
88
- def accessed
89
- attributes.select { |_, attr| attr.has_been_read? }.keys
90
- end
91
-
92
- def map(&block)
93
- new_attributes = attributes.transform_values(&block)
94
- AttributeSet.new(new_attributes)
95
- end
96
-
97
- def ==(other)
98
- attributes == other.attributes
99
- end
100
-
101
- # TODO Change this to private once we've dropped Ruby 2.2 support.
102
- # Workaround for Ruby 2.2 "private attribute?" warning.
103
- protected
104
-
105
- attr_reader :attributes
106
-
107
- private
108
-
109
- def initialized_attributes
110
- attributes.select { |_, attr| attr.initialized? }
111
- end
112
- end
113
- end
@@ -1,126 +0,0 @@
1
- require "active_record/attribute"
2
-
3
- module ActiveRecord
4
- class AttributeSet # :nodoc:
5
- class Builder # :nodoc:
6
- attr_reader :types, :default_attributes
7
-
8
- def initialize(types, default_attributes = {})
9
- @types = types
10
- @default_attributes = default_attributes
11
- end
12
-
13
- def build_from_database(values = {}, additional_types = {})
14
- attributes = LazyAttributeHash.new(types, values, additional_types, default_attributes)
15
- AttributeSet.new(attributes)
16
- end
17
- end
18
- end
19
-
20
- class LazyAttributeHash # :nodoc:
21
- delegate :transform_values, :each_key, :each_value, :fetch, :except, to: :materialize
22
-
23
- def initialize(types, values, additional_types, default_attributes, delegate_hash = {})
24
- @types = types
25
- @values = values
26
- @additional_types = additional_types
27
- @materialized = false
28
- @delegate_hash = delegate_hash
29
- @default_attributes = default_attributes
30
- end
31
-
32
- def key?(key)
33
- delegate_hash.key?(key) || values.key?(key) || types.key?(key)
34
- end
35
-
36
- def [](key)
37
- delegate_hash[key] || assign_default_value(key)
38
- end
39
-
40
- def []=(key, value)
41
- if frozen?
42
- raise RuntimeError, "Can't modify frozen hash"
43
- end
44
- delegate_hash[key] = value
45
- end
46
-
47
- def deep_dup
48
- dup.tap do |copy|
49
- copy.instance_variable_set(:@delegate_hash, delegate_hash.transform_values(&:dup))
50
- end
51
- end
52
-
53
- def initialize_dup(_)
54
- @delegate_hash = Hash[delegate_hash]
55
- super
56
- end
57
-
58
- def select
59
- keys = types.keys | values.keys | delegate_hash.keys
60
- keys.each_with_object({}) do |key, hash|
61
- attribute = self[key]
62
- if yield(key, attribute)
63
- hash[key] = attribute
64
- end
65
- end
66
- end
67
-
68
- def ==(other)
69
- if other.is_a?(LazyAttributeHash)
70
- materialize == other.materialize
71
- else
72
- materialize == other
73
- end
74
- end
75
-
76
- def marshal_dump
77
- [@types, @values, @additional_types, @default_attributes, @delegate_hash]
78
- end
79
-
80
- def marshal_load(values)
81
- if values.is_a?(Hash)
82
- empty_hash = {}.freeze
83
- initialize(empty_hash, empty_hash, empty_hash, empty_hash, values)
84
- @materialized = true
85
- else
86
- initialize(*values)
87
- end
88
- end
89
-
90
- # TODO Change this to private once we've dropped Ruby 2.2 support.
91
- # Workaround for Ruby 2.2 "private attribute?" warning.
92
- protected
93
-
94
- attr_reader :types, :values, :additional_types, :delegate_hash, :default_attributes
95
-
96
- def materialize
97
- unless @materialized
98
- values.each_key { |key| self[key] }
99
- types.each_key { |key| self[key] }
100
- unless frozen?
101
- @materialized = true
102
- end
103
- end
104
- delegate_hash
105
- end
106
-
107
- private
108
-
109
- def assign_default_value(name)
110
- type = additional_types.fetch(name, types[name])
111
- value_present = true
112
- value = values.fetch(name) { value_present = false }
113
-
114
- if value_present
115
- delegate_hash[name] = Attribute.from_database(name, value, type)
116
- elsif types.key?(name)
117
- attr = default_attributes[name]
118
- if attr
119
- delegate_hash[name] = attr.dup
120
- else
121
- delegate_hash[name] = Attribute.uninitialized(name, type)
122
- end
123
- end
124
- end
125
- end
126
- end
@@ -1,41 +0,0 @@
1
- module ActiveRecord
2
- class AttributeSet
3
- # Attempts to do more intelligent YAML dumping of an
4
- # ActiveRecord::AttributeSet to reduce the size of the resulting string
5
- class YAMLEncoder # :nodoc:
6
- def initialize(default_types)
7
- @default_types = default_types
8
- end
9
-
10
- def encode(attribute_set, coder)
11
- coder["concise_attributes"] = attribute_set.each_value.map do |attr|
12
- if attr.type.equal?(default_types[attr.name])
13
- attr.with_type(nil)
14
- else
15
- attr
16
- end
17
- end
18
- end
19
-
20
- def decode(coder)
21
- if coder["attributes"]
22
- coder["attributes"]
23
- else
24
- attributes_hash = Hash[coder["concise_attributes"].map do |attr|
25
- if attr.type.nil?
26
- attr = attr.with_type(default_types[attr.name])
27
- end
28
- [attr.name, attr]
29
- end]
30
- AttributeSet.new(attributes_hash)
31
- end
32
- end
33
-
34
- # TODO Change this to private once we've dropped Ruby 2.2 support.
35
- # Workaround for Ruby 2.2 "private attribute?" warning.
36
- protected
37
-
38
- attr_reader :default_types
39
- end
40
- end
41
- end
@@ -1,10 +0,0 @@
1
- module ActiveRecord
2
- module ConnectionAdapters
3
- module PostgreSQL
4
- module OID # :nodoc:
5
- class Json < Type::Internal::AbstractJson
6
- end
7
- end
8
- end
9
- end
10
- end