Class: SolidusPromotions::Benefit
- Inherits:
-
Spree::Base
- Object
- Spree::Base
- SolidusPromotions::Benefit
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
51
52
53
54
55
56
57
|
# File 'app/models/solidus_promotions/benefit.rb', line 51
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
104
105
106
107
108
|
# File 'app/models/solidus_promotions/benefit.rb', line 104
def applicable_line_items(order)
order.discountable_line_items.select do |line_item|
eligible_by_applicable_conditions?(line_item)
end
end
|
#available_calculators ⇒ Object
72
73
74
|
# File 'app/models/solidus_promotions/benefit.rb', line 72
def available_calculators
SolidusPromotions.config.promotion_calculators[self.class] || []
end
|
#available_conditions ⇒ Object
68
69
70
|
# File 'app/models/solidus_promotions/benefit.rb', line 68
def available_conditions
possible_conditions - conditions.select(&:persisted?)
end
|
#can_discount?(object) ⇒ Boolean
29
30
31
32
|
# File 'app/models/solidus_promotions/benefit.rb', line 29
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
46
47
48
49
|
# File 'app/models/solidus_promotions/benefit.rb', line 46
def compute_amount(adjustable)
promotion_amount = calculator.compute(adjustable) || BigDecimal("0")
[adjustable.discountable_amount, promotion_amount.abs].min * -1
end
|
#discount(adjustable) ⇒ Object
34
35
36
37
38
39
40
41
42
43
|
# File 'app/models/solidus_promotions/benefit.rb', line 34
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
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
101
102
|
# File 'app/models/solidus_promotions/benefit.rb', line 76
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
|
#level ⇒ Object
63
64
65
66
|
# File 'app/models/solidus_promotions/benefit.rb', line 63
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_conditions ⇒ Object
110
111
112
|
# File 'app/models/solidus_promotions/benefit.rb', line 110
def possible_conditions
Set.new(SolidusPromotions.config.order_conditions)
end
|
#preload_relations ⇒ Object
25
26
27
|
# File 'app/models/solidus_promotions/benefit.rb', line 25
def preload_relations
[:calculator]
end
|
#to_partial_path ⇒ Object
59
60
61
|
# File 'app/models/solidus_promotions/benefit.rb', line 59
def to_partial_path
"solidus_promotions/admin/benefit_fields/#{model_name.element}"
end
|