elasticsearch-model-extensions 0.2.0 → 0.2.2
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.
- checksums.yaml +4 -4
- data/lib/elasticsearch/model/extensions/callback.rb +31 -0
- data/lib/elasticsearch/model/extensions/delayed_job/document_job.rb +2 -0
- data/lib/elasticsearch/model/extensions/delayed_job.rb +1 -0
- data/lib/elasticsearch/model/extensions/destroy_callback.rb +11 -13
- data/lib/elasticsearch/model/extensions/update_callback.rb +24 -26
- data/lib/elasticsearch/model/extensions/version.rb +1 -1
- data/spec/setup/articles_with_comments_and_delayed_jobs.rb +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc88b24aa703c53b9c35f7f0dfc72c291299d861
|
4
|
+
data.tar.gz: 8ee00b4d40f76173ec408ea6068d4a697a3fb9b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 181f6efceba36b6a0b032f06a9e612e51d8a371f3c88cc780b641262b7a59149ebbf2dd1528c6bf4f864823c7e6bd6773a4a4f9fada2eccc6e5d7e4248c364d5
|
7
|
+
data.tar.gz: 139e8c22de2c2be86dc885b1a54552f940aac3725ec88bc7d6a137efd50cd253c890f37268a2da7e78ddcd79d75b4f8972855cffa674fb73a17361bbdb074aca
|
@@ -12,6 +12,37 @@ module Elasticsearch
|
|
12
12
|
def config
|
13
13
|
@config
|
14
14
|
end
|
15
|
+
|
16
|
+
def with_error_logging
|
17
|
+
begin
|
18
|
+
yield
|
19
|
+
rescue => e
|
20
|
+
log "An error occured while calling Elasticsearch::Model::Extensions::#{self.class.name}#after_commit"
|
21
|
+
log e.message
|
22
|
+
log e.backtrace.join("\n")
|
23
|
+
end
|
24
|
+
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
def update_for_records(*records)
|
29
|
+
field_to_update = config.field_to_update
|
30
|
+
optionally_delayed = config.optionally_delayed
|
31
|
+
block = config.block
|
32
|
+
|
33
|
+
records.map(&:reload).map(&optionally_delayed).each do |t|
|
34
|
+
log "Indexing #{t.class} id=#{t.id} fields=#{[*field_to_update].join(', ')}"
|
35
|
+
block.call(t, [*field_to_update])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def log(message)
|
40
|
+
if defined?(::Rails.logger.warn)
|
41
|
+
::Rails.logger.warn message
|
42
|
+
else
|
43
|
+
warn message
|
44
|
+
end
|
45
|
+
end
|
15
46
|
end
|
16
47
|
end
|
17
48
|
end
|
@@ -5,23 +5,21 @@ module Elasticsearch
|
|
5
5
|
module Extensions
|
6
6
|
class DestroyCallback < Callback
|
7
7
|
def after_commit(record)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
block = config.block
|
8
|
+
with_error_logging do
|
9
|
+
records_to_update_documents = config.records_to_update_documents
|
10
|
+
only_if = config.only_if
|
11
|
+
callback = self
|
13
12
|
|
14
|
-
|
15
|
-
|
13
|
+
record.instance_eval do
|
14
|
+
return unless only_if.call(self)
|
16
15
|
|
17
|
-
|
16
|
+
target = records_to_update_documents.call(self)
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
if target.respond_to? :each
|
19
|
+
callback.update_for_records(*target)
|
20
|
+
else
|
21
|
+
callback.update_for_records(target)
|
22
22
|
end
|
23
|
-
else
|
24
|
-
optionally_delayed.call(target.reload).partially_update_document(field_to_update)
|
25
23
|
end
|
26
24
|
end
|
27
25
|
end
|
@@ -5,36 +5,34 @@ module Elasticsearch
|
|
5
5
|
module Extensions
|
6
6
|
class UpdateCallback < Callback
|
7
7
|
def after_commit(record)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
block = config.block
|
8
|
+
with_error_logging do
|
9
|
+
records_to_update_documents = config.records_to_update_documents
|
10
|
+
only_if = config.only_if
|
11
|
+
callback = self
|
13
12
|
|
14
|
-
|
15
|
-
|
13
|
+
record.instance_eval do
|
14
|
+
return unless only_if.call(self) && index_update_required?
|
16
15
|
|
17
|
-
|
16
|
+
target = records_to_update_documents.call(self)
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
18
|
+
if target.respond_to? :each
|
19
|
+
# `reload` required to ensure that the outer record is up-to-date with changes
|
20
|
+
# when `self` is an instance of a `through` model.
|
21
|
+
#
|
22
|
+
# Imagine the case where we have an association containing:
|
23
|
+
#
|
24
|
+
# `article has_many comments through article_comments`
|
25
|
+
#
|
26
|
+
# and:
|
27
|
+
#
|
28
|
+
# `article_comments belongs_to article`
|
29
|
+
#
|
30
|
+
# Here, `article_comment.article` may contain outdated `comments` because `article_comment.article`
|
31
|
+
# won't be notified with changes in `article_comments` thus won't reload `comments` automatically.
|
32
|
+
callback.update_for_records(*target)
|
33
|
+
else
|
34
|
+
callback.update_for_records(target)
|
35
35
|
end
|
36
|
-
else
|
37
|
-
optionally_delayed.call(target.reload).partially_update_document(field_to_update)
|
38
36
|
end
|
39
37
|
end
|
40
38
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elasticsearch-model-extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yusuke KUOKA
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|