From 069b314ae7bff15b5c305e7d6dd5c2ceeb4eed95 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Wed, 23 Mar 2022 10:20:52 +0100 Subject: [PATCH] Add producer and category to a product in the table + refactor and automatize optional column rendered --- app/components/product_component.rb | 28 +++++++++++++++---- .../product_component.html.haml | 9 ++---- app/components/products_table_component.rb | 6 +++- config/locales/en.yml | 2 ++ 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/app/components/product_component.rb b/app/components/product_component.rb index a9c6a31496..22dfc885bf 100644 --- a/app/components/product_component.rb +++ b/app/components/product_component.rb @@ -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 diff --git a/app/components/product_component/product_component.html.haml b/app/components/product_component/product_component.html.haml index e9110779b8..8d36d1bc2e 100644 --- a/app/components/product_component/product_component.html.haml +++ b/app/components/product_component/product_component.html.haml @@ -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] diff --git a/app/components/products_table_component.rb b/app/components/products_table_component.rb index d01101cd88..4aca2c791f 100644 --- a/app/components/products_table_component.rb +++ b/app/components/products_table_component.rb @@ -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 }] diff --git a/config/locales/en.yml b/config/locales/en.yml index a14f4a0bb4..d37024ff3c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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