1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Deprecate set_inheritance_column in favour of self.inheritance_column=

This commit is contained in:
Jon Leighton 2011-11-29 15:34:22 +00:00
parent 0b72a04d0c
commit 34609d67b4
5 changed files with 48 additions and 24 deletions

View file

@ -1,12 +1,18 @@
## Rails 3.2.0 (unreleased) ## ## Rails 3.2.0 (unreleased) ##
* Deprecated `set_table_name`. Use `self.table_name=` instead, or define your own * Deprecated:
`self.table_name` method:
* `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 class Project < ActiveRecord::Base
self.table_name = "project" self.table_name = "project"
end end
Or define your own `self.table_name` method:
class Post < ActiveRecord::Base class Post < ActiveRecord::Base
def self.table_name def self.table_name
"special_" + super "special_" + super

View file

@ -702,10 +702,36 @@ module ActiveRecord #:nodoc:
(parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix (parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix
end end
# Defines the column name for use with single table inheritance. Use # The name of the column containing the object's class when Single Table Inheritance is used
# <tt>set_inheritance_column</tt> to set a different value.
def inheritance_column 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 end
# Lazy-set the sequence name to the connection's default. This method # Lazy-set the sequence name to the connection's default. This method
@ -720,20 +746,6 @@ module ActiveRecord #:nodoc:
default default
end 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 # 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 # 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 # given block. This is required for Oracle and is useful for any

View file

@ -1495,13 +1495,18 @@ class BasicsTest < ActiveRecord::TestCase
k = Class.new( ActiveRecord::Base ) k = Class.new( ActiveRecord::Base )
k.inheritance_column = "foo" k.inheritance_column = "foo"
assert_equal "foo", k.inheritance_column assert_equal "foo", k.inheritance_column
assert_deprecated do
k.set_inheritance_column "bar" k.set_inheritance_column "bar"
end
assert_equal "bar", k.inheritance_column assert_equal "bar", k.inheritance_column
end end
def test_set_inheritance_column_with_block def test_set_inheritance_column_with_block
k = Class.new( ActiveRecord::Base ) k = Class.new( ActiveRecord::Base )
assert_deprecated do
k.set_inheritance_column { original_inheritance_column + "_id" } k.set_inheritance_column { original_inheritance_column + "_id" }
end
assert_equal "type_id", k.inheritance_column assert_equal "type_id", k.inheritance_column
end end

View file

@ -236,11 +236,11 @@ class InheritanceTest < ActiveRecord::TestCase
c.save c.save
end end
[ Company, Firm, Client].each { |klass| klass.reset_column_information } [ Company, Firm, Client].each { |klass| klass.reset_column_information }
Company.set_inheritance_column('ruby_type') Company.inheritance_column = 'ruby_type'
end end
def switch_to_default_inheritance_column def switch_to_default_inheritance_column
[ Company, Firm, Client].each { |klass| klass.reset_column_information } [ Company, Firm, Client].each { |klass| klass.reset_column_information }
Company.set_inheritance_column('type') Company.inheritance_column = 'type'
end end
end end

View file

@ -1,5 +1,6 @@
class Parrot < ActiveRecord::Base 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 :pirates
has_and_belongs_to_many :treasures has_and_belongs_to_many :treasures
has_many :loots, :as => :looter has_many :loots, :as => :looter