ActiveModel::Validations in another module?

Hey,

I’m trying to specify validations with ActiveModel::Validations at the
module level, i.e., I wish to mix-in validations to a module. I am not
succeeding at this, however. How do I mix-in ActiveModel::Validations
into another module and use its methods just as I would in a class?

Thanks,

L

On Oct 29, 9:47pm, Grary S. [email protected] wrote:

Hey,

I’m trying to specify validations with ActiveModel::Validations at the
module level, i.e., I wish to mix-in validations to a module. I am not
succeeding at this, however. How do I mix-in ActiveModel::Validations
into another module and use its methods just as I would in a class?

I’m not sure what you mean. Do you want want to put a shared set of
validations in a module or create a module that when included adds a
set of validations (among other things)

Fred

Frederick,

I seek to create Ruby modules that use ActiveModel::Validations. The
examples I have seen of ActiveModel::Validations all involve including
that module in classes. This is not what I want. I want to involve the
use of ActiveModel::Validations in a module, which I may then choose to
implement as a ruby class (even if after a few more descents via other
modules.)

In short, how do I do get to do the following (or its equivalent) in a
module?

validates :some_attribute, :numericality_of=>{:greater_than => 0,
:allow_blank=>true}

G

On Oct 30, 9:15am, Grary S. [email protected] wrote:

module?

validates :some_attribute, :numericality_of=>{:greater_than => 0,
:allow_blank=>true}

I think ActiveSupport::Concern is what you’re looking for here - you
could create a module that defines validations like this:

module SomeModule
extend ActiveSupport::Concern
include ActiveModel::Validations

included do
validates :some_attribute, …
end
end

–Matt J.

Matt,

Thanks, that did the trick, and I think it unlikely that I would have
found that without some help!

Now, on to what is likely the subject of yet another post: How do I
obtain the effect of ActiveModel::Validations#validates_associated if
applied to non-ActiveRecord classes where I seek to obtain nested
validations when the given class is composed of multiple Ruby objects…

G