Skip to content

Commit

Permalink
Extract order_cart_reset service from enterprises_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
luisramos0 committed May 15, 2020
1 parent a04e6ed commit 90d3c8e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 43 deletions.
44 changes: 1 addition & 43 deletions app/controllers/enterprises_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,54 +65,12 @@ def enough_stock?
def reset_order
order = current_order(true)

reset_distributor(order, distributor)

reset_user_and_customer(order) if try_spree_current_user

reset_order_cycle(order, distributor)

order.save!
OrderCartReset.new(order, params[:id], try_spree_current_user, current_customer).call
rescue ActiveRecord::RecordNotFound
flash[:error] = I18n.t(:enterprise_shop_show_error)
redirect_to shops_path
end

def distributor
@distributor ||= Enterprise.is_distributor.find_by_permalink(params[:id]) ||
Enterprise.is_distributor.find(params[:id])
end

def reset_distributor(order, distributor)
if order.distributor && order.distributor != distributor
order.empty!
order.set_order_cycle! nil
end
order.distributor = distributor
end

def reset_user_and_customer(order)
order.associate_user!(spree_current_user) if order.user.blank? || order.email.blank?
order.__send__(:associate_customer) if order.customer.nil? # Only associates existing customers
end

def reset_order_cycle(order, distributor)
order_cycles = Shop::OrderCyclesList.new(distributor, current_customer).call

if order.order_cycle.present? && !order_cycles.include?(order.order_cycle)
order.order_cycle = nil
order.empty!
order.save!
end

select_default_order_cycle(order, order_cycles)
end

def select_default_order_cycle(order, order_cycles)
return unless order.order_cycle.blank? && order_cycles.size == 1

order.order_cycle = order_cycles.first
end

def set_noindex_meta_tag
@noindex_meta_tag = true unless current_distributor.visible?
end
Expand Down
54 changes: 54 additions & 0 deletions app/services/order_cart_reset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: false

# Resets an order by verifying it's state and fixing any issues
class OrderCartReset
def initialize(order, distributor_id, current_user, current_customer)
@order = order
@distributor ||= Enterprise.is_distributor.find_by_permalink(distributor_id) ||
Enterprise.is_distributor.find(distributor_id)
@current_user = current_user
@current_customer = current_customer
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
if order.distributor && order.distributor != distributor
order.empty!
order.set_order_cycle! nil
end
order.distributor = distributor
end

def reset_user_and_customer
order.associate_user!(current_user) if order.user.blank? || order.email.blank?
order.__send__(:associate_customer) if order.customer.nil? # Only associates existing customers
end

def reset_order_cycle
order_cycles = Shop::OrderCyclesList.new(distributor, current_customer).call

if order.order_cycle.present? && !order_cycles.include?(order.order_cycle)
order.order_cycle = nil
order.empty!
order.save!
end

select_default_order_cycle(order, order_cycles)
end

def select_default_order_cycle(order, order_cycles)
return unless order.order_cycle.blank? && order_cycles.size == 1

order.order_cycle = order_cycles.first
end
end

0 comments on commit 90d3c8e

Please sign in to comment.