Class: SolidusFriendlyPromotions::Benefit

Inherits:
Spree::Base
  • Object
show all
Includes:
Spree::AdjustmentSource, Spree::CalculatedAdjustments, Spree::Preferences::Persistable
Defined in:
app/models/solidus_friendly_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



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

def adjustment_label(adjustable)
  I18n.t(
    "solidus_friendly_promotions.adjustment_labels.#{adjustable.class.name.demodulize.underscore}",
    promotion: SolidusFriendlyPromotions::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_friendly_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_friendly_promotions/benefit.rb', line 70

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

#available_conditionsObject



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

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

#can_discount?(object) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


29
30
31
# File 'app/models/solidus_friendly_promotions/benefit.rb', line 29

def can_discount?(object)
  raise NotImplementedError
end

#compute_amount(adjustable) ⇒ Object

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



45
46
47
48
# File 'app/models/solidus_friendly_promotions/benefit.rb', line 45

def compute_amount(adjustable)
  promotion_amount = calculator.compute(adjustable) || BigDecimal("0")
  [adjustable.discountable_amount, promotion_amount.abs].min * -1
end

#discount(adjustable) ⇒ Object



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

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_friendly_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_friendly_promotions, :eligibility_errors]))
      )
    end

    eligible
  end.all?
end

#levelObject

Raises:

  • (NotImplementedError)


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

def level
  raise NotImplementedError
end

#possible_conditionsObject



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

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

#preload_relationsObject



25
26
27
# File 'app/models/solidus_friendly_promotions/benefit.rb', line 25

def preload_relations
  [:calculator]
end

#to_partial_pathObject



58
59
60
# File 'app/models/solidus_friendly_promotions/benefit.rb', line 58

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