Class: SpreeCmCommissioner::PricingModels::OrderContext

Inherits:
Object
  • Object
show all
Defined in:
lib/spree_cm_commissioner/pricing_models/order_context.rb

Constant Summary collapse

PREVIEW_PERMITTED_PARAMS =
{
  order_context: [
    :number,
    :token,
    { line_item_contexts: LineItemContext::PREVIEW_PERMITTED_FIELDS }
  ]
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ OrderContext

Returns a new instance of OrderContext.



14
15
16
17
18
19
20
# File 'lib/spree_cm_commissioner/pricing_models/order_context.rb', line 14

def initialize(options = {})
  @id = options[:id]
  @item_total = options[:item_total] || 0
  @total = options[:total] || 0
  @pricing_adjustment_total = options[:pricing_adjustment_total] || 0
  @line_item_contexts = options[:line_item_contexts] || []
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/spree_cm_commissioner/pricing_models/order_context.rb', line 12

def id
  @id
end

#item_totalObject (readonly)

Returns the value of attribute item_total.



12
13
14
# File 'lib/spree_cm_commissioner/pricing_models/order_context.rb', line 12

def item_total
  @item_total
end

#line_item_contextsObject (readonly)

Returns the value of attribute line_item_contexts.



12
13
14
# File 'lib/spree_cm_commissioner/pricing_models/order_context.rb', line 12

def line_item_contexts
  @line_item_contexts
end

#pricing_adjustment_totalObject (readonly)

Returns the value of attribute pricing_adjustment_total.



12
13
14
# File 'lib/spree_cm_commissioner/pricing_models/order_context.rb', line 12

def pricing_adjustment_total
  @pricing_adjustment_total
end

#totalObject (readonly)

Returns the value of attribute total.



12
13
14
# File 'lib/spree_cm_commissioner/pricing_models/order_context.rb', line 12

def total
  @total
end

Class Method Details

.build_from_context(attributes: {}, fallback_order: nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/spree_cm_commissioner/pricing_models/order_context.rb', line 36

def self.build_from_context(attributes: {}, fallback_order: nil) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  line_item_contexts = Array(attributes[:line_item_contexts]).map do |line_item_attributes|
    line_item = if fallback_order.present? && line_item_attributes[:id].present?
                  fallback_order.line_items.find { |li| li.id == line_item_attributes[:id].to_i }
                end

    LineItemContext.build_from_context(attributes: line_item_attributes, fallback_line_item: line_item)
  end

  Array(fallback_order&.line_items).each do |line_item|
    next if line_item_contexts.any? { |lc| lc.id == line_item.id }

    line_item_contexts << LineItemContext.build_from_context(fallback_line_item: line_item)
  end

  new(
    id: attributes[:id] || fallback_order&.id,
    item_total: attributes[:item_total] || fallback_order&.item_total,
    total: attributes[:total] || fallback_order&.total,
    pricing_adjustment_total: attributes[:pricing_adjustment_total] || fallback_order&.all_adjustments&.pricing_action&.sum(:amount),
    line_item_contexts: line_item_contexts
  )
end

.load!(params) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spree_cm_commissioner/pricing_models/order_context.rb', line 22

def self.load!(params)
  params = params.to_h.deep_symbolize_keys
  number = params[:number].presence
  token  = params[:token].presence

  raise ArgumentError, 'order_context.number is required' if number.blank?
  raise ArgumentError, 'order_context.token is required' if token.blank?

  order = Spree::Order.find_by(number: number, token: token)
  raise ArgumentError, "order #{number} not found" if order.nil?

  build_from_context(attributes: params, fallback_order: order)
end