vanderleipinto / preparation_mentorship

0 stars 0 forks source link

Validate CPF in Author #29

Closed vanderleipinto closed 9 months ago

vanderleipinto commented 9 months ago

Add Gem

The Gem 'cpf_cnpj' was added in the Validate CNPJ in Supplier

Add validates into the Author model including the callback validator 'validate_cnpj':

In the file: app/models/author.rb

class Author < ApplicationRecord
  has_many :books, dependent: :destroy

  validates :name, presence: true
  validates :cpf, presence: true, uniqueness: true

  validate :cpf, :validate_cpf

  private

  def validate_cpf
    errors.add :cpf, "is invalid" unless CPF.valid?(cpf)
  end

end