2323require 'active_support/core_ext/module/introspection'
2424require 'active_support/core_ext/object/duplicable'
2525require 'active_support/core_ext/object/blank'
26+ require 'active_support/deprecation'
2627require 'arel'
2728require 'active_record/errors'
2829require 'active_record/log_subscriber'
@@ -624,14 +625,61 @@ def serialize(attr_name, class_name = Object)
624625 # the table name guess for an Invoice class becomes "myapp_invoices".
625626 # Invoice::Lineitem becomes "myapp_invoice_lineitems".
626627 #
627- # You can also overwrite this class method to allow for unguessable
628- # links, such as a Mouse class with a link to a "mice" table. Example:
628+ # You can also set your own table name explicitly:
629629 #
630630 # class Mouse < ActiveRecord::Base
631- # set_table_name "mice"
631+ # self.table_name = "mice"
632632 # end
633+ #
634+ # Alternatively, you can override the table_name method to define your
635+ # own computation. (Possibly using <tt>super</tt> to manipulate the default
636+ # table name.) Example:
637+ #
638+ # class Post < ActiveRecord::Base
639+ # def self.table_name
640+ # "special_" + super
641+ # end
642+ # end
643+ # Post.table_name # => "special_posts"
633644 def table_name
634- reset_table_name
645+ reset_table_name unless defined? ( @table_name )
646+ @table_name
647+ end
648+
649+ # Sets the table name explicitly. Example:
650+ #
651+ # class Project < ActiveRecord::Base
652+ # self.table_name = "project"
653+ # end
654+ #
655+ # You can also just define your own <tt>self.table_name</tt> method; see
656+ # the documentation for ActiveRecord::Base#table_name.
657+ def table_name = ( value )
658+ @quoted_table_name = nil
659+ @arel_table = nil
660+ @table_name = value
661+ @relation = Relation . new ( self , arel_table )
662+ end
663+
664+ def set_table_name ( value = nil , &block ) #:nodoc:
665+ if block
666+ ActiveSupport ::Deprecation . warn (
667+ "Calling set_table_name is deprecated. If you need to lazily evaluate " \
668+ "the table name, define your own `self.table_name` class method. You can use `super` " \
669+ "to get the default table name where you would have called `original_table_name`."
670+ )
671+
672+ @quoted_table_name = nil
673+ define_attr_method :table_name , value , &block
674+ @arel_table = nil
675+ @relation = Relation . new ( self , arel_table )
676+ else
677+ ActiveSupport ::Deprecation . warn (
678+ "Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead."
679+ )
680+
681+ self . table_name = value
682+ end
635683 end
636684
637685 # Returns a quoted version of the table name, used to construct SQL statements.
@@ -641,9 +689,13 @@ def quoted_table_name
641689
642690 # Computes the table name, (re)sets it internally, and returns it.
643691 def reset_table_name #:nodoc:
644- return if abstract_class?
645-
646- self . table_name = compute_table_name
692+ if superclass . abstract_class?
693+ self . table_name = superclass . table_name || compute_table_name
694+ elsif abstract_class?
695+ self . table_name = superclass == Base ? nil : superclass . table_name
696+ else
697+ self . table_name = compute_table_name
698+ end
647699 end
648700
649701 def full_table_name_prefix #:nodoc:
@@ -668,21 +720,6 @@ def reset_sequence_name #:nodoc:
668720 default
669721 end
670722
671- # Sets the table name. If the value is nil or false then the value returned by the given
672- # block is used.
673- #
674- # class Project < ActiveRecord::Base
675- # set_table_name "project"
676- # end
677- def set_table_name ( value = nil , &block )
678- @quoted_table_name = nil
679- define_attr_method :table_name , value , &block
680- @arel_table = nil
681-
682- @relation = Relation . new ( self , arel_table )
683- end
684- alias :table_name= :set_table_name
685-
686723 # Sets the name of the inheritance column to use to the given value,
687724 # or (if the value # is nil or false) to the value returned by the
688725 # given block.
0 commit comments