Last active
October 7, 2022 09:17
-
-
Save joeldrapper/6f919846958a207a6051f8e7bfb17ee4 to your computer and use it in GitHub Desktop.
Active Record Spy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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