Skip to content

Commit

Permalink
[wip] Add bulk_update product form
Browse files Browse the repository at this point in the history
Todo: consider rename _table to _form. Hmm nah we should be decoupling these two later anyway.
  • Loading branch information
dacook committed Jul 13, 2023
1 parent 4460052 commit b36b21b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 67 deletions.
29 changes: 29 additions & 0 deletions app/controllers/admin/products_v3_controller.rb
Expand Up @@ -3,5 +3,34 @@
module Admin
class ProductsV3Controller < Spree::Admin::BaseController
def index; end

def bulk_update
product_set = product_set_from_params

product_set.collection.each { |p| authorize! :update, p }

#FIXME

Check warning on line 12 in app/controllers/admin/products_v3_controller.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Missing space after `#`. Raw Output: app/controllers/admin/products_v3_controller.rb:12:7: C: Layout/LeadingCommentSpace: Missing space after `#`.
if product_set.save
redirect_to :index # todo: also include pagination/filter params?
elsif product_set.errors.present?
render json: { errors: product_set.errors }, status: :bad_request
else
render body: nil, status: :internal_server_error
end
end

def product_set_from_params
collection_hash = products_bulk_params[:products].map { |id, attributes|
{ "id" => id, **attributes }
}.each_with_index.map { |p, i|

Check warning on line 25 in app/controllers/admin/products_v3_controller.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Pass a block to `to_h` instead of calling `map.to_h`. Raw Output: app/controllers/admin/products_v3_controller.rb:25:25: C: Style/MapToHash: Pass a block to `to_h` instead of calling `map.to_h`.
[i, p]
}.to_h
Sets::ProductSet.new(collection_attributes: collection_hash)
end

