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:

  • (Boolean)


48
49
50
51
52
53
54
55
# File 'app/models/spree/promotion/rules/taxon.rb', line 48

def actionable?(line_item)
  store = line_item.order.store

  store.products.
    joins(:classifications).
    where(Spree::Classification.table_name => { taxon_id: taxon_ids, product_id: line_item.product_id }).
    exists?
end

#applicable?(promotable) ⇒ Boolean

Returns:

  • (Boolean)


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:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/spree/promotion/rules/taxon.rb', line 33

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



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

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

#taxon_ids_string=(s) ⇒ Object



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

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