# The methods added to this helper will be available to all templates in the application. module ApplicationHelper # Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container # where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and # the "firsts" as option text. Hashes are turned into this form automatically, so the keys become "firsts" and values # become lasts. If +selected+ is specified, the matching "last" or element will get the selected option-tag. # # Examples (call, result): # html_options([["Dollar", "$"], ["Kroner", "DKK"]]) # \n # # html_options([ "VISA", "Mastercard" ], "Mastercard") # \n # # html_options({ "Basic" => "$20", "Plus" => "$40" }, "$40") # \n def html_options(container, selected = nil) container = container.to_a if Hash === container html_options = container.inject([]) do |options, element| if element.is_a? Array if element.last != selected options << "" else options << "" end else options << ((element != selected) ? "" : "") end end html_options.join("\n") end # Creates a hyperlink to a Wiki page, without checking if the page exists or not def link_to_existing_page(page, text = nil, html_options = {}) link_to( text || page.plain_name, {:web => @web.address, :action => 'show', :id => page.name, :only_path => true}, html_options) end # Creates a hyperlink to a Wiki page, or to a "new page" form if the page doesn't exist yet def link_to_page(page_name, web = @web, text = nil, options = {}) raise 'Web not defined' if web.nil? UrlGenerator.new(@controller).make_link(@web, page_name, web, text, options.merge(:base_url => "#{base_url}/#{web.address}")) end def author_link(page, options = {}) UrlGenerator.new(@controller).make_link(@web, page.author.name, page.web, nil, options) end # Create a hyperlink to a particular revision of a Wiki page def link_to_revision(page, revision_number, text = nil, mode = nil, html_options = {}) revision_number == page.revisions.size ? link_to( text || page.plain_name, {:web => @web.address, :action => 'show', :id => page.name, :mode => mode}, html_options) : link_to( text || page.plain_name + "(rev # #{revision_number})", {:web => @web.address, :action => 'revision', :id => page.name, :rev => revision_number, :mode => mode}, html_options) end # Create a hyperlink to the history of a particular Wiki page def link_to_history(page, text = nil, html_options = {}) link_to( text || page.plain_name + "(history)", {:web => @web.address, :action => 'history', :id => page.name}, html_options) end def base_url home_page_url = url_for :controller => 'admin', :action => 'create_system', :only_path => true home_page_url.sub(%r-/create_system/?$-, '') end # Creates a menu of categories def categories_menu if @categories.empty? '' else "