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:
parent
48c1d8c341
commit
67d1cec4c8
3 changed files with 53 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue