From fa0cf9032fbbf7724dfef3fb7007189c28633050 Mon Sep 17 00:00:00 2001 From: Alexey Verkhovsky Date: Fri, 9 Sep 2005 03:31:49 +0000 Subject: [PATCH] Extracted link generation from Web into a separate class --- app/models/web.rb | 57 +++-------------------------------------- lib/url_generator.rb | 61 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 54 deletions(-) create mode 100644 lib/url_generator.rb diff --git a/app/models/web.rb b/app/models/web.rb index 13cb6f31..5a1bf180 100644 --- a/app/models/web.rb +++ b/app/models/web.rb @@ -46,24 +46,6 @@ class Web < ActiveRecord::Base read_attribute('markup').to_sym end - 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}" - else "#{text}" end - else - if has_file?(name) - "#{text}" - else - "#{text}?" - end - end - end - # 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. @@ -76,49 +58,16 @@ class Web < ActiveRecord::Base link_type = options[:link_type] || :show case link_type.to_sym 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 - make_file_link(mode, name, text, base_url) + UrlGenerator.new.make_file_link(mode, name, text, base_url, has_file?(name)) when :pic - make_pic_link(mode, name, text, base_url) + UrlGenerator.new.make_pic_link(mode, name, text, base_url, has_file?(name)) else raise "Unknown link type: #{link_type}" 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 %{#{text}} - else %{#{text}} end - when :publish - if has_page?(name) then %{#{text}} - else %{#{text}} end - else - if has_page?(name) - %{#{text}} - else - %{#{text}?} - 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 %{#{text}} - else %{#{text}} end - when :publish - if has_file?(name) then %{#{text}} - else %{#{text}} end - else - if has_file?(name) then %{#{text}} - else %{#{text}?} end - end - end - # Clears the display cache for all the pages with references to def refresh_pages_with_references(page_name) #select.pages_that_reference(page_name).each { |page| diff --git a/lib/url_generator.rb b/lib/url_generator.rb new file mode 100644 index 00000000..4fd1ff93 --- /dev/null +++ b/lib/url_generator.rb @@ -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 "#{text}" + else "#{text}" end + when :publish + if known_file then "#{text}" + else "#{text}" end + else + if known_file + "#{text}" + else + "#{text}?" + 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 %{#{text}} + else %{#{text}} end + when :publish + if known_page then %{#{text}} + else %{#{text}} end + else + if known_page + %{#{text}} + else + %{#{text}?} + 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 %{#{text}} + else %{#{text}} end + when :publish + if known_pic then %{#{text}} + else %{#{text}} end + else + if known_pic then %{#{text}} + else %{#{text}?} end + end + end + +end + +class ControllerStub +end \ No newline at end of file