From 9c04ed3461c1fd2fa63117e5af505d88251067ef Mon Sep 17 00:00:00 2001 From: Alexey Verkhovsky Date: Sun, 13 Feb 2005 18:53:49 +0000 Subject: [PATCH] Rehashed URL generation once again - templates should not use web.make_link anymore, there is link_for_page helper instead --- app/helpers/application_helper.rb | 7 +++++ app/models/chunks/wiki.rb | 4 +-- app/models/page_lock.rb | 5 ++-- app/models/web.rb | 47 ++++++++++++++++--------------- app/models/wiki_content.rb | 4 +-- app/views/wiki/authors.rhtml | 4 +-- app/views/wiki/locked.rhtml | 13 +++++---- app/views/wiki/revision.rhtml | 2 +- app/views/wiki/web_list.rhtml | 8 +++--- test/unit/chunks/wiki_test.rb | 19 ++++++------- 10 files changed, 62 insertions(+), 51 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9956a1cd..c8d597ac 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/models/chunks/wiki.rb b/app/models/chunks/wiki.rb index c277162d..c1e6491e 100644 --- a/app/models/chunks/wiki.rb +++ b/app/models/chunks/wiki.rb @@ -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 diff --git a/app/models/page_lock.rb b/app/models/page_lock.rb index 244814af..276274e6 100644 --- a/app/models/page_lock.rb +++ b/app/models/page_lock.rb @@ -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 \ No newline at end of file diff --git a/app/models/web.rb b/app/models/web.rb index 03412e46..453f84cb 100644 --- a/app/models/web.rb +++ b/app/models/web.rb @@ -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 "#{text}" else "#{text}" end when :publish - if has_file?(name) then "#{text}" + if has_file?(name) then "#{text}" else "#{text}" end else if has_file?(name) - "#{text}" + "#{text}" else - "#{text}?" + "#{text}?" 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 %{#{text}} else %{#{text}} end when :publish - if has_page?(name) then %{#{text}} + if has_page?(name) then %{#{text}} else %{#{text}} end else if has_page?(name) - %{#{text}} + %{#{text}} else - %{#{text}?} + %{#{text}?} 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 %{#{text}} else %{#{text}} end @@ -111,8 +114,8 @@ class Web if has_file?(name) then %{#{text}} else %{#{text}} end else - if has_file?(name) then %{#{text}} - else %{#{text}?} end + if has_file?(name) then %{#{text}} + else %{#{text}?} end end end diff --git a/app/models/wiki_content.rb b/app/models/wiki_content.rb index 9873ce9e..1b2cfe81 100644 --- a/app/models/wiki_content.rb +++ b/app/models/wiki_content.rb @@ -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 @@ -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 diff --git a/app/views/wiki/authors.rhtml b/app/views/wiki/authors.rhtml index 95c2d924..0a2942a7 100644 --- a/app/views/wiki/authors.rhtml +++ b/app/views/wiki/authors.rhtml @@ -3,9 +3,9 @@ diff --git a/app/views/wiki/locked.rhtml b/app/views/wiki/locked.rhtml index c878233f..cdcc12eb 100644 --- a/app/views/wiki/locked.rhtml +++ b/app/views/wiki/locked.rhtml @@ -1,10 +1,13 @@ <% @title = "#{@page.plain_name} is locked" %> -<% if @page.lock_duration(Time.now) == 0 %> -

<%= @page.locked_by_link %> just started editing this page.

-<% else %> -

<%= @page.locked_by_link %> has been editing this page for <%= @page.lock_duration(Time.now) %> minutes.

-<% end %> +

+ <%= 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 %> +

<%= link_to 'Edit the page anyway', diff --git a/app/views/wiki/revision.rhtml b/app/views/wiki/revision.rhtml index 1819e37e..725a6e7b 100644 --- a/app/views/wiki/revision.rhtml +++ b/app/views/wiki/revision.rhtml @@ -18,7 +18,7 @@