Mark fields as changed if they contain unsaved values.

This can happen when there's a validation error. The field with error will also be marked changed, but the error style will override it.

I'd like to move this into a FormBuilder. Existing formbuilder gems don't seem to support it (though I didn't look very hard).
This commit is contained in:
David Cook
2023-10-24 11:50:16 +11:00
parent 04032e61e2
commit aa4630d74c
3 changed files with 11 additions and 6 deletions

View File

@@ -24,6 +24,10 @@ module Spree
amount
end
def price_changed?
amount_changed?
end
def price=(price)
self[:amount] = parse_price(price)
end

View File

@@ -50,7 +50,8 @@ module Spree
has_many :prices,
class_name: 'Spree::Price',
dependent: :destroy
delegate :display_price, :display_amount, :price, :price=, :currency, :currency=,
delegate :display_price, :display_amount, :price, :price_changed?, :price=,
:currency, :currency=,
to: :find_or_build_default_price
has_many :exchange_variants

View File

@@ -40,10 +40,10 @@
%tr
%td.field.align-left.header
= product_form.hidden_field :id
= product_form.text_field :name, 'aria-label': t('admin.products_page.columns.name')
= product_form.text_field :name, 'aria-label': t('admin.products_page.columns.name'), class: (product_form.object.name_changed? ? 'changed' : nil)
= error_message_on product, :name
%td.field
= product_form.text_field :sku, 'aria-label': t('admin.products_page.columns.sku')
= product_form.text_field :sku, 'aria-label': t('admin.products_page.columns.sku'), class: (product_form.object.sku_changed? ? 'changed' : nil)
= error_message_on product, :sku
%td.align-right
.content
@@ -69,15 +69,15 @@
%tr.condensed
%td.field
= variant_form.hidden_field :id
= variant_form.text_field :display_name, 'aria-label': t('admin.products_page.columns.name'), placeholder: product.name
= variant_form.text_field :display_name, 'aria-label': t('admin.products_page.columns.name'), placeholder: product.name, class: (variant_form.object.display_name_changed? ? 'changed' : nil)
= error_message_on variant, :display_name
%td.field
= variant_form.text_field :sku, 'aria-label': t('admin.products_page.columns.sku')
= variant_form.text_field :sku, 'aria-label': t('admin.products_page.columns.sku'), class: (variant_form.object.sku_changed? ? 'changed' : nil)
= error_message_on variant, :sku
%td.align-right
.content= variant.unit_to_display
%td.field
= variant_form.text_field :price, 'aria-label': t('admin.products_page.columns.price'), value: number_to_currency(variant.price, unit: '')&.strip # TODO: add a spec to prove that this formatting is necessary. If so, it should be in a shared form helper for currency inputs
= variant_form.text_field :price, 'aria-label': t('admin.products_page.columns.price'), class: (variant_form.object.price_changed? ? 'changed' : nil), value: number_to_currency(variant.price, unit: '')&.strip # TODO: add a spec to prove that this formatting is necessary. If so, it should be in a shared form helper for currency inputs
= error_message_on variant, :price
%td.align-right
.content= variant.on_hand || 0 #TODO: spec for this according to requirements.