Rehashed URL generation once again - templates should not use web.make_link anymore, there is link_for_page helper instead
This commit is contained in:
parent
fd1d0ccc1e
commit
9c04ed3461
|
@ -33,4 +33,11 @@ module ApplicationHelper
|
|||
html_options.join("\n")
|
||||
end
|
||||
|
||||
def link_to_page(page_name, web = @web, text = nil, options = {})
|
||||
raise 'Web not defined' if web.nil?
|
||||
home_page_url = url_for :web => web.address, :action => 'show', :id => 'HomePage', :only_path => true
|
||||
base_url = home_page_url.sub(%r-/show/HomePage/?$-, '')
|
||||
web.make_link(page_name, text, options.merge(:base_url => base_url))
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -15,7 +15,7 @@ module WikiChunk
|
|||
|
||||
def initialize(*args)
|
||||
super
|
||||
@link_type = 'show'
|
||||
@link_type = :show
|
||||
end
|
||||
|
||||
def self.apply_to(content)
|
||||
|
@ -124,7 +124,7 @@ module WikiChunk
|
|||
link_type_match = LINK_TYPE_SEPARATION.match(@page_name)
|
||||
if link_type_match
|
||||
@link_text = @page_name = link_type_match[1]
|
||||
@link_type = link_type_match[2..3].compact[0]
|
||||
@link_type = link_type_match[2..3].compact[0].to_sym
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
module PageLock
|
||||
LOCKING_PERIOD = 30 * 60 # 30 minutes
|
||||
|
||||
attr_reader :locked_by
|
||||
|
||||
def lock(time, locked_by)
|
||||
@locked_at, @locked_by = time, locked_by
|
||||
end
|
||||
|
@ -18,7 +20,4 @@ module PageLock
|
|||
@locked_at + LOCKING_PERIOD > comparison_time unless @locked_at.nil?
|
||||
end
|
||||
|
||||
def locked_by_link
|
||||
web.make_link(@locked_by)
|
||||
end
|
||||
end
|
|
@ -46,20 +46,20 @@ class Web
|
|||
wiki.file_yard(self).has_file?(name)
|
||||
end
|
||||
|
||||
def make_file_link(mode, name, text)
|
||||
def make_file_link(mode, name, text, base_url)
|
||||
link = CGI.escape(name)
|
||||
case mode
|
||||
when :export
|
||||
if has_file?(name) then "<a class=\"existingWikiWord\" href=\"#{link}.html\">#{text}</a>"
|
||||
else "<span class=\"newWikiWord\">#{text}</span>" end
|
||||
when :publish
|
||||
if has_file?(name) then "<a class=\"existingWikiWord\" href=\"../published/#{link}\">#{text}</a>"
|
||||
if has_file?(name) then "<a class=\"existingWikiWord\" href=\"#{base_url}/published/#{link}\">#{text}</a>"
|
||||
else "<span class=\"newWikiWord\">#{text}</span>" end
|
||||
else
|
||||
if has_file?(name)
|
||||
"<a class=\"existingWikiWord\" href=\"../file/#{link}\">#{text}</a>"
|
||||
"<a class=\"existingWikiWord\" href=\"#{base_url}/file/#{link}\">#{text}</a>"
|
||||
else
|
||||
"<span class=\"newWikiWord\">#{text}<a href=\"../file/#{link}\">?</a></span>"
|
||||
"<span class=\"newWikiWord\">#{text}<a href=\"#{base_url}/file/#{link}\">?</a></span>"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -67,43 +67,46 @@ class Web
|
|||
# Create a link for the given page name and link text based
|
||||
# on the render mode in options and whether the page exists
|
||||
# in the this web.
|
||||
# The links a relative, and will work only if displayed on another WikiPage.
|
||||
# It should not be used in menus, templates and such - instead, use link_to_page helper
|
||||
def make_link(name, text = nil, options = {})
|
||||
text = CGI.escapeHTML(text || WikiWords.separate(name))
|
||||
mode = options[:mode]
|
||||
link_type = options[:link_type] || 'show'
|
||||
case link_type
|
||||
when 'show'
|
||||
make_page_link(mode, name, text)
|
||||
when 'file'
|
||||
make_file_link(mode, name, text)
|
||||
when 'pic'
|
||||
make_pic_link(mode, name, text)
|
||||
mode = options[:mode] || :show
|
||||
base_url = options[:base_url] || '..'
|
||||
link_type = options[:link_type] || :show
|
||||
case link_type.to_sym
|
||||
when :show
|
||||
make_page_link(mode, name, text, base_url)
|
||||
when :file
|
||||
make_file_link(mode, name, text, base_url)
|
||||
when :pic
|
||||
make_pic_link(mode, name, text, base_url)
|
||||
else
|
||||
raise "Unknown link type: #{link_type}"
|
||||
end
|
||||
end
|
||||
|
||||
def make_page_link(mode, name, text)
|
||||
def make_page_link(mode, name, text, base_url)
|
||||
link = CGI.escape(name)
|
||||
case mode
|
||||
case mode.to_sym
|
||||
when :export
|
||||
if has_page?(name) then %{<a class="existingWikiWord" href="#{link}.html">#{text}</a>}
|
||||
else %{<span class="newWikiWord">#{text}</span>} end
|
||||
when :publish
|
||||
if has_page?(name) then %{<a class="existingWikiWord" href="../published/#{link}">#{text}</a>}
|
||||
if has_page?(name) then %{<a class="existingWikiWord" href="#{base_url}/published/#{link}">#{text}</a>}
|
||||
else %{<span class="newWikiWord">#{text}</span>} end
|
||||
else
|
||||
if has_page?(name)
|
||||
%{<a class="existingWikiWord" href="../show/#{link}">#{text}</a>}
|
||||
%{<a class="existingWikiWord" href="#{base_url}/show/#{link}">#{text}</a>}
|
||||
else
|
||||
%{<span class="newWikiWord">#{text}<a href="../show/#{link}">?</a></span>}
|
||||
%{<span class="newWikiWord">#{text}<a href="#{base_url}/show/#{link}">?</a></span>}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_pic_link(mode, name, text)
|
||||
def make_pic_link(mode, name, text, base_url)
|
||||
link = CGI.escape(name)
|
||||
case mode
|
||||
case mode.to_sym
|
||||
when :export
|
||||
if has_file?(name) then %{<img alt="#{text}" src="#{link}" />}
|
||||
else %{<img alt="#{text}" src="no image" />} end
|
||||
|
@ -111,8 +114,8 @@ class Web
|
|||
if has_file?(name) then %{<img alt="#{text}" src="#{link}" />}
|
||||
else %{<span class="newWikiWord">#{text}</span>} end
|
||||
else
|
||||
if has_file?(name) then %{<img alt="#{text}" src="../pic/#{link}" />}
|
||||
else %{<span class="newWikiWord">#{text}<a href="../pic/#{link}">?</a></span>} end
|
||||
if has_file?(name) then %{<img alt="#{text}" src="#{base_url}/pic/#{link}" />}
|
||||
else %{<span class="newWikiWord">#{text}<a href="#{base_url}/pic/#{link}">?</a></span>} end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ require 'chunks/nowiki'
|
|||
# engine. By default these are:
|
||||
# Literal::Pre, Literal::Tags
|
||||
# * :mode
|
||||
# => How should the content be rendered? For normal display (:display),
|
||||
# => How should the content be rendered? For normal display (show),
|
||||
# publishing (:publish) or export (:export)?
|
||||
#
|
||||
# AUTHOR: Mark Reid <mark @ threewordslong . com>
|
||||
|
@ -47,7 +47,7 @@ class WikiContent < String
|
|||
:post_engine_actions => POST_ENGINE_ACTIONS,
|
||||
:engine => Engines::Textile,
|
||||
:engine_opts => [],
|
||||
:mode => [:display]
|
||||
:mode => :show
|
||||
}
|
||||
|
||||
attr_reader :web, :options, :rendered, :chunks
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
<ul id="authorList">
|
||||
<% for author in @authors %>
|
||||
<li>
|
||||
<%= @web.make_link(author) %>
|
||||
<%= link_to_page author %>
|
||||
co- or authored:
|
||||
<%= @web.select.pages_authored_by(author).collect { |page| page.link }.join ', ' %>
|
||||
<%= @web.select.pages_authored_by(author).collect { |page| link_to_page(page.name) }.join ', ' %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
<% @title = "#{@page.plain_name} is locked" %>
|
||||
|
||||
<% if @page.lock_duration(Time.now) == 0 %>
|
||||
<p><%= @page.locked_by_link %> just started editing this page.</p>
|
||||
<% else %>
|
||||
<p><%= @page.locked_by_link %> has been editing this page for <%= @page.lock_duration(Time.now) %> minutes.</p>
|
||||
<% end %>
|
||||
<p>
|
||||
<%= link_to_page(@page.locked_by) %>
|
||||
<% if @page.lock_duration(Time.now) == 0 %>
|
||||
just started editing this page.
|
||||
<% else %>
|
||||
has been editing this page for <%= @page.lock_duration(Time.now) %> minutes.
|
||||
<% end %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= link_to 'Edit the page anyway',
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
<div class="byline">
|
||||
<%= "Revision from #{@revision.pretty_created_at} by" %>
|
||||
<%= @page.web.make_link(@revision.author) %>
|
||||
<%= link_to_page @revision.author %>
|
||||
</div>
|
||||
|
||||
<div class="navigation">
|
||||
|
|
|
@ -4,15 +4,15 @@
|
|||
<% for web in @webs %>
|
||||
<li>
|
||||
<% if web.published %>
|
||||
<%= web.make_link 'HomePage', web.name, :mode => :publish %> (read-only) /
|
||||
<%= web.make_link 'HomePage', 'editable version', :mode => :edit %> (requires login)
|
||||
<%= link_to_page 'HomePage', web, web.name, :mode => 'publish' %>
|
||||
(read-only) /
|
||||
<%= link_to_page 'HomePage', web, 'editable version', :mode => 'show' %> (requires login)
|
||||
<% else %>
|
||||
<%= web.make_link 'HomePage', nil, :mode => :edit %>
|
||||
<%= link_to_page 'HomePage', web, web.name, :mode => 'show' %>
|
||||
<% end %>
|
||||
<div class="byline" style="margin-bottom: 0px">
|
||||
<%= web.pages.length %> pages by <%= web.authors.length %> authors
|
||||
</div>
|
||||
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
|
|
@ -44,26 +44,26 @@ class WikiTest < Test::Unit::TestCase
|
|||
|
||||
def test_file_types
|
||||
# only link
|
||||
assert_link_parsed_as 'only text', 'only text', 'show', '[[only text]]'
|
||||
assert_link_parsed_as 'only text', 'only text', :show, '[[only text]]'
|
||||
# link and text
|
||||
assert_link_parsed_as 'page name', 'link text', 'show', '[[page name|link text]]'
|
||||
assert_link_parsed_as 'page name', 'link text', :show, '[[page name|link text]]'
|
||||
# link and type (file)
|
||||
assert_link_parsed_as 'foo.tar.gz', 'foo.tar.gz', 'file', '[[foo.tar.gz:file]]'
|
||||
assert_link_parsed_as 'foo.tar.gz', 'foo.tar.gz', :file, '[[foo.tar.gz:file]]'
|
||||
# link and type (pic)
|
||||
assert_link_parsed_as 'foo.tar.gz', 'foo.tar.gz', 'pic', '[[foo.tar.gz:pic]]'
|
||||
assert_link_parsed_as 'foo.tar.gz', 'foo.tar.gz', :pic, '[[foo.tar.gz:pic]]'
|
||||
# link, text and type
|
||||
assert_link_parsed_as 'foo.tar.gz', 'FooTar', 'file', '[[foo.tar.gz|FooTar:file]]'
|
||||
assert_link_parsed_as 'foo.tar.gz', 'FooTar', :file, '[[foo.tar.gz|FooTar:file]]'
|
||||
|
||||
# NEGATIVE TEST CASES
|
||||
|
||||
# empty page name
|
||||
assert_link_parsed_as '|link text?', '|link text?', 'file', '[[|link text?:file]]'
|
||||
assert_link_parsed_as '|link text?', '|link text?', :file, '[[|link text?:file]]'
|
||||
# empty link text
|
||||
assert_link_parsed_as 'page name?|', 'page name?|', 'file', '[[page name?|:file]]'
|
||||
assert_link_parsed_as 'page name?|', 'page name?|', :file, '[[page name?|:file]]'
|
||||
# empty link type
|
||||
assert_link_parsed_as 'page name', 'link?:', 'show', '[[page name|link?:]]'
|
||||
assert_link_parsed_as 'page name', 'link?:', :show, '[[page name|link?:]]'
|
||||
# unknown link type
|
||||
assert_link_parsed_as 'page name:create_system', 'page name:create_system', 'show',
|
||||
assert_link_parsed_as 'page name:create_system', 'page name:create_system', :show,
|
||||
'[[page name:create_system]]'
|
||||
end
|
||||
|
||||
|
@ -78,4 +78,3 @@ class WikiTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue