Skip to content

Loading…

Observer.observe should not accept a string #7911

Closed
jmazzi opened this Issue · 2 comments

3 participants

@jmazzi

Most of the time you can just use a string observe "something". However, there are some edge cases where autoloading can keep something like observe "service/model" from working.

The documentation for Observer.observe states:

Observers will by default be mapped to the class with which they share a name. So CommentObserver will >be tied to observing Comment, ProductManagerObserver to ProductManager, and so on. If you want to >name your observer differently than the class you’re interested in observing, you can use the >Observer.observe class method which takes either the concrete class (Product) or a symbol for >that class (:product):"

I'm wondering if you would consider accepting a patch that forces observe to raise an exception when it's passed a string.

@senny
Ruby on Rails member

@jmazzi why does the autloading fail with 'service/model'?

the observe method is pretty simple and only uses camelize and constantize to turn the arguments into classes. Where exactly is the problem with the strings?

def observe(*models)
  models.flatten!
  models.collect! { |model| model.respond_to?(:to_sym) ? model.to_s.camelize.constantize : model }
  singleton_class.redefine_method(:observed_classes) { models }
end
@steveklabnik
Ruby on Rails member

We're extracting observers to a plugin anyway, so I'd wait until that work is done and then submit a pull there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.