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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

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

Instance Attribute Details

#taxon_ids_to_addObject

Attributes



45
46
47
# File 'app/models/spree/promotion/rules/taxon.rb', line 45

def taxon_ids_to_add
  @taxon_ids_to_add
end

Class Method Details

.additional_permitted_attributesObject



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

def self.additional_permitted_attributes
  [category_ids: []]
end

.api_typeObject

Wire-format shorthand is ‘category` (the model is still `Taxon` pre-6.0 rename). `key` (instance) cascades through `api_type`.



19
20
21
# File 'app/models/spree/promotion/rules/taxon.rb', line 19

def self.api_type
  'category'
end

Instance Method Details

#actionable?(line_item) ⇒ Boolean

Returns:



78
79
80
# File 'app/models/spree/promotion/rules/taxon.rb', line 78

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:



52
53
54
# File 'app/models/spree/promotion/rules/taxon.rb', line 52

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

#category_idsObject



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

def category_ids
  taxon_ids
end

#category_ids=(ids) ⇒ Object

PrefixedId’s auto-resolver in ‘assign_attributes` only fires when the `_ids` stem matches an association — `categories` doesn’t, so decode prefixed IDs explicitly here.



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

def category_ids=(ids)
  self.taxon_ids = Array(ids).map do |id|
    Spree::PrefixedId.prefixed_id?(id) ? Spree::Taxon.find_by_param!(id).id : id
  end
end

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

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/spree/promotion/rules/taxon.rb', line 60

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



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

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

#taxon_ids_stringObject



82
83
84
85
86
87
# File 'app/models/spree/promotion/rules/taxon.rb', line 82

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

#taxon_ids_string=(s) ⇒ Object



89
90
91
92
93
94
95
# File 'app/models/spree/promotion/rules/taxon.rb', line 89

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