Skip to content
This repository was archived by the owner on Aug 10, 2021. It is now read-only.

Better handle locale in request.path - solve #268 #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/spree/static_content_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class StaticContentController < StoreController
layout :determine_layout

def show
@page = Spree::StaticPage.finder_scope.by_store(current_store).find_by!(slug: request.path)
@page = Spree::StaticPage.finder_scope.by_store(current_store).find_by!(slug: [params[:path], "/#{params[:path]}"])
end

private
Expand Down
2 changes: 0 additions & 2 deletions app/models/spree/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ def link
private

def update_positions_and_slug
# Ensure that all slugs start with a slash.
slug.prepend('/') if not_using_foreign_link? && !slug.start_with?('/')
return if new_record?
return unless (prev_position = Spree::Page.find(id).position)
if prev_position > position
Expand Down
7 changes: 4 additions & 3 deletions lib/spree_static_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ def remove_spree_mount_point(path)
module Spree
class StaticPage
def self.matches?(request)
return false if request.path =~ %r{\A\/+(admin|account|cart|checkout|content|login|pg\/|orders|products|s\/|session|signup|shipments|states|t\/|tax_categories|user)+}
!self.finder_scope.find_by(slug: request.path).nil?
path = request.path_parameters[:path]
return false if path =~ %r{\A+(admin|account|cart|checkout|content|login|pg\/|orders|products|s\/|session|signup|shipments|states|t\/|tax_categories|user)+}
!self.finder_scope.find_by(slug: [path, "/#{path}"]).nil?
end

protected

def self.finder_scope
scope = Spree::Page.visible
scope = scope.joins(:translations) if defined?(SpreeGlobalize)
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/spree_static_content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
context '.matches?' do
it 'is true when valid page' do
page = create(:page, slug: 'hello', visible: true)
request = double('request', path: page.slug)
request = double('request', path_parameters: {path: page.slug})
expect(described_class.matches?(request)).to be true
end

it 'is false when using reserved slug name' do
page = create(:page, slug: 'login', visible: true)
request = double('request', path: page.slug)
request = double('request', path_parameters: {path: page.slug})
expect(described_class.matches?(request)).to be false
end

it 'is false when page is not accessible' do
page = create(:page, slug: 'hello', visible: false)
request = double('request', path: page.slug)
request = double('request', path_parameters: {path: page.slug})
expect(described_class.matches?(request)).to be false
end
end
Expand Down
5 changes: 0 additions & 5 deletions spec/models/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
end
end

it 'always add / prefix to slug' do
page = create(:page, slug: 'hello')
expect(page.slug).to eq '/hello'
end

context '.link' do
it 'returns slug if foreign_link blank' do
page = create(:page, slug: 'hello')
Expand Down