Skip to content

Commit

Permalink
Refactor Order#current_order argument
Browse files Browse the repository at this point in the history
  • Loading branch information
HugsDaniel committed Apr 12, 2018
1 parent 46775ab commit ae93520
Show file tree
Hide file tree
Showing 19 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/controllers/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def set_order_cycles

# And default to the only order cycle if there's only the one
if @order_cycles.count == 1
current_order(true).set_order_cycle! @order_cycles.first
current_order({ create_order_if_necessary: true }).set_order_cycle! @order_cycles.first
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/enterprises_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ def clean_permalink
end

def check_stock_levels
if current_order(true).insufficient_stock_lines.present?
if current_order({ create_order_if_necessary: true }).insufficient_stock_lines.present?
redirect_to spree.cart_path
end
end

def reset_order
distributor = Enterprise.is_distributor.find_by_permalink(params[:id]) || Enterprise.is_distributor.find(params[:id])
order = current_order(true)
order = current_order({ create_order_if_necessary: true })

reset_distributor(order, distributor)

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/shop_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def products
def order_cycle
if request.post?
if oc = OrderCycle.with_distributor(@distributor).active.find_by_id(params[:order_cycle_id])
current_order(true).set_order_cycle! oc
current_order({ create_order_if_necessary: true }).set_order_cycle! oc
render partial: "json/order_cycle"
else
render status: 404, json: ""
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/spree/orders_controller_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# Patching to redirect to shop if order is empty
def edit
@order = current_order(true)
@order = current_order({ create_order_if_necessary: true })
@insufficient_stock_lines = @order.insufficient_stock_lines

if @order.line_items.empty?
Expand Down Expand Up @@ -78,7 +78,7 @@ def populate
# callbacks on Spree::Adjustment and then manually invoke Spree::Order#update! on success.

Spree::Adjustment.without_callbacks do
populator = Spree::OrderPopulator.new(current_order(true), current_currency)
populator = Spree::OrderPopulator.new(current_order({ create_order_if_necessary: true }), current_currency)

if populator.populate(params.slice(:products, :variants, :quantity), true)
fire_event('spree.cart.add')
Expand Down Expand Up @@ -127,7 +127,7 @@ def li_stock_levels(order)
end

def update_distribution
@order = current_order(true)
@order = current_order({ create_order_if_necessary: true })

if params[:commit] == 'Choose Hub'
distributor = Enterprise.is_distributor.find params[:order][:distributor_id]
Expand Down Expand Up @@ -159,7 +159,7 @@ def remove_missing_line_items(attrs)
end

def clear
@order = current_order(true)
@order = current_order({ create_order_if_necessary: true })
@order.empty!
@order.set_order_cycle! nil
redirect_to main_app.enterprise_path(@order.distributor.id)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/enterprises_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module EnterprisesHelper
def current_distributor
@current_distributor ||= current_order(false).andand.distributor
@current_distributor ||= current_order({ create_order_if_necessary: false }).andand.distributor
end

def current_customer
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/order_cycles_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module OrderCyclesHelper
def current_order_cycle
@current_order_cycle ||= current_order(false).andand.order_cycle
@current_order_cycle ||= current_order({ create_order_if_necessary: false }).andand.order_cycle
end

def permitted_enterprises_for(order_cycle)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/shared_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SharedHelper
def distributor_link_class(distributor)
cart = current_order(true)
cart = current_order({ create_order_if_necessary: true })
@active_distributors ||= Enterprise.distributors_with_active_order_cycles

klass = "shop-distributor"
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/spree/orders_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Spree
module OrdersHelper
def cart_is_empty
order = current_order(false)
order = current_order({ create_order_if_necessary: false })
order.nil? || order.line_items.empty?
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/reset_order_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def call

# Builds an order setting the token and distributor of the one specified
def build_new_order
new_order = controller.current_order(true)
new_order = controller.current_order({ create_order_if_necessary: true })
new_order.set_distributor!(distributor)
new_order.tokenized_permission.token = token
new_order.tokenized_permission.save!
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/products/_add_to_cart.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.add-to-cart
- order = current_order(false)
- order = current_order({ create_order_if_necessary: false })

