Class: ActiveRecordChangeMatchers::TimestampStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_change_matchers/strategies/timestamp_strategy.rb

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ TimestampStrategy

Returns a new instance of TimestampStrategy.



4
5
6
# File 'lib/active_record_change_matchers/strategies/timestamp_strategy.rb', line 4

def initialize(block)
  @block = block
end

Instance Method Details

#new_records(classes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_record_change_matchers/strategies/timestamp_strategy.rb', line 8

def new_records(classes)
  time_before = Time.current

  existing_records = classes.each_with_object({}) do |klass, hash|
    hash[klass] = klass.where("#{column_name} = ?", time_before).to_a
  end

  block.call

  classes.each_with_object({}) do |klass, new_records|
    new_records[klass] = klass.where("#{column_name} > ?", time_before).to_a
    new_records[klass] += klass.where("#{column_name} = ?", time_before).where.not(klass.primary_key => existing_records[klass]).to_a
  end
end