diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b4ef66b1821..d7ec9d36c23 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -143,7 +143,6 @@ Layout/LeadingEmptyLines: # SupportedStyles: aligned, indented Layout/MultilineOperationIndentation: Exclude: - - "core/lib/spree/core/controller_helpers/strong_parameters.rb" - "core/lib/spree/core/engine.rb" - "core/lib/spree/core/importer/order.rb" - "core/lib/spree/permission_sets/default_customer.rb" @@ -232,12 +231,6 @@ Lint/EmptyConditionalBody: Exclude: - "core/lib/spree/preferences/statically_configurable.rb" -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Lint/ParenthesesAsGroupedExpression: - Exclude: - - "core/spec/lib/spree/core/controller_helpers/auth_spec.rb" - # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). Lint/RedundantCopDisableDirective: @@ -331,7 +324,6 @@ Rails/Blank: - "core/app/models/spree/reimbursement_type/exchange.rb" - "core/app/models/spree/wallet_payment_source.rb" - "core/app/models/spree/zone.rb" - - "core/lib/spree/core/controller_helpers/auth.rb" - "core/lib/spree/core/importer/order.rb" - "core/lib/spree/core/search/base.rb" @@ -415,12 +407,6 @@ Rails/OutputSafety: - "core/app/helpers/spree/products_helper.rb" - "core/lib/spree/money.rb" -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Rails/Presence: - Exclude: - - "core/lib/spree/core/controller_helpers/common.rb" - # Offense count: 10 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: NotNilAndNotEmpty, NotBlank, UnlessBlank. @@ -477,7 +463,6 @@ Rails/SafeNavigation: - "core/app/models/spree/variant/pricing_options.rb" - "core/app/models/spree/wallet.rb" - "core/app/models/spree/wallet/default_payment_builder.rb" - - "core/spec/lib/spree/core/controller_helpers/order_spec.rb" - "core/spec/models/spree/variant/vat_price_generator_spec.rb" # Offense count: 43 @@ -698,7 +683,6 @@ Style/RedundantRegexpEscape: # Configuration parameters: AllowMultipleReturnValues. Style/RedundantReturn: Exclude: - - "core/lib/spree/core/controller_helpers/order.rb" - "core/lib/spree/preferences/store.rb" # Offense count: 2 @@ -720,13 +704,6 @@ Style/RedundantSort: Style/SafeNavigation: Enabled: false -# Offense count: 2 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowModifier. -Style/SoleNestedConditional: - Exclude: - - "core/lib/spree/core/controller_helpers/order.rb" - # Offense count: 13 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: Mode. diff --git a/core/app/helpers/spree/core/controller_helpers/auth.rb b/core/app/helpers/spree/core/controller_helpers/auth.rb new file mode 100644 index 00000000000..60269fe4af6 --- /dev/null +++ b/core/app/helpers/spree/core/controller_helpers/auth.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +require 'cancan' + +module Spree + module Core + module ControllerHelpers + module Auth + extend ActiveSupport::Concern + + # @!attribute [rw] unauthorized_redirect + # @!scope class + # Extension point for overriding behaviour of access denied errors. + # Default behaviour is to redirect back or to "/unauthorized" with a flash + # message. + # @return [Proc] action to take when access denied error is raised. + + included do + before_action :set_guest_token + helper_method :spree_current_user + + class_attribute :unauthorized_redirect + self.unauthorized_redirect = -> do + flash[:error] = I18n.t('spree.authorization_failure') + redirect_back(fallback_location: "/unauthorized") + end + + rescue_from CanCan::AccessDenied do + instance_exec(&unauthorized_redirect) + end + end + + # Needs to be overriden so that we use Spree's Ability rather than anyone else's. + def current_ability + @current_ability ||= Spree::Ability.new(spree_current_user) + end + + def redirect_back_or_default(default) + redirect_to(session["spree_user_return_to"] || default) + session["spree_user_return_to"] = nil + end + + def set_guest_token + if cookies.signed[:guest_token].blank? + cookies.permanent.signed[:guest_token] = Spree::Config[:guest_token_cookie_options].merge( + value: SecureRandom.urlsafe_base64(nil, false), + httponly: true + ) + end + end + + def store_location + Spree::UserLastUrlStorer.new(self).store_location + end + + # Auth extensions are expected to define it, otherwise it's a no-op + def spree_current_user + defined?(super) ? super : nil + end + end + end + end +end diff --git a/core/app/helpers/spree/core/controller_helpers/common.rb b/core/app/helpers/spree/core/controller_helpers/common.rb new file mode 100644 index 00000000000..10d8a6cbfc3 --- /dev/null +++ b/core/app/helpers/spree/core/controller_helpers/common.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +require 'carmen' + +module Spree + module Core + module ControllerHelpers + module Common + extend ActiveSupport::Concern + included do + helper_method :title + helper_method :title= + helper_method :accurate_title + + layout :get_layout + + before_action :set_user_language + end + + protected + + # can be used in views as well as controllers. + # e.g. <% self.title = 'This is a custom title for this view' %> + attr_writer :title + + def title + title_string = @title.presence || accurate_title + if title_string.present? + if Spree::Config[:always_put_site_name_in_title] + [title_string, default_title].join(' - ') + else + title_string + end + else + default_title + end + end + + def default_title + current_store.name + end + + # this is a hook for subclasses to provide title + def accurate_title + current_store.seo_title + end + + private + + def set_user_language_locale_key + :locale + end + + def set_user_language + available_locales = Spree.i18n_available_locales + locale = [ + params[:locale], + session[set_user_language_locale_key], + (config_locale if respond_to?(:config_locale, true)), + I18n.default_locale + ].detect do |candidate| + candidate && + available_locales.include?(candidate.to_sym) + end + session[set_user_language_locale_key] = locale + I18n.locale = locale + Carmen.i18n_backend.locale = locale + end + + # Returns which layout to render. + # + # You can set the layout you want to render inside your Spree configuration with the +:layout+ option. + # + # Default layout is: +app/views/spree/layouts/spree_application+ + # + def get_layout + Spree::Config[:layout] + end + end + end + end +end diff --git a/core/app/helpers/spree/core/controller_helpers/order.rb b/core/app/helpers/spree/core/controller_helpers/order.rb new file mode 100644 index 00000000000..a63b1e7b8e7 --- /dev/null +++ b/core/app/helpers/spree/core/controller_helpers/order.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +module Spree + module Core + module ControllerHelpers + module Order + extend ActiveSupport::Concern + include ControllerHelpers::Pricing + + included do + helper_method :current_order + end + + # The current incomplete order from the guest_token for use in cart and during checkout + def current_order(options = {}) + should_create = options[:create_order_if_necessary] || false + should_build = options[:build_order_if_necessary] || should_create + + return @current_order if @current_order + + @current_order = find_order_by_token_or_user(lock: options[:lock]) + + if should_build && (@current_order.nil? || @current_order.completed?) + @current_order = Spree::Order.new(new_order_params) + @current_order.user ||= spree_current_user + # See issue https://github.com/spree/spree/issues/3346 for reasons why this line is here + @current_order.created_by ||= spree_current_user + @current_order.save! if should_create + end + + if @current_order + @current_order.record_ip_address(ip_address) + @current_order + end + end + + def associate_user + @order ||= current_order + if spree_current_user && @order && (@order.user.blank? || @order.email.blank?) + @order.associate_user!(spree_current_user) + end + end + + def set_current_order + if spree_current_user && current_order + spree_current_user.orders.by_store(current_store).incomplete.where('id != ?', current_order.id).find_each do |order| + current_order.merge!(order, spree_current_user) + end + end + end + + def ip_address + request.remote_ip + end + + private + + def last_incomplete_order + @last_incomplete_order ||= spree_current_user.last_incomplete_spree_order(store: current_store) + end + + def current_order_params + { currency: current_pricing_options.currency, guest_token: cookies.signed[:guest_token], store_id: current_store.id, user_id: spree_current_user.try(:id) } + end + + def new_order_params + current_order_params.merge(last_ip_address: ip_address) + end + + def find_order_by_token_or_user(options = {}) + should_lock = options[:lock] || false + + # Find any incomplete orders for the guest_token + order = Spree::Order.incomplete.lock(should_lock).find_by(current_order_params) + + # Find any incomplete orders for the current user + if order.nil? && spree_current_user + order = last_incomplete_order + end + + order + end + end + end + end +end diff --git a/core/app/helpers/spree/core/controller_helpers/payment_parameters.rb b/core/app/helpers/spree/core/controller_helpers/payment_parameters.rb new file mode 100644 index 00000000000..dc106e80349 --- /dev/null +++ b/core/app/helpers/spree/core/controller_helpers/payment_parameters.rb @@ -0,0 +1,165 @@ +# frozen_string_literal: true + +module Spree + module Core::ControllerHelpers::PaymentParameters + # This method handles the awkwardness of how the html forms are currently + # set up for frontend. + # + # This method expects a params hash in the format of: + # + # { + # payment_source: { + # # The keys here are spree_payment_method.id's + # '1' => {...source attributes for payment method 1...}, + # '2' => {...source attributes for payment method 2...}, + # }, + # order: { + # # Note that only a single entry is expected/handled in this array + # payments_attributes: [ + # { + # payment_method_id: '1', + # }, + # ], + # ...other params... + # }, + # ...other params... + # } + # + # And this method modifies the params into the format of: + # + # { + # order: { + # payments_attributes: [ + # { + # payment_method_id: '1', + # source_attributes: {...source attributes for payment method 1...} + # }, + # ], + # ...other params... + # }, + # ...other params... + # } + # + def move_payment_source_into_payments_attributes(params) + # Step 1: Gather all the information and ensure all the pieces are there. + + return params if params[:payment_source].blank? + + payment_params = params[:order] && + params[:order][:payments_attributes] && + params[:order][:payments_attributes].first + return params if payment_params.blank? + + payment_method_id = payment_params[:payment_method_id] + return params if payment_method_id.blank? + + source_params = params[:payment_source][payment_method_id] + return params if source_params.blank? + + # Step 2: Perform the modifications. + + payment_params[:source_attributes] = source_params + params.delete(:payment_source) + + params + end + + # This method handles the awkwardness of how the html forms are currently + # set up for frontend. + # + # This method expects a params hash in the format of: + # + # { + # order: { + # wallet_payment_source_id: '123', + # ...other params... + # }, + # cvc_confirm: '456', # optional + # ...other params... + # } + # + # And this method modifies the params into the format of: + # + # { + # order: { + # payments_attributes: [ + # { + # source_attributes: { + # wallet_payment_source_id: '123', + # verification_value: '456', + # }, + # }, + # ] + # ...other params... + # }, + # ...other params... + # } + # + def move_wallet_payment_source_id_into_payments_attributes(params) + return params if params[:order].blank? + + wallet_payment_source_id = params[:order][:wallet_payment_source_id].presence + cvc_confirm = params[:cvc_confirm].presence + + return params if wallet_payment_source_id.nil? + + params[:order][:payments_attributes] = [ + { + source_attributes: { + wallet_payment_source_id:, + verification_value: cvc_confirm + } + } + ] + + params[:order].delete(:wallet_payment_source_id) + params.delete(:cvc_confirm) + + params + end + + # This is a strange thing to do since an order can have multiple payments + # but we always assume that it only has a single payment and that its + # amount should be the current order total after store credit is applied. + # We should reconsider this method and its usage at some point. + # + # This method expects a params hash in the format of: + # + # { + # order: { + # # Note that only a single entry is expected/handled in this array + # payments_attributes: [ + # { + # ...params... + # }, + # ], + # ...other params... + # }, + # ...other params... + # } + # + # And this method modifies the params into the format of: + # + # { + # order: { + # payments_attributes: [ + # { + # ...params... + # amount: , + # }, + # ], + # ...other params... + # }, + # ...other params... + # } + # + def set_payment_parameters_amount(params, order) + return params if params[:order].blank? + return params if params[:order][:payments_attributes].blank? + + params[:order][:payments_attributes].first[:amount] = order.order_total_after_store_credit + + params + end + end +end diff --git a/core/app/helpers/spree/core/controller_helpers/pricing.rb b/core/app/helpers/spree/core/controller_helpers/pricing.rb new file mode 100644 index 00000000000..85d6ed1c6c0 --- /dev/null +++ b/core/app/helpers/spree/core/controller_helpers/pricing.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Spree + module Core + module ControllerHelpers + module Pricing + extend ActiveSupport::Concern + + included do + helper_method :current_pricing_options + end + + def current_pricing_options + Spree::Config.pricing_options_class.from_context(self) + end + end + end + end +end diff --git a/core/app/helpers/spree/core/controller_helpers/search.rb b/core/app/helpers/spree/core/controller_helpers/search.rb new file mode 100644 index 00000000000..7b31b173148 --- /dev/null +++ b/core/app/helpers/spree/core/controller_helpers/search.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Spree + module Core + module ControllerHelpers + module Search + def build_searcher(params) + Spree::Config.searcher_class.new(params).tap do |searcher| + searcher.current_user = spree_current_user + searcher.pricing_options = current_pricing_options + end + end + end + end + end +end diff --git a/core/app/helpers/spree/core/controller_helpers/store.rb b/core/app/helpers/spree/core/controller_helpers/store.rb new file mode 100644 index 00000000000..598e0461490 --- /dev/null +++ b/core/app/helpers/spree/core/controller_helpers/store.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Spree + module Core + module ControllerHelpers + module Store + extend ActiveSupport::Concern + + included do + helper_method :current_store + end + + def current_store + @current_store ||= Spree::Config.current_store_selector_class.new(request).store + end + end + end + end +end diff --git a/core/app/helpers/spree/core/controller_helpers/strong_parameters.rb b/core/app/helpers/spree/core/controller_helpers/strong_parameters.rb new file mode 100644 index 00000000000..fa9568c2dec --- /dev/null +++ b/core/app/helpers/spree/core/controller_helpers/strong_parameters.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +module Spree + module Core + module ControllerHelpers + module StrongParameters + def permitted_attributes + Spree::PermittedAttributes + end + + delegate(*Spree::PermittedAttributes::ATTRIBUTES, + to: :permitted_attributes, + prefix: :permitted) + + def permitted_credit_card_update_attributes + permitted_attributes.credit_card_update_attributes + [ + address_attributes: permitted_address_attributes + ] + end + + def permitted_payment_attributes + permitted_attributes.payment_attributes + [ + source_attributes: permitted_source_attributes + ] + end + + def permitted_source_attributes + permitted_attributes.source_attributes + [ + address_attributes: permitted_address_attributes + ] + end + + def permitted_checkout_address_attributes + permitted_attributes.checkout_address_attributes + end + + def permitted_checkout_delivery_attributes + permitted_attributes.checkout_delivery_attributes + end + + def permitted_checkout_payment_attributes + permitted_attributes.checkout_payment_attributes + end + + def permitted_checkout_confirm_attributes + permitted_attributes.checkout_confirm_attributes + end + + def permitted_order_attributes + permitted_checkout_address_attributes + + permitted_checkout_delivery_attributes + + permitted_checkout_payment_attributes + + permitted_checkout_confirm_attributes + [ + line_items_attributes: permitted_line_item_attributes + ] + end + + def permitted_product_attributes + permitted_attributes.product_attributes + [ + product_properties_attributes: permitted_product_properties_attributes + ] + end + + def permitted_user_attributes + permitted_attributes.user_attributes + [ + bill_address_attributes: permitted_address_attributes, + ship_address_attributes: permitted_address_attributes + ] + end + end + end + end +end diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb index e5c3fe42794..765ef833a50 100644 --- a/core/lib/spree/core.rb +++ b/core/lib/spree/core.rb @@ -111,14 +111,6 @@ class GatewayError < RuntimeError; end require 'spree/core/importer' require 'spree/core/permalinks' require 'spree/core/product_duplicator' -require 'spree/core/controller_helpers/auth' -require 'spree/core/controller_helpers/common' -require 'spree/core/controller_helpers/order' -require 'spree/core/controller_helpers/payment_parameters' -require 'spree/core/controller_helpers/pricing' -require 'spree/core/controller_helpers/search' -require 'spree/core/controller_helpers/store' -require 'spree/core/controller_helpers/strong_parameters' require 'spree/core/role_configuration' require 'spree/core/state_machines' require 'spree/core/stock_configuration' diff --git a/core/lib/spree/core/controller_helpers/auth.rb b/core/lib/spree/core/controller_helpers/auth.rb index d424e0bc5fc..72f6898b795 100644 --- a/core/lib/spree/core/controller_helpers/auth.rb +++ b/core/lib/spree/core/controller_helpers/auth.rb @@ -1,63 +1,7 @@ # frozen_string_literal: true -require 'cancan' - -module Spree - module Core - module ControllerHelpers - module Auth - extend ActiveSupport::Concern - - # @!attribute [rw] unauthorized_redirect - # @!scope class - # Extension point for overriding behaviour of access denied errors. - # Default behaviour is to redirect back or to "/unauthorized" with a flash - # message. - # @return [Proc] action to take when access denied error is raised. - - included do - before_action :set_guest_token - helper_method :spree_current_user - - class_attribute :unauthorized_redirect - self.unauthorized_redirect = -> do - flash[:error] = I18n.t('spree.authorization_failure') - redirect_back(fallback_location: "/unauthorized") - end - - rescue_from CanCan::AccessDenied do - instance_exec(&unauthorized_redirect) - end - end - - # Needs to be overriden so that we use Spree's Ability rather than anyone else's. - def current_ability - @current_ability ||= Spree::Ability.new(spree_current_user) - end - - def redirect_back_or_default(default) - redirect_to(session["spree_user_return_to"] || default) - session["spree_user_return_to"] = nil - end - - def set_guest_token - unless cookies.signed[:guest_token].present? - cookies.permanent.signed[:guest_token] = Spree::Config[:guest_token_cookie_options].merge( - value: SecureRandom.urlsafe_base64(nil, false), - httponly: true - ) - end - end - - def store_location - Spree::UserLastUrlStorer.new(self).store_location - end - - # Auth extensions are expected to define it, otherwise it's a no-op - def spree_current_user - defined?(super) ? super : nil - end - end - end - end -end +Spree.deprecator.warn( + <<~MSG + The file "#{__FILE__}" does not need to be `require`d any longer, it is now autoloaded. + MSG +) diff --git a/core/lib/spree/core/controller_helpers/common.rb b/core/lib/spree/core/controller_helpers/common.rb index 606a3aed76c..72f6898b795 100644 --- a/core/lib/spree/core/controller_helpers/common.rb +++ b/core/lib/spree/core/controller_helpers/common.rb @@ -1,82 +1,7 @@ # frozen_string_literal: true -require 'carmen' - -module Spree - module Core - module ControllerHelpers - module Common - extend ActiveSupport::Concern - included do - helper_method :title - helper_method :title= - helper_method :accurate_title - - layout :get_layout - - before_action :set_user_language - end - - protected - - # can be used in views as well as controllers. - # e.g. <% self.title = 'This is a custom title for this view' %> - attr_writer :title - - def title - title_string = @title.present? ? @title : accurate_title - if title_string.present? - if Spree::Config[:always_put_site_name_in_title] - [title_string, default_title].join(' - ') - else - title_string - end - else - default_title - end - end - - def default_title - current_store.name - end - - # this is a hook for subclasses to provide title - def accurate_title - current_store.seo_title - end - - private - - def set_user_language_locale_key - :locale - end - - def set_user_language - available_locales = Spree.i18n_available_locales - locale = [ - params[:locale], - session[set_user_language_locale_key], - (config_locale if respond_to?(:config_locale, true)), - I18n.default_locale - ].detect do |candidate| - candidate && - available_locales.include?(candidate.to_sym) - end - session[set_user_language_locale_key] = locale - I18n.locale = locale - Carmen.i18n_backend.locale = locale - end - - # Returns which layout to render. - # - # You can set the layout you want to render inside your Spree configuration with the +:layout+ option. - # - # Default layout is: +app/views/spree/layouts/spree_application+ - # - def get_layout - Spree::Config[:layout] - end - end - end - end -end +Spree.deprecator.warn( + <<~MSG + The file "#{__FILE__}" does not need to be `require`d any longer, it is now autoloaded. + MSG +) diff --git a/core/lib/spree/core/controller_helpers/order.rb b/core/lib/spree/core/controller_helpers/order.rb index e3bb2b61c69..72f6898b795 100644 --- a/core/lib/spree/core/controller_helpers/order.rb +++ b/core/lib/spree/core/controller_helpers/order.rb @@ -1,88 +1,7 @@ # frozen_string_literal: true -require 'spree/core/controller_helpers/pricing' - -module Spree - module Core - module ControllerHelpers - module Order - extend ActiveSupport::Concern - include ControllerHelpers::Pricing - - included do - helper_method :current_order - end - - # The current incomplete order from the guest_token for use in cart and during checkout - def current_order(options = {}) - should_create = options[:create_order_if_necessary] || false - should_build = options[:build_order_if_necessary] || should_create - - return @current_order if @current_order - - @current_order = find_order_by_token_or_user(lock: options[:lock]) - - if should_build && (@current_order.nil? || @current_order.completed?) - @current_order = Spree::Order.new(new_order_params) - @current_order.user ||= spree_current_user - # See issue https://github.com/spree/spree/issues/3346 for reasons why this line is here - @current_order.created_by ||= spree_current_user - @current_order.save! if should_create - end - - if @current_order - @current_order.record_ip_address(ip_address) - return @current_order - end - end - - def associate_user - @order ||= current_order - if spree_current_user && @order - @order.associate_user!(spree_current_user) if @order.user.blank? || @order.email.blank? - end - end - - def set_current_order - if spree_current_user && current_order - spree_current_user.orders.by_store(current_store).incomplete.where('id != ?', current_order.id).find_each do |order| - current_order.merge!(order, spree_current_user) - end - end - end - - def ip_address - request.remote_ip - end - - private - - def last_incomplete_order - @last_incomplete_order ||= spree_current_user.last_incomplete_spree_order(store: current_store) - end - - def current_order_params - { currency: current_pricing_options.currency, guest_token: cookies.signed[:guest_token], store_id: current_store.id, user_id: spree_current_user.try(:id) } - end - - def new_order_params - current_order_params.merge(last_ip_address: ip_address) - end - - def find_order_by_token_or_user(options = {}) - should_lock = options[:lock] || false - - # Find any incomplete orders for the guest_token - order = Spree::Order.incomplete.lock(should_lock).find_by(current_order_params) - - # Find any incomplete orders for the current user - if order.nil? && spree_current_user - order = last_incomplete_order - end - - order - end - end - end - end -end +Spree.deprecator.warn( + <<~MSG + The file "#{__FILE__}" does not need to be `require`d any longer, it is now autoloaded. + MSG +) diff --git a/core/lib/spree/core/controller_helpers/payment_parameters.rb b/core/lib/spree/core/controller_helpers/payment_parameters.rb index dc106e80349..72f6898b795 100644 --- a/core/lib/spree/core/controller_helpers/payment_parameters.rb +++ b/core/lib/spree/core/controller_helpers/payment_parameters.rb @@ -1,165 +1,7 @@ # frozen_string_literal: true -module Spree - module Core::ControllerHelpers::PaymentParameters - # This method handles the awkwardness of how the html forms are currently - # set up for frontend. - # - # This method expects a params hash in the format of: - # - # { - # payment_source: { - # # The keys here are spree_payment_method.id's - # '1' => {...source attributes for payment method 1...}, - # '2' => {...source attributes for payment method 2...}, - # }, - # order: { - # # Note that only a single entry is expected/handled in this array - # payments_attributes: [ - # { - # payment_method_id: '1', - # }, - # ], - # ...other params... - # }, - # ...other params... - # } - # - # And this method modifies the params into the format of: - # - # { - # order: { - # payments_attributes: [ - # { - # payment_method_id: '1', - # source_attributes: {...source attributes for payment method 1...} - # }, - # ], - # ...other params... - # }, - # ...other params... - # } - # - def move_payment_source_into_payments_attributes(params) - # Step 1: Gather all the information and ensure all the pieces are there. - - return params if params[:payment_source].blank? - - payment_params = params[:order] && - params[:order][:payments_attributes] && - params[:order][:payments_attributes].first - return params if payment_params.blank? - - payment_method_id = payment_params[:payment_method_id] - return params if payment_method_id.blank? - - source_params = params[:payment_source][payment_method_id] - return params if source_params.blank? - - # Step 2: Perform the modifications. - - payment_params[:source_attributes] = source_params - params.delete(:payment_source) - - params - end - - # This method handles the awkwardness of how the html forms are currently - # set up for frontend. - # - # This method expects a params hash in the format of: - # - # { - # order: { - # wallet_payment_source_id: '123', - # ...other params... - # }, - # cvc_confirm: '456', # optional - # ...other params... - # } - # - # And this method modifies the params into the format of: - # - # { - # order: { - # payments_attributes: [ - # { - # source_attributes: { - # wallet_payment_source_id: '123', - # verification_value: '456', - # }, - # }, - # ] - # ...other params... - # }, - # ...other params... - # } - # - def move_wallet_payment_source_id_into_payments_attributes(params) - return params if params[:order].blank? - - wallet_payment_source_id = params[:order][:wallet_payment_source_id].presence - cvc_confirm = params[:cvc_confirm].presence - - return params if wallet_payment_source_id.nil? - - params[:order][:payments_attributes] = [ - { - source_attributes: { - wallet_payment_source_id:, - verification_value: cvc_confirm - } - } - ] - - params[:order].delete(:wallet_payment_source_id) - params.delete(:cvc_confirm) - - params - end - - # This is a strange thing to do since an order can have multiple payments - # but we always assume that it only has a single payment and that its - # amount should be the current order total after store credit is applied. - # We should reconsider this method and its usage at some point. - # - # This method expects a params hash in the format of: - # - # { - # order: { - # # Note that only a single entry is expected/handled in this array - # payments_attributes: [ - # { - # ...params... - # }, - # ], - # ...other params... - # }, - # ...other params... - # } - # - # And this method modifies the params into the format of: - # - # { - # order: { - # payments_attributes: [ - # { - # ...params... - # amount: , - # }, - # ], - # ...other params... - # }, - # ...other params... - # } - # - def set_payment_parameters_amount(params, order) - return params if params[:order].blank? - return params if params[:order][:payments_attributes].blank? - - params[:order][:payments_attributes].first[:amount] = order.order_total_after_store_credit - - params - end - end -end +Spree.deprecator.warn( + <<~MSG + The file "#{__FILE__}" does not need to be `require`d any longer, it is now autoloaded. + MSG +) diff --git a/core/lib/spree/core/controller_helpers/pricing.rb b/core/lib/spree/core/controller_helpers/pricing.rb index 85d6ed1c6c0..72f6898b795 100644 --- a/core/lib/spree/core/controller_helpers/pricing.rb +++ b/core/lib/spree/core/controller_helpers/pricing.rb @@ -1,19 +1,7 @@ # frozen_string_literal: true -module Spree - module Core - module ControllerHelpers - module Pricing - extend ActiveSupport::Concern - - included do - helper_method :current_pricing_options - end - - def current_pricing_options - Spree::Config.pricing_options_class.from_context(self) - end - end - end - end -end +Spree.deprecator.warn( + <<~MSG + The file "#{__FILE__}" does not need to be `require`d any longer, it is now autoloaded. + MSG +) diff --git a/core/lib/spree/core/controller_helpers/search.rb b/core/lib/spree/core/controller_helpers/search.rb index 7b31b173148..72f6898b795 100644 --- a/core/lib/spree/core/controller_helpers/search.rb +++ b/core/lib/spree/core/controller_helpers/search.rb @@ -1,16 +1,7 @@ # frozen_string_literal: true -module Spree - module Core - module ControllerHelpers - module Search - def build_searcher(params) - Spree::Config.searcher_class.new(params).tap do |searcher| - searcher.current_user = spree_current_user - searcher.pricing_options = current_pricing_options - end - end - end - end - end -end +Spree.deprecator.warn( + <<~MSG + The file "#{__FILE__}" does not need to be `require`d any longer, it is now autoloaded. + MSG +) diff --git a/core/lib/spree/core/controller_helpers/store.rb b/core/lib/spree/core/controller_helpers/store.rb index 598e0461490..72f6898b795 100644 --- a/core/lib/spree/core/controller_helpers/store.rb +++ b/core/lib/spree/core/controller_helpers/store.rb @@ -1,19 +1,7 @@ # frozen_string_literal: true -module Spree - module Core - module ControllerHelpers - module Store - extend ActiveSupport::Concern - - included do - helper_method :current_store - end - - def current_store - @current_store ||= Spree::Config.current_store_selector_class.new(request).store - end - end - end - end -end +Spree.deprecator.warn( + <<~MSG + The file "#{__FILE__}" does not need to be `require`d any longer, it is now autoloaded. + MSG +) diff --git a/core/lib/spree/core/controller_helpers/strong_parameters.rb b/core/lib/spree/core/controller_helpers/strong_parameters.rb index 01e23d9dcae..72f6898b795 100644 --- a/core/lib/spree/core/controller_helpers/strong_parameters.rb +++ b/core/lib/spree/core/controller_helpers/strong_parameters.rb @@ -1,73 +1,7 @@ # frozen_string_literal: true -module Spree - module Core - module ControllerHelpers - module StrongParameters - def permitted_attributes - Spree::PermittedAttributes - end - - delegate(*Spree::PermittedAttributes::ATTRIBUTES, - to: :permitted_attributes, - prefix: :permitted) - - def permitted_credit_card_update_attributes - permitted_attributes.credit_card_update_attributes + [ - address_attributes: permitted_address_attributes - ] - end - - def permitted_payment_attributes - permitted_attributes.payment_attributes + [ - source_attributes: permitted_source_attributes - ] - end - - def permitted_source_attributes - permitted_attributes.source_attributes + [ - address_attributes: permitted_address_attributes - ] - end - - def permitted_checkout_address_attributes - permitted_attributes.checkout_address_attributes - end - - def permitted_checkout_delivery_attributes - permitted_attributes.checkout_delivery_attributes - end - - def permitted_checkout_payment_attributes - permitted_attributes.checkout_payment_attributes - end - - def permitted_checkout_confirm_attributes - permitted_attributes.checkout_confirm_attributes - end - - def permitted_order_attributes - permitted_checkout_address_attributes + - permitted_checkout_delivery_attributes + - permitted_checkout_payment_attributes + - permitted_checkout_confirm_attributes + [ - line_items_attributes: permitted_line_item_attributes - ] - end - - def permitted_product_attributes - permitted_attributes.product_attributes + [ - product_properties_attributes: permitted_product_properties_attributes - ] - end - - def permitted_user_attributes - permitted_attributes.user_attributes + [ - bill_address_attributes: permitted_address_attributes, - ship_address_attributes: permitted_address_attributes - ] - end - end - end - end -end +Spree.deprecator.warn( + <<~MSG + The file "#{__FILE__}" does not need to be `require`d any longer, it is now autoloaded. + MSG +) diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb index b6aa20b36cd..9f37c1c65d5 100644 --- a/core/lib/spree/core/engine.rb +++ b/core/lib/spree/core/engine.rb @@ -21,6 +21,11 @@ class Engine < ::Rails::Engine [Symbol, BigDecimal, ActiveSupport::HashWithIndifferentAccess] end + initializer "spree.zeitwerk_ignores" do + old_helpers = Engine.root.join("lib", "spree", "core", "controller_helpers", "*", "*.rb") + Rails.application.autoloaders.main.ignore(old_helpers) + end + initializer "spree.environment", before: :load_config_initializers do |app| app.config.spree = Spree::Config.environment end diff --git a/core/spec/lib/spree/core/controller_helpers/auth_spec.rb b/core/spec/helpers/controller_helpers/auth_spec.rb similarity index 98% rename from core/spec/lib/spree/core/controller_helpers/auth_spec.rb rename to core/spec/helpers/controller_helpers/auth_spec.rb index d0acf6b3ed8..fffd97d54bc 100644 --- a/core/spec/lib/spree/core/controller_helpers/auth_spec.rb +++ b/core/spec/helpers/controller_helpers/auth_spec.rb @@ -100,7 +100,7 @@ def controller.index context "when an ancestor defines it" do it "delegates" do controller = Class.new(ApplicationController) do - include (Module.new do + include(Module.new do def spree_current_user :user end diff --git a/core/spec/lib/spree/core/controller_helpers/order_spec.rb b/core/spec/helpers/controller_helpers/order_spec.rb similarity index 98% rename from core/spec/lib/spree/core/controller_helpers/order_spec.rb rename to core/spec/helpers/controller_helpers/order_spec.rb index f6ec2ce1ee8..080104381a1 100644 --- a/core/spec/lib/spree/core/controller_helpers/order_spec.rb +++ b/core/spec/helpers/controller_helpers/order_spec.rb @@ -46,7 +46,7 @@ expect { subject }.to change { - Spree::Order.last.try!(:last_ip_address) + Spree::Order.last&.last_ip_address }.from(nil).to("0.0.0.0") end end diff --git a/core/spec/lib/spree/core/controller_helpers/payment_parameters_spec.rb b/core/spec/helpers/controller_helpers/payment_parameters_spec.rb similarity index 100% rename from core/spec/lib/spree/core/controller_helpers/payment_parameters_spec.rb rename to core/spec/helpers/controller_helpers/payment_parameters_spec.rb diff --git a/core/spec/lib/spree/core/controller_helpers/pricing_spec.rb b/core/spec/helpers/controller_helpers/pricing_spec.rb similarity index 100% rename from core/spec/lib/spree/core/controller_helpers/pricing_spec.rb rename to core/spec/helpers/controller_helpers/pricing_spec.rb diff --git a/core/spec/lib/spree/core/controller_helpers/search_spec.rb b/core/spec/helpers/controller_helpers/search_spec.rb similarity index 100% rename from core/spec/lib/spree/core/controller_helpers/search_spec.rb rename to core/spec/helpers/controller_helpers/search_spec.rb diff --git a/core/spec/lib/spree/core/controller_helpers/store_spec.rb b/core/spec/helpers/controller_helpers/store_spec.rb similarity index 100% rename from core/spec/lib/spree/core/controller_helpers/store_spec.rb rename to core/spec/helpers/controller_helpers/store_spec.rb diff --git a/core/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb b/core/spec/helpers/controller_helpers/strong_parameters_spec.rb similarity index 100% rename from core/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb rename to core/spec/helpers/controller_helpers/strong_parameters_spec.rb diff --git a/core/spec/lib/spree/core/controller_helpers_spec.rb b/core/spec/lib/spree/core/controller_helpers_spec.rb new file mode 100644 index 00000000000..13631bb429b --- /dev/null +++ b/core/spec/lib/spree/core/controller_helpers_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe "require 'spree/core/controller_helpers" do + %w( + auth + common + order + payment_parameters + pricing + search + store + strong_parameters + ).each do |helper_name| + describe "require 'spree/core/controller_helpers/#{helper_name}" do + it "exists but will print a deprecation warning" do + expect(Spree.deprecator).to receive(:warn).with( + "The file \"#{Spree::Core::Engine.root}/lib/spree/core/controller_helpers/#{helper_name}.rb\" does not need to be `require`d any longer, it is now autoloaded.\n" + ) + require "spree/core/controller_helpers/#{helper_name}" + end + end + end +end