activerecord 6.0.0.beta3 → 6.0.2.rc2

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 (142) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +466 -9
  3. data/README.rdoc +3 -1
  4. data/lib/active_record.rb +0 -1
  5. data/lib/active_record/association_relation.rb +15 -6
  6. data/lib/active_record/associations.rb +4 -3
  7. data/lib/active_record/associations/association.rb +10 -2
  8. data/lib/active_record/associations/builder/association.rb +14 -18
  9. data/lib/active_record/associations/builder/belongs_to.rb +5 -2
  10. data/lib/active_record/associations/builder/collection_association.rb +5 -15
  11. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -1
  12. data/lib/active_record/associations/builder/has_many.rb +2 -0
  13. data/lib/active_record/associations/builder/has_one.rb +35 -1
  14. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  15. data/lib/active_record/associations/collection_association.rb +6 -2
  16. data/lib/active_record/associations/collection_proxy.rb +2 -2
  17. data/lib/active_record/associations/has_many_through_association.rb +4 -11
  18. data/lib/active_record/associations/join_dependency.rb +14 -9
  19. data/lib/active_record/associations/join_dependency/join_association.rb +12 -3
  20. data/lib/active_record/associations/preloader.rb +13 -8
  21. data/lib/active_record/associations/preloader/association.rb +34 -30
  22. data/lib/active_record/associations/preloader/through_association.rb +48 -28
  23. data/lib/active_record/attribute_methods.rb +3 -53
  24. data/lib/active_record/attribute_methods/before_type_cast.rb +4 -1
  25. data/lib/active_record/attribute_methods/dirty.rb +47 -14
  26. data/lib/active_record/attribute_methods/primary_key.rb +7 -15
  27. data/lib/active_record/attribute_methods/query.rb +2 -3
  28. data/lib/active_record/attribute_methods/read.rb +3 -9
  29. data/lib/active_record/attribute_methods/write.rb +6 -12
  30. data/lib/active_record/attributes.rb +13 -0
  31. data/lib/active_record/autosave_association.rb +21 -7
  32. data/lib/active_record/base.rb +0 -1
  33. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +109 -11
  34. data/lib/active_record/connection_adapters/abstract/database_limits.rb +8 -4
  35. data/lib/active_record/connection_adapters/abstract/database_statements.rb +88 -61
  36. data/lib/active_record/connection_adapters/abstract/query_cache.rb +6 -4
  37. data/lib/active_record/connection_adapters/abstract/quoting.rb +63 -6
  38. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +4 -7
  39. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +1 -1
  40. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +79 -22
  41. data/lib/active_record/connection_adapters/abstract/transaction.rb +12 -4
  42. data/lib/active_record/connection_adapters/abstract_adapter.rb +111 -33
  43. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +78 -73
  44. data/lib/active_record/connection_adapters/column.rb +17 -13
  45. data/lib/active_record/connection_adapters/connection_specification.rb +2 -2
  46. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +2 -2
  47. data/lib/active_record/connection_adapters/mysql/database_statements.rb +48 -8
  48. data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -7
  49. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +7 -5
  50. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +9 -6
  51. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +6 -10
  52. data/lib/active_record/connection_adapters/mysql2_adapter.rb +18 -5
  53. data/lib/active_record/connection_adapters/postgresql/column.rb +17 -30
  54. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +8 -2
  55. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
  56. data/lib/active_record/connection_adapters/postgresql/quoting.rb +39 -2
  57. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +34 -38
  58. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +23 -27
  59. data/lib/active_record/connection_adapters/postgresql_adapter.rb +67 -26
  60. data/lib/active_record/connection_adapters/schema_cache.rb +32 -14
  61. data/lib/active_record/connection_adapters/sql_type_metadata.rb +11 -8
  62. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +120 -0
  63. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +38 -2
  64. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +2 -2
  65. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +66 -114
  66. data/lib/active_record/connection_handling.rb +31 -13
  67. data/lib/active_record/core.rb +23 -24
  68. data/lib/active_record/database_configurations.rb +73 -44
  69. data/lib/active_record/database_configurations/hash_config.rb +11 -11
  70. data/lib/active_record/database_configurations/url_config.rb +12 -12
  71. data/lib/active_record/dynamic_matchers.rb +1 -1
  72. data/lib/active_record/enum.rb +15 -0
  73. data/lib/active_record/errors.rb +1 -1
  74. data/lib/active_record/fixtures.rb +11 -6
  75. data/lib/active_record/gem_version.rb +2 -2
  76. data/lib/active_record/insert_all.rb +179 -0
  77. data/lib/active_record/integration.rb +13 -1
  78. data/lib/active_record/internal_metadata.rb +5 -1
  79. data/lib/active_record/locking/optimistic.rb +3 -4
  80. data/lib/active_record/log_subscriber.rb +1 -1
  81. data/lib/active_record/middleware/database_selector.rb +3 -3
  82. data/lib/active_record/middleware/database_selector/resolver.rb +4 -6
  83. data/lib/active_record/migration.rb +62 -44
  84. data/lib/active_record/migration/command_recorder.rb +28 -14
  85. data/lib/active_record/migration/compatibility.rb +10 -0
  86. data/lib/active_record/model_schema.rb +3 -0
  87. data/lib/active_record/persistence.rb +206 -13
  88. data/lib/active_record/querying.rb +17 -12
  89. data/lib/active_record/railtie.rb +0 -1
  90. data/lib/active_record/railties/databases.rake +127 -25
  91. data/lib/active_record/reflection.rb +3 -3
  92. data/lib/active_record/relation.rb +99 -20
  93. data/lib/active_record/relation/calculations.rb +38 -40
  94. data/lib/active_record/relation/delegation.rb +22 -30
  95. data/lib/active_record/relation/finder_methods.rb +17 -12
  96. data/lib/active_record/relation/merger.rb +11 -16
  97. data/lib/active_record/relation/query_methods.rb +228 -76
  98. data/lib/active_record/relation/where_clause.rb +9 -5
  99. data/lib/active_record/sanitization.rb +33 -4
  100. data/lib/active_record/schema.rb +1 -1
  101. data/lib/active_record/schema_dumper.rb +10 -1
  102. data/lib/active_record/schema_migration.rb +1 -1
  103. data/lib/active_record/scoping/default.rb +6 -7
  104. data/lib/active_record/scoping/named.rb +3 -2
  105. data/lib/active_record/statement_cache.rb +2 -2
  106. data/lib/active_record/store.rb +48 -0
  107. data/lib/active_record/table_metadata.rb +9 -13
  108. data/lib/active_record/tasks/database_tasks.rb +109 -6
  109. data/lib/active_record/tasks/mysql_database_tasks.rb +3 -1
  110. data/lib/active_record/test_databases.rb +1 -16
  111. data/lib/active_record/test_fixtures.rb +1 -0
  112. data/lib/active_record/timestamp.rb +26 -16
  113. data/lib/active_record/touch_later.rb +4 -2
  114. data/lib/active_record/transactions.rb +56 -46
  115. data/lib/active_record/type_caster/connection.rb +16 -10
  116. data/lib/active_record/validations.rb +1 -0
  117. data/lib/active_record/validations/uniqueness.rb +3 -5
  118. data/lib/arel.rb +12 -5
  119. data/lib/arel/insert_manager.rb +3 -3
  120. data/lib/arel/nodes.rb +2 -1
  121. data/lib/arel/nodes/comment.rb +29 -0
  122. data/lib/arel/nodes/select_core.rb +16 -12
  123. data/lib/arel/nodes/unary.rb +1 -0
  124. data/lib/arel/nodes/values_list.rb +2 -17
  125. data/lib/arel/select_manager.rb +10 -10
  126. data/lib/arel/visitors/depth_first.rb +7 -2
  127. data/lib/arel/visitors/dot.rb +7 -2
  128. data/lib/arel/visitors/ibm_db.rb +13 -0
  129. data/lib/arel/visitors/informix.rb +6 -0
  130. data/lib/arel/visitors/mssql.rb +15 -1
  131. data/lib/arel/visitors/oracle12.rb +4 -5
  132. data/lib/arel/visitors/postgresql.rb +4 -10
  133. data/lib/arel/visitors/to_sql.rb +107 -131
  134. data/lib/arel/visitors/visitor.rb +9 -5
  135. data/lib/rails/generators/active_record/migration/migration_generator.rb +1 -1
  136. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +1 -1
  137. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +4 -2
  138. data/lib/rails/generators/active_record/model/model_generator.rb +1 -1
  139. data/lib/rails/generators/active_record/model/templates/model.rb.tt +10 -1
  140. metadata +16 -12
  141. data/lib/active_record/collection_cache_key.rb +0 -53
  142. data/lib/arel/nodes/values.rb +0 -16
