Skip to content

Commit ee24edc

Browse files
committed
[MODEL] Added Elasticsearch::Model::Callbacks module to automatically update the index by using model callbacks
The behaviour is opt-in -- you have to include the module so the callbacks are registered. Example: class Article < ActiveRecord::Base include Elasticsearch::Model include Elasticsearch::Model::Callbacks end Article.create title: 'New Record' (0.3ms) begin transaction SQL (0.3ms) INSERT INTO "articles" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", 2013-11-12 18:21:15 UTC], ["title", "New Record"], ["updated_at", 2013-11-12 18:21:15 UTC]] (0.1ms) commit transaction 2013-11-12 19:21:15 +0100: PUT http://localhost:9200/articles/article/4 [status:201, request:0.005s, query:n/a] 2013-11-12 19:21:15 +0100: > {"id":4,"title":"New Record","published_at":null,"created_at":"2013-11-12T18:21:15Z","updated_at":"2013-11-12T18:21:15Z"} 2013-11-12 19:21:15 +0100: < {"ok":true,"_index":"articles","_type":"article","_id":"4","_version":1} # => #<Article id: 4, title: "New Record", published_at: nil, created_at: "2013-11-12 18:21:15", updated_at: "2013-11-12 18:21:15">
1 parent 0495e27 commit ee24edc

File tree

6 files changed

+39
-0
lines changed

6 files changed

+39
-0
lines changed

elasticsearch-model/lib/elasticsearch/model.rb

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require 'elasticsearch/model/naming'
1818
require 'elasticsearch/model/serializing'
1919
require 'elasticsearch/model/searching'
20+
require 'elasticsearch/model/callbacks'
2021

2122
require 'elasticsearch/model/proxy'
2223

elasticsearch-model/lib/elasticsearch/model/adapter.rb

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def records_mixin
2424
adapter.const_get(:Records)
2525
end
2626

27+
def callbacks_mixin
28+
adapter.const_get(:Callbacks)
29+
end
30+
2731
def adapter
2832
@adapter ||= begin
2933
self.class.adapters.find( lambda {[]} ) { |name, condition| condition.call(klass) }.first \

elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ def order(*args)
4747
end
4848
end
4949

50+
module Callbacks
51+
def self.included(base)
52+
base.class_eval do
53+
after_commit lambda { __elasticsearch__.index_document }, on: [:create]
54+
after_commit lambda { __elasticsearch__.update_document }, on: [:update]
55+
after_commit lambda { __elasticsearch__.delete_document }, on: [:destroy]
56+
end
57+
end
58+
end
59+
5060
end
5161

5262
end

elasticsearch-model/lib/elasticsearch/model/adapters/default.rb

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def records
1212
end
1313
end
1414

15+
module Callbacks
16+
# noop
17+
end
18+
1519
end
1620
end
1721
end

elasticsearch-model/lib/elasticsearch/model/adapters/mongoid.rb

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ def records
3535
end
3636
end
3737

38+
module Callbacks
39+
def self.included(base)
40+
base.after_create { |document| document.__elasticsearch__.index_document }
41+
base.after_update { |document| document.__elasticsearch__.update_document }
42+
base.after_destroy { |document| document.__elasticsearch__.delete_document }
43+
end
44+
end
45+
3846
end
3947

4048
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Elasticsearch
2+
module Model
3+
module Callbacks
4+
5+
def self.included(base)
6+
adapter = Adapter.from_class(base)
7+
base.__send__ :include, adapter.callbacks_mixin
8+
end
9+
10+
end
11+
end
12+
end

0 commit comments

Comments
 (0)