Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Remaining Storefront controllers
Damian Legawiec315bb41ceebf
diff --git a/storefront/app/controllers/spree/contacts_controller.rb b/storefront/app/controllers/spree/contacts_controller.rbnew file mode 100644index 0000000000..2b10cefc2c--- /dev/null+++ b/storefront/app/controllers/spree/contacts_controller.rb@@ -0,0 +1,66 @@+module Spree+ class ContactsController < StoreController+ before_action :load_store+ before_action :validate_captcha, only: :create+ before_action :ensure_customer_support_email++ def new+ @contact = Spree::Contact.new+ end++ def create+ @contact = Spree::Contact.new(params[:contact])+ @contact.customer_support_email = current_store.customer_support_email+ @contact.request = request++ if @contact.deliver+ flash[:success] = 'Message sent!'+ else+ message_not_sent+ end+ redirect_to action: :new+ end++ private++ def load_store+ @store = current_store+ end++ def message_not_sent(exception = nil)+ Spree::ErrorHandler.call(+ exception: exception || StandardError.new('Contact Form Message not sent'),+ opts: {+ report_context: {+ contact: @contact&.id+ }+ }+ )++ flash[:error] = "Unfortunately we weren't able to send the email at this time. Please try again later"+ end++ def ensure_customer_support_email+ return if current_store.customer_support_email.present?++ flash[:error] = Spree.t(:customer_support_email_not_configured)+ redirect_back_or_default root_path+ end++ def validate_captcha+ return unless store_integration('recaptcha')++ hostname = request.host || @store.url_or_custom_domain+ return if verify_recaptcha(+ action: 'contacts/create',+ hostname: hostname,+ secret_key: store_integration('recaptcha').secret_key,+ site_key: store_integration('recaptcha').site_key,+ minimum_score: 0.5+ )++ flash[:error] = 'Captcha verification failed, please try again.'+ redirect_to action: :new+ end+ end+enddiff --git a/storefront/app/controllers/spree/order_status_controller.rb b/storefront/app/controllers/spree/order_status_controller.rbnew file mode 100644index 0000000000..106c1caf91--- /dev/null+++ b/storefront/app/controllers/spree/order_status_controller.rb@@ -0,0 +1,26 @@+module Spree+ class OrderStatusController < StoreController+ # page for user to input their email address and order number+ # GET /order_status+ def new; end++ # validate email/order number and redirect to order page+ # POST /order_status+ def create+ @order = current_store.orders.complete.without_vendor.find_by(number: params[:number], email: params[:email])++ if @order+ redirect_to spree.order_path(@order, token: @order.token)+ else+ flash[:error] = Spree.t(:order_not_found)+ render :new+ end+ end++ private++ def accurate_title+ Spree.t(:order_status)+ end+ end+enddiff --git a/storefront/app/controllers/spree/password_controller.rb b/storefront/app/controllers/spree/password_controller.rbnew file mode 100644index 0000000000..8235cc38e6--- /dev/null+++ b/storefront/app/controllers/spree/password_controller.rb@@ -0,0 +1,21 @@+module Spree+ class PasswordController < StoreController+ layout 'spree/password'++ skip_before_action :redirect_to_password++ def show+ @current_page = current_theme.pages.find_by(type: 'Spree::Pages::Password')+ end++ def check+ if params[:password] == current_store.storefront_password+ session[:password_valid] = true+ redirect_to root_path+ else+ flash[:error] = 'Incorrect password. Please try again.'+ redirect_to password_path+ end+ end+ end+enddiff --git a/storefront/app/controllers/spree/policies_controller.rb b/storefront/app/controllers/spree/policies_controller.rbnew file mode 100644index 0000000000..0ce59103ef--- /dev/null+++ b/storefront/app/controllers/spree/policies_controller.rb@@ -0,0 +1,17 @@+module Spree+ class PoliciesController < StoreController+ def show+ if params[:id].in?(supported_policies)+ @policy = current_store.send("customer_#{params[:id]}")+ else+ raise ActiveRecord::RecordNotFound+ end+ end++ private++ def supported_policies+ %w[privacy_policy terms_of_service returns_policy shipping_policy]+ end+ end+enddiff --git a/storefront/app/controllers/spree/posts_controller.rb b/storefront/app/controllers/spree/posts_controller.rbnew file mode 100644index 0000000000..9c86d7da05--- /dev/null+++ b/storefront/app/controllers/spree/posts_controller.rb@@ -0,0 +1,76 @@+module Spree+ class PostsController < Spree::StoreController+ before_action :load_category, if: -> { params[:category_id].present? }, only: :index+ before_action :load_tag, if: -> { params[:tag].present? }, only: :index++ after_action :track_show, only: :show++ def index+ @current_page = current_theme.pages.find_by!(type: 'Spree::Pages::PostList')++ scope = current_store.posts.published+ scope = scope.tagged_with(@tag.name) if @tag.present?+ scope = scope.where(post_category_id: @category.id) if @category.present?++ sorted_posts = scope.order(published_at: :desc)+ @posts = sorted_posts.page(params[:page]).per(20)+ end++ def show+ if params[:preview_id].present?+ preview_post = current_store.posts.friendly.find(params[:id])+ raise ActiveRecord::RecordNotFound if preview_post.id.to_s != params[:preview_id]++ @post ||= preview_post+ else+ @post = current_store.posts.friendly.published.find(params[:id])+ end++ @current_page = current_theme.pages.find_by!(type: 'Spree::Pages::Post')+ end++ def related_products+ @post = Spree::Post.friendly.find(params[:id])+ current_page = current_theme.pages.find_by!(type: 'Spree::Pages::Post')+ @section = current_page.sections.find_by!(id: params[:section_id], type: 'Spree::PageSections::RelatedProducts')+ end++ private++ def accurate_title+ if @post+ @post.meta_title.presence || @post.title+ elsif @category.present? && @tag.present?+ "#{@category.title} / #{@tag.name.titleize}"+ elsif @category.present?+ @category.title+ elsif @tag.present?+ Spree.t(:all_posts_with_tag, tag: @tag.name.titleize)+ else+ Spree.t(:all_posts)+ end+ end++ def load_category+ @category = current_store.post_categories.friendly.find(params[:category_id])+ @page_description = @category.description.to_plain_text+ end++ def load_tag+ @tag = post_tags_scope.find_by!(name: params[:tag].strip)+ end++ def post_tags_scope+ @post_tags_scope ||= ActsAsTaggableOn::Tag.+ joins(:taggings).+ where('taggings.taggable_type = ?', Spree::Post.to_s).+ for_context(:tags).for_tenant(current_store.id)+ end++ def track_show+ return if turbo_frame_request? || turbo_stream_request?++ track_event('post_viewed', { post_id: @post.id })+ end+ end+enddiff --git a/storefront/app/controllers/spree/seo_controller.rb b/storefront/app/controllers/spree/seo_controller.rbnew file mode 100644index 0000000000..ef819fedd7--- /dev/null+++ b/storefront/app/controllers/spree/seo_controller.rb@@ -0,0 +1,25 @@+module Spree+ class SeoController < StoreController+ include BaseHelper+ include StorefrontHelper++ def robots+ render view: 'seo/robots', layout: false, content_type: 'text/plain', locals: { store: current_store }+ end++ def sitemap+ unless current_store.prefers_index_in_search_engines?+ head :not_found+ return+ end++ respond_to do |format|+ format.xml+ format.gzip do+ gz_xml = ActiveSupport::Gzip.compress(render_to_string(template: 'spree/seo/sitemap', formats: [:xml]))+ send_data(gz_xml, filename: 'sitemap.xml.gz', type: 'application/x-gzip', disposition: 'inline')+ end+ end+ end+ end+enddiff --git a/storefront/app/controllers/spree/settings_controller.rb b/storefront/app/controllers/spree/settings_controller.rbnew file mode 100644index 0000000000..cfc6804b2e--- /dev/null+++ b/storefront/app/controllers/spree/settings_controller.rb@@ -0,0 +1,68 @@+module Spree+ class SettingsController < Spree::StoreController+ def show; end++ 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++ private++ def find_slug_in_current_locale(params, new_locale)+ if params[:id].present?+ case params[:controller]+ when 'spree/products'+ old_product = find_with_fallback(params[:locale]) { current_store.products.friendly.find(params[:id]) }+ old_product.slug(locale: new_locale) || old_product.slug(locale: current_store.default_locale)+ when 'spree/taxons'+ old_taxon = find_with_fallback(params[:locale]) { current_store.taxons.friendly.find(params[:id]) }+ old_taxon.permalink(locale: new_locale) || old_taxon.permalink(locale: current_store.default_locale)+ else+ params[:id]+ end+ end+ end++ def find_with_fallback(locale, &block)+ Mobility.with_locale(locale) { find_with_fallback_default_locale(&block) }+ end+ end+enddiff --git a/storefront/app/controllers/spree/taxonomies_controller.rb b/storefront/app/controllers/spree/taxonomies_controller.rbnew file mode 100644index 0000000000..c7c5adf96a--- /dev/null+++ b/storefront/app/controllers/spree/taxonomies_controller.rb@@ -0,0 +1,30 @@+module Spree+ class TaxonomiesController < Spree::StoreController+ def show+ @taxonomy = current_store.taxons.where(parent_id: nil).friendly.find(params[:id])++ @taxons = @taxonomy.children.order(name: :asc)+ taxons_grouped = @taxons.group_by { |b| b.name[0].upcase }++ numbers = ('0'..'9').to_a+ alphabet_letters = ('A'..'Z').to_a++ @taxons_grouped_by_letter = begin+ taxons_grouped_by_letter = {}+ alphabet_letters.each do |letter|+ taxons_grouped_by_letter[letter] = taxons_grouped[letter]+ end+ taxons_grouped_by_letter+ end++ @taxons_grouped_by_numbers = taxons_grouped.select { |k, _v| numbers.include?(k) }+ @taxons_grouped_by_other = taxons_grouped.select { |k, _v| alphabet_letters.concat(numbers).exclude?(k) }+ end++ private++ def accurate_title+ @taxonomy.root.seo_title.presence || @taxonomy.root.name+ end+ end+enddiff --git a/storefront/app/controllers/spree/taxons_controller.rb b/storefront/app/controllers/spree/taxons_controller.rbnew file mode 100644index 0000000000..0fbe370694--- /dev/null+++ b/storefront/app/controllers/spree/taxons_controller.rb@@ -0,0 +1,46 @@+module Spree+ class TaxonsController < Spree::StoreController+ include Spree::StorefrontHelper+ helper 'spree/products'++ before_action :load_taxon+ after_action :track_show, only: :show++ def show+ redirect_if_legacy_path unless turbo_frame_request? || turbo_stream_request?++ @current_page = current_theme.pages.find_by(type: 'Spree::Pages::Taxon')+ end++ private++ def accurate_title+ load_taxon unless @taxon+ @taxon.seo_title+ end++ def load_taxon+ @taxon ||= find_with_fallback_default_locale { current_store.taxons.friendly.find(params[:id]) }+ end++ def track_show+ return if turbo_frame_request? || turbo_stream_request?++ track_event('product_list_viewed', { taxon: @taxon })+ end++ def redirect_if_legacy_path+ # If an old id or a numeric id was used to find the record,+ # we should do a 301 redirect that uses the current friendly id.+ if params[:id] != @taxon.friendly_id+ params[:id] = @taxon.friendly_id+ params.permit!+ redirect_to url_for(params), status: :moved_permanently+ end+ end++ def default_products_sort+ @taxon.sort_order+ end+ end+enddiff --git a/storefront/spec/controllers/spree/contacts_controller_spec.rb b/storefront/spec/controllers/spree/contacts_controller_spec.rbnew file mode 100644index 0000000000..828c8c3744--- /dev/null+++ b/storefront/spec/controllers/spree/contacts_controller_spec.rb@@ -0,0 +1,80 @@+require 'rails_helper'++RSpec.describe Spree::ContactsController, type: :controller do+ let(:store) { Spree::Store.default }+ let(:contact_params) { { name: "John Doe", email: "[email protected]", message: "Test message" } }++ context 'when customer support email is not configured' do+ it 'redirects to root path' do+ get :new+ expect(response).to redirect_to(root_path)+ end+ end++ context 'when customer support email is configured' do+ before do+ store.update(customer_support_email: '[email protected]')+ end++ describe "POST #create" do+ context "when contact is delivered successfully" do+ before do+ allow_any_instance_of(Spree::Contact).to receive(:deliver).and_return(true)+ end++ it "sets a success flash message" do+ post :create, params: { contact: contact_params }+ expect(flash[:success]).to eq('Message sent!')+ end++ it "redirects to new action" do+ post :create, params: { contact: contact_params }+ expect(response).to redirect_to(action: :new)+ end+ end++ context "when contact delivery fails" do+ before do+ allow_any_instance_of(Spree::Contact).to receive(:deliver).and_return(false)+ end++ it "sets an error flash message" do+ post :create, params: { contact: contact_params }+ expect(flash[:error]).to eq("Unfortunately we weren't able to send the email at this time. Please try again later")+ end++ it "redirects to new action" do+ post :create, params: { contact: contact_params }+ expect(response).to redirect_to(action: :new)+ end+ end+ end++ describe "#validate_captcha" do+ context "when recaptcha integration is present" do+ let!(:recaptcha_integration) { create(:recaptcha_integration) }++ it "verifies recaptcha" do+ expect(controller).to receive(:verify_recaptcha).and_return(true)+ post :create, params: { contact: contact_params }+ end++ context "when recaptcha verification fails" do+ before do+ allow(controller).to receive(:verify_recaptcha).and_return(false)+ end++ it "sets an error flash message" do+ post :create, params: { contact: contact_params }+ expect(flash[:error]).to eq('Captcha verification failed, please try again.')+ end++ it "redirects to new action" do+ post :create, params: { contact: contact_params }+ expect(response).to redirect_to(action: :new)+ end+ end+ end+ end+ end+enddiff --git a/storefront/spec/controllers/spree/settings_controller_spec.rb b/storefront/spec/controllers/spree/settings_controller_spec.rbnew file mode 100644index 0000000000..b9d73c2e2c--- /dev/null+++ b/storefront/spec/controllers/spree/settings_controller_spec.rb@@ -0,0 +1,224 @@+require 'spec_helper'++describe Spree::SettingsController, type: :controller do+ let(:store) { Spree::Store.default }+ let(:user) { create(:user) }++ before do+ store.update(supported_locales: 'en,fr,es')+ end++ describe 'PUT #update' do+ describe 'currencies' do+ subject(:set_currency) { put :update, params: params }++ let(:params) { { switch_to_currency: 'EUR' } }+ let(:order) { create(:order_with_totals, currency: 'USD', store: store) }++ before do+ allow(controller).to receive(:current_order).and_return(order)+ store.update!(supported_currencies: supported_currencies)+ end++ context 'when switching to a supported currency' do+ let(:supported_currencies) { 'USD,EUR,PLN' }++ it 'sets the new currency' do+ set_currency++ expect(response.status).to eq(302)++ expect(session[:currency]).to eq('EUR')+ expect(order.reload.currency).to eq('EUR')+ end+ end++ context 'when switching to an unsupported currency' do+ let(:supported_currencies) { 'USD,PLN' }++ it 'skips setting a new currency' do+ set_currency++ expect(response.status).to eq(302)++ expect(session[:currency]).to be_nil+ expect(order.reload.currency).to eq('USD')+ end+ end++ context 'without params' do+ let(:params) { nil }+ let(:supported_currencies) { 'USD,EUR,PLN' }++ it 'skips setting a new currency' do+ set_currency++ expect(response.status).to eq(302)++ expect(session[:currency]).to be_nil+ expect(order.reload.currency).to eq('USD')+ end+ end++ context 'without the current order' do+ let(:order) { nil }+ let(:supported_currencies) { 'USD,EUR,PLN' }++ it 'sets the currency for the storefront only' do+ set_currency++ expect(response.status).to eq(302)+ expect(session[:currency]).to eq('EUR')+ end+ end+ end++ describe 'locales' do+ context 'when user is logged in' do+ before do+ allow(controller).to receive(:try_spree_current_user).and_return(user)+ end++ context 'with a valid locale' do+ it 'updates the user locale' do+ put :update, params: { switch_to_locale: 'fr' }+ expect(user.reload.selected_locale).to eq('fr')+ end++ context 'without referer' do+ subject { put :update, params: { switch_to_locale: switch_to_locale } }++ let(:switch_to_locale) { 'fr' }++ it 'redirects to root path with new locale' do+ subject+ expect(response).to redirect_to(spree.root_path(locale: 'fr'))+ end++ context 'when switching back to the store default locale' do+ let(:switch_to_locale) { 'en' }++ it 'redirects to the product page with the correct slug in the default locale' do+ subject+ expect(response).to redirect_to('/')+ end+ end+ end++ context 'with referer' do+ before do+ request.env['HTTP_REFERER'] = spree.products_path+ end++ it 'redirects to the previous page with new locale' do+ put :update, params: { switch_to_locale: 'fr' }+ expect(response).to redirect_to(spree.products_path(locale: 'fr'))+ end++ context 'when referer is a product page' do+ let!(:product) { create(:product, name: 'Test Product') }++ before do+ Mobility.with_locale(:fr) { product.update!(name: 'Produit Test', slug: 'produit-test') }+ Mobility.with_locale(:es) { product.update!(name: 'Producto de Prueba', slug: 'producto-de-prueba') }++ request.env['HTTP_REFERER'] = spree.product_path(product)+ end++ it 'redirects to the product page with the correct slug in the new locale' do+ put :update, params: { switch_to_locale: 'fr' }+ expect(response).to redirect_to('/fr/products/produit-test')+ end++ context 'when switching back to the store default locale' do+ before do+ request.env['HTTP_REFERER'] = spree.product_path(product, locale: 'fr')+ end++ it 'redirects to the product page with the correct slug in the default locale' do+ put :update, params: { switch_to_locale: 'en' }+ expect(response).to redirect_to('/products/test-product')+ end+ end+ end++ context 'when referer is a taxon page' do+ let!(:taxon) { create(:taxon, name: 'Category') }++ before do+ Mobility.with_locale(:fr) { taxon.update!(name: 'Catégorie', permalink: 'categorie') }+ Mobility.with_locale(:es) { taxon.update!(name: 'Categoría', permalink: 'categoria') }++ request.env['HTTP_REFERER'] = spree.nested_taxons_path(taxon.permalink)+ end++ it 'redirects to the taxon page with the correct permalink in the new locale' do+ put :update, params: { switch_to_locale: 'fr' }+ expect(response).to redirect_to("/fr/t/#{taxon.parent.slug}/categorie")+ end++ context 'when switching back to the store default locale' do+ before do+ request.env['HTTP_REFERER'] = spree.nested_taxons_path(taxon.permalink, locale: 'fr')+ end++ it 'redirects to the taxon page with the correct permalink in the default locale' do+ put :update, params: { switch_to_locale: 'en' }+ expect(response).to redirect_to("/t/#{taxon.parent.slug}/category")+ end+ end+ end++ context 'when referer is another controller' do+ before do+ request.env['HTTP_REFERER'] = spree.cart_path+ end++ it 'redirects to the same page with new locale' do+ put :update, params: { switch_to_locale: 'fr' }+ expect(response).to redirect_to(spree.cart_path(locale: 'fr'))+ end+ end+ end+ end++ context 'with an invalid locale' do+ it 'redirects to root path without changing locale' do+ put :update, params: { switch_to_locale: 'invalid' }+ expect(response).to redirect_to(spree.root_path)+ expect(user.reload.selected_locale).not_to eq('invalid')+ end+ end+ end++ context 'when user is not logged in' do+ before do+ allow(controller).to receive(:try_spree_current_user).and_return(nil)+ end++ it 'does not attempt to update user locale' do+ put :update, params: { switch_to_locale: 'fr' }+ expect(response).to redirect_to(spree.root_path(locale: 'fr'))+ end++ context 'with referer' do+ before do+ request.env['HTTP_REFERER'] = spree.products_path+ end++ it 'redirects to the previous page with new locale' do+ put :update, params: { switch_to_locale: 'fr' }+ expect(response).to redirect_to(spree.products_path(locale: 'fr'))+ end+ end++ context 'with an invalid locale' do+ it 'redirects to root path without changing locale' do+ put :update, params: { switch_to_locale: 'invalid' }+ expect(response).to redirect_to(spree.root_path)+ end+ end+ end+ end+ end+end