Skip to content

Commit

Permalink
App configuration: Use SimpleOrderContents by default
Browse files Browse the repository at this point in the history
With the null promotion configuration in place, we can't use the legacy
Spree::OrderContents class, as that makes references to the legacy
promotion system.

See solidusio#5755
  • Loading branch information
mamhoff committed Jun 6, 2024
1 parent ea09d7e commit f132999
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 40 deletions.
14 changes: 0 additions & 14 deletions api/spec/requests/spree/api/orders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,6 @@ module Spree::Api
end
end
end

context "with existing promotion" do
let(:discount) { 2 }
before do
create(:promotion, :with_line_item_adjustment, apply_automatically: true, adjustment_rate: discount )
end

it "activates the promotion" do
post spree.api_orders_path, params: { order: { line_items: { "0" => { variant_id: variant.to_param, quantity: 1 } } } }
order = Spree::Order.last
line_item = order.line_items.first
expect(order.total).to eq(line_item.price - discount)
end
end
end

context "when the current user can administrate the order" do
Expand Down
2 changes: 1 addition & 1 deletion core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def default_pricing_options
# @!attribute [rw] order_contents_class
# @return [Class] a class with the same public interfaces as
# Spree::OrderContents.
class_name_attribute :order_contents_class, default: 'Spree::OrderContents'
class_name_attribute :order_contents_class, default: 'Spree::SimpleOrderContents'

# Allows providing your own class for shipping an order.
#
Expand Down
4 changes: 4 additions & 0 deletions core/spec/lib/spree/app_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
expect(prefs.variant_search_class).to eq Spree::Core::Search::Variant
end

it "uses simple order contents class by default" do
expect(prefs.order_contents_class).to eq Spree::SimpleOrderContents
end

it "uses variant price selector class by default" do
expect(prefs.variant_price_selector_class).to eq Spree::Variant::PriceSelector
end
Expand Down
25 changes: 0 additions & 25 deletions core/spec/models/spree/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -915,31 +915,6 @@
end
end

describe "adding items to the cart" do
let(:order) { create :order }
let(:line_item) { create :line_item, order: order }
let(:promo) { create :promotion_with_item_adjustment, adjustment_rate: 5, code: 'promo' }
let(:promotion_code) { promo.codes.first }
let(:variant) { create :variant }

it "updates the promotions for new line items" do
expect(line_item.adjustments).to be_empty
expect(order.adjustment_total).to eq 0

promo.activate order: order, promotion_code: promotion_code
order.recalculate

expect(line_item.adjustments.size).to eq(1)
expect(order.adjustment_total).to eq(-5)

other_line_item = order.contents.add(variant, 1, currency: order.currency)

expect(other_line_item).not_to eq line_item
expect(other_line_item.adjustments.size).to eq(1)
expect(order.adjustment_total).to eq(-10)
end
end

describe "promotion deletion" do
subject { promotion.destroy! }

Expand Down
25 changes: 25 additions & 0 deletions legacy_promotions/spec/models/spree/promotion_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,29 @@
}.to change{ order.reload.state }.from("confirm").to("address")
end
end

describe "adding items to the cart" do
let(:order) { create :order }
let(:line_item) { create :line_item, order: order }
let(:promo) { create :promotion_with_item_adjustment, adjustment_rate: 5, code: 'promo' }
let(:promotion_code) { promo.codes.first }
let(:variant) { create :variant }

it "updates the promotions for new line items" do
expect(line_item.adjustments).to be_empty
expect(order.adjustment_total).to eq 0

promo.activate order: order, promotion_code: promotion_code
order.recalculate

expect(line_item.adjustments.size).to eq(1)
expect(order.adjustment_total).to eq(-5)

other_line_item = order.contents.add(variant, 1, currency: order.currency)

expect(other_line_item).not_to eq line_item
expect(other_line_item.adjustments.size).to eq(1)
expect(order.adjustment_total).to eq(-10)
end
end
end
61 changes: 61 additions & 0 deletions legacy_promotions/spec/requests/spree/api/orders_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'API Orders', type: :request do
let!(:order) { create(:order) }
let(:variant) { create(:variant) }
let(:line_item) { create(:line_item) }

let(:attributes) {
[:number, :item_total, :display_total, :total,
:state, :adjustment_total,
:user_id, :created_at, :updated_at,
:completed_at, :payment_total, :shipment_state,
:payment_state, :email, :special_instructions,
:total_quantity, :display_item_total, :currency]
}

let(:address_params) { { country_id: Country.first.id, state_id: State.first.id } }

let(:current_api_user) do
user = Spree.user_class.new(email: "[email protected]")
user.generate_spree_api_key!
user
end

before do
stub_authentication!
end

describe "POST create" do
let(:target_user) { create :user }
let(:date_override) { Time.parse('2015-01-01') }
let(:attributes) { { user_id: target_user.id, created_at: date_override, email: target_user.email } }

subject do
post spree.api_orders_path, params: { order: attributes }
response
end

context "when the current user cannot administrate the order" do
custom_authorization! do |_|
can :create, Spree::Order
end

context "with existing promotion" do
let(:discount) { 2 }
before do
create(:promotion, :with_line_item_adjustment, apply_automatically: true, adjustment_rate: discount )
end

it "activates the promotion" do
post spree.api_orders_path, params: { order: { line_items: { "0" => { variant_id: variant.to_param, quantity: 1 } } } }
order = Spree::Order.last
line_item = order.line_items.first
expect(order.total).to eq(line_item.price - discount)
end
end
end
end
end

0 comments on commit f132999

Please sign in to comment.