Re: modifying ActiveRecord::Base - where?

Duane Johnson
Wed, 07 Dec 2005 01:33:22 -0000

On Dec 5, 2005, at 11:26 PM, Ara.T.Howard wrote:

> On Mon, 5 Dec 2005, Duane Johnson wrote:
>
>> Traditionally, it would go in your lib/ folder and then you''d
add
>> something
>> like ''require "my_active_record.rb"'' in
your environment.rb file.
>> I usually
>> go one step further and add a ''patches'' folder within
''lib'' so I can
>> differentiate between rails additions and other libraries.
>
> hmmm.  i''d like to have it autoloaded - guess i could put the
> require in my
> environment.rb then.  that kosher?
>
Yes.  In fact, here''s what I do in environment.rb:

# Auto-require method to include all libraries within a directory
def require_all_within(directory, description = "library")
   Find.find(directory) do |path|
     if File.file?(path) && File.extname(path) == ".rb"
			expanded_file = File.expand_path(path)
			begin
       	STDERR << "Require #{description}
#{File.basename(path)}\n"
if require_dependency(expanded_file)
			rescue Exception => e
				STDERR << "\nError requiring #{expanded_file}\n"
				STDERR << e.to_s + "\n"
			end
     end
   end
end

require_all_within("#{RAILS_ROOT}/lib/enhancements")
require_all_within("#{RAILS_ROOT}/lib/patches")

That way all my enhancements and patches are pre-loaded on bootup,
plus I get the benefit of Rails auto-loading if a filename within
lib/ matches a class or module (the usual behavior).

Duane Johnson
(canadaduane)