Class: CatalogItem::QuantityBoundsValidator

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

Overview

Validates the consistency of a catalog item's quantity discount tiers:
lower bounds and price discounts must be paired, strictly increasing,
quantity_discount_price_type must be set when any bound is present,
and percent discounts must fall between 1 and 50.

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ void

This method returns an undefined value.

Runs all quantity-bounds consistency checks on the record.

Parameters:

  • record (CatalogItem)

    the catalog item being validated



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/validators/catalog_item/quantity_bounds_validator.rb', line 12

def validate(record)
  # Validate that quantity_discount_price_type is present if any quantity bounds are set
  validate_discount_price_type_presence(record)

  # Validate bounds and discount values
  (1..4).each do |i|
    lower_bound = record.send(:"quantity_#{i}_lower_bound")
    higher_bound = record.send(:"quantity_#{i + 1}_lower_bound")
    price_discount = record.send(:"quantity_#{i}_price_discount")
    next_price_discount = record.send(:"quantity_#{i + 1}_price_discount")

    validate_pair_presence(record, i, lower_bound, price_discount)
    validate_order(record, i, lower_bound, higher_bound, price_discount, next_price_discount)
    validate_price_discount_range(record, i, price_discount)
  end
  validate_pair_presence(record, 5, record.quantity_5_lower_bound, record.quantity_5_price_discount)
  validate_price_discount_range(record, 5, record.quantity_5_price_discount)
end