railscope 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Railscope
4
+ module Subscribers
5
+ class ModelSubscriber < BaseSubscriber
6
+ IGNORED_MODEL_PREFIXES = %w[Railscope:: ActiveRecord::].freeze
7
+
8
+ def self.subscribe
9
+ return if @subscribed
10
+
11
+ @subscribed = true
12
+
13
+ Rails.logger.info("[Railscope] ModelSubscriber.subscribe called - registering callbacks on ActiveRecord::Base")
14
+
15
+ ActiveRecord::Base.after_create_commit do
16
+ Rails.logger.debug("[Railscope] after_create_commit fired for #{self.class.name}:#{self.id}")
17
+ Railscope::Subscribers::ModelSubscriber.track("created", self)
18
+ end
19
+
20
+ ActiveRecord::Base.after_update_commit do
21
+ Rails.logger.debug("[Railscope] after_update_commit fired for #{self.class.name}:#{self.id}")
22
+ Railscope::Subscribers::ModelSubscriber.track("updated", self)
23
+ end
24
+
25
+ ActiveRecord::Base.after_destroy_commit do
26
+ Rails.logger.debug("[Railscope] after_destroy_commit fired for #{self.class.name}:#{self.id}")
27
+ Railscope::Subscribers::ModelSubscriber.track("deleted", self)
28
+ end
29
+ end
30
+
31
+ def self.track(action, model)
32
+ Rails.logger.debug("[Railscope] ModelSubscriber.track(#{action}, #{model.class.name}:#{model.id})")
33
+ new.record(action: action, model: model)
34
+ end
35
+
36
+ def record(action:, model:)
37
+ Rails.logger.debug("[Railscope] ModelSubscriber.record - enabled=#{Railscope.enabled?} ready=#{Railscope.ready?} recording=#{recording?}")
38
+
39
+ return unless Railscope.enabled?
40
+ return unless Railscope.ready?
41
+
42
+ model_name = model.class.name
43
+ return if model_name.nil?
44
+ return if IGNORED_MODEL_PREFIXES.any? { |prefix| model_name.start_with?(prefix) }
45
+
46
+ create_entry!(
47
+ entry_type: "model",
48
+ payload: build_payload(action, model),
49
+ tags: build_tags(action, model),
50
+ family_hash: build_family_hash(action, model),
51
+ should_display_on_index: true
52
+ )
53
+ Rails.logger.debug("[Railscope] ModelSubscriber - entry created for #{model_name}")
54
+ rescue StandardError => e
55
+ Rails.logger.error("[Railscope] Failed to record model event: #{e.class}: #{e.message}")
56
+ Rails.logger.error(e.backtrace&.first(5)&.join("\n"))
57
+ end
58
+
59
+ private
60
+
61
+ def build_payload(action, model)
62
+ payload = {
63
+ action: action,
64
+ model: "#{model.class.name}:#{model.id}"
65
+ }
66
+
67
+ if action != "deleted" && model.respond_to?(:saved_changes)
68
+ changes = model.saved_changes.except("updated_at", "created_at")
69
+ payload[:changes] = changes if changes.present?
70
+ end
71
+
72
+ payload
73
+ end
74
+
75
+ def build_tags(action, model)
76
+ ["model", action, model.class.name.underscore.gsub("/", "_")]
77
+ end
78
+
79
+ def build_family_hash(action, model)
80
+ generate_family_hash("model", model.class.name, action)
81
+ end
82
+ end
83
+ end
84
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Railscope
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/railscope.rb CHANGED
@@ -29,7 +29,13 @@ module Railscope
29
29
  attr_writer :retention_days, :redis, :storage_backend, :ignore_paths
30
30
  attr_accessor :authenticate_with
31
31
 
32
+ def enabled=(value)
33
+ @enabled = value
34
+ end
35
+
32
36
  def enabled?
37
+ return @enabled if defined?(@enabled) && !@enabled.nil?
38
+
33
39
  %w[true 1].include?(ENV.fetch("RAILSCOPE_ENABLED", nil))
34
40
  end
35
41
 
@@ -100,7 +106,9 @@ module Railscope
100
106
  authenticate_with.call(controller)
101
107
  end
102
108
 
103
- delegate :filter, to: :Filter
109
+ def filter(payload)
110
+ Filter.filter(payload)
111
+ end
104
112
 
105
113
  def add_sensitive_keys(*keys)
106
114
  Filter.add_sensitive_keys(*keys)