Module: RailsOtelContext::ActiveRecordContext::ScopeNameTracking

Defined in:
lib/rails_otel_context/activerecord_context.rb

Overview

Wraps scope-generated class methods to store the scope name on the Relation.

Instance Method Summary collapse

Instance Method Details

#scope(name, body) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rails_otel_context/activerecord_context.rb', line 134

def scope(name, body, &)
  super

  # Guard against double-wrapping on class reload in development
  @_otel_wrapped_scopes ||= {}
  return if @_otel_wrapped_scopes[name]

  name_str = name.to_s.freeze
  original = method(name)
  define_singleton_method(name) do |*args|
    relation = original.call(*args)
    if relation.is_a?(::ActiveRecord::Relation)
      relation.instance_variable_set(:@_otel_scope_name, name_str)
    end
    relation
  end
  @_otel_wrapped_scopes[name] = true
end