Forum: Ruby on Rails Keep all validations inside a single ActiveModel::Validator class

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.
8c01de31d4ea71756e85a0aca3852e2e?d=identicon&s=25 Irio Musskopf Junior (irio)
on 2012-11-04 03:41
Hi!

I plan to use ActiveModel::Validator to validate a plain Ruby class, but
I have intention to keep all validations outside the original class,
while using PresenceValidator and FormatValidator, default on Rails.
Something like this:

# payer.rb
module MyMoip
  class Payer
    include ActiveModel::Validations

    attr_accessor :id, :name, :email,
                  :address_street, :address_street_number,
:address_street_extra,
                  :address_neighbourhood, :address_city, :address_state,
                  :address_country, :address_cep, :address_phone

    validates_with Validators::PayerValidator
  end
end

# payer_validator.rb
module MyMoip
  module Validators
    class PayerValidator < ActiveModel::Validator

      validate :id, presence: true

      def validate(record)
        # more complex validations
      end
    end
  end
end


It is possible?
This topic is locked and can not be replied to.