Class: SpreeCmCommissioner::PricingModels::LineItemContext
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::PricingModels::LineItemContext
- 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
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#connected_trip_id ⇒ Object
readonly
Returns the value of attribute connected_trip_id.
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#distance ⇒ Object
readonly
Returns the value of attribute distance.
-
#from_date ⇒ Object
readonly
Returns the value of attribute from_date.
-
#guest_contexts ⇒ Object
readonly
Returns the value of attribute guest_contexts.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#order_id ⇒ Object
readonly
Returns the value of attribute order_id.
-
#price ⇒ Object
readonly
Returns the value of attribute price.
-
#quantity ⇒ Object
readonly
Returns the value of attribute quantity.
-
#service_origin_id ⇒ Object
readonly
Returns the value of attribute service_origin_id.
-
#to_date ⇒ Object
readonly
Returns the value of attribute to_date.
-
#trip_id ⇒ Object
readonly
Returns the value of attribute trip_id.
-
#variant_id ⇒ Object
readonly
Returns the value of attribute variant_id.
Class Method Summary collapse
-
.build_from_context(attributes: {}, fallback_line_item: nil) ⇒ Object
Build from AR record with all persisted guests.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ LineItemContext
constructor
A new instance of LineItemContext.
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( = {}) @id = [:id] @variant_id = [:variant_id] @order_id = [:order_id] @quantity = [:quantity] @price = [:price] @amount = [:amount] @currency = [:currency] @from_date = [:from_date] @to_date = [:to_date] @trip_id = [:trip_id] @connected_trip_id = [:connected_trip_id] @service_origin_id = [:service_origin_id] @distance = [:distance] @guest_contexts = [:guest_contexts] || [] end |
Instance Attribute Details
#amount ⇒ Object (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_id ⇒ Object (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 |
#currency ⇒ Object (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 |
#distance ⇒ Object (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_date ⇒ Object (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_contexts ⇒ Object (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 |
#id ⇒ Object (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_id ⇒ Object (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 |
#price ⇒ Object (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 |
#quantity ⇒ Object (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_id ⇒ Object (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_date ⇒ Object (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_id ⇒ Object (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_id ⇒ Object (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 |