vanderleipinto / preparation_mentorship

0 stars 0 forks source link

feat: Validate ISBN in Book #36

Closed vanderleipinto closed 9 months ago

vanderleipinto commented 9 months ago

Add Gem

Add gem 'isbn' into the project. In the file Gemfile:

# Validator to ISBN
gem 'isbn', '~> 2.0', '>= 2.0.11'

Runs bundle to install the Gem

bundle

Add validates into the Book model including the callback validator '':

class Book < ApplicationRecord
  belongs_to :author
  has_and_belongs_to_many :assemblies

  validates :name, presence: true
  validate :isbn, :isbn_valid

  private

  def isbn_valid
    errors.add :isbn, "invalid isbn." unless ISBN.valid?(isbn)
  end
end

OBS: The attribute name will be changed further to title.