jackc / edge

Edge provides graph functionality to ActiveRecord.
MIT License
90 stars 10 forks source link

acts_as_forest doesn't survive class reloading #2

Closed jschroeder closed 11 years ago

jschroeder commented 11 years ago

Models that act_as_forest "forget" their three new class methods "find_forest", "with_descendants" and "find_tree" in my development environment after a short while.

Error message: NoMethodError: private method `find_forest' called for #Class:0x007fb38a51e128

It turns out this happens whenever the class that uses "acts_as_forest" gets reloaded.

You can verify this in your console. Type "reload!", and all the sudden your class doesn't answer to "find_forest" anymore.

The same is true when declaring "acts_as_forest" twice in your model.

jackc commented 11 years ago

I found the problem. I though I was defining methods on the class that called acts_as_forest. As it turns out the methods were being defined on Edge::Forest::ClassMethods directly. The last method defined is private. When acts_as_forest is called a second time the module still remembers that new methods are private. So it redefines find_forest and friends as private.

I fixed it by altering acts_as_forest to extend the the class that calls it with a module that has find_forest instead of trying to write it directly into the class. So the solving of this bug seems to have led to a better design, too.

I just released Edge 0.2.1 which includes the fix. Update to that and it should work.