Module: Spree::LineItemDecorator

Defined in:
app/models/spree/line_item_decorator.rb

Overview

Zeitwerk requires this module's name to match its file path exactly (app/models/spree/line_item_decorator.rb -> Spree::LineItemDecorator) — under dev's lazy autoloading, a mismatched name (e.g. a SpreeSquare-prefixed one) means nothing ever triggers loading this file at all, silently, since nothing references the name Zeitwerk expects.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#square_modifier_idsObject

Transient carrier for selected modifier ids from add-to-cart through to SpreeSquare::Cart::AddItem, which reads it right after super to build the persistent LineItemModifier snapshot rows. Never persisted itself — square_modifier_ids= is populated via LineItem#options= (see variant_decorator.rb's square_modifier_ids_price_modifier_amount, dispatched from the same options hash), which only lives for the request that created the line item.



26
27
28
# File 'app/models/spree/line_item_decorator.rb', line 26

def square_modifier_ids
  @square_modifier_ids
end

Class Method Details

.prepended(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'app/models/spree/line_item_decorator.rb', line 8

def self.prepended(base)
  # No `dependent: :destroy` here originally meant removing a line item
  # that had modifier selections hit a foreign-key violation instead of
  # actually removing it (spree_square_line_item_modifiers.line_item_id
  # has no ON DELETE behavior beyond Postgres's RESTRICT default) — only
  # surfaced once a real modifier-bearing item was added to a cart and
  # then removed, which nothing exercised before.
  base.has_many :square_line_item_modifiers, class_name: 'SpreeSquare::LineItemModifier',
                                               foreign_key: 'line_item_id', dependent: :destroy
end

Instance Method Details

#recalculate_priceObject

Both of these reset price to the variant's base price with no idea about our modifier selections (which live in a separate table specifically so a later Square catalog edit can't retroactively change what a customer already paid for) — necessary so a genuine price change from Square (M3) takes effect, but it means the modifier delta has to be re-added every time, not just once at add-to-cart.

Confirmed (by grepping all of spree_core, not sampling) these are the only two places core reassigns line item price outside our own code: recalculate_price fires on cart mutations (Cart::AddItem calls it directly); update_price is what Order#update_line_item_prices! calls on every line item via a before_transition from: :address checkout callback — the bug that surfaced this: modifier pricing was correct right after add-to-cart, then silently reverted to base price the moment checkout address was submitted.



43
44
45
46
# File 'app/models/spree/line_item_decorator.rb', line 43

def recalculate_price
  super
  apply_square_modifier_delta!
end

#update_priceObject



48
49
50
51
# File 'app/models/spree/line_item_decorator.rb', line 48

def update_price
  super
  apply_square_modifier_delta!
end