Skip to content

Commit

Permalink
Merge pull request #5775 from mamhoff/use-simple-order-contents-by-de…
Browse files Browse the repository at this point in the history
…fault

App configuration: Use SimpleOrderContents by default
  • Loading branch information
tvdeyen authored Jun 11, 2024
2 parents 8048013 + 4438744 commit e68681f
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 41 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

before do
allow(Spree::Order).to receive_message_chain(:includes, find_by!: order)
allow(order).to receive_messages(contents: Spree::OrderContents.new(order))
allow(order).to receive_messages(contents: Spree::Config.order_contents_class.new(order))
end

context "#approve" 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
File renamed without changes.
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
50 changes: 50 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,50 @@
# 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(: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(:attributes) { { user_id: target_user.id, 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 e68681f

Please sign in to comment.