From 6fec4036550e2e936e89237f208ce28644ba1969 Mon Sep 17 00:00:00 2001 From: kaspernj Date: Wed, 18 Mar 2015 14:01:39 +0100 Subject: [PATCH 1/4] Fixed not-found error if using a mount-path for the Spree engine. --- app/controllers/spree/static_content_controller.rb | 2 +- app/models/spree/page.rb | 9 +++++++++ lib/spree_static_content.rb | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/controllers/spree/static_content_controller.rb b/app/controllers/spree/static_content_controller.rb index 9aca45e0..39b3551f 100644 --- a/app/controllers/spree/static_content_controller.rb +++ b/app/controllers/spree/static_content_controller.rb @@ -5,7 +5,7 @@ class Spree::StaticContentController < Spree::StoreController layout :determine_layout def show - @page = Spree::Page.by_store(current_store).visible.find_by_slug!(request.path) + @page = Spree::Page.by_store(current_store).visible.slug_relative_to_mount_point(request.path).first end private diff --git a/app/models/spree/page.rb b/app/models/spree/page.rb index d0663410..3551eb0c 100644 --- a/app/models/spree/page.rb +++ b/app/models/spree/page.rb @@ -14,6 +14,15 @@ class Spree::Page < ActiveRecord::Base scope :header_links, -> { where(:show_in_header => true).visible } scope :footer_links, -> { where(:show_in_footer => true).visible } scope :sidebar_links, -> { where(:show_in_sidebar => true).visible } + scope :slug_relative_to_mount_point, lambda { |request_path| + # Remove Spree engine mount point from the path. + spree_path_regex_str = Rails.application.routes.named_routes[:spree].path.source + spree_path_regex = Regexp.new(spree_path_regex_str) + path = request_path.gsub(spree_path_regex, "") + + # Match slug to path without Spree engine mount point. + where(:slug => path) + } scope :by_store, lambda { |store| joins(:stores).where("spree_pages_stores.store_id = ?", store) } diff --git a/lib/spree_static_content.rb b/lib/spree_static_content.rb index a40032d2..aed998fa 100644 --- a/lib/spree_static_content.rb +++ b/lib/spree_static_content.rb @@ -13,6 +13,6 @@ def self.remove_spree_mount_point(path) class Spree::StaticPage def self.matches?(request) return false if request.path =~ /(^\/+(admin|account|cart|checkout|content|login|pg\/|orders|products|s\/|session|signup|shipments|states|t\/|tax_categories|user)+)/ - !Spree::Page.visible.find_by_slug(request.path).nil? + Spree::Page.visible.slug_relative_to_mount_point(request.path).any? end end From 154f3c44588a8aa9c312902075a9c8d83b8c10c0 Mon Sep 17 00:00:00 2001 From: kaspernj Date: Wed, 18 Mar 2015 14:18:45 +0100 Subject: [PATCH 2/4] Raise exception if page is not found. Dont replace root-slash url's. --- app/controllers/spree/static_content_controller.rb | 2 +- app/models/spree/page.rb | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/controllers/spree/static_content_controller.rb b/app/controllers/spree/static_content_controller.rb index 39b3551f..12105413 100644 --- a/app/controllers/spree/static_content_controller.rb +++ b/app/controllers/spree/static_content_controller.rb @@ -5,7 +5,7 @@ class Spree::StaticContentController < Spree::StoreController layout :determine_layout def show - @page = Spree::Page.by_store(current_store).visible.slug_relative_to_mount_point(request.path).first + @page = Spree::Page.by_store(current_store).visible.slug_relative_to_mount_point(request.path).first! end private diff --git a/app/models/spree/page.rb b/app/models/spree/page.rb index 3551eb0c..f36f5004 100644 --- a/app/models/spree/page.rb +++ b/app/models/spree/page.rb @@ -17,8 +17,12 @@ class Spree::Page < ActiveRecord::Base scope :slug_relative_to_mount_point, lambda { |request_path| # Remove Spree engine mount point from the path. spree_path_regex_str = Rails.application.routes.named_routes[:spree].path.source - spree_path_regex = Regexp.new(spree_path_regex_str) - path = request_path.gsub(spree_path_regex, "") + if spree_path_regex_str == "\\A/" + path = request_path + else + spree_path_regex = Regexp.new(spree_path_regex_str) + path = request_path.gsub(spree_path_regex, "") + end # Match slug to path without Spree engine mount point. where(:slug => path) From 7c81b1cc7933521b0ef7a942934d5f798865602b Mon Sep 17 00:00:00 2001 From: kaspernj Date: Wed, 18 Mar 2015 14:36:26 +0100 Subject: [PATCH 3/4] Use existing method to remove mount point instead. --- app/controllers/spree/static_content_controller.rb | 2 +- app/models/spree/page.rb | 13 ------------- lib/spree_static_content.rb | 2 +- 3 files changed, 2 insertions(+), 15 deletions(-) diff --git a/app/controllers/spree/static_content_controller.rb b/app/controllers/spree/static_content_controller.rb index 12105413..fecc5fe3 100644 --- a/app/controllers/spree/static_content_controller.rb +++ b/app/controllers/spree/static_content_controller.rb @@ -5,7 +5,7 @@ class Spree::StaticContentController < Spree::StoreController layout :determine_layout def show - @page = Spree::Page.by_store(current_store).visible.slug_relative_to_mount_point(request.path).first! + @page = Spree::Page.by_store(current_store).visible.find_by_slug!(::StaticPage.remove_spree_mount_point(request.path)) end private diff --git a/app/models/spree/page.rb b/app/models/spree/page.rb index f36f5004..d0663410 100644 --- a/app/models/spree/page.rb +++ b/app/models/spree/page.rb @@ -14,19 +14,6 @@ class Spree::Page < ActiveRecord::Base scope :header_links, -> { where(:show_in_header => true).visible } scope :footer_links, -> { where(:show_in_footer => true).visible } scope :sidebar_links, -> { where(:show_in_sidebar => true).visible } - scope :slug_relative_to_mount_point, lambda { |request_path| - # Remove Spree engine mount point from the path. - spree_path_regex_str = Rails.application.routes.named_routes[:spree].path.source - if spree_path_regex_str == "\\A/" - path = request_path - else - spree_path_regex = Regexp.new(spree_path_regex_str) - path = request_path.gsub(spree_path_regex, "") - end - - # Match slug to path without Spree engine mount point. - where(:slug => path) - } scope :by_store, lambda { |store| joins(:stores).where("spree_pages_stores.store_id = ?", store) } diff --git a/lib/spree_static_content.rb b/lib/spree_static_content.rb index aed998fa..eb3af207 100644 --- a/lib/spree_static_content.rb +++ b/lib/spree_static_content.rb @@ -13,6 +13,6 @@ def self.remove_spree_mount_point(path) class Spree::StaticPage def self.matches?(request) return false if request.path =~ /(^\/+(admin|account|cart|checkout|content|login|pg\/|orders|products|s\/|session|signup|shipments|states|t\/|tax_categories|user)+)/ - Spree::Page.visible.slug_relative_to_mount_point(request.path).any? + !Spree::Page.visible.find_by_slug(::StaticPage.remove_spree_mount_point(request.path)).nil? end end From 9d12a608e5e0178fab22443cf33ecca64e4b49e4 Mon Sep 17 00:00:00 2001 From: kaspernj Date: Wed, 18 Mar 2015 14:44:27 +0100 Subject: [PATCH 4/4] Only remove path if exists. --- app/controllers/spree/static_content_controller.rb | 2 +- lib/spree_static_content.rb | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/controllers/spree/static_content_controller.rb b/app/controllers/spree/static_content_controller.rb index fecc5fe3..8635405b 100644 --- a/app/controllers/spree/static_content_controller.rb +++ b/app/controllers/spree/static_content_controller.rb @@ -5,7 +5,7 @@ class Spree::StaticContentController < Spree::StoreController layout :determine_layout def show - @page = Spree::Page.by_store(current_store).visible.find_by_slug!(::StaticPage.remove_spree_mount_point(request.path)) + @page = Spree::Page.by_store(current_store).visible.find_by_slug!(::StaticPage.remove_spree_mount_point_if_exists(request.path)) end private diff --git a/lib/spree_static_content.rb b/lib/spree_static_content.rb index eb3af207..93674936 100644 --- a/lib/spree_static_content.rb +++ b/lib/spree_static_content.rb @@ -8,11 +8,20 @@ def self.remove_spree_mount_point(path) regex = Regexp.new '\A' + Rails.application.routes.url_helpers.spree_path path.sub( regex, '').split('?')[0] end + + def self.remove_spree_mount_point_if_exists(path) + spree_path = Rails.application.routes.url_helpers.spree_path + if spree_path == "/" + return path + else + return remove_spree_mount_point(path) + end + end end class Spree::StaticPage def self.matches?(request) return false if request.path =~ /(^\/+(admin|account|cart|checkout|content|login|pg\/|orders|products|s\/|session|signup|shipments|states|t\/|tax_categories|user)+)/ - !Spree::Page.visible.find_by_slug(::StaticPage.remove_spree_mount_point(request.path)).nil? + !Spree::Page.visible.find_by_slug(::StaticPage.remove_spree_mount_point_if_exists(request.path)).nil? end end