Class: SpreeCmCommissioner::Promotion::Rules::Date

Inherits:
Spree::PromotionRule
  • Object
show all
Defined in:
app/models/spree_cm_commissioner/promotion/rules/date.rb

Direct Known Subclasses

CustomDates, FixedDate, Weekend

Constant Summary collapse

MATCH_POLICIES =
%w[any all].freeze

Instance Method Summary collapse

Instance Method Details

#actionable?(line_item) ⇒ Boolean

override

Returns:

  • (Boolean)


38
39
40
# File 'app/models/spree_cm_commissioner/promotion/rules/date.rb', line 38

def actionable?(line_item)
  line_item_eligible?(line_item)
end

#applicable?(promotable) ⇒ Boolean

override

Returns:

  • (Boolean)


14
15
16
# File 'app/models/spree_cm_commissioner/promotion/rules/date.rb', line 14

def applicable?(promotable)
  promotable.is_a?(Spree::Order) && promotable.line_items.any?(&:date_present?)
end

#date_eligible?(_date) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/spree_cm_commissioner/promotion/rules/date.rb', line 42

def date_eligible?(_date)
  raise 'date_eligible? should be implemented in a sub-class of SpreeCmCommissioner::Promotion::Rules::Date'
end

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

override use to check whether promotion is eligible to this rule

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
# File 'app/models/spree_cm_commissioner/promotion/rules/date.rb', line 20

def eligible?(order, _options = {})
  case preferred_match_policy
  when 'any'
    order.line_items.any? { |item| line_item_eligible?(item) }
  when 'all'
    order.line_items.all? { |item| line_item_eligible?(item) }
  end
end

#line_item_eligible?(line_item) ⇒ Boolean

line items is considered eligible when any of date range is eligible. then, we use action to apply promotion only on eligible dates.

Returns:

  • (Boolean)


31
32
33
34
35
# File 'app/models/spree_cm_commissioner/promotion/rules/date.rb', line 31

def line_item_eligible?(line_item)
  return false unless line_item.date_present?

  line_item.date_range.any? { |date| date_eligible?(date) }
end