Class: ActiveRecord::Associations::Builder::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_reindex/association.rb

Class Method Summary collapse

Class Method Details

.define_callbacks(model, reflection) ⇒ Object

This method monkeypatches ActiveRecord define_callbacks to add reindex callbacks if corresponding option specified if reindex; true - add syncronous callback to reindex associated records if reindex: :async - add asyncronous callback to reindex associated records



38
39
40
41
42
43
44
45
46
47
# File 'lib/activerecord_reindex/association.rb', line 38

def define_callbacks(model, reflection)
  original_define_callbacks(model, reflection)
  if reflection.reindex_sync?
    add_reindex_callback(model, reflection, async: false)
    model.sync_reindexable_reflections += [reflection]
  elsif reflection.reindex_async?
    add_reindex_callback(model, reflection, async: true)
    model.async_reindexable_reflections += [reflection]
  end
end

.original_define_callbacksObject



32
# File 'lib/activerecord_reindex/association.rb', line 32

alias original_define_callbacks define_callbacks

.original_valid_options_ar_reindexObject



20
# File 'lib/activerecord_reindex/association.rb', line 20

alias original_valid_options_ar_reindex valid_options

.valid_options(*args) ⇒ Object

This method monkey patches ActiveRecord valid_options to add one more valid option :reindex Examples:

belongs_to :tag, reindex: true
belongs_to :tagging, reindex: :async
has_many :tags, reindex: async
has_many :tags, through: :taggings, reindex: true


28
29
30
# File 'lib/activerecord_reindex/association.rb', line 28

def valid_options(*args)
  original_valid_options_ar_reindex(*args) + [:reindex]
end