- if product_out_of_stock
= content_tag('strong', t(:out_of_stock))
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/products/_distributor_details.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
%legend
= t :products_distributor
.distributor-details
- order = current_order(false)
- order = current_order({ create_order_if_necessary: false })
- if order.andand.distributor.present?
= render 'enterprises/distributor_details', :distributor => order.distributor
- else
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/products/_source.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
%h6.product-section-title= t(:distributors)
%table#product-source.table-display{:width => "100%"}
%tbody
- order = current_order(false)
- order = current_order({ create_order_if_necessary: false })
- validator = DistributionChangeValidator.new(order)
- Enterprise.distributing_products(@product).each do |distributor|
- if !order.nil? && distributor == order.distributor
Expand Down
4 changes: 2 additions & 2 deletions lib/spree/core/controller_helpers/order_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Spree::Core::ControllerHelpers::Order.class_eval do
def current_order_with_scoped_variants(create_order_if_necessary = false)
order = current_order_without_scoped_variants(create_order_if_necessary)
def current_order_with_scoped_variants(options = {})
order = current_order_without_scoped_variants(options)

if order
scoper = OpenFoodNetwork::ScopeVariantToHub.new(order.distributor)
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/checkout_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
end

it "sets the new order's token to the same as the old order" do
order = controller.current_order(true)
order = controller.current_order({ create_order_if_necessary: true })
spree_post :update, order: {}
expect(controller.current_order.token).to eq order.token
end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/enterprises_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe EnterprisesController, type: :controller do
describe "shopping for a distributor" do
let(:order) { controller.current_order(true) }
let(:order) { controller.current_order({ create_order_if_necessary: true }) }


let!(:current_distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/spree/checkout_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

describe Spree::CheckoutController, type: :controller do
context 'rendering edit from within spree for the current checkout state' do
let(:order) { controller.current_order(true) }
let(:order) { controller.current_order({ create_order_if_necessary: true }) }
let(:user) { create(:user) }

before do
Expand Down
10 changes: 5 additions & 5 deletions spec/controllers/spree/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
it "redirects home with message if hub is not ready for checkout" do
VariantOverride.stub(:indexed).and_return({})

order = subject.current_order(true)
order = subject.current_order({ create_order_if_necessary: true })
distributor.stub(:ready_for_checkout?) { false }
order.stub(distributor: distributor, order_cycle: order_cycle)

Expand All @@ -44,7 +44,7 @@
end

describe "when an item has insufficient stock" do
let(:order) { subject.current_order(true) }
let(:order) { subject.current_order({ create_order_if_necessary: true }) }
let(:oc) { create(:simple_order_cycle, distributors: [d], variants: [variant]) }
let(:d) { create(:distributor_enterprise, shipping_methods: [create(:shipping_method)], payment_methods: [create(:payment_method)]) }
let(:variant) { create(:variant, on_demand: false, on_hand: 5) }
Expand Down Expand Up @@ -129,7 +129,7 @@
distributor_product = create(:distributor_enterprise)
p = create(:product, :distributors => [distributor_product], :group_buy => true)

order = subject.current_order(true)
order = subject.current_order({ create_order_if_necessary: true })
order.stub(:distributor) { distributor_product }
order.should_receive(:set_variant_attributes).with(p.master, {'max_quantity' => '3'})
controller.stub(:current_order).and_return(order)
Expand Down Expand Up @@ -164,7 +164,7 @@
describe "removing line items from cart" do
describe "when I pass params that includes a line item no longer in our cart" do
it "should silently ignore the missing line item" do
order = subject.current_order(true)
order = subject.current_order({ create_order_if_necessary: true })
li = order.add_variant(create(:simple_product, on_hand: 110).variants.first)
spree_get :update, order: { line_items_attributes: {
"0" => {quantity: "0", id: "9999"},
Expand All @@ -176,7 +176,7 @@
end

it "filters line items that are missing from params" do
order = subject.current_order(true)
order = subject.current_order({ create_order_if_necessary: true })
li = order.add_variant(create(:simple_product).master)

attrs = {
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/spree/paypal_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Spree
end

context 'when confirming' do
let(:previous_order) { controller.current_order(true) }
let(:previous_order) { controller.current_order({ create_order_if_necessary: true }) }
let(:payment_method) { create(:payment_method) }

before do
Expand Down
2 changes: 1 addition & 1 deletion spec/performance/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:distributor) { create(:distributor_enterprise) }
let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor], variants: products.map { |p| p.variants.first }) }
let(:products) { (0...num_products).map { create(:product) } }
let(:order) { subject.current_order(true) }
let(:order) { subject.current_order({ create_order_if_necessary: true }) }
let(:num_products) { 20 }

before do
Expand Down

0 comments on commit ae93520

Please sign in to comment.