@@ -13,6 +13,8 @@ columns. Although these mappings can be defined explicitly, it's recommended
13
13
  to follow naming conventions, especially when getting started with the
14
14
  library.
15
15
 
16
+ You can read more about Active Record in the {Active Record Basics}[https://edgeguides.rubyonrails.org/active_record_basics.html] guide.
17
+
16
18
  A short rundown of some of the major features:
17
19
 
18
20
  * Automated mapping between classes and tables, attributes and columns.
@@ -206,7 +208,7 @@ Active Record is released under the MIT license:
206
208
 
207
209
  API documentation is at:
208
210
 
209
- * http://api.rubyonrails.org
211
+ * https://api.rubyonrails.org
210
212
 
211
213
  Bug reports for the Ruby on Rails project can be filed here:
212
214
 
@@ -55,7 +55,6 @@ module ActiveRecord
55
55
  autoload :Persistence
56
56
  autoload :QueryCache
57
57
  autoload :Querying
58
- autoload :CollectionCacheKey
59
58
  autoload :ReadonlyAttributes
60
59
  autoload :RecordInvalid, "active_record/validations"
61
60
  autoload :Reflection
@@ -15,17 +15,26 @@ module ActiveRecord
15
15
  other == records
16
16
  end
17
17
 
18
- def build(*args, &block)
19
- scoping { @association.build(*args, &block) }
18
+ def build(attributes = nil, &block)
19
+ block = _deprecated_scope_block("new", &block)
20
+ @association.scoping(self) do
21
+ @association.build(attributes, &block)
22
+ end
20
23
  end
21
24
  alias new build
22
25
 
23
- def create(*args, &block)
24
- scoping { @association.create(*args, &block) }
26
+ def create(attributes = nil, &block)
27
+ block = _deprecated_scope_block("create", &block)
28
+ @association.scoping(self) do
29
+ @association.create(attributes, &block)
30
+ end
25
31
  end
26
32
 
27
- def create!(*args, &block)
28
- scoping { @association.create!(*args, &block) }
33
+ def create!(attributes = nil, &block)
34
+ block = _deprecated_scope_block("create!", &block)
35
+ @association.scoping(self) do
36
+ @association.create!(attributes, &block)
37
+ end
29
38
  end
30
39
 
31
40
  private
@@ -92,7 +92,7 @@ module ActiveRecord
92
92
  through_reflection = reflection.through_reflection
93
93
  source_reflection_names = reflection.source_reflection_names
94
94
  source_associations = reflection.through_reflection.klass._reflections.keys
95
- super("Could not find the source association(s) #{source_reflection_names.collect(&:inspect).to_sentence(two_words_connector: ' or ', last_word_connector: ', or ', locale: :en)} in model #{through_reflection.klass}. Try 'has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}, :source => <name>'. Is it one of #{source_associations.to_sentence(two_words_connector: ' or ', last_word_connector: ', or ', locale: :en)}?")
95
+ super("Could not find the source association(s) #{source_reflection_names.collect(&:inspect).to_sentence(two_words_connector: ' or ', last_word_connector: ', or ')} in model #{through_reflection.klass}. Try 'has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}, :source => <name>'. Is it one of #{source_associations.to_sentence(two_words_connector: ' or ', last_word_connector: ', or ')}?")
96
96
  else
97
97
  super("Could not find the source association(s).")
98
98
  end
@@ -703,8 +703,9 @@ module ActiveRecord
703
703
  # #belongs_to associations.
704
704
  #
705
705
  # Extra options on the associations, as defined in the
706
- # <tt>AssociationReflection::INVALID_AUTOMATIC_INVERSE_OPTIONS</tt> constant, will
707
- # also prevent the association's inverse from being found automatically.
706
+ # <tt>AssociationReflection::INVALID_AUTOMATIC_INVERSE_OPTIONS</tt>
707
+ # constant, or a custom scope, will also prevent the association's inverse
708
+ # from being found automatically.
708
709
  #
709
710
  # The automatic guessing of the inverse association uses a heuristic based
710
711
  # on the name of the class, so it may not work for all associations,
@@ -43,6 +43,7 @@ module ActiveRecord
43
43
  reflection.check_validity!
44
44
 
45
45
  @owner, @reflection = owner, reflection
46
+ @_scope = nil
46
47
 
47
48
  reset
48
49
  reset_scope
@@ -95,7 +96,7 @@ module ActiveRecord
95
96
  end
96
97
 
97
98
  def scope
98
- target_scope.merge!(association_scope)
99
+ @_scope&.spawn || target_scope.merge!(association_scope)
99
100
  end
100
101
 
101
102
  def reset_scope
@@ -195,6 +196,13 @@ module ActiveRecord
195
196
  _create_record(attributes, true, &block)
196
197
  end
197
198
 
199
+ def scoping(relation, &block)
200
+ @_scope = relation
201
+ relation.scoping(&block)
202
+ ensure
203
+ @_scope = nil
204
+ end
205
+
198
206
  private
199
207
  def find_target
200
208
  scope = self.scope
@@ -225,7 +233,7 @@ module ActiveRecord
225
233
  # Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
226
234
  # through association's scope)
227
235
  def target_scope
228
- AssociationRelation.create(klass, self).merge!(klass.all)
236
+ AssociationRelation.create(klass, self).merge!(klass.scope_for_association)
229
237
  end
230
238
 
231
239
  def scope_for_create
@@ -27,40 +27,32 @@ module ActiveRecord::Associations::Builder # :nodoc:
27
27
  "Please choose a different association name."
28
28
  end
29
29
 
30
- extension = define_extensions model, name, &block
31
- reflection = create_reflection model, name, scope, options, extension
30
+ reflection = create_reflection(model, name, scope, options, &block)
32
31
  define_accessors model, reflection
33
32
  define_callbacks model, reflection
34
33
  define_validations model, reflection
35
34
  reflection
36
35
  end
37
36
 
38
- def self.create_reflection(model, name, scope, options, extension = nil)
37
+ def self.create_reflection(model, name, scope, options, &block)
39
38
  raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)
