diff --git a/admin/app/components/solidus_admin/products/index/component.rb b/admin/app/components/solidus_admin/products/index/component.rb
index b72e4ef13c3..67ddee3c3fe 100644
--- a/admin/app/components/solidus_admin/products/index/component.rb
+++ b/admin/app/components/solidus_admin/products/index/component.rb
@@ -96,29 +96,14 @@ def name_column
def status_column
{
header: :status,
- data: ->(product) { component('products/status').new(product: product) }
+ data: ->(product) { component('products/status').from_product(product) }
}
end
def stock_column
{
header: :stock,
- data: ->(product) do
- stock_info =
- case (on_hand = product.total_on_hand)
- when Float::INFINITY
- content_tag :span, t('.stock.in_stock', on_hand: t('.stock.infinity')), class: 'text-forest'
- when 1..Float::INFINITY
- content_tag :span, t('.stock.in_stock', on_hand: on_hand), class: 'text-forest'
- else
- content_tag :span, t('.stock.in_stock', on_hand: on_hand), class: 'text-red-500'
- end
-
- variant_info =
- t('.for_variants', count: product.variants.count)
-
- content_tag :div, safe_join([stock_info, variant_info], ' ')
- end
+ data: ->(product) { component('products/stock').from_product(product) }
}
end
diff --git a/admin/app/components/solidus_admin/products/index/component.yml b/admin/app/components/solidus_admin/products/index/component.yml
index d51634eb5f0..cc6fd827a10 100644
--- a/admin/app/components/solidus_admin/products/index/component.yml
+++ b/admin/app/components/solidus_admin/products/index/component.yml
@@ -1,10 +1,6 @@
en:
product_image: 'Image'
add_product: 'Add Product'
- stock:
- infinity: '∞'
- in_stock: '%{on_hand} in stock'
- for_variants: 'for %{count} variants'
batch_actions:
delete: 'Delete'
discontinue: 'Discontinue'
diff --git a/admin/app/components/solidus_admin/products/show/component.html.erb b/admin/app/components/solidus_admin/products/show/component.html.erb
index edde127bff7..dba38b84824 100644
--- a/admin/app/components/solidus_admin/products/show/component.html.erb
+++ b/admin/app/components/solidus_admin/products/show/component.html.erb
@@ -9,7 +9,7 @@
) %>
diff --git a/admin/app/components/solidus_admin/products/status/component.rb b/admin/app/components/solidus_admin/products/status/component.rb
index 05062c19e20..190e97779a4 100644
--- a/admin/app/components/solidus_admin/products/status/component.rb
+++ b/admin/app/components/solidus_admin/products/status/component.rb
@@ -1,31 +1,23 @@
# frozen_string_literal: true
class SolidusAdmin::Products::Status::Component < SolidusAdmin::BaseComponent
- COLORS = {
+ STATUSES = {
available: :green,
discontinued: :red
}.freeze
- # @param product [Spree::Product]
- def initialize(product:)
- @product = product
+ def self.from_product(product)
+ new(status: product.available? ? :available : :discontinued)
+ end
+
+ def initialize(status:)
+ @status = status
end
def call
render component('ui/badge').new(
- name: t(".#{status}"),
- color: COLORS.fetch(status)
+ name: t(".#{@status}"),
+ color: STATUSES.fetch(@status)
)
end
-
- # @return [Symbol]
- # :available when the product is available
- # :discontinued when the product is not available
- def status
- if @product.available?
- :available
- else
- :discontinued
- end
- end
end
diff --git a/admin/app/components/solidus_admin/products/stock/component.rb b/admin/app/components/solidus_admin/products/stock/component.rb
new file mode 100644
index 00000000000..c4365664c82
--- /dev/null
+++ b/admin/app/components/solidus_admin/products/stock/component.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+class SolidusAdmin::Products::Stock::Component < SolidusAdmin::BaseComponent
+ def self.from_product(product)
+ new(
+ on_hand: product.total_on_hand,
+ variants_count: product.variants.count,
+ )
+ end
+
+ def initialize(on_hand:, variants_count:)
+ @on_hand = on_hand
+ @variants_count = variants_count
+ end
+
+ def call
+ stock_info =
+ case @on_hand
+ when Float::INFINITY
+ tag.span t('.stock.in_stock', on_hand: t('.stock.infinity')), class: 'text-forest'
+ when 1..Float::INFINITY
+ tag.span t('.stock.in_stock', on_hand: @on_hand), class: 'text-forest'
+ else
+ tag.span t('.stock.in_stock', on_hand: @on_hand), class: 'text-red-500'
+ end
+
+ variant_info = t('.for_variants', count: @variants_count)
+
+ tag.div safe_join([stock_info, variant_info], ' ')
+ end
+end
diff --git a/admin/app/components/solidus_admin/products/stock/component.yml b/admin/app/components/solidus_admin/products/stock/component.yml
new file mode 100644
index 00000000000..467aefeb77f
--- /dev/null
+++ b/admin/app/components/solidus_admin/products/stock/component.yml
@@ -0,0 +1,5 @@
+en:
+ stock:
+ infinity: '∞'
+ in_stock: '%{on_hand} in stock'
+ for_variants: 'for %{count} variants'
diff --git a/admin/spec/components/previews/solidus_admin/products/status/component_preview.rb b/admin/spec/components/previews/solidus_admin/products/status/component_preview.rb
index 0d60429a087..e04545090cd 100644
--- a/admin/spec/components/previews/solidus_admin/products/status/component_preview.rb
+++ b/admin/spec/components/previews/solidus_admin/products/status/component_preview.rb
@@ -5,26 +5,6 @@ class SolidusAdmin::Products::Status::ComponentPreview < ViewComponent::Preview
include SolidusAdmin::Preview
def overview
- render_with_template(locals:
- {
- definitions: {
- available: available_component,
- discontinued: discontinued_component
- }
- })
- end
-
- private
-
- def available_component
- current_component.new(
- product: Spree::Product.new(available_on: Time.current)
- )
- end
-
- def discontinued_component
- current_component.new(
- product: Spree::Product.new(available_on: nil)
- )
+ render_with_template
end
end
diff --git a/admin/spec/components/previews/solidus_admin/products/status/component_preview/overview.html.erb b/admin/spec/components/previews/solidus_admin/products/status/component_preview/overview.html.erb
index ee9120ee41e..3fad845c74a 100644
--- a/admin/spec/components/previews/solidus_admin/products/status/component_preview/overview.html.erb
+++ b/admin/spec/components/previews/solidus_admin/products/status/component_preview/overview.html.erb
@@ -1,16 +1,15 @@
-
-
-
- <% definitions.each_key do |status| %>
- <%= status.to_s.humanize %> |
- <% end %>
-
-
-
-
- <% definitions.each_value do |component| %>
- <%= render component %> |
- <% end %>
-
-
-
+
+
+ Available
+
+
+ <%= render current_component.new(status: :available) %>
+
+
+
+
+ Discontinued
+
+
+ <%= render current_component.new(status: :discontinued) %>
+
diff --git a/admin/spec/components/previews/solidus_admin/products/stock/component_preview.rb b/admin/spec/components/previews/solidus_admin/products/stock/component_preview.rb
new file mode 100644
index 00000000000..a6d688fc613
--- /dev/null
+++ b/admin/spec/components/previews/solidus_admin/products/stock/component_preview.rb
@@ -0,0 +1,10 @@
+# frozen_string_literal: true
+
+# @component "products/stock"
+class SolidusAdmin::Products::Stock::ComponentPreview < ViewComponent::Preview
+ include SolidusAdmin::Preview
+
+ def overview
+ render_with_template
+ end
+end
diff --git a/admin/spec/components/previews/solidus_admin/products/stock/component_preview/overview.html.erb b/admin/spec/components/previews/solidus_admin/products/stock/component_preview/overview.html.erb
new file mode 100644
index 00000000000..14380105b6e
--- /dev/null
+++ b/admin/spec/components/previews/solidus_admin/products/stock/component_preview/overview.html.erb
@@ -0,0 +1,31 @@
+
+
+ In Stock
+
+
+ <%= render current_component.new(on_hand: 32, variants_count: 12) %>
+
+
+
+
+ Infinite stock
+
+
+ <%= render current_component.new(on_hand: Float::INFINITY, variants_count: 12) %>
+
+
+
+
+ Out of stock
+
+
+ <%= render current_component.new(on_hand: 0, variants_count: 12) %>
+
+
+
+
+ Negative stock
+
+
+ <%= render current_component.new(on_hand: -10, variants_count: 12) %>
+
diff --git a/admin/spec/components/solidus_admin/products/status/component_spec.rb b/admin/spec/components/solidus_admin/products/status/component_spec.rb
index b0cf11c8929..45f1d52ba75 100644
--- a/admin/spec/components/solidus_admin/products/status/component_spec.rb
+++ b/admin/spec/components/solidus_admin/products/status/component_spec.rb
@@ -11,17 +11,17 @@
it "returns :available when the product is available" do
product = Spree::Product.new(available_on: Time.current)
- component = described_class.new(product: product)
+ render_inline described_class.from_product(product)
- expect(component.status).to eq(:available)
+ expect(rendered_content).to have_text("Available")
end
it "returns :discontinued when the product is not available" do
product = Spree::Product.new(available_on: nil)
- component = described_class.new(product: product)
+ render_inline described_class.from_product(product)
- expect(component.status).to eq(:discontinued)
+ expect(rendered_content).to have_text("Discontinued")
end
end
end
diff --git a/admin/spec/components/solidus_admin/products/stock/component_spec.rb b/admin/spec/components/solidus_admin/products/stock/component_spec.rb
new file mode 100644
index 00000000000..88b099c45dc
--- /dev/null
+++ b/admin/spec/components/solidus_admin/products/stock/component_spec.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+require "spec_helper"
+
+RSpec.describe SolidusAdmin::Products::Stock::Component, type: :component do
+ it "renders the overview preview" do
+ render_preview(:overview)
+ end
+end