Class: Railscope::Subscribers::ViewSubscriber

Inherits:
BaseSubscriber show all
Defined in:
lib/railscope/subscribers/view_subscriber.rb

Constant Summary collapse

TEMPLATE_EVENT =
"render_template.action_view"
PARTIAL_EVENT =
"render_partial.action_view"
LAYOUT_EVENT =
"render_layout.action_view"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup_controller_tracking ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/railscope/subscribers/view_subscriber.rb', line 29

def self.setup_controller_tracking
  return if @controller_tracking_setup

  @controller_tracking_setup = true

  controller_module = Module.new do
    extend ActiveSupport::Concern

    included do
      around_action :railscope_track_controller
    end

    private

    def railscope_track_controller
      Thread.current[:railscope_current_controller] = self
      yield
    ensure
      Thread.current[:railscope_current_controller] = nil
    end
  end

  if defined?(ActionController::Base)
    ActionController::Base.include(controller_module)
  else
    ActiveSupport.on_load(:action_controller_base) do
      include controller_module
    end
  end
end

.subscribe ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/railscope/subscribers/view_subscriber.rb', line 10

def self.subscribe
  return if @subscribed

  @subscribed = true

  # Include controller tracking in ApplicationController
  setup_controller_tracking

  ActiveSupport::Notifications.subscribe(TEMPLATE_EVENT) do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    new.record_template(event)
  end

  ActiveSupport::Notifications.subscribe(PARTIAL_EVENT) do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    new.record_partial(event)
  end
end

Instance Method Details

#record_partial(event) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/railscope/subscribers/view_subscriber.rb', line 76

def record_partial(event)
  return unless Railscope.enabled?
  return unless Railscope.ready?
  return if ignore_view?(event)

  create_entry!(
    entry_type: "view",
    payload: build_payload(event, "partial"),
    tags: build_tags(event, "partial"),
    family_hash: build_family_hash(event),
    should_display_on_index: false # Partials don't show in index, only in related entries
  )
rescue StandardError => e
  Rails.logger.error("[Railscope] Failed to record view partial: #{e.message}")
end

#record_template(event) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/railscope/subscribers/view_subscriber.rb', line 60

def record_template(event)
  return unless Railscope.enabled?
  return unless Railscope.ready?
  return if ignore_view?(event)

  create_entry!(
    entry_type: "view",
    payload: build_payload(event, "template"),
    tags: build_tags(event, "template"),
    family_hash: build_family_hash(event),
    should_display_on_index: true
  )
rescue StandardError => e
  Rails.logger.error("[Railscope] Failed to record view template: #{e.message}")
end