Class: Spree::SettingsController

Inherits:
StoreController show all
Defined in:
app/controllers/spree/settings_controller.rb

Instance Method Summary collapse

Methods inherited from StoreController

#current_taxon, #default_products_sort, #permitted_products_params, #products_filters_params, #render_404_if_store_not_exists, #store_filter_names, #store_filter_names_hash

Methods included from AnalyticsHelper

#analytics_event_handlers, #track_event, #unsupported_event?, #visitor_id

Methods included from WishlistHelper

#current_wishlist

Methods included from PasswordProtected

#redirect_to_password

Methods included from StorefrontHelper

#as_aspect_ratio, #page_description, #page_image, #paths_equal?, #render_storefront_partials, #show_account_pane?, #svg_country_icon, #tailwind_classes_for

Methods included from ThemeConcern

#default_url_options, #set_theme_view_paths

Instance Method Details

#showObject



3
# File 'app/controllers/spree/settings_controller.rb', line 3

def show; end

#updateObject



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
# File 'app/controllers/spree/settings_controller.rb', line 5

def update
  new_locale = (params[:switch_to_locale] || params[:locale]).to_s
  new_currency = params[:switch_to_currency]&.upcase

  if new_currency.present? && supported_currency?(new_currency)
    current_order&.update(currency: new_currency)
    session[:currency] = new_currency
  end

  if new_locale.present? && supported_locale?(new_locale)
    if try_spree_current_user && try_spree_current_user.selected_locale != new_locale
      try_spree_current_user.update!(selected_locale: new_locale)
    end

    locale_for_slug = new_locale
    new_locale = nil if new_locale.to_s == current_store.default_locale.to_s

    if request.referer.present?
      uri = URI(request.referer)

      previous_params = begin
        Spree::Core::Engine.routes.recognize_path(uri.path)
      rescue ActionController::RoutingError
        Rails.application.routes.recognize_path(uri.path)
      end

      redirect_to spree.root_path(locale: new_locale) && return if previous_params.blank?

      new_params = previous_params.clone.merge(locale: new_locale)

      # We only care if the previous url was for a specific record, because we need to find it with the slug/permalink from the new locale
      new_params[:id] = find_slug_in_current_locale(previous_params, locale_for_slug)

      redirect_to new_params
    else
      redirect_to spree.root_path(locale: new_locale)
    end
  else
    redirect_to spree.root_path
  end
end