Class: Spree::Stock::AvailabilityValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/spree/stock/availability_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(line_item) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/models/spree/stock/availability_validator.rb', line 6

def validate(line_item)
  if is_valid?(line_item)
    true
  else
    variant = line_item.variant
    display_name = variant.name.to_s
    display_name += %{ (#{variant.options_text})} unless variant.options_text.blank?

    line_item.errors.add(:quantity, I18n.t(
      'spree.selected_quantity_not_available',
      item: display_name.inspect
    ))
    false
  end
end