Class: TelegramBotEngine::Event

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/telegram_bot_engine/event.rb

Class Method Summary collapse

Class Method Details

.log(event_type:, action:, chat_id: nil, username: nil, bot_id: nil, details: {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/telegram_bot_engine/event.rb', line 17

def self.log(event_type:, action:, chat_id: nil, username: nil, bot_id: nil, details: {})
  return unless TelegramBotEngine.config.event_logging
  return unless table_exists?

  attrs = {
    event_type: event_type,
    action: action,
    chat_id: chat_id,
    username: username,
    details: details
  }
  # Guard the "code references bot_id before the column is migrated in" boot crash
  # (docs/0001 ยง9): only write bot_id once the column actually exists.
  attrs[:bot_id] = bot_id if column_names.include?("bot_id")
  create!(attrs)

  purge_old_randomly!
end

.purge_old!Object



36
37
38
# File 'app/models/telegram_bot_engine/event.rb', line 36

def self.purge_old!
  where("created_at < ?", TelegramBotEngine.config.event_retention_days.days.ago).delete_all
end

.purge_old_randomly!Object



40
41
42
# File 'app/models/telegram_bot_engine/event.rb', line 40

def self.purge_old_randomly!
  purge_old! if rand(100).zero?
end