load_models.rb
1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Noosfero::Application.class_eval do
config.after_initialize do
Rails.application.eager_load! #TODO: REMOVE THIS LINE
indeces_models searchables_models
end
def searchables_models
ActiveRecord::Base.descendants.select do |model|
model.const_defined?("SEARCHABLE_FIELDS")
end
end
def indeces_models models
indexed_models = Array.new
models.each do |model|
next if indexed_models.include? model
create_searchable_model model
indexed_models.push model
if model.descendants.count > 0
model.descendants.each { | descendant_model|
indexed_models.push descendant_model
create_searchable_model descendant_model
}
end
end
end
def create_searchable_model model
model.class_eval do
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
settings index: { number_of_shards: 1 } do
mappings dynamic: 'false' do
model::SEARCHABLE_FIELDS.each do |field, value|
indexes field
end
end
model.__elasticsearch__.client.indices.delete \
index: model.index_name rescue nil
model.__elasticsearch__.client.indices.create \
index: model.index_name,
body: {
settings: model.settings.to_hash,
mappings: model.mappings.to_hash
}
model.import
end
end
end
end