Skip to content

Commit 43f0720

Browse files
committed
Make Controller Helpers autoloadable
There's no real good reason to keep these files in lib and require them on app startup. Let's put them in app/ and let Zeitwerk handle their loading.
1 parent 06a1148 commit 43f0720

File tree

26 files changed

+596
-520
lines changed

26 files changed

+596
-520
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
3+
require 'cancan'
4+
5+
module Spree
6+
module Core
7+
module ControllerHelpers
8+
module Auth
9+
extend ActiveSupport::Concern
10+
11+
# @!attribute [rw] unauthorized_redirect
12+
# @!scope class
13+
# Extension point for overriding behaviour of access denied errors.
14+
# Default behaviour is to redirect back or to "/unauthorized" with a flash
15+
# message.
16+
# @return [Proc] action to take when access denied error is raised.
17+
18+
included do
19+
before_action :set_guest_token
20+
helper_method :spree_current_user
21+
22+
class_attribute :unauthorized_redirect
23+
deprecate :unauthorized_redirect= => "Use a custom Spree::Config.unauthorized_redirect_handler_class instead", :deprecator => Spree.deprecator
24+
25+
rescue_from CanCan::AccessDenied, with: :handle_unauthorized_access
26+
end
27+
28+
# Needs to be overriden so that we use Spree's Ability rather than anyone else's.
29+
def current_ability
30+
@current_ability ||= Spree::Ability.new(spree_current_user)
31+
end
32+
33+
def redirect_back_or_default(default)
34+
redirect_to(session["spree_user_return_to"] || default)
35+
session["spree_user_return_to"] = nil
36+
end
37+
38+
def set_guest_token
39+
unless cookies.signed[:guest_token].present?
40+
cookies.permanent.signed[:guest_token] = Spree::Config[:guest_token_cookie_options].merge(
41+
value: SecureRandom.urlsafe_base64(nil, false),
42+
httponly: true
43+
)
44+
end
45+
end
46+
47+
def store_location
48+
Spree::UserLastUrlStorer.new(self).store_location
49+
end
50+
51+
# Auth extensions are expected to define it, otherwise it's a no-op
52+
def spree_current_user
53+
defined?(super) ? super : nil
54+
end
55+
56+
def handle_unauthorized_access
57+
if unauthorized_redirect
58+
instance_exec(&unauthorized_redirect)
59+
else
60+
Spree::Config.unauthorized_redirect_handler_class.new(self).call
61+
end
62+
end
63+
end
64+
end
65+
end
66+
end
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
3+
require 'carmen'
4+
5+
module Spree
6+
module Core
7+
module ControllerHelpers
8+
module Common
9+
extend ActiveSupport::Concern
10+
included do
11+
helper_method :title
12+
helper_method :title=
13+
helper_method :accurate_title
14+
15+
layout :get_layout
16+
17+
before_action :set_user_language
18+
end
19+
20+
protected
21+
22+
# can be used in views as well as controllers.
23+
# e.g. <% self.title = 'This is a custom title for this view' %>
24+
attr_writer :title
25+
26+
def title
27+
title_string = @title.present? ? @title : accurate_title
28+
if title_string.present?
29+
if Spree::Config[:always_put_site_name_in_title]
30+
[title_string, default_title].join(' - ')
31+
else
32+
title_string
33+
end
34+
else
35+
default_title
36+
end
37+
end
38+
39+
def default_title
40+
current_store.name
41+
end
42+
43+
# this is a hook for subclasses to provide title
44+
def accurate_title
45+
current_store.seo_title
46+
end
47+
48+
private
49+
50+
def set_user_language_locale_key
51+
:locale
52+
end
53+
54+
def set_user_language
55+
available_locales = Spree.i18n_available_locales
56+
locale = [
57+
params[:locale],
58+
session[set_user_language_locale_key],
59+
(config_locale if respond_to?(:config_locale, true)),
60+
I18n.default_locale
61+
].detect do |candidate|
62+
candidate &&
63+
available_locales.include?(candidate.to_sym)
64+
end
65+
session[set_user_language_locale_key] = locale
66+
I18n.locale = locale
67+
Carmen.i18n_backend.locale = locale
68+
end
69+
70+
# Returns which layout to render.
71+
#
72+
# You can set the layout you want to render inside your Spree configuration with the +:layout+ option.
73+
#
74+
# Default layout is: +app/views/spree/layouts/spree_application+
75+
#
76+
def get_layout
77+
Spree::Config[:layout]
78+
end
79+
end
80+
end
81+
end
82+
end
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# frozen_string_literal: true
2+
3+
module Spree
4+
module Core
5+
module ControllerHelpers
6+
module Order
7+
extend ActiveSupport::Concern
8+
include ControllerHelpers::Pricing
9+
10+
included do
11+
helper_method :current_order
12+
end
13+
14+
# The current incomplete order from the guest_token for use in cart and during checkout
15+
def current_order(options = {})
16+
should_create = options[:create_order_if_necessary] || false
17+
should_build = options[:build_order_if_necessary] || should_create
18+
19+
return @current_order if @current_order
20+
21+
@current_order = find_order_by_token_or_user(lock: options[:lock])
22+
23+
if should_build && (@current_order.nil? || @current_order.completed?)
24+
@current_order = Spree::Order.new(new_order_params)
25+
@current_order.user ||= spree_current_user
26+
# See issue https://github.com/spree/spree/issues/3346 for reasons why this line is here
27+
@current_order.created_by ||= spree_current_user
28+
@current_order.save! if should_create
29+
end
30+
31+
if @current_order
32+
@current_order.record_ip_address(ip_address)
33+
return @current_order
34+
end
35+
end
36+
37+
def associate_user
38+
@order ||= current_order
39+
if spree_current_user && @order
40+
@order.associate_user!(spree_current_user) if @order.user.blank? || @order.email.blank?
41+
end
42+
end
43+
44+
def set_current_order
45+
if spree_current_user && current_order
46+
spree_current_user.orders.by_store(current_store).incomplete.where('id != ?', current_order.id).find_each do |order|
47+
current_order.merge!(order, spree_current_user)
48+
end
49+
end
50+
end
51+
52+
def ip_address
53+
request.remote_ip
54+
end
55+
56+
private
57+
58+
def last_incomplete_order
59+
@last_incomplete_order ||= spree_current_user.last_incomplete_spree_order(store: current_store)
60+
end
61+
62+
def current_order_params
63+
{ currency: current_pricing_options.currency, guest_token: cookies.signed[:guest_token], store_id: current_store.id, user_id: spree_current_user.try(:id) }
64+
end
65+
66+
def new_order_params
67+
current_order_params.merge(last_ip_address: ip_address)
68+
end
69+
70+
def find_order_by_token_or_user(options = {})
71+
should_lock = options[:lock] || false
72+
73+
# Find any incomplete orders for the guest_token
74+
order = Spree::Order.incomplete.lock(should_lock).find_by(current_order_params)
75+
76+
# Find any incomplete orders for the current user
77+
if order.nil? && spree_current_user
78+
order = last_incomplete_order
79+
end
80+
81+
order
82+
end
83+
end
84+
end
85+
end
86+
end

0 commit comments

Comments
 (0)