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