Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

table_name_prefix and table_name_suffix are class_attributes instead …

…of cattr_accessors. [#4230]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit bab1f910c7399fcfe9f031a1ce3a1f36bf5fd277 1 parent 788d923
@phs phs authored josevalim committed
View
9 activerecord/lib/active_record/base.rb
@@ -3,6 +3,7 @@
require 'active_support/benchmarkable'
require 'active_support/dependencies'
require 'active_support/time'
+require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/class/delegating_attributes'
require 'active_support/core_ext/class/inheritable_attributes'
@@ -341,15 +342,15 @@ def self.reset_subclasses #:nodoc:
#
# 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 = ""
+ class_attribute :table_name_prefix, :instance_writer => false
+ self.table_name_prefix = ""
##
# :singleton-method:
# Works like +table_name_prefix+, but appends instead of prepends (set to "_basecamp" gives "projects_basecamp",
# "people_basecamp"). By default, the suffix is the empty string.
- cattr_accessor :table_name_suffix, :instance_writer => false
- @@table_name_suffix = ""
+ class_attribute :table_name_suffix, :instance_writer => false
+ self.table_name_suffix = ""
##
# :singleton-method:
View
58 activerecord/test/cases/base_test.rb
@@ -579,9 +579,9 @@ def test_load_with_condition
assert_equal(topics(:second).title, topics.first.title)
end
- def test_table_name_guesses
- classes = [Category, Smarts, CreditCard, CreditCard::PinNumber, CreditCard::PinNumber::CvvCode, CreditCard::SubPinNumber, CreditCard::Brand, MasterCreditCard]
+ GUESSED_CLASSES = [Category, Smarts, CreditCard, CreditCard::PinNumber, CreditCard::PinNumber::CvvCode, CreditCard::SubPinNumber, CreditCard::Brand, MasterCreditCard]
+ def test_table_name_guesses
assert_equal "topics", Topic.table_name
assert_equal "categories", Category.table_name
@@ -592,9 +592,13 @@ def test_table_name_guesses
assert_equal "credit_card_pin_numbers", CreditCard::SubPinNumber.table_name
assert_equal "categories", CreditCard::Brand.table_name
assert_equal "master_credit_cards", MasterCreditCard.table_name
+ ensure
+ GUESSED_CLASSES.each(&:reset_table_name)
+ end
+ def test_singular_table_name_guesses
ActiveRecord::Base.pluralize_table_names = false
- classes.each(&:reset_table_name)
+ GUESSED_CLASSES.each(&:reset_table_name)
assert_equal "category", Category.table_name
assert_equal "smarts", Smarts.table_name
@@ -604,10 +608,12 @@ def test_table_name_guesses
assert_equal "credit_card_pin_number", CreditCard::SubPinNumber.table_name
assert_equal "category", CreditCard::Brand.table_name
assert_equal "master_credit_card", MasterCreditCard.table_name
-
+ ensure
ActiveRecord::Base.pluralize_table_names = true
- classes.each(&:reset_table_name)
+ GUESSED_CLASSES.each(&:reset_table_name)
+ end
+ def test_table_name_guesses_with_prefixes_and_suffixes
ActiveRecord::Base.table_name_prefix = "test_"
Category.reset_table_name
assert_equal "test_categories", Category.table_name
@@ -620,8 +626,15 @@ def test_table_name_guesses
ActiveRecord::Base.table_name_suffix = ""
Category.reset_table_name
assert_equal "categories", Category.table_name
+ ensure
+ ActiveRecord::Base.table_name_prefix = ""
+ ActiveRecord::Base.table_name_suffix = ""
+ GUESSED_CLASSES.each(&:reset_table_name)
+ end
+ def test_singular_table_name_guesses_with_prefixes_and_suffixes
ActiveRecord::Base.pluralize_table_names = false
+
ActiveRecord::Base.table_name_prefix = "test_"
Category.reset_table_name
assert_equal "test_category", Category.table_name
@@ -634,9 +647,40 @@ def test_table_name_guesses
ActiveRecord::Base.table_name_suffix = ""
Category.reset_table_name
assert_equal "category", Category.table_name
-
+ ensure
ActiveRecord::Base.pluralize_table_names = true
- classes.each(&:reset_table_name)
+ ActiveRecord::Base.table_name_prefix = ""
+ ActiveRecord::Base.table_name_suffix = ""
+ GUESSED_CLASSES.each(&:reset_table_name)
+ end
+
+ def test_table_name_guesses_with_inherited_prefixes_and_suffixes
+ GUESSED_CLASSES.each(&:reset_table_name)
+
+ CreditCard.table_name_prefix = "test_"
+ CreditCard.reset_table_name
+ Category.reset_table_name
+ assert_equal "test_credit_cards", CreditCard.table_name
+ assert_equal "categories", Category.table_name
+ CreditCard.table_name_suffix = "_test"
+ CreditCard.reset_table_name
+ Category.reset_table_name
+ assert_equal "test_credit_cards_test", CreditCard.table_name
+ assert_equal "categories", Category.table_name
+ CreditCard.table_name_prefix = ""
+ CreditCard.reset_table_name
+ Category.reset_table_name
+ assert_equal "credit_cards_test", CreditCard.table_name
+ assert_equal "categories", Category.table_name
+ CreditCard.table_name_suffix = ""
+ CreditCard.reset_table_name
+ Category.reset_table_name
+ assert_equal "credit_cards", CreditCard.table_name
+ assert_equal "categories", Category.table_name
+ ensure
+ CreditCard.table_name_prefix = ""
+ CreditCard.table_name_suffix = ""
+ GUESSED_CLASSES.each(&:reset_table_name)
end
def test_destroy_all
Please sign in to comment.
Something went wrong with that request. Please try again.