summaryrefslogtreecommitdiff
path: root/app/models/solidus_subscriptions/subscription_promotion_rule.rb
blob: fd99979ee2096e73bd34ce31fd0f54b028e3fcf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

module SolidusSubscriptions
  class SubscriptionPromotionRule < ::Spree::PromotionRule
    # Promotion can be applied to an entire order. Will only be true
    # for Spree::Order
    #
    # @param promotable [Object] Any object which could have this
    #   promotion rule applied to it.
    #
    # @return [Boolean]
    def applicable?(promotable)
      promotable.is_a? ::Spree::Order
    end

    # An order is eligible if it contains a line item with an associates
    # subscription_line_item.  This rule applies to order and so its eligibility
    # will always be considered against an order. Will only return true for
    # orders containing Spree::Line item with associated subscription_line_items
    #
    # @param order [Spree::Order] The order which could have this rule applied
    #   to it.
    #
    # @return [Boolean]
    def eligible?(order, **_options)
      order.subscription_line_items.any?
    end

    # Certain actions create adjustments on line items. In this case, only
    # line items with associated subscription_line_items are eligible to be
    # adjusted. Will only return true # if :line_item has an associated
    # subscription.
    #
    # @param line_item [Spree::LineItem] The line item which could be adjusted
    #   by the promotion.
    def actionable?(line_item)
      line_item.subscription_line_items.present?
    end
  end
end