Commander Johnson
2011-Jul-02 09:17 UTC
How to load a gem''s .rb files AFTER Rails is loaded
Hey, I have a gem called ''authorizer'' which does authorization for me. It has two observer classes that need to be added to the list of ActiveRecord observers: config/environment.rb: require "authorizer/user_observer" require "authorizer/object_observer" config.active_record.observers = "Authorizer::UserObserver", "Authorizer::ObjectObserver" This gives me the error: `load_missing_constant'': uninitialized constant Authorizer::ActiveRecord (NameError) This problem is solved by copying the gem''s source files into the lib/ folder or by using a plugin. But I don''t want that. How can I ensure the gem is loaded AFTER the Rails framework is loaded? This is on Rails 2.3.11 All the best, CmdJohnson -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rob Biedenharn
2011-Jul-02 13:55 UTC
Re: How to load a gem''s .rb files AFTER Rails is loaded
On Jul 2, 2011, at 5:17 AM, Commander Johnson wrote:> Hey, > > I have a gem called ''authorizer'' which does authorization for me. It > has two observer classes that need to be added to the list of > ActiveRecord observers: > > config/environment.rb: > > require "authorizer/user_observer" > require "authorizer/object_observer" > > config.active_record.observers = "Authorizer::UserObserver", > "Authorizer::ObjectObserver" > > This gives me the error: > > `load_missing_constant'': uninitialized constant > Authorizer::ActiveRecord (NameError) > > This problem is solved by copying the gem''s source files into the > lib/ folder or by using a plugin. But I don''t want that. How can I > ensure the gem is loaded AFTER the Rails framework is loaded? > > This is on Rails 2.3.11 > > All the best, > CmdJohnsonI''m not sure if this works when you have to reference the ''config'' object, but you can wrap code inside a block and use the #after_initialize method: config.after_initialize do config.active_record.observers = "Authorizer::UserObserver", "Authorizer::ObjectObserver" end -Rob Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.com/ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Commander Johnson
2011-Jul-02 16:29 UTC
Re: How to load a gem''s .rb files AFTER Rails is loaded
Thanks Rob. That almost fixed it. Solution is this: 1. Create a file called rails/init.rb 2. Put this in it: config.after_initialize do ActiveRecord::Base.observers << Authorizer::UserObserver ActiveRecord::Base.observers << Authorizer::ObjectObserver end No more need for the user to mess with config/environment.rb. I found it thanks to http://riotprojects.com/post/106808946/activerecord-observers-in-gems-plugins It also works fine in development mode. Because of the post from Riotproject I was expecting trouble, but this hasn''t occurred. On Sat, Jul 2, 2011 at 3:55 PM, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org>wrote:> > On Jul 2, 2011, at 5:17 AM, Commander Johnson wrote: > > Hey, > > I have a gem called ''authorizer'' which does authorization for me. It has > two observer classes that need to be added to the list of ActiveRecord > observers: > > config/environment.rb: > > require "authorizer/user_observer" > require "authorizer/object_observer" > > config.active_record.observers = "Authorizer::UserObserver", > "Authorizer::ObjectObserver" > > This gives me the error: > > `load_missing_constant'': uninitialized constant Authorizer::ActiveRecord > (NameError) > > This problem is solved by copying the gem''s source files into the lib/ > folder or by using a plugin. But I don''t want that. How can I ensure the gem > is loaded AFTER the Rails framework is loaded? > > This is on Rails 2.3.11 > > All the best, > CmdJohnson > > > I''m not sure if this works when you have to reference the ''config'' object, > but you can wrap code inside a block and use the #after_initialize method: > > config.after_initialize do > config.active_record.observers = "Authorizer::UserObserver", > "Authorizer::ObjectObserver" > end > > -Rob > > Rob Biedenharn > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ > rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org <Rob-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org> > http://GaslightSoftware.com/ > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.