mongomatic icon indicating copy to clipboard operation
mongomatic copied to clipboard

Observables don't work when in submodules

Open dacort opened this issue 14 years ago • 3 comments

If I use Mongomatic classes within submodules, observers don't seem to work. With the example below, if I try to do a Bob::Person.new, I receive a NameError: wrong constant name Bob::MyCustomCallbacks error. This appears to be a result of the Module.const_defined? call in observable.rb.

module Bob
  class MyCustomCallbacks < Mongomatic::Observer
    def after_initialize
      puts "after insert or update"
    end
  end

  class Person < Mongomatic::Base
    include Mongomatic::Observable
    observer MyCustomCallbacks
  end
end

dacort avatar Mar 23 '11 02:03 dacort

I can get to looking at this later this week hopefully. If you have time before then, if you could spike out a test that I can run against to ensure I fix this properly.

jsmestad avatar Apr 06 '11 18:04 jsmestad

Observers also seem to fail in 1.8.7 entirely. I just ran the tests. @dacort what vsn of Ruby were you running. I also may have some time to recreate this weekend. @jsmestad have you started working on this already?

jrwest avatar Apr 08 '11 16:04 jrwest

I created a new branch https://github.com/benmyles/mongomatic/tree/observer_fixes to address this issue.

This commit specifically fixes the observer issues in 1.8.7 but does not solve the issue above. The fix was to use Object.const_defined? instead of Module.const_defined? which works slightly differently in 1.8.7: https://github.com/benmyles/mongomatic/commit/63860b28771529ef377fdc3d2bd109b8c88702db

This commit fixes both problems but I'm not sure I like the use of eval("#{observer}") so let me know what you think. An alternative could be to detect observers nested in modules and properly call THE::PARENT::MODULE.const_defined? falling back to Object.const_defined? when the observer is not contained in a module: https://github.com/benmyles/mongomatic/commit/4fba79dd5421576b756f0733d484eb3871711231

jrwest avatar Apr 08 '11 20:04 jrwest