Module: SweetNotifications

Extended by:
ControllerRuntime, Railtie
Defined in:
lib/sweet_notifications.rb,
lib/sweet_notifications/railtie.rb,
lib/sweet_notifications/version.rb,
lib/sweet_notifications/log_subscriber.rb,
lib/sweet_notifications/controller_runtime.rb

Overview

Syntactic sugar for ActiveSupport::Notifications subscribers for logging purposes in Rails.

Defined Under Namespace

Modules: ControllerRuntime, Railtie Classes: LogSubscriber

Constant Summary collapse

VERSION =
'1.1.1'.freeze

Class Method Summary collapse

Methods included from ControllerRuntime

controller_runtime

Methods included from Railtie

initialize_rails, railtie

Class Method Details

.subscribe(name, label: nil) { ... } ⇒ Rails::Railtie, ActiveSupport::LogSubscriber

Subscribe to an ActiveSupport::Notifications namespace.

This will subscribe to the namespace given as argument and, if necessary, create a Rails initializer that will be run when the application is initialized.

Examples

SweetNotifications.subscribe :active_record do
  color ActiveSupport::LogSubscriber::GREEN
  event :sql, runtime: true do |event|
    return unless logger.debug?
    debug message(event, event.payload[:name], event.payload[:sql])
  end
end

Parameters:

  • name (Symbol)

    event namespace

  • label (String) (defaults to: nil)

    optional label for logging

Yields:

  • event subscription

Returns:

  • (Rails::Railtie, ActiveSupport::LogSubscriber)

    An array consisting of a Railtie and a LogSubscriber



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sweet_notifications.rb', line 33

def self.subscribe(name, label: nil, &block)
  label ||= name
  log_subscriber = Class.new(SweetNotifications::LogSubscriber, &block)
  controller_runtime = self.controller_runtime(label, log_subscriber)
  if rails_initialized?
    initialize_rails(name, log_subscriber, controller_runtime)
    [nil, log_subscriber]
  else
    [railtie(name, log_subscriber, controller_runtime), log_subscriber]
  end
end