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

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?
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.