Class: Spree::Commissions::Context
- Inherits:
-
Object
- Object
- Spree::Commissions::Context
- Defined in:
- app/models/spree/commissions/context.rb
Overview
What a commission rule is asked about: one sale, and everything a rule might want to know about it.
Assembled once per line rather than let each rule reach into the record itself, so a rule stays a predicate and the expensive parts — the category walk especially — are resolved for the whole order up front. Mirrors Spree::Pricing::Context, which serves price rules the same way.
Instance Attribute Summary collapse
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#fulfillment ⇒ Object
readonly
Returns the value of attribute fulfillment.
-
#line_item ⇒ Object
readonly
Returns the value of attribute line_item.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#seller ⇒ Object
readonly
Returns the value of attribute seller.
Instance Method Summary collapse
-
#categories ⇒ Array<Spree::Category>
The product's categories and every category above them.
-
#commission_basis(rate = nil) ⇒ BigDecimal?
The money a rule about value compares against.
-
#initialize(seller:, order:, line_item: nil, fulfillment: nil, currency: nil, categories: nil) ⇒ Context
constructor
A new instance of Context.
-
#product ⇒ Spree::Product?
Nil when commissioning a delivery.
-
#subject ⇒ Spree::LineItem, ...
What is being commissioned — the item, or the delivery.
Constructor Details
#initialize(seller:, order:, line_item: nil, fulfillment: nil, currency: nil, categories: nil) ⇒ Context
Returns a new instance of Context.
23 24 25 26 27 28 29 30 |
# File 'app/models/spree/commissions/context.rb', line 23 def initialize(seller:, order:, line_item: nil, fulfillment: nil, currency: nil, categories: nil) @seller = seller @order = order @line_item = line_item @fulfillment = fulfillment @currency = currency || order&.currency @categories = categories end |
Instance Attribute Details
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
13 14 15 |
# File 'app/models/spree/commissions/context.rb', line 13 def currency @currency end |
#fulfillment ⇒ Object (readonly)
Returns the value of attribute fulfillment.
13 14 15 |
# File 'app/models/spree/commissions/context.rb', line 13 def fulfillment @fulfillment end |
#line_item ⇒ Object (readonly)
Returns the value of attribute line_item.
13 14 15 |
# File 'app/models/spree/commissions/context.rb', line 13 def line_item @line_item end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
13 14 15 |
# File 'app/models/spree/commissions/context.rb', line 13 def order @order end |
#seller ⇒ Object (readonly)
Returns the value of attribute seller.
13 14 15 |
# File 'app/models/spree/commissions/context.rb', line 13 def seller @seller end |
Instance Method Details
#categories ⇒ Array<Spree::Category>
The product's categories and every category above them. Empty when commissioning a delivery, which belongs to no category.
48 49 50 51 52 |
# File 'app/models/spree/commissions/context.rb', line 48 def categories return @categories if @categories @categories = product ? Spree::Commissions::ResolveRate.categories_for([product]).fetch(product.id, []) : [] end |
#commission_basis(rate = nil) ⇒ BigDecimal?
The money a rule about value compares against.
Whether that is the tax-inclusive or tax-exclusive figure depends on the rate being considered, which is why the rate is passed in: a band and the fee it gates must weigh the same sale the same way, or two bands meeting at 50 would straddle a number the operator never set.
64 65 66 67 68 |
# File 'app/models/spree/commissions/context.rb', line 64 def commission_basis(rate = nil) return nil if subject.nil? Spree::Commissions::CalculateLine.base_for(rate, subject) end |
#product ⇒ Spree::Product?
Returns nil when commissioning a delivery.
40 41 42 |
# File 'app/models/spree/commissions/context.rb', line 40 def product line_item&.variant&.product end |
#subject ⇒ Spree::LineItem, ...
What is being commissioned — the item, or the delivery.
35 36 37 |
# File 'app/models/spree/commissions/context.rb', line 35 def subject line_item || fulfillment end |