Class: Spree::Promotion::Rules::Taxon

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

Constant Summary collapse

MATCH_POLICIES =
%w(any all)

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#eligibility_errors, for

Methods inherited from Base

belongs_to_required_by_default, page, spree_base_scopes

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

#actionable?(line_item) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/spree/promotion/rules/taxon.rb', line 32

def actionable?(line_item)
  taxon_product_ids.include? line_item.variant.product_id
end

#applicable?(promotable) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'app/models/spree/promotion/rules/taxon.rb', line 13

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

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

Returns:

  • (Boolean)


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

def eligible?(order, _options = {})
  if preferred_match_policy == 'all'
    unless (taxons.to_a - taxons_in_order_including_parents(order)).empty?
      eligibility_errors.add(:base, eligibility_error_message(:missing_taxon))
    end
  else
    order_taxons = taxons_in_order_including_parents(order)
    unless taxons.any? { |taxon| order_taxons.include? taxon }
      eligibility_errors.add(:base, eligibility_error_message(:no_matching_taxons))
    end
  end

  eligibility_errors.empty?
end

#taxon_ids_stringObject



36
37
38
# File 'app/models/spree/promotion/rules/taxon.rb', line 36

def taxon_ids_string
  taxons.pluck(:id).join(',')
end

#taxon_ids_string=(s) ⇒ Object



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

def taxon_ids_string=(s)
  ids = s.to_s.split(',').map(&:strip)
  self.taxons = Spree::Taxon.find(ids)
end