-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lazy update attributes callback (#7)
* chore: use candidate release of esse * chore: rename index_callbacks to index_callback * chore: rename esse_index_repos to esse_callbacks * chore: deprecate renamed methods * chore: group deprecated methods * chore: expanding the existing callback to support different callback types * feat: refactoring indexing callback by moving them to a callback repository * chore: renaming :index to :indexing * chore: refactoring specs * chore: add tests to the esse_callback * chore: add tests to the update_lazy_attribute_callback callback * chore: add tests to update_lazy_attribute_callback callback * chore: install esse from rubygems * chore: fix rubocop offenses * chore: fix rubocop offenses
- Loading branch information
Showing
28 changed files
with
1,151 additions
and
499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
module Esse | ||
module ActiveRecord | ||
class Callback | ||
attr_reader :repo, :options, :block_result | ||
|
||
def initialize(repo:, block_result: nil, **kwargs) | ||
@repo = repo | ||
@options = kwargs | ||
@block_result = block_result | ||
end | ||
|
||
def call(model) | ||
raise NotImplementedError, 'You must implement #call method' | ||
end | ||
end | ||
|
||
module Callbacks | ||
class << self | ||
def to_h | ||
@callbacks || {}.freeze | ||
end | ||
|
||
def register_callback(identifier, operation, callback_class) | ||
unless callback_class < Esse::ActiveRecord::Callback | ||
raise ArgumentError, 'callback_class must be a subclass of Esse::ActiveRecord::Callback' | ||
end | ||
|
||
key = :"#{identifier}_on_#{operation}" | ||
|
||
@callbacks = @callbacks ? @callbacks.dup : {} | ||
if @callbacks.key?(key) | ||
raise ArgumentError, "callback #{identifier} for #{operation} operation already registered" | ||
end | ||
|
||
@callbacks[key] = callback_class | ||
ensure | ||
@callbacks&.freeze | ||
end | ||
|
||
def registered?(identifier, operation) | ||
return false unless @callbacks | ||
|
||
@callbacks.key?(:"#{identifier}_on_#{operation}") | ||
end | ||
|
||
def fetch!(identifier, operation) | ||
key = :"#{identifier}_on_#{operation}" | ||
if registered?(identifier, operation) | ||
[key, @callbacks[key]] | ||
else | ||
raise ArgumentError, "callback #{identifier} for #{operation} operation not registered" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
require_relative 'callbacks/indexing_on_create' | ||
require_relative 'callbacks/indexing_on_update' | ||
require_relative 'callbacks/indexing_on_destroy' | ||
require_relative 'callbacks/update_lazy_attribute' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Esse::ActiveRecord | ||
module Callbacks | ||
class IndexingOnCreate < Callback | ||
def call(model) | ||
record = block_result || model | ||
document = repo.serialize(record) | ||
repo.index.index(document, **options) if document | ||
true | ||
end | ||
end | ||
|
||
register_callback(:indexing, :create, IndexingOnCreate) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Esse::ActiveRecord | ||
module Callbacks | ||
class IndexingOnDestroy < Callback | ||
def call(model) | ||
record = block_result || model | ||
document = repo.serialize(record) | ||
repo.index.delete(document, **options) if document | ||
true | ||
rescue Esse::Transport::NotFoundError | ||
true | ||
end | ||
end | ||
|
||
register_callback(:indexing, :destroy, IndexingOnDestroy) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module Esse::ActiveRecord | ||
module Callbacks | ||
class IndexingOnUpdate < Callback | ||
def call(model) | ||
record = block_result || model | ||
|
||
document = repo.serialize(record) | ||
return true unless document | ||
|
||
repo.index.index(document, **options) | ||
return true unless document.routing | ||
|
||
prev_record = model.class.new(model.attributes.merge(model.previous_changes.transform_values(&:first))).tap(&:readonly!) | ||
prev_document = repo.serialize(prev_record) | ||
|
||
return true unless prev_document | ||
return true if [prev_document.id, prev_document.routing].include?(nil) | ||
return true if prev_document.routing == document.routing | ||
return true if prev_document.id != document.id | ||
|
||
begin | ||
repo.index.delete(prev_document, **options) | ||
rescue Esse::Transport::NotFoundError | ||
end | ||
|
||
true | ||
end | ||
end | ||
|
||
register_callback(:indexing, :update, IndexingOnUpdate) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Esse::ActiveRecord | ||
module Callbacks | ||
class UpdateLazyAttribute < Callback | ||
attr_reader :attribute_name | ||
|
||
def initialize(attribute_name:, **kwargs, &block) | ||
@attribute_name = attribute_name | ||
super(**kwargs, &block) | ||
end | ||
|
||
def call(model) | ||
related_ids = Array(block_result || model.id) | ||
return true if related_ids.empty? | ||
|
||
repo.update_documents_attribute(attribute_name, *related_ids, **options) | ||
|
||
true | ||
end | ||
end | ||
|
||
register_callback(:update_lazy_attribute, :create, UpdateLazyAttribute) | ||
register_callback(:update_lazy_attribute, :update, UpdateLazyAttribute) | ||
register_callback(:update_lazy_attribute, :destroy, UpdateLazyAttribute) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.