From 93a0034d5771fa3ba51a23ababb1c22ec5865853 Mon Sep 17 00:00:00 2001 From: Jacques Fuentes Date: Sat, 7 Aug 2010 17:59:36 -0400 Subject: [PATCH] adding :prefix => true as option to set_table_name to ensure module prefix --- activerecord/lib/active_record/base.rb | 12 ++++++++++-- activerecord/test/cases/modules_test.rb | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 8da4fbc..2f528cd 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -624,13 +624,21 @@ 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 4b63579..e02ef1f 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