refactor: move unit price formatting logic out of model into helper

Solves Psych::DisallowedClass errors
Example: https://github.com/openfoodfoundation/openfoodnetwork/actions/runs/14739687958/job/41374343627?pr=13232

unit_price returns object with amount and unit, as mentioned in https://github.com/openfoodfoundation/openfoodnetwork/pull/6905\#discussion_r578401368
This commit is contained in:
Carlos Chitty
2025-04-30 11:27:30 -04:00
committed by Filipe
parent e6ef52f91c
commit 85e4f749f9
7 changed files with 18 additions and 10 deletions

View File

@@ -45,5 +45,9 @@ module Spree
shop: current_distributor.name,
oc_close: l(current_order_cycle.orders_close_at, format: "%A, %b %d, %Y @ %H:%M"))
end
def format_unit_price(unit_price)
"#{Spree::Money.new(unit_price[:amount]).to_html} / #{unit_price[:unit]}"
end
end
end

View File

@@ -4,7 +4,7 @@ class Invoice
class DataPresenter
class LineItem < Invoice::DataPresenter::Base
attributes :id, :added_tax, :currency, :included_tax, :price_with_adjustments, :quantity,
:variant_id, :unit_price_price_and_unit, :unit_presentation,
:variant_id, :unit_price, :unit_presentation,
:enterprise_fee_additional_tax, :enterprise_fee_included_tax
attributes_with_presenter :variant
invoice_generation_attributes :added_tax, :included_tax, :price_with_adjustments,

View File

@@ -234,10 +234,13 @@ module Spree
final_weight_volume / quantity
end
def unit_price_price_and_unit
unit_price = UnitPrice.new(variant)
Spree::Money.new(price_with_adjustments / unit_price.denominator).to_html +
"&nbsp;/&nbsp;".html_safe + unit_price.unit
def unit_price
unit = UnitPrice.new(variant).unit
amount = price_with_adjustments / UnitPrice.new(variant).denominator
{
amount:,
unit:,
}
end
def scoper

View File

@@ -3,7 +3,7 @@
class Invoice
class LineItemSerializer < ActiveModel::Serializer
attributes :id, :added_tax, :currency, :included_tax, :price_with_adjustments, :quantity,
:variant_id, :unit_price_price_and_unit, :unit_presentation,
:variant_id, :unit_price, :unit_presentation,
:enterprise_fee_additional_tax, :enterprise_fee_included_tax
has_one :variant, serializer: Invoice::VariantSerializer

View File

@@ -2,5 +2,6 @@
= line_item.variant.product.name
- unless line_item.variant.product.name.include? line_item.name_to_display
%span= "- #{line_item.name_to_display}"
- if line_item.unit_price_price_and_unit
= raw("(#{line_item.unit_price_price_and_unit})")
- if line_item.unit_price
= raw("(#{format_unit_price(line_item.unit_price)})")

View File

@@ -24,7 +24,7 @@
= line_item.single_display_amount_with_adjustments.to_html
%br
%span.unit-price
= line_item.unit_price_price_and_unit
= format_unit_price(line_item.unit_price)
%td.text-center.cart-item-quantity
= line_item.quantity
%td.cart-item-total.text-right

View File

@@ -22,7 +22,7 @@
= line_item.single_display_amount_with_adjustments.to_html
%br
%span.unit-price
= line_item.unit_price_price_and_unit
= format_unit_price(line_item.unit_price)
%td.text-center.cart-item-quantity
- finalized_quantity = @order.completed? ? line_item.quantity : 0
= item_form.number_field :quantity,