Skip to content
This repository
Browse code

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
Phil Smith authored April 10, 2010 josevalim committed April 12, 2010
9  activerecord/lib/active_record/base.rb
@@ -3,6 +3,7 @@
3 3
 require 'active_support/benchmarkable'
4 4
 require 'active_support/dependencies'
5 5
 require 'active_support/time'
  6
+require 'active_support/core_ext/class/attribute'
6 7
 require 'active_support/core_ext/class/attribute_accessors'
7 8
 require 'active_support/core_ext/class/delegating_attributes'
8 9
 require 'active_support/core_ext/class/inheritable_attributes'
@@ -341,15 +342,15 @@ def self.reset_subclasses #:nodoc:
341 342
     #
342 343
     # If you are organising your models within modules you can add a prefix to the models within a namespace by defining
343 344
     # a singleton method in the parent module called table_name_prefix which returns your chosen prefix.
344  
-    cattr_accessor :table_name_prefix, :instance_writer => false
345  
-    @@table_name_prefix = ""
  345
+    class_attribute :table_name_prefix, :instance_writer => false
  346
+    self.table_name_prefix = ""
346 347
 
347 348
     ##
348 349
     # :singleton-method:
349 350
     # Works like +table_name_prefix+, but appends instead of prepends (set to "_basecamp" gives "projects_basecamp",
350 351
     # "people_basecamp"). By default, the suffix is the empty string.
351  
-    cattr_accessor :table_name_suffix, :instance_writer => false
352  
-    @@table_name_suffix = ""
  352
+    class_attribute :table_name_suffix, :instance_writer => false
  353
+    self.table_name_suffix = ""
353 354
 
354 355
     ##
355 356
     # :singleton-method:
58  activerecord/test/cases/base_test.rb
@@ -579,9 +579,9 @@ def test_load_with_condition
579 579
     assert_equal(topics(:second).title, topics.first.title)
580 580
   end
581 581
 
582  
-  def test_table_name_guesses
583  
-    classes = [Category, Smarts, CreditCard, CreditCard::PinNumber, CreditCard::PinNumber::CvvCode, CreditCard::SubPinNumber, CreditCard::Brand, MasterCreditCard]
  582
+  GUESSED_CLASSES = [Category, Smarts, CreditCard, CreditCard::PinNumber, CreditCard::PinNumber::CvvCode, CreditCard::SubPinNumber, CreditCard::Brand, MasterCreditCard]
584 583
 
  584
+  def test_table_name_guesses
585 585
     assert_equal "topics", Topic.table_name
586 586
 
587 587
     assert_equal "categories", Category.table_name
@@ -592,9 +592,13 @@ def test_table_name_guesses
592 592
     assert_equal "credit_card_pin_numbers", CreditCard::SubPinNumber.table_name
593 593
     assert_equal "categories", CreditCard::Brand.table_name
594 594
     assert_equal "master_credit_cards", MasterCreditCard.table_name
  595
+  ensure
  596
+    GUESSED_CLASSES.each(&:reset_table_name)
  597
+  end
595 598
 
  599
+  def test_singular_table_name_guesses
596 600
     ActiveRecord::Base.pluralize_table_names = false
597  
-    classes.each(&:reset_table_name)
  601
+    GUESSED_CLASSES.each(&:reset_table_name)
598 602
 
599 603
     assert_equal "category", Category.table_name
600 604
     assert_equal "smarts", Smarts.table_name
@@ -604,10 +608,12 @@ def test_table_name_guesses
604 608
     assert_equal "credit_card_pin_number", CreditCard::SubPinNumber.table_name
605 609
     assert_equal "category", CreditCard::Brand.table_name
606 610
     assert_equal "master_credit_card", MasterCreditCard.table_name
607  
-
  611
+  ensure
608 612
     ActiveRecord::Base.pluralize_table_names = true
609  
-    classes.each(&:reset_table_name)
  613
+    GUESSED_CLASSES.each(&:reset_table_name)
  614
+  end
610 615
 
  616
+  def test_table_name_guesses_with_prefixes_and_suffixes
611 617
     ActiveRecord::Base.table_name_prefix = "test_"
612 618
     Category.reset_table_name
613 619
     assert_equal "test_categories", Category.table_name
@@ -620,8 +626,15 @@ def test_table_name_guesses
620 626
     ActiveRecord::Base.table_name_suffix = ""
621 627
     Category.reset_table_name
622 628
     assert_equal "categories", Category.table_name
  629
+  ensure
  630
+    ActiveRecord::Base.table_name_prefix = ""
  631
+    ActiveRecord::Base.table_name_suffix = ""
  632
+    GUESSED_CLASSES.each(&:reset_table_name)
  633
+  end
623 634
 
  635
+  def test_singular_table_name_guesses_with_prefixes_and_suffixes
624 636
     ActiveRecord::Base.pluralize_table_names = false
  637
+
625 638
     ActiveRecord::Base.table_name_prefix = "test_"
626 639
     Category.reset_table_name
627 640
     assert_equal "test_category", Category.table_name
@@ -634,9 +647,40 @@ def test_table_name_guesses
634 647
     ActiveRecord::Base.table_name_suffix = ""
635 648
     Category.reset_table_name
636 649
     assert_equal "category", Category.table_name
637  
-
  650
+  ensure
638 651
     ActiveRecord::Base.pluralize_table_names = true
639  
-    classes.each(&:reset_table_name)
  652
+    ActiveRecord::Base.table_name_prefix = ""
  653
+    ActiveRecord::Base.table_name_suffix = ""
  654
+    GUESSED_CLASSES.each(&:reset_table_name)
  655
+  end
  656
+
  657
+  def test_table_name_guesses_with_inherited_prefixes_and_suffixes
  658
+    GUESSED_CLASSES.each(&:reset_table_name)
  659
+
  660
+    CreditCard.table_name_prefix = "test_"
  661
+    CreditCard.reset_table_name
  662
+    Category.reset_table_name
  663
+    assert_equal "test_credit_cards", CreditCard.table_name
  664
+    assert_equal "categories", Category.table_name
  665
+    CreditCard.table_name_suffix = "_test"
  666
+    CreditCard.reset_table_name
  667
+    Category.reset_table_name
  668
+    assert_equal "test_credit_cards_test", CreditCard.table_name
  669
+    assert_equal "categories", Category.table_name
  670
+    CreditCard.table_name_prefix = ""
  671
+    CreditCard.reset_table_name
  672
+    Category.reset_table_name
  673
+    assert_equal "credit_cards_test", CreditCard.table_name
  674
+    assert_equal "categories", Category.table_name
  675
+    CreditCard.table_name_suffix = ""
  676
+    CreditCard.reset_table_name
  677
+    Category.reset_table_name
  678
+    assert_equal "credit_cards", CreditCard.table_name
  679
+    assert_equal "categories", Category.table_name
  680
+  ensure
  681
+    CreditCard.table_name_prefix = ""
  682
+    CreditCard.table_name_suffix = ""
  683
+    GUESSED_CLASSES.each(&:reset_table_name)
640 684
   end
641 685
 
642 686
   def test_destroy_all

0 notes on commit bab1f91

Please sign in to comment.
Something went wrong with that request. Please try again.