Extracted link generation from Web into a separate class
This commit is contained in:
parent
829d54a368
commit
fa0cf9032f
|
@ -46,24 +46,6 @@ class Web < ActiveRecord::Base
|
||||||
read_attribute('markup').to_sym
|
read_attribute('markup').to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
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=\"#{base_url}/published/#{link}\">#{text}</a>"
|
|
||||||
else "<span class=\"newWikiWord\">#{text}</span>" end
|
|
||||||
else
|
|
||||||
if has_file?(name)
|
|
||||||
"<a class=\"existingWikiWord\" href=\"#{base_url}/file/#{link}\">#{text}</a>"
|
|
||||||
else
|
|
||||||
"<span class=\"newWikiWord\">#{text}<a href=\"#{base_url}/file/#{link}\">?</a></span>"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Create a link for the given page name and link text based
|
# Create a link for the given page name and link text based
|
||||||
# on the render mode in options and whether the page exists
|
# on the render mode in options and whether the page exists
|
||||||
# in the this web.
|
# in the this web.
|
||||||
|
@ -76,49 +58,16 @@ class Web < ActiveRecord::Base
|
||||||
link_type = options[:link_type] || :show
|
link_type = options[:link_type] || :show
|
||||||
case link_type.to_sym
|
case link_type.to_sym
|
||||||
when :show
|
when :show
|
||||||
make_page_link(mode, name, text, base_url)
|
UrlGenerator.new.make_page_link(mode, name, text, base_url, has_page?(name))
|
||||||
when :file
|
when :file
|
||||||
make_file_link(mode, name, text, base_url)
|
UrlGenerator.new.make_file_link(mode, name, text, base_url, has_file?(name))
|
||||||
when :pic
|
when :pic
|
||||||
make_pic_link(mode, name, text, base_url)
|
UrlGenerator.new.make_pic_link(mode, name, text, base_url, has_file?(name))
|
||||||
else
|
else
|
||||||
raise "Unknown link type: #{link_type}"
|
raise "Unknown link type: #{link_type}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_page_link(mode, name, text, base_url)
|
|
||||||
link = CGI.escape(name)
|
|
||||||
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="#{base_url}/published/#{link}">#{text}</a>}
|
|
||||||
else %{<span class="newWikiWord">#{text}</span>} end
|
|
||||||
else
|
|
||||||
if has_page?(name)
|
|
||||||
%{<a class="existingWikiWord" href="#{base_url}/show/#{link}">#{text}</a>}
|
|
||||||
else
|
|
||||||
%{<span class="newWikiWord">#{text}<a href="#{base_url}/show/#{link}">?</a></span>}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def make_pic_link(mode, name, text, base_url)
|
|
||||||
link = CGI.escape(name)
|
|
||||||
case mode.to_sym
|
|
||||||
when :export
|
|
||||||
if has_file?(name) then %{<img alt="#{text}" src="#{link}" />}
|
|
||||||
else %{<img alt="#{text}" src="no image" />} end
|
|
||||||
when :publish
|
|
||||||
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="#{base_url}/pic/#{link}" />}
|
|
||||||
else %{<span class="newWikiWord">#{text}<a href="#{base_url}/pic/#{link}">?</a></span>} end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Clears the display cache for all the pages with references to
|
# Clears the display cache for all the pages with references to
|
||||||
def refresh_pages_with_references(page_name)
|
def refresh_pages_with_references(page_name)
|
||||||
#select.pages_that_reference(page_name).each { |page|
|
#select.pages_that_reference(page_name).each { |page|
|
||||||
|
|
61
lib/url_generator.rb
Normal file
61
lib/url_generator.rb
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
class UrlGenerator
|
||||||
|
|
||||||
|
def initialize(controller = nil)
|
||||||
|
@controller = controller or ControllerStub.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def make_file_link(mode, name, text, base_url, known_file)
|
||||||
|
link = CGI.escape(name)
|
||||||
|
case mode
|
||||||
|
when :export
|
||||||
|
if known_file then "<a class=\"existingWikiWord\" href=\"#{link}.html\">#{text}</a>"
|
||||||
|
else "<span class=\"newWikiWord\">#{text}</span>" end
|
||||||
|
when :publish
|
||||||
|
if known_file then "<a class=\"existingWikiWord\" href=\"#{base_url}/published/#{link}\">#{text}</a>"
|
||||||
|
else "<span class=\"newWikiWord\">#{text}</span>" end
|
||||||
|
else
|
||||||
|
if known_file
|
||||||
|
"<a class=\"existingWikiWord\" href=\"#{base_url}/file/#{link}\">#{text}</a>"
|
||||||
|
else
|
||||||
|
"<span class=\"newWikiWord\">#{text}<a href=\"#{base_url}/file/#{link}\">?</a></span>"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def make_page_link(mode, name, text, base_url, known_page)
|
||||||
|
link = CGI.escape(name)
|
||||||
|
case mode.to_sym
|
||||||
|
when :export
|
||||||
|
if known_page then %{<a class="existingWikiWord" href="#{link}.html">#{text}</a>}
|
||||||
|
else %{<span class="newWikiWord">#{text}</span>} end
|
||||||
|
when :publish
|
||||||
|
if known_page then %{<a class="existingWikiWord" href="#{base_url}/published/#{link}">#{text}</a>}
|
||||||
|
else %{<span class="newWikiWord">#{text}</span>} end
|
||||||
|
else
|
||||||
|
if known_page
|
||||||
|
%{<a class="existingWikiWord" href="#{base_url}/show/#{link}">#{text}</a>}
|
||||||
|
else
|
||||||
|
%{<span class="newWikiWord">#{text}<a href="#{base_url}/show/#{link}">?</a></span>}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def make_pic_link(mode, name, text, base_url, known_pic)
|
||||||
|
link = CGI.escape(name)
|
||||||
|
case mode.to_sym
|
||||||
|
when :export
|
||||||
|
if known_pic then %{<img alt="#{text}" src="#{link}" />}
|
||||||
|
else %{<img alt="#{text}" src="no image" />} end
|
||||||
|
when :publish
|
||||||
|
if known_pic then %{<img alt="#{text}" src="#{link}" />}
|
||||||
|
else %{<span class="newWikiWord">#{text}</span>} end
|
||||||
|
else
|
||||||
|
if known_pic 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
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
class ControllerStub
|
||||||
|
end
|
Loading…
Reference in a new issue