From ba628721b2ab319537ae311f014d457d118b2e20 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 6 Aug 2020 22:25:12 +0200 Subject: [PATCH] Allow to set a custom Page::UrlPath class Closes #1918 --- app/models/alchemy/page.rb | 17 ++++++++++++++++- spec/models/alchemy/page_spec.rb | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/models/alchemy/page.rb b/app/models/alchemy/page.rb index 669ad7b92c..202309f262 100644 --- a/app/models/alchemy/page.rb +++ b/app/models/alchemy/page.rb @@ -149,6 +149,21 @@ class Page < BaseRecord # Class methods # class << self + # The url_path class + # @see Alchemy::Page::UrlPath + def url_path_class + @_url_path_class ||= Alchemy::Page::UrlPath + end + + # Set a custom url path class + # + # # config/initializers/alchemy.rb + # Alchemy::Page.url_path_class = MyPageUrlPathClass + # + def url_path_class=(klass) + @_url_path_class = klass + end + # Used to store the current page previewed in the edit page template. # def current_preview=(page) @@ -298,7 +313,7 @@ def find_elements(options = {}) # # @see Alchemy::Page::UrlPath#call def url_path - Alchemy::Page::UrlPath.new(self).call + self.class.url_path_class.new(self).call end # The page's view partial is dependent from its page layout diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index f0a8879f86..8dab11d25c 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -266,6 +266,24 @@ module Alchemy # ClassMethods (a-z) + describe ".url_path_class" do + subject { described_class.url_path_class } + + it { is_expected.to eq(Alchemy::Page::UrlPath) } + + context "if set to another url path class" do + class AnotherUrlPathClass; end + + before do + described_class.url_path_class = AnotherUrlPathClass + end + + it { is_expected.to eq(AnotherUrlPathClass) } + + after { described_class.instance_variable_set(:@_url_path_class, nil) } + end + end + describe ".all_from_clipboard_for_select" do context "with clipboard holding pages having non unique page layout" do it "should return the pages" do