Module: IronTrail::Testing

Defined in:
lib/iron_trail/testing/rspec.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledObject

Returns the value of attribute enabled.



13
14
15
# File 'lib/iron_trail/testing/rspec.rb', line 13

def enabled
  @enabled
end

Class Method Details

.disable!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/iron_trail/testing/rspec.rb', line 20

def disable!
  # We "disable" it by replacing the trigger function by a no-op one.
  # This should be faster than adding/removing triggers from several
  # tables every time.
  sql = <<~SQL
    CREATE OR REPLACE FUNCTION irontrail_log_row()
    RETURNS TRIGGER AS $$
    BEGIN
      RETURN NULL;
    END;
    $$ LANGUAGE plpgsql;
  SQL

  ActiveRecord::Base.connection.execute(sql)
  @enabled = false
end

.enable!Object



15
16
17
18
# File 'lib/iron_trail/testing/rspec.rb', line 15

def enable!
  DbFunctions.new(ActiveRecord::Base.connection).install_functions
  @enabled = true
end

.with_iron_trail(want_enabled:, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/iron_trail/testing/rspec.rb', line 37

def with_iron_trail(want_enabled:, &block)
  was_enabled = IronTrail::Testing.enabled

  if want_enabled
    ::IronTrail::Testing.enable! unless was_enabled
  else
    ::IronTrail::Testing.disable! if was_enabled
  end

  block.call
ensure
  if want_enabled && !was_enabled
    ::IronTrail::Testing.disable!
  elsif !want_enabled && was_enabled
    ::IronTrail::Testing.enable!
  end
end