Module: Magick::Rails::Events

Defined in:
lib/magick/rails/events.rb

Constant Summary collapse

EVENT_PREFIX =

Event names (using Rails 8.1 structured event format)

'magick.feature_flag'
EVENTS =
{
  changed: "#{EVENT_PREFIX}.changed",
  enabled: "#{EVENT_PREFIX}.enabled",
  disabled: "#{EVENT_PREFIX}.disabled",
  enabled_globally: "#{EVENT_PREFIX}.enabled_globally",
  disabled_globally: "#{EVENT_PREFIX}.disabled_globally",
  dependency_added: "#{EVENT_PREFIX}.dependency_added",
  dependency_removed: "#{EVENT_PREFIX}.dependency_removed",
  variant_set: "#{EVENT_PREFIX}.variant_set",
  variant_selected: "#{EVENT_PREFIX}.variant_selected",
  targeting_added: "#{EVENT_PREFIX}.targeting_added",
  targeting_removed: "#{EVENT_PREFIX}.targeting_removed",
  version_saved: "#{EVENT_PREFIX}.version_saved",
  rollback: "#{EVENT_PREFIX}.rollback",
  exported: "#{EVENT_PREFIX}.exported",
  imported: "#{EVENT_PREFIX}.imported",
  audit_logged: "#{EVENT_PREFIX}.audit_logged",
  usage_tracked: "#{EVENT_PREFIX}.usage_tracked",
  deprecated_warning: "#{EVENT_PREFIX}.deprecated_warning"
}.freeze

Class Method Summary collapse

Class Method Details

.audit_logged(feature_name, action:, user_id: nil, changes: {}, **metadata) ⇒ Object

Audit log entry created



203
204
205
206
207
208
209
210
211
212
# File 'lib/magick/rails/events.rb', line 203

def self.audit_logged(feature_name, action:, user_id: nil, changes: {}, **)
  notify(:audit_logged, {
           feature_name: feature_name.to_s,
           action: action.to_s,
           user_id: user_id,
           changes: changes,
           timestamp: Time.now.iso8601,
           **
         })
end

.dependency_added(feature_name, dependency_name, **metadata) ⇒ Object

Dependency added



99
100
101
102
103
104
105
106
# File 'lib/magick/rails/events.rb', line 99

def self.dependency_added(feature_name, dependency_name, **)
  notify(:dependency_added, {
           feature_name: feature_name.to_s,
           dependency_name: dependency_name.to_s,
           timestamp: Time.now.iso8601,
           **
         })
end

.dependency_removed(feature_name, dependency_name, **metadata) ⇒ Object

Dependency removed



109
110
111
112
113
114
115
116
# File 'lib/magick/rails/events.rb', line 109

def self.dependency_removed(feature_name, dependency_name, **)
  notify(:dependency_removed, {
           feature_name: feature_name.to_s,
           dependency_name: dependency_name.to_s,
           timestamp: Time.now.iso8601,
           **
         })
end

.deprecated_warning(feature_name, **metadata) ⇒ Object

Deprecation warning



227
228
229
230
231
232
233
# File 'lib/magick/rails/events.rb', line 227

def self.deprecated_warning(feature_name, **)
  notify(:deprecated_warning, {
           feature_name: feature_name.to_s,
           timestamp: Time.now.iso8601,
           **
         })
end

.exported(format:, feature_count:, **metadata) ⇒ Object

Export performed



183
184
185
186
187
188
189
190
# File 'lib/magick/rails/events.rb', line 183

def self.exported(format:, feature_count:, **)
  notify(:exported, {
           format: format.to_s,
           feature_count: feature_count,
           timestamp: Time.now.iso8601,
           **
         })
end

.feature_changed(feature_name, changes:, user_id: nil, **metadata) ⇒ Object

Feature flag changed (value, status, etc.)



48
49
50
51
52
53
54
55
56
# File 'lib/magick/rails/events.rb', line 48

def self.feature_changed(feature_name, changes:, user_id: nil, **)
  notify(:changed, {
           feature_name: feature_name.to_s,
           changes: changes,
           user_id: user_id,
           timestamp: Time.now.iso8601,
           **
         })
end

.feature_disabled(feature_name, context: {}, **metadata) ⇒ Object

Feature disabled



69
70
71
72
73
74
75
76
# File 'lib/magick/rails/events.rb', line 69

def self.feature_disabled(feature_name, context: {}, **)
  notify(:disabled, {
           feature_name: feature_name.to_s,
           context: context,
           timestamp: Time.now.iso8601,
           **
         })
end

.feature_disabled_globally(feature_name, user_id: nil, **metadata) ⇒ Object

Feature disabled globally (no targeting)



89
90
91
92
93
94
95
96
# File 'lib/magick/rails/events.rb', line 89

def self.feature_disabled_globally(feature_name, user_id: nil, **)
  notify(:disabled_globally, {
           feature_name: feature_name.to_s,
           user_id: user_id,
           timestamp: Time.now.iso8601,
           **
         })
end

.feature_enabled(feature_name, context: {}, **metadata) ⇒ Object

Feature enabled



