aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations.rb17
-rw-r--r--activerecord/lib/active_record/associations/collection_proxy.rb24
-rw-r--r--activerecord/lib/active_record/base.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/column.rb2
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
-rw-r--r--activerecord/lib/active_record/serialization.rb2
-rw-r--r--activerecord/lib/rails/generators/active_record/model/templates/migration.rb2
-rw-r--r--activerecord/test/cases/associations_test.rb12
8 files changed, 50 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 9bc44e5163..4979113b0b 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -33,7 +33,7 @@ module ActiveRecord
class HasManyThroughAssociationPointlessSourceTypeError < ActiveRecordError #:nodoc:
def initialize(owner_class_name, reflection, source_reflection)
- super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' with a :source_type option if the '#{reflection.through_reflection.class_name}##{source_reflection.name}' is not polymorphic. Try removing :source_type on your association.")
+ super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' with a :source_type option if the '#{reflection.through_reflection.class_name}##{source_reflection.name}' is not polymorphic. Try removing :source_type on your association.")
end
end
@@ -48,7 +48,7 @@ module ActiveRecord
through_reflection = reflection.through_reflection
source_reflection_names = reflection.source_reflection_names
source_associations = reflection.through_reflection.klass.reflect_on_all_associations.collect { |a| a.name.inspect }
- super("Could not find the source association(s) #{source_reflection_names.collect{ |a| a.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)}?")
+ super("Could not find the source association(s) #{source_reflection_names.collect{ |a| a.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)}?")
end
end
@@ -96,7 +96,7 @@ module ActiveRecord
class ReadOnlyAssociation < ActiveRecordError #:nodoc:
def initialize(reflection)
- super("Can not add to a has_many :through association. Try adding to #{reflection.through_reflection.name.inspect}.")
+ super("Can not add to a has_many :through association. Try adding to #{reflection.through_reflection.name.inspect}.")
end
end
@@ -457,12 +457,13 @@ module ActiveRecord
# has_many :people, :extend => [FindOrCreateByNameExtension, FindRecentExtension]
# end
#
- # Some extensions can only be made to work with knowledge of the association proxy's internals.
- # Extensions can access relevant state using accessors on the association proxy:
+ # Some extensions can only be made to work with knowledge of the association's internals.
+ # Extensions can access relevant state using the following methods (where 'items' is the
+ # name of the association):
#
- # * +proxy_owner+ - Returns the object the association is part of.
- # * +proxy_reflection+ - Returns the reflection object that describes the association.
- # * +proxy_target+ - Returns the associated object for +belongs_to+ and +has_one+, or
+ # * +record.association(:items).owner+ - Returns the object the association is part of.
+ # * +record.association(:items).reflection+ - Returns the reflection object that describes the association.
+ # * +record.association(:items).target+ - Returns the associated object for +belongs_to+ and +has_one+, or
# the collection of associated objects for +has_many+ and +has_and_belongs_to_many+.
#
# === Association Join Models
diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb
index adfc71d435..8415942a1a 100644
--- a/activerecord/lib/active_record/associations/collection_proxy.rb
+++ b/activerecord/lib/active_record/associations/collection_proxy.rb
@@ -123,6 +123,30 @@ module ActiveRecord
method_missing(:new, *args, &block)
end
end
+
+ def proxy_owner
+ ActiveSupport::Deprecation.warn(
+ "Calling record.#{@association.reflection.name}.proxy_owner is deprecated. Please use " \
+ "record.association(:#{@association.reflection.name}).owner instead."
+ )
+ @association.owner
+ end
+
+ def proxy_target
+ ActiveSupport::Deprecation.warn(
+ "Calling record.#{@association.reflection.name}.proxy_target is deprecated. Please use " \
+ "record.association(:#{@association.reflection.name}).target instead."
+ )
+ @association.target
+ end
+
+ def proxy_reflection
+ ActiveSupport::Deprecation.warn(
+ "Calling record.#{@association.reflection.name}.proxy_reflection is deprecated. Please use " \
+ "record.association(:#{@association.reflection.name}).reflection instead."
+ )
+ @association.reflection
+ end
end
end
end
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index cd16b8d3ca..30e2d1a82c 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -2018,7 +2018,7 @@ MSG
def extract_callstack_for_multiparameter_attributes(pairs)
attributes = { }
- for pair in pairs
+ pairs.each do |pair|
multiparameter_name, value = pair
attribute_name = multiparameter_name.split("(").first
attributes[attribute_name] = {} unless attributes.include?(attribute_name)
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb
index 4e3d8a096f..3eddb69e73 100644
--- a/activerecord/lib/active_record/connection_adapters/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/column.rb
@@ -189,7 +189,7 @@ module ActiveRecord
def new_time(year, mon, mday, hour, min, sec, microsec)
# Treat 0000-00-00 00:00:00 as nil.
- return nil if year.nil? || year == 0
+ return nil if year.nil? || (year == 0 && mon == 0 && mday == 0)
Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
end
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 32d1cff6c3..475e4338ca 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -226,7 +226,7 @@ module ActiveRecord
end
def apply_join_dependency(relation, join_dependency)
- for association in join_dependency.join_associations
+ join_dependency.join_associations.each do |association|
relation = association.join_relation(relation)
end
diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb
index 2bde06f562..be4354ce6a 100644
--- a/activerecord/lib/active_record/serialization.rb
+++ b/activerecord/lib/active_record/serialization.rb
@@ -37,7 +37,7 @@ module ActiveRecord #:nodoc:
include_has_options = include_associations.is_a?(Hash)
associations = include_has_options ? include_associations.keys : Array.wrap(include_associations)
- for association in associations
+ associations.each do |association|
records = case self.class.reflect_on_association(association).macro
when :has_many, :has_and_belongs_to_many
send(association).to_a
diff --git a/activerecord/lib/rails/generators/active_record/model/templates/migration.rb b/activerecord/lib/rails/generators/active_record/model/templates/migration.rb
index 4f81a52fd0..851930344a 100644
--- a/activerecord/lib/rails/generators/active_record/model/templates/migration.rb
+++ b/activerecord/lib/rails/generators/active_record/model/templates/migration.rb
@@ -1,7 +1,7 @@
class <%= migration_class_name %> < ActiveRecord::Migration
def change
create_table :<%= table_name %> do |t|
-<% for attribute in attributes -%>
+<% attributes.each do |attribute| -%>
t.<%= attribute.type %> :<%= attribute.name %>
<% end -%>
<% if options[:timestamps] %>
diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb
index 49d82ba2df..38d439d68a 100644
--- a/activerecord/test/cases/associations_test.rb
+++ b/activerecord/test/cases/associations_test.rb
@@ -203,6 +203,18 @@ class AssociationProxyTest < ActiveRecord::TestCase
assert_equal david.projects, david.projects.reload.reload
end
end
+
+ # Tests that proxy_owner, proxy_target and proxy_reflection are implement as deprecated methods
+ def test_proxy_deprecations
+ david = developers(:david)
+ david.projects.load_target
+
+ [:owner, :target, :reflection].each do |name|
+ assert_deprecated do
+ assert_equal david.association(:projects).send(name), david.projects.send("proxy_#{name}")
+ end
+ end
+ end
end
class OverridingAssociationsTest < ActiveRecord::TestCase