esse-active_record 0.2.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -1
- data/Gemfile.lock +5 -5
- data/README.md +4 -4
- data/ci/Gemfile.rails-5.2.lock +3 -3
- data/ci/Gemfile.rails-6.0.lock +3 -3
- data/ci/Gemfile.rails-6.1.lock +3 -3
- data/ci/Gemfile.rails-7.0.lock +3 -3
- data/ci/Gemfile.rails-7.1.lock +3 -3
- data/lib/esse/active_record/callbacks/indexing_on_create.rb +16 -0
- data/lib/esse/active_record/callbacks/indexing_on_destroy.rb +18 -0
- data/lib/esse/active_record/callbacks/indexing_on_update.rb +55 -0
- data/lib/esse/active_record/callbacks/update_lazy_attribute.rb +27 -0
- data/lib/esse/active_record/callbacks.rb +64 -0
- data/lib/esse/active_record/hooks.rb +2 -2
- data/lib/esse/active_record/model.rb +56 -71
- data/lib/esse/active_record/version.rb +1 -1
- data/lib/esse/active_record.rb +1 -0
- metadata +9 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b48e48c6b27fee2e8612414bd9ba6730bbfd57ddc30b9f5ebadf5742c57d4be4
|
4
|
+
data.tar.gz: c1b74e88c8942aea5a72c58326c0051d57326ff6d6c61e80b723abd043dd74a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46019ab0f4d79af084a062fe41de8d1341b9261f0cf3651c94bb3e290db71a0f1794350fed8f2ef58f8dbc98910c2854e7a15fd5027cce0f1fd057ccda734b34
|
7
|
+
data.tar.gz: e2c5861b580b09bf968d04a984ab3e6d94d27c89a2386a84397ceabf9860237be85ec135c79c387c3ad4559e4cbeb1462f863604de01f1a9c047143df0f5448a
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
esse-active_record (0.2
|
4
|
+
esse-active_record (0.3.2)
|
5
5
|
activerecord (>= 4.2, < 8)
|
6
|
-
esse (>= 0.
|
6
|
+
esse (>= 0.3.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
@@ -39,7 +39,7 @@ GEM
|
|
39
39
|
elasticsearch-transport (7.17.10)
|
40
40
|
faraday (>= 1, < 3)
|
41
41
|
multi_json
|
42
|
-
esse (0.
|
42
|
+
esse (0.3.0)
|
43
43
|
multi_json
|
44
44
|
thor (>= 0.19)
|
45
45
|
esse-rspec (0.0.6)
|
@@ -122,7 +122,7 @@ GEM
|
|
122
122
|
standard-performance (1.0.1)
|
123
123
|
lint_roller (~> 1.0)
|
124
124
|
rubocop-performance (~> 1.16.0)
|
125
|
-
thor (1.3.
|
125
|
+
thor (1.3.1)
|
126
126
|
thread_safe (0.3.6)
|
127
127
|
tzinfo (1.2.11)
|
128
128
|
thread_safe (~> 0.1)
|
@@ -142,7 +142,7 @@ DEPENDENCIES
|
|
142
142
|
awesome_print
|
143
143
|
dotenv
|
144
144
|
elasticsearch (~> 7.17, >= 7.17.10)
|
145
|
-
esse (~> 0.
|
145
|
+
esse (~> 0.3.0)
|
146
146
|
esse-active_record!
|
147
147
|
esse-rspec (~> 0.0.6)
|
148
148
|
pry
|
data/README.md
CHANGED
@@ -154,7 +154,7 @@ end
|
|
154
154
|
|
155
155
|
### Indexing Callbacks
|
156
156
|
|
157
|
-
The `
|
157
|
+
The `index_callback` callback can be used to automaitcally index or delete documents after commit on create/update/destroy events.
|
158
158
|
|
159
159
|
```ruby
|
160
160
|
class UsersIndex < Esse::Index
|
@@ -173,9 +173,9 @@ class User < ApplicationRecord
|
|
173
173
|
# Using a index and repository as argument. Note that the index name is used instead of the
|
174
174
|
# of the constant name. it's so because index and model depends on each other should result in
|
175
175
|
# circular dependencies issues.
|
176
|
-
|
176
|
+
index_callback 'users_index:user'
|
177
177
|
# Using a block to direct a different object to be indexed
|
178
|
-
|
178
|
+
index_callback('organizations') { user.organization } # The `_index` suffix and repo name is optional on the index name
|
179
179
|
end
|
180
180
|
```
|
181
181
|
|
@@ -194,7 +194,7 @@ or by some specific list of index or index's repository
|
|
194
194
|
```ruby
|
195
195
|
Esse::ActiveRecord::Hooks.disable!(UsersIndex.repo)
|
196
196
|
Esse::ActiveRecord::Hooks.enable!(UsersIndex.repo)
|
197
|
-
Esse::ActiveRecord::Hooks.without_indexing(AccountsIndex UsersIndex.repo
|
197
|
+
Esse::ActiveRecord::Hooks.without_indexing(AccountsIndex, UsersIndex.repo) do
|
198
198
|
10.times { User.create! }
|
199
199
|
end
|
200
200
|
```
|
data/ci/Gemfile.rails-5.2.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
esse-active_record (0.2
|
4
|
+
esse-active_record (0.3.2)
|
5
5
|
activerecord (>= 4.2, < 8)
|
6
|
-
esse (>= 0.
|
6
|
+
esse (>= 0.3.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
@@ -39,7 +39,7 @@ GEM
|
|
39
39
|
elasticsearch-transport (7.17.10)
|
40
40
|
faraday (>= 1, < 3)
|
41
41
|
multi_json
|
42
|
-
esse (0.
|
42
|
+
esse (0.3.0)
|
43
43
|
multi_json
|
44
44
|
thor (>= 0.19)
|
45
45
|
esse-rspec (0.0.6)
|
data/ci/Gemfile.rails-6.0.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
esse-active_record (0.2
|
4
|
+
esse-active_record (0.3.2)
|
5
5
|
activerecord (>= 4.2, < 8)
|
6
|
-
esse (>= 0.
|
6
|
+
esse (>= 0.3.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
@@ -37,7 +37,7 @@ GEM
|
|
37
37
|
elasticsearch-transport (7.17.10)
|
38
38
|
faraday (>= 1, < 3)
|
39
39
|
multi_json
|
40
|
-
esse (0.
|
40
|
+
esse (0.3.0)
|
41
41
|
multi_json
|
42
42
|
thor (>= 0.19)
|
43
43
|
esse-rspec (0.0.6)
|
data/ci/Gemfile.rails-6.1.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
esse-active_record (0.2
|
4
|
+
esse-active_record (0.3.2)
|
5
5
|
activerecord (>= 4.2, < 8)
|
6
|
-
esse (>= 0.
|
6
|
+
esse (>= 0.3.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
@@ -37,7 +37,7 @@ GEM
|
|
37
37
|
elasticsearch-transport (7.17.10)
|
38
38
|
faraday (>= 1, < 3)
|
39
39
|
multi_json
|
40
|
-
esse (0.
|
40
|
+
esse (0.3.0)
|
41
41
|
multi_json
|
42
42
|
thor (>= 0.19)
|
43
43
|
esse-rspec (0.0.6)
|
data/ci/Gemfile.rails-7.0.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
esse-active_record (0.2
|
4
|
+
esse-active_record (0.3.2)
|
5
5
|
activerecord (>= 4.2, < 8)
|
6
|
-
esse (>= 0.
|
6
|
+
esse (>= 0.3.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
@@ -37,7 +37,7 @@ GEM
|
|
37
37
|
elasticsearch-transport (7.17.10)
|
38
38
|
faraday (>= 1, < 3)
|
39
39
|
multi_json
|
40
|
-
esse (0.
|
40
|
+
esse (0.3.0)
|
41
41
|
multi_json
|
42
42
|
thor (>= 0.19)
|
43
43
|
esse-rspec (0.0.6)
|
data/ci/Gemfile.rails-7.1.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
esse-active_record (0.2
|
4
|
+
esse-active_record (0.3.2)
|
5
5
|
activerecord (>= 4.2, < 8)
|
6
|
-
esse (>= 0.
|
6
|
+
esse (>= 0.3.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
@@ -47,7 +47,7 @@ GEM
|
|
47
47
|
elasticsearch-transport (7.17.10)
|
48
48
|
faraday (>= 1, < 3)
|
49
49
|
multi_json
|
50
|
-
esse (0.
|
50
|
+
esse (0.3.0)
|
51
51
|
multi_json
|
52
52
|
thor (>= 0.19)
|
53
53
|
esse-rspec (0.0.6)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Esse::ActiveRecord
|
4
|
+
module Callbacks
|
5
|
+
class IndexingOnCreate < Callback
|
6
|
+
def call(model)
|
7
|
+
record = block_result || model
|
8
|
+
document = repo.serialize(record)
|
9
|
+
repo.index.index(document, **options) if document
|
10
|
+
true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
register_callback(:indexing, :create, IndexingOnCreate)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Esse::ActiveRecord
|
4
|
+
module Callbacks
|
5
|
+
class IndexingOnDestroy < Callback
|
6
|
+
def call(model)
|
7
|
+
record = block_result || model
|
8
|
+
document = repo.serialize(record)
|
9
|
+
repo.index.delete(document, **options) if document
|
10
|
+
true
|
11
|
+
rescue Esse::Transport::NotFoundError
|
12
|
+
true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
register_callback(:indexing, :destroy, IndexingOnDestroy)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Esse::ActiveRecord
|
4
|
+
module Callbacks
|
5
|
+
class IndexingOnUpdate < Callback
|
6
|
+
attr_reader :update_with
|
7
|
+
|
8
|
+
def initialize(with: :index, **kwargs, &block)
|
9
|
+
@update_with = with
|
10
|
+
super(**kwargs, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(model)
|
14
|
+
record = block_result || model
|
15
|
+
|
16
|
+
document = repo.serialize(record)
|
17
|
+
return true unless document
|
18
|
+
|
19
|
+
update_document(document)
|
20
|
+
return true unless document.routing
|
21
|
+
|
22
|
+
prev_record = model.class.new(model.attributes.merge(model.previous_changes.transform_values(&:first))).tap(&:readonly!)
|
23
|
+
prev_document = repo.serialize(prev_record)
|
24
|
+
|
25
|
+
return true unless prev_document
|
26
|
+
return true if [prev_document.id, prev_document.routing].include?(nil)
|
27
|
+
return true if prev_document.routing == document.routing
|
28
|
+
return true if prev_document.id != document.id
|
29
|
+
|
30
|
+
begin
|
31
|
+
repo.index.delete(prev_document, **options)
|
32
|
+
rescue Esse::Transport::NotFoundError
|
33
|
+
end
|
34
|
+
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
def update_document(document)
|
41
|
+
if update_with == :update
|
42
|
+
begin
|
43
|
+
repo.index.update(document, **options)
|
44
|
+
rescue Esse::Transport::NotFoundError
|
45
|
+
repo.index.index(document, **options)
|
46
|
+
end
|
47
|
+
else
|
48
|
+
repo.index.index(document, **options)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
register_callback(:indexing, :update, IndexingOnUpdate)
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Esse::ActiveRecord
|
4
|
+
module Callbacks
|
5
|
+
class UpdateLazyAttribute < Callback
|
6
|
+
attr_reader :attribute_name
|
7
|
+
|
8
|
+
def initialize(attribute_name:, **kwargs, &block)
|
9
|
+
@attribute_name = attribute_name
|
10
|
+
super(**kwargs, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(model)
|
14
|
+
related_ids = Array(block_result || model.id)
|
15
|
+
return true if related_ids.empty?
|
16
|
+
|
17
|
+
repo.update_documents_attribute(attribute_name, *related_ids, **options)
|
18
|
+
|
19
|
+
true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
register_callback(:update_lazy_attribute, :create, UpdateLazyAttribute)
|
24
|
+
register_callback(:update_lazy_attribute, :update, UpdateLazyAttribute)
|
25
|
+
register_callback(:update_lazy_attribute, :destroy, UpdateLazyAttribute)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Esse
|
4
|
+
module ActiveRecord
|
5
|
+
class Callback
|
6
|
+
attr_reader :repo, :options, :block_result
|
7
|
+
|
8
|
+
def initialize(repo:, block_result: nil, **kwargs)
|
9
|
+
@repo = repo
|
10
|
+
@options = kwargs
|
11
|
+
@block_result = block_result
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(model)
|
15
|
+
raise NotImplementedError, 'You must implement #call method'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Callbacks
|
20
|
+
class << self
|
21
|
+
def to_h
|
22
|
+
@callbacks || {}.freeze
|
23
|
+
end
|
24
|
+
|
25
|
+
def register_callback(identifier, operation, callback_class)
|
26
|
+
unless callback_class < Esse::ActiveRecord::Callback
|
27
|
+
raise ArgumentError, 'callback_class must be a subclass of Esse::ActiveRecord::Callback'
|
28
|
+
end
|
29
|
+
|
30
|
+
key = :"#{identifier}_on_#{operation}"
|
31
|
+
|
32
|
+
@callbacks = @callbacks ? @callbacks.dup : {}
|
33
|
+
if @callbacks.key?(key)
|
34
|
+
raise ArgumentError, "callback #{identifier} for #{operation} operation already registered"
|
35
|
+
end
|
36
|
+
|
37
|
+
@callbacks[key] = callback_class
|
38
|
+
ensure
|
39
|
+
@callbacks&.freeze
|
40
|
+
end
|
41
|
+
|
42
|
+
def registered?(identifier, operation)
|
43
|
+
return false unless @callbacks
|
44
|
+
|
45
|
+
@callbacks.key?(:"#{identifier}_on_#{operation}")
|
46
|
+
end
|
47
|
+
|
48
|
+
def fetch!(identifier, operation)
|
49
|
+
key = :"#{identifier}_on_#{operation}"
|
50
|
+
if registered?(identifier, operation)
|
51
|
+
[key, @callbacks[key]]
|
52
|
+
else
|
53
|
+
raise ArgumentError, "callback #{identifier} for #{operation} operation not registered"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
require_relative 'callbacks/indexing_on_create'
|
62
|
+
require_relative 'callbacks/indexing_on_update'
|
63
|
+
require_relative 'callbacks/indexing_on_destroy'
|
64
|
+
require_relative 'callbacks/update_lazy_attribute'
|
@@ -82,7 +82,7 @@ module Esse
|
|
82
82
|
def ensure_registered_model_class!(model_class)
|
83
83
|
return if registered_model_class?(model_class)
|
84
84
|
|
85
|
-
raise ArgumentError, "Model class #{model_class} is not registered. The model should inherit from Esse::ActiveRecord::Model and have a `
|
85
|
+
raise ArgumentError, "Model class #{model_class} is not registered. The model should inherit from Esse::ActiveRecord::Model and have a `index_callback' callback defined"
|
86
86
|
end
|
87
87
|
|
88
88
|
# Check if the given model is enabled for indexing. If no repository is specified, all repositories will be checked.
|
@@ -147,7 +147,7 @@ module Esse
|
|
147
147
|
# Returns a list of all repositories for the given model
|
148
148
|
# @return [Array<Symbol>]
|
149
149
|
def model_repos(model_class)
|
150
|
-
expand_index_repos(*model_class.
|
150
|
+
expand_index_repos(*model_class.esse_callbacks.keys)
|
151
151
|
end
|
152
152
|
|
153
153
|
# Returns a list of all repositories for the given model
|
@@ -5,93 +5,68 @@ module Esse
|
|
5
5
|
module Model
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
|
-
def self.inherited(subclass)
|
9
|
-
super
|
10
|
-
subclass.esse_index_repos = esse_index_repos.dup
|
11
|
-
end
|
12
|
-
|
13
8
|
module ClassMethods
|
14
|
-
|
9
|
+
extend Esse::Deprecations::Deprecate
|
15
10
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
# For example a index with a single repository named `users` is `users`. And a index with
|
20
|
-
# multiple repositories named `animals` and `dog` as the repository name is `animals/dog`.
|
21
|
-
# For namespace, use `/` as the separator.
|
22
|
-
# @raise [ArgumentError] when the repo and events are already registered
|
23
|
-
# @raise [ArgumentError] when the specified index have multiple repos
|
24
|
-
def index_callbacks(index_repo_name, on: %i[create update destroy], **options, &block)
|
25
|
-
@esse_index_repos ||= {}
|
11
|
+
def esse_callbacks
|
12
|
+
@esse_callbacks ||= {}.freeze
|
13
|
+
end
|
26
14
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
15
|
+
def esse_callback(index_repo_name, operation_name, on: %i[create update destroy], **options, &block)
|
16
|
+
@esse_callbacks = esse_callbacks.dup
|
17
|
+
if_enabled = -> { Esse::ActiveRecord::Hooks.enabled?(index_repo_name) && Esse::ActiveRecord::Hooks.enabled_for_model?(self.class, index_repo_name) }
|
31
18
|
|
32
|
-
|
33
|
-
|
34
|
-
record: block || -> { self },
|
35
|
-
options: options,
|
36
|
-
}
|
19
|
+
Array(on).each do |event|
|
20
|
+
identifier, klass = Esse::ActiveRecord::Callbacks.fetch!(operation_name, event)
|
37
21
|
|
38
|
-
|
22
|
+
if @esse_callbacks.dig(index_repo_name, identifier)
|
23
|
+
raise ArgumentError, format('index repository %<name>p already registered %<op>s operation', name: index_repo_name, op: operation_name)
|
24
|
+
end
|
25
|
+
|
26
|
+
@esse_callbacks[index_repo_name] = @esse_callbacks[index_repo_name]&.dup || {}
|
27
|
+
@esse_callbacks[index_repo_name][identifier] = [klass, options, block]
|
39
28
|
|
40
|
-
if_enabled = -> { Esse::ActiveRecord::Hooks.enabled?(index_repo_name) && Esse::ActiveRecord::Hooks.enabled_for_model?(self.class, index_repo_name) }
|
41
|
-
(on & %i[create]).each do |event|
|
42
29
|
after_commit(on: event, if: if_enabled) do
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
repo.index.index(document, **opts[:options]) if document
|
49
|
-
true
|
30
|
+
klass, options, block = self.class.esse_callbacks.fetch(index_repo_name).fetch(identifier)
|
31
|
+
options[:repo] = Esse::ActiveRecord::Hooks.resolve_index_repository(index_repo_name)
|
32
|
+
options[:block_result] = instance_exec(&block) if block.respond_to?(:call)
|
33
|
+
instance = klass.new(**options)
|
34
|
+
instance.call(self)
|
50
35
|
end
|
51
36
|
end
|
52
|
-
(on & %i[update]).each do |event|
|
53
|
-
after_commit(on: event, if: if_enabled) do
|
54
|
-
opts = self.class.esse_index_repos.fetch(index_repo_name).fetch(operation_name)
|
55
|
-
record = opts.fetch(:record)
|
56
|
-
record = instance_exec(&record) if record.respond_to?(:call)
|
57
|
-
repo = Esse::ActiveRecord::Hooks.resolve_index_repository(index_repo_name)
|
58
|
-
document = repo.serialize(record)
|
59
|
-
next true unless document
|
60
|
-
|
61
|
-
repo.index.index(document, **opts[:options])
|
62
|
-
next true unless document.routing
|
63
|
-
|
64
|
-
prev_record = self.class.new(attributes.merge(previous_changes.transform_values(&:first))).tap(&:readonly!)
|
65
|
-
prev_document = repo.serialize(prev_record)
|
66
37
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
38
|
+
Esse::ActiveRecord::Hooks.register_model(self)
|
39
|
+
ensure
|
40
|
+
@esse_callbacks&.each_value { |v| v.freeze }&.freeze
|
41
|
+
end
|
71
42
|
|
72
|
-
|
73
|
-
|
74
|
-
|
43
|
+
# Define callback for create/update/delete elasticsearch index document after model commit.
|
44
|
+
#
|
45
|
+
# @param [String] index_repo_name The path of index and repository name.
|
46
|
+
# For example a index with a single repository named `users` is `users`. And a index with
|
47
|
+
# multiple repositories named `animals` and `dog` as the repository name is `animals/dog`.
|
48
|
+
# For namespace, use `/` as the separator.
|
49
|
+
# @raise [ArgumentError] when the repo and events are already registered
|
50
|
+
# @raise [ArgumentError] when the specified index have multiple repos
|
51
|
+
def index_callback(index_repo_name, on: %i[create update destroy], with: nil, **options, &block)
|
52
|
+
if with
|
53
|
+
Array(on).each do |event|
|
54
|
+
if on == :update
|
55
|
+
esse_callback(index_repo_name, :indexing, on: event, with: with, **options, &block)
|
56
|
+
else
|
57
|
+
esse_callback(index_repo_name, :indexing, on: event, **options, &block)
|
75
58
|
end
|
76
|
-
|
77
|
-
true
|
78
|
-
end
|
79
|
-
end
|
80
|
-
(on & %i[destroy]).each do |event|
|
81
|
-
after_commit(on: event, if: if_enabled) do
|
82
|
-
opts = self.class.esse_index_repos.fetch(index_repo_name).fetch(operation_name)
|
83
|
-
record = opts.fetch(:record)
|
84
|
-
record = instance_exec(&record) if record.respond_to?(:call)
|
85
|
-
repo = Esse::ActiveRecord::Hooks.resolve_index_repository(index_repo_name)
|
86
|
-
document = repo.serialize(record)
|
87
|
-
repo.index.delete(document, **opts[:options]) if document
|
88
|
-
true
|
89
|
-
rescue Esse::Transport::NotFoundError
|
90
|
-
true
|
91
59
|
end
|
60
|
+
else
|
61
|
+
esse_callback(index_repo_name, :indexing, on: on, **options, &block)
|
92
62
|
end
|
93
63
|
end
|
94
64
|
|
65
|
+
def update_lazy_attribute_callback(index_repo_name, attribute_name, on: %i[create update destroy], **options, &block)
|
66
|
+
options[:attribute_name] = attribute_name
|
67
|
+
esse_callback(index_repo_name, :update_lazy_attribute, on: on, **options, &block)
|
68
|
+
end
|
69
|
+
|
95
70
|
# Disable indexing for the block execution on model level
|
96
71
|
# Example:
|
97
72
|
# User.without_indexing { }
|
@@ -101,6 +76,16 @@ module Esse
|
|
101
76
|
yield
|
102
77
|
end
|
103
78
|
end
|
79
|
+
|
80
|
+
def index_callbacks(*args, **options, &block)
|
81
|
+
index_callback(*args, **options, &block)
|
82
|
+
end
|
83
|
+
deprecate :index_callbacks, :index_callback, 2024, 12
|
84
|
+
|
85
|
+
def esse_index_repos
|
86
|
+
esse_callbacks
|
87
|
+
end
|
88
|
+
deprecate :esse_index_repos, :esse_callbacks, 2024, 12
|
104
89
|
end
|
105
90
|
end
|
106
91
|
end
|
data/lib/esse/active_record.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: esse-active_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcos G. Zimmermann
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-07-
|
11
|
+
date: 2024-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: esse
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.3.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 0.3.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activerecord
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -222,6 +222,11 @@ files:
|
|
222
222
|
- ci/Gemfile.rails-7.1
|
223
223
|
- ci/Gemfile.rails-7.1.lock
|
224
224
|
- lib/esse/active_record.rb
|
225
|
+
- lib/esse/active_record/callbacks.rb
|
226
|
+
- lib/esse/active_record/callbacks/indexing_on_create.rb
|
227
|
+
- lib/esse/active_record/callbacks/indexing_on_destroy.rb
|
228
|
+
- lib/esse/active_record/callbacks/indexing_on_update.rb
|
229
|
+
- lib/esse/active_record/callbacks/update_lazy_attribute.rb
|
225
230
|
- lib/esse/active_record/collection.rb
|
226
231
|
- lib/esse/active_record/hooks.rb
|
227
232
|
- lib/esse/active_record/model.rb
|