Where is the best place to stash Ruby files that add functionality to
Rails classes (in my case, ActiveRecord::Base)?  I think my module is
being loaded too late, and causing a class method to be undefined.

Here's what I tried: I wrote a module to add a new class method to
ActiveRecord::Base following a pattern that I picked up from
http://guides.rubyonrails.org/plugins.html; in my module implement the
included callback and have it extend the target class with my class
methods (actually singleton methods on the metaclass of my class
object if I'm understanding all of this weirdness correctly):

    def self.included(base)
      base.send :extend, ClassMethods
    end

    module ClassMethods
      def my_method
      end
    end

Then outside of the module definition I tell ActiveRecord::Base to
include my module:

  ActiveRecord::Base.send(:include, MyModule)

And according to my (obviously poorly written) unit test, this all
worked wonderfully:

  def test_responds_to_my_method
    assert MyModel.respond_to?(:my_method, true)
  end

Great!  Except..

  class MyModel < ActiveRecord::Base
    my_method # undefined local variable or method `my_method'
  end

So it seems reasonable to me that this might happen if my_model.rb is
loaded first, then my module, then the test.  Additional evidence is
that if I add a "debugger" line to my included callback, I'm never
sent to the debugger.  Unless I remove the call to my_method, then I
am.

So if my hypothesis is correct, it's a bad idea to add my modules to
lib\modules and append that to the end of my load path.  But then
where should I stash modules that update rails classes?



--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to