From b65031d2fe557c19bdd040a5b4376aac906159fa Mon Sep 17 00:00:00 2001 From: Gaspard Bucher Date: Tue, 5 Nov 2013 18:00:40 +0100 Subject: [PATCH] Fixed urls for custom base and home node in alias sites. --- lib/zena/use/ancestry.rb | 1 + lib/zena/use/urls.rb | 50 ++++++++++++------- test/integration/zafu_compiler/alias_site.yml | 20 ++++++-- test/integration/zafu_compiler_test.rb | 12 +++++ 4 files changed, 60 insertions(+), 23 deletions(-) diff --git a/lib/zena/use/ancestry.rb b/lib/zena/use/ancestry.rb index 8689ecd1..28528a23 100644 --- a/lib/zena/use/ancestry.rb +++ b/lib/zena/use/ancestry.rb @@ -4,6 +4,7 @@ module Use module Ancestry def self.basepath_from_fullpath(fullpath) + return '' if !fullpath # This happens with pseudo root/home when node is not accessible fullpath.split('/')[1..-1].join('/') end diff --git a/lib/zena/use/urls.rb b/lib/zena/use/urls.rb index 1a20c6d4..ca9285bb 100644 --- a/lib/zena/use/urls.rb +++ b/lib/zena/use/urls.rb @@ -123,36 +123,50 @@ def zen_path(node, options={}) stamp = make_cachestamp(node, mode) end - path = if !asset && node[:id] == visitor.site[:root_id] && mode.nil? && format == 'html' + path = if !asset && node[:id] == visitor.site.home_id && mode.nil? && format == 'html' "#{abs_url_prefix}/#{pre}" # index page elsif node[:custom_base] "#{abs_url_prefix}/#{pre}/" + - basepath_as_url(node.basepath) + - (mode ? "_#{mode}" : '') + - (asset ? "=#{asset}" : '') + - (stamp ? ".#{stamp}" : '') + + basepath_as_url(node, true) + + (mode ? "_#{mode}" : '') + + (asset ? "=#{asset}" : '') + + (stamp ? ".#{stamp}" : '') + (format == 'html' ? '' : ".#{format}") else "#{abs_url_prefix}/#{pre}/" + - (node.basepath.blank? ? '' : "#{basepath_as_url(node.basepath)}/") + - (node.klass.downcase ) + - (node[:zip].to_s ) + - (mode ? "_#{mode}" : '') + - (asset ? "=#{asset}" : '') + - (stamp ? ".#{stamp}" : '') + + basepath_as_url(node, false)+ + (node.klass.downcase ) + + (node[:zip].to_s ) + + (mode ? "_#{mode}" : '') + + (asset ? "=#{asset}" : '') + + (stamp ? ".#{stamp}" : '') + ".#{format}" end append_query_params(path, opts) end - def basepath_as_url(path) - path.split('/').map do |zip| - if n = secure(Node) { Node.find_by_zip(zip) } - n.title.url_name - else - nil + def basepath_as_url(node, is_end) + path = node.basepath + if !path.blank? + @home_base ||= begin + p = Zena::Use::Ancestry.basepath_from_fullpath(current_site.home_node.fullpath) + %r{^#{p}/?} end - end.compact.join('/') + path = path.sub(@home_base, '') + return '' if path.blank? + path = path.split('/').map do |zip| + if n = secure(Node) { Node.find_by_zip(zip) } + n.title.url_name + else + nil + end + end.compact.join('/') + end + if is_end + path + else + path.blank? ? '' : "#{path}/" + end end def append_query_params(path, opts) diff --git a/test/integration/zafu_compiler/alias_site.yml b/test/integration/zafu_compiler/alias_site.yml index 7e60ec89..a66d3fd4 100644 --- a/test/integration/zafu_compiler/alias_site.yml +++ b/test/integration/zafu_compiler/alias_site.yml @@ -8,10 +8,6 @@ default: tem: "/visitor.site.home_node" res: "alias.host: a wiki with Zena" -link_href_home: - src: "" - res: "status title" - test_home: context: node: wiki @@ -39,4 +35,18 @@ from_home: # For this test, we add some sub-nodes in wiki in_home: src: - res: bird, flower, one, three, two \ No newline at end of file + res: bird, flower, one, three, two + +link_to_home: + src: + res: a wiki with Zena + +link_with_custom_base: + # custom base is set on 'home' before this test + src: + res: bird three + +link_with_custom_base_out_of_home: + # custom base is set on 'home' before this test + src: + res: status title \ No newline at end of file diff --git a/test/integration/zafu_compiler_test.rb b/test/integration/zafu_compiler_test.rb index 8870faad..a961dd4c 100644 --- a/test/integration/zafu_compiler_test.rb +++ b/test/integration/zafu_compiler_test.rb @@ -398,5 +398,17 @@ def test_alias_site_in_home secure(Page) { Page.create(:title => 'three', :parent_id => sub.id)} yt_do_test('alias_site', 'in_home') end + + def test_alias_site_link_with_custom_base + login(:lion) + secure(Node) { nodes(:wiki) }.tap do |w| + w.update_attributes(:custom_base => true) + err w + end + node = secure(Page) { Page.create(:title => 'one', :parent_id => nodes_id(:wiki), :custom_base => true)} + sub = secure(Page) { Page.create(:title => 'two', :parent_id => node.id)} + secure(Page) { Page.create(:title => 'three', :parent_id => sub.id)} + yt_do_test('alias_site', 'link_with_custom_base') + end yt_make end \ No newline at end of file