Skip to content

armgit / spree

Public Imported from GitHub
0

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.

[SD-1402] - As a Admin on Edit Product page I can see a Preview Product link without Spree Frontend (#11216)

Matthew Kennedy0ca1aa5f7897
diff --git a/backend/app/helpers/spree/admin/base_helper.rb b/backend/app/helpers/spree/admin/base_helper.rb
index e595f26877..568b916250 100644
--- a/backend/app/helpers/spree/admin/base_helper.rb
+++ b/backend/app/helpers/spree/admin/base_helper.rb
@@ -237,21 +237,17 @@ module Spree
end
def product_preview_link(product)
- return unless frontend_available?
-
button_link_to(
Spree.t(:preview_product),
- spree.product_url(product),
+ spree_storefront_resource_url(product),
class: 'btn-outline-secondary', icon: 'view.svg', id: 'admin_preview_product', target: :blank
)
end
def taxon_preview_link(taxon)
- return unless frontend_available?
-
button_link_to(
Spree.t(:preview_taxon),
- seo_url(taxon),
+ spree_storefront_resource_url(taxon),
class: 'btn-outline-secondary', icon: 'view.svg', id: 'admin_preview_taxon', target: :blank
)
end
diff --git a/backend/spec/features/admin/products/edit/products_spec.rb b/backend/spec/features/admin/products/edit/products_spec.rb
index a6b7549ebc..b0275b60a6 100644
--- a/backend/spec/features/admin/products/edit/products_spec.rb
+++ b/backend/spec/features/admin/products/edit/products_spec.rb
@@ -3,18 +3,18 @@ require 'spec_helper'
describe 'Product Details', type: :feature, js: true do
stub_authorization!
+ let!(:product) { create(:product, name: 'Bún thịt nướng', sku: 'A100', description: 'lorem ipsum', available_on: '2013-08-14 01:02:03')}
+
context 'editing a product with WYSIWYG disabled' do
before do
Spree::Config.product_wysiwyg_editor_enabled = false
- create(:product, name: 'Bún thịt nướng', sku: 'A100', description: 'lorem ipsum', available_on: '2013-08-14 01:02:03')
- visit spree.admin_products_path
- within_row(1) { click_icon :edit }
+ visit spree.admin_product_path(product)
end
after { Spree::Config.product_wysiwyg_editor_enabled = true }
it 'displays the product description as a standard input field' do
- expect(page).to have_field(id: 'product_description', with: 'lorem ipsum')
+ expect(page).to have_field(id: 'product_description', with: product.description)
expect(page).not_to have_css('#product_description_ifr')
end
end
@@ -22,9 +22,7 @@ describe 'Product Details', type: :feature, js: true do
context 'editing a product with WYSIWYG editer enabled' do
before do
Spree::Config.product_wysiwyg_editor_enabled = true
- create(:product, name: 'Bún thịt nướng', sku: 'A100', description: 'lorem ipsum', available_on: '2013-08-14 01:02:03')
- visit spree.admin_products_path
- within_row(1) { click_icon :edit }
+ visit spree.admin_product_path(product)
end
it 'displays the product details with a WYSIWYG editor for the product description input' do
@@ -52,10 +50,11 @@ describe 'Product Details', type: :feature, js: true do
it 'has a link to preview a product' do
allow(Spree::Core::Engine).to receive(:frontend_available?).and_return(true)
- allow_any_instance_of(ActionDispatch::Routing::RoutesProxy).to receive(:product_url).and_return('http://example.com/products/product-slug')
+
click_link 'Details'
+
expect(page).to have_css('#admin_preview_product')
- expect(page).to have_link Spree.t(:preview_product), href: 'http://example.com/products/product-slug'
+ expect(page).to have_link Spree.t(:preview_product), href: "http://www.example.com/products/bun-th-t-n-ng"
end
end
end
diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb
index 6f4be70843..c7d79df4b0 100644
--- a/core/app/helpers/spree/base_helper.rb
+++ b/core/app/helpers/spree/base_helper.rb
@@ -175,6 +175,24 @@ module Spree
Rails::VERSION::STRING < '6.0'
end
+ def spree_storefront_resource_url(resource, options = {})
+ if locale_param
+ options.merge!(locale: locale_param)
+ end
+
+ localize = if options[:locale].present?
+ "/#{options[:locale]}"
+ else
+ ''
+ end
+
+ if resource.instance_of?(Spree::Product)
+ "#{current_store.formatted_url}#{localize}/#{Spree::Config[:storefront_products_path]}/#{resource.slug}"
+ elsif resource.instance_of?(Spree::Taxon)
+ "#{current_store.formatted_url}#{localize}/#{Spree::Config[:storefront_taxons_path]}/#{resource.permalink}"
+ end
+ end
+
# we should always try to render image of the default variant
# same as it's done on PDP
def default_image_for_product(product)
diff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rb
index c01368191e..3f01b32af0 100644
--- a/core/spec/helpers/base_helper_spec.rb
+++ b/core/spec/helpers/base_helper_spec.rb
@@ -71,6 +71,54 @@ describe Spree::BaseHelper, type: :helper do
end
end
+ describe '#spree_storefront_resource_url' do
+ let!(:store) { create(:store) }
+ let!(:taxon) { create(:taxon) }
+ let!(:product) { create(:product) }
+
+ before do
+ allow(helper).to receive(:frontend_available?).and_return(false)
+ allow(helper).to receive(:current_store).and_return(store)
+ allow(helper).to receive(:locale_param)
+ end
+
+ context 'for Product URL' do
+ it { expect(helper.spree_storefront_resource_url(product)).to eq("http://www.example.com/products/#{product.slug}") }
+
+ context 'when a locale is passed' do
+ before do
+ allow(helper).to receive(:current_store).and_return(store)
+ end
+
+ it { expect(helper.spree_storefront_resource_url(product, locale: :de)).to eq("http://www.example.com/de/products/#{product.slug}") }
+ end
+
+ context 'when locale_param is present' do
+ before do
+ allow(helper).to receive(:locale_param).and_return(:fr)
+ end
+
+ it { expect(helper.spree_storefront_resource_url(product)).to eq("http://www.example.com/fr/products/#{product.slug}") }
+ end
+ end
+
+ context 'for Taxon URL' do
+ it { expect(helper.spree_storefront_resource_url(taxon)).to eq("http://www.example.com/t/#{taxon.permalink}") }
+
+ context 'when a locale is passed' do
+ it { expect(helper.spree_storefront_resource_url(taxon, locale: :de)).to eq("http://www.example.com/de/t/#{taxon.permalink}") }
+ end
+
+ context 'when locale_param is present' do
+ before do
+ allow(helper).to receive(:locale_param).and_return(:fr)
+ end
+
+ it { expect(helper.spree_storefront_resource_url(taxon)).to eq("http://www.example.com/fr/t/#{taxon.permalink}") }
+ end
+ end
+ end
+
# Regression test for #1436
context 'defining custom image helpers' do
let(:product) { build(:product) }