From 8d82bef58a002b4441d86d0b08e7a5fd493c7e39 Mon Sep 17 00:00:00 2001 From: Thiago Pradi Date: Mon, 14 Jun 2010 12:01:00 -0300 Subject: [PATCH 1/2] Documentation for #quoted_table_name method Signed-off-by: Xavier Noria --- activerecord/lib/active_record/base.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 63ab6efae21dd..110f131e6c3a6 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -647,10 +647,12 @@ def table_name reset_table_name end + # Returns a quoted version of the table name, used to construct SQL statements. def quoted_table_name @quoted_table_name ||= connection.quote_table_name(table_name) end + # Computes the table name, resets it internally, and returns it. def reset_table_name #:nodoc: base = base_class From 4a8c8804ff267db8fb4fcf147b8a4eeed78db209 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 14 Jun 2010 18:36:04 +0200 Subject: [PATCH 2/2] refactors AR::Base#reset_table_name --- activerecord/lib/active_record/base.rb | 39 +++++++++++++------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 110f131e6c3a6..3b6ffa46f2872 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -14,6 +14,7 @@ require 'active_support/core_ext/string/behavior' require 'active_support/core_ext/kernel/singleton_class' require 'active_support/core_ext/module/delegation' +require 'active_support/core_ext/module/introspection' require 'active_support/core_ext/object/duplicable' require 'active_support/core_ext/object/blank' require 'arel' @@ -652,26 +653,9 @@ def quoted_table_name @quoted_table_name ||= connection.quote_table_name(table_name) end - # Computes the table name, resets it internally, and returns it. + # Computes the table name, (re)sets it internally, and returns it. def reset_table_name #:nodoc: - base = base_class - - name = - # STI subclasses always use their superclass' table. - unless self == base - base.table_name - else - # Nested classes are prefixed with singular parent table name. - if parent < ActiveRecord::Base && !parent.abstract_class? - contained = parent.table_name - contained = contained.singularize if parent.pluralize_table_names - contained << '_' - end - name = "#{full_table_name_prefix}#{contained}#{undecorated_table_name(base.name)}#{table_name_suffix}" - end - - set_table_name(name) - name + self.table_name = compute_table_name end def full_table_name_prefix #:nodoc: @@ -1003,6 +987,23 @@ def undecorated_table_name(class_name = base_class.name) table_name end + # Computes and returns a table name according to default conventions. + def compute_table_name + base = base_class + if self == base + # Nested classes are prefixed with singular parent table name. + if parent < ActiveRecord::Base && !parent.abstract_class? + contained = parent.table_name + contained = contained.singularize if parent.pluralize_table_names + contained << '_' + end + "#{full_table_name_prefix}#{contained}#{undecorated_table_name(name)}#{table_name_suffix}" + else + # STI subclasses always use their superclass' table. + base.table_name + end + end + # Enables dynamic finders like find_by_user_name(user_name) and find_by_user_name_and_password(user_name, password) # that are turned into where(:user_name => user_name).first and where(:user_name => user_name, :password => :password).first # respectively. Also works for all by using find_all_by_amount(50) that is turned into where(:amount => 50).all.