Permalink
Browse files

Deprecate set_inheritance_column in favour of self.inheritance_column=

  • Loading branch information...
1 parent 0b72a04 commit 34609d67b442366644945a95b019daf5b474727b @jonleighton jonleighton committed Nov 29, 2011
View
@@ -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
@@ -702,10 +702,36 @@ def full_table_name_prefix #: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
- # <tt>set_inheritance_column</tt> 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 @@ def reset_sequence_name #: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
@@ -1495,13 +1495,18 @@ def test_set_inheritance_column_with_value
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
@@ -236,11 +236,11 @@ def switch_to_alt_inheritance_column
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
@@ -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

0 comments on commit 34609d6

Please sign in to comment.