|
2 | 2 |
|
3 | 3 | describe 'naming inheritance' do
|
4 | 4 |
|
5 |
| - before(:all) do |
6 |
| - class ::TestBase |
7 |
| - extend ActiveModel::Naming |
| 5 | + context 'without using proxy' do |
| 6 | + before(:all) do |
| 7 | + TestBase = Class.new do |
| 8 | + extend ActiveModel::Naming |
8 | 9 |
|
9 |
| - extend Elasticsearch::Model::Naming::ClassMethods |
10 |
| - include Elasticsearch::Model::Naming::InstanceMethods |
11 |
| - end |
| 10 | + extend Elasticsearch::Model::Naming::ClassMethods |
| 11 | + include Elasticsearch::Model::Naming::InstanceMethods |
| 12 | + end |
12 | 13 |
|
13 |
| - class ::Animal < ::TestBase |
14 |
| - extend ActiveModel::Naming |
| 14 | + Animal = Class.new TestBase do |
| 15 | + extend ActiveModel::Naming |
15 | 16 |
|
16 |
| - extend Elasticsearch::Model::Naming::ClassMethods |
17 |
| - include Elasticsearch::Model::Naming::InstanceMethods |
| 17 | + extend Elasticsearch::Model::Naming::ClassMethods |
| 18 | + include Elasticsearch::Model::Naming::InstanceMethods |
| 19 | + |
| 20 | + index_name "mammals" |
| 21 | + document_type "mammal" |
| 22 | + end |
| 23 | + |
| 24 | + Dog = Class.new Animal |
| 25 | + |
| 26 | + module ::MyNamespace |
| 27 | + Dog = Class.new Animal |
| 28 | + end |
| 29 | + |
| 30 | + Cat = Class.new Animal do |
| 31 | + extend ActiveModel::Naming |
| 32 | + |
| 33 | + extend Elasticsearch::Model::Naming::ClassMethods |
| 34 | + include Elasticsearch::Model::Naming::InstanceMethods |
| 35 | + |
| 36 | + index_name "cats" |
| 37 | + document_type "cat" |
| 38 | + end |
18 | 39 |
|
19 |
| - index_name "mammals" |
20 |
| - document_type "mammal" |
21 | 40 | end
|
22 | 41 |
|
23 |
| - class ::Dog < ::Animal |
| 42 | + after(:all) do |
| 43 | + remove_classes(TestBase, Animal, MyNamespace, Cat) |
24 | 44 | end
|
25 | 45 |
|
26 |
| - module ::MyNamespace |
27 |
| - class Dog < ::Animal |
28 |
| - end |
| 46 | + around(:all) do |example| |
| 47 | + original_value = Elasticsearch::Model.inheritance_enabled |
| 48 | + Elasticsearch::Model.inheritance_enabled = true |
| 49 | + example.run |
| 50 | + Elasticsearch::Model.inheritance_enabled = original_value |
29 | 51 | end
|
30 | 52 |
|
31 |
| - class ::Cat < ::Animal |
32 |
| - extend ActiveModel::Naming |
| 53 | + describe '#index_name' do |
| 54 | + |
| 55 | + it 'returns the default index name' do |
| 56 | + expect(TestBase.index_name).to eq('test_bases') |
| 57 | + expect(TestBase.new.index_name).to eq('test_bases') |
| 58 | + end |
| 59 | + |
| 60 | + it 'returns the explicit index name' do |
| 61 | + expect(Animal.index_name).to eq('mammals') |
| 62 | + expect(Animal.new.index_name).to eq('mammals') |
33 | 63 |
|
34 |
| - extend Elasticsearch::Model::Naming::ClassMethods |
35 |
| - include Elasticsearch::Model::Naming::InstanceMethods |
| 64 | + expect(Cat.index_name).to eq('cats') |
| 65 | + expect(Cat.new.index_name).to eq('cats') |
| 66 | + end |
36 | 67 |
|
37 |
| - index_name "cats" |
38 |
| - document_type "cat" |
| 68 | + it 'returns the ancestor index name' do |
| 69 | + expect(Dog.index_name).to eq('mammals') |
| 70 | + expect(Dog.new.index_name).to eq('mammals') |
| 71 | + end |
| 72 | + |
| 73 | + it 'returns the ancestor index name for namespaced models' do |
| 74 | + expect(::MyNamespace::Dog.index_name).to eq('mammals') |
| 75 | + expect(::MyNamespace::Dog.new.index_name).to eq('mammals') |
| 76 | + end |
39 | 77 | end
|
40 | 78 |
|
41 |
| - end |
| 79 | + describe '#document_type' do |
42 | 80 |
|
43 |
| - after(:all) do |
44 |
| - remove_classes(TestBase, Animal, MyNamespace, Cat) |
45 |
| - end |
| 81 | + it 'returns nil' do |
| 82 | + expect(TestBase.document_type).to eq('_doc') |
| 83 | + expect(TestBase.new.document_type).to eq('_doc') |
| 84 | + end |
46 | 85 |
|
47 |
| - around(:all) do |example| |
48 |
| - original_value = Elasticsearch::Model.settings[:inheritance_enabled] |
49 |
| - Elasticsearch::Model.settings[:inheritance_enabled] = true |
50 |
| - example.run |
51 |
| - Elasticsearch::Model.settings[:inheritance_enabled] = original_value |
52 |
| - end |
| 86 | + it 'returns the explicit document type' do |
| 87 | + expect(Animal.document_type).to eq('mammal') |
| 88 | + expect(Animal.new.document_type).to eq('mammal') |
53 | 89 |
|
| 90 | + expect(Cat.document_type).to eq('cat') |
| 91 | + expect(Cat.new.document_type).to eq('cat') |
| 92 | + end |
54 | 93 |
|
55 |
| - describe '#index_name' do |
| 94 | + it 'returns the ancestor document type' do |
| 95 | + expect(Dog.document_type).to eq('mammal') |
| 96 | + expect(Dog.new.document_type).to eq('mammal') |
| 97 | + end |
56 | 98 |
|
57 |
| - it 'returns the default index name' do |
58 |
| - expect(TestBase.index_name).to eq('test_bases') |
59 |
| - expect(TestBase.new.index_name).to eq('test_bases') |
| 99 | + it 'returns the ancestor document type for namespaced models' do |
| 100 | + expect(::MyNamespace::Dog.document_type).to eq('mammal') |
| 101 | + expect(::MyNamespace::Dog.new.document_type).to eq('mammal') |
| 102 | + end |
60 | 103 | end
|
| 104 | + end |
| 105 | + |
| 106 | + context 'when using proxy' do |
| 107 | + before(:all) do |
| 108 | + TestBase = Class.new do |
| 109 | + extend ActiveModel::Naming |
61 | 110 |
|
62 |
| - it 'returns the explicit index name' do |
63 |
| - expect(Animal.index_name).to eq('mammals') |
64 |
| - expect(Animal.new.index_name).to eq('mammals') |
| 111 | + include Elasticsearch::Model |
| 112 | + end |
65 | 113 |
|
66 |
| - expect(Cat.index_name).to eq('cats') |
67 |
| - expect(Cat.new.index_name).to eq('cats') |
| 114 | + Animal = Class.new TestBase do |
| 115 | + index_name "mammals" |
| 116 | + document_type "mammal" |
| 117 | + end |
| 118 | + |
| 119 | + Dog = Class.new Animal |
| 120 | + |
| 121 | + module MyNamespace |
| 122 | + Dog = Class.new Animal |
| 123 | + end |
| 124 | + |
| 125 | + Cat = Class.new Animal do |
| 126 | + index_name "cats" |
| 127 | + document_type "cat" |
| 128 | + end |
68 | 129 | end
|
69 | 130 |
|
70 |
| - it 'returns the ancestor index name' do |
71 |
| - expect(Dog.index_name).to eq('mammals') |
72 |
| - expect(Dog.new.index_name).to eq('mammals') |
| 131 | + after(:all) do |
| 132 | + remove_classes(TestBase, Animal, MyNamespace, Cat) |
73 | 133 | end
|
74 | 134 |
|
75 |
| - it 'returns the ancestor index name for namespaced models' do |
76 |
| - expect(::MyNamespace::Dog.index_name).to eq('mammals') |
77 |
| - expect(::MyNamespace::Dog.new.index_name).to eq('mammals') |
| 135 | + around(:all) do |example| |
| 136 | + original_value = Elasticsearch::Model.settings[:inheritance_enabled] |
| 137 | + Elasticsearch::Model.settings[:inheritance_enabled] = true |
| 138 | + example.run |
| 139 | + Elasticsearch::Model.settings[:inheritance_enabled] = original_value |
78 | 140 | end
|
79 |
| - end |
80 | 141 |
|
81 |
| - describe '#document_type' do |
| 142 | + describe '#index_name' do |
82 | 143 |
|
83 |
| - it 'returns the default document type' do |
84 |
| - expect(TestBase.document_type).to eq('_doc') |
85 |
| - expect(TestBase.new.document_type).to eq('_doc') |
86 |
| - end |
| 144 | + it 'returns the default index name' do |
| 145 | + expect(TestBase.index_name).to eq('test_bases') |
| 146 | + end |
87 | 147 |
|
88 |
| - it 'returns the explicit document type' do |
89 |
| - expect(Animal.document_type).to eq('mammal') |
90 |
| - expect(Animal.new.document_type).to eq('mammal') |
| 148 | + it 'returns the explicit index name' do |
| 149 | + expect(Animal.index_name).to eq('mammals') |
91 | 150 |
|
92 |
| - expect(Cat.document_type).to eq('cat') |
93 |
| - expect(Cat.new.document_type).to eq('cat') |
94 |
| - end |
| 151 | + expect(Cat.index_name).to eq('cats') |
| 152 | + end |
| 153 | + |
| 154 | + it 'returns the ancestor index name' do |
| 155 | + expect(Dog.index_name).to eq('mammals') |
| 156 | + end |
95 | 157 |
|
96 |
| - it 'returns the ancestor document type' do |
97 |
| - expect(Dog.document_type).to eq('mammal') |
98 |
| - expect(Dog.new.document_type).to eq('mammal') |
| 158 | + it 'returns the ancestor index name for namespaced models' do |
| 159 | + expect(::MyNamespace::Dog.index_name).to eq('mammals') |
| 160 | + end |
99 | 161 | end
|
100 | 162 |
|
101 |
| - it 'returns the ancestor document type for namespaced models' do |
102 |
| - expect(::MyNamespace::Dog.document_type).to eq('mammal') |
103 |
| - expect(::MyNamespace::Dog.new.document_type).to eq('mammal') |
| 163 | + describe '#document_type' do |
| 164 | + |
| 165 | + it 'returns nil' do |
| 166 | + expect(TestBase.document_type).to eq('_doc') |
| 167 | + end |
| 168 | + |
| 169 | + it 'returns the explicit document type' do |
| 170 | + expect(Animal.document_type).to eq('mammal') |
| 171 | + |
| 172 | + expect(Cat.document_type).to eq('cat') |
| 173 | + end |
| 174 | + |
| 175 | + it 'returns the ancestor document type' do |
| 176 | + expect(Dog.document_type).to eq('mammal') |
| 177 | + end |
| 178 | + |
| 179 | + it 'returns the ancestor document type for namespaced models' do |
| 180 | + expect(::MyNamespace::Dog.document_type).to eq('mammal') |
| 181 | + end |
104 | 182 | end
|
105 | 183 | end
|
106 | 184 | end
|
0 commit comments