Module: DatabaseRewinder::InsertRecorder

Defined in:
lib/database_rewinder/active_record_monkey.rb

Class Method Summary collapse

Class Method Details

.prepended(mod) ⇒ Object

This method actually no longer has to be a ‘prepended` hook because InsertRecorder is a module without a direct method now, but still doing this just for compatibility



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/database_rewinder/active_record_monkey.rb', line 6

def self.prepended(mod)
  [:execute, :exec_query, :internal_exec_query].each do |method_name|
    if mod.instance_methods.include?(method_name) && (meth = mod.instance_method(method_name))
      method_body = if meth.parameters.any? {|type, _name| [:key, :keyreq, :keyrest].include? type }
        <<-RUBY
          def #{method_name}(sql, *, **)
            DatabaseRewinder.record_inserted_table self, sql
            super
          end
        RUBY
      else
        <<-RUBY
          def #{method_name}(sql, *)
            DatabaseRewinder.record_inserted_table self, sql
            super
          end
        RUBY
      end

      mod.send :prepend, Module.new { class_eval method_body }
    end
  end
end