elastic_record 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/elastic_record.gemspec +1 -1
- data/lib/elastic_record/index.rb +12 -2
- data/lib/elastic_record/index/documents.rb +5 -1
- data/lib/elastic_record/model.rb +0 -2
- data/test/elastic_record/index/documents_test.rb +1 -1
- data/test/elastic_record/index_test.rb +17 -0
- data/test/elastic_record/model_test.rb +1 -1
- data/test/support/widget.rb +5 -0
- metadata +1 -1
data/elastic_record.gemspec
CHANGED
data/lib/elastic_record/index.rb
CHANGED
@@ -9,17 +9,27 @@ module ElasticRecord
|
|
9
9
|
include Mapping
|
10
10
|
|
11
11
|
attr_accessor :model
|
12
|
+
attr_accessor :disabled
|
12
13
|
|
13
14
|
def initialize(model)
|
14
15
|
@model = model
|
16
|
+
@disabled = false
|
15
17
|
end
|
16
18
|
|
17
19
|
def alias_name
|
18
|
-
model.model_name.collection
|
20
|
+
@alias_name ||= model.base_class.model_name.collection
|
19
21
|
end
|
20
22
|
|
21
23
|
def type
|
22
|
-
model.model_name.element
|
24
|
+
@type ||= model.base_class.model_name.element
|
25
|
+
end
|
26
|
+
|
27
|
+
def disable!
|
28
|
+
@disabled = true
|
29
|
+
end
|
30
|
+
|
31
|
+
def enable!
|
32
|
+
@disabled = false
|
23
33
|
end
|
24
34
|
|
25
35
|
private
|
@@ -2,8 +2,10 @@ module ElasticRecord
|
|
2
2
|
class Index
|
3
3
|
module Documents
|
4
4
|
def index_record(record, index_name = nil)
|
5
|
+
return if disabled
|
6
|
+
|
5
7
|
index_name ||= alias_name
|
6
|
-
document = record.respond_to?(:as_search) ? record.as_search :
|
8
|
+
document = record.respond_to?(:as_search) ? record.as_search : {}
|
7
9
|
|
8
10
|
connection.index(document, id: record.id, index: index_name)
|
9
11
|
# json_put "#{index_name}/#{type}/#{record.id}", document
|
@@ -21,6 +23,8 @@ module ElasticRecord
|
|
21
23
|
end
|
22
24
|
|
23
25
|
def bulk_add(batch, index_name = nil)
|
26
|
+
return if disabled
|
27
|
+
|
24
28
|
index_name ||= alias_name
|
25
29
|
|
26
30
|
connection.bulk do
|
data/lib/elastic_record/model.rb
CHANGED
@@ -7,4 +7,21 @@ class ElasticRecord::IndexTest < MiniTest::Spec
|
|
7
7
|
assert_equal 'widgets', index.alias_name
|
8
8
|
assert_equal 'widget', index.type
|
9
9
|
end
|
10
|
+
|
11
|
+
def test_disable
|
12
|
+
index = ElasticRecord::Index.new(Widget)
|
13
|
+
|
14
|
+
index.disable!
|
15
|
+
|
16
|
+
assert index.disabled
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_enable
|
20
|
+
index = ElasticRecord::Index.new(Widget)
|
21
|
+
|
22
|
+
index.disable!
|
23
|
+
index.enable!
|
24
|
+
|
25
|
+
refute index.disabled
|
26
|
+
end
|
10
27
|
end
|
data/test/support/widget.rb
CHANGED
@@ -4,6 +4,7 @@ class Widget
|
|
4
4
|
define_model_callbacks :save, :destroy
|
5
5
|
|
6
6
|
include ElasticRecord::Model
|
7
|
+
include ElasticRecord::Callbacks
|
7
8
|
|
8
9
|
self.elastic_index.mapping[:properties].update(color: {
|
9
10
|
type: 'string',
|
@@ -24,6 +25,10 @@ class Widget
|
|
24
25
|
instance_eval(&block)
|
25
26
|
end
|
26
27
|
end
|
28
|
+
|
29
|
+
def base_class
|
30
|
+
self
|
31
|
+
end
|
27
32
|
end
|
28
33
|
|
29
34
|
attr_accessor :id, :color
|