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

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

Constant Summary collapse

MATCH_POLICIES =

Preferences

%w(any all)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#eligibility_errors, for, #human_description, #human_name, #key

Instance Attribute Details

#taxon_ids_to_addObject

Attributes



22
23
24
# File 'app/models/spree/promotion/rules/taxon.rb', line 22

def taxon_ids_to_add
  @taxon_ids_to_add
end

Instance Method Details

#actionable?(line_item) ⇒ Boolean

Returns:



55
56
57
# File 'app/models/spree/promotion/rules/taxon.rb', line 55

def actionable?(line_item)
  Spree::Classification.where(taxon_id: eligible_taxon_ids_including_children, product_id: line_item.product_id).exists?
end

#applicable?(promotable) ⇒ Boolean

Returns:



29
30
31
# File 'app/models/spree/promotion/rules/taxon.rb', line 29

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

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

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/spree/promotion/rules/taxon.rb', line 37

def eligible?(order, _options = {})
  return true if eligible_taxon_ids.empty?

  if preferred_match_policy == 'all'
    order_taxon_ids_with_ancestors = taxon_ids_in_order_including_ancestors(order)
    unless eligible_taxon_ids.all? { |id| order_taxon_ids_with_ancestors.include?(id) }
      eligibility_errors.add(:base, eligibility_error_message(:missing_taxon))
    end
  else
    order_taxon_ids_with_ancestors = taxon_ids_in_order_including_ancestors(order)
    unless eligible_taxon_ids.any? { |id| order_taxon_ids_with_ancestors.include?(id) }
      eligibility_errors.add(:base, eligibility_error_message(:no_matching_taxons))
    end
  end

  eligibility_errors.empty?
end

#eligible_taxon_idsObject



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

def eligible_taxon_ids
  @eligible_taxon_ids ||= promotion_rule_taxons.pluck(:taxon_id)
end

#taxon_ids_stringObject



59
60
61
62
63
64
# File 'app/models/spree/promotion/rules/taxon.rb', line 59

def taxon_ids_string
  ActiveSupport::Deprecation.warn(
    'Please use `taxon_ids=` instead.'
  )
  taxons.pluck(:id).join(',')
end

#taxon_ids_string=(s) ⇒ Object



66
67
68
69
70
71
72
# File 'app/models/spree/promotion/rules/taxon.rb', line 66

def taxon_ids_string=(s)
  ActiveSupport::Deprecation.warn(
    'Please use `taxon_ids=` instead.'
  )
  ids = s.to_s.split(',').map(&:strip)
  self.taxons = Spree::Taxon.for_stores(stores).find(ids)
end