40
39
 
41
40
  validate_options(options)
42
41
 
43
- scope = build_scope(scope, extension)
42
+ extension = define_extensions(model, name, &block)
43
+ options[:extend] = [*options[:extend], extension] if extension
44
+
45
+ scope = build_scope(scope)
44
46
 
45
47
  ActiveRecord::Reflection.create(macro, name, scope, options, model)
46
48
  end
47
49
 
48
- def self.build_scope(scope, extension)
49
- new_scope = scope
50
-
50
+ def self.build_scope(scope)
51
51
  if scope && scope.arity == 0
52
- new_scope = proc { instance_exec(&scope) }
53
- end
54
-
55
- if extension
56
- new_scope = wrap_scope new_scope, extension
52
+ proc { instance_exec(&scope) }
53
+ else
54
+ scope
57
55
  end
58
-
59
- new_scope
60
- end
61
-
62
- def self.wrap_scope(scope, extension)
63
- scope
64
56
  end
65
57
 
66
58
  def self.macro
@@ -136,5 +128,9 @@ module ActiveRecord::Associations::Builder # :nodoc:
136
128
  name = reflection.name
137
129
  model.before_destroy lambda { |o| o.association(name).handle_dependency }
138
130
  end
131
+
132
+ private_class_method :build_scope, :macro, :valid_options, :validate_options, :define_extensions,
133
+ :define_callbacks, :define_accessors, :define_readers, :define_writers, :define_validations,
134
+ :valid_dependent_options, :check_dependent_options, :add_destroy_callbacks
139
135
  end
