Observables don't work when in submodules #11

Open
dacort opened this Issue Mar 23, 2011 · 3 comments

Projects

None yet

3 participants

@dacort
dacort commented Mar 23, 2011

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
@jsmestad
Collaborator
jsmestad commented Apr 6, 2011

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.

@jrwest
Collaborator
jrwest commented Apr 8, 2011

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
Collaborator
jrwest commented Apr 8, 2011

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: 63860b2

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:
4fba79d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment