Class: SpreeCmCommissioner::PricingModels::LineItemContext

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

Constant Summary collapse

PREVIEW_PERMITTED_FIELDS =
[
  :id,
  { guest_contexts: GuestContext::PREVIEW_PERMITTED_FIELDS }
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LineItemContext

Returns a new instance of LineItemContext.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 13

def initialize(options = {})
  @id = options[:id]
  @variant_id = options[:variant_id]
  @order_id = options[:order_id]
  @quantity = options[:quantity]
  @price = options[:price]
  @amount = options[:amount]
  @currency = options[:currency]
  @from_date = options[:from_date]
  @to_date = options[:to_date]
  @trip_id = options[:trip_id]
  @connected_trip_id = options[:connected_trip_id]
  @service_origin_id = options[:service_origin_id]
  @distance = options[:distance]
  @guest_contexts = options[:guest_contexts] || []
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def amount
  @amount
end

#connected_trip_idObject (readonly)

Returns the value of attribute connected_trip_id.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def connected_trip_id
  @connected_trip_id
end

#currencyObject (readonly)

Returns the value of attribute currency.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def currency
  @currency
end

#distanceObject (readonly)

Returns the value of attribute distance.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def distance
  @distance
end

#from_dateObject (readonly)

Returns the value of attribute from_date.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def from_date
  @from_date
end

#guest_contextsObject (readonly)

Returns the value of attribute guest_contexts.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def guest_contexts
  @guest_contexts
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def id
  @id
end

#order_idObject (readonly)

Returns the value of attribute order_id.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def order_id
  @order_id
end

#priceObject (readonly)

Returns the value of attribute price.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def price
  @price
end

#quantityObject (readonly)

Returns the value of attribute quantity.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def quantity
  @quantity
end

#service_origin_idObject (readonly)

Returns the value of attribute service_origin_id.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def service_origin_id
  @service_origin_id
end

#to_dateObject (readonly)

Returns the value of attribute to_date.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def to_date
  @to_date
end

#trip_idObject (readonly)

Returns the value of attribute trip_id.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def trip_id
  @trip_id
end

#variant_idObject (readonly)

Returns the value of attribute variant_id.



9
10
11
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 9

def variant_id
  @variant_id
end

Class Method Details

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

Build from AR record with all persisted guests.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/spree_cm_commissioner/pricing_models/line_item_context.rb', line 31

def self.build_from_context(attributes: {}, fallback_line_item: nil) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  guest_contexts = Array(attributes[:guest_contexts]).map do |guest_attributes|
    persisted_guest = if fallback_line_item.present? && guest_attributes[:id].present?
                        fallback_line_item.guests.find { |guest| guest.id == guest_attributes[:id].to_i }
                      end

    GuestContext.build_from_context(attributes: guest_attributes, fallback_guest: persisted_guest)
  end

  fallback_line_item&.guests&.each do |guest|
    guest_contexts << GuestContext.build_from_context(fallback_guest: guest) if guest_contexts.none? { |gc| gc.id == guest.id }
  end

  new(
    id: attributes[:id]&.to_i || fallback_line_item&.id,
    variant_id: attributes[:variant_id]&.to_i || fallback_line_item&.variant_id,
    order_id: attributes[:order_id]&.to_i || fallback_line_item&.order_id,
    quantity: attributes[:quantity]&.to_i || fallback_line_item&.quantity,
    price: attributes[:price]&.to_f || fallback_line_item&.price,
    amount: attributes[:amount]&.to_f || fallback_line_item&.amount,
    currency: attributes[:currency] || fallback_line_item&.currency,
    from_date: attributes[:from_date] || fallback_line_item&.from_date,
    to_date: attributes[:to_date] || fallback_line_item&.to_date,
    trip_id: attributes[:trip_id]&.to_i || fallback_line_item&.trip_id,
    connected_trip_id: attributes[:connected_trip_id]&.to_i || fallback_line_item&.connected_trip_id,
    service_origin_id: attributes[:service_origin_id]&.to_i || fallback_line_item&.service_origin_id,
    distance: attributes[:distance]&.to_i || fallback_line_item&.distance,
    guest_contexts: guest_contexts
  )
end