Forum: Ruby on Rails modifying ActiveRecord::Base - where?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 ara.t.howard (Guest)
on 2005-12-06 06:19
(Received via mailing list)
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
===============================================================================
D8cb8c8cd40ddf0cd05241443a591868?d=identicon&s=25 duane.johnson (Guest)
on 2005-12-06 07:24
(Received via mailing list)
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 Johnson
(canadaduane)
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 ara.t.howard (Guest)
on 2005-12-06 08:09
(Received via mailing list)
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?

> 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
===============================================================================
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 ezra (Guest)
on 2005-12-06 17:49
(Received via mailing list)
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 Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 petermichaux (Guest)
on 2005-12-06 19:07
(Received via mailing list)
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 ara.t.howard (Guest)
on 2005-12-06 19:27
(Received via mailing list)
On Tue, 6 Dec 2005, Peter Michaux wrote:

> wiki about modifying rails core
>
> http://wiki.rubyonrails.com/rails/pages/HowToWrite...

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
===============================================================================
D8cb8c8cd40ddf0cd05241443a591868?d=identicon&s=25 duane.johnson (Guest)
on 2005-12-07 02:35
(Received via mailing list)
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 Johnson
(canadaduane)
This topic is locked and can not be replied to.