From 34609d67b442366644945a95b019daf5b474727b Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 29 Nov 2011 15:34:22 +0000 Subject: Deprecate set_inheritance_column in favour of self.inheritance_column= --- activerecord/CHANGELOG.md | 10 +++++-- activerecord/lib/active_record/base.rb | 46 ++++++++++++++++++----------- activerecord/test/cases/base_test.rb | 9 ++++-- activerecord/test/cases/inheritance_test.rb | 4 +-- activerecord/test/models/parrot.rb | 3 +- 5 files changed, 48 insertions(+), 24 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 2d15b04334..2e226cc55f 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,12 +1,18 @@ ## Rails 3.2.0 (unreleased) ## -* Deprecated `set_table_name`. Use `self.table_name=` instead, or define your own - `self.table_name` method: +* Deprecated: + + * `set_table_name` + * `set_inheritance_column` + + Use an assignment method instead. For example, instead of `set_table_name`, use `self.table_name=`: class Project < ActiveRecord::Base self.table_name = "project" end + Or define your own `self.table_name` method: + class Post < ActiveRecord::Base def self.table_name "special_" + super diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index bccf1b9b77..d1d3888de2 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -702,10 +702,36 @@ module ActiveRecord #:nodoc: (parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix end - # Defines the column name for use with single table inheritance. Use - # set_inheritance_column to set a different value. + # The name of the column containing the object's class when Single Table Inheritance is used def inheritance_column - @inheritance_column ||= "type" + if self == Base + 'type' + else + defined?(@inheritance_column) ? @inheritance_column : superclass.inheritance_column + end + end + + # Sets the value of inheritance_column + def inheritance_column=(value) + @inheritance_column = value.to_s + end + + def set_inheritance_column(value = nil, &block) #:nodoc: + if block + ActiveSupport::Deprecation.warn( + "Calling set_inheritance_column is deprecated. If you need to lazily evaluate " \ + "the inheritance column, define your own `self.inheritance_column` class method. You can use `super` " \ + "to get the default inheritance column where you would have called `original_inheritance_column`." + ) + + define_attr_method :inheritance_column, value, &block + else + ActiveSupport::Deprecation.warn( + "Calling set_inheritance_column is deprecated. Please use `self.inheritance_column = 'the_name'` instead." + ) + + self.inheritance_column = value + end end # Lazy-set the sequence name to the connection's default. This method @@ -720,20 +746,6 @@ module ActiveRecord #:nodoc: default end - # Sets the name of the inheritance column to use to the given value, - # or (if the value # is nil or false) to the value returned by the - # given block. - # - # class Project < ActiveRecord::Base - # set_inheritance_column do - # original_inheritance_column + "_id" - # end - # end - def set_inheritance_column(value = nil, &block) - define_attr_method :inheritance_column, value, &block - end - alias :inheritance_column= :set_inheritance_column - # Sets the name of the sequence to use when generating ids to the given # value, or (if the value is nil or false) to the value returned by the # given block. This is required for Oracle and is useful for any diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 523b0c74ed..1ae5e6029f 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1495,13 +1495,18 @@ class BasicsTest < ActiveRecord::TestCase k = Class.new( ActiveRecord::Base ) k.inheritance_column = "foo" assert_equal "foo", k.inheritance_column - k.set_inheritance_column "bar" + + assert_deprecated do + k.set_inheritance_column "bar" + end assert_equal "bar", k.inheritance_column end def test_set_inheritance_column_with_block k = Class.new( ActiveRecord::Base ) - k.set_inheritance_column { original_inheritance_column + "_id" } + assert_deprecated do + k.set_inheritance_column { original_inheritance_column + "_id" } + end assert_equal "type_id", k.inheritance_column end diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index b5d8314541..fab858e09c 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -236,11 +236,11 @@ class InheritanceTest < ActiveRecord::TestCase c.save end [ Company, Firm, Client].each { |klass| klass.reset_column_information } - Company.set_inheritance_column('ruby_type') + Company.inheritance_column = 'ruby_type' end def switch_to_default_inheritance_column [ Company, Firm, Client].each { |klass| klass.reset_column_information } - Company.set_inheritance_column('type') + Company.inheritance_column = 'type' end end diff --git a/activerecord/test/models/parrot.rb b/activerecord/test/models/parrot.rb index 737ef9131b..c4ee2bd19d 100644 --- a/activerecord/test/models/parrot.rb +++ b/activerecord/test/models/parrot.rb @@ -1,5 +1,6 @@ class Parrot < ActiveRecord::Base - set_inheritance_column :parrot_sti_class + self.inheritance_column = :parrot_sti_class + has_and_belongs_to_many :pirates has_and_belongs_to_many :treasures has_many :loots, :as => :looter -- cgit v1.2.3