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 cc1aa8b
Showing
7 changed files
with
129 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
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::Products::Status::Component < SolidusAdmin::BaseComponent | ||
COLORS = { | ||
active: :green, | ||
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] | ||
# :active when the product is not persisted | ||
# :available when the product is persisted and available | ||
# :discontinued when the product is persisted and not available | ||
def status | ||
return :active if @product.id.nil? | ||
|
||
return :available if @product.available? | ||
|
||
:discontinued | ||
end | ||
end |
4 changes: 4 additions & 0 deletions
4
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,4 @@ | ||
en: | ||
active: 'Active' | ||
available: 'Available' | ||
discontinued: 'Discontinued' |
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
# @component "products/status" | ||
class SolidusAdmin::Products::Status::ComponentPreview < ViewComponent::Preview | ||
include SolidusAdmin::Preview | ||
|
||
def overview | ||
render_with_template(locals: | ||
{ | ||
definitions: { | ||
active: active_component, | ||
available: available_component, | ||
discontinued: discontinued_component | ||
} | ||
}) | ||
end | ||
|
||
private | ||
|
||
def active_component | ||
current_component.new( | ||
product: Spree::Product.new | ||
) | ||
end | ||
|
||
def available_component | ||
current_component.new( | ||
product: Spree::Product.new(id: 1, available_on: Time.current) | ||
) | ||
end | ||
|
||
def discontinued_component | ||
current_component.new( | ||
product: Spree::Product.new(id: 1, 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> |
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
# 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 :active when the product is not persisted" do | ||
product = Spree::Product.new | ||
|
||
component = described_class.new(product: product) | ||
|
||
expect(component.status).to eq(:active) | ||
end | ||
|
||
it "returns :available when the product is persisted and available" do | ||
product = Spree::Product.new(id: 1, available_on: Time.current) | ||
|
||
component = described_class.new(product: product) | ||
|
||
expect(component.status).to eq(:available) | ||
end | ||
|
||
it "returns :discontinued when the product is persisted and not available" do | ||
product = Spree::Product.new(id: 1, available_on: nil) | ||
|
||
component = described_class.new(product: product) | ||
|
||
expect(component.status).to eq(:discontinued) | ||
end | ||
end | ||
end |