Class: Spree::QuantityRule

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model
Defined in:
app/models/spree/quantity_rule.rb

Overview

A resolved pair of purchasing rules for one variant and one buyer: the least they may order and the increment they must land on. MOQ 48 with multiple 24 admits 48, 72, 96 and refuses 50.

Resolution (variant base -> catalog default -> catalog x variant override) happens in Catalogs::ResolveQuantityRules; this object is only the answer and the arithmetic over it, so the same rules apply identically wherever a quantity is checked or a stepper is drawn.

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



105
106
107
# File 'app/models/spree/quantity_rule.rb', line 105

def ==(other)
  other.is_a?(self.class) && minimum == other.minimum && multiple == other.multiple
end

#hash ⇒ Object



110
111
112
# File 'app/models/spree/quantity_rule.rb', line 110

def hash
  [minimum, multiple].hash
end

#minimum ⇒ Integer

Returns the least quantity that may be ordered.

Returns:

  • (Integer) —

    the least quantity that may be ordered



18
19
20
21
# File 'app/models/spree/quantity_rule.rb', line 18

def minimum
  value = minimum_order_quantity.to_i
  value.positive? ? value : 1
end

#multiple ⇒ Integer

Returns the increment quantities must land on.

Returns:

  • (Integer) —

    the increment quantities must land on



24
25
26
27
# File 'app/models/spree/quantity_rule.rb', line 24

def multiple
  value = order_multiple.to_i
  value.positive? ? value : 1
end

#nearest_valid(quantity) ⇒ Array<Integer>

The valid quantities either side of an invalid one, used to tell a buyer what they may order instead. Both are present unless the quantity is below the minimum, where there is nothing lower to offer.

Parameters:

  • quantity (Integer)

Returns:

  • (Array<Integer>) —

    one or two ascending quantities



71
72
73
# File 'app/models/spree/quantity_rule.rb', line 71

def nearest_valid(quantity)
  [previous_valid(quantity), next_valid(quantity)].compact.uniq
end

#next_valid(quantity) ⇒ Integer

The smallest valid quantity at or above the given one.

Parameters:

  • quantity (Integer)

Returns:

  • (Integer)


57
58
59
60
61
62
63
# File 'app/models/spree/quantity_rule.rb', line 57

def next_valid(quantity)
  quantity = quantity.to_i
  return minimum if quantity <= minimum

  remainder = (quantity - offset) % multiple
  remainder.zero? ? quantity : quantity + (multiple - remainder)
end

#offset ⇒ Object

Steps are counted from a DECLARED minimum rather than from zero, so a rule of "at least 50, in 24s" offers 50 / 74 / 98 instead of refusing its own minimum.

Only a declared one: the implicit minimum of 1 is the absence of a rule, and letting it set the offset would turn "sold in packs of 5" into 1 / 6 / 11, refusing a buyer who orders exactly one pack. A minimum that already sits on the multiple leaves this at 0 either way, and the arithmetic is the plain modulo it looks like.



99
100
101
102
103
# File 'app/models/spree/quantity_rule.rb', line 99

def offset
  return 0 unless minimum_order_quantity.to_i.positive?

  minimum % multiple
end

#previous_valid(quantity) ⇒ Integer?

The largest valid quantity at or below the given one, or nil when the minimum is already above it.

Parameters:

  • quantity (Integer)

Returns:

  • (Integer, nil)


47
48
49
50
51
52
# File 'app/models/spree/quantity_rule.rb', line 47

def previous_valid(quantity)
  quantity = quantity.to_i
  return nil if quantity < minimum

  quantity - ((quantity - offset) % multiple)
end

#satisfied_by?(quantity) ⇒ Boolean

Parameters:

  • quantity (Integer)

Returns:

  • (Boolean)


38
39
40
41
# File 'app/models/spree/quantity_rule.rb', line 38

def satisfied_by?(quantity)
  quantity = quantity.to_i
  quantity >= minimum && ((quantity - offset) % multiple).zero?
end

#unrestricted? ⇒ Boolean

True when neither rule narrows anything — the shape a retail buyer sees, and what a variant with empty columns resolves to.

Returns:

  • (Boolean)


32
33
34
# File 'app/models/spree/quantity_rule.rb', line 32

def unrestricted?
  minimum == 1 && multiple == 1
end

#violation_message(name, quantity) ⇒ String?

Why a quantity was refused, naming what may be ordered instead — nil when the quantity is fine. Lives here beside the arithmetic so the two cart workflows and the completion check all refuse in the same words; the server never rounds, so the neighbours are the whole answer.

Parameters:

  • name (String) —

    what the buyer called for

  • quantity (Integer)

Returns:

  • (String, nil)


83
84
85
86
87
88
# File 'app/models/spree/quantity_rule.rb', line 83

def violation_message(name, quantity)
  return nil if satisfied_by?(quantity)

  I18n.t('spree.cart_line_item.quantity_rule_violated',
          li_name: name, quantities: nearest_valid(quantity).to_sentence)
end