forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We'll need the same logic in the product forms, so we need to extract it to a component. Ref. solidusio#5329
- Loading branch information
1 parent
84940a2
commit f21466f
Showing
7 changed files
with
111 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
admin/app/components/solidus_admin/products/index/component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
admin/app/components/solidus_admin/products/status/component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::Products::Status::Component < SolidusAdmin::BaseComponent | ||
COLORS = { | ||
available: :green, | ||
discontinued: :red | ||
}.freeze | ||
|
||
# @param product [Spree::Product] | ||
def initialize(product:, badge_component: component('ui/badge')) | ||
@product = product | ||
@badge_component = badge_component | ||
end | ||
|
||
def call | ||
render @badge_component.new( | ||
name: t(".#{status}"), | ||
color: COLORS.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 |
3 changes: 3 additions & 0 deletions
3
admin/app/components/solidus_admin/products/status/component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
en: | ||
available: 'Available' | ||
discontinued: 'Discontinued' |
30 changes: 30 additions & 0 deletions
30
admin/spec/components/previews/solidus_admin/products/status/component_preview.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
# @component "products/status" | ||
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) | ||
) | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
...pec/components/previews/solidus_admin/products/status/component_preview/overview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<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> |
27 changes: 27 additions & 0 deletions
27
admin/spec/components/solidus_admin/products/status/component_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe SolidusAdmin::Products::Status::Component, type: :component do | ||
it "renders the overview preview" do | ||
render_preview(:overview) | ||
end | ||
|
||
describe "#status" do | ||
it "returns :available when the product is available" do | ||
product = Spree::Product.new(available_on: Time.current) | ||
|
||
component = described_class.new(product: product) | ||
|
||
expect(component.status).to eq(: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) | ||
|
||
expect(component.status).to eq(:discontinued) | ||
end | ||
end | ||
end |