Sha256: 05e3fc2848ba80dfaeddf2687d0c2de47cdfc50c86042264c4b87e64f6366e1c

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

require 'active_support/log_subscriber'
require 'active_support/core_ext/module/attribute_accessors'

require 'hickory/ext/rails/rack/logger'
require 'hickory/rails/railtie'
require 'hickory/log_subscribers/action_controller'
require 'hickory/log_subscribers/action_mailer'
require 'hickory/log_subscribers/active_job'

module Hickory
  module Rails
    module_function

    mattr_accessor :application, :ignore_if

    def setup(app)
      self.application = app
      self.ignore_if   = config.ignore_if

      remove_existing_log_subscriptions
      Hickory::LogSubscribers::ActionController.attach_to :action_controller
      Hickory::LogSubscribers::ActionMailer.attach_to :action_mailer
      Hickory::LogSubscribers::ActiveJob.attach_to :active_job
    end

    def config
      application.config.hickory
    end

    def remove_existing_log_subscriptions
      ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
        case subscriber
        when ActionView::LogSubscriber
          unsubscribe(:action_view, subscriber)
        when ActionController::LogSubscriber
          unsubscribe(:action_controller, subscriber)
        when ActionMailer::LogSubscriber
          unsubscribe(:action_mailer, subscriber)
        when ActiveJob::Logging::LogSubscriber
          unsubscribe(:active_job, subscriber)
        end
      end
    end

    def unsubscribe(component, subscriber)
      events = subscriber.public_methods(false).reject { |method| method.to_s == 'call' }
      events.each do |event|
        ActiveSupport::Notifications.notifier.listeners_for("#{event}.#{component}").each do |listener|
          if listener.instance_variable_get('@delegate') == subscriber
            ActiveSupport::Notifications.unsubscribe listener
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hickory-0.1.6 lib/hickory/rails.rb
hickory-0.1.5 lib/hickory/rails.rb
hickory-0.1.4 lib/hickory/rails.rb
hickory-0.1.3 lib/hickory/rails.rb