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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 147bafb31fb754df6328f7fa3c2e27a5af454d06
4
- data.tar.gz: dc047cf6c1423e9b3a45682737cee36869389f94
3
+ metadata.gz: cc88b24aa703c53b9c35f7f0dfc72c291299d861
4
+ data.tar.gz: 8ee00b4d40f76173ec408ea6068d4a697a3fb9b0
5
5
  SHA512:
6
- metadata.gz: 05d3f75d719269e736958c19e64cf3c19c16a3a89562f73e71c9c925023526dbc755487c67cd1d001ba2adc235ed765514f3dccd7d516641d3201c1c84ce8041
7
- data.tar.gz: 33b5d02632d47fa7584ede224f6692c8f12604cd6f1ffff16c3d316e732fb4d26fef182ffc5cd1880b25e46860859b68915750540334f84e0e22163e1290f60e
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,6 +5,8 @@ module Elasticsearch
5
5
  module Extensions
6
6
  module DelayedJob
7
7
 
8
+ require 'delayed_job'
9
+
8
10
  class DocumentJob
9
11
  def initialize(params)
10
12
  record = params[:record]
@@ -1,3 +1,4 @@
1
+ require 'delayed_job'
1
2
  require_relative 'delayed_job/index_document_job'
2
3
  require_relative 'delayed_job/partially_update_document_job'
3
4
  require_relative 'delayed_job/delete_document_job'
@@ -5,23 +5,21 @@ module Elasticsearch
5
5
  module Extensions
6
6
  class DestroyCallback < Callback
7
7
  def after_commit(record)
8
- field_to_update = config.field_to_update
9
- records_to_update_documents = config.records_to_update_documents
10
- optionally_delayed = config.optionally_delayed
11
- only_if = config.only_if
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
- record.instance_eval do
15
- return unless only_if.call(self)
13
+ record.instance_eval do
14
+ return unless only_if.call(self)
16
15
 
17
- target = records_to_update_documents.call(self)
16
+ target = records_to_update_documents.call(self)
18
17
 
19
- if target.respond_to? :each
20
- target.map(&:reload).map(&optionally_delayed).each do |t|
21
- block.call(t, [*field_to_update])
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
- field_to_update = config.field_to_update
9
- records_to_update_documents = config.records_to_update_documents
10
- optionally_delayed = config.optionally_delayed
11
- only_if = config.only_if
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
- record.instance_eval do
15
- return unless only_if.call(self) && index_update_required?
13
+ record.instance_eval do
14
+ return unless only_if.call(self) && index_update_required?
16
15
 
17
- target = records_to_update_documents.call(self)
16
+ target = records_to_update_documents.call(self)
18
17
 
19
- if target.respond_to? :each
20
- # `reload` required to ensure that the outer record is up-to-date with changes
21
- # when `self` is an instance of a `through` model.
22
- #
23
- # Imagine the case where we have an association containing:
24
- #
25
- # `article has_many comments through article_comments`
26
- #
27
- # and:
28
- #
29
- # `article_comments belongs_to article`
30
- #
31
- # Here, `article_comment.article` may contain outdated `comments` because `article_comment.article`
32
- # won't be notified with changes in `article_comments` thus won't reload `comments` automatically.
33
- target.map(&:reload).map(&optionally_delayed).each do |t|
34
- block.call(t, [*field_to_update])
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
@@ -1,7 +1,7 @@
1
1
  module Elasticsearch
2
2
  module Model
3
3
  module Extensions
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.2"
5
5
  end
6
6
  end
7
7
  end
@@ -1,5 +1,7 @@
1
1
  load 'setup/undefine.rb'
2
2
 
3
+ require 'delayed_job_active_record'
4
+
3
5
  require 'elasticsearch/model/extensions/all'
4
6
  require 'elasticsearch/model/extensions/delayed_job'
5
7
 
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.0
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-10-30 00:00:00.000000000 Z
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler