-
-
Notifications
You must be signed in to change notification settings - Fork 708
/
enterprises_controller.rb
84 lines (64 loc) · 2.24 KB
/
enterprises_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true
require 'open_food_network/enterprise_injection_data'
class EnterprisesController < BaseController
layout "darkswarm"
include OrderCyclesHelper
include SerializerHelper
include WhiteLabel
protect_from_forgery except: :check_permalink
# These prepended filters are in the reverse order of execution
prepend_before_action :set_order_cycles, :require_distributor_chosen, :reset_order, only: :shop
before_action :clean_permalink, only: :check_permalink
before_action :hide_ofn_navigation, only: :shop
respond_to :js, only: :permalink_checker
def shop
return redirect_to main_app.cart_path unless enough_stock?
set_noindex_meta_tag
@enterprise = current_distributor
end
def relatives
set_enterprise
respond_to do |format|
format.json do
enterprises = @enterprise&.relatives&.activated
render(json: enterprises,
each_serializer: Api::EnterpriseSerializer,
data: OpenFoodNetwork::EnterpriseInjectionData.new)
end
end
end
def check_permalink
if Enterprise.find_by permalink: params[:permalink]
render(plain: params[:permalink], status: :conflict) && return
end
begin
Rails.application.routes.recognize_path( "/#{params[:permalink]}" )
render plain: params[:permalink], status: :conflict
rescue ActionController::RoutingError
render plain: params[:permalink], status: :ok
end
end
private
def set_enterprise
@enterprise = Enterprise.find_by(id: params[:id])
end
def clean_permalink
params[:permalink] = params[:permalink].parameterize
end
def enough_stock?
current_order(true).insufficient_stock_lines.blank?
end
def reset_order
order = current_order(true)
# reset_distributor must be called before any call to current_customer or current_distributor
order_cart_reset = Orders::CartResetService.new(order, params[:id])
order_cart_reset.reset_distributor
order_cart_reset.reset_other!(spree_current_user, current_customer)
rescue ActiveRecord::RecordNotFound
flash[:error] = I18n.t(:enterprise_shop_show_error)
redirect_to shops_path
end
def set_noindex_meta_tag
@noindex_meta_tag = true unless current_distributor.visible?
end
end