Module: Esse::ActiveRecord::Model::ClassMethods

Extended by:
Deprecations::Deprecate
Defined in:
lib/esse/active_record/model.rb

Instance Method Summary collapse

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, **options, &block)
  @esse_callbacks = esse_callbacks.dup
  cb_if = options.delete(:if)
  cb_unless = options.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, options, block]

    after_commit(on: event, if: if_enabled) do
      klass, options, block = self.class.esse_callbacks.fetch(index_repo_name).fetch(identifier)
      options[:repo] = Esse::ActiveRecord::Hooks.resolve_index_repository(index_repo_name)
      options[:block_result] = instance_exec(&block) if block.respond_to?(:call)
      instance = klass.new(**options)
      instance.call(self)
    end
  end

  Esse::ActiveRecord::Hooks.register_model(self)
ensure
  @esse_callbacks&.each_value { |v| v.freeze }&.freeze
end

#esse_callbacksObject



11
12
13
# File 'lib/esse/active_record/model.rb', line 11

def esse_callbacks
  @esse_callbacks ||= {}.freeze
end

#esse_index_reposObject



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.

Parameters:

  • index_repo_name (String)

    The path of index and repository name. For example a index with a single repository named ‘users` is `users`. And a index with multiple repositories named `animals` and `dog` as the repository name is `animals/dog`. For namespace, use `/` as the separator.

Raises:

  • (ArgumentError)

    when the repo and events are already registered

  • (ArgumentError)

    when the specified index have multiple repos



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, **options, &block)
  if with
    Array(on).each do |event|
      if on == :update
        esse_callback(index_repo_name, :indexing, on: event, with: with, **options, &block)
      else
        esse_callback(index_repo_name, :indexing, on: event, **options, &block)
      end
    end
  else
    esse_callback(index_repo_name, :indexing, on: on, **options, &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, **options, &block)
  index_callback(*args, **options, &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], **options, &block)
  options[:attribute_name] = attribute_name
  esse_callback(index_repo_name, :update_lazy_attribute, identifier_suffix: attribute_name.to_sym, on: on, **options, &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