Skip to content

Instantly share code, notes, and snippets.

@joeldrapper
Last active October 7, 2022 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeldrapper/6f919846958a207a6051f8e7bfb17ee4 to your computer and use it in GitHub Desktop.
Save joeldrapper/6f919846958a207a6051f8e7bfb17ee4 to your computer and use it in GitHub Desktop.
Active Record Spy
class Spy
def initialize(object)
@object = object
end
def respond_to_missing?(name)
@object.respond_to?(name)
end
def method_missing(name, *args, **kwargs, &block)
puts name
if @object.is_a? ActiveRecord::Base
if @object._reflections[name.to_s]
puts "reflection"
end
if @object.attributes[name.to_s]
puts "attribute"
end
end
@object.send(name, *args, **kwargs, &block)
end
end
# Extend this in a model
module Spy::Model
def where(...)
Spy.new(super)
end
def find(...)
Spy.new(super)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment