|
14 | 14 | require 'active_support/core_ext/string/behavior'
|
15 | 15 | require 'active_support/core_ext/kernel/singleton_class'
|
16 | 16 | require 'active_support/core_ext/module/delegation'
|
| 17 | +require 'active_support/core_ext/module/introspection' |
17 | 18 | require 'active_support/core_ext/object/duplicable'
|
18 | 19 | require 'active_support/core_ext/object/blank'
|
19 | 20 | require 'arel'
|
@@ -652,26 +653,9 @@ def quoted_table_name
|
652 | 653 | @quoted_table_name ||= connection.quote_table_name(table_name)
|
653 | 654 | end
|
654 | 655 |
|
655 |
| - # Computes the table name, resets it internally, and returns it. |
| 656 | + # Computes the table name, (re)sets it internally, and returns it. |
656 | 657 | 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 |
675 | 659 | end
|
676 | 660 |
|
677 | 661 | def full_table_name_prefix #:nodoc:
|
@@ -1003,6 +987,23 @@ def undecorated_table_name(class_name = base_class.name)
|
1003 | 987 | table_name
|
1004 | 988 | end
|
1005 | 989 |
|
| 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 | + |
1006 | 1007 | # Enables dynamic finders like <tt>find_by_user_name(user_name)</tt> and <tt>find_by_user_name_and_password(user_name, password)</tt>
|
1007 | 1008 | # that are turned into <tt>where(:user_name => user_name).first</tt> and <tt>where(:user_name => user_name, :password => :password).first</tt>
|
1008 | 1009 | # 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