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

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

Overview

A rule to apply to an order greater than (or greater than or equal to) a specific amount

To add extra operators please override `self.operators_map` or any other helper method. To customize the error message you can also override `ineligible_message`.

Constant Summary collapse

OPERATORS =
Deprecated.
operators_map.keys.map(&:to_s)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#actionable?, #eligibility_errors, for, #preload_relations, #to_partial_path

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Class Method Details

.operator_optionsObject


26
27
28
29
30
# File 'app/models/spree/promotion/rules/item_total.rb', line 26

def self.operator_options
  operators_map.map do |name, _method|
    [I18n.t(name, scope: 'spree.item_total_rule.operators'), name]
  end
end

.operators_mapObject

The list of allowed operators names mapped to their symbols.


19
20
21
22
23
24
# File 'app/models/spree/promotion/rules/item_total.rb', line 19

def self.operators_map
  {
    gte: :>=,
    gt: :>,
  }
end

Instance Method Details

#applicable?(promotable) ⇒ Boolean

Returns:

  • (Boolean)

41
42
43
# File 'app/models/spree/promotion/rules/item_total.rb', line 41

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

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

Returns:

  • (Boolean)

45
46
47
48
49
50
51
52
53
# File 'app/models/spree/promotion/rules/item_total.rb', line 45

def eligible?(order, _options = {})
  return false unless order.currency == preferred_currency

  unless total_for_order(order).send(operator, threshold)
    eligibility_errors.add(:base, ineligible_message, error_code: ineligible_error_code)
  end

  eligibility_errors.empty?
end