Class: PartySizeValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/party_size_validator.rb

Overview

typed: ignore frozen_string_literal: true

Instance Method Summary collapse

Instance Method Details

#validate(reservation) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/validators/party_size_validator.rb', line 4

def validate(reservation)
  return false if reservation.restaurant.nil?
  return true if reservation.package.present?
  if reservation.kids.to_i.positive? && !reservation.adult.to_i.positive?
    reservation.errors.add(:adult, 'is invalid, adult should greater than or equal 1')
    return false
  end
  party = reservation.party_size.to_i
  unless party.positive?
    return true if reservation.adult.to_i.positive?
    reservation.errors.add(:party_size, 'is invalid, party size should greater than or equal 1')
    return false
  end

  return true if reservation.party_size >= 6

  return true if reservation.restaurant.party_size_range.include?(reservation.party_size.to_i)
  reservation.errors.add(:party_size, "is invalid, allowed party size is between #{reservation.restaurant.min_party_size} and #{reservation.restaurant.largest_table}")
  false
end