59
60
61
62
63
64
65
66
# File 'lib/magick/rails/events.rb', line 59

def self.feature_enabled(feature_name, context: {}, **)
  notify(:enabled, {
           feature_name: feature_name.to_s,
           context: context,
           timestamp: Time.now.iso8601,
           **
         })
end

.feature_enabled_globally(feature_name, user_id: nil, **metadata) ⇒ Object

Feature enabled globally (no targeting)



79
80
81
82
83
84
85
86
# File 'lib/magick/rails/events.rb', line 79

def self.feature_enabled_globally(feature_name, user_id: nil, **)
  notify(:enabled_globally, {
           feature_name: feature_name.to_s,
           user_id: user_id,
           timestamp: Time.now.iso8601,
           **
         })
end

.imported(format:, feature_count:, **metadata) ⇒ Object

Import performed



193
194
195
196
197
198
199
200
# File 'lib/magick/rails/events.rb', line 193

def self.imported(format:, feature_count:, **)
  notify(:imported, {
           format: format.to_s,
           feature_count: feature_count,
           timestamp: Time.now.iso8601,
           **
         })
end

.notify(event_name, payload = {}) ⇒ Object



35
36
37
38
39
40
# File 'lib/magick/rails/events.rb', line 35

def self.notify(event_name, payload = {})
  return unless rails81?

  event_name_str = EVENTS[event_name] || event_name.to_s
  Rails.event.notify(event_name_str, payload)
end

.rails81?Boolean

Check if Rails 8.1+ structured events are available

Returns:

  • (Boolean)


7
8
9
# File 'lib/magick/rails/events.rb', line 7

def self.rails81?
  defined?(Rails) && Rails.respond_to?(:event) && Rails.event.respond_to?(:notify)
end

.rails8?Boolean

Backward compatibility alias

Returns:

  • (Boolean)


43
44
45
# File 'lib/magick/rails/events.rb', line 43

def self.rails8?
  rails81?
end

.rollback(feature_name, version:, **metadata) ⇒ Object

Rollback performed



173
174
175
176
177
178
179
180
# File 'lib/magick/rails/events.rb', line 173

def self.rollback(feature_name, version:, **)
  notify(:rollback, {
           feature_name: feature_name.to_s,
           version: version,
           timestamp: Time.now.iso8601,
           **
         })
end

.targeting_added(feature_name, targeting_type:, targeting_value:, **metadata) ⇒ Object

Targeting added



140
141
142
143
144
145
146
147
148
# File 'lib/magick/rails/events.rb', line 140

def self.targeting_added(feature_name, targeting_type:, targeting_value:, **)
  notify(:targeting_added, {
           feature_name: feature_name.to_s,
           targeting_type: targeting_type.to_s,
           targeting_value: targeting_value,
           timestamp: Time.now.iso8601,
           **
         })
end

.targeting_removed(feature_name, targeting_type:, targeting_value: nil, **metadata) ⇒ Object

Targeting removed



151
152
153
154
155
156
157
158
159
# File 'lib/magick/rails/events.rb', line 151

def self.targeting_removed(feature_name, targeting_type:, targeting_value: nil, **)
  notify(:targeting_removed, {
           feature_name: feature_name.to_s,
           targeting_type: targeting_type.to_s,
           targeting_value: targeting_value,
           timestamp: Time.now.iso8601,
           **
         })
end

.usage_tracked(feature_name, operation:, duration:, success: true, **metadata) ⇒ Object

Usage tracked



215
216
217
218
219
220
221
222
223
224
# File 'lib/magick/rails/events.rb', line 215

def self.usage_tracked(feature_name, operation:, duration:, success: true, **)
  notify(:usage_tracked, {
           feature_name: feature_name.to_s,
           operation: operation.to_s,
           duration: duration,
           success: success,
           timestamp: Time.now.iso8601,
           **
         })
end

.variant_selected(feature_name, variant_name:, context: {}, **metadata) ⇒ Object

Variant selected



129
130
131
132
133
134
135
136
137
# File 'lib/magick/rails/events.rb', line 129

def self.variant_selected(feature_name, variant_name:, context: {}, **)
  notify(:variant_selected, {
           feature_name: feature_name.to_s,
           variant_name: variant_name.to_s,
           context: context,
           timestamp: Time.now.iso8601,
           **
         })
end

.variant_set(feature_name, variants:, **metadata) ⇒ Object

Variants set



119
120
121
122
123
124
125
126
# File 'lib/magick/rails/events.rb', line 119

def self.variant_set(feature_name, variants:, **)
  notify(:variant_set, {
           feature_name: feature_name.to_s,
           variants: variants.is_a?(Array) ? variants.map(&:to_h) : variants,
           timestamp: Time.now.iso8601,
           **
         })
end

.version_saved(feature_name, version:, created_by: nil, **metadata) ⇒ Object

Version saved



162
163
164
165
166
167
168
169
170
# File 'lib/magick/rails/events.rb', line 162

def self.version_saved(feature_name, version:, created_by: nil, **)
  notify(:version_saved, {
           feature_name: feature_name.to_s,
           version: version,
           created_by: created_by,
           timestamp: Time.now.iso8601,
           **
         })
end