Skip to content

Commit

Permalink
Merge pull request solidusio#5791 from mamhoff/admin-adjustable-compo…
Browse files Browse the repository at this point in the history
…nents

Admin adjustable components
  • Loading branch information
tvdeyen authored Jun 20, 2024
2 parents b555db8 + ec15751 commit a176e05
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Adjustments::Index::Adjustable::Component < SolidusAdmin::BaseComponent
attr_reader :adjustment, :adjustable, :model_name

def initialize(adjustment)
@adjustment = adjustment
@adjustable = adjustment.adjustable
@model_name = adjustable&.model_name&.human
end

def call
render component("ui/thumbnail_with_caption").new(caption: caption, detail: detail) do
thumbnail
end
end

def thumbnail
render(component("ui/thumbnail").for(adjustment.adjustable, class: "basis-10"))
end

def caption
end

def detail
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Adjustments::Index::Adjustable::SpreeLineItem::Component < SolidusAdmin::Orders::Show::Adjustments::Index::Adjustable::Component
delegate :variant, to: :adjustable

def caption
options_text = variant.options_text.presence
options_text || variant.sku
end

def detail
link_to(variant.product.name, solidus_admin.product_path(variant.product), class: "body-link")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Adjustments::Index::Adjustable::SpreeOrder::Component < SolidusAdmin::Orders::Show::Adjustments::Index::Adjustable::Component
def caption
"#{Spree::Order.model_name.human} ##{adjustable.number}"
end

def detail
adjustable.display_total
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Adjustments::Index::Adjustable::SpreeShipment::Component < SolidusAdmin::Orders::Show::Adjustments::Index::Adjustable::Component
def caption
"#{t('spree.shipment')} ##{adjustable.number}"
end

def detail
link_to(
adjustable.shipping_method.name,
spree.edit_admin_shipping_method_path(adjustable.shipping_method),
class: "body-link"
)
end
end

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,14 @@ def columns
header: :adjustable,
col: { class: 'w-56' },
data: ->(adjustment) {
tag.figure(safe_join([
render(component("ui/thumbnail").for(adjustment.adjustable, class: "basis-10")),
figcaption_for_adjustable(adjustment),
]), class: "flex items-center gap-2")
render_thumbnail_with_caption(adjustment, :adjustable)
}
},
{
header: :source,
col: { class: "w-56" },
data: ->(adjustment) {
component_name = adjustment.source&.class&.table_name&.singularize
component_key = ["orders/show/adjustments/index/adjustment", component_name].compact.join("/")
render component(component_key).new(adjustment)
render_thumbnail_with_caption(adjustment, :source)
}
},
{
Expand Down Expand Up @@ -142,56 +137,11 @@ def columns
]
end

def icon_thumbnail(name)
render component("ui/thumbnail").new(src: svg_data_uri(icon_tag(name)))
end

def svg_data_uri(data)
"data:image/svg+xml;base64,#{Base64.strict_encode64(data)}"
end

def figcaption_for_adjustable(adjustment)
# ["Spree::LineItem", "Spree::Order", "Spree::Shipment"]
record = adjustment.adjustable
record_class = adjustment.adjustable_type&.constantize

case record || record_class
when Spree::LineItem
variant = record.variant
options_text = variant.options_text.presence

description = options_text || variant.sku
detail = link_to(variant.product.name, solidus_admin.product_path(record.variant.product), class: "body-link")
when Spree::Order
description = "#{Spree::Order.model_name.human} ##{record.number}"
detail = record.display_total
when Spree::Shipment
description = "#{t('spree.shipment')} ##{record.number}"
detail = link_to(record.shipping_method.name, spree.edit_admin_shipping_method_path(record.shipping_method), class: "body-link")
when nil
# noop
else
name_method = [:display_name, :name, :number].find { record.respond_to? _1 } if record
price_method = [:display_amount, :display_total, :display_cost].find { record.respond_to? _1 } if record

description = record_class.model_name.human
description = "#{description} - #{record.public_send(name_method)}" if name_method

