Class: MySQL::Search::Callbacks

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql/search/callbacks.rb

Overview

Manages callbacks for updating search indices.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_model_class, source_config) ⇒ Callbacks

Returns a new instance of Callbacks.



29
30
31
32
33
# File 'lib/mysql/search/callbacks.rb', line 29

def initialize(source_model_class, source_config)
  @source_model_class = source_model_class
  @callbacks_config = transform_source_config(source_config)
  @assigned = false
end

Instance Attribute Details

#assignedObject (readonly)

Returns the value of attribute assigned.



7
8
9
# File 'lib/mysql/search/callbacks.rb', line 7

def assigned
  @assigned
end

#callbacks_configObject (readonly)

Returns the value of attribute callbacks_config.



7
8
9
# File 'lib/mysql/search/callbacks.rb', line 7

def callbacks_config
  @callbacks_config
end

#source_model_classObject (readonly)

Returns the value of attribute source_model_class.



7
8
9
# File 'lib/mysql/search/callbacks.rb', line 7

def source_model_class
  @source_model_class
end

Class Method Details

.callback(source_model_class_name, associated, association_path, on_attributes_change) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mysql/search/callbacks.rb', line 9

def self.callback(source_model_class_name, associated, association_path, on_attributes_change)
  return unless enqueue_update?(associated, on_attributes_change)

  perform = ::MySQL::Search.update_asyncronously ? :perform_later : :perform_now

  ::MySQL::Search::Jobs::UpdaterJob.set(wait: 10.seconds).public_send(
    perform,
    source_model_class_name,
    associated.class.name,
    associated.id,
    association_path
  )
end

.enqueue_update?(associated, on_attributes_change) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/mysql/search/callbacks.rb', line 23

def self.enqueue_update?(associated, on_attributes_change)
  return false unless ::MySQL::Search.automatic_update

  on_attributes_change.any? { |attribute| associated.saved_change_to_attribute?(attribute) }
end

Instance Method Details

#assignObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mysql/search/callbacks.rb', line 35

def assign
  return if @assigned

  callbacks_config.each do |association_path, on_attributes_change|
    source_model_class_name = source_model_class.name
    associated_class(association_path).after_save do
      ::MySQL::Search::Callbacks.callback(source_model_class_name, self, association_path, on_attributes_change)
    end
  end

  @assigned = true
end