Files
openfoodnetwork/app/controllers/enterprises_controller.rb
Rob Harrington 48d4c8733d When we load up incomplete orders, make sure that they have a user associated if the user is logged in
Also associate customer ONLY if one already exists. This is required to prevent unauthorised access to customer-only shopfronts.
2016-05-27 16:23:26 +10:00

60 lines
1.8 KiB
Ruby

class EnterprisesController < BaseController
layout "darkswarm"
helper Spree::ProductsHelper
include OrderCyclesHelper
# These prepended filters are in the reverse order of execution
prepend_before_filter :set_order_cycles, :require_distributor_chosen, :reset_order, only: :shop
before_filter :check_stock_levels, only: :shop
before_filter :clean_permalink, only: :check_permalink
respond_to :js, only: :permalink_checker
def check_permalink
return render text: params[:permalink], status: 409 if Enterprise.find_by_permalink params[:permalink]
path = Rails.application.routes.recognize_path( "/#{ params[:permalink].to_s }" )
if path && path[:controller] == "cms_content"
render text: params[:permalink], status: 200
else
render text: params[:permalink], status: 409
end
end
private
def clean_permalink
params[:permalink] = params[:permalink].parameterize
end
def check_stock_levels
if current_order(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)
if order.distributor && order.distributor != distributor
order.empty!
order.set_order_cycle! nil
end
order.distributor = distributor
if user = try_spree_current_user
order.associate_user!(user) if (order.user.blank? || order.email.blank?)
order.send(:associate_customer) if order.customer.nil? # Only associates existing customers
end
order_cycle_options = OrderCycle.active.with_distributor(distributor)
order.order_cycle = order_cycle_options.first if order_cycle_options.count == 1
order.save!
end
end