Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SolidusAdmin: Components per Adjustment Source #5789

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<figure class="flex items-center gap-2">
<figcaption class="flex flex-col gap-0 max-w-[15rem]">
<div class="text-black body-small whitespace-nowrap text-ellipsis overflow-hidden">
<%= adjustment.label %>
</div>
<% if detail %>
<div class="text-gray-500 body-small whitespace-nowrap text-ellipsis overflow-hidden">
<%= detail %>
</div>
<% end %>
</figcaption>
</figure>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Adjustments::Index::Adjustment::SpreeUnitCancel::Component < SolidusAdmin::Orders::Show::Adjustments::Index::Adjustment::Component
end
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def columns
header: :source,
col: { class: "w-56" },
data: ->(adjustment) {
tag.figure(safe_join([
figcaption_for_source(adjustment),
]), class: "flex items-center gap-2")
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)
}
},
{
Expand Down Expand Up @@ -150,26 +150,6 @@ def svg_data_uri(data)
"data:image/svg+xml;base64,#{Base64.strict_encode64(data)}"
end

def figcaption_for_source(adjustment)
return thumbnail_caption(adjustment.label, nil) unless adjustment.source_type

# ["Spree::PromotionAction", nil, "Spree::UnitCancel", "Spree::TaxRate"]
record = adjustment.source
record_class = adjustment.source_type&.constantize
model_name = record_class.model_name.human

case record || record_class
when Spree::TaxRate
detail = link_to("#{model_name}: #{record.zone&.name || t('spree.all_zones')}", spree.edit_admin_tax_rate_path(adjustment.source_id), class: "body-link")
when Spree::PromotionAction
detail = link_to("#{model_name}: #{record.promotion.name}", spree.edit_admin_promotion_path(adjustment.source_id), class: "body-link")
else
detail = "#{model_name}: #{record.display_amount}" if record.respond_to?(:display_amount)
end

thumbnail_caption(adjustment.label, detail)
end

def figcaption_for_adjustable(adjustment)
# ["Spree::LineItem", "Spree::Order", "Spree::Shipment"]
record = adjustment.adjustable
Expand Down
76 changes: 56 additions & 20 deletions admin/spec/features/orders/adjustments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@
require 'spec_helper'

describe "Order", :js, type: :feature do
before { sign_in create(:admin_user, email: '[email protected]') }
let(:order) { create(:order, number: "R123456789") }

it "allows detaching a customer from an order" do
before do
allow(SolidusAdmin::Config).to receive(:enable_alpha_features?) { true }
sign_in create(:admin_user, email: '[email protected]')
end

order = create(:order, number: "R123456789", user: create(:user))
Spree::Adjustment.insert_all([
{
order_id: order.id,
adjustable_id: order.id,
adjustable_type: "Spree::Order",
amount: 10,
label: "Test Adjustment",
eligible: true,
finalized: false,
created_at: Time.current,
updated_at: Time.current,
included: false,
source_type: "Spree::Order",
source_id: order.id,
promotion_code_id: nil,
},
])
it "allows locking and unlocking adjustments" do
taxrate = create(:tax_rate)
Spree::Adjustment.create(
order: order,
adjustable: order,
amount: 10,
label: "Test Adjustment",
eligible: true,
finalized: false,
created_at: Time.current,
updated_at: Time.current,
included: false,
source: taxrate,
promotion_code_id: nil,
)
visit "/admin/orders/R123456789"

click_on "Adjustments"
Expand All @@ -49,4 +48,41 @@

expect(page).to be_axe_clean
end

it "can display an adjustment without a source" do
Spree::Adjustment.create(
order: order,
adjustable: order,
amount: 10,
label: "No Source Adjustment",
eligible: true,
finalized: false,
created_at: Time.current,
updated_at: Time.current,
included: false,
source: nil,
promotion_code_id: nil,
)
visit "/admin/orders/R123456789"

click_on "Adjustments"
expect(page).to have_content("No Source Adjustment")
expect(page).to be_axe_clean
end

context "with a unit cancellation" do
let(:order) { create(:order_ready_to_ship, number: "R123456789") }

before do
Spree::OrderCancellations.new(order).short_ship([order.inventory_units.first])
end

it "can display an adjustment with a unit cancellation" do
visit "/admin/orders/R123456789"

click_on "Adjustments"
expect(page).to have_content("Cancellation - Short Ship")
expect(page).to be_axe_clean
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Adjustments::Index::Adjustment::SpreePromotionAction::Component < SolidusAdmin::Orders::Show::Adjustments::Index::Adjustment::Component
def detail
link_to("#{model_name}: #{promotion_name}", spree.edit_admin_promotion_path(adjustment.source_id), class: "body-link")
end

private

def promotion_name
source.promotion.name
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe "Order", :js, type: :feature, solidus_admin: true do
let(:admin) { create(:admin_user) }
let(:order) { create(:order, number: "R123456789") }

before do
allow(SolidusAdmin::Config).to receive(:enable_alpha_features?) { true }
sign_in admin
end

context "with a promotion adjustment" do
let(:order) { create(:order_ready_to_ship, number: "R123456789") }
let(:promotion) { create(:promotion, :with_adjustable_action) }

before do
Spree::Adjustment.create!(
order: order,
source: promotion.actions.first,
adjustable: order.line_items.first,
amount: 2,
label: "Promotion Adjustment"
)
end

it "can display the adjustment" do
visit "/admin/orders/R123456789"

click_on "Adjustments"
expect(page).to have_content("Promotion Adjustment")
expect(page).to be_axe_clean
end
end
end