Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observables don't work when in submodules #11

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

Observables don't work when in submodules #11

dacort opened this issue Mar 23, 2011 · 3 comments

Comments

@dacort
Copy link

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
Copy link
Contributor

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
Copy link
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
Copy link
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
Projects
None yet
Development

No branches or pull requests

3 participants