140
136
  end
@@ -74,11 +74,11 @@ module ActiveRecord::Associations::Builder # :nodoc:
74
74
 
75
75
  def self.add_touch_callbacks(model, reflection)
76
76
  foreign_key = reflection.foreign_key
77
- n = reflection.name
77
+ name = reflection.name
78
78
  touch = reflection.options[:touch]
79
79
 
80
80
  callback = lambda { |changes_method| lambda { |record|
81
- BelongsTo.touch_record(record, record.send(changes_method), foreign_key, n, touch, belongs_to_touch_method)
81
+ BelongsTo.touch_record(record, record.send(changes_method), foreign_key, name, touch, belongs_to_touch_method)
82
82
  }}
83
83
 
84
84
  if reflection.counter_cache_column
@@ -123,5 +123,8 @@ module ActiveRecord::Associations::Builder # :nodoc:
123
123
  model.validates_presence_of reflection.name, message: :required
124
124
  end
125
125
  end
126
+
127
+ private_class_method :macro, :valid_options, :valid_dependent_options, :define_callbacks, :define_validations,
128
+ :add_counter_cache_callbacks, :add_touch_callbacks, :add_default_callbacks, :add_destroy_callbacks
126
129
  end
127
130
  end
@@ -20,11 +20,11 @@ module ActiveRecord::Associations::Builder # :nodoc:
20
20
  }
