mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-02 06:51:40 +00:00
Compare commits
3 Commits
RachL-patc
...
v2.9.10
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73323e1e25 | ||
|
|
d758787a72 | ||
|
|
0dea2f0917 |
@@ -65,7 +65,10 @@ class EnterprisesController < BaseController
|
|||||||
def reset_order
|
def reset_order
|
||||||
order = current_order(true)
|
order = current_order(true)
|
||||||
|
|
||||||
OrderCartReset.new(order, params[:id], try_spree_current_user, current_customer).call
|
# reset_distributor must be called before any call to current_customer or current_distributor
|
||||||
|
order_cart_reset = OrderCartReset.new(order, params[:id])
|
||||||
|
order_cart_reset.reset_distributor
|
||||||
|
order_cart_reset.reset_other!(try_spree_current_user, current_customer)
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
flash[:error] = I18n.t(:enterprise_shop_show_error)
|
flash[:error] = I18n.t(:enterprise_shop_show_error)
|
||||||
redirect_to shops_path
|
redirect_to shops_path
|
||||||
|
|||||||
@@ -2,25 +2,12 @@
|
|||||||
|
|
||||||
# Resets an order by verifying it's state and fixing any issues
|
# Resets an order by verifying it's state and fixing any issues
|
||||||
class OrderCartReset
|
class OrderCartReset
|
||||||
def initialize(order, distributor_id, current_user, current_customer)
|
def initialize(order, distributor_id)
|
||||||
@order = order
|
@order = order
|
||||||
@distributor ||= Enterprise.is_distributor.find_by_permalink(distributor_id) ||
|
@distributor ||= Enterprise.is_distributor.find_by_permalink(distributor_id) ||
|
||||||
Enterprise.is_distributor.find(distributor_id)
|
Enterprise.is_distributor.find(distributor_id)
|
||||||
@current_user = current_user
|
|
||||||
@current_customer = current_customer
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
|
||||||
reset_distributor
|
|
||||||
reset_user_and_customer if current_user
|
|
||||||
reset_order_cycle
|
|
||||||
order.save!
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
attr_reader :order, :distributor, :current_user, :current_customer
|
|
||||||
|
|
||||||
def reset_distributor
|
def reset_distributor
|
||||||
if order.distributor && order.distributor != distributor
|
if order.distributor && order.distributor != distributor
|
||||||
order.empty!
|
order.empty!
|
||||||
@@ -29,12 +16,24 @@ class OrderCartReset
|
|||||||
order.distributor = distributor
|
order.distributor = distributor
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset_user_and_customer
|
def reset_other!(current_user, current_customer)
|
||||||
|
reset_user_and_customer(current_user)
|
||||||
|
reset_order_cycle(current_customer)
|
||||||
|
order.save!
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
attr_reader :order, :distributor, :current_user
|
||||||
|
|
||||||
|
def reset_user_and_customer(current_user)
|
||||||
|
return unless current_user
|
||||||
|
|
||||||
order.associate_user!(current_user) if order.user.blank? || order.email.blank?
|
order.associate_user!(current_user) if order.user.blank? || order.email.blank?
|
||||||
order.__send__(:associate_customer) if order.customer.nil? # Only associates existing customers
|
order.__send__(:associate_customer) if order.customer.nil? # Only associates existing customers
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset_order_cycle
|
def reset_order_cycle(current_customer)
|
||||||
listed_order_cycles = Shop::OrderCyclesList.new(distributor, current_customer).call
|
listed_order_cycles = Shop::OrderCyclesList.new(distributor, current_customer).call
|
||||||
|
|
||||||
if order_cycle_not_listed?(order.order_cycle, listed_order_cycles)
|
if order_cycle_not_listed?(order.order_cycle, listed_order_cycles)
|
||||||
|
|||||||
@@ -17,11 +17,11 @@
|
|||||||
= label_tag nil, t(:environment)
|
= label_tag nil, t(:environment)
|
||||||
.omega.eight.columns
|
.omega.eight.columns
|
||||||
= collection_select(:payment_method, :environment, Rails.configuration.database_configuration.keys.sort, :to_s, :titleize, {}, {id: 'gtwy-env', class: 'select2 fullwidth'})
|
= collection_select(:payment_method, :environment, Rails.configuration.database_configuration.keys.sort, :to_s, :titleize, {}, {id: 'gtwy-env', class: 'select2 fullwidth'})
|
||||||
.row
|
.row
|
||||||
.alpha.three.columns
|
.alpha.three.columns
|
||||||
= label_tag nil, t(:display)
|
= label_tag nil, t(:display)
|
||||||
.omega.eight.columns
|
.omega.eight.columns
|
||||||
= select(:payment_method, :display_on, Spree::PaymentMethod::DISPLAY.collect { |display| [t(display), display == :both ? nil : display.to_s] }, {}, {class: 'select2 fullwidth'})
|
= select(:payment_method, :display_on, Spree::PaymentMethod::DISPLAY.collect { |display| [t(display), display == :both ? nil : display.to_s] }, {}, {class: 'select2 fullwidth'})
|
||||||
.row
|
.row
|
||||||
.alpha.three.columns
|
.alpha.three.columns
|
||||||
= label_tag nil, t(:active)
|
= label_tag nil, t(:active)
|
||||||
|
|||||||
@@ -15,13 +15,12 @@
|
|||||||
.omega.eight.columns
|
.omega.eight.columns
|
||||||
= f.text_area :description, class: 'fullwidth', rows: 2, placeholder: t(:spree_admin_eg_collect_your_order)
|
= f.text_area :description, class: 'fullwidth', rows: 2, placeholder: t(:spree_admin_eg_collect_your_order)
|
||||||
= error_message_on :shipping_method, :description
|
= error_message_on :shipping_method, :description
|
||||||
- if spree_current_user.admin?
|
.row
|
||||||
.row
|
.alpha.three.columns
|
||||||
.alpha.three.columns
|
= f.label :display_on, t(:display)
|
||||||
= f.label :display_on, t(:display)
|
.omega.eight.columns
|
||||||
.omega.eight.columns
|
= select(:shipping_method, :display_on, [[t(".both"), nil], [t(".back_end"), "back_end"]], {}, {class: 'select2 fullwidth'})
|
||||||
= select(:shipping_method, :display_on, [[t(".both"), nil], [t(".back_end"), "back_end"]], {}, {class: 'select2 fullwidth'})
|
= error_message_on :shipping_method, :display_on
|
||||||
= error_message_on :shipping_method, :display_on
|
|
||||||
|
|
||||||
.row
|
.row
|
||||||
.alpha.three.columns
|
.alpha.three.columns
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ require 'spec_helper'
|
|||||||
|
|
||||||
describe EnterprisesController, type: :controller do
|
describe EnterprisesController, type: :controller do
|
||||||
describe "shopping for a distributor" do
|
describe "shopping for a distributor" do
|
||||||
|
let(:user) { create(:user) }
|
||||||
let(:order) { controller.current_order(true) }
|
let(:order) { controller.current_order(true) }
|
||||||
let(:line_item) { create(:line_item) }
|
let(:line_item) { create(:line_item) }
|
||||||
let!(:current_distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
|
let!(:current_distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
|
||||||
@@ -17,10 +18,23 @@ describe EnterprisesController, type: :controller do
|
|||||||
it "sets the shop as the distributor on the order when shopping for the distributor" do
|
it "sets the shop as the distributor on the order when shopping for the distributor" do
|
||||||
spree_get :shop, id: distributor
|
spree_get :shop, id: distributor
|
||||||
|
|
||||||
|
expect(controller.current_distributor).to eq(distributor)
|
||||||
expect(controller.current_order.distributor).to eq(distributor)
|
expect(controller.current_order.distributor).to eq(distributor)
|
||||||
expect(controller.current_order.order_cycle).to be_nil
|
expect(controller.current_order.order_cycle).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when user is logged in" do
|
||||||
|
before { allow(controller).to receive(:spree_current_user) { user } }
|
||||||
|
|
||||||
|
it "sets the shop as the distributor on the order when shopping for the distributor" do
|
||||||
|
spree_get :shop, id: distributor
|
||||||
|
|
||||||
|
expect(controller.current_distributor).to eq(distributor)
|
||||||
|
expect(controller.current_order.distributor).to eq(distributor)
|
||||||
|
expect(controller.current_order.order_cycle).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "sorts order cycles by the distributor's preferred ordering attr" do
|
it "sorts order cycles by the distributor's preferred ordering attr" do
|
||||||
distributor.update_attribute(:preferred_shopfront_order_cycle_order, 'orders_close_at')
|
distributor.update_attribute(:preferred_shopfront_order_cycle_order, 'orders_close_at')
|
||||||
spree_get :shop, id: distributor
|
spree_get :shop, id: distributor
|
||||||
@@ -32,7 +46,6 @@ describe EnterprisesController, type: :controller do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "using FilterOrderCycles tag rules" do
|
context "using FilterOrderCycles tag rules" do
|
||||||
let(:user) { create(:user) }
|
|
||||||
let!(:order_cycle3) { create(:simple_order_cycle, distributors: [distributor], orders_open_at: 3.days.ago, orders_close_at: 4.days.from_now) }
|
let!(:order_cycle3) { create(:simple_order_cycle, distributors: [distributor], orders_open_at: 3.days.ago, orders_close_at: 4.days.from_now) }
|
||||||
let!(:oc3_exchange) { order_cycle3.exchanges.outgoing.to_enterprise(distributor).first }
|
let!(:oc3_exchange) { order_cycle3.exchanges.outgoing.to_enterprise(distributor).first }
|
||||||
let(:customer) { create(:customer, user: user, enterprise: distributor) }
|
let(:customer) { create(:customer, user: user, enterprise: distributor) }
|
||||||
|
|||||||
@@ -158,6 +158,8 @@ feature '
|
|||||||
it "creates payment methods" do
|
it "creates payment methods" do
|
||||||
visit spree.new_admin_payment_method_path
|
visit spree.new_admin_payment_method_path
|
||||||
fill_in 'payment_method_name', with: 'Cheque payment method'
|
fill_in 'payment_method_name', with: 'Cheque payment method'
|
||||||
|
expect(page).to have_field 'payment_method_description'
|
||||||
|
expect(page).to have_select 'payment_method_display_on'
|
||||||
|
|
||||||
check "payment_method_distributor_ids_#{distributor1.id}"
|
check "payment_method_distributor_ids_#{distributor1.id}"
|
||||||
find(:css, "tags-input .tags input").set "local\n"
|
find(:css, "tags-input .tags input").set "local\n"
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ feature 'shipping methods' do
|
|||||||
# Show the correct fields
|
# Show the correct fields
|
||||||
expect(page).to have_field 'shipping_method_name'
|
expect(page).to have_field 'shipping_method_name'
|
||||||
expect(page).to have_field 'shipping_method_description'
|
expect(page).to have_field 'shipping_method_description'
|
||||||
expect(page).not_to have_select 'shipping_method_display_on'
|
expect(page).to have_select 'shipping_method_display_on'
|
||||||
expect(page).to have_css 'div#shipping_method_zones_field'
|
expect(page).to have_css 'div#shipping_method_zones_field'
|
||||||
expect(page).to have_field 'shipping_method_require_ship_address_true', checked: true
|
expect(page).to have_field 'shipping_method_require_ship_address_true', checked: true
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ describe OrderCartReset do
|
|||||||
let(:new_distributor) { create(:distributor_enterprise) }
|
let(:new_distributor) { create(:distributor_enterprise) }
|
||||||
|
|
||||||
it "empties order" do
|
it "empties order" do
|
||||||
OrderCartReset.new(order, new_distributor.id.to_s, nil, nil).call
|
OrderCartReset.new(order, new_distributor.id.to_s).reset_distributor
|
||||||
|
|
||||||
expect(order.line_items).to be_empty
|
expect(order.line_items).to be_empty
|
||||||
end
|
end
|
||||||
@@ -28,7 +28,7 @@ describe OrderCartReset do
|
|||||||
it "empties order and makes order cycle nil" do
|
it "empties order and makes order cycle nil" do
|
||||||
expect(order_cycle_list).to receive(:call).and_return([])
|
expect(order_cycle_list).to receive(:call).and_return([])
|
||||||
|
|
||||||
OrderCartReset.new(order, distributor.id.to_s, nil, nil).call
|
OrderCartReset.new(order, distributor.id.to_s).reset_other!(nil, nil)
|
||||||
|
|
||||||
expect(order.line_items).to be_empty
|
expect(order.line_items).to be_empty
|
||||||
expect(order.order_cycle).to be_nil
|
expect(order.order_cycle).to be_nil
|
||||||
@@ -38,7 +38,7 @@ describe OrderCartReset do
|
|||||||
other_order_cycle = create(:simple_order_cycle, distributors: [distributor])
|
other_order_cycle = create(:simple_order_cycle, distributors: [distributor])
|
||||||
expect(order_cycle_list).to receive(:call).and_return([other_order_cycle])
|
expect(order_cycle_list).to receive(:call).and_return([other_order_cycle])
|
||||||
|
|
||||||
OrderCartReset.new(order, distributor.id.to_s, nil, nil).call
|
OrderCartReset.new(order, distributor.id.to_s).reset_other!(nil, nil)
|
||||||
|
|
||||||
expect(order.order_cycle).to eq other_order_cycle
|
expect(order.order_cycle).to eq other_order_cycle
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user