mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Refactor: with form builder
This commit is contained in:
13
app/helpers/bulk_form_builder.rb
Normal file
13
app/helpers/bulk_form_builder.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class BulkFormBuilder < ActionView::Helpers::FormBuilder
|
||||
def text_field(field, **opts)
|
||||
# Mark field if it is changed (unsaved)
|
||||
changed_method = "#{field}_changed?"
|
||||
if object.respond_to?(changed_method) && object.public_send(changed_method)
|
||||
opts[:class] = "#{opts[:class]} changed".strip
|
||||
end
|
||||
|
||||
super(field, **opts)
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,5 @@
|
||||
= form_with url: bulk_update_admin_products_path, method: :patch, id: "products-form",
|
||||
builder: BulkFormBuilder,
|
||||
html: {'data-reflex-serialize-form': true, 'data-reflex': 'submit->products#bulk_update',
|
||||
'data-controller': "bulk-form", 'data-bulk-form-disable-selector-value': "#sort,#filters"} do |form|
|
||||
%fieldset.form-actions.hidden{ 'data-bulk-form-target': "actions" }
|
||||
@@ -40,10 +41,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'), class: (product_form.object.name_changed? ? 'changed' : nil)
|
||||
= product_form.text_field :name, 'aria-label': t('admin.products_page.columns.name')
|
||||
= error_message_on product, :name
|
||||
%td.field
|
||||
= product_form.text_field :sku, 'aria-label': t('admin.products_page.columns.sku'), class: (product_form.object.sku_changed? ? 'changed' : nil)
|
||||
= product_form.text_field :sku, 'aria-label': t('admin.products_page.columns.sku')
|
||||
= error_message_on product, :sku
|
||||
%td.align-right
|
||||
.content
|
||||
@@ -69,15 +70,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, class: (variant_form.object.display_name_changed? ? 'changed' : nil)
|
||||
= variant_form.text_field :display_name, 'aria-label': t('admin.products_page.columns.name'), placeholder: product.name
|
||||
= error_message_on variant, :display_name
|
||||
%td.field
|
||||
= variant_form.text_field :sku, 'aria-label': t('admin.products_page.columns.sku'), class: (variant_form.object.sku_changed? ? 'changed' : nil)
|
||||
= variant_form.text_field :sku, 'aria-label': t('admin.products_page.columns.sku')
|
||||
= 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'), 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
|
||||
= 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
|
||||
= error_message_on variant, :price
|
||||
%td.align-right
|
||||
.content= variant.on_hand || 0 #TODO: spec for this according to requirements.
|
||||
|
||||
26
spec/helpers/bulk_form_builder_spec.rb
Normal file
26
spec/helpers/bulk_form_builder_spec.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
class TestHelper < ActionView::Base; end
|
||||
|
||||
describe BulkFormBuilder do
|
||||
describe '#text_field' do
|
||||
let(:product) { create(:product) }
|
||||
let(:form) { BulkFormBuilder.new(:product, product, self, {}) }
|
||||
|
||||
it { expect(form.text_field(:name)).to_not include "changed" }
|
||||
|
||||
context "attribute has been changed" do
|
||||
before { product.assign_attributes name: "updated name" }
|
||||
|
||||
it { expect(form.text_field(:name)).to include "changed" }
|
||||
|
||||
context "and saved" do
|
||||
before { product.save }
|
||||
|
||||
it { expect(form.text_field(:name)).to_not include "changed" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user