Skip to content

Commit

Permalink
Accept basic values when creating products/status components
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Oct 12, 2023
1 parent 7869560 commit c1d423f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
) %>
<h1 class="flex items-center gap-2">
<span class="body-title"><%= @product.name %></span>
<%= render component("products/status").new(product: @product) %>
<%= render component("products/status").from_product(@product) %>
</h1>

<div class="ml-auto flex gap-2 items-center">
Expand Down
26 changes: 9 additions & 17 deletions admin/app/components/solidus_admin/products/status/component.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<table>
<thead>
<tr>
<% definitions.each_key do |status| %>
<th class="px-3 py-1 text-gray-500 text-center"><%= status.to_s.humanize %></th>
<% end %>
</tr>
</thead>
<tbody>
<tr>
<% definitions.each_value do |component| %>
<th class="px-3 py-1"><%= render component %></th>
<% end %>
</tr>
</tbody>
</table>
<div class="mb-8">
<h6 class="text-gray-500 mb-3 mt-0">
Available
</h6>

<%= render current_component.new(status: :available) %>
</div>

<div class="mb-8">
<h6 class="text-gray-500 mb-3 mt-0">
Discontinued
</h6>

<%= render current_component.new(status: :discontinued) %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c1d423f

Please sign in to comment.