activerecord 5.1.0 → 5.2.3

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 +5 -5
  2. data/CHANGELOG.md +596 -450
  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.rb +11 -4
  8. data/lib/active_record/aggregations.rb +6 -5
  9. data/lib/active_record/association_relation.rb +7 -5
  10. data/lib/active_record/associations.rb +77 -85
  11. data/lib/active_record/associations/alias_tracker.rb +23 -32
  12. data/lib/active_record/associations/association.rb +49 -35
  13. data/lib/active_record/associations/association_scope.rb +55 -55
  14. data/lib/active_record/associations/belongs_to_association.rb +30 -11
  15. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -8
  16. data/lib/active_record/associations/builder/association.rb +4 -7
  17. data/lib/active_record/associations/builder/belongs_to.rb +21 -8
  18. data/lib/active_record/associations/builder/collection_association.rb +1 -1
  19. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
  20. data/lib/active_record/associations/builder/has_many.rb +2 -0
  21. data/lib/active_record/associations/builder/has_one.rb +2 -0
  22. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  23. data/lib/active_record/associations/collection_association.rb +66 -53
  24. data/lib/active_record/associations/collection_proxy.rb +30 -73
  25. data/lib/active_record/associations/foreign_association.rb +2 -0
  26. data/lib/active_record/associations/has_many_association.rb +13 -2
  27. data/lib/active_record/associations/has_many_through_association.rb +37 -19
  28. data/lib/active_record/associations/has_one_association.rb +14 -1
  29. data/lib/active_record/associations/has_one_through_association.rb +13 -8
  30. data/lib/active_record/associations/join_dependency.rb +52 -96
  31. data/lib/active_record/associations/join_dependency/join_association.rb +22 -75
  32. data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
  33. data/lib/active_record/associations/join_dependency/join_part.rb +9 -9
  34. data/lib/active_record/associations/preloader.rb +17 -37
  35. data/lib/active_record/associations/preloader/association.rb +53 -92
  36. data/lib/active_record/associations/preloader/through_association.rb +72 -73
  37. data/lib/active_record/associations/singular_association.rb +14 -16
  38. data/lib/active_record/associations/through_association.rb +27 -12
  39. data/lib/active_record/attribute_assignment.rb +2 -5
  40. data/lib/active_record/attribute_decorators.rb +3 -2
  41. data/lib/active_record/attribute_methods.rb +65 -24
  42. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
  43. data/lib/active_record/attribute_methods/dirty.rb +33 -216
  44. data/lib/active_record/attribute_methods/primary_key.rb +10 -13
  45. data/lib/active_record/attribute_methods/query.rb +2 -0
  46. data/lib/active_record/attribute_methods/read.rb +9 -3
  47. data/lib/active_record/attribute_methods/serialization.rb +23 -0
  48. data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
  49. data/lib/active_record/attribute_methods/write.rb +22 -19
  50. data/lib/active_record/attributes.rb +7 -6
  51. data/lib/active_record/autosave_association.rb +15 -13
  52. data/lib/active_record/base.rb +2 -0
  53. data/lib/active_record/callbacks.rb +12 -6
  54. data/lib/active_record/coders/json.rb +2 -0
  55. data/lib/active_record/coders/yaml_column.rb +2 -0
  56. data/lib/active_record/collection_cache_key.rb +15 -11
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +120 -39
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -0
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +192 -37
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +13 -2
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -25
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +15 -6
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +65 -7
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +158 -87
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +66 -21
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +86 -98
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +126 -189
  70. data/lib/active_record/connection_adapters/column.rb +4 -2
  71. data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
  72. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +13 -2
  73. data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
  74. data/lib/active_record/connection_adapters/mysql/database_statements.rb +45 -15
  75. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
  76. data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
  77. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
  78. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -10
  79. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -23
  80. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +106 -1
  81. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
  82. data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
  83. data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
  84. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -32
  85. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid.rb +3 -1
  87. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +13 -1
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
  95. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
  99. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -1
  101. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +8 -2
  104. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
  106. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
  107. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
  109. data/lib/active_record/connection_adapters/postgresql/quoting.rb +22 -1
  110. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  111. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +50 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -11
  113. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
  114. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +258 -129
  115. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
  116. data/lib/active_record/connection_adapters/postgresql/utils.rb +3 -1
  117. data/lib/active_record/connection_adapters/postgresql_adapter.rb +75 -87
  118. data/lib/active_record/connection_adapters/schema_cache.rb +4 -2
  119. data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
  120. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
  121. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +24 -1
  122. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
  124. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
  125. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +75 -1
  126. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +90 -96
  127. data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
  128. data/lib/active_record/connection_handling.rb +4 -2
  129. data/lib/active_record/core.rb +41 -61
  130. data/lib/active_record/counter_cache.rb +20 -15
  131. data/lib/active_record/define_callbacks.rb +5 -3
  132. data/lib/active_record/dynamic_matchers.rb +9 -9
  133. data/lib/active_record/enum.rb +18 -13
  134. data/lib/active_record/errors.rb +60 -15
  135. data/lib/active_record/explain.rb +3 -1
  136. data/lib/active_record/explain_registry.rb +2 -0
  137. data/lib/active_record/explain_subscriber.rb +2 -0
  138. data/lib/active_record/fixture_set/file.rb +2 -0
  139. data/lib/active_record/fixtures.rb +67 -60
  140. data/lib/active_record/gem_version.rb +4 -2
  141. data/lib/active_record/inheritance.rb +49 -19
  142. data/lib/active_record/integration.rb +58 -19
  143. data/lib/active_record/internal_metadata.rb +2 -0
  144. data/lib/active_record/legacy_yaml_adapter.rb +3 -1
  145. data/lib/active_record/locking/optimistic.rb +30 -42
  146. data/lib/active_record/locking/pessimistic.rb +10 -7
  147. data/lib/active_record/log_subscriber.rb +46 -4
  148. data/lib/active_record/migration.rb +189 -139
  149. data/lib/active_record/migration/command_recorder.rb +11 -9
  150. data/lib/active_record/migration/compatibility.rb +81 -29
  151. data/lib/active_record/migration/join_table.rb +2 -0
  152. data/lib/active_record/model_schema.rb +74 -58
  153. data/lib/active_record/nested_attributes.rb +18 -6
  154. data/lib/active_record/no_touching.rb +3 -1
  155. data/lib/active_record/null_relation.rb +2 -0
  156. data/lib/active_record/persistence.rb +199 -54
  157. data/lib/active_record/query_cache.rb +8 -10
  158. data/lib/active_record/querying.rb +5 -3
  159. data/lib/active_record/railtie.rb +62 -6
  160. data/lib/active_record/railties/console_sandbox.rb +2 -0
  161. data/lib/active_record/railties/controller_runtime.rb +2 -0
  162. data/lib/active_record/railties/databases.rake +48 -38
  163. data/lib/active_record/readonly_attributes.rb +3 -2
  164. data/lib/active_record/reflection.rb +137 -207
  165. data/lib/active_record/relation.rb +132 -207
  166. data/lib/active_record/relation/batches.rb +32 -17
  167. data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
  168. data/lib/active_record/relation/calculations.rb +66 -25
  169. data/lib/active_record/relation/delegation.rb +45 -29
  170. data/lib/active_record/relation/finder_methods.rb +76 -85
  171. data/lib/active_record/relation/from_clause.rb +2 -8
  172. data/lib/active_record/relation/merger.rb +53 -23
  173. data/lib/active_record/relation/predicate_builder.rb +60 -79
  174. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
  175. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  176. data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
  177. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
  178. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  179. data/lib/active_record/relation/predicate_builder/range_handler.rb +26 -9
  180. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  181. data/lib/active_record/relation/query_attribute.rb +28 -2
  182. data/lib/active_record/relation/query_methods.rb +135 -103
  183. data/lib/active_record/relation/record_fetch_warning.rb +2 -0
  184. data/lib/active_record/relation/spawn_methods.rb +4 -2
  185. data/lib/active_record/relation/where_clause.rb +65 -67
  186. data/lib/active_record/relation/where_clause_factory.rb +5 -48
  187. data/lib/active_record/result.rb +2 -0
  188. data/lib/active_record/runtime_registry.rb +2 -0
  189. data/lib/active_record/sanitization.rb +129 -121
  190. data/lib/active_record/schema.rb +4 -2
  191. data/lib/active_record/schema_dumper.rb +36 -26
  192. data/lib/active_record/schema_migration.rb +2 -0
  193. data/lib/active_record/scoping.rb +12 -10
  194. data/lib/active_record/scoping/default.rb +10 -7
  195. data/lib/active_record/scoping/named.rb +40 -12
  196. data/lib/active_record/secure_token.rb +2 -0
  197. data/lib/active_record/serialization.rb +2 -0
  198. data/lib/active_record/statement_cache.rb +22 -12
  199. data/lib/active_record/store.rb +3 -1
  200. data/lib/active_record/suppressor.rb +2 -0
  201. data/lib/active_record/table_metadata.rb +12 -3
  202. data/lib/active_record/tasks/database_tasks.rb +38 -26
  203. data/lib/active_record/tasks/mysql_database_tasks.rb +11 -50
  204. data/lib/active_record/tasks/postgresql_database_tasks.rb +11 -3
  205. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
  206. data/lib/active_record/timestamp.rb +13 -6
  207. data/lib/active_record/touch_later.rb +2 -0
  208. data/lib/active_record/transactions.rb +32 -27
  209. data/lib/active_record/translation.rb +2 -0
  210. data/lib/active_record/type.rb +4 -1
  211. data/lib/active_record/type/adapter_specific_registry.rb +2 -0
  212. data/lib/active_record/type/date.rb +2 -0
  213. data/lib/active_record/type/date_time.rb +2 -0
  214. data/lib/active_record/type/decimal_without_scale.rb +2 -0
  215. data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
  216. data/lib/active_record/type/internal/timezone.rb +2 -0
  217. data/lib/active_record/type/json.rb +30 -0
  218. data/lib/active_record/type/serialized.rb +6 -0
  219. data/lib/active_record/type/text.rb +2 -0
  220. data/lib/active_record/type/time.rb +2 -0
  221. data/lib/active_record/type/type_map.rb +2 -0
  222. data/lib/active_record/type/unsigned_integer.rb +2 -0
  223. data/lib/active_record/type_caster.rb +2 -0
  224. data/lib/active_record/type_caster/connection.rb +2 -0
  225. data/lib/active_record/type_caster/map.rb +3 -1
  226. data/lib/active_record/validations.rb +2 -0
  227. data/lib/active_record/validations/absence.rb +2 -0
  228. data/lib/active_record/validations/associated.rb +2 -0
  229. data/lib/active_record/validations/length.rb +2 -0
  230. data/lib/active_record/validations/presence.rb +2 -0
  231. data/lib/active_record/validations/uniqueness.rb +36 -6
  232. data/lib/active_record/version.rb +2 -0
  233. data/lib/rails/generators/active_record.rb +3 -1
  234. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  235. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  236. data/lib/rails/generators/active_record/migration.rb +2 -0
  237. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
  238. data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
  239. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
  240. data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
  241. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
  242. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  243. metadata +24 -36
  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.rb +0 -240
  252. data/lib/active_record/attribute/user_provided_default.rb +0 -30
  253. data/lib/active_record/attribute_mutation_tracker.rb +0 -113
  254. data/lib/active_record/attribute_set.rb +0 -113
  255. data/lib/active_record/attribute_set/builder.rb +0 -124
  256. data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
  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 -33
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  class Relation
3
5
  class FromClause # :nodoc:
