-
Hi there Mongoid Community team, We are doing an internal audit of our Mongoid models and want to understand just how coupled they are to one another ( e.g. callbacks that hit other models, validations that check other models, etc) When a Mongoid object is updated( e.g. save operation), is there an efficient way to track(via code) all other Mongoid objects that might be accessed? I am open to all options( static checking, runtime, etc) Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @d2army, there's not really a built-in way to do this, but here's a quick-and-dirty proof-of-concept I threw together. Maybe it'll give you some ideas? module SaveTracking
extend ActiveSupport::Concern
class <<self
def apply!
Mongoid.models.each { |klass| klass.include(SaveTracking) }
reset!
end
def reset!
@touched_classes_map = nil
end
def touched_classes_map
@touched_classes_map ||= Hash.new(0)
end
def track_save!(model)
touched_classes_map[model.class] += 1
end
def around
reset!
yield
touched_classes_map
end
end
included do
before_save { SaveTracking.track_save!(self) }
end
end
class Foo
include Mongoid::Document
before_save { bars << Bar.new }
has_many :bars
end
class Bar
include Mongoid::Document
belongs_to :foo
end
SaveTracking.apply!
result = SaveTracking.around do
Foo.create!
end
p result |
Beta Was this translation helpful? Give feedback.
Hey @d2army, there's not really a built-in way to do this, but here's a quick-and-dirty proof-of-concept I threw together. Maybe it'll give you some ideas?