1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add the ability to specify table_name_prefix on individual modules

Signed-off-by: wycats <wycats@gmail.com>
This commit is contained in:
Andrew White 2010-02-22 10:46:45 +00:00 committed by wycats
parent 48c1d8c341
commit 67d1cec4c8
3 changed files with 53 additions and 1 deletions

View file

@ -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 # 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 # 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. # 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 cattr_accessor :table_name_prefix, :instance_writer => false
@@table_name_prefix = "" @@table_name_prefix = ""
@ -765,7 +768,7 @@ module ActiveRecord #:nodoc:
contained = contained.singularize if parent.pluralize_table_names contained = contained.singularize if parent.pluralize_table_names
contained << '_' contained << '_'
end 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 end
@quoted_table_name = nil @quoted_table_name = nil
@ -773,6 +776,10 @@ module ActiveRecord #:nodoc:
name name
end 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 # Defines the column name for use with single table inheritance
# -- can be set in subclasses like so: self.inheritance_column = "type_id" # -- can be set in subclasses like so: self.inheritance_column = "type_id"
def inheritance_column def inheritance_column

View file

@ -82,4 +82,32 @@ class ModulesTest < ActiveRecord::TestCase
end end
end 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 end

View file

@ -32,6 +32,23 @@ module MyApplication
has_and_belongs_to_many :developers has_and_belongs_to_many :developers
end 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 end
module Billing module Billing