21
21
  end
22
22
 
23
- def self.define_extensions(model, name)
23
+ def self.define_extensions(model, name, &block)
24
24
  if block_given?
25
- extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
26
- extension = Module.new(&Proc.new)
27
- model.module_parent.const_set(extension_module_name, extension)
25
+ extension_module_name = "#{name.to_s.camelize}AssociationExtension"
26
+ extension = Module.new(&block)
27
+ model.const_set(extension_module_name, extension)
28
28
  end
29
29
  end
30
30
 
@@ -67,16 +67,6 @@ module ActiveRecord::Associations::Builder # :nodoc:
67
67
  CODE
68
68
  end
69
69
 
70
- def self.wrap_scope(scope, mod)
71
- if scope
72
- if scope.arity > 0
73
- proc { |owner| instance_exec(owner, &scope).extending(mod) }
74
- else
75
- proc { instance_exec(&scope).extending(mod) }
76
- end
77
- else
78
- proc { extending(mod) }
79
- end
80
- end
70
+ private_class_method :valid_options, :define_callback, :define_extensions, :define_readers, :define_writers
81
71
  end
82
72
  end
@@ -63,7 +63,7 @@ module ActiveRecord::Associations::Builder # :nodoc:
63
63
 
64
64
  def middle_reflection(join_model)
65
65
  middle_name = [lhs_model.name.downcase.pluralize,
66
- association_name].join("_").gsub("::", "_").to_sym
66
+ association_name.to_s].sort.join("_").gsub("::", "_").to_sym
67
67
  middle_options = middle_options join_model
68
68
 