# attempt creating a link
url_options = [:admin, record, :edit, { only_path: true }]
url = begin; spree.url_for(url_options); rescue NoMethodError => e; logger.error(e.to_s); nil end

description = link_to(description, url, class: "body-link") if url
detail = record.public_send(price_method) if price_method
end

thumbnail_caption(description, detail)
end
private

def thumbnail_caption(first_line, second_line)
tag.figcaption(safe_join([
tag.div(first_line || NBSP, class: 'text-black body-small whitespace-nowrap text-ellipsis overflow-hidden'),
tag.div(second_line || NBSP, class: 'text-gray-500 body-small whitespace-nowrap text-ellipsis overflow-hidden')
]), class: "flex flex-col gap-0 max-w-[15rem]")
def render_thumbnail_with_caption(adjustment, role)
component_name = adjustment.send(role).class.base_class.name.delete("::").underscore if adjustment.send(role)
component_key = ["orders/show/adjustments/index/#{role}", component_name].compact.join("/")
render component(component_key).new(adjustment)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Adjustments::Index::Source::Component < SolidusAdmin::BaseComponent
attr_reader :adjustment, :source, :model_name

def initialize(adjustment)
@adjustment = adjustment
@source = adjustment.source
@model_name = source&.model_name&.human
end

def call
render component("ui/thumbnail_with_caption").new(icon: icon, caption: caption, detail: detail)
end

def caption
adjustment.label
end

def detail
end

def icon
"question-line"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Adjustments::Index::Source::SpreeTaxRate::Component < SolidusAdmin::Orders::Show::Adjustments::Index::Source::Component
def icon
"percent-line"
end

def detail
link_to("#{model_name}: #{zone_name}", spree.edit_admin_tax_rate_path(adjustment.source_id), class: "body-link")
end

private

def zone_name
source.zone&.name || t("spree.all_zones")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Adjustments::Index::Source::SpreeUnitCancel::Component < SolidusAdmin::Orders::Show::Adjustments::Index::Source::Component
def icon
"close-circle-line"
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<figure class="flex items-center gap-2">
<% if content? %>
<%= content %>
<% else %>
<%= icon_thumbnail %>
<% end %>
<figcaption class="flex flex-col gap-0 max-w-[15rem]">
<div class="text-black body-small whitespace-nowrap text-ellipsis overflow-hidden">
<%= adjustment.label %>
<%= caption %>
</div>
<% if detail %>
<div class="text-gray-500 body-small whitespace-nowrap text-ellipsis overflow-hidden">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class SolidusAdmin::UI::ThumbnailWithCaption::Component < SolidusAdmin::BaseComponent
attr_reader :icon, :caption, :detail

def initialize(icon: "question-line", caption: "", detail: nil)
@icon = icon
@caption = caption
@detail = detail
end

def icon_thumbnail
render component("ui/thumbnail").new(icon: icon)
end
end
21 changes: 21 additions & 0 deletions admin/spec/features/orders/adjustments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,25 @@
expect(page).to be_axe_clean
end
end

context "with a shipment being adjusted" do
let(:order) { create(:order_with_line_items, number: "R123456789") }

before do
order.shipments.first.adjustments.create!(
order: order,
label: "Manual shipping discount",
amount: -2,
source: nil
)
end

it "can display a shipment adjustment" do
visit "/admin/orders/R123456789"

click_on "Adjustments"
expect(page).to have_content("Manual shipping discount")
expect(page).to be_axe_clean
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Adjustments::Index::Adjustment::SpreePromotionAction::Component < SolidusAdmin::Orders::Show::Adjustments::Index::Adjustment::Component
class SolidusAdmin::Orders::Show::Adjustments::Index::Source::SpreePromotionAction::Component < SolidusAdmin::Orders::Show::Adjustments::Index::Source::Component
def icon
"megaphone-line"
end

def detail
link_to("#{model_name}: #{promotion_name}", spree.edit_admin_promotion_path(adjustment.source_id), class: "body-link")
end
Expand Down

0 comments on commit a176e05

Please sign in to comment.