|
| 1 | +require 'cases/helper' |
| 2 | +require 'models/topic' |
| 3 | + |
| 4 | +class ModelBaseInherited < ActiveRecord::Base |
| 5 | + class << self |
| 6 | + # Use the Man table |
| 7 | + def table_name |
| 8 | + "men" |
| 9 | + end |
| 10 | + |
| 11 | + def reset_configs |
| 12 | + self.table_name_prefix = "" |
| 13 | + self.table_name_suffix = "" |
| 14 | + end |
| 15 | + end |
| 16 | +end |
| 17 | + |
| 18 | +class ModelAppRecordInherited < ApplicationRecord |
| 19 | +end |
| 20 | + |
| 21 | +class ModelDoubleInherited < ModelAppRecordInherited |
| 22 | +end |
| 23 | + |
| 24 | +class ApplicationRecordTest < ActiveRecord::TestCase |
| 25 | + fixtures :topics |
| 26 | + |
| 27 | + def test_changing_prefix_on_ar_base |
| 28 | + prefix = 'my_prefix' |
| 29 | + ActiveRecord::Base.table_name_prefix = prefix |
| 30 | + assert_equal prefix, ModelBaseInherited.table_name_prefix |
| 31 | + assert_equal prefix, ModelAppRecordInherited.table_name_prefix |
| 32 | + end |
| 33 | + |
| 34 | + def test_changing_prefix_on_ar_base_inherited_model |
| 35 | + prefix = 'my_prefix' |
| 36 | + new_prefix = 'new_prefix' |
| 37 | + ActiveRecord::Base.table_name_prefix = prefix |
| 38 | + ModelBaseInherited.table_name_prefix = new_prefix |
| 39 | + |
| 40 | + assert_equal new_prefix, ModelBaseInherited.table_name_prefix |
| 41 | + assert_equal prefix, ActiveRecord::Base.table_name_prefix |
| 42 | + end |
| 43 | + |
| 44 | + def test_changing_prefix_on_application_record_inherited_model |
| 45 | + prefix = 'my_prefix' |
| 46 | + new_prefix = 'new_prefix' |
| 47 | + ActiveRecord::Base.table_name_prefix = prefix |
| 48 | + Topic.table_name_prefix = new_prefix |
| 49 | + |
| 50 | + assert_equal new_prefix, Topic.table_name_prefix |
| 51 | + assert_equal prefix, ActiveRecord::Base.table_name_prefix |
| 52 | + end |
| 53 | + |
| 54 | + def test_double_inheritence_rules |
| 55 | + top_suffix = ActiveRecord::Base.table_name_suffix |
| 56 | + new_suffix = 'some_new_suffix' |
| 57 | + newest_suffix = 'an_even_newer_suffix' |
| 58 | + |
| 59 | + ModelAppRecordInherited.table_name_suffix = new_suffix |
| 60 | + assert_equal top_suffix, ActiveRecord::Base.table_name_suffix |
| 61 | + assert_equal new_suffix, ModelAppRecordInherited.table_name_suffix |
| 62 | + assert_equal new_suffix, ModelDoubleInherited.table_name_suffix |
| 63 | + |
| 64 | + ModelDoubleInherited.table_name_suffix = newest_suffix |
| 65 | + assert_equal top_suffix, ActiveRecord::Base.table_name_suffix |
| 66 | + assert_equal new_suffix, ModelAppRecordInherited.table_name_suffix |
| 67 | + assert_equal newest_suffix, ModelDoubleInherited.table_name_suffix |
| 68 | + end |
| 69 | + |
| 70 | + def test_creating_new_record_from_base_inherited |
| 71 | + ModelBaseInherited.reset_configs |
| 72 | + |
| 73 | + assert_nothing_raised do |
| 74 | + ModelBaseInherited.new |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + def test_persisting_new_record_from_base_inherited |
| 79 | + ModelBaseInherited.reset_configs |
| 80 | + |
| 81 | + man = ModelBaseInherited.new |
| 82 | + man.name = 'John Wang' |
| 83 | + assert_nothing_raised { man.save } |
| 84 | + |
| 85 | + assert_equal man.id, ModelBaseInherited.find_by_name('John Wang').id |
| 86 | + end |
| 87 | +end |
| 88 | + |
| 89 | +class MultipleApplicationRecordTest < ActiveRecord::TestCase |
| 90 | + module FirstNamespace |
| 91 | + class ApplicationRecord < ::ApplicationRecord |
| 92 | + end |
| 93 | + |
| 94 | + class SomeModel < ApplicationRecord |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + module SecondNamespace |
| 99 | + class ApplicationRecord < ::ApplicationRecord |
| 100 | + end |
| 101 | + |
| 102 | + class SomeModel < ApplicationRecord |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + def test_changing_config_in_namespaces_are_isolated |
| 107 | + FirstNamespace::SomeModel.pluralize_table_names = false |
| 108 | + SecondNamespace::SomeModel.pluralize_table_names = true |
| 109 | + |
| 110 | + assert !FirstNamespace::SomeModel.pluralize_table_names |
| 111 | + assert SecondNamespace::SomeModel.pluralize_table_names |
| 112 | + |
| 113 | + FirstNamespace::SomeModel.pluralize_table_names = true |
| 114 | + end |
| 115 | + |
| 116 | + def test_changing_config_on_models_isolated_across_namespaces |
| 117 | + format = :number |
| 118 | + ApplicationRecord.cache_timestamp_format = format |
| 119 | + assert_equal format, FirstNamespace::SomeModel.cache_timestamp_format |
| 120 | + assert_equal format, SecondNamespace::SomeModel.cache_timestamp_format |
| 121 | + |
| 122 | + new_format = :long |
| 123 | + FirstNamespace::SomeModel.cache_timestamp_format = new_format |
| 124 | + assert_equal format, ApplicationRecord.cache_timestamp_format |
| 125 | + assert_equal new_format, FirstNamespace::SomeModel.cache_timestamp_format |
| 126 | + assert_equal format, SecondNamespace::SomeModel.cache_timestamp_format |
| 127 | + end |
| 128 | + |
| 129 | + def test_changing_module_application_record_does_not_propogate |
| 130 | + original_prefix = ApplicationRecord.table_name_prefix |
| 131 | + first_prefix = 'first_prefix' |
| 132 | + second_prefix = 'second_prefix' |
| 133 | + |
| 134 | + FirstNamespace::ApplicationRecord.table_name_prefix = first_prefix |
| 135 | + SecondNamespace::ApplicationRecord.table_name_prefix = second_prefix |
| 136 | + |
| 137 | + assert_equal first_prefix, FirstNamespace::ApplicationRecord.table_name_prefix |
| 138 | + assert_equal second_prefix, SecondNamespace::ApplicationRecord.table_name_prefix |
| 139 | + assert_equal first_prefix, FirstNamespace::SomeModel.table_name_prefix |
| 140 | + assert_equal second_prefix, SecondNamespace::SomeModel.table_name_prefix |
| 141 | + assert_equal original_prefix, ApplicationRecord.table_name_prefix |
| 142 | + end |
| 143 | + |
| 144 | + def test_changing_config_on_model_does_not_move_up_ancestor_chain |
| 145 | + base_scopes = ActiveRecord::Base.default_scopes |
| 146 | + app_record_scopes = ApplicationRecord.default_scopes |
| 147 | + first_namespace_scopes = FirstNamespace::ApplicationRecord.default_scopes |
| 148 | + second_namespace_scopes = SecondNamespace::ApplicationRecord.default_scopes |
| 149 | + new_scopes = [ proc {} ] |
| 150 | + |
| 151 | + FirstNamespace::SomeModel.default_scopes = new_scopes |
| 152 | + |
| 153 | + assert_equal new_scopes, FirstNamespace::SomeModel.default_scopes |
| 154 | + assert_equal base_scopes, ActiveRecord::Base.default_scopes |
| 155 | + assert_equal app_record_scopes, ApplicationRecord.default_scopes |
| 156 | + assert_equal first_namespace_scopes, FirstNamespace::ApplicationRecord.default_scopes |
| 157 | + assert_equal second_namespace_scopes, SecondNamespace::ApplicationRecord.default_scopes |
| 158 | + end |
| 159 | +end |
0 commit comments