elastic_record 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'elastic_record'
5
- s.version = '0.4.0'
5
+ s.version = '0.4.1'
6
6
  s.summary = 'Use Elastic Search with your objects'
7
7
  s.description = 'Find your records with elastic search'
8
8
 
@@ -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 : record.default_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
@@ -2,8 +2,6 @@ module ElasticRecord
2
2
  module Model
3
3
  def self.included(base)
4
4
  base.class_eval do
5
- include Callbacks
6
-
7
5
  extend Connection, Searching, ClassMethods
8
6
  end
9
7
  end
@@ -26,7 +26,7 @@ class ElasticRecord::Index::DocumentsTest < MiniTest::Spec
26
26
  refute index.record_exists?('abc')
27
27
  end
28
28
 
29
- def test_bulk
29
+ def test_bulk_add
30
30
  record = Widget.new(id: 'abc', color: 'red')
31
31
 
32
32
  index.bulk_add [record]
@@ -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
@@ -13,4 +13,4 @@ class ElasticRecord::ModelTest < MiniTest::Spec
13
13
 
14
14
  assert_equal Widget, index.model
15
15
  end
16
- end
16
+ end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: