From 12bb66e2dc57706dded66ba2ac9ed7e20a0113c3 Mon Sep 17 00:00:00 2001 From: Jacques Fuentes Date: Fri, 22 Oct 2010 13:48:22 -0400 Subject: [PATCH] adding :prefix => true as opt to set_table_name to ensure module prefix is respected --- activerecord/lib/active_record/base.rb | 11 +++++++++-- activerecord/test/cases/modules_test.rb | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 053b796..3f106dd 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -603,13 +603,20 @@ module ActiveRecord #:nodoc: end # Sets the table name. If the value is nil or false then the value returned by the given - # block is used. + # block is used. Accepts a boolean to include the module's table prefix in +options+. # # class Project < ActiveRecord::Base # set_table_name "project" # end - def set_table_name(value = nil, &block) + # + # Example using :prefix in +options which respects the module's namespace: + # + # class Prefixed::Project < ActiveRecord::Base + # set_table_name "project", :prefix => true + # end + def set_table_name(value = nil, options={}, &block) @quoted_table_name = nil + value = (options.is_a?(Hash) && options[:prefix]) ? "#{full_table_name_prefix}#{value}" : value define_attr_method :table_name, value, &block end alias :table_name= :set_table_name diff --git a/activerecord/test/cases/modules_test.rb b/activerecord/test/cases/modules_test.rb index 14870cb..13e3f7a 100644 --- a/activerecord/test/cases/modules_test.rb +++ b/activerecord/test/cases/modules_test.rb @@ -90,6 +90,21 @@ class ModulesTest < ActiveRecord::TestCase assert_equal 'companies', MyApplication::Business::Prefixed::Firm.table_name, 'explicit table_name for ActiveRecord model in module with table_name_prefix should not be prefixed' end + def test_set_table_name_with_prefix_option + MyApplication::Business::Prefixed::Company.set_table_name :custom_table_name, :prefix => true + assert_equal 'prefixed_custom_table_name', MyApplication::Business::Prefixed::Company.table_name + + MyApplication::Business::Prefixed::Company.table_name_prefix = '' + + MyApplication::Business::Prefixed::Company.set_table_name :custom_table_name + assert_equal 'custom_table_name', MyApplication::Business::Prefixed::Company.table_name + + MyApplication::Business::Prefixed::Company.set_table_name :custom_table_name, :prefix => false + assert_equal 'custom_table_name', MyApplication::Business::Prefixed::Company.table_name + + MyApplication::Business::Prefixed::Company.reset_table_name + end + def test_module_table_name_prefix_with_global_prefix classes = [ MyApplication::Business::Company, MyApplication::Business::Firm, -- 1.7.0.4