Organizing monkey patches in environment.rb?

I have been monkey patching directly in environment.rb for a while now.

I’m at a point where I’d like to move all of this code out of
environment.rb into one or more files with meaningful names so as to
keep environment.rb a reasonable size.

Anyone decided to do this and willing to share advice on how they
organized their extracted monkey patches?

Perhaps put each “monkey patch” file into RAILS_ROOT/lib and requiring
them from environment.rb? Something else?

All ears,
Wes

Hi –

On Thu, 6 Mar 2008, Wes G. wrote:

Perhaps put each “monkey patch” file into RAILS_ROOT/lib and requiring
them from environment.rb? Something else?

I acknowledge the term “monkey patching” only under protest :slight_smile: (I
hate it, and never quite know what it means anyway.) But have a look,
in Rails 2.0 applications, at config/initializers. It’s a directory
where you can offload environment.rb stuff and have it automatically
loaded.

David


Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!

On Mar 5, 2008, at 7:24 PM, David A. Black wrote:

Anyone decided to do this and willing to share advice on how they
loaded.

David


Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!

I’ve collected these bits in separate files under RAILS_ROOT/lib/ext/

-rw-r–r-- 1 rab rab 63 Jan 22 14:17 lib/ext/array.rb
-rw-r–r-- 1 rab rab 3302 Feb 26 19:23 lib/ext/net_http_post.rb
-rw-r–r-- 1 rab rab 187 Jan 4 14:29 lib/ext/nil_class.rb
-rw-r–r-- 1 rab rab 131 Dec 11 12:20 lib/ext/string.rb
-rw-r–r-- 1 rab rab 1601 Jan 28 09:55 lib/ext/time.rb

And then arrange form to be required within config/environment.rb with:

Dir[File.join(RAILS_ROOT, ‘lib’, ‘ext’, ‘*.rb’)].each do |f|
base = File.basename(f, ‘.rb’)
require “ext/#{base}”
end

I haven’t started any projects in Rails 2.x yet, but will check out
the initializers when I do.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]