jamesgolick/observational

Observational and cache sweepers

Opened this issue · 1 comments

I ran into a weird issue when I tried to add a cache sweeper while using the observational gem (which I really like by the way).

Whenever the sweeper is called, it is raising a 'method undefined' exception in lib/observational/observable.rb:21 when calling 'observes_action' on the sweeper's observer (since the sweeper's observer is a regular Observer, not an Observational::Observer).

Here's the relevant stacktrace:

NoMethodError (undefined method observes_action?' for #<Admin::RegionsController:0x1033480b0>): observational (0.2.5) lib/observational/observable.rb:21:inobservers_for'
observational (0.2.5) lib/observational/observable.rb:21:in select' observational (0.2.5) lib/observational/observable.rb:21:inobservers_for'
observational (0.2.5) lib/observational/observable.rb:31:in notify_observers' observational (0.2.5) lib/observational/active_record_observers.rb:4:insend'
observational (0.2.5) lib/observational/active_record_observers.rb:4:in `run_callbacks'

I was able to fix the issue by adding a check for the :observes_action? method to observers_for:

  def observers_for(action)
    observers.select { |o| o.observes_action?(action) if o.respond_to?(:observes_action?) }
  end

I'm not really sure if this is best solution, but its working for me right now. Anyway, I thought you should know. Oh, and I'm using Ruby 1.8.7, Rails 2.3.5, RubyGems 1.3.6, and Observational 0.2.5.

Let me know if you need anymore info.

I've forked this repo here and committed my fix to it.