using same validation-set in more than one class

hello,
lets describe my situation: i have two classes which have a lot same
validations defined ... to stay DRY i want to put them in one place and
use it within both ActiveRecord classes.

- i think subclassing is not possible because both classes are AR of
different tables

- tried to include a module but this didnt work either:

module CustomExtensions
validates_length_of :a, :in => 1..10
validates_length_of :b, :in => 1..20
end

class A < AR::Base
  include CustomExtensions
end

thx for help!

hello,
lets describe my situation: i have two classes which have a lot same
validations defined ... to stay DRY i want to put them in one place
and
use it within both ActiveRecord classes.

- i think subclassing is not possible because both classes are AR of
different tables

- tried to include a module but this didnt work either:

module CustomExtensions
validates_length_of :a, :in => 1..10
validates_length_of :b, :in => 1..20
end

class A < AR::Base
include CustomExtensions
end

nearly there!

module CommonStuff
   def self.included(base)
     base.instance_eval do
       validates_length_of :a, :in => 1..10
       validates_length_of :b, :in => 1..20
     end
   end
   ...
end

class A < AR::Base
   include CommonStuff
end

hey thx for the quick reply .. it worked!
i am just wondering why its "instance_eval" and not "class_eval"...
should the validations not be applied to the whole class?

hey thx for the quick reply .. it worked!
i am just wondering why its "instance_eval" and not "class_eval"...
should the validations not be applied to the whole class?

In this particular case it doesn't make any difference. instance_eval
means that self will be the base class (ie A) when the validates_*
calls are made. module_eval (which is the same as class eval) would do
the same thing (it would be different if we were defining methods
inside the eval block, but we're not)

Fred

Frederick Cheung wrote:

In this particular case it doesn't make any difference. instance_eval
means that self will be the base class (ie A) when the validates_*
calls are made. module_eval (which is the same as class eval) would do
the same thing (it would be different if we were defining methods
inside the eval block, but we're not)

i see.. you mean if we would do this??

def self.included(base)
  class_eval do
    ...
  end
end

Frederick Cheung wrote:

In this particular case it doesn't make any difference. instance_eval
means that self will be the base class (ie A) when the validates_*
calls are made. module_eval (which is the same as class eval) would
do
the same thing (it would be different if we were defining methods
inside the eval block, but we're not)

i see.. you mean if we would do this??

I mean
module CommonStuff
  def self.included(base)
    base.instance_eval do
      validates_length_of :a, :in => 1..10
      validates_length_of :b, :in => 1..20
    end
  end

has the same effect as

module CommonStuff
  def self.included(base)
    base.class_eval do
      validates_length_of :a, :in => 1..10
      validates_length_of :b, :in => 1..20
    end
  end

but if your eval block had

def foo
end
then the method would be added in different places (A.foo instead of
A.new.foo)

Fred

Frederick Cheung wrote:

oh i see now... thanks for your time! learned something again