Skip to content

Commit 4a8c880

Browse files
committed
refactors AR::Base#reset_table_name
1 parent 8d82bef commit 4a8c880

File tree

1 file changed

+20
-19
lines changed
  • activerecord/lib/active_record

1 file changed

+20
-19
lines changed

activerecord/lib/active_record/base.rb

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require 'active_support/core_ext/string/behavior'
1515
require 'active_support/core_ext/kernel/singleton_class'
1616
require 'active_support/core_ext/module/delegation'
17+
require 'active_support/core_ext/module/introspection'
1718
require 'active_support/core_ext/object/duplicable'
1819
require 'active_support/core_ext/object/blank'
1920
require 'arel'
@@ -652,26 +653,9 @@ def quoted_table_name
652653
@quoted_table_name ||= connection.quote_table_name(table_name)
653654
end
654655

655-
# Computes the table name, resets it internally, and returns it.
656+
# Computes the table name, (re)sets it internally, and returns it.
656657
def reset_table_name #:nodoc:
657-
base = base_class
658-
659-
name =
660-
# STI subclasses always use their superclass' table.
661-
unless self == base
662-
base.table_name
663-
else
664-
# Nested classes are prefixed with singular parent table name.
665-
if parent < ActiveRecord::Base && !parent.abstract_class?
666-
contained = parent.table_name
667-
contained = contained.singularize if parent.pluralize_table_names
668-
contained << '_'
669-
end
670-
name = "#{full_table_name_prefix}#{contained}#{undecorated_table_name(base.name)}#{table_name_suffix}"
671-
end
672-
673-
set_table_name(name)
674-
name
658+
self.table_name = compute_table_name
675659
end
676660

677661
def full_table_name_prefix #:nodoc:
@@ -1003,6 +987,23 @@ def undecorated_table_name(class_name = base_class.name)
1003987
table_name
1004988
end
1005989

990+
# Computes and returns a table name according to default conventions.
991+
def compute_table_name
992+
base = base_class
993+
if self == base
994+
# Nested classes are prefixed with singular parent table name.
995+
if parent < ActiveRecord::Base && !parent.abstract_class?
996+
contained = parent.table_name
997+
contained = contained.singularize if parent.pluralize_table_names
998+
contained << '_'
999+
end
1000+
"#{full_table_name_prefix}#{contained}#{undecorated_table_name(name)}#{table_name_suffix}"
1001+
else
1002+
# STI subclasses always use their superclass' table.
1003+
base.table_name
1004+
end
1005+
end
1006+
10061007
# Enables dynamic finders like <tt>find_by_user_name(user_name)</tt> and <tt>find_by_user_name_and_password(user_name, password)</tt>
10071008
# that are turned into <tt>where(:user_name => user_name).first</tt> and <tt>where(:user_name => user_name, :password => :password).first</tt>
10081009
# respectively. Also works for <tt>all</tt> by using <tt>find_all_by_amount(50)</tt> that is turned into <tt>where(:amount => 50).all</tt>.

0 commit comments

Comments
 (0)