69
69
  HasMany.create_reflection(lhs_model,
@@ -13,5 +13,7 @@ module ActiveRecord::Associations::Builder # :nodoc:
13
13
  def self.valid_dependent_options
14
14
  [:destroy, :delete_all, :nullify, :restrict_with_error, :restrict_with_exception]
15
15
  end
16
+
17
+ private_class_method :macro, :valid_options, :valid_dependent_options
16
18
  end
17
19
  end
@@ -7,7 +7,7 @@ module ActiveRecord::Associations::Builder # :nodoc:
7
7
  end
8
8
 
9
9
  def self.valid_options(options)
10
- valid = super + [:as]
10
+ valid = super + [:as, :touch]
11
11
  valid += [:through, :source, :source_type] if options[:through]
12
12
  valid
13
13
  end
@@ -16,6 +16,11 @@ module ActiveRecord::Associations::Builder # :nodoc:
16
16
  [:destroy, :delete, :nullify, :restrict_with_error, :restrict_with_exception]
17
17
  end
18
18
 
19
+ def self.define_callbacks(model, reflection)
20
+ super
21
+ add_touch_callbacks(model, reflection) if reflection.options[:touch]
22
+ end
23
+
19
24
  def self.add_destroy_callbacks(model, reflection)
20
25
  super unless reflection.options[:through]
21
26
  end
@@ -26,5 +31,34 @@ module ActiveRecord::Associations::Builder # :nodoc:
26
31
  model.validates_presence_of reflection.name, message: :required
27
32
  end
28
33
  end
34
+
35
+ def self.touch_record(o, name, touch)
36
+ record = o.send name
37
+
38
+ return unless record && record.persisted?
39
+
40
+ if touch != true
41
+ record.touch(touch)
42
+ else
43
+ record.touch
44
+ end
45
+ end
46
+
47
+ def self.add_touch_callbacks(model, reflection)
48
+ name = reflection.name
49
+ touch = reflection.options[:touch]
50
+
51
+ callback = lambda { |record|
52
+ HasOne.touch_record(record, name, touch)
53
+ }
54
+
55
+ model.after_create callback, if: :saved_changes?
56
+ model.after_update callback, if: :saved_changes?
57
+ model.after_destroy callback
58
+ model.after_touch callback
59
+ end
60
+
61
+ private_class_method :macro, :valid_options, :valid_dependent_options, :add_destroy_callbacks,
62
+ :define_callbacks, :define_validations, :add_touch_callbacks
29
63
  end
30
64
  end
@@ -38,5 +38,7 @@ module ActiveRecord::Associations::Builder # :nodoc:
38
38
  end
39
39
  CODE
40
40
  end
41
+
42
+ private_class_method :valid_options, :define_accessors, :define_constructors
41
43
  end
42
44
  end
@@ -378,7 +378,9 @@ module ActiveRecord
378
378
  end
379
379
 
380
380
  def remove_records(existing_records, records, method)
381
- records.each { |record| callback(:before_remove, record) }
381
+ catch(:abort) do
382
+ records.each { |record| callback(:before_remove, record) }
383
+ end || return
382
384
 
383
385
  delete_records(existing_records, method) if existing_records.any?
384
386
  @target -= records
@@ -434,7 +436,9 @@ module ActiveRecord
434
436
  end
435
437
 
436
438
  def replace_on_target(record, index, skip_callbacks)
437
- callback(:before_add, record) unless skip_callbacks
439
+ catch(:abort) do
440
+ callback(:before_add, record)
441
+ end || return unless skip_callbacks
438
442
 
439
443
  set_inverse_instance(record)
440
444
 
@@ -1002,7 +1002,7 @@ module ActiveRecord
1002
1002
  end
1003
1003
 
1004
1004
  # Adds one or more +records+ to the collection by setting their foreign keys
1005
- # to the association's primary key. Since +<<+ flattens its argument list and
1005
+ # to the association's primary key. Since <tt><<</tt> flattens its argument list and
1006
1006
  # inserts each record, +push+ and +concat+ behave identically. Returns +self+
1007
1007
  # so several appends may be chained together.
1008
1008
  #
@@ -1029,7 +1029,7 @@ module ActiveRecord
1029
1029
  alias_method :append, :<<
1030
1030
  alias_method :concat, :<<
1031
1031
 
1032
- def prepend(*args)
1032
+ def prepend(*args) # :nodoc:
1033
1033
  raise NoMethodError, "prepend on association is not defined. Please use <<, push or append"
1034
1034
  end
1035
1035
 
@@ -57,21 +57,14 @@ module ActiveRecord
57
57
  @through_records[record.object_id] ||= begin
58
58
  ensure_mutable
59
59
 
60
- through_record = through_association.build(*options_for_through_record)
61
- through_record.send("#{source_reflection.name}=", record)
60
+ attributes = through_scope_attributes
61
+ attributes[source_reflection.name] = record
62
+ attributes[source_reflection.foreign_type] = options[:source_type] if options[:source_type]
62
63
 
63
- if options[:source_type]
64
- through_record.send("#{source_reflection.foreign_type}=", options[:source_type])
65
- end
66
-
67
- through_record
64
+ through_association.build(attributes)
68
65
  end
69
66
  end
70
67
 
71
- def options_for_through_record
72
- [through_scope_attributes]
73
- end
74
-
75
68
  def through_scope_attributes
76
69
  scope.where_values_hash(through_association.reflection.name.to_s).
77
70
  except!(through_association.reflection.foreign_key,
@@ -64,16 +64,21 @@ module ActiveRecord
64
64
  end
65
65
  end
66
66
 
67
- def initialize(base, table, associations)
67
+ def initialize(base, table, associations, join_type)
68
68
  tree = self.class.make_tree associations
69
69
  @join_root = JoinBase.new(base, table, build(tree, base))
70
+ @join_type = join_type
71
+ end
72
+
73
+ def base_klass
74
+ join_root.base_klass
70
75
  end
71
76
 
72
77
  def reflections
73
78
  join_root.drop(1).map!(&:reflection)
74
79
  end
75
80
 
76
- def join_constraints(joins_to_add, join_type, alias_tracker)
81
+ def join_constraints(joins_to_add, alias_tracker)
77
82
  @alias_tracker = alias_tracker
78
83
 
79
84
  construct_tables!(join_root)
@@ -82,9 +87,9 @@ module ActiveRecord
82
87
  joins.concat joins_to_add.flat_map { |oj|
83
88
  construct_tables!(oj.join_root)
84
89
  if join_root.match? oj.join_root
85
- walk join_root, oj.join_root
90
+ walk(join_root, oj.join_root, oj.join_type)
86
91
  else
87
- make_join_constraints(oj.join_root, join_type)
92
+ make_join_constraints(oj.join_root, oj.join_type)
88
93
  end
89
94
  }
90
95
  end
@@ -125,7 +130,7 @@ module ActiveRecord
125
130
  end
126
131
 
127
132
  protected
128
- attr_reader :join_root
133
+ attr_reader :join_root, :join_type
129
134
 
130
135
  private
131
136
  attr_reader :alias_tracker
@@ -151,7 +156,7 @@ module ActiveRecord
151
156
  end
152
157
  end
153
158
 
154
- def make_constraints(parent, child, join_type = Arel::Nodes::OuterJoin)
159
+ def make_constraints(parent, child, join_type)
155
160
  foreign_table = parent.table
156
161
  foreign_klass = parent.base_klass
157
162
  joins = child.join_constraints(foreign_table, foreign_klass, join_type, alias_tracker)
@@ -173,13 +178,13 @@ module ActiveRecord
173
178
  join ? "#{name}_join" : name
174
179
  end
175
180
 
176
- def walk(left, right)
181
+ def walk(left, right, join_type)
177
182
  intersection, missing = right.children.map { |node1|
178
183
  [left.children.find { |node2| node1.match? node2 }, node1]
179
184
  }.partition(&:first)
180
185
 
181
- joins = intersection.flat_map { |l, r| r.table = l.table; walk(l, r) }
182
- joins.concat missing.flat_map { |_, n| make_constraints(left, n) }
186
+ joins = intersection.flat_map { |l, r| r.table = l.table; walk(l, r, join_type) }
187
+ joins.concat missing.flat_map { |_, n| make_constraints(left, n, join_type) }
183
188
  end
184
189
 
185
190
  def find_reflection(klass, name)