Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails ActiveRecord::Observer in plugin

I want to add an Observer for Issues to an existing rails application.

I created a plugin, and under plugin's app/model i saved the custom_observer.rb

It works when I add :custom_observer to config.active_record.observers in config/application.rb

I tried something like

RedmineApp::Application.configure do
    config.after_initialize do
             config.active_record.observers << :custom_observer
    end
end

in the plugin's init.rb but that doesn't work.

What is a better way to do that? thanks

like image 873
ilgofat Avatar asked Nov 03 '22 12:11

ilgofat


1 Answers

If you're using Rails 4.0, observers have been extracted out of the core into a gem called rails-observers

Although you've not said which version of rails you're using, if you're using rails 4, I'd recommend doing this:

GemFile

#Rails Observers
gem "rails-observers", "~> 0.1.2"

Config

#config/application.rb
#Observer Classes
config.active_record.observers = :custom_observer

Observer

#models/customer_observer.rb
class CustomObserver < ActiveRecord::Observer
end

Works for us, and I've supplied live code for you :)

like image 69
Richard Peck Avatar answered Nov 12 '22 17:11

Richard Peck