Class: SolidusPromotions::Benefit

Inherits:
Spree::Base
  • Object
show all
Includes:
Spree::AdjustmentSource, Spree::CalculatedAdjustments, Spree::Preferences::Persistable
Defined in:
app/models/solidus_promotions/benefit.rb

Overview

Base class for all types of benefit.

Benefits perform the necessary tasks when a promotion is activated by an event and determined to be eligible.

Instance Method Summary collapse

Instance Method Details

#adjustment_label(adjustable) ⇒ Object



49
50
51
52
53
54
55
# File 'app/models/solidus_promotions/benefit.rb', line 49

def adjustment_label(adjustable)
  I18n.t(
    "solidus_promotions.adjustment_labels.#{adjustable.class.name.demodulize.underscore}",
    promotion: SolidusPromotions::Promotion.model_name.human,
    promotion_customer_label: promotion.customer_label
  )
end

#applicable_line_items(order) ⇒ Object



102
103
104
105
106
# File 'app/models/solidus_promotions/benefit.rb', line 102

def applicable_line_items(order)
  order.discountable_line_items.select do |line_item|
    eligible_by_applicable_conditions?(line_item)
  end
end

#available_calculatorsObject



70
71
72
# File 'app/models/solidus_promotions/benefit.rb', line 70

def available_calculators
  SolidusPromotions.config.promotion_calculators[self.class] || []
end

#available_conditionsObject



66
67
68
# File 'app/models/solidus_promotions/benefit.rb', line 66

def available_conditions
  possible_conditions - conditions.select(&:persisted?)
end

#can_discount?(object) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


27
28
29
30
# File 'app/models/solidus_promotions/benefit.rb', line 27

def can_discount?(object)
  raise NotImplementedError, "Please implement the correct interface, or include one of the `SolidusPromotions::Benefits::OrderBenefit`, " \
    "`SolidusPromotions::Benefits::LineItemBenefit` or `SolidusPromotions::Benefits::ShipmentBenefit` modules"
end

#compute_amount(adjustable) ⇒ Object

Ensure a negative amount which does not exceed the object’s amount



44
45
46
47
# File 'app/models/solidus_promotions/benefit.rb', line 44

def compute_amount(adjustable)
  promotion_amount = calculator.compute(adjustable) || Spree::ZERO
  [adjustable.discountable_amount, promotion_amount.abs].min * -1
end

#discount(adjustable) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'app/models/solidus_promotions/benefit.rb', line 32

def discount(adjustable)
  amount = compute_amount(adjustable)
  return if amount.zero?
  ItemDiscount.new(
    item: adjustable,
    label: adjustment_label(adjustable),
    amount: amount,
    source: self
  )
end

#eligible_by_applicable_conditions?(promotable, dry_run: false) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/solidus_promotions/benefit.rb', line 74

def eligible_by_applicable_conditions?(promotable, dry_run: false)
  applicable_conditions = conditions.select do |condition|
    condition.applicable?(promotable)
  end

  applicable_conditions.map do |applicable_condition|
    eligible = applicable_condition.eligible?(promotable)

    break [false] if !eligible && !dry_run

    if dry_run
      if applicable_condition.eligibility_errors.details[:base].first
        code = applicable_condition.eligibility_errors.details[:base].first[:error_code]
        message = applicable_condition.eligibility_errors.full_messages.first
      end
      promotion.eligibility_results.add(
        item: promotable,
        condition: applicable_condition,
        success: eligible,
        code: eligible ? nil : (code || :coupon_code_unknown_error),
        message: eligible ? nil : (message || I18n.t(:coupon_code_unknown_error, scope: [:solidus_promotions, :eligibility_errors]))
      )
    end

    eligible
  end.all?
end

#levelObject

Raises:

  • (NotImplementedError)


61
62
63
64
# File 'app/models/solidus_promotions/benefit.rb', line 61

def level
  raise NotImplementedError, "Please implement the correct interface, or include one of the `SolidusPromotions::Benefits::OrderBenefit`, " \
    "`SolidusPromotions::Benefits::LineItemBenefit` or `SolidusPromotions::Benefits::ShipmentBenefit` modules"
end

#possible_conditionsObject



108
109
110
# File 'app/models/solidus_promotions/benefit.rb', line 108

def possible_conditions
  Set.new(SolidusPromotions.config.order_conditions)
end

#preload_relationsObject



23
24
25
# File 'app/models/solidus_promotions/benefit.rb', line 23

def preload_relations
  [:calculator]
end

#to_partial_pathObject



57
58
59
# File 'app/models/solidus_promotions/benefit.rb', line 57

def to_partial_path
  "solidus_promotions/admin/benefit_fields/#{model_name.element}"
end