diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/component.rb b/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/component.rb new file mode 100644 index 00000000000..341e5f8931e --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/component.rb @@ -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 diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/spree_line_item/component.rb b/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/spree_line_item/component.rb new file mode 100644 index 00000000000..8f421f9a7b9 --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/spree_line_item/component.rb @@ -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 diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/spree_order/component.rb b/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/spree_order/component.rb new file mode 100644 index 00000000000..3fe8cfa7faf --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/spree_order/component.rb @@ -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 diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/spree_shipment/component.rb b/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/spree_shipment/component.rb new file mode 100644 index 00000000000..99515df64de --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustable/spree_shipment/component.rb @@ -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 diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/component.rb b/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/component.rb deleted file mode 100644 index f5a55798567..00000000000 --- a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/component.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class SolidusAdmin::Orders::Show::Adjustments::Index::Adjustment::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 detail - end -end diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/spree_tax_rate/component.rb b/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/spree_tax_rate/component.rb deleted file mode 100644 index bd2b3a59c5a..00000000000 --- a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/spree_tax_rate/component.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -class SolidusAdmin::Orders::Show::Adjustments::Index::Adjustment::SpreeTaxRate::Component < SolidusAdmin::Orders::Show::Adjustments::Index::Adjustment::Component - 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 diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/spree_unit_cancel/component.rb b/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/spree_unit_cancel/component.rb deleted file mode 100644 index cf19bd49b58..00000000000 --- a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/spree_unit_cancel/component.rb +++ /dev/null @@ -1,4 +0,0 @@ -# frozen_string_literal: true - -class SolidusAdmin::Orders::Show::Adjustments::Index::Adjustment::SpreeUnitCancel::Component < SolidusAdmin::Orders::Show::Adjustments::Index::Adjustment::Component -end diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/component.rb b/admin/app/components/solidus_admin/orders/show/adjustments/index/component.rb index dea11e9f369..7a06b5abd72 100644 --- a/admin/app/components/solidus_admin/orders/show/adjustments/index/component.rb +++ b/admin/app/components/solidus_admin/orders/show/adjustments/index/component.rb @@ -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) } }, { @@ -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 diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/source/component.rb b/admin/app/components/solidus_admin/orders/show/adjustments/index/source/component.rb new file mode 100644 index 00000000000..4f1e34781c2 --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/adjustments/index/source/component.rb @@ -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 diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/source/spree_tax_rate/component.rb b/admin/app/components/solidus_admin/orders/show/adjustments/index/source/spree_tax_rate/component.rb new file mode 100644 index 00000000000..d3659ac3d56 --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/adjustments/index/source/spree_tax_rate/component.rb @@ -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 diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/source/spree_unit_cancel/component.rb b/admin/app/components/solidus_admin/orders/show/adjustments/index/source/spree_unit_cancel/component.rb new file mode 100644 index 00000000000..dd405bf43ed --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/adjustments/index/source/spree_unit_cancel/component.rb @@ -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 diff --git a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/component.html.erb b/admin/app/components/solidus_admin/ui/thumbnail_with_caption/component.html.erb similarity index 77% rename from admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/component.html.erb rename to admin/app/components/solidus_admin/ui/thumbnail_with_caption/component.html.erb index 16dcd17d1b9..d8d6dbed29b 100644 --- a/admin/app/components/solidus_admin/orders/show/adjustments/index/adjustment/component.html.erb +++ b/admin/app/components/solidus_admin/ui/thumbnail_with_caption/component.html.erb @@ -1,7 +1,12 @@
+ <% if content? %> + <%= content %> + <% else %> + <%= icon_thumbnail %> + <% end %>
- <%= adjustment.label %> + <%= caption %>
<% if detail %>
diff --git a/admin/app/components/solidus_admin/ui/thumbnail_with_caption/component.rb b/admin/app/components/solidus_admin/ui/thumbnail_with_caption/component.rb new file mode 100644 index 00000000000..575a57fc35b --- /dev/null +++ b/admin/app/components/solidus_admin/ui/thumbnail_with_caption/component.rb @@ -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 diff --git a/admin/spec/features/orders/adjustments_spec.rb b/admin/spec/features/orders/adjustments_spec.rb index c9219ef8a7a..949a72d4d37 100644 --- a/admin/spec/features/orders/adjustments_spec.rb +++ b/admin/spec/features/orders/adjustments_spec.rb @@ -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 diff --git a/legacy_promotions/lib/components/admin/solidus_admin/orders/show/adjustments/index/adjustment/spree_promotion_action/component.rb b/legacy_promotions/lib/components/admin/solidus_admin/orders/show/adjustments/index/source/spree_promotion_action/component.rb similarity index 54% rename from legacy_promotions/lib/components/admin/solidus_admin/orders/show/adjustments/index/adjustment/spree_promotion_action/component.rb rename to legacy_promotions/lib/components/admin/solidus_admin/orders/show/adjustments/index/source/spree_promotion_action/component.rb index aec27a1435b..cb84016a112 100644 --- a/legacy_promotions/lib/components/admin/solidus_admin/orders/show/adjustments/index/adjustment/spree_promotion_action/component.rb +++ b/legacy_promotions/lib/components/admin/solidus_admin/orders/show/adjustments/index/source/spree_promotion_action/component.rb @@ -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