Extending activeRecord::Base

Hello,

I want to add some behaviour to ActiveRecord::Base, so I did this:

  1. Added ActiveRecordExtensions.rb in lib directory
  2. Added the following code to this file

module ActiveRecordExtended

class ActiveRecord::Base
def date_formatter_for(field_name)
module_eval(
“def solicitation_date_formatted \n” +

end
end

  1. Added the following line to environment.rb

require ‘ActiveRecordExtensions’

  1. In every model that I want to use the extension I add the following
    line

    include ActiveRecordExtended

It works well, I have all the functions available in my model. But,
validations just stop working. all validates_presence_of just don’t
work. Anybody knows what could be this?

Thanks in advance.


Ricardo A.
[email protected]
Acras Desenvolvimento de Sistemas
+55+41-3232-6404
www.acras.net

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ricardo A. wrote:

def date_formatter_for(field_name)
4) In every model that I want to use the extension I add the following line

include ActiveRecordExtended

It works well, I have all the functions available in my model. But,
validations just stop working. all validates_presence_of just don’t
work. Anybody knows what could be this?

Ricardo,

It really depends what your code is in your module. Could you post
that to the list? Also, ActiveRecord::Extensions is already an existing
plugin, which adds plugin architecture to ActiveRecord. You may not want
to use that name (or maybe you do),

Zach
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFRM+VMyx0fW1d8G0RAhjrAJ950H2NAPot7NGrQdrgdJvSLkXCsACfWL+N
hfm1A+cUm8QvsxJKNTr9YYY=
=EySQ
-----END PGP SIGNATURE-----

Ricardo,

It really depends what your code is in your module. Could you post
that to the list? Also, ActiveRecord::Extensions is already an existing
plugin, which adds plugin architecture to ActiveRecord. You may not want
to use that name (or maybe you do),

Zach

Hello,

I guess I found a solution. My code now is like this:

module ActiveRecord
def Base.date_formatter_for(field_name)
module_eval(
“def solicitation_date_formatted \n” +
" solicitation_date.strftime ‘%d/%m/%Y’ if solicitation_date
!= nil \n" +
“end \n\n”
)

 module_eval(
   "def solicitation_date_formatted=(value) \n " +
   "self.solicitation_date = Date.strptime(value,'%d/%m/%Y') \n " +
   "end \n "
 )

end
end

Notice that I deleted the line

class ActiveRecord::Base

from the declaration. I think I, in some way, passed through the
inheritances of the class by redeclaring it. Declaring just the
methods works fine.

Thank you