Module: Esse::ActiveRecord::Model::ClassMethods
- Extended by:
- Deprecations::Deprecate
- Defined in:
- lib/esse/active_record/model.rb
Instance Method Summary collapse
- #esse_callback(index_repo_name, operation_name, on: %i[create update destroy],, identifier_suffix: nil, **options, &block) ⇒ Object
- #esse_callbacks ⇒ Object
- #esse_index_repos ⇒ Object
-
#index_callback(index_repo_name, on: %i[create update destroy],, with: nil, **options, &block) ⇒ Object
Define callback for create/update/delete elasticsearch index document after model commit.
- #index_callbacks(*args, **options, &block) ⇒ Object
- #update_lazy_attribute_callback(index_repo_name, attribute_name, on: %i[create update destroy],, **options, &block) ⇒ Object
-
#without_indexing(*repos) ⇒ Object
Disable indexing for the block execution on model level Example: User.without_indexing { } User.without_indexing(UsersIndex, AccountsIndex::User) { }.
Instance Method Details
#esse_callback(index_repo_name, operation_name, on: %i[create update destroy],, identifier_suffix: nil, **options, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/esse/active_record/model.rb', line 15 def esse_callback(index_repo_name, operation_name, on: %i[create update destroy], identifier_suffix: nil, **, &block) @esse_callbacks = esse_callbacks.dup cb_if = .delete(:if) cb_unless = .delete(:unless) if_enabled = -> { (cb_if.nil? || (cb_if.respond_to?(:call) ? instance_exec(&cb_if) : send(cb_if))) && (cb_unless.nil? || (cb_unless.respond_to?(:call) ? !instance_exec(&cb_unless) : !send(cb_unless))) && Esse::ActiveRecord::Hooks.enabled?(index_repo_name) && Esse::ActiveRecord::Hooks.enabled_for_model?(self.class, index_repo_name) } Array(on).each do |event| identifier, klass = Esse::ActiveRecord::Callbacks.fetch!(operation_name, event) if identifier_suffix identifier = :"#{identifier}_#{identifier_suffix}" end if @esse_callbacks.dig(index_repo_name, identifier) raise ArgumentError, format('index repository %<name>p already registered %<op>s operation', name: index_repo_name, op: operation_name) end @esse_callbacks[index_repo_name] = @esse_callbacks[index_repo_name]&.dup || {} @esse_callbacks[index_repo_name][identifier] = [klass, , block] after_commit(on: event, if: if_enabled) do klass, , block = self.class.esse_callbacks.fetch(index_repo_name).fetch(identifier) [:repo] = Esse::ActiveRecord::Hooks.resolve_index_repository(index_repo_name) [:block_result] = instance_exec(&block) if block.respond_to?(:call) instance = klass.new(**) instance.call(self) end end Esse::ActiveRecord::Hooks.register_model(self) ensure @esse_callbacks&.each_value { |v| v.freeze }&.freeze end |
#esse_callbacks ⇒ Object
11 12 13 |
# File 'lib/esse/active_record/model.rb', line 11 def esse_callbacks @esse_callbacks ||= {}.freeze end |
#esse_index_repos ⇒ Object
95 96 97 |
# File 'lib/esse/active_record/model.rb', line 95 def esse_index_repos esse_callbacks end |
#index_callback(index_repo_name, on: %i[create update destroy],, with: nil, **options, &block) ⇒ Object
Define callback for create/update/delete elasticsearch index document after model commit.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/esse/active_record/model.rb', line 61 def index_callback(index_repo_name, on: %i[create update destroy], with: nil, **, &block) if with Array(on).each do |event| if on == :update esse_callback(index_repo_name, :indexing, on: event, with: with, **, &block) else esse_callback(index_repo_name, :indexing, on: event, **, &block) end end else esse_callback(index_repo_name, :indexing, on: on, **, &block) end end |
#index_callbacks(*args, **options, &block) ⇒ Object
90 91 92 |
# File 'lib/esse/active_record/model.rb', line 90 def index_callbacks(*args, **, &block) index_callback(*args, **, &block) end |
#update_lazy_attribute_callback(index_repo_name, attribute_name, on: %i[create update destroy],, **options, &block) ⇒ Object
75 76 77 78 |
# File 'lib/esse/active_record/model.rb', line 75 def update_lazy_attribute_callback(index_repo_name, attribute_name, on: %i[create update destroy], **, &block) [:attribute_name] = attribute_name esse_callback(index_repo_name, :update_lazy_attribute, identifier_suffix: attribute_name.to_sym, on: on, **, &block) end |
#without_indexing(*repos) ⇒ Object
Disable indexing for the block execution on model level Example:
User.without_indexing { }
User.without_indexing(UsersIndex, AccountsIndex::User) { }
84 85 86 87 88 |
# File 'lib/esse/active_record/model.rb', line 84 def without_indexing(*repos) Esse::ActiveRecord::Hooks.without_indexing_for_model(self, *repos) do yield end end |