def products_bulk_params
params.permit(products: ::PermittedAttributes::Product.attributes).
to_h.with_indifferent_access
end
end
end
139 changes: 73 additions & 66 deletions app/views/admin/products_v3/_table.html.haml
@@ -1,67 +1,74 @@
%table.products
%col{ width:"15%" }
%col{ width:"5%", style: "max-width:5em" }
%col{ width:"8%" }
%col{ width:"5%", style: "max-width:5em"}
%col{ width:"5%", style: "max-width:5em"}
%col{ width:"10%" }= # producer
%col{ width:"10%" }
%col{ width:"5%" }
%col{ width:"5%", style: "max-width:5em" }
%thead
%tr
%th.align-left= t('admin.products_page.columns.name')
%th.align-right= t('admin.products_page.columns.sku')
%th.align-right= t('admin.products_page.columns.unit')
%th.align-right= t('admin.products_page.columns.price')
%th.align-right= t('admin.products_page.columns.on_hand')
%th.align-left= t('admin.products_page.columns.producer')
%th.align-left= t('admin.products_page.columns.category')
%th.align-left= t('admin.products_page.columns.tax_category')
%th.align-left= t('admin.products_page.columns.inherits_properties')
- products.each do |product|
%tbody.relaxed
= form_with url: bulk_update_admin_products_v3_index_path, method: :patch do |form|
.container
.sixteen.columns.align-right
#fieldset
= t('.products_modified', count: 'X')
= form.submit t('.reset'), type: :reset
= form.submit t('.save')
%table.products
%col{ width:"15%" }
%col{ width:"5%", style: "max-width:5em" }
%col{ width:"8%" }
%col{ width:"5%", style: "max-width:5em"}
%col{ width:"5%", style: "max-width:5em"}
%col{ width:"10%" }= # producer
%col{ width:"10%" }
%col{ width:"5%" }
%col{ width:"5%", style: "max-width:5em" }
%thead
%tr
%td.align-left.header
.line-clamp-1= product.name
%td.align-right
.line-clamp-1= product.sku
%td.align-right
.line-clamp-1
= product.variant_unit.upcase_first
/ TODO: properly handle custom unit names
= WeightsAndMeasures::UNITS[product.variant_unit] && "(" + WeightsAndMeasures::UNITS[product.variant_unit][product.variant_unit_scale]["name"] + ")"
%td.align-right
-# empty
%td.align-right
-# TODO: new requirement "DISPLAY ON DEMAND IF ALL VARIANTS ARE ON DEMAND". And translate value
.line-clamp-1= if product.variants.all?(&:on_demand) then "On demand" else product.on_hand || 0 end
%td.align-left
.line-clamp-1= product.supplier.name
%td.align-left
.line-clamp-1= product.taxons.map(&:name).join(', ')
%td.align-left
.line-clamp-1= product.tax_category&.name || "None" # TODO: convert to dropdown, else translate hardcoded string.
%td.align-left
.line-clamp-1= product.inherits_properties ? 'YES' : 'NO' #TODO: consider using https://github.com/RST-J/human_attribute_values, else use I18n.t (also below)
- product.variants.each do |variant|
%tr.condensed
%td.align-left
.line-clamp-1= variant.display_name
%td.align-right
.line-clamp-1= variant.sku
%td.align-right
.line-clamp-1= variant.unit_to_display
%td.align-right
.line-clamp-1= number_to_currency(variant.price)
%td.align-right
.line-clamp-1= variant.on_hand || 0 #TODO: spec for this according to requirements.
%td.align-left
.line-clamp-1= variant.product.supplier.name # same as product
%td.align-left
-# empty
%td.align-left
-# empty
%td.align-left
-# empty
%th.align-left= t('admin.products_page.columns.name')
%th.align-right= t('admin.products_page.columns.sku')
%th.align-right= t('admin.products_page.columns.unit')
%th.align-right= t('admin.products_page.columns.price')
%th.align-right= t('admin.products_page.columns.on_hand')
%th.align-left= t('admin.products_page.columns.producer')
%th.align-left= t('admin.products_page.columns.category')
%th.align-left= t('admin.products_page.columns.tax_category')
%th.align-left= t('admin.products_page.columns.inherits_properties')
- products.each do |product|
= form.fields_for(product) do |product_form|
%tbody.relaxed
%tr
%td.align-left.header
.line-clamp-1= product_form.text_field :name, name: "[products][#{product.id}][name]"
%td.align-right
.line-clamp-1= product.sku
%td.align-right
.line-clamp-1
= product.variant_unit.upcase_first
/ TODO: properly handle custom unit names
= WeightsAndMeasures::UNITS[product.variant_unit] && "(" + WeightsAndMeasures::UNITS[product.variant_unit][product.variant_unit_scale]["name"] + ")"
%td.align-right
-# empty
%td.align-right
-# TODO: new requirement "DISPLAY ON DEMAND IF ALL VARIANTS ARE ON DEMAND". And translate value
.line-clamp-1= if product.variants.all?(&:on_demand) then "On demand" else product.on_hand || 0 end
%td.align-left
.line-clamp-1= product.supplier.name
%td.align-left
.line-clamp-1= product.taxons.map(&:name).join(', ')
%td.align-left
.line-clamp-1= product.tax_category&.name || "None" # TODO: convert to dropdown, else translate hardcoded string.
%td.align-left
.line-clamp-1= product.inherits_properties ? 'YES' : 'NO' #TODO: consider using https://github.com/RST-J/human_attribute_values, else use I18n.t (also below)
- product.variants.each do |variant|
%tr.condensed
%td.align-left
.line-clamp-1= variant.display_name
%td.align-right
.line-clamp-1= variant.sku
%td.align-right
.line-clamp-1= variant.unit_to_display
%td.align-right
.line-clamp-1= number_to_currency(variant.price)
%td.align-right
.line-clamp-1= variant.on_hand || 0 #TODO: spec for this according to requirements.
%td.align-left
.line-clamp-1= variant.product.supplier.name # same as product
%td.align-left
-# empty
%td.align-left
-# empty
%td.align-left
-# empty
7 changes: 7 additions & 0 deletions config/locales/en.yml
Expand Up @@ -787,6 +787,13 @@ en:
no_products_found: No products found
import_products: Import multiple products
no_products_found_for_search: No products found for your search criteria
table:
products_modified:
zero: No changes made.
one: "%{count} product has been modified."
other: "%{count} products have been modified."
save: Save changes
reset: Discard changes
product_import:
title: Product Import
file_not_found: File not found or could not be opened
Expand Down
4 changes: 3 additions & 1 deletion config/routes/admin.rb
Expand Up @@ -73,7 +73,9 @@
get '/new_products', to: 'products#index'
end
constraints FeatureToggleConstraint.new(:admin_style_v3) do
resources :products_v3, only: :index
resources :products_v3, as: :products_v3, only: :index do
patch :bulk_update, on: :collection
end
end

resources :variant_overrides do
Expand Down

0 comments on commit b36b21b

Please sign in to comment.