<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>usefulfor.com/ruby &#187; Add new tag</title>
	<atom:link href="http://usefulfor.com/ruby/tag/add-new-tag/feed/" rel="self" type="application/rss+xml" />
	<link>http://usefulfor.com/ruby</link>
	<description>ruby goodness for your daily needs</description>
	<lastBuildDate>Mon, 07 Dec 2009 23:20:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Use Rails to Create a Static Site</title>
		<link>http://usefulfor.com/ruby/2009/02/04/use-rails-to-create-a-static-site/</link>
		<comments>http://usefulfor.com/ruby/2009/02/04/use-rails-to-create-a-static-site/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 12:00:16 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[dradis]]></category>
		<category><![CDATA[Add new tag]]></category>

		<guid isPermaLink="false">http://usefulfor.com/ruby/?p=75</guid>
		<description><![CDATA[One of the new things we released last week with dradis v2.0 was a new web site for the project (dradis.sourceforge.net).
The old site consisted of 20 static pages or so, which was nice and easy but a real pain to maintain or restructure. So we thought that letting Rails do the heavy lifting for us [...]]]></description>
			<content:encoded><![CDATA[<p>One of the new things we released last week with <a href="/security/2009/01/30/dradis-v2/">dradis v2.0</a> was a new web site for the project (<a href="http://dradis.sourceforge.net/">dradis.sourceforge.net</a>).</p>
<p>The old site consisted of 20 static pages or so, which was nice and easy but a real pain to maintain or restructure. So we thought that letting Rails do the heavy lifting for us would be a good idea, but we did not want to set up a Rail environment in the server&#8230;</p>
<p>What we finally did is use Rails as a tool to create a static site that we could <em>.tar.gz</em> and upload to the server. As a starting point we used a post in <a href="http://www.chuckvose.com/articles/2006/04/20/monkey">www.chuckvose.com</a> and this is how we completed it to fit our needs.</p>
<p><strong>Follow up</strong> (2009-03-23): do not miss how to integrate your rails-static site with Rake and Subversion in the second article of this series: <a href="http://usefulfor.com/ruby/2009/03/23/use-rails-to-create-a-static-site-rake-and-subversion/">Use Rails to Create a Static Site: Rake and Subversion</a>.</p>
<p><span id="more-75"></span><br />
<a name="thecontroller"></a></p>
<h3>The Controller</h3>
<p>We just need one action in our main controller (<code>PagesController</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> PagesController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  layout <span style="color:#996600;">'main'</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> index
    <span style="color:#0066ff; font-weight:bold;">@page</span> = params.<span style="color:#9900CC;">fetch</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:id</span>, <span style="color:#996600;">'index'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    expanded_page = <span style="color:#996600;">&quot;#{RAILS_ROOT}/app/views/pages/#{@page}.html.erb&quot;</span>
    exists = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span>expanded_page<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> exists
      render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@page</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      render <span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{expanded_page} doesn't exist&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>We <code>fetch</code> the <strong>:id</strong> parameter from the request that contains the name of the requested page (or <code>index</code> by default), then we try to locate the corresponding file (in <code>/app/views/pages/</code>) and if found we render the action. Rendering the action with no action code in the controller will just render it&#8217;s <a href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html">ERB</a> template which is exactly what we want.</p>
<p>We also added a couple of routes in <code>/config/routes.rb</code> so every time a something<strong>.html</strong> was requested, the <code>PagesController</code> would be invoked:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">map.<span style="color:#9900CC;">root</span> <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'pages'</span>
map.<span style="color:#9900CC;">connect</span> <span style="color:#996600;">':id.html'</span>, <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'pages'</span>, <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'index'</span></pre></div></div>

<h3>The Layout and the Pages</h3>
<p>The layout we used was quite simple. The most interesting bits follow.</p>
<p>First, we want each page to be able to set their own HTML title. This is accomplished by including the following code in the  layout:-</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&lt;head&gt;
  &lt;title&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#9966CC; font-weight:bold;">yield</span> <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/title&gt;
&lt;/head&gt;</pre></div></div>

<p>Then pages must include this line to set the contents of the <strong>:title</strong> variable:-</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#5A0A0A; font-weight:bold;">content_for</span> <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>Announcements - dradis<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Other than that, we used the standard <code>@content_for_layout</code> in the main section of the page. However, the site has two menus, one in the top and one in the right hand side. This exposes a new challenge because we need to create the structure of the page in a way that is simple enough to maintain and add new content in the future. We had to code some Rails helpers to aid us in this.</p>
<h3>The Helpers</h3>
<h4>The Top Menu</h4>
<p><img src="http://usefulfor.com/ruby/files/2009/02/dradis2_topmenu.png" alt="" width="409" height="35" class="aligncenter size-full wp-image-79" /></p>
<p>The Top menu bar consists only of the main sections of the site. In the layout we call the Helper straight away passing the current page (see <a href="#thecontroller">The Controller</a> above):</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&lt;div class=&quot;bar&quot;&gt;
&lt;ul&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= barmenu<span style="color:#006600; font-weight:bold;">&#40;</span>@page<span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/ul&gt;
&lt;/div&gt;</pre></div></div>

<p>The code of the helper in this instance is quite simple. The only intelligence we are adding is a check to see if the current page is one of the items in the menu, if it is, we add the CSS <strong>active</strong> class to the list item.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> barmenu<span style="color:#006600; font-weight:bold;">&#40;</span>page<span style="color:#006600; font-weight:bold;">&#41;</span>
    items = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'download'</span>, <span style="color:#996600;">'demo'</span>, <span style="color:#996600;">'screenshots'</span>, <span style="color:#996600;">'documentation'</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
    items.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
      active = <span style="color:#996600;">''</span>
      label = i.<span style="color:#9900CC;">capitalize</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>page == i<span style="color:#006600; font-weight:bold;">&#41;</span>
        active = <span style="color:#996600;">' class=&quot;active&quot;'</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        label = <span style="color:#996600;">&quot;&lt;a href=<span style="color:#000099;">\&quot;</span>#{i}.html<span style="color:#000099;">\&quot;</span>&gt;#{i.capitalize}&lt;/a&gt;&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#996600;">&quot;&lt;li#{active}&gt;#{label}&lt;/li&gt;&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h4>Right Hand Side Navigation</h4>
<p><img src="http://usefulfor.com/ruby/files/2009/02/dradis2_navigation.png" alt="" width="275" height="253" class="alignright size-full wp-image-80" /><br />
The navigation menu in the right side was a bit trickier. We had our pages organised in three different sections: <em>using dradis</em>, <em>developing dradis</em> and <em>support from</em>. </p>
<p>Each section contains a number of items, some of the items have our content, some are links to external sites. In addition to this it would be useful if we could nest subsections as shown in the screenshot of the right.</p>
<p>So we had to figure out a way of representing the structure of the menu. Probably the neatest solution would have been to create an HTML page that contained a series of nested <code>ul</code> and <code>li</code> elements and then make the helper parse that structure assign the <strong>active</strong> CSS class to the right element and display it. Or maybe using a fancy JavaScript trick to locate the active item and assign the CSS class. However, as often happens we needed a solution, and it had to be fast, so we opted for having the structure loaded in a ruby array with one <code>Hash</code> per section:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#ff6633; font-weight:bold;">$sections</span> = <span style="color:#006600; font-weight:bold;">&#91;</span>
  <span style="color:#006600; font-weight:bold;">&#123;</span> ... <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#008000; font-style:italic;"># using dradis</span>
  <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'developing dradis'</span>,
    <span style="color:#ff3333; font-weight:bold;">:pages</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>
      <span style="color:#006600; font-weight:bold;">&#123;</span>
        <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'info. for developers'</span>,
        <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'developers.html'</span>,
        <span style="color:#ff3333; font-weight:bold;">:pages</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:extensions</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>,
      <span style="color:#ff3333; font-weight:bold;">:roadmap</span>,
      <span style="color:#006600; font-weight:bold;">&#123;</span>
        <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:bug_tracker</span>,
        <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://sourceforge.net/tracker/?atid=1010917&amp;group_id=209736&amp;func=browse'</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>,
      <span style="color:#006600; font-weight:bold;">&#123;</span>
        <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:feature_requests</span>,
        <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://sourceforge.net/tracker/?atid=1010920&amp;group_id=209736&amp;func=browse'</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>,
      <span style="color:#006600; font-weight:bold;">&#123;</span>
        <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:subversion</span>,
        <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://sourceforge.net/svn/?group_id=209736'</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#006600; font-weight:bold;">&#123;</span> ... <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;"># support from</span>
<span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Each of the three section <code>Hash</code>es has a <code>:title</code> and a <code>:pages</code> attribute. The latter is an array containing the information of the pages associated with the parent section. The elements of the array can be either <code>Symbol</code>s or new <code>Hash</code>es that describe the different pages.</p>
<p>We use a <code>Symbol</code> if the content of the page is provided by the site such as in the <strong>:roadmap</strong> case whose contents are in <code>/app/views/pages/roadmap.html.erb</code> and the link displayed in the menu would be &#8220;roadmap&#8221;. In more complex cases (i.e. we want a custom title for the link or a section that contains subsections) we use a <code>Hash</code>.</p>
<p>When a <code>Hash</code> is used at least a <strong>:title</strong> and a <strong>:url</strong> elements must be provided. Optionally a <strong>:pages</strong> element can be used to nest sections inside sections. The format of this <strong>:pages</strong> value is again an array as the one used in the top level section. So there you have your recursion!</p>
<p>The helper in this case is a bit more complex. First, the call in the layout, nothing fancy here:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&lt;div&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= rightmenu<span style="color:#006600; font-weight:bold;">&#40;</span>@page<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/div&gt;</pre></div></div>

<p>The <code>rightmenu()</code> helper code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> rightmenu<span style="color:#006600; font-weight:bold;">&#40;</span>page<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#ff6633; font-weight:bold;">$sections</span>.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>section<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#996600;">&quot;&lt;h3&gt;#{section[:title]}:&lt;/h3&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> sectionmenu<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> page, <span style="color:#ff3333; font-weight:bold;">:section</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> section<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Each main section is enclosed in <code>h3</code> tags and then <code>sectionmenu</code> (a helper-to-the-helper method <img src='http://usefulfor.com/ruby/wp-includes/images/smilies/icon_rolleyes.gif' alt=':roll:' class='wp-smiley' />  ) is invoked for each section. There are some tricks going on in the following piece of code, have a look and we will nail it down later:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> sectionmenu<span style="color:#006600; font-weight:bold;">&#40;</span>options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    current_page = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:page</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    section = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:section</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    level = options.<span style="color:#9900CC;">fetch</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:level</span>, <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    css = options.<span style="color:#9900CC;">fetch</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:css</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'right_articles'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    css <span style="color:#006600; font-weight:bold;">|</span>= <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;submenu#{level}&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>level <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    section<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:pages</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>page<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#008000; font-style:italic;"># If the last item was the active one, clear the flag</span>
      css.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'active'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> css.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'active'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      title = <span style="color:#996600;">''</span>
      url = <span style="color:#996600;">''</span>
      complex = <span style="color:#0000FF; font-weight:bold;">false</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># simple page (Symbol) vs. complex page (Hash)</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>page.<span style="color:#9966CC; font-weight:bold;">class</span> == <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        complex = <span style="color:#0000FF; font-weight:bold;">true</span>
        title = page<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:title</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>_<span style="color:#006600; font-weight:bold;">/</span>,<span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        url = page<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:url</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        title = page.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>_<span style="color:#006600; font-weight:bold;">/</span>,<span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        url = page.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'.html'</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># active vs. inactive</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>current_page <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'.html'</span> == url<span style="color:#006600; font-weight:bold;">&#41;</span>
        css <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">'active'</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        title = <span style="color:#996600;">&quot;&lt;a href=<span style="color:#000099;">\&quot;</span>#{url}<span style="color:#000099;">\&quot;</span>&gt;#{title}&lt;/a&gt;&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># subsections</span>
      <span style="color:#008000; font-style:italic;"># tradeoff: calculate the sub section,</span>
      <span style="color:#008000; font-style:italic;"># if it doesn't contain the &quot;active&quot; page, don't use it</span>
      subsection = <span style="color:#996600;">''</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>complex <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> page.<span style="color:#9900CC;">key</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:pages</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        subsection =  sectionmenu<span style="color:#006600; font-weight:bold;">&#40;</span>
            <span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> current_page,
            <span style="color:#ff3333; font-weight:bold;">:section</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> page,
            <span style="color:#ff3333; font-weight:bold;">:level</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> level <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
        <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
        found = !<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>active<span style="color:#006600; font-weight:bold;">/</span> =~ subsection<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span> !found <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> !css.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'active'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
          subsection = <span style="color:#996600;">''</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#996600;">&quot;&lt;div class=<span style="color:#000099;">\&quot;</span>#{css.join(' ')}<span style="color:#000099;">\&quot;</span>&gt;#{title}&lt;/div&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> subsection
    <span style="color:#9966CC; font-weight:bold;">end</span>.<span style="color:#9900CC;">join</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>In lines 2 to 6 we initialise the properties for this section. Then for each page in the section we apply the loop in 8 to 51. Due to the way the data is structured in the <code>$sections</code> variable, this code can be used to recursively crawl the tree of sections and subsections.</p>
<p>For each page in the section:</p>
<ul>
<li>we initialise the internal variables (9 to 13). If the page is a <code>Hash</code> we extract the title and URL for the link from it&#8217;s elements, if it is a <code>Symbol</code> we <em>infer</em> the title and link from the symbol name.</li>
<li>Then we check if the current page in the iteration is the active one (26 to 30). If it is, we add a CSS attribute to the item, if it is not, then we add a hyper-link to it (it would not make sense to have a link to the page if you are already in it).</li>
<li>Finally in 36 to 47 we check if there is a <strong>:pages</strong> element in the <code>Hash</code> and if there is one, we make a recursive call to render the subsection.</li>
</ul>
<p>Two things need to be said about the above piece of code. First, each subsection will be assigned a CSS class in accordance to it&#8217;s level: <code>submenu1</code>, <code>submenu2</code>, etc. This will allow us to style the different subsections appropriately. And second, because there is no easy way of knowing if the active belongs to one of the sub sections of the current section we make the recursive call, and then see if we have found the active page. If we have not, we discard the subsection altogether. We want this behaviour of not displaying a subsection in the menu unless the user is already in that section and this was the easiest way of implementing it. </p>
<h4>The Bread Crumbs</h4>
<p><img src="http://usefulfor.com/ruby/files/2009/02/dradis2_breadcrums.png" alt="" width="295" height="94" class="alignright size-full wp-image-81" /></p>
<p>The bread crumbs help our users to know where they are in the site (see picture in the right sid). We need to have a flexible helper that would display the path the user has followed to get into the page they are at the moment. Below is the code of the layout, just above the main content and with a check to verify that we are not in the main page:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&lt;div class=&quot;left&quot;&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@page</span> != <span style="color:#996600;">'index'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;div id=&quot;breadcrums&quot;&gt;
    &lt;ul&gt;
      <span style="color:#006600; font-weight:bold;">&lt;%</span>= breadcrumsmenu<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@page</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    &lt;/ul&gt;
  &lt;/div&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@content_for_layout</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/div&gt;</pre></div></div>

<p>And again, this helper will use the <code>$sections</code> as discussed above.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> breadcrumsmenu<span style="color:#006600; font-weight:bold;">&#40;</span>options=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    crums = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#ff6633; font-weight:bold;">$sections</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>section<span style="color:#006600; font-weight:bold;">|</span>
      options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:section</span><span style="color:#006600; font-weight:bold;">&#93;</span> = section
      crums = breadcrums<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">break</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>crums.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    this_page = crums.<span style="color:#9900CC;">pop</span>
    <span style="color:#006600; font-weight:bold;">&#40;</span>crums.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;&lt;li&gt;&lt;a href=<span style="color:#000099;">\&quot;</span>#{c[:url]}<span style="color:#000099;">\&quot;</span>&gt;#{c[:title]}&lt;/a&gt;&amp;lt/li&amp;gt&quot;</span>  <span style="color:#9966CC; font-weight:bold;">end</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">join</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;&lt;li&gt;#{this_page[:title]}&lt;/li&gt;&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Again a similar tradeoff here, we cycle through all the sections trying to find the <strong>active</strong> page. We use the <code>breadcrums</code> helper-to-the-helper function to find out if the page is in the given section and then we render the bread crumbs. I realise that the two helper-to-the-helper functions are quite similar (cycle through a section trying to locate the active page), and possibly we could refactor both into a single function that does thing. But then again, once it worked, we did not have too much spare time to look into it, so we may improve it in the future, but this is the solution we came up with at that point. The code of <code>breadcrums()</code> is very similar in structure to the code in <code>sectionmenu()</code> but this time, it returns an array of pages that will lead us to the active one:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> breadcrums<span style="color:#006600; font-weight:bold;">&#40;</span>options=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    current_page = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:page</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    section = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:section</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    crums = options.<span style="color:#9900CC;">fetch</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:crums</span>, <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:title <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'home'</span>, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'/'</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    section<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:pages</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>page<span style="color:#006600; font-weight:bold;">|</span>
      title = <span style="color:#996600;">''</span>
      url = <span style="color:#996600;">''</span>
      complex = <span style="color:#0000FF; font-weight:bold;">false</span>
      found = <span style="color:#0000FF; font-weight:bold;">false</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>page.<span style="color:#9966CC; font-weight:bold;">class</span> == <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        complex = <span style="color:#0000FF; font-weight:bold;">true</span>
        title = page<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:title</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>_<span style="color:#006600; font-weight:bold;">/</span>,<span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        url = page<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:url</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        title = page.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>_<span style="color:#006600; font-weight:bold;">/</span>,<span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        url = page.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'.html'</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>current_page <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'.html'</span> == url<span style="color:#006600; font-weight:bold;">&#41;</span>
        found = <span style="color:#0000FF; font-weight:bold;">true</span>
        crums <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> title, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> url <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>!found <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> complex <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> page.<span style="color:#9900CC;">key</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:pages</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        subsectioncrums = breadcrums<span style="color:#006600; font-weight:bold;">&#40;</span>
          <span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> current_page,
          <span style="color:#ff3333; font-weight:bold;">:section</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> page,
          <span style="color:#ff3333; font-weight:bold;">:crums</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:title <span style="color:#006600; font-weight:bold;">=&gt;</span> title, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> url<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        <span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>subsectioncrums.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          crums <span style="color:#006600; font-weight:bold;">+</span>= subsectioncrums
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">break</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>crums.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> crums
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>Finishing Touch</h3>
<p>Finally, to generate the static pages that we could compress and upload to the server, some good old <a href="http://www.gnu.org/software/wget/">wget</a> magic was used:</p>
<div class="hl-surround" ><div class="hl-main"><pre>&lt;br /&gt;
wget -m -nH http://localhost:3000/&lt;br /&gt;</pre></div></div></p>
<img src="http://usefulfor.com/ruby/?ak_action=api_record_view&id=75&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://usefulfor.com/ruby/2009/02/04/use-rails-to-create-a-static-site/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
