Skip to content

Commit b1b8567

Browse files
authored
Merge pull request #6062 from mamhoff/autoloadable-controller-helpers
Make Controller Helpers autoloadable
2 parents c976eac + 0ef2e29 commit b1b8567

27 files changed

+598
-545
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ Layout/LeadingEmptyLines:
143143
# SupportedStyles: aligned, indented
144144
Layout/MultilineOperationIndentation:
145145
Exclude:
146-
- "core/lib/spree/core/controller_helpers/strong_parameters.rb"
147146
- "core/lib/spree/core/engine.rb"
148147
- "core/lib/spree/core/importer/order.rb"
149148
- "core/lib/spree/permission_sets/default_customer.rb"
@@ -226,12 +225,6 @@ Lint/EmptyConditionalBody:
226225
Exclude:
227226
- "core/lib/spree/preferences/statically_configurable.rb"
228227

229-
# Offense count: 1
230-
# This cop supports safe autocorrection (--autocorrect).
231-
Lint/ParenthesesAsGroupedExpression:
232-
Exclude:
233-
- "core/spec/lib/spree/core/controller_helpers/auth_spec.rb"
234-
235228
# Offense count: 1
236229
# This cop supports safe autocorrection (--autocorrect).
237230
Lint/RedundantCopDisableDirective:
@@ -325,7 +318,6 @@ Rails/Blank:
325318
- "core/app/models/spree/reimbursement_type/exchange.rb"
326319
- "core/app/models/spree/wallet_payment_source.rb"
327320
- "core/app/models/spree/zone.rb"
328-
- "core/lib/spree/core/controller_helpers/auth.rb"
329321
- "core/lib/spree/core/importer/order.rb"
330322
- "core/lib/spree/core/search/base.rb"
331323

@@ -409,12 +401,6 @@ Rails/OutputSafety:
409401
- "core/app/helpers/spree/products_helper.rb"
410402
- "core/app/models/spree/money.rb"
411403

412-
# Offense count: 1
413-
# This cop supports safe autocorrection (--autocorrect).
414-
Rails/Presence:
415-
Exclude:
416-
- "core/lib/spree/core/controller_helpers/common.rb"
417-
418404
# Offense count: 10
419405
# This cop supports safe autocorrection (--autocorrect).
420406
# Configuration parameters: NotNilAndNotEmpty, NotBlank, UnlessBlank.
@@ -471,7 +457,6 @@ Rails/SafeNavigation:
471457
- "core/app/models/spree/variant/pricing_options.rb"
472458
- "core/app/models/spree/wallet.rb"
473459
- "core/app/models/spree/wallet/default_payment_builder.rb"
474-
- "core/spec/lib/spree/core/controller_helpers/order_spec.rb"
475460
- "core/spec/models/spree/variant/vat_price_generator_spec.rb"
476461

477462
# Offense count: 43
@@ -691,7 +676,6 @@ Style/RedundantRegexpEscape:
691676
# Configuration parameters: AllowMultipleReturnValues.
692677
Style/RedundantReturn:
693678
Exclude:
694-
- "core/lib/spree/core/controller_helpers/order.rb"
695679
- "core/lib/spree/preferences/store.rb"
696680

697681
# Offense count: 2
@@ -713,13 +697,6 @@ Style/RedundantSort:
713697
Style/SafeNavigation:
714698
Enabled: false
715699

716-
# Offense count: 2
717-
# This cop supports safe autocorrection (--autocorrect).
718-
# Configuration parameters: AllowModifier.
719-
Style/SoleNestedConditional:
720-
Exclude:
721-
- "core/lib/spree/core/controller_helpers/order.rb"
722-
723700
# Offense count: 13
724701
# This cop supports unsafe autocorrection (--autocorrect-all).
725702
# Configuration parameters: Mode.
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+
if cookies.signed[:guest_token].blank?
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.presence || 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+
@current_order
34+
end
35+
end
36+
37+
def associate_user
38+
@order ||= current_order
39+
if spree_current_user && @order && (@order.user.blank? || @order.email.blank?)
40+
@order.associate_user!(spree_current_user)
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)