Class: Spree::Promotion::Rules::ItemTotal

Inherits:
Spree::PromotionRule show all
Defined in:
app/models/spree/promotion/rules/item_total.rb

Constant Summary collapse

OPERATORS_MIN =
['gt', 'gte']
OPERATORS_MAX =
['lt','lte']

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#actionable?, #eligibility_errors, for

Methods inherited from Base

page

Methods included from Spree::Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Method Details

#applicable?(promotable) ⇒ Boolean

Returns:

  • (Boolean)

16
17
18
# File 'app/models/spree/promotion/rules/item_total.rb', line 16

def applicable?(promotable)
  promotable.is_a?(Spree::Order)
end

#eligible?(order, options = {}) ⇒ Boolean

Returns:

  • (Boolean)

20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/spree/promotion/rules/item_total.rb', line 20

def eligible?(order, options = {})
  item_total = order.item_total

  lower_limit_condition = item_total.send(preferred_operator_min == 'gte' ? :>= : :>, BigDecimal.new(preferred_amount_min.to_s))
  upper_limit_condition = item_total.send(preferred_operator_max == 'lte' ? :<= : :<, BigDecimal.new(preferred_amount_max.to_s))

  eligibility_errors.add(:base, ineligible_message_max) unless upper_limit_condition
  eligibility_errors.add(:base, ineligible_message_min) unless lower_limit_condition

  eligibility_errors.empty?
end