Class: ActiveRecordChangeMatchers::IdStrategy

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

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ IdStrategy

Returns a new instance of IdStrategy.



4
5
6
# File 'lib/active_record_change_matchers/strategies/id_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
# File 'lib/active_record_change_matchers/strategies/id_strategy.rb', line 8

def new_records(classes)
  ids_before = classes.each_with_object({}) do |klass, ids_before|
    ids_before[klass] = klass.select("MAX(#{column_name}) as max_id").take.try(:max_id) || 0
  end

  block.call

  classes.each_with_object({}) do |klass, new_records|
    id_before = ids_before[klass]
    new_records[klass] = klass.where("#{column_name} > ?", id_before).to_a
  end
end