Modifying ActiveRecord::Base - where?


#1

i need to add a method to ActiveRecord::Base. to be specific

class ActiveRecord::Base
def self::restful *a
@restful = a.shift || @restful
end
end

so i can

class MyModel < ActiveRecord::Base
has_one :foobar
restful true
end

where is the currently preferred location to place a file which does
this?

-a

===============================================================================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| – bodhicaryavatara


#2

On Dec 5, 2005, at 10:16 PM, Ara.T.Howard wrote:

class MyModel < ActiveRecord::Base
has_one :foobar
restful true
end

where is the currently preferred location to place a file which
does this?

-a

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.

Nowadays, however, it seems that plugins may do exactly what you’re
after. But it seems a little overkill to put one tiny method like
that into a plugin.

Duane J.
(canadaduane)


#3

On Mon, 5 Dec 2005, Duane J. 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?

Nowadays, however, it seems that plugins may do exactly what you’re after.
But it seems a little overkill to put one tiny method like that into a
plugin.

agreed.

-a

===============================================================================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| – bodhicaryavatara


#4

wiki about modifying rails core

http://wiki.rubyonrails.com/rails/pages/HowToWritePluginToModifyRailsCore


#5

On Tue, 6 Dec 2005, Peter M. wrote:

wiki about modifying rails core

http://wiki.rubyonrails.com/rails/pages/HowToWritePluginToModifyRailsCore

perfect. thanks.

-a

===============================================================================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| – bodhicaryavatara


#6

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

require in my

-a

Ara- FYI you can require it with require_dependency if you want it to
be reloaded in dev mode in case you want to make changes to it
without restarting the webserver.

Cheers-
-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732


#7

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

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 J.
(canadaduane)