Skip to content

Commit

Permalink
Implement deprecated version of AssociationReflection#primary_key_nam…
Browse files Browse the repository at this point in the history
…e, which has been renamed to #foreign_key. Also bumping the deprecation_horizon in Active Support to 3.1.
  • Loading branch information
jonleighton authored and tenderlove committed Jan 4, 2011
1 parent a9bed98 commit 0619dc2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions activerecord/lib/active_record/reflection.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/module/deprecation'

module ActiveRecord
# = Active Record Reflection
Expand Down Expand Up @@ -200,6 +201,11 @@ def foreign_key
@foreign_key ||= options[:foreign_key] || derive_foreign_key
end

def primary_key_name
foreign_key
end
deprecate :primary_key_name => :foreign_key

def foreign_type
@foreign_type ||= options[:foreign_type] || "#{name}_type"
end
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/reflection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ def test_never_validate_association_if_explicit
assert !AssociationReflection.new(:has_and_belongs_to_many, :clients, { :autosave => true, :validate => false }, Firm).validate?
end

def test_foreign_key
assert_equal "author_id", Author.reflect_on_association(:posts).foreign_key.to_s
assert_equal "category_id", Post.reflect_on_association(:categorizations).foreign_key.to_s
end

def test_primary_key_name
assert_deprecated do
assert_equal "author_id", Author.reflect_on_association(:posts).primary_key_name.to_s
assert_equal "category_id", Post.reflect_on_association(:categorizations).primary_key_name.to_s
end
end

private
def assert_reflection(klass, association, options)
assert reflection = klass.reflect_on_association(association)
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/deprecation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class << self
# The version the deprecated behavior will be removed, by default.
attr_accessor :deprecation_horizon
end
self.deprecation_horizon = '3.0'
self.deprecation_horizon = '3.1'

# By default, warnings are not silenced and debugging is off.
self.silenced = false
Expand Down

0 comments on commit 0619dc2

Please sign in to comment.