Module: Logidze::Detachable::ClassMethods

Defined in:
lib/logidze/detachable.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#create_logidze_snapshot(timestamp: nil, only: nil, except: nil, sql_filter: nil) ⇒ Object

Initialize log_data with the current state if it’s null



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/logidze/detachable.rb', line 22

def create_logidze_snapshot(timestamp: nil, only: nil, except: nil, sql_filter: nil)
  ActiveRecord::Base.connection.execute <<~SQL.squish
    INSERT INTO #{Logidze::LogidzeData.quoted_table_name} (log_data, loggable_type, loggable_id)
    SELECT logidze_snapshot(
        to_jsonb(#{quoted_table_name}),
        #{snapshot_query_args(timestamp: timestamp, only: only, except: except)}
      ),
      '#{name}',
      #{quoted_table_name}.id
    FROM #{quoted_table_name}
    #{sql_filter}
    ON CONFLICT (loggable_type, loggable_id)
    DO UPDATE
    SET log_data = EXCLUDED.log_data;
  SQL
end

#reset_log_dataInteger

Nullify log_data column for a association

Returns:

  • (Integer)

    number of deleted Logidze::LogidzeData records



17
18
19
# File 'lib/logidze/detachable.rb', line 17

def reset_log_data
  Logidze::LogidzeData.where(loggable_id: ids, loggable_type: name).delete_all
end