Class: Spree::Promotion::Rules::Country

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

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#actionable?, additional_permitted_attributes, #eligibility_errors, for, human_description, #human_description, human_name, #human_name, #key

Instance Method Details

#applicable?(promotable) ⇒ Boolean

Returns:



15
16
17
# File 'app/models/spree/promotion/rules/country.rb', line 15

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

#countriesObject



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

def countries
  isos = preferred_country_isos.presence || [preferred_country_iso].compact_blank
  return Spree::Country.none if isos.blank?

  Spree::Country.where(iso: isos.map { |s| s.to_s.upcase })
end

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

Returns:



26
27
28
29
30
31
32
33
34
# File 'app/models/spree/promotion/rules/country.rb', line 26

def eligible?(order, options = {})
  allowed_isos = eligible_country_isos(order)
  shipping_iso = options[:country_iso] || order.ship_address&.country_iso

  return true if allowed_isos.include?(shipping_iso)

  eligibility_errors.add(:base, eligibility_error_message(:wrong_country))
  false
end

#eligible_country_isos(order = nil) ⇒ Object

Effective list of eligible country ISOs, merging legacy single-country preferences into the multi-country list. Order-of-precedence: explicit ISO list > legacy single ISO > legacy single ID > store default. Memoized per-instance —eligibility checks fire repeatedly per cart change.



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

def eligible_country_isos(order = nil)
  @eligible_country_isos ||= compute_eligible_country_isos(order)
end