Module: ActiveRecord::BatchTouching

Extended by:
ActiveSupport::Concern
Defined in:
lib/activerecord/batch_touching.rb,
lib/activerecord/batch_touching/state.rb

Overview

Active Record Batch Touching

Defined Under Namespace

Classes: State

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_touchesObject



114
115
116
117
118
119
120
121
# File 'lib/activerecord/batch_touching.rb', line 114

def apply_touches
  batched_touches&.each do |(klass, columns), records|
    records.reject!(&:destroyed?)
    touch_records klass, columns, records, batched_touches_time
  end

  reset!
end

.batch_touching?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/activerecord/batch_touching.rb', line 106

def batch_touching?
  states.present? && !disabled?
end

.disableObject

Disable batch touching for a block



79
80
81
82
83
84
# File 'lib/activerecord/batch_touching.rb', line 79

def disable
  Thread.current[:batch_touching_disabled] = true
  yield
ensure
  Thread.current[:batch_touching_disabled] = false
end

.disable!Object

Disable batch touching globally



69
70
71
# File 'lib/activerecord/batch_touching.rb', line 69

def disable!
  @disabled = true
end

.disabled?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/activerecord/batch_touching.rb', line 86

def disabled?
  Thread.current[:batch_touching_disabled] || @disabled
end

.enable!Object

Enable batch touching globally



74
75
76
# File 'lib/activerecord/batch_touching.rb', line 74

def enable!
  @disabled = false
end

.should_apply_touches?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/activerecord/batch_touching.rb', line 110

def should_apply_touches?
  batched_touches.present?
end

.start(requires_new:) ⇒ Object

Start batching all touches. When done, apply them. (Unless nested.)



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/activerecord/batch_touching.rb', line 91

def start(requires_new:)
  states.push State.new
  yield.tap do
    gather_touches if states.length == 1
  end
ensure
  merge_transactions unless $! && requires_new

  # Decrement nesting even if +gather_touches+ raised an error. To ensure the stack of States
  # is empty after the top-level transaction exits.
  states.pop
end

Instance Method Details

#touch(*names, time: nil) ⇒ Object

Override ActiveRecord::Base#touch. If currently batching touches, always return true because there’s no way to tell if the write would have failed.



57
58
59
60
61
62
63
64
65
# File 'lib/activerecord/batch_touching.rb', line 57

def touch(*names, time: nil)
  if BatchTouching.batch_touching? && !no_touching?
    add_to_transaction
    BatchTouching.add_record(self, names)
    true
  else
    super
  end
end

#touch_later(*names) ⇒ Object

Override ActiveRecord::Base#touch_later. This will effectively disable the current built-in mechanism AR uses to delay touching in favor of our method of batch touching.



51
52
53
# File 'lib/activerecord/batch_touching.rb', line 51

def touch_later(*names)
  BatchTouching.batch_touching? ? touch(*names) : super
end