@@ -8,14 +10,6 @@ module ActiveRecord
8
10
  @name = name
9
11
  end
10
12
 
11
- def binds
12
- if value.is_a?(Relation)
13
- value.bound_attributes
14
- else
15
- []
16
- end
17
- end
18
-
19
13
  def merge(other)
20
14
  self
21
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/hash/keys"
2
4
 
3
5
  module ActiveRecord
@@ -21,7 +23,11 @@ module ActiveRecord
21
23
  # build a relation to merge in rather than directly merging
22
24
  # the values.
23
25
  def other
24
- other = Relation.create(relation.klass, relation.table, relation.predicate_builder)
26
+ other = Relation.create(
27
+ relation.klass,
28
+ table: relation.table,
29
+ predicate_builder: relation.predicate_builder
30
+ )
25
31
  hash.each { |k, v|
26
32
  if k == :joins
27
33
  if Hash === v
@@ -50,7 +56,7 @@ module ActiveRecord
50
56
 
51
57
  NORMAL_VALUES = Relation::VALUE_METHODS -
52
58
  Relation::CLAUSE_METHODS -
53
- [:includes, :preload, :joins, :order, :reverse_order, :lock, :create_with, :reordering] # :nodoc:
59
+ [:includes, :preload, :joins, :left_outer_joins, :order, :reverse_order, :lock, :create_with, :reordering] # :nodoc:
54
60
 
55
61
  def normal_values
56
62
  NORMAL_VALUES
@@ -77,6 +83,7 @@ module ActiveRecord
77
83
  merge_clauses
78
84
  merge_preloads
79
85
  merge_joins
86
+ merge_outer_joins
80
87
 
81
88
  relation
82
89
  end
@@ -110,41 +117,57 @@ module ActiveRecord
110
117
  if other.klass == relation.klass
111
118
  relation.joins!(*other.joins_values)
112
119
  else
113
- joins_dependency, rest = other.joins_values.partition do |join|
120
+ joins_dependency = other.joins_values.map do |join|
114
121
  case join
115
122
  when Hash, Symbol, Array
116
- true
123
+ ActiveRecord::Associations::JoinDependency.new(
124
+ other.klass, other.table, join
125
+ )
117
126
  else
118
- false
127
+ join
119
128
  end
120
129
  end
121
130
 
122
- join_dependency = ActiveRecord::Associations::JoinDependency.new(other.klass,
123
- joins_dependency,
124
- [])
125
- relation.joins! rest
131
+ relation.joins!(*joins_dependency)
132
+ end
133
+ end
134
+
135
+ def merge_outer_joins
136
+ return if other.left_outer_joins_values.blank?
137
+
138
+ if other.klass == relation.klass
139
+ relation.left_outer_joins!(*other.left_outer_joins_values)
140
+ else
141
+ joins_dependency = other.left_outer_joins_values.map do |join|
142
+ case join
143
+ when Hash, Symbol, Array
144
+ ActiveRecord::Associations::JoinDependency.new(
145
+ other.klass, other.table, join
146
+ )
147
+ else
148
+ join
149
+ end
150
+ end
126
151
 
127
- @relation = relation.joins join_dependency
152
+ relation.left_outer_joins!(*joins_dependency)
128
153
  end
129
154
  end
130
155
 
131
156
  def merge_multi_values
132
157
  if other.reordering_value
133
158
  # override any order specified in the original relation
134
- relation.reorder! other.order_values
135
- elsif other.order_values
159
+ relation.reorder!(*other.order_values)
160
+ elsif other.order_values.any?
136
161
  # merge in order_values from relation
137
- relation.order! other.order_values
162
+ relation.order!(*other.order_values)
138
163
  end
139
164
 
140
- relation.extend(*other.extending_values) unless other.extending_values.blank?
165
+ extensions = other.extensions - relation.extensions
166
+ relation.extending!(*extensions) if extensions.any?
141
167
  end
142
168
 
143
169
  def merge_single_values
144
- if relation.from_clause.empty?
145
- relation.from_clause = other.from_clause
146
- end
147
- relation.lock_value ||= other.lock_value
170
+ relation.lock_value ||= other.lock_value if other.lock_value
148
171
 
149
172
  unless other.create_with_value.blank?
150
173
  relation.create_with_value = (relation.create_with_value || {}).merge(other.create_with_value)
@@ -152,11 +175,18 @@ module ActiveRecord
152
175
  end
153
176
 
154
177
  def merge_clauses
155
- CLAUSE_METHODS.each do |method|
156
- clause = relation.get_value(method)
157
- other_clause = other.get_value(method)
158
- relation.set_value(method, clause.merge(other_clause))
159
- end
178
+ relation.from_clause = other.from_clause if replace_from_clause?
179
+
180
+ where_clause = relation.where_clause.merge(other.where_clause)
181
+ relation.where_clause = where_clause unless where_clause.empty?
182
+
183
+ having_clause = relation.having_clause.merge(other.having_clause)
184
+ relation.having_clause = having_clause unless having_clause.empty?
185
+ end
186
+
187
+ def replace_from_clause?
188
+ relation.from_clause.empty? && !other.from_clause.empty? &&
189
+ relation.klass.base_class == other.klass.base_class
160
190
  end
161
191
  end
162
192
  end
@@ -1,27 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  class PredicateBuilder # :nodoc:
3
- require "active_record/relation/predicate_builder/array_handler"
4
- require "active_record/relation/predicate_builder/association_query_handler"
5
- require "active_record/relation/predicate_builder/base_handler"
6
- require "active_record/relation/predicate_builder/basic_object_handler"
7
- require "active_record/relation/predicate_builder/polymorphic_array_handler"
8
- require "active_record/relation/predicate_builder/range_handler"
9
- require "active_record/relation/predicate_builder/relation_handler"
10
-
11
5
  delegate :resolve_column_aliases, to: :table
12
6
 
13
7
  def initialize(table)
14
8
  @table = table
15
9
  @handlers = []
16
10
 
17
- register_handler(BasicObject, BasicObjectHandler.new)
11
+ register_handler(BasicObject, BasicObjectHandler.new(self))
18
12
  register_handler(Base, BaseHandler.new(self))
19
- register_handler(Range, RangeHandler.new)
20
- register_handler(RangeHandler::RangeWithBinds, RangeHandler.new)
13
+ register_handler(Range, RangeHandler.new(self))
21
14
  register_handler(Relation, RelationHandler.new)
22
15
  register_handler(Array, ArrayHandler.new(self))
23
- register_handler(AssociationQueryValue, AssociationQueryHandler.new(self))
24
- register_handler(PolymorphicArrayValue, PolymorphicArrayHandler.new(self))
16
+ register_handler(Set, ArrayHandler.new(self))
25
17
  end
26
18
 
27
19
  def build_from_hash(attributes)
@@ -29,11 +21,6 @@ module ActiveRecord
29
21
  expand_from_hash(attributes)
30
22
  end
31
23
 
32
- def create_binds(attributes)
33
- attributes = convert_dot_notation_to_hash(attributes)
34
- create_binds_for_hash(attributes)
35
- end
36
-
37
24
  def self.references(attributes)
38
25
  attributes.map do |key, value|
39
26
  if value.is_a?(Hash)
@@ -61,11 +48,19 @@ module ActiveRecord
61
48
  end
62
49
 
63
50
  def build(attribute, value)
64
- handler_for(value).call(attribute, value)
51
+ if table.type(attribute.name).force_equality?(value)
52
+ bind = build_bind_attribute(attribute.name, value)
53
+ attribute.eq(bind)
54
+ else
55
+ handler_for(value).call(attribute, value)
56
+ end
57
+ end
58
+
59
+ def build_bind_attribute(column_name, value)
60
+ attr = Relation::QueryAttribute.new(column_name.to_s, value, table.type(column_name))
61
+ Arel::Nodes::BindParam.new(attr)
65
62
  end
66
63
 
67
- # TODO Change this to private once we've dropped Ruby 2.2 support.
68
- # Workaround for Ruby 2.2 "private attribute?" warning.
69
64
  protected
70
65
 
71
66
  attr_reader :table
@@ -76,56 +71,47 @@ module ActiveRecord
76
71
  attributes.flat_map do |key, value|
77
72
  if value.is_a?(Hash) && !table.has_column?(key)
78
73
  associated_predicate_builder(key).expand_from_hash(value)
79
- else
80
- build(table.arel_attribute(key), value)
81
- end
82
- end
83
- end
74
+ elsif table.associated_with?(key)
75
+ # Find the foreign key when using queries such as:
76
+ # Post.where(author: author)
77
+ #
78
+ # For polymorphic relationships, find the foreign key and type:
79
+ # PriceEstimate.where(estimate_of: treasure)
80
+ associated_table = table.associated_table(key)
81
+ if associated_table.polymorphic_association?
82
+ case value.is_a?(Array) ? value.first : value
83
+ when Base, Relation
84
+ value = [value] unless value.is_a?(Array)
85
+ klass = PolymorphicArrayValue
86
+ end
87
+ end
84
88
 
85
- def create_binds_for_hash(attributes)
86
- result = attributes.dup
87
- binds = []
88
-
89
- attributes.each do |column_name, value|
90
- case
91
- when value.is_a?(Hash) && !table.has_column?(column_name)
92
- attrs, bvs = associated_predicate_builder(column_name).create_binds_for_hash(value)
93
- result[column_name] = attrs
94
- binds += bvs
95
- next
96
- when value.is_a?(Relation)
97
- binds += value.bound_attributes
98
- when value.is_a?(Range) && !table.type(column_name).respond_to?(:subtype)
99
- first = value.begin
100
- last = value.end
101
- unless first.respond_to?(:infinite?) && first.infinite?
102
- binds << build_bind_param(column_name, first)
103
- first = Arel::Nodes::BindParam.new
89
+ klass ||= AssociationQueryValue
90
+ queries = klass.new(associated_table, value).queries.map do |query|
91
+ expand_from_hash(query).reduce(&:and)
104
92
  end
105
- unless last.respond_to?(:infinite?) && last.infinite?
106
- binds << build_bind_param(column_name, last)
107
- last = Arel::Nodes::BindParam.new
93
+ queries.reduce(&:or)
94
+ elsif table.aggregated_with?(key)
95
+ mapping = table.reflect_on_aggregation(key).mapping
96
+ values = value.nil? ? [nil] : Array.wrap(value)
97
+ if mapping.length == 1 || values.empty?
98
+ column_name, aggr_attr = mapping.first
99
+ values = values.map do |object|
100
+ object.respond_to?(aggr_attr) ? object.public_send(aggr_attr) : object
101
+ end
102
+ build(table.arel_attribute(column_name), values)
103
+ else
104
+ queries = values.map do |object|
105
+ mapping.map do |field_attr, aggregate_attr|
106
+ build(table.arel_attribute(field_attr), object.try!(aggregate_attr))
107
+ end.reduce(&:and)
108
+ end
109
+ queries.reduce(&:or)
108
110
  end
109
-
110
- result[column_name] = RangeHandler::RangeWithBinds.new(first, last, value.exclude_end?)
111
111
  else
112
- if can_be_bound?(column_name, value)
113
- result[column_name] = Arel::Nodes::BindParam.new
114
- binds << build_bind_param(column_name, value)
115
- end
116
- end
117
-
118
- # Find the foreign key when using queries such as:
119
- # Post.where(author: author)
120
- #
121
- # For polymorphic relationships, find the foreign key and type:
122
- # PriceEstimate.where(estimate_of: treasure)
123
- if table.associated_with?(column_name)
124
- result[column_name] = AssociationQueryHandler.value_for(table, column_name, value)
112
+ build(table.arel_attribute(key), value)
125
113
  end
126
114
  end
127
-
128
- [result, binds]
129
115
  end
130
116
 
131
117
  private
@@ -153,19 +139,14 @@ module ActiveRecord
153
139
  def handler_for(object)
154
140
  @handlers.detect { |klass, _| klass === object }.last
155
141
  end
156
-
157
- def can_be_bound?(column_name, value)
158
- return if table.associated_with?(column_name)
159
- case value
160
- when Array, Range
161
- table.type(column_name).respond_to?(:subtype)
162
- else
163
- !value.nil? && handler_for(value).is_a?(BasicObjectHandler)
164
- end
165
- end
166
-
167
- def build_bind_param(column_name, value)
168
- Relation::QueryAttribute.new(column_name.to_s, value, table.type(column_name))
169
- end
170
142
  end
171
143
  end
144
+
145
+ require "active_record/relation/predicate_builder/array_handler"
146
+ require "active_record/relation/predicate_builder/base_handler"
147
+ require "active_record/relation/predicate_builder/basic_object_handler"
148
+ require "active_record/relation/predicate_builder/range_handler"
149
+ require "active_record/relation/predicate_builder/relation_handler"
150
+
151
+ require "active_record/relation/predicate_builder/association_query_value"
152
+ require "active_record/relation/predicate_builder/polymorphic_array_value"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  class PredicateBuilder
3
5
  class ArrayHandler # :nodoc:
@@ -6,18 +8,21 @@ module ActiveRecord
6
8
  end
7
9
 
8
10
  def call(attribute, value)
11
+ return attribute.in([]) if value.empty?
12
+
9
13
  values = value.map { |x| x.is_a?(Base) ? x.id : x }
10
14
  nils, values = values.partition(&:nil?)
11
-
12
- return attribute.in([]) if values.empty? && nils.empty?
13
-
14
15
  ranges, values = values.partition { |v| v.is_a?(Range) }
15
16
 
16
17
  values_predicate =
17
18
  case values.length
18
19
  when 0 then NullPredicate
19
20
  when 1 then predicate_builder.build(attribute, values.first)
20
- else attribute.in(values)
21
+ else
22
+ values.map! do |v|
23
+ predicate_builder.build_bind_attribute(attribute.name, v)
24
+ end
25
+ values.empty? ? NullPredicate : attribute.in(values)
21
26
  end
22
27
 
23
28
  unless nils.empty?
@@ -26,11 +31,9 @@ module ActiveRecord
26
31
 
27
32
  array_predicates = ranges.map { |range| predicate_builder.build(attribute, range) }
28
33
  array_predicates.unshift(values_predicate)
29
- array_predicates.inject { |composite, predicate| composite.or(predicate) }
34
+ array_predicates.inject(&:or)
30
35
  end
31
36
 
32
- # TODO Change this to private once we've dropped Ruby 2.2 support.
33
- # Workaround for Ruby 2.2 "private attribute?" warning.
34
37
  protected
35
38
 
36
39
  attr_reader :predicate_builder
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ class PredicateBuilder
5
+ class AssociationQueryValue # :nodoc:
6
+ def initialize(associated_table, value)
7
+ @associated_table = associated_table
8
+ @value = value
9
+ end
10
+
11
+ def queries
12
+ [associated_table.association_join_foreign_key.to_s => ids]
13
+ end
14
+
15
+ # TODO Change this to private once we've dropped Ruby 2.2 support.
16
+ # Workaround for Ruby 2.2 "private attribute?" warning.
17
+ protected
18
+ attr_reader :associated_table, :value
19
+
20
+ private
21
+ def ids
22
+ case value
23
+ when Relation
24
+ value.select_values.empty? ? value.select(primary_key) : value
25
+ when Array
26
+ value.map { |v| convert_to_id(v) }
27
+ else
28
+ convert_to_id(value)
29
+ end
30
+ end
31
+
32
+ def primary_key
33
+ associated_table.association_join_primary_key
34
+ end
35
+
36
+ def convert_to_id(value)
37
+ case value
38
+ when Base
39
+ value._read_attribute(primary_key)
40
+ else
41
+ value
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  class PredicateBuilder
3
5
  class BaseHandler # :nodoc:
@@ -9,8 +11,6 @@ module ActiveRecord
9
11
  predicate_builder.build(attribute, value.id)
10
12
  end
11
13
 
12
- # TODO Change this to private once we've dropped Ruby 2.2 support.
13
- # Workaround for Ruby 2.2 "private attribute?" warning.
14
14
  protected
15
15
 
16
16
  attr_reader :predicate_builder
@@ -1,9 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  class PredicateBuilder
3
5
  class BasicObjectHandler # :nodoc:
6
+ def initialize(predicate_builder)
7
+ @predicate_builder = predicate_builder
8
+ end
9
+
4
10
  def call(attribute, value)
5
- attribute.eq(value)
11
+ bind = predicate_builder.build_bind_attribute(attribute.name, value)
12
+ attribute.eq(bind)
6
13
  end
14
+
15
+ protected
16
+
17
+ attr_reader :predicate_builder
7
18
  end
8
19
  end
9
20
  end