From 7692cebbd35b365b63b3b594b098ec25f7dcc287 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 17 Mar 2022 17:30:21 +0100 Subject: [PATCH] Create Product component Update product_component.html.haml --- app/components/product_component.rb | 11 +++++++++++ .../product_component/product_component.html.haml | 12 ++++++++++++ app/views/admin/products/_product.html.haml | 14 -------------- app/views/admin/products/index.html.haml | 3 ++- 4 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 app/components/product_component.rb create mode 100644 app/components/product_component/product_component.html.haml delete mode 100644 app/views/admin/products/_product.html.haml diff --git a/app/components/product_component.rb b/app/components/product_component.rb new file mode 100644 index 0000000000..a9c6a31496 --- /dev/null +++ b/app/components/product_component.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +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 + end +end diff --git a/app/components/product_component/product_component.html.haml b/app/components/product_component/product_component.html.haml new file mode 100644 index 0000000000..93d671c30f --- /dev/null +++ b/app/components/product_component/product_component.html.haml @@ -0,0 +1,12 @@ +%tr + %td.products_column.title + - if @image + .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 diff --git a/app/views/admin/products/_product.html.haml b/app/views/admin/products/_product.html.haml deleted file mode 100644 index d7be7958bb..0000000000 --- a/app/views/admin/products/_product.html.haml +++ /dev/null @@ -1,14 +0,0 @@ -%div.product - = if product.images[0] - %div.image - %img{src: product.images[0].attachment.url(:mini, false) } - %div.title - = product.name - %div.unit - = product.unit_value - = product.variant_unit - %div.price - = product.price - -%pre - = product.to_json diff --git a/app/views/admin/products/index.html.haml b/app/views/admin/products/index.html.haml index 55aea2617f..bd26726ccb 100644 --- a/app/views/admin/products/index.html.haml +++ b/app/views/admin/products/index.html.haml @@ -10,4 +10,5 @@ Filter results #new_products - = render partial: "product", collection: @products + %table.products_table + = render(ProductComponent.with_collection(@products, columns: [:price, :unit]))