ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ericp <er...@caldergroup.com>
Subject RBatis validations and resultmaps
Date Sun, 07 Jan 2007 17:45:59 GMT

Hi,

My question is, how do I get a handle to the resultmaps for an RBatis
object at initialization time?

I've enjoyed using RBatis, but validations aren't working for me, I'm
using Rails 1.2. For example

  validates_numericality_of :field_id

Complains that :field_id_before_type_cast does not exist. OK, no problem
the work around is to alias the field so the validation will work

  alias :field_id_before_type_cast :field_id
  validates_numericality_of :field_id

I would like to get rid of the explicit alias, and just have RBatis take
 care of creating the *_before_type_cast attributes. I can create a
method to dynamically add the attributes, but need to know what
attributes to add. The result map, offers a data-structure with the
attributes defined as hash keys, so it seems like a good place to start.

I propose adding something like the following to RBatis::Base

    # Creates new instance can optionally pass
    # Hash to initialize all attributes.
    def initialize(attributes={})
      self.attributes = attributes

      # HOWTO do the following -->
      resultmaps.each.keys.each do |name|
	alias_attr_for_validation(name)
      end
      # <-- END NEW block

    end

    # NEW METHOD
    # Called at initialization time
    # to alias the attributes to their
    # "raw" values, this is required by
    # ActiveRecord::Validations
    #
    def alias_attr_for_validation(name)
      @name = name
      instance_eval "
       if self.respond_to?(name)
          alias :#{name}_before_type_cast :#{name}
       end
     "
    end



If this is a decent proposal, some help with the resultmap block would
be appreciated.

A nice possible extension would be to dynamically define the
attr_accessors along with the attribute alias off of the resultmaps.
That way once the resultmap is defined you are off and running, only
needing the map the sql results into the resultmap.

Thanks
ERIC

Mime
View raw message