<?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>A Real American Hero &#187; Ruby on Rails</title>
	<atom:link href="http://www.erikjacobs.com/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.erikjacobs.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 06 May 2010 03:17:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Manipulating links with HTML select and jQuery</title>
		<link>http://www.erikjacobs.com/2010/01/04/manipulating-links-with-html-select-and-jquery/</link>
		<comments>http://www.erikjacobs.com/2010/01/04/manipulating-links-with-html-select-and-jquery/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 00:27:41 +0000</pubDate>
		<dc:creator>Erik Jacobs</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.erikjacobs.com/?p=114</guid>
		<description><![CDATA[As with most web projects, there's always some little new glitch that pops up. We've been building and massaging our own analytics back end for Riding Resource for some time now, and the change of year from 2009 to 2010 brought some new quirks that had to be dealt with.]]></description>
			<content:encoded><![CDATA[<p>As with most web projects, there&#8217;s always some little new glitch that pops up. We&#8217;ve been building and massaging our own analytics back end for <a title="Riding Resource - A Stable Place to Search" href="http://www.ridingresource.com" target="_blank">Riding Resource</a> for some time now, and the change of year from 2009 to 2010 brought some new quirks that had to be dealt with.</p>
<p>While there were some minor issues related to year/day calculations creating invalid dates, the bigger issue that (I think) was solved rather elegantly was choosing, via a select tag, which year&#8217;s analytics report to generate. <a title="jQuery" href="http://jQuery.com" target="_blank">jQuery</a> came to the rescue, with what turned out to be a far more simple solution than I originally had envisioned.</p>
<p>Select tags are not exactly the most complicated things in the world. But when you don&#8217;t have a form to go with them, it&#8217;s hard sometimes to figure out how to make them be useful. Instead of having a link for every year&#8217;s report, I figured a nice little drop-down would be an elegant way to choose. But this is where the difficulty was. I wanted a single text link to the report, but I wanted to change what that link actually&#8230; linked to&#8230; based on the selection in the box.</p>
<p>Here&#8217;s some of the original HTML that was generated by the app:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;union-county-veterinary-clinic-target&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/analytics/show2/union-county-veterinary-clinic.pdf&quot;</span>&gt;</span>PDF<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;union-county-veterinary-clinic-select&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;date[year]&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2009&quot;</span>&gt;</span>2009<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">selected</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;selected&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2010&quot;</span>&gt;</span>2010<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></pre></div></div>

<p>Here is where jQuery came to the rescue, and some generous fellow with the handle of &#8220;kit10&#8243; on the #jquery channel on IRC on <a title="Freenode IRC network" href="http://freenode.net/" target="_blank">Freenode</a>. kit10 suggested that I add a &#8220;rel&#8221; attribute to the select element, and give that &#8220;rel&#8221; attribute the value of the id of the PDF link. In this way, jQuery could look at the select element, and, when it changed, update the link. After a few machinations, here&#8217;s what popped out:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// courtesy of kit10 on #jquery on freenode</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;select[id$='-select']&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> target <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rel'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// set the target to be the value of the rel of the selector</span>
        <span style="color: #003366; font-weight: bold;">var</span> regex <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/year=\d\d\d\d/</span>
        target.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #339933;">,</span>target.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>regex<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;year=&quot;</span><span style="color: #339933;">+</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To read this code in a sense of plain english:<br />
Whenever a select element that contains &#8216;-select&#8217; in the ID changes<br />
Create a variable called target, and assign it the value of the rel attribute of the select element<br />
Replace the href attribute of the target with the new year by using the regex of /year=\d\d\d\d/ (to match year=2009, year=2010, etc)</p>
<p>That was really all there is to it. The new HTML ended up looking like as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1000-acres-ranch-target&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/analytics/show2/1000-acres-ranch.pdf?year=2009&quot;</span>&gt;</span>PDF<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1000-acres-ranch-select&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;date[year]&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2009&quot;</span>&gt;</span>2009<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">selected</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;selected&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2010&quot;</span>&gt;</span>2010<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.erikjacobs.com/2010/01/04/manipulating-links-with-html-select-and-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Git socket timeout issues with CentOS</title>
		<link>http://www.erikjacobs.com/2009/09/13/git-socket-timeout-issues-with-centos/</link>
		<comments>http://www.erikjacobs.com/2009/09/13/git-socket-timeout-issues-with-centos/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 19:50:44 +0000</pubDate>
		<dc:creator>Erik Jacobs</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.erikjacobs.com/?p=104</guid>
		<description><![CDATA[RidingResource.com was developed in Ruby on Rails, as many of you may know.  At some point this year I made the switch from using a local Subversion source control system to using Github, which has been pretty good.  The one pain I was having, which I thought was a CentOS 4 / libcurl issue, actually turned out to be the APF firewall that Wiredtree uses on all of their VPS.]]></description>
			<content:encoded><![CDATA[<p>So <a title="Riding Resource - A Stable Place to Search" href="http://www.ridingresource.com" target="_blank">Riding Resource</a> was developed in <a title="Ruby on Rails" href="http://www.rubyonrails.org" target="_blank">Ruby on Rails</a>, as many of you may know.  At some point this year I made the switch from using a local <a title="Subversion - An open source version control system" href="http://subversion.tigris.org/" target="_blank">Subversion</a> source control system to using <a title="git - Fast Version Control System" href="http://git-scm.com/" target="_blank">git</a> with <a title="Github - Social Coding" href="http://www.github.com/" target="_blank">Github</a>, which has been pretty good.  The one pain I was having, which I thought was a <a title="CentOS - The Community ENterprise Operating System" href="http://www.centos.org/" target="_blank">CentOS</a> 4 / libcurl issue, actually turned out to be the <a title="APF - Advanced Policy Firewall" href="http://www.rfxn.com/?page_id=6" target="_blank">APF</a> firewall that <a title="Wiredtree" href="http://www.wiredtree.com" target="_blank">Wiredtree </a>uses on all of their VPS.</p>
<p>APF is a pretty neat firewall.  It does a lot of neat things.  It&#8217;s also installed by default on the VPS we use from Wiredtree.  When I would pull or clone our private repository for <a title="Riding Resource - A Stable Place to Search" href="http://www.ridingresource.com/" target="_blank">Riding Resource</a>, I had no issues with git.  However, trying to clone public repositories I would always be greeted with something like:</p>
<pre>fatal: unable to connect a socket (Connection timed out)</pre>
<p>It took me a little while to figure out what was going on here, but what I tracked it down to was actually a firewall issue, and not any kind of issue with git or <a title="Github - Social Coding" href="http://www.github.com/" target="_blank">Github</a>.  I don&#8217;t know how I figured it out, but I discovered that cloning with <a title="Github - Social Coding" href="http://www.github.com/" target="_blank">Github</a> uses port 9418.  This link discusses using tunnels and mentions the port.</p>
<p>After some inspection, I realized that inbound and outbound traffic was blocked by <a title="APF - Advanced Policy Firewall" href="http://www.rfxn.com/?page_id=6" target="_blank">APF</a> on port 9418.  A quickie modification to the EG_TCP_CPORTS and IG_TCP_CPORTS values by adding 9418 and restarting the APF service managed to do the trick.</p>
<p>This is definitely not relegated only to<a title="CentOS - The Community ENterprise Operating System" href="http://www.centos.org/" target="_blank"> CentOS</a> or to systems running APF when trying to clone from Github.  Any Linux system could be subject to these timeout issues against Github if your firewall is configured to block 9418. So if you are seeing socket connection issues, or fatal errors with fetch-pack, you might just want to check your firewall.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erikjacobs.com/2009/09/13/git-socket-timeout-issues-with-centos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom (dynamic) error pages with Ruby on Rails</title>
		<link>http://www.erikjacobs.com/2009/08/05/custom-dynamic-error-pages-with-ruby-on-rails/</link>
		<comments>http://www.erikjacobs.com/2009/08/05/custom-dynamic-error-pages-with-ruby-on-rails/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 18:13:00 +0000</pubDate>
		<dc:creator>Erik Jacobs</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.erikjacobs.com/?p=101</guid>
		<description><![CDATA[One thing that bugged me with Riding Resource was error pages. Sure, Rails allows you to create static 404 and 500 and other pages for those situations when things go awry. But the fact that those pages were static caused me some heartburn.]]></description>
			<content:encoded><![CDATA[<p><a title="Ruby on Rails" href="http://www.rubyonrails.org" target="_blank">Ruby on Rails</a> is pretty neat stuff. Whenever I try to find out how to do something, it seems that I&#8217;m not the first to look. And, fortunately, many have usually solved that problem before. One thing that bugged me with <a title="Riding Resource - A Stable Place to Search" href="http://www.ridingresource.com" target="_blank">Riding Resource</a> was error pages. Sure, Rails allows you to create static 404 and 500 and other pages for those situations when things go awry. But the fact that those pages were static caused me some heartburn.</p>
<p>For one, if the layout of the website changed, it meant I needed to update the error pages. This is certainly not <a title="Don't repeat yourself" href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" target="_blank">DRY</a>. And, using static pages, I could not use any <a title="Ruby - A programmer's best friend" href="http://www.ruby-lang.org" target="_blank">Ruby</a> code in my error page, or do anything dynamically at all.</p>
<p>After some quick searching, I came across <a title="Rails 2.2 Custom Error Pages With Exception Loggable" href="http://blog.ubrio.us/code/rails-22-custom-error-pages-with-exception-loggable/" target="_blank">this post</a> by Rob Hurring which lead me to <a title="Rescue from dispatching" href="http://m.onkey.org/2008/7/20/rescue-from-dispatching" target="_blank">this post</a> on has_many :bugs, :through =&gt; :rails regarding how to create customized error pages with Ruby on Rails. Granted, neither of these solutions was exactly what I was looking for, so a little customization was required. However, the basic requirements were met.</p>
<ol>
<li>we can see that we can <a title="Module: ActiveSupport::Rescuable::ClassMethods" href="http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html#M001002" target="_blank">rescue_from</a> many of the standard <a title="Module: ActionController::Rescue" href="http://api.rubyonrails.org/classes/ActionController/Rescue.html" target="_blank">ActionController</a> errors.</li>
<li>using :with, we can specify a method to invoke to process the rescue activity</li>
<li>the method called to rescue us can render a page with a layout</li>
</ol>
<p>This took care of everything we needed. For almost all of the error types that would arise, we could redirect to a custom 404 template that allowed for Ruby to be embedded that would use any number of existing layouts, which keeps things tight and DRY.</p>
<p>One caveat that we had particularly related to <a title="Riding Resource - A Stable Place to Search" href="http://www.ridingresource.com" target="_blank">Riding Resource</a> was that meta tags are currently generated on the fly by a view helper, but we are actually looking at the current params to determine what controller/action we are in, and, therefore, what meta tags to spit out. Unfortunately, I could not find a way to detect, within the template, what status the template had been rendered with.</p>
<p>I ended up creating an instance variable before rendering the error template:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>.<span style="color:#9900CC;">consider_all_requests_local</span>
  <span style="color:#008000; font-style:italic;"># yeah, its a long line</span>
  rescue_from <span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span>, <span style="color:#6666ff; font-weight:bold;">ActionController::RoutingError</span>, <span style="color:#6666ff; font-weight:bold;">ActionController::UnknownController</span>, <span style="color:#6666ff; font-weight:bold;">ActionController::UnknownAction</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:render_404</span>
  rescue_from <span style="color:#CC00FF; font-weight:bold;">RuntimeError</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:render_500</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
protected
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> render_404
  <span style="color:#0066ff; font-weight:bold;">@status</span> = <span style="color:#996600;">&quot;404&quot;</span>
  render <span style="color:#ff3333; font-weight:bold;">:template</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;shared/404&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:not_found</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>By creating this instance variable, I could then check for the value of @status in my meta tag generator and properly handle things.</p>
<p>One caveat to this solution, in general, is that errors that occur with your process of rendering the custom page will result in no errors being shown at all, except for in the log. Note Ron&#8217;s trick to be able to generate the custom error pages even while in development mode.</p>
<p>In the long term I will probably clean up the meta tag generator to create instance variables and then use partials to get the information into the view, but that&#8217;s for another day. We also recently added <a title="Paperclip - Manage Uploaded Files Without Hassle" href="http://www.thoughtbot.com/projects/paperclip" target="_blank">Thoughtbot&#8217;s Paperclip</a> plugin to be able to easily attach pictures of facilities, but that post will be made once we get watermarking working.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erikjacobs.com/2009/08/05/custom-dynamic-error-pages-with-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Internal Analytics with Open Flash Chart</title>
		<link>http://www.erikjacobs.com/2009/03/28/internal-analytics-with-open-flash-chart/</link>
		<comments>http://www.erikjacobs.com/2009/03/28/internal-analytics-with-open-flash-chart/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 14:01:00 +0000</pubDate>
		<dc:creator>Erik Jacobs</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.erikjacobs.com/?p=90</guid>
		<description><![CDATA[We wanted to be able to get some analytics for the various facilities on RidingResource, and that required some thinking. While Google Analytics is certainly great, and we use it heavily, there are some things that it can't capture that are valuable data to both us and our customers.]]></description>
			<content:encoded><![CDATA[<p>We wanted to be able to get some analytics for the various facilities on <a title="Riding Resource - A Stable Place to Search" href="http://www.RidingResource.com" target="_blank">RidingResource</a>, and that required some thinking. While <a title="Google Analytics - Win customers, not just visitors." href="http://www.google.com/analytics/" target="_blank">Google Analytics</a> is certainly great, and we use it heavily, there are some things that it can&#8217;t capture that are valuable data to both us and our customers.</p>
<p>Since <a title="Riding Resource - A Stable Place to Search" href="http://www.RidingResource.com" target="_blank">RidingResource</a> is essentially a search engine, we realized that there was value in knowing how often a facility&#8217;s listing &#8220;came up,&#8221; either by being directly viewed on it&#8217;s detail page, or also by being seen in the search results page. Since we also built an API for a partner, which we&#8217;ll announce publicly once it goes live, we thought it would be valuable to track API &#8220;hits&#8221; as well.</p>
<p>Creating the table to store the analytics data was relatively simple. We just created an Analytic model and connected it to the Contact model &#8211; Contact is the model that stores the basic information about facilities listed on RidingResource.  We use <a title="Rails Single Table Inheritance" href="http://www.juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/" target="_blank">Single Table Inheritance</a> (STI) for the different types of facilities listed, but that&#8217;s for another posting.</p>
<p>We realized that there were not a lot of fields necessary for the analytics table. Since the analytics were connected to a contact, we needed to store the contact ID. Since we identified three different types of analytics, we store an integer for the type field, which we may make into an actual model later.</p>
<p>Lastly, we decided it would be valuable to store the parameters that were used at the time to cause this listing to be displayed. It&#8217;s entirely possible that there may some more valuable data that we could search on later, so knowing the params of the &#8220;hit&#8221; could be valuable.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Analytic <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:contact</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> CreateAnalytics <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:analytics</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:contact_id</span>
      t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:parameters</span>
      t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:analytic_type</span>
      t.<span style="color:#9900CC;">timestamps</span>
    <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;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:analytics</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><a title="Ruby on Rails" href="http://www.rubyonrails.org" target="_blank">Ruby on Rails</a> is kind enough to automatically store the params for us as serialized <a title="YAML: human-readable data serialization" href="http://en.wikipedia.org/wiki/YAML" target="_blank">YAML</a>. This way, when we want to actually process and dissect them later, we can simply do the following to get the params hash back the way we need it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@the_analytics</span> = Analaytic.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:some_conditions</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#0066ff; font-weight:bold;">@the_params</span> = <span style="color:#CC00FF; font-weight:bold;">YAML</span>::<span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span>@the_analytics<span style="color:#006600; font-weight:bold;">&#91;</span>some_specific_one<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">parameters</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>One thing that needed to be carefully considered was storing &#8220;hits&#8221; on the search results pages. Because the database is currently a little bit nasty, we&#8217;re ending up finding all entries in the DB that match a subset of criteria, and then filtering out the rest that don&#8217;t match the remaining criteria. This is actually faster than all the weird joins that end up occuring. After that, there is still the matter of pagination. It&#8217;s entirely possible that a facility could be pulled from the DB several times without actually being displayed, so we couldn&#8217;t just assume that pulling from the DB in the results page was a hit.</p>
<p>What I realized was that <a title="Mislav's will_paginate" href="http://wiki.github.com/mislav/will_paginate" target="_blank">Mislav&#8217;s will_paginate</a> does something nice for us &#8211; it ends up lopping off all the records outside the pagination range, and leaves us with the few for the current page. This enabled us to simply iterate over the paginated records and store the hits.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@contacts</span> = filter_results<span style="color:#006600; font-weight:bold;">&#40;</span>@contact<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#0066ff; font-weight:bold;">@contacts</span> = <span style="color:#0066ff; font-weight:bold;">@contacts</span>.<span style="color:#9900CC;">paginate</span><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> params<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>, <span style="color:#ff3333; font-weight:bold;">:per_page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">8</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#0066ff; font-weight:bold;">@contacts</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>contact<span style="color:#006600; font-weight:bold;">|</span>
  contact.<span style="color:#9900CC;">analytics</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> Analytic.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:contact_id <span style="color:#006600; font-weight:bold;">=&gt;</span> contact.<span style="color:#9900CC;">id</span>, <span style="color:#ff3333; font-weight:bold;">:parameters</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> params, <span style="color:#ff3333; font-weight:bold;">:analytic_type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Storing the hits for the detail page and for the API was trivial. There&#8217;s only one record to grab on the detail page, so obviously someone is looking at it &#8211; hit. Since we don&#8217;t know what the people at the other end of the API are actually doing with the data, all we can do is record that a record was provided to the API.</p>
<p>So now that we&#8217;re storing the analytics, how the heck do we display them? That&#8217;s where <a title="Open Flash Chart" href="http://teethgrinder.co.uk/open-flash-chart-2/" target="_blank">Open Flash Chart</a> comes into play. Unfortunately, this turned out to initially be a nightmare for many reasons.</p>
<p>When we first started building <a title="Riding Resource - A Stable Place to Search" href="http://www.RidingResource.com" target="_blank">RidingResource</a>, I was certainly a rails noob. Not that I am by any means not a noob at this point, but at least I am a little more polished since those early days of not knowing how to do anything. Because I was busy fighting everything at that point, I decided to save myself some headache for the administration area and use <a title="ActiveScaffold - A Rails plugin for dynamic, AJAX CRUD interfaces" href="http://www.activescaffold.com/" target="_blank">Active Scaffold</a>.</p>
<p><a title="ActiveScaffold - A Rails plugin for dynamic, AJAX CRUD interfaces" href="http://www.activescaffold.com/" target="_blank">Active Scaffold</a> certainly is a nice plugin. It does have a tendency to throw wrenches into the works on occasion because it does some strange things using the <a title="Prototype Javascript Framework" href="http://www.prototypejs.org/" target="_blank">Prototype</a> javascript libraries. My first crack at graphing data was to take a look at <a title="flot - Attractive Javascript plotting for jQuery" href="http://code.google.com/p/flot/" target="_blank">Flot</a> because it seemed simple and could do the basic things we wanted. The <a title="flot - Attractive Javascript plotting for jQuery" href="http://code.google.com/p/flot/" target="_blank">Flot</a> plugin I found (<a title="Joshua Miller's Flotilla" href="http://github.com/joshuamiller/flotilla/tree/master" target="_blank">Flotilla</a>) wanted to use <a title="jQuery Javascript Library" href="http://jquery.com/" target="_blank">jQuery</a> via <a title="jRails is a jQuery replacement for Prototype/script.aculo.us" href="http://ennerchi.com/projects/jrails" target="_blank">JRails</a> (don&#8217;t think <a title="JRuby is a 100% pure-Java implementation of Ruby" href="http://jruby.codehaus.org/" target="_blank">JRuby</a>) which interfered with the <a title="Prototype Javascript Framework" href="http://www.prototypejs.org/" target="_blank">Prototype</a> implementation of <a title="ActiveScaffold - A Rails plugin for dynamic, AJAX CRUD interfaces" href="http://www.activescaffold.com/" target="_blank">Active Scaffold</a>. Since the initial graphing was for our admin area, this was out.</p>
<p>After some pondering and question asking in the #rubyonrails channel on <a title="freenode IRC network" href="http://freenode.net/" target="_blank">Freenode</a> (you can find me there as <em>thoraxe</em>), a few other suggestions came up. <a title="Scruffy is a Ruby library for generating attractive and powerful graphs, useful for web applications, print media and many others." href="http://scruffy.rubyforge.org/" target="_blank">Scruffy</a> and <a title="The Gruff Graphing Library is a project to make beautiful graphs with Ruby. " href="http://nubyonrails.com/pages/gruff" target="_blank">Gruff</a> were suggested, but these both used <a title="SVG specifications for two-dimensional vector graphics" href="http://en.wikipedia.org/wiki/Scalable_Vector_Graphics" target="_blank">Scalable Vector Graphics</a>. While Firefox supports these today, I was informed that IE does not without a plugin. Our customer base is mostly going to be IE people, and probably not the most tech-savvy. In case these analytics became customer-facing, I did not want to have to worry about teaching non-tech-savvy people how to install browser plugins for IE. Scruffy and Gruff = out.</p>
<p>Next came the flash implementations, of which there were two notable ones. The first I will mention is <a title="Ziya - charting the rails" href="http://ziya.liquidrail.com/" target="_blank">Ziya</a>, although we did not ultimately choose it. <a title="Ziya - charting the rails" href="http://ziya.liquidrail.com/" target="_blank">Ziya</a> charts certainly are sexy, but for some reason I decided implementation looked difficult and that the charts were a little bit of overkill for what we needed.</p>
<p>Enter <a title="Open Flash Chart" href="http://teethgrinder.co.uk/open-flash-chart-2/" target="_blank">Open Flash Chart</a>, our savior. Well, in the end the savior. It was a hell of a headache getting it to work.</p>
<p>There are several implementations of <a title="Open Flash Chart" href="http://teethgrinder.co.uk/open-flash-chart-2/" target="_blank">Open Flash Chart</a> in <a title="Ruby on Rails" href="http://www.rubyonrails.org" target="_blank">Ruby on Rails</a>. I have to say that, to a certain extent, all of them are a little sucky. Don&#8217;t get me wrong &#8211; it&#8217;s unfair for me to complain about free code that could make me money! But there is something to say about the cleanliness and simplicity of <a title="Rick Olson is techno weenie" href="http://techno-weenie.net/" target="_blank">Technoweenie&#8217;s</a> code when compared to some of these plugins.</p>
<p><a title="Open Flash Chart" href="http://teethgrinder.co.uk/open-flash-chart-2/" target="_blank">Open Flash Chart</a> is smart. It is a flash file that you basically feed <a title="JavaScript Object Notation" href="http://en.wikipedia.org/wiki/JSON" target="_blank">JSON</a> data to generate charts. That makes it simple. Unless you are using the <a title="JSON implementation for Ruby" href="http://json.rubyforge.org/" target="_blank">JSON gem</a> already. Which we are because it is used by <a title="Ruby wrapper for the Twitter Search API" href="http://github.com/dancroak/twitter-search/tree/master" target="_blank">dancroak&#8217;s twitter_search</a> plugin. Which makes things <em>insane</em>. Remember how <a title="ActiveScaffold - A Rails plugin for dynamic, AJAX CRUD interfaces" href="http://www.activescaffold.com/" target="_blank">Active Scaffold</a> was interfering with <a title="flot - Attractive Javascript plotting for jQuery" href="http://code.google.com/p/flot/" target="_blank">Flot</a>? Well, here we were again. Something I was already using interfering with something I wanted to do.</p>
<p>To make a long story short, after much headache surrounding various <a title="Open Flash Chart" href="http://teethgrinder.co.uk/open-flash-chart-2/" target="_blank">Open Flash Chart</a> plugins that used Rails&#8217; built-in JSONification, one of the plugins that is mentioned on the OFC webpage happened to use the <a title="JSON implementation for Ruby" href="http://json.rubyforge.org/" target="_blank">JSON gem</a> itself. Perfect!</p>
<p><a title="Open Flash Chart 2 rails plugin" href="http://github.com/korin/open_flash_chart_2_plugin/tree/master" target="_blank">Korin&#8217;s Open Flash Chart 2</a> plugin did the trick. I won&#8217;t go into the implementation of everything in its entirety, but I will share the following bits which you may or may not find useful.</p>
<p>Korin&#8217;s examples use two controller actions to generate the graph. The first action creates the @graph object which basically just stores the string which represents some code that the swfobject javascript library uses to create the proper html to display the openflashchart.swf.  The second action actually generates the JSON that gets fed into the SWF.</p>
<p>One of the other plugins I had found that did not work for me (because of the previously discussed <a title="JSON implementation for Ruby" href="http://json.rubyforge.org/" target="_blank">JSON gem</a> issues) was <a title="Pullmonkey's Open Flash Chart plugin" href="http://github.com/pullmonkey/open_flash_chart/tree/master" target="_blank">Pullmonkey&#8217;s Open Flash Chart</a> plugin. Pullmonkey did something neat using the <a title="Module: ActionController::MimeResponds::InstanceMethods" href="http://api.rubyonrails.org/classes/ActionController/MimeResponds/InstanceMethods.html" target="_blank">respond_to</a> method of MimeResponds in ActionController.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> show
  <span style="color:#008000; font-style:italic;"># find the contact requested</span>
  <span style="color:#0066ff; font-weight:bold;">@contact</span> = Contact.<span style="color:#9900CC;">find_by_url</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>wants<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#0066ff; font-weight:bold;">@all_results</span> = Analytic.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:contact_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@contact</span>.<span style="color:#9900CC;">id</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#0066ff; font-weight:bold;">@data_results</span> = Analytic.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:analytic_type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#ff3333; font-weight:bold;">:contact_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@contact</span>.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#0066ff; font-weight:bold;">@data_details</span> = Analytic.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:analytic_type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span>, <span style="color:#ff3333; font-weight:bold;">:contact_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@contact</span>.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#0066ff; font-weight:bold;">@data_api</span> = Analytic.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:analytic_type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span>, <span style="color:#ff3333; font-weight:bold;">:contact_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@contact</span>.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    wants.<span style="color:#9900CC;">html</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># set up the graph on the request</span>
      <span style="color:#0066ff; font-weight:bold;">@graph_results</span> = ofc2<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">650</span>,<span style="color:#006666;">300</span>,url_for<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:show</span>, <span style="color:#ff3333; font-weight:bold;">:format</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:json</span>, <span style="color:#ff3333; font-weight:bold;">:graphtype</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:results</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@graph_details</span> = ofc2<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">650</span>,<span style="color:#006666;">300</span>,url_for<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:show</span>, <span style="color:#ff3333; font-weight:bold;">:format</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:json</span>, <span style="color:#ff3333; font-weight:bold;">:graphtype</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:details</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@graph_api</span> = ofc2<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">650</span>,<span style="color:#006666;">300</span>,url_for<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:show</span>, <span style="color:#ff3333; font-weight:bold;">:format</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:json</span>, <span style="color:#ff3333; font-weight:bold;">:graphtype</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:api</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    wants.<span style="color:#9900CC;">json</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#008000; font-style:italic;"># provide the JSON back to the flash</span>
      <span style="color:#008000; font-style:italic;"># call the function to generate the graph based on the graph type that is supplied via params</span>
      render <span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> results_graph.<span style="color:#9900CC;">render</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>This implementation is a little more elegant, but I&#8217;m actually finding that, because both the HTML and <a title="JavaScript Object Notation" href="http://en.wikipedia.org/wiki/JSON" target="_blank">JSON</a> generation are happening inside the same controller action, some of my finds are being performed multiple times. This is because the records for these finds are needed by both parts of the action, but the action gets called each time any part of the respond_to gets called. I may end up de-elegantizing this and splitting it back into multiple actions if it becomes a performance issue.</p>
<p>In determining what to do with the analytics data, we decided that initially it made sense to simply graph hits by day. Since we cheated in our creation of the analytics table and used the built in timestamps, we already had a created_at field which contained the <a title="DateTime Ruby class" href="http://www.ruby-doc.org/core-1.9/classes/DateTime.html" target="_blank">DateTime</a> of the hit. If you look at that last sentence carefully, you realize that <a title="DateTime Ruby class" href="http://www.ruby-doc.org/core-1.9/classes/DateTime.html" target="_blank">DateTime</a> is not <a title="Date Ruby class" href="http://www.ruby-doc.org/core/classes/Date.html" target="_blank">Date</a>. So how do you lop off the time part? This also still leaves the trouble of calculating how many hits occured on each day, too. This is where the elegance of <a title="Ruby programming language" href="http://www.ruby-lang.org/en/" target="_blank">Ruby</a> really shines.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">line_values = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
x_labels_text = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
instance_variable_get<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@data_#{params[:graphtype]}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">group_by</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">ordinal</span><span style="color:#006600; font-weight:bold;">&#40;</span>a.<span style="color:#9900CC;">created_at</span>.<span style="color:#9900CC;">year</span>, a.<span style="color:#9900CC;">created_at</span>.<span style="color:#9900CC;">yday</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>day, results<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#008000; font-style:italic;"># this will group all of the analytic hits together by day instead of date/time. it then iterates over</span>
  <span style="color:#008000; font-style:italic;"># these results in a block, where day holds the value of the day of the results, and results is an array</span>
  <span style="color:#008000; font-style:italic;"># containing the individual results from that day.</span>
&nbsp;
  x_labels_text <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> day.<span style="color:#9900CC;">to_s</span> <span style="color:#008000; font-style:italic;"># put the value of the day into the x axis label</span>
  line_values  <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> results.<span style="color:#9900CC;">length</span> <span style="color:#008000; font-style:italic;"># how many hits occured on this day</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>group_by, ordinal, and iterating over blocks totally saved our butts here. What the above code enabled us to do was to group the entire array of analytics data by the day (after munching the time off using ordinal), and then iterate over the resulting groups. The blocks allow us to both store the day into the array of labels for the x-axis, as well as determine how many hits occured on that day by using the length of the array of data in the group. Brilliant!</p>
<p>As you can see, what started out as a relatively simple idea (&#8220;Let&#8217;s graph some analytics about the facilities on <a title="Riding Resource - A Stable Place to Search" href="http://www.RidingResource.com" target="_blank">RidingResource</a>!&#8221;) ended up being a relatively non-trivial coding exercise that took us almost a solid day of man-hours. But, in the end, we were left with something simple that did the job, but which leaves us a lot of room for growth and power.</p>
<p>The only real pain point right now has to do with the actual analytics data. If a day goes by without any hits, nothing gets stored in the database. Since we are grouping by the records that we actually pull out of the database, any dates in the middle with no hits will not be represented. So we are left with the problem of how to determine what dates are &#8220;in the middle&#8221; that have no hits. It&#8217;s not an issue right now, but it may become one in the future. I&#8217;m sure we&#8217;ll be able to figure it out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erikjacobs.com/2009/03/28/internal-analytics-with-open-flash-chart/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CAPTCHA in Rails &#8211; an experiment in anti-spam</title>
		<link>http://www.erikjacobs.com/2009/03/23/captcha-in-rails-an-experiment-in-anti-spam/</link>
		<comments>http://www.erikjacobs.com/2009/03/23/captcha-in-rails-an-experiment-in-anti-spam/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 01:45:12 +0000</pubDate>
		<dc:creator>Erik Jacobs</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.erikjacobs.com/?p=88</guid>
		<description><![CDATA[One of the things that we decided would be a good idea for RidingResource was to let users of the site contact the various facilities that we have listed. We quickly ran into an issue with spam.]]></description>
			<content:encoded><![CDATA[<p>One of the things that we decided would be a good idea for <a title="Riding Resource - A Stable Place to Search" href="http://www.RidingResource.com" target="_blank">RidingResource</a> was to let users of the site contact the various facilities that we have listed. We also wanted to make sure those facilities knew we helped facilitate that contact by injecting some extra information into the email. The email functionality is left for another post.</p>
<p>We quickly ran into an issue with spam. As administrators of the site, we get copies of all of the email that is sent to facilities. We noticed that some weird spam-like email came through. Fortunately it was someone trying to exploit our site as an open mailer, but it didn&#8217;t seem to work, and no email got to our customer. We will have to investigate how to help prevent that (if it&#8217;s even possible to exploit) later. But we knew that we needed to do something to prevent spammers from being able to send automated junk mail to our customers, and <a title="CAPTCHA challenge-response" href="http://en.wikipedia.org/wiki/Captcha" target="_blank">CAPTCHA</a> seemed like a good idea.</p>
<p>Until I tried it.</p>
<p>After some quick <a title="Google" href="http://www.google.com" target="_blank">Googling</a> for &#8220;rails captcha&#8221; and other terms, I discovered the <a title="Rails simple_captcha plugin" href="http://expressica.com/simple_captcha/" target="_blank">simple_captcha</a> plugin. This is a handy plugin that can be used to generate a CAPTCHA image with some convenient options. It also offers a friendly validation of said CAPTCHA in your controllers, amongst other things.</p>
<p>One &#8220;issue&#8221; that I had with simple_captcha is that it requires both the <a title="ImageMagick: Convert, Edit and Compose Images" href="http://www.imagemagick.org/script/index.php" target="_blank">Imagemagick</a> image manipulation program to be installed as well as the <a title="Graphics Processing for Ruby and Ruby on Rails" href="http://rmagick.rubyforge.org/" target="_blank">RMagick</a> gem. The first part was already present, as I was using the <a title="Mini Magick" href="http://rubyforge.org/projects/mini-magick/" target="_blank">mini_magick</a> gem for another <a title="Ruby on Rails" href="http://www.rubyonrails.org" target="_blank">Rails</a> application. Unfortunately, I didn&#8217;t have RMagick, and installing it proved less than trivial.</p>
<p>First, trying to install the RMagick gem resulted in an error:</p>
<p>Can&#8217;t install RMagick 2.9.1. Can&#8217;t find Magick-config in /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin</p>
<p>This got me to poking around. Now, most of the tutorials I had found referenced Debian as the Linux distribution onto which people were using simple_captcha. I happen to be running <a title="Fedora, a Linux-based Operating System" href="http://fedoraproject.org/" target="_blank">Fedora</a> as a development server and <a title="CentOS - The Community ENterprise Operating System" href="http://www.centos.org/" target="_blank">CentOS</a> in production. This meant things were a little different. After some creative Googling, I discovered what was required to install <a title="How to install RMagick on CentOS" href="http://www.cherpec.com/2008/08/howto-install-rmagick-on-centos-4/" target="_blank">RMagick on CentOS</a>. The current version of RMagick is actually 1.15.17 &#8211; slightly newer than the one referenced in the tutorial.</p>
<p>In the end, the tutorials on the <a title="Rails simple_captcha plugin" href="http://expressica.com/simple_captcha/" target="_blank">simple_captcha</a> website were sufficient to get what we needed going. Since we already had a contact form, it really was less than 15 new lines of code to get things up and running. I&#8217;m not as pleased as I could be with the CSS and formatting of where the CAPTCHA is on the contact form, but it looks good enough for now.</p>
<p>If you&#8217;ve got some contact forms and you&#8217;re concerned about spam, or you&#8217;ve got some registration forms that you want to anti-robot, give simple_captcha a try.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erikjacobs.com/2009/03/23/captcha-in-rails-an-experiment-in-anti-spam/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Building an API in Ruby on Rails</title>
		<link>http://www.erikjacobs.com/2009/03/05/building-an-api-in-ruby-on-rails/</link>
		<comments>http://www.erikjacobs.com/2009/03/05/building-an-api-in-ruby-on-rails/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 16:31:50 +0000</pubDate>
		<dc:creator>Erik Jacobs</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.erikjacobs.com/?p=84</guid>
		<description><![CDATA[I know there are quite a few tutorials and links out there on building an API for your Ruby on Rails application, but I figured that I would document a little bit about how it was done for Riding Resource, but at a high level.  A partner had requested access to some of our data via an API, and wanted the results to be spit out as XML, so here's a little bit about how that was done.]]></description>
			<content:encoded><![CDATA[<p>I know there are quite a few tutorials and links out there on building an <a title="Application Programming Interface" href="http://en.wikipedia.org/wiki/API" target="_blank">API</a> for your <a title="Ruby on Rails" href="http://www.rubyonrails.org/" target="_blank">Ruby on Rails</a> application, but I figured that I would document a little bit about how it was done for <a title="Riding Resource - A Stable Place to Search" href="http://www.ridingresource.com" target="_blank">Riding Resource</a>, but at a high level.  A partner had requested access to some of our data via an API, and wanted the results to be spit out as <a title="Extensible Markup Language" href="http://en.wikipedia.org/wiki/XML" target="_blank">XML</a>, so here&#8217;s a little bit about how that was done.</p>
<p>First things first, it took me a little while to figure out exactly how to handle the whole XML thing, but it was actually far easier than I originally had thought.  Rails is smart enough to look for lots of different file types that match the action, so in the case of our API, all I had to do was make sure I had a .rxml (as opposed to .rhtml) file that matched the action.</p>
<p>Second, I wanted to restrict use of the API to authorized parties.  Just like how <a title="Google" href="http://www.google.com" target="_blank">Google</a> has API keys, I decided I wanted to do something similar.  Since I didn&#8217;t want to take the time to create some crazy key system that reverse-lookups the requestor and does stuff like that, I thought it might be a little easier to simply use the key to specify a legal set of requestor IP ranges/addresses.</p>
<p>Lastly, if the API request wasn&#8217;t from a legal requestor, I wanted to return nothing.</p>
<p>Here&#8217;s what I did (pseudocody) for the search controller</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ApiController <span style="color:#006600; font-weight:bold;">&amp;</span>lt; ApplicationController
  <span style="color:#008000; font-style:italic;"># api ip address ranges allowed</span>
  API_KEYS = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;5b6ba960531c458021e8be98f3842c182c773b2f&quot;</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'192.168.2.0/24'</span>, <span style="color:#996600;">'aaa.bbb.ccc.ddd'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> search
    <span style="color:#9966CC; font-weight:bold;">for</span> ip <span style="color:#9966CC; font-weight:bold;">in</span> API_KEYS<span style="color:#006600; font-weight:bold;">&#91;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:key</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> IPAddr.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>ip<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>IPAddr.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>request.<span style="color:#9900CC;">remote_ip</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0066ff; font-weight:bold;">@barns</span> = Barn.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:stuff</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0066ff; font-weight:bold;">@count</span> = <span style="color:#0066ff; font-weight:bold;">@barns</span>.<span style="color:#9900CC;">length</span>
        <span style="color:#0000FF; font-weight:bold;">return</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:blank</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Some interesting things to note here. <a title="Class: IPAddr" href="http://www.ruby-doc.org/core/classes/IPAddr.html" target="_blank"> IPAddr</a> is a nice ruby class that allows us to perform manipulations on IP addresses, one of which is checking if an address is included in a range.  You have to require the ipaddr class in your environment in order to use it &#8212; it is not loaded by default.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">for</span> ip <span style="color:#9966CC; font-weight:bold;">in</span> API_KEYS<span style="color:#006600; font-weight:bold;">&#91;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:key</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> IPAddr.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>ip<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>IPAddr.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>request.<span style="color:#9900CC;">remote_ip</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Here we are iterating over all the ip ranges and IPs that are in the hash associated with the key provided by the incoming params.  For each IP range/address, we check to see if the requesting IP is included in the range.  Fortunately, IPAddr is smart enough to know that a single IP is included within itself, so using IP addresses as opposed to ranges is perfectly acceptable.</p>
<p>If the IP is within one of the ranges, we perform our find.  If not, we render the blank action.  The blank action has no code, and blank.rxml is empty as well.  This way, if an illegal request comes in, we just do nothing &#8212; I don&#8217;t care to cater to people trying to access the API that shouldn&#8217;t be.</p>
<p>The XML part was tricky at first, but it actually turned out to be far simpler than I thought.  Once you are in an XML view, Rails is kind enough to provide an xml object already for you, without needing to instantiate anything.  This seemed to be contrary to a few tutorials I had found.  Here&#8217;s some pseudocode to represent what i did:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">xml.instruct! :xml, :version =&gt; &quot;1.0&quot;
xml.barns do |barn|
  xml.count @count
  xml.requestor request.remote_ip
  xml.api_key params[:key]
  xml.requested_location params[:zip]
  xml.requested_distance params[:dist]
  for b in @barns do
    xml.barn do
      xml.name b.name
      xml.address b.address
      xml.distance b.distance
      xml.phone b.phone
      xml.website b.website
      xml.url &quot;http://www.ridingresource.com/contact/show/#{b.url}&quot;
    end
  end
end</pre></div></div>

<p>Because XML is a markup language that requires properly open and closed &#8220;containers&#8221; similar to HTML, you can see there are a lot of do/end blocks.  The main do.end block is the barns block, which contains all of our results.  I also was kind enough to let the API user know some of the things they asked of us, as well as the number of results we found.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">xml.barns do |barn|
  for b in @barns do
    xml.barn do
      xml.something value
    end
  end
end</pre></div></div>

<p>As we iterate over each result in @barns, we want to create an xml container for each one.  The ease of Rails/Ruby here is awesome &#8212; you simply specify the container name, and then the value: xml.something value</p>
<p>The resultant XML output looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;barns<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;count<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/count<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;requestor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>aaa.bbb.ccc.ddd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/requestor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;api_key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>key<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/api_key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;requested_location<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>30093<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/requested_location<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;requested_distance<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/requested_distance<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;barn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Camp Creek Stables<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;address<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4150 Arcadia Industrial Circle SW  Lilburn GA, 30047<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/address<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;distance<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0317320843575<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/distance<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phone<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>7709252402<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phone<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;website<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/website<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.ridingresource.com/contact/show/camp-creek-stables<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/barn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>As you can see, it can be pretty easy to build an XML-returning API using Ruby on Rails.  While this certainly is by no means a tutorial, it can provide some insight if you are a little stuck.</p>
<p>If you are interested in getting access to the Riding Resource API, please be sure to contact me at <a href="mailto:erik@ridingresource.com">erik@ridingresource.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.erikjacobs.com/2009/03/05/building-an-api-in-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fun with jqModal, flash, sending email and submission forms in Rails</title>
		<link>http://www.erikjacobs.com/2009/02/08/fun-with-jqmodal-flash-sending-email-and-submission-forms-in-rails/</link>
		<comments>http://www.erikjacobs.com/2009/02/08/fun-with-jqmodal-flash-sending-email-and-submission-forms-in-rails/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 05:05:33 +0000</pubDate>
		<dc:creator>Erik Jacobs</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.erikjacobs.com/?p=74</guid>
		<description><![CDATA[As many of you may already know, Riding Resource has been up and running since January 1st, and we're chugging right along. One thing we realized we wanted was to have people be able to send us their contact information to sign up for a mailing list.]]></description>
			<content:encoded><![CDATA[<p>As many of you may already know, <a title="Riding Resource - A Stable Place to Search" href="http://www.RidingResource.com" target="_blank">Riding Resource</a> has been up and running since January 1st, and we&#8217;re chugging right along. One thing we realized we wanted was to have people be able to send us their contact information to sign up for a mailing list.</p>
<p>Because we haven&#8217;t implemented anything like <a title="Constant Contact" href="http://www.constantcontact.com" target="_blank">Constant Contact</a> or <a title="Mailchimp" href="http://www.mailchimp.com" target="_blank">Mailchimp</a>, we just wanted a submission form to collect the information and receive it via email.  We didn&#8217;t want to require people to create an email themselves. Because we are anal retentive and want everything to look pretty, we also decided that it would be really nice to use <a title="Modal Windows" href="http://en.wikipedia.org/wiki/Modal_window" target="_blank">modals</a> to put the form in.</p>
<p>First step &#8211; using modals. Since <a title="Riding Resource - A Stable Place to Search" href="http://www.RidingResource.com" target="_blank">Riding Resource</a> already makes use of a good bit of <a title="jQuery" href="http://jQuery.com" target="_blank">jQuery</a> and other jQuery plugins, I thought it would be rather easy to use a modal plugin for jQuery. Once such plugin happens to exist in <a title="jQmodal" href="http://dev.iceburg.net/jquery/jqModal/" target="_blank">jQmodal</a>. Its usage is rather simple, but, in the case of <a title="Riding Resource - A Stable Place to Search" href="http://www.RidingResource.com" target="_blank">Riding Resource</a>, required a couple of tricks.</p>
<p>First, because of the way that <a title="Ruby on Rails" href="http://www.rubyonrails.org" target="_blank">Ruby on Rails</a> works, I wanted to keep the modal for the submission form on the view for the action that would actually spawn the modal. Unfortunately, this caused a problem with <a title="Firefox Web Browser" href="http://www.mozilla.com/en-US/firefox/" target="_blank">Firefox</a> 2 and the way the layers are handled. This was solved by using the &#8220;toTop&#8221; directive in the declaration of the modal. I also only wanted the modal to trigger when the links to sign up were clicked. This resulted in the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mailing-modal'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">jqm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  trigger<span style="color: #339933;">:</span> <span style="color: #3366CC;">'.mailing'</span><span style="color: #339933;">,</span>
  toTop<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>This brought up the modal just fine. But what to put in it? I had never built a contact form before, although I&#8217;ve obviously built some very complex forms. A quick Google search pointed me to <a title="How to make a simple Contact Form (Rails)" href="http://railsforum.com/viewtopic.php?id=404" target="_blank">this thread</a> about just such a thing. A little bit of tweaking and I had built just the form I needed.</p>
<p>Unfortunately, I realized quickly that after the form was submitted, the user didn&#8217;t get any notification. <a title="Riding Resource - A Stable Place to Search" href="http://www.RidingResource.com" target="_blank">Riding Resource</a> had not had any need for Rails flash messages until this point, so what were we going to do with them? Since we were already using jqModal, why not continue in that trend? Popping up a friendly modal letting the user know their submission was received would surely do the trick.  But this brought its own pitfall &#8211; how to make the modal only display when needed? The following snippet helped solve that problem:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">$('#flashes').jqm({
  trigger: '#flashes'
});
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:warning</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  $('#flashes').jqmShow();
<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>In this case, we are declaring that the #flashes div is a jqModal, and that it should only trigger on itself. This prevents it from popping up when other jqModal triggers are clicked (all modals will pop up by default). The second thing we did was force this modal to show only if the different flashes exist. The div for this looks like so:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&lt;div id=&quot;flashes&quot; class=&quot;jqmWindow&quot;&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= display_standard_flashes <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;p&gt;&lt;a href=&quot;#&quot; class=&quot;jqmClose&quot;&gt;&lt;strong&gt;Close&lt;/strong&gt; X&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;</pre></div></div>

<p>In case you&#8217;re wondering what display_standard_flashes is, it&#8217;s a handy helper that I happened to pick up from dZone <a title="Helper to display Rails flashes" href="http://snippets.dzone.com/posts/show/2348" target="_blank">here</a>.</p>
<p>That&#8217;s pretty much all there was to it. While it took several hours to get all of this figured out, now that it&#8217;s done, it should be much easier to do submission forms using jqModal in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erikjacobs.com/2009/02/08/fun-with-jqmodal-flash-sending-email-and-submission-forms-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cleaning the sessions database store with a rake task in Rails</title>
		<link>http://www.erikjacobs.com/2008/12/24/cleaning-the-sessions-database-store-with-a-rake-task-in-rails/</link>
		<comments>http://www.erikjacobs.com/2008/12/24/cleaning-the-sessions-database-store-with-a-rake-task-in-rails/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 17:58:47 +0000</pubDate>
		<dc:creator>Erik Jacobs</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.erikjacobs.com/?p=71</guid>
		<description><![CDATA[So, for one reason or another, I am using the built-in db store for sessions on the RidingResource site. I was about to embark on a path to utilizing sessions to help maintain the search selections across the site, and I noticed that the sessions database was full of... junk.]]></description>
			<content:encoded><![CDATA[<p>So, for one reason or another, I am using the built-in db store for sessions on the <a title="Riding Resource - A Stable Place to Search" href="http://www.ridingresource.com" target="_blank">RidingResource</a> site. I was about to embark on a path to utilizing sessions to help maintain the search selections across the site, and I noticed that the sessions database was full of&#8230; junk.</p>
<p>Rails is nice enough to provide us with a number of great built-in ways to handle sessions. Unfortunately, it is not nice enough to clean up after itself. I noticed that there is a built-in rake task (rake db:sessions:clear), but this actually just nukes the whole sessions table. Now, granted, I could run this as a cron at 4:00AM and it would probably not affect any users. But, I figured I should design a way to simply not affect any users regardless.</p>
<p>I found <a href="http://rails.wincent.com/wiki/Clearing_out_old_Rails_session_records_from_a_MySQL_database" target=_blank alt="Clearing out old Rails session records from a MySQL database">someone</a> else who was trying to clean up session data and they figured out a MySQL query that works: DELETE FROM sessions WHERE updated_at &lt; DATE_SUB(CURDATE(), INTERVAL 30 DAY); This will delete any sessions that haven&#8217;t been updated in 30 days. For our app, I decided that a 2-day interval is probably sufficient. The other cute benefit is that someone who comes back within two days will probably see their last search retained &#8212; that may or may not be desireable in the long run.</p>
<p>Since I had just written a rake task the other day to handle some stuff relating to paid advertisers and what not, I realized that I already had a tool that could (potentially) be used to process this MySQL query &#8212; rake. However, that required figuring out a way to run a MySQL query directly with rake.</p>
<p>Again, Rails to the rescue &#8212; it provides us with ActiveRecord::Base.connection.execute(sql). So, I created a rake task to do what was needed.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># clearing out sessions older than 2 days because we are using sessions to store</span>
<span style="color:#008000; font-style:italic;"># search information to maintain the search preferences in the show action</span>
desc <span style="color:#996600;">&quot;This task will delete sessions that have not been updated in 2 days.&quot;</span>
task <span style="color:#ff3333; font-weight:bold;">:clean_sessions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;DELETE FROM sessions WHERE updated_at &lt; DATE_SUB(CURDATE(), INTERVAL 2 DAY);&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Combined with a cron job, this should work splendidly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erikjacobs.com/2008/12/24/cleaning-the-sessions-database-store-with-a-rake-task-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Actually handling blanking default form input values&#8230; in all browsers</title>
		<link>http://www.erikjacobs.com/2008/12/05/actually-handling-blanking-default-form-input-values-in-all-browsers/</link>
		<comments>http://www.erikjacobs.com/2008/12/05/actually-handling-blanking-default-form-input-values-in-all-browsers/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 17:26:44 +0000</pubDate>
		<dc:creator>Erik Jacobs</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.erikjacobs.com/?p=66</guid>
		<description><![CDATA[The other day I wrote about needing to blank default values when text input fields gain focus in a form.  I had found a jQuery-based script that seemed to do the trick... or so I thought.]]></description>
			<content:encoded><![CDATA[<p>The other day I wrote about needing to blank default values when text input fields gain focus in a form.  I had found a jQuery-based script that seemed to do the trick&#8230; or so I thought.</p>
<p>When <a title="Riding Resource - A Stable Place to Search" href="http://www.ridingresource.com" target="_blank">RidingResource</a> went beta, I was tailing the RoR logs and noticed that &#8220;ZIP&#8221; was getting sent as a value on form submission. After some further prodding, I realized that the <a title="jQuery" href="http://jquery.com" target="_blank">jQuery</a> form script I was using was not working in Internet Explorer. FireFox would have the default values not sent, but for some reason IE was not processing the jQuery in time and the default value in the text input was being sent along and wreaking havoc on the application.</p>
<p>I had to do some quick fixery to get it to work that way, but the search began for a better (read: functional) solution. Enter Jason Palmer and his <a title="Jason Palmer - jQuery Form Field Default Value" href="http://www.jason-palmer.com/2008/08/jquery-plugin-form-field-default-value/" target="_blank">jQuery Form Field Default Value</a> plugin. Not to steal any of Jason&#8217;s thunder, but the plugin is elegant and doesn&#8217;t suffer any issues in Internet Explorer. You should give it a try.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erikjacobs.com/2008/12/05/actually-handling-blanking-default-form-input-values-in-all-browsers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Authenticating all controllers for your Rails application with restful_authentication</title>
		<link>http://www.erikjacobs.com/2008/12/03/authenticating-all-controllers-for-your-rails-application-with-restful_authentication/</link>
		<comments>http://www.erikjacobs.com/2008/12/03/authenticating-all-controllers-for-your-rails-application-with-restful_authentication/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 05:45:04 +0000</pubDate>
		<dc:creator>Erik Jacobs</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.erikjacobs.com/?p=62</guid>
		<description><![CDATA[So while authenticating all your controllers using restful_authentication might seem intuitive, I found it a little tricky. Preliminary Google searching didn't reveal anything inherently obvious. However, robbrit on Freenode was able to lend me a hand and I got it figured out.]]></description>
			<content:encoded><![CDATA[<p>So while this might seem intuitive, I found it a little tricky. Preliminary Google searching didn&#8217;t reveal anything inherently obvious. However, robbrit on Freenode was able to lend me a hand and I got it figured out.</p>
<p>As you might expect, restful_authentication&#8217;s before_filter, :login_required, will direct you to the sessions controller if you are not logged in. So, I initially applied this before_filter to the entire application by placing it as the first line in app/controllers/application_controller.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ApplicationController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
  before_filter <span style="color:#ff3333; font-weight:bold;">:login_required</span></pre></div></div>

<p>Unfortunately, this had unintended consequences. Because this requires login for *all* controllers and *all* actions, we find that we are caught in an endless loop. The login route sends us to /sessions/new, but since we are not logged in, this action tries to again send us to login. Oops!</p>
<p>Rails is kind enough to allow for some exceptions and other fun with filters. So, I initially realized that we should probably add an exception for the &#8220;new&#8221; action, since that is where we are redirected for a login.  We add this exception by re-iterating the before_filter in the app/controllers/sessions_controller.rb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SessionsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  before_filter <span style="color:#ff3333; font-weight:bold;">:login_required</span>, <span style="color:#ff3333; font-weight:bold;">:except</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:new</span></pre></div></div>

<p>Unfortunately, this did not quite cut it entirely. Do not forget that the new action essentially just renders the login form. The create action is where all of the *real* work is done. But, since we did not except create, we end up in a login loop. We can fill out the form, press the &#8220;login&#8221; button, but when we reach the create action we are not logged in. This causes the filter to fire off and send us back to&#8230; you guessed it&#8230; the new action.</p>
<p>Adding an additional exception (and destroy, just in case) provides the results we are looking for:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SessionsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  before_filter <span style="color:#ff3333; font-weight:bold;">:login_required</span>, <span style="color:#ff3333; font-weight:bold;">:except</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;">:new</span>, <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:destroy</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Hopefully all of this stuff will work for you, too. This is just one way I found to authenticate all controllers with restful_authentication as I had a particular application that we wanted to lock down. The extra fun with this type of stuff is that you could put a before_filter on the signup actions. This would have the effect of only allowing a user with an existing account to create new users. This is useful for development lockdown to a certain extent.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erikjacobs.com/2008/12/03/authenticating-all-controllers-for-your-rails-application-with-restful_authentication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
