Add producer and category to a product in the table

+ refactor and automatize optional column rendered
This commit is contained in:
Jean-Baptiste Bellet
2022-03-23 10:20:52 +01:00
parent 7716d2734e
commit 069b314ae7
4 changed files with 33 additions and 12 deletions

View File

@@ -2,10 +2,28 @@
class ProductComponent < ViewComponentReflex::Component
def initialize(product:, columns:)
@columns = columns
@image = product.images[0] if product.images.any?
@name = product.name
@unit = "#{product.unit_value} #{product.variant_unit}"
@price = product.price
super
@product = product
@image = @product.images[0] if product.images.any?
@name = @product.name
@columns = columns.map { |c|
{
id: c,
value: column_value(c)
}
}
end
def column_value(column)
case column
when 'price'
@product.price
when 'unit'
"#{@product.unit_value} #{@product.variant_unit}"
when 'producer'
@product.supplier.name
when 'category'
@product.taxons.map(&:name).join(', ')
end
end
end

View File

@@ -4,9 +4,6 @@
.image
= image_tag @image.url(:mini)
= @name
- if @columns.include?("unit")
%td.products_column.unit
= @unit
- if @columns.include?("price")
%td.products_column.price
= @price
- @columns.each do |column|
%td.products_column{class: column[:id]}
= column[:value]

View File

@@ -7,7 +7,11 @@ class ProductsTableComponent < ViewComponentReflex::Component
super
@user = user
@columns = [{ label: I18n.t("admin.products_page.columns_selector.price"), value: "price" },
{ label: I18n.t("admin.products_page.columns_selector.unit"), value: "unit" }]
{ label: I18n.t("admin.products_page.columns_selector.unit"), value: "unit" },
{ label: I18n.t("admin.products_page.columns_selector.producer"),
value: "producer" },
{ label: I18n.t("admin.products_page.columns_selector.category"),
value: "category" }].sort { |a, b| a[:label] <=> b[:label] }
@columns_selected = ["price", "unit"]
@per_page = [{ label: "10", value: 10 }, { label: "25", value: 25 }, { label: "50", value: 50 },
{ label: "100", value: 100 }]

View File

@@ -462,6 +462,8 @@ en:
columns_selector:
unit: Unit
price: Price
producer: Producer
category: Category
adjustments:
skipped_changing_canceled_order: "You can't change a cancelled order."
# Common properties / models