From 67d1cec4c8a65846cc3b2779bc5eca7b8b73e6fc Mon Sep 17 00:00:00 2001 From: Andrew White Date: Mon, 22 Feb 2010 10:46:45 +0000 Subject: [PATCH] Add the ability to specify table_name_prefix on individual modules Signed-off-by: wycats --- activerecord/lib/active_record/base.rb | 9 +++++- activerecord/test/cases/modules_test.rb | 28 +++++++++++++++++++ activerecord/test/models/company_in_module.rb | 17 +++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 21b89694fc..289c95ad8f 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -338,6 +338,9 @@ module ActiveRecord #:nodoc: # Accessor for the name of the prefix string to prepend to every table name. So if set to "basecamp_", all # table names will be named like "basecamp_projects", "basecamp_people", etc. This is a convenient way of creating a namespace # for tables in a shared database. By default, the prefix is the empty string. + # + # If you are organising your models within modules you can add a prefix to the models within a namespace by defining + # a singleton method in the parent module called table_name_prefix which returns your chosen prefix. cattr_accessor :table_name_prefix, :instance_writer => false @@table_name_prefix = "" @@ -765,7 +768,7 @@ module ActiveRecord #:nodoc: contained = contained.singularize if parent.pluralize_table_names contained << '_' end - name = "#{table_name_prefix}#{contained}#{undecorated_table_name(base.name)}#{table_name_suffix}" + name = "#{modularized_table_name_prefix}#{contained}#{undecorated_table_name(base.name)}#{table_name_suffix}" end @quoted_table_name = nil @@ -773,6 +776,10 @@ module ActiveRecord #:nodoc: name end + def modularized_table_name_prefix #:nodoc: + (parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix + end + # Defines the column name for use with single table inheritance # -- can be set in subclasses like so: self.inheritance_column = "type_id" def inheritance_column diff --git a/activerecord/test/cases/modules_test.rb b/activerecord/test/cases/modules_test.rb index d781a229f4..7209966bf8 100644 --- a/activerecord/test/cases/modules_test.rb +++ b/activerecord/test/cases/modules_test.rb @@ -82,4 +82,32 @@ class ModulesTest < ActiveRecord::TestCase end end end + + def test_module_table_name_prefix + assert_equal 'prefixed_companies', MyApplication::Business::Prefixed::Company.table_name, 'inferred table_name for ActiveRecord model in module with table_name_prefix' + assert_equal 'prefixed_companies', MyApplication::Business::Prefixed::Nested::Company.table_name, 'table_name for ActiveRecord model in nested module with a parent table_name_prefix' + 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_module_table_name_prefix_with_global_prefix + classes = [ MyApplication::Business::Company, + MyApplication::Business::Firm, + MyApplication::Business::Client, + MyApplication::Business::Client::Contact, + MyApplication::Business::Developer, + MyApplication::Business::Project, + MyApplication::Business::Prefixed::Company, + MyApplication::Business::Prefixed::Nested::Company, + MyApplication::Billing::Account ] + + ActiveRecord::Base.table_name_prefix = 'global_' + classes.each(&:reset_table_name) + assert_equal 'global_companies', MyApplication::Business::Company.table_name, 'inferred table_name for ActiveRecord model in module without table_name_prefix' + assert_equal 'prefixed_companies', MyApplication::Business::Prefixed::Company.table_name, 'inferred table_name for ActiveRecord model in module with table_name_prefix' + assert_equal 'prefixed_companies', MyApplication::Business::Prefixed::Nested::Company.table_name, 'table_name for ActiveRecord model in nested module with a parent table_name_prefix' + 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' + ensure + ActiveRecord::Base.table_name_prefix = '' + classes.each(&:reset_table_name) + end end diff --git a/activerecord/test/models/company_in_module.rb b/activerecord/test/models/company_in_module.rb index cdda7a44d4..83d71b6909 100644 --- a/activerecord/test/models/company_in_module.rb +++ b/activerecord/test/models/company_in_module.rb @@ -32,6 +32,23 @@ module MyApplication has_and_belongs_to_many :developers end + module Prefixed + def self.table_name_prefix + 'prefixed_' + end + + class Company < ActiveRecord::Base + end + + class Firm < Company + self.table_name = 'companies' + end + + module Nested + class Company < ActiveRecord::Base + end